X-Git-Url: http://nitlanguage.org diff --git a/lib/gamnit/dynamic_resolution.nit b/lib/gamnit/dynamic_resolution.nit index c0a2081..2c6a4c8 100644 --- a/lib/gamnit/dynamic_resolution.nit +++ b/lib/gamnit/dynamic_resolution.nit @@ -54,6 +54,20 @@ redef class App private var perf_clock_dynamic_resolution = new Clock is lazy + # Real screen framebuffer + private var screen_framebuffer_cache: Int = -1 + + # Real screen framebuffer name + fun screen_framebuffer: Int + do + var cache = screen_framebuffer_cache + if cache != -1 then return cache + + cache = glGetIntegerv(gl_FRAMEBUFFER_BINDING, 0) + self.screen_framebuffer_cache = cache + return cache + end + redef fun create_gamnit do super @@ -68,7 +82,7 @@ redef class App redef fun on_resize(display) do - dynamic_context.resize(display, max_dynamic_resolution_ratio) + if dynamic_context_cache != null then dynamic_context.resize(display, max_dynamic_resolution_ratio) super end @@ -79,12 +93,11 @@ redef class App if dynamic_resolution_ratio == 1.0 then # Draw directly to the screen framebuffer - glBindFramebuffer(gl_FRAMEBUFFER, dynamic_context.screen_framebuffer) + bind_screen_framebuffer screen_framebuffer glViewport(0, 0, display.width, display.height) glClear gl_COLOR_BUFFER_BIT | gl_DEPTH_BUFFER_BIT - var gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR return end @@ -98,8 +111,7 @@ redef class App glClear gl_COLOR_BUFFER_BIT | gl_DEPTH_BUFFER_BIT - var gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR end # Draw the dynamic screen to the real screen if `dynamic_resolution_ratio != 1.0` @@ -111,7 +123,7 @@ redef class App var ratio = dynamic_resolution_ratio ratio = ratio.clamp(min_dynamic_resolution_ratio, max_dynamic_resolution_ratio) - glBindFramebuffer(gl_FRAMEBUFFER, dynamic_context.screen_framebuffer) + bind_screen_framebuffer screen_framebuffer glBindBuffer(gl_ARRAY_BUFFER, dynamic_context.buffer_array) glViewport(0, 0, display.width, display.height) glClear gl_COLOR_BUFFER_BIT | gl_DEPTH_BUFFER_BIT @@ -133,18 +145,15 @@ redef class App n_floats = 2 glEnableVertexAttribArray dynres_program.tex_coord.location glVertexAttribPointeri(dynres_program.tex_coord.location, n_floats, gl_FLOAT, false, 0, offset) - var gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR # Draw glDrawArrays(gl_TRIANGLE_STRIP, 0, 4) - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR # Take down glBindBuffer(gl_ARRAY_BUFFER, 0) - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR sys.perfs["gamnit flat dyn res"].add app.perf_clock_dynamic_resolution.lapse end @@ -178,9 +187,6 @@ end # Handles to reused GL buffers and texture private class DynamicContext - # Real screen framebuffer - var screen_framebuffer: Int = -1 - # Dynamic screen framebuffer var dynamic_framebuffer: Int = -1 @@ -198,25 +204,19 @@ private class DynamicContext do # TODO enable antialiasing. - # Set aside the real screen framebuffer name - var screen_framebuffer = glGetIntegerv(gl_FRAMEBUFFER_BINDING, 0) - self.screen_framebuffer = screen_framebuffer - # Framebuffer var framebuffer = glGenFramebuffers(1).first glBindFramebuffer(gl_FRAMEBUFFER, framebuffer) assert glIsFramebuffer(framebuffer) self.dynamic_framebuffer = framebuffer - var gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR # Depth & texture/color var depthbuffer = glGenRenderbuffers(1).first self.depth_renderbuffer = depthbuffer var texture = glGenTextures(1).first self.texture = texture - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR resize(display, max_dynamic_resolution_ratio) assert glCheckFramebufferStatus(gl_FRAMEBUFFER) == gl_FRAMEBUFFER_COMPLETE @@ -225,8 +225,7 @@ private class DynamicContext buffer_array = glGenBuffers(1).first glBindBuffer(gl_ARRAY_BUFFER, buffer_array) assert glIsBuffer(buffer_array) - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR ## coord var data = new Array[Float] @@ -244,8 +243,7 @@ private class DynamicContext glBindBuffer(gl_ARRAY_BUFFER, 0) - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR end # Init size or resize `depth_renderbuffer` and `texture` @@ -264,8 +262,7 @@ private class DynamicContext assert glIsRenderbuffer(depthbuffer) glRenderbufferStorage(gl_RENDERBUFFER, gl_DEPTH_COMPONENT16, width, height) glFramebufferRenderbuffer(gl_FRAMEBUFFER, gl_DEPTH_ATTACHMENT, gl_RENDERBUFFER, depthbuffer) - var gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR # Texture glBindTexture(gl_TEXTURE_2D, texture) @@ -277,15 +274,13 @@ private class DynamicContext 0, gl_RGB, gl_UNSIGNED_BYTE, new Pointer.nul) glFramebufferTexture2D(gl_FRAMEBUFFER, gl_COLOR_ATTACHMENT0, gl_TEXTURE_2D, texture, 0) - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR # Take down glBindRenderbuffer(gl_RENDERBUFFER, 0) glBindFramebuffer(gl_FRAMEBUFFER, 0) - gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR end var destroyed = false @@ -296,8 +291,7 @@ private class DynamicContext # Free the buffer glDeleteBuffers([buffer_array]) - var gl_error = glGetError - assert gl_error == gl_NO_ERROR else print_error gl_error + assert glGetError == gl_NO_ERROR buffer_array = -1 # Free the dynamic framebuffer and its attachments