1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Virtual screen with a resolution independent from the real screen
17 # The attribute `dynamic_resolution_ratio` sets the size of the virtual screen
18 # in relation to the real screen. See its documentation for more information.
20 # Reference implementation:
21 # https://software.intel.com/en-us/articles/dynamic-resolution-rendering-on-opengl-es-2
22 module dynamic_resolution
24 import performance_analysis
29 # Resolution of the dynamic screen as ratio of the real screen resolution.
31 # - At 1.0, the default, the virtual screen is not used and the visuals are
32 # drawn directly to the real screen and pixels.
33 # - When below 1.0, there is less pixels in the dynamic screen than in the
34 # real screen. This reduces the strain on the GPU, especially of high
35 # resolution displays.
36 # - Values above 1.0 are not supported at this point, but they would allow
39 # This value must be set either by the user using a video quality slider or
40 # by an heuristic according to the device capabilities.
41 # A lower value should use less battery power on mobile devices.
43 # This value is applied to both X and Y, so it has an exponential effect on
44 # the number of pixels.
45 var dynamic_resolution_ratio
= 1.0 is writable
47 # Minimum dynamic screen resolution
48 var min_dynamic_resolution_ratio
= 0.0125 is writable
50 # Maximum dynamic screen resolution, must be set before first draw
51 var max_dynamic_resolution_ratio
= 1.0 is writable
53 private var dynres_program
= new DynamicResolutionProgram
55 private var perf_clock_dynamic_resolution
= new Clock is lazy
57 # Real screen framebuffer
58 private var screen_framebuffer_cache
: Int = -1
60 # Real screen framebuffer name
61 fun screen_framebuffer
: Int
63 var cache
= screen_framebuffer_cache
64 if cache
!= -1 then return cache
66 cache
= glGetIntegerv
(gl_FRAMEBUFFER_BINDING
, 0)
67 self.screen_framebuffer_cache
= cache
71 redef fun create_gamnit
75 var program
= dynres_program
76 program
.compile_and_link
77 var error
= program
.error
78 assert error
== null else print_error error
80 dynamic_context_cache
= null
83 redef fun on_resize
(display
)
85 if dynamic_context_cache
!= null then dynamic_context
.resize
(display
, max_dynamic_resolution_ratio
)
89 # Prepare to draw to the dynamic screen if `dynamic_resolution_ratio != 1.0`
90 protected fun frame_core_dynamic_resolution_before
(display
: GamnitDisplay)
92 # TODO autodetect when to lower/increase resolution
94 if dynamic_resolution_ratio
== 1.0 then
95 # Draw directly to the screen framebuffer
96 bind_screen_framebuffer screen_framebuffer
97 glViewport
(0, 0, display
.width
, display
.height
)
98 glClear gl_COLOR_BUFFER_BIT
| gl_DEPTH_BUFFER_BIT
100 assert glGetError
== gl_NO_ERROR
104 # Draw to our dynamic framebuffer
105 glBindFramebuffer
(gl_FRAMEBUFFER
, dynamic_context
.dynamic_framebuffer
)
107 var ratio
= dynamic_resolution_ratio
108 ratio
= ratio
.clamp
(min_dynamic_resolution_ratio
, max_dynamic_resolution_ratio
)
109 glViewport
(0, 0, (display
.width
.to_f
*ratio
).to_i
,
110 (display
.height
.to_f
*ratio
).to_i
)
112 glClear gl_COLOR_BUFFER_BIT
| gl_DEPTH_BUFFER_BIT
114 assert glGetError
== gl_NO_ERROR
117 # Draw the dynamic screen to the real screen if `dynamic_resolution_ratio != 1.0`
118 protected fun frame_core_dynamic_resolution_after
(display
: GamnitDisplay)
120 if dynamic_resolution_ratio
== 1.0 then return
121 perf_clock_dynamic_resolution
.lapse
123 var ratio
= dynamic_resolution_ratio
124 ratio
= ratio
.clamp
(min_dynamic_resolution_ratio
, max_dynamic_resolution_ratio
)
126 bind_screen_framebuffer screen_framebuffer
127 glBindBuffer
(gl_ARRAY_BUFFER
, dynamic_context
.buffer_array
)
128 glViewport
(0, 0, display
.width
, display
.height
)
129 glClear gl_COLOR_BUFFER_BIT
| gl_DEPTH_BUFFER_BIT
133 glActiveTexture gl_TEXTURE0
134 glBindTexture
(gl_TEXTURE_2D
, dynamic_context
.texture
)
135 dynres_program
.texture
.uniform
0
136 dynres_program
.ratio
.uniform ratio
139 var sizeof_gl_float
= 4
141 glEnableVertexAttribArray dynres_program
.coord
.location
142 glVertexAttribPointeri
(dynres_program
.coord
.location
, n_floats
, gl_FLOAT
, false, 0, 0)
143 var offset
= 4 * n_floats
* sizeof_gl_float
146 glEnableVertexAttribArray dynres_program
.tex_coord
.location
147 glVertexAttribPointeri
(dynres_program
.tex_coord
.location
, n_floats
, gl_FLOAT
, false, 0, offset
)
148 assert glGetError
== gl_NO_ERROR
151 glDrawArrays
(gl_TRIANGLE_STRIP
, 0, 4)
152 assert glGetError
== gl_NO_ERROR
155 glBindBuffer
(gl_ARRAY_BUFFER
, 0)
156 assert glGetError
== gl_NO_ERROR
158 sys
.perfs
["gamnit flat dyn res"].add app
.perf_clock_dynamic_resolution
.lapse
161 # Framebuffer and texture for dynamic resolution intermediate drawing
162 private fun dynamic_context
: DynamicContext
164 var cache
= dynamic_context_cache
165 if cache
!= null then return cache
167 cache
= create_dynamic_context
168 dynamic_context_cache
= cache
172 private var dynamic_context_cache
: nullable DynamicContext = null
174 private fun create_dynamic_context
: DynamicContext
176 # TODO option or flag to regen on real resolution change.
178 var display
= display
179 assert display
!= null
181 var context
= new DynamicContext
182 context
.prepare_once
(display
, max_dynamic_resolution_ratio
)
187 # Handles to reused GL buffers and texture
188 private class DynamicContext
190 # Dynamic screen framebuffer
191 var dynamic_framebuffer
: Int = -1
193 # Depth renderbuffer attached to `dynamic_framebuffer`
194 var depth_renderbuffer
: Int = -1
196 # Texture attached to `dynamic_framebuffer` as color attachment
197 var texture
: Int = -1
199 # Buffer name for vertex data
200 var buffer_array
: Int = -1
202 # Prepare all attributes once per resolution change
203 fun prepare_once
(display
: GamnitDisplay, max_dynamic_resolution_ratio
: Float)
205 # TODO enable antialiasing.
208 var framebuffer
= glGenFramebuffers
(1).first
209 glBindFramebuffer
(gl_FRAMEBUFFER
, framebuffer
)
210 assert glIsFramebuffer
(framebuffer
)
211 self.dynamic_framebuffer
= framebuffer
212 assert glGetError
== gl_NO_ERROR
214 # Depth & texture/color
215 var depthbuffer
= glGenRenderbuffers
(1).first
216 self.depth_renderbuffer
= depthbuffer
217 var texture
= glGenTextures
(1).first
218 self.texture
= texture
219 assert glGetError
== gl_NO_ERROR
221 resize
(display
, max_dynamic_resolution_ratio
)
222 assert glCheckFramebufferStatus
(gl_FRAMEBUFFER
) == gl_FRAMEBUFFER_COMPLETE
225 buffer_array
= glGenBuffers
(1).first
226 glBindBuffer
(gl_ARRAY_BUFFER
, buffer_array
)
227 assert glIsBuffer
(buffer_array
)
228 assert glGetError
== gl_NO_ERROR
231 var data
= new Array[Float]
232 data
.add_all
([-1.0, -1.0, 0.0,
237 data
.add_all
([0.0, 0.0,
241 var c_data
= new GLfloatArray.from
(data
)
242 glBufferData
(gl_ARRAY_BUFFER
, data
.length
*4, c_data
.native_array
, gl_STATIC_DRAW
)
244 glBindBuffer
(gl_ARRAY_BUFFER
, 0)
246 assert glGetError
== gl_NO_ERROR
249 # Init size or resize `depth_renderbuffer` and `texture`
250 fun resize
(display
: GamnitDisplay, max_dynamic_resolution_ratio
: Float)
252 var width
= (display
.width
.to_f
* max_dynamic_resolution_ratio
).to_i
253 var height
= (display
.height
.to_f
* max_dynamic_resolution_ratio
).to_i
255 glBindFramebuffer
(gl_FRAMEBUFFER
, dynamic_framebuffer
)
257 var depthbuffer
= self.depth_renderbuffer
258 var texture
= self.texture
261 glBindRenderbuffer
(gl_RENDERBUFFER
, depthbuffer
)
262 assert glIsRenderbuffer
(depthbuffer
)
263 glRenderbufferStorage
(gl_RENDERBUFFER
, gl_DEPTH_COMPONENT16
, width
, height
)
264 glFramebufferRenderbuffer
(gl_FRAMEBUFFER
, gl_DEPTH_ATTACHMENT
, gl_RENDERBUFFER
, depthbuffer
)
265 assert glGetError
== gl_NO_ERROR
268 glBindTexture
(gl_TEXTURE_2D
, texture
)
269 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_MIN_FILTER
, gl_LINEAR
)
270 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_MAG_FILTER
, gl_LINEAR
)
271 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_WRAP_S
, gl_CLAMP_TO_EDGE
)
272 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_WRAP_T
, gl_CLAMP_TO_EDGE
)
273 glTexImage2D
(gl_TEXTURE_2D
, 0, gl_RGB
, width
, height
,
274 0, gl_RGB
, gl_UNSIGNED_BYTE
, new Pointer.nul
)
275 glFramebufferTexture2D
(gl_FRAMEBUFFER
, gl_COLOR_ATTACHMENT0
, gl_TEXTURE_2D
, texture
, 0)
277 assert glGetError
== gl_NO_ERROR
280 glBindRenderbuffer
(gl_RENDERBUFFER
, 0)
281 glBindFramebuffer
(gl_FRAMEBUFFER
, 0)
283 assert glGetError
== gl_NO_ERROR
286 var destroyed
= false
289 if destroyed
then return
293 glDeleteBuffers
([buffer_array
])
294 assert glGetError
== gl_NO_ERROR
297 # Free the dynamic framebuffer and its attachments
298 glDeleteBuffers
([buffer_array
])
299 glDeleteFramebuffers
([dynamic_framebuffer
])
300 glDeleteRenderbuffers
([depth_renderbuffer
])
301 glDeleteTextures
([texture
])
305 private class DynamicResolutionProgram
306 super GamnitProgramFromSource
308 redef var vertex_shader_source
= """
309 // Vertex coordinates
310 attribute vec3 coord;
312 // Vertex coordinates on textures
313 attribute vec2 tex_coord;
315 // Output to the fragment shader
316 varying vec2 v_coord;
320 gl_Position = vec4(coord, 1.0);
323 """ @ glsl_vertex_shader
325 redef var fragment_shader_source
= """
326 precision mediump float;
328 // Virtual screen texture / color attachment
329 uniform sampler2D texture0;
331 // Input from the vertex shader
332 varying vec2 v_coord;
334 // Ratio of the virtual screen to draw
339 gl_FragColor = texture2D(texture0, v_coord*ratio);
341 """ @ glsl_fragment_shader
343 # Vertices coordinates
344 var coord
= attributes
["coord"].as(AttributeVec3) is lazy
346 # Coordinates on the textures, per vertex
347 var tex_coord
= attributes
["tex_coord"].as(AttributeVec2) is lazy
349 # Virtual screen texture / color attachment
350 var texture
= uniforms
["texture0"].as(UniformSampler2D) is lazy
352 # Ratio of the virtual screen to draw
353 var ratio
= uniforms
["ratio"].as(UniformFloat) is lazy