lib/glesv2: wrap more features to query informations on attributes and uniforms
[nit.git] / lib / glesv2 / glesv2.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # OpenGL graphics rendering library for embedded systems, version 2.0
18 #
19 # This is a low-level wrapper, it can be useful for developers already familiar
20 # with the C API of OpenGL. Most developers will prefer to use higher level
21 # wrappers such as `mnit` and `gammit`.
22 #
23 # Defines the annotations `glsl_vertex_shader` and `glsl_fragment_shader`
24 # applicable on string literals to check shader code using `glslangValidator`.
25 # The tool must be in PATH. It can be downloaded from
26 # https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
27 #
28 # Most services of this module are a direct wrapper of the underlying
29 # C library. If a method or class is not documented in Nit, refer to
30 # the official documentation by the Khronos Group at:
31 # http://www.khronos.org/opengles/sdk/docs/man/
32 module glesv2 is
33 pkgconfig
34 new_annotation glsl_vertex_shader
35 new_annotation glsl_fragment_shader
36 end
37
38
39 in "C Header" `{
40 #include <GLES2/gl2.h>
41 `}
42
43 # OpenGL ES program to which we attach shaders
44 extern class GLProgram `{GLuint`}
45 # Create a new program
46 #
47 # The newly created instance should be checked using `is_ok`.
48 new `{ return glCreateProgram(); `}
49
50 # Is this a valid program?
51 fun is_ok: Bool `{ return glIsProgram(recv); `}
52
53 # Attach a `shader` to this program
54 fun attach_shader(shader: GLShader) `{ glAttachShader(recv, shader); `}
55
56 # Set the location for the attribute by `name`
57 fun bind_attrib_location(index: Int, name: String) import String.to_cstring `{
58 GLchar *c_name = String_to_cstring(name);
59 glBindAttribLocation(recv, index, c_name);
60 `}
61
62 # Get the location of the attribute by `name`
63 #
64 # Returns `-1` if there is no active attribute named `name`.
65 fun attrib_location(name: String): Int import String.to_cstring `{
66 GLchar *c_name = String_to_cstring(name);
67 return glGetAttribLocation(recv, c_name);
68 `}
69
70 # Get the location of the uniform by `name`
71 #
72 # Returns `-1` if there is no active uniform named `name`.
73 fun uniform_location(name: String): Int import String.to_cstring `{
74 GLchar *c_name = String_to_cstring(name);
75 return glGetUniformLocation(recv, c_name);
76 `}
77
78 # Query information on this program
79 fun query(pname: Int): Int `{
80 int val;
81 glGetProgramiv(recv, pname, &val);
82 return val;
83 `}
84
85 # Try to link this program
86 #
87 # Check result using `in_linked` and `info_log`.
88 fun link `{ glLinkProgram(recv); `}
89
90 # Is this program linked?
91 fun is_linked: Bool do return query(0x8B82) != 0
92
93 # Use this program for the following operations
94 fun use `{ glUseProgram(recv); `}
95
96 # Delete this program
97 fun delete `{ glDeleteProgram(recv); `}
98
99 # Has this program been deleted?
100 fun is_deleted: Bool do return query(0x8B80) != 0
101
102 # Validate whether this program can be executed in the current OpenGL state
103 #
104 # Check results using `is_validated` and `info_log`.
105 fun validate `{ glValidateProgram(recv); `}
106
107 # Boolean result of `validate`, must be called after `validate`
108 fun is_validated: Bool do return query(0x8B83) != 0
109
110 # Retrieve the information log of this program
111 #
112 # Useful with `link` and `validate`
113 fun info_log: String import NativeString.to_s `{
114 int size;
115 glGetProgramiv(recv, GL_INFO_LOG_LENGTH, &size);
116 GLchar *msg = malloc(size);
117 glGetProgramInfoLog(recv, size, NULL, msg);
118 return NativeString_to_s(msg);
119 `}
120
121 # Number of active uniform in this program
122 #
123 # This should be the number of uniforms declared in all shader, except
124 # unused uniforms which may have been optimized out.
125 fun n_active_uniforms: Int do return query(0x8B86)
126
127 # Length of the longest uniform name in this program, including `\n`
128 fun active_uniform_max_length: Int do return query(0x8B87)
129
130 # Number of active attributes in this program
131 #
132 # This should be the number of uniforms declared in all shader, except
133 # unused uniforms which may have been optimized out.
134 fun n_active_attributes: Int do return query(0x8B89)
135
136 # Length of the longest uniform name in this program, including `\n`
137 fun active_attribute_max_length: Int do return query(0x8B8A)
138
139 # Number of shaders attached to this program
140 fun n_attached_shaders: Int do return query(0x8B85)
141
142 # Name of the active attribute at `index`
143 fun active_attrib_name(index: Int): String
144 do
145 var max_size = active_attribute_max_length
146 return active_attrib_name_native(index, max_size).to_s
147 end
148 private fun active_attrib_name_native(index, max_size: Int): NativeString `{
149 char *name = malloc(max_size);
150 glGetActiveAttrib(recv, index, max_size, NULL, NULL, NULL, name);
151 return name;
152 `}
153
154 # Size of the active attribute at `index`
155 fun active_attrib_size(index: Int): Int `{
156 int size;
157 glGetActiveAttrib(recv, index, 0, NULL, NULL, &size, NULL);
158 return size;
159 `}
160
161 # Type of the active attribute at `index`
162 #
163 # May only be float related data types (single float, vectors and matrix).
164 fun active_attrib_type(index: Int): GLFloatDataType `{
165 GLenum type;
166 glGetActiveAttrib(recv, index, 0, NULL, &type, NULL, NULL);
167 return type;
168 `}
169
170 # Name of the active uniform at `index`
171 fun active_uniform_name(index: Int): String
172 do
173 var max_size = active_attribute_max_length
174 return active_uniform_name_native(index, max_size).to_s
175 end
176 private fun active_uniform_name_native(index, max_size: Int): NativeString `{
177 char *name = malloc(max_size);
178 glGetActiveUniform(recv, index, max_size, NULL, NULL, NULL, name);
179 return name;
180 `}
181
182 # Size of the active uniform at `index`
183 fun active_uniform_size(index: Int): Int `{
184 int size;
185 glGetActiveUniform(recv, index, 0, NULL, NULL, &size, NULL);
186 return size;
187 `}
188
189 # Type of the active uniform at `index`
190 #
191 # May be any data type supported by OpenGL ES 2.0 shaders.
192 fun active_uniform_type(index: Int): GLDataType `{
193 GLenum type;
194 glGetActiveUniform(recv, index, 0, NULL, &type, NULL, NULL);
195 return type;
196 `}
197 end
198
199 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
200 extern class GLShader `{GLuint`}
201 # Set the source of the shader
202 fun source=(code: String) import String.to_cstring, String.length `{
203 GLchar *c_code = String_to_cstring(code);
204 glShaderSource(recv, 1, (const GLchar * const*)&c_code, NULL);
205 `}
206
207 # Source of the shader, if available
208 #
209 # Returns `null` if the source is not available, usually when the shader
210 # was created from a binary file.
211 fun source: nullable String
212 do
213 var size = query(0x8B88)
214 if size == 0 then return null
215 return source_native(size).to_s
216 end
217
218 private fun source_native(size: Int): NativeString `{
219 GLchar *code = malloc(size);
220 glGetShaderSource(recv, size, NULL, code);
221 return code;
222 `}
223
224 # Query information on this shader
225 protected fun query(pname: Int): Int `{
226 int val;
227 glGetShaderiv(recv, pname, &val);
228 return val;
229 `}
230
231 # Try to compile `source` into a binary GPU program
232 #
233 # Check the result using `is_compiled` and `info_log`
234 fun compile `{ glCompileShader(recv); `}
235
236 # Has this shader been compiled?
237 fun is_compiled: Bool do return query(0x8B81) != 0
238
239 # Delete this shader
240 fun delete `{ glDeleteShader(recv); `}
241
242 # Has this shader been deleted?
243 fun is_deleted: Bool do return query(0x8B80) != 0
244
245 # Is this a valid shader?
246 fun is_ok: Bool `{ return glIsShader(recv); `}
247
248 # Retrieve the information log of this shader
249 #
250 # Useful with `link` and `validate`
251 fun info_log: String import NativeString.to_s `{
252 int size;
253 glGetShaderiv(recv, GL_INFO_LOG_LENGTH, &size);
254 GLchar *msg = malloc(size);
255 glGetShaderInfoLog(recv, size, NULL, msg);
256 return NativeString_to_s(msg);
257 `}
258 end
259
260 # An OpenGL ES 2.0 fragment shader
261 extern class GLFragmentShader
262 super GLShader
263
264 # Create a new fragment shader
265 #
266 # The newly created instance should be checked using `is_ok`.
267 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
268 end
269
270 # An OpenGL ES 2.0 vertex shader
271 extern class GLVertexShader
272 super GLShader
273
274 # Create a new fragment shader
275 #
276 # The newly created instance should be checked using `is_ok`.
277 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
278 end
279
280 # An array of `Float` associated to a program variable
281 class VertexArray
282 var index: Int
283
284 # Number of data per vertex
285 var count: Int
286
287 protected var glfloat_array: GLfloatArray
288
289 init(index, count: Int, array: Array[Float])
290 do
291 self.index = index
292 self.count = count
293 self.glfloat_array = new GLfloatArray(array)
294 end
295
296 fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array)
297 private fun attrib_pointer_intern(index, count: Int, array: GLfloatArray) `{
298 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
299 `}
300
301 fun enable do enable_intern(index)
302 private fun enable_intern(index: Int) `{ glEnableVertexAttribArray(index); `}
303
304 fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count)
305 private fun draw_arrays_triangles_intern(index, count: Int) `{
306 glDrawArrays(GL_TRIANGLES, index, count);
307 `}
308 end
309
310 # Low level array of `Float`
311 extern class GLfloatArray `{GLfloat *`}
312 new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{
313 int i;
314 int len = Array_of_Float_length(array);
315 GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
316 for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
317 return vertex_array;
318 `}
319 end
320
321 # An OpenGL ES 2.0 error code
322 extern class GLError `{ GLenum `}
323 fun is_ok: Bool do return is_no_error
324 fun is_no_error: Bool `{ return recv == GL_NO_ERROR; `}
325 fun is_invalid_enum: Bool `{ return recv == GL_INVALID_ENUM; `}
326 fun is_invalid_value: Bool `{ return recv == GL_INVALID_VALUE; `}
327 fun is_invalid_operation: Bool `{ return recv == GL_INVALID_OPERATION; `}
328 fun is_invalid_framebuffer_operation: Bool `{ return recv == GL_INVALID_FRAMEBUFFER_OPERATION; `}
329 fun is_out_of_memory: Bool `{ return recv == GL_OUT_OF_MEMORY; `}
330
331 redef fun to_s
332 do
333 if is_no_error then return "No error"
334 if is_invalid_enum then return "Invalid enum"
335 if is_invalid_value then return "Invalid value"
336 if is_invalid_operation then return "Invalid operation"
337 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
338 if is_out_of_memory then return "Out of memory"
339 return "Truely unknown error"
340 end
341 end
342
343 # Clear the color buffer with `r`, `g`, `b`, `a`
344 protected fun gl_clear_color(r, g, b, a: Float) `{ glClearColor(r, g, b, a); `}
345
346 # Set the viewport
347 protected fun gl_viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `}
348
349 # Direct call to `glClear`, call with a combinaison of `gl_clear_color_buffer`,
350 # `gl_stencil_buffer_bit` and `gl_color_buffer_bit`.
351 private fun gl_clear(flag: Int) `{ glClear(flag); `}
352
353 protected fun gl_depth_buffer_bit: Int do return 0x0100
354 protected fun gl_stencil_buffer_bit: Int do return 0x0400
355 protected fun gl_color_buffer_bit: Int do return 0x4000
356
357 protected fun gl_clear_color_buffer do gl_clear(gl_color_buffer_bit)
358 protected fun gl_clear_depth_buffer do gl_clear(gl_depth_buffer_bit)
359 protected fun gl_clear_stencil_buffer do gl_clear(gl_stencil_buffer_bit)
360
361 protected fun gl_error: GLError `{ return glGetError(); `}
362 protected fun assert_no_gl_error
363 do
364 var error = gl_error
365 if not error.is_ok then
366 print "GL error: {error}"
367 abort
368 end
369 end
370
371 # Query the boolean value at `key`
372 private fun gl_get_bool(key: Int): Bool `{
373 GLboolean val;
374 glGetBooleanv(key, &val);
375 return val == GL_TRUE;
376 `}
377
378 # Query the floating point value at `key`
379 private fun gl_get_float(key: Int): Float `{
380 GLfloat val;
381 glGetFloatv(key, &val);
382 return val;
383 `}
384
385 # Query the integer value at `key`
386 private fun gl_get_int(key: Int): Int `{
387 GLint val;
388 glGetIntegerv(key, &val);
389 return val;
390 `}
391
392 # Does this driver support shader compilation?
393 #
394 # Should always return `true` in OpenGL ES 2.0 and 3.0.
395 fun gl_shader_compiler: Bool do return gl_get_bool(0x8DFA)
396
397 # Float related data types of OpenGL ES 2.0 shaders
398 #
399 # Only data types supported by shader attributes, as seen with
400 # `GLProgram::active_attrib_type`.
401 extern class GLFloatDataType `{ GLenum `}
402 fun is_float: Bool `{ return recv == GL_FLOAT; `}
403 fun is_float_vec2: Bool `{ return recv == GL_FLOAT_VEC2; `}
404 fun is_float_vec3: Bool `{ return recv == GL_FLOAT_VEC3; `}
405 fun is_float_vec4: Bool `{ return recv == GL_FLOAT_VEC4; `}
406 fun is_float_mat2: Bool `{ return recv == GL_FLOAT_MAT2; `}
407 fun is_float_mat3: Bool `{ return recv == GL_FLOAT_MAT3; `}
408 fun is_float_mat4: Bool `{ return recv == GL_FLOAT_MAT4; `}
409 end
410
411 # All data types of OpenGL ES 2.0 shaders
412 #
413 # These types can be used by shader uniforms, as seen with
414 # `GLProgram::active_uniform_type`.
415 extern class GLDataType
416 super GLFloatDataType
417
418 fun is_int: Bool `{ return recv == GL_INT; `}
419 fun is_int_vec2: Bool `{ return recv == GL_INT_VEC2; `}
420 fun is_int_vec3: Bool `{ return recv == GL_INT_VEC3; `}
421 fun is_int_vec4: Bool `{ return recv == GL_INT_VEC4; `}
422 fun is_bool: Bool `{ return recv == GL_BOOL; `}
423 fun is_bool_vec2: Bool `{ return recv == GL_BOOL_VEC2; `}
424 fun is_bool_vec3: Bool `{ return recv == GL_BOOL_VEC3; `}
425 fun is_bool_vec4: Bool `{ return recv == GL_BOOL_VEC4; `}
426 fun is_sampler_2d: Bool `{ return recv == GL_SAMPLER_2D; `}
427 fun is_sampler_cube: Bool `{ return recv == GL_SAMPLER_CUBE; `}
428 end