testing iOS multithread rendering

This commit is contained in:
2019-03-01 10:07:54 +01:00
parent 44ca908c6d
commit 8cd02787b3
4 changed files with 67 additions and 52 deletions

View File

@@ -55,7 +55,7 @@ std::recursive_mutex lock_mutex;
// lock_thread = [NSThread currentThread];
// lock_count++;
[EAGLContext setCurrentContext:self.context];
GLKView* view = (GLKView*)self.view;
GameView* view = (GameView*)self.view;
[view bindDrawable];
}
- (void)async_unlock
@@ -194,7 +194,7 @@ std::recursive_mutex lock_mutex;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
GLKView *view = (GLKView *)self.view;
GameView *view = (GameView *)self.view;
CGRect frame = view.frame;
frame.size.height -= kbSize.height;
view.frame = frame;
@@ -380,7 +380,7 @@ std::set<UITouch*> ignored_touch;
App::I.initLog();
App::I.ios_view = self;
self.preferredFramesPerSecond = 60;
//self.preferredFramesPerSecond = 60;
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (!self.context) {
@@ -389,9 +389,10 @@ std::set<UITouch*> ignored_touch;
gl_lock = [[NSLock alloc] init];
GLKView *view = (GLKView *)self.view;
GameView *view = (GameView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.enableSetNeedsDisplay = NO;
glview = view;
App::I.width = view.frame.size.width * view.contentScaleFactor;
@@ -433,8 +434,59 @@ std::set<UITouch*> ignored_touch;
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
[self async_lock];
App::I.init();
[self async_unlock];
std::thread([self]{
while (true)
{
static auto time = std::chrono::steady_clock::now();
static double elapsed = 0;
static double min_fps_timer = 0;
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - time);
time = now;
elapsed += dt.count();
min_fps_timer += dt.count();
if (min_fps_timer > 0.1)
{
App::I.redraw = true;
min_fps_timer = 0;
}
std::deque<std::packaged_task<void()>> working_list;
if (!tasklist.empty())
{
std::lock_guard<std::mutex> lock(task_mutex);
working_list = std::move(tasklist);
}
App::I.tick(dt.count());
[self async_lock];
if (!(App::I.redraw || App::I.animate || !working_list.empty()))
{
//[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self async_unlock];
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
while (!working_list.empty())
{
working_list.front()();
working_list.pop_front();
}
App::I.clear();
App::I.update(elapsed);
[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self async_unlock];
[self->glview display];
elapsed = 0;
}
}).detach();
}
- (void)tearDownGL
@@ -442,44 +494,8 @@ std::set<UITouch*> ignored_touch;
[EAGLContext setCurrentContext:self.context];
}
#pragma mark - GLKView and GLKViewController delegate methods
@end
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
static auto time = std::chrono::steady_clock::now();
static double elapsed = 0;
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - time);
time = now;
elapsed += dt.count();
std::deque<std::packaged_task<void()>> working_list;
if (!tasklist.empty())
{
std::lock_guard<std::mutex> lock(task_mutex);
working_list = std::move(tasklist);
}
App::I.tick(dt.count());
[self async_lock];
if (!(App::I.redraw || App::I.animate || !working_list.empty()))
{
[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self async_unlock];
return;
}
while (!working_list.empty())
{
working_list.front()();
working_list.pop_front();
}
App::I.clear();
App::I.update(elapsed);
[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self async_unlock];
elapsed = 0;
}
@implementation GameView
@end