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
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
61 var program
= dynres_program
62 program
.compile_and_link
63 var error
= program
.error
64 assert error
== null else print_error error
67 # Prepare to draw to the dynamic screen if `dynamic_resolution_ratio != 1.0`
68 protected fun frame_core_dynamic_resolution_before
(display
: GamnitDisplay)
70 # TODO autodetect when to lower/increase resolution
72 if dynamic_resolution_ratio
== 1.0 then
73 # Draw directly to the screen framebuffer
74 glBindFramebuffer
(gl_FRAMEBUFFER
, dynamic_context
.screen_framebuffer
)
75 glViewport
(0, 0, display
.width
, display
.height
)
77 var gl_error
= glGetError
78 assert gl_error
== gl_NO_ERROR
else print_error gl_error
82 # Draw to our dynamic framebuffer
83 glBindFramebuffer
(gl_FRAMEBUFFER
, dynamic_context
.dynamic_framebuffer
)
84 glClear gl_COLOR_BUFFER_BIT
| gl_DEPTH_BUFFER_BIT
86 var ratio
= dynamic_resolution_ratio
87 ratio
= ratio
.clamp
(min_dynamic_resolution_ratio
, max_dynamic_resolution_ratio
)
88 glViewport
(0, 0, (display
.width
.to_f
*ratio
).to_i
,
89 (display
.height
.to_f
*ratio
).to_i
)
91 var gl_error
= glGetError
92 assert gl_error
== gl_NO_ERROR
else print_error gl_error
95 # Draw the dynamic screen to the real screen if `dynamic_resolution_ratio != 1.0`
96 protected fun frame_core_dynamic_resolution_after
(display
: GamnitDisplay)
98 if dynamic_resolution_ratio
== 1.0 then return
99 perf_clock_dynamic_resolution
.lapse
101 var ratio
= dynamic_resolution_ratio
102 ratio
= ratio
.clamp
(min_dynamic_resolution_ratio
, max_dynamic_resolution_ratio
)
104 glBindFramebuffer
(gl_FRAMEBUFFER
, dynamic_context
.screen_framebuffer
)
105 glBindBuffer
(gl_ARRAY_BUFFER
, dynamic_context
.buffer_array
)
106 glViewport
(0, 0, display
.width
, display
.height
)
107 glClear gl_COLOR_BUFFER_BIT
| gl_DEPTH_BUFFER_BIT
111 glActiveTexture gl_TEXTURE0
112 glBindTexture
(gl_TEXTURE_2D
, dynamic_context
.texture
)
113 dynres_program
.texture
.uniform
0
114 dynres_program
.ratio
.uniform ratio
117 var sizeof_gl_float
= 4
119 glEnableVertexAttribArray dynres_program
.coord
.location
120 glVertexAttribPointeri
(dynres_program
.coord
.location
, n_floats
, gl_FLOAT
, false, 0, 0)
121 var offset
= 4 * n_floats
* sizeof_gl_float
124 glEnableVertexAttribArray dynres_program
.tex_coord
.location
125 glVertexAttribPointeri
(dynres_program
.tex_coord
.location
, n_floats
, gl_FLOAT
, false, 0, offset
)
126 var gl_error
= glGetError
127 assert gl_error
== gl_NO_ERROR
else print_error gl_error
130 glDrawArrays
(gl_TRIANGLE_STRIP
, 0, 4)
132 gl_error
= glGetError
133 assert gl_error
== gl_NO_ERROR
else print_error gl_error
135 sys
.perfs
["gamnit flat dyn res"].add app
.perf_clock_dynamic_resolution
.lapse
138 # Framebuffer and texture for dynamic resolution intermediate drawing
139 private var dynamic_context
: DynamicContext = create_dynamic_context
is lazy
141 private fun create_dynamic_context
: DynamicContext
143 # TODO option or flag to regen on real resolution change.
145 var display
= display
146 assert display
!= null
148 var context
= new DynamicContext
149 context
.prepare_once
(display
, max_dynamic_resolution_ratio
)
154 # Program drawing the dynamic screen to the real screen
155 private class DynamicContext
157 # Real screen framebuffer
158 var screen_framebuffer
: Int = -1
160 # Dynamic screen framebuffer
161 var dynamic_framebuffer
: Int = -1
163 # Depth renderbuffer attached to `dynamic_framebuffer`
164 var depth_renderbuffer
: Int = -1
166 # Texture attached to `dynamic_framebuffer` as color attachment
167 var texture
: Int = -1
169 # Buffer name for vertex data
170 var buffer_array
: Int = -1
172 var float_per_vertex
: Int is lazy
do return 4 + 4 + 3
174 # Prepare all attributes once per resolution change
175 fun prepare_once
(display
: GamnitDisplay, max_dynamic_resolution_ratio
: Float)
177 # TODO enable antialiasing.
179 var width
= (display
.width
.to_f
* max_dynamic_resolution_ratio
).to_i
180 var height
= (display
.height
.to_f
* max_dynamic_resolution_ratio
).to_i
182 # Set aside the real screen framebuffer name
183 var screen_framebuffer
= glGetIntegerv
(gl_FRAMEBUFFER_BINDING
, 0)
184 self.screen_framebuffer
= screen_framebuffer
187 var framebuffer
= glGenFramebuffers
(1).first
188 glBindFramebuffer
(gl_FRAMEBUFFER
, framebuffer
)
189 assert glIsFramebuffer
(framebuffer
)
190 self.dynamic_framebuffer
= framebuffer
193 var depthbuffer
= glGenRenderbuffers
(1).first
194 glBindRenderbuffer
(gl_RENDERBUFFER
, depthbuffer
)
195 assert glIsRenderbuffer
(depthbuffer
)
196 glRenderbufferStorage
(gl_RENDERBUFFER
, gl_DEPTH_COMPNENT16
, width
, height
)
197 glFramebufferRenderbuffer
(gl_FRAMEBUFFER
, gl_DEPTH_ATTACHMENT
, gl_RENDERBUFFER
, depthbuffer
)
198 self.depth_renderbuffer
= depthbuffer
201 var texture
= glGenTextures
(1).first
202 glBindTexture
(gl_TEXTURE_2D
, texture
)
203 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_MIN_FILTER
, gl_LINEAR
)
204 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_MAG_FILTER
, gl_LINEAR
)
205 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_WRAP_S
, gl_CLAMP_TO_EDGE
)
206 glTexParameteri
(gl_TEXTURE_2D
, gl_TEXTURE_WRAP_T
, gl_CLAMP_TO_EDGE
)
207 glTexImage2D
(gl_TEXTURE_2D
, 0, gl_RGB
, width
, height
,
208 0, gl_RGB
, gl_UNSIGNED_BYTE
, new Pointer.nul
)
209 glFramebufferTexture2D
(gl_FRAMEBUFFER
, gl_COLOR_ATTACHMENT0
, gl_TEXTURE_2D
, texture
, 0)
210 self.texture
= texture
212 var gl_error
= glGetError
213 assert gl_error
== gl_NO_ERROR
else print_error gl_error
214 assert glCheckFramebufferStatus
(gl_FRAMEBUFFER
) == gl_FRAMEBUFFER_COMPLETE
217 glBindRenderbuffer
(gl_RENDERBUFFER
, 0)
218 glBindFramebuffer
(gl_FRAMEBUFFER
, 0)
221 buffer_array
= glGenBuffers
(1).first
222 glBindBuffer
(gl_ARRAY_BUFFER
, buffer_array
)
223 assert glIsBuffer
(buffer_array
)
224 gl_error
= glGetError
225 assert gl_error
== gl_NO_ERROR
else print_error gl_error
228 var data
= new Array[Float]
229 data
.add_all
([-1.0, -1.0, 0.0,
234 data
.add_all
([0.0, 0.0,
238 var c_data
= new GLfloatArray.from
(data
)
239 glBufferData
(gl_ARRAY_BUFFER
, data
.length
*4, c_data
.native_array
, gl_STATIC_DRAW
)
241 glBindBuffer
(gl_ARRAY_BUFFER
, 0)
243 gl_error
= glGetError
244 assert gl_error
== gl_NO_ERROR
else print_error gl_error
247 var destroyed
= false
250 if destroyed
then return
254 glDeleteBuffers
([buffer_array
])
255 var gl_error
= glGetError
256 assert gl_error
== gl_NO_ERROR
else print_error gl_error
259 # Free the dynamic framebuffer and its attachments
260 glDeleteBuffers
([buffer_array
])
261 glDeleteFramebuffers
([dynamic_framebuffer
])
262 glDeleteRenderbuffers
([depth_renderbuffer
])
263 glDeleteTextures
([texture
])
267 private class DynamicResolutionProgram
268 super GamnitProgramFromSource
270 redef var vertex_shader_source
= """
271 // Vertex coordinates
272 attribute vec3 coord;
274 // Vertex coordinates on textures
275 attribute vec2 tex_coord;
277 // Output to the fragment shader
278 varying vec2 v_coord;
282 gl_Position = vec4(coord, 1.0);
285 """ @ glsl_vertex_shader
287 redef var fragment_shader_source
= """
288 precision mediump float;
290 // Virtual screen texture / color attachment
291 uniform sampler2D texture0;
293 // Input from the vertex shader
294 varying vec2 v_coord;
296 // Ratio of the virtual screen to draw
301 gl_FragColor = texture2D(texture0, v_coord*ratio);
303 """ @ glsl_fragment_shader
305 # Vertices coordinates
306 var coord
= attributes
["coord"].as(AttributeVec3) is lazy
308 # Coordinates on the textures, per vertex
309 var tex_coord
= attributes
["tex_coord"].as(AttributeVec2) is lazy
311 # Virtual screen texture / color attachment
312 var texture
= uniforms
["texture0"].as(UniformSampler2D) is lazy
314 # Ratio of the virtual screen to draw
315 var ratio
= uniforms
["ratio"].as(UniformFloat) is lazy