lib/glesv2: improve doc of `glesv2`
[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
71 # Query information on this program
72 fun query(pname: Int): Int `{
73 int val;
74 glGetProgramiv(recv, pname, &val);
75 return val;
76 `}
77
78 # Try to link this program
79 #
80 # Check result using `in_linked` and `info_log`.
81 fun link `{ glLinkProgram(recv); `}
82
83 # Is this program linked?
84 fun is_linked: Bool do return query(0x8B82) != 0
85
86 # Use this program for the following operations
87 fun use `{ glUseProgram(recv); `}
88
89 # Delete this program
90 fun delete `{ glDeleteProgram(recv); `}
91
92 # Has this program been deleted?
93 fun is_deleted: Bool do return query(0x8B80) != 0
94
95 # Validate whether this program can be executed in the current OpenGL state
96 #
97 # Check results using `is_validated` and `info_log`.
98 fun validate `{ glValidateProgram(recv); `}
99
100 # Boolean result of `validate`, must be called after `validate`
101 fun is_validated: Bool do return query(0x8B83) != 0
102
103 # Retrieve the information log of this program
104 #
105 # Useful with `link` and `validate`
106 fun info_log: String import NativeString.to_s `{
107 int size;
108 glGetProgramiv(recv, GL_INFO_LOG_LENGTH, &size);
109 GLchar *msg = malloc(size);
110 glGetProgramInfoLog(recv, size, NULL, msg);
111 return NativeString_to_s(msg);
112 `}
113 end
114
115 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
116 extern class GLShader `{GLuint`}
117 # Set the source of the shader
118 fun source=(code: String) import String.to_cstring, String.length `{
119 GLchar *c_code = String_to_cstring(code);
120 glShaderSource(recv, 1, (const GLchar * const*)&c_code, NULL);
121 `}
122
123 # Source of the shader, if available
124 #
125 # Returns `null` if the source is not available, usually when the shader
126 # was created from a binary file.
127 fun source: nullable String
128 do
129 var size = query(0x8B88)
130 if size == 0 then return null
131 return source_native(size).to_s
132 end
133
134 private fun source_native(size: Int): NativeString `{
135 GLchar *code = malloc(size);
136 glGetShaderSource(recv, size, NULL, code);
137 return code;
138 `}
139
140 # Query information on this shader
141 protected fun query(pname: Int): Int `{
142 int val;
143 glGetShaderiv(recv, pname, &val);
144 return val;
145 `}
146
147 # Try to compile `source` into a binary GPU program
148 #
149 # Check the result using `is_compiled` and `info_log`
150 fun compile `{ glCompileShader(recv); `}
151
152 # Has this shader been compiled?
153 fun is_compiled: Bool do return query(0x8B81) != 0
154
155 # Delete this shader
156 fun delete `{ glDeleteShader(recv); `}
157
158 # Has this shader been deleted?
159 fun is_deleted: Bool do return query(0x8B80) != 0
160
161 # Is this a valid shader?
162 fun is_ok: Bool `{ return glIsShader(recv); `}
163
164 # Retrieve the information log of this shader
165 #
166 # Useful with `link` and `validate`
167 fun info_log: String import NativeString.to_s `{
168 int size;
169 glGetShaderiv(recv, GL_INFO_LOG_LENGTH, &size);
170 GLchar *msg = malloc(size);
171 glGetShaderInfoLog(recv, size, NULL, msg);
172 return NativeString_to_s(msg);
173 `}
174 end
175
176 # An OpenGL ES 2.0 fragment shader
177 extern class GLFragmentShader
178 super GLShader
179
180 # Create a new fragment shader
181 #
182 # The newly created instance should be checked using `is_ok`.
183 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
184 end
185
186 # An OpenGL ES 2.0 vertex shader
187 extern class GLVertexShader
188 super GLShader
189
190 # Create a new fragment shader
191 #
192 # The newly created instance should be checked using `is_ok`.
193 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
194 end
195
196 # An array of `Float` associated to a program variable
197 class VertexArray
198 var index: Int
199
200 # Number of data per vertex
201 var count: Int
202
203 protected var glfloat_array: GLfloatArray
204
205 init(index, count: Int, array: Array[Float])
206 do
207 self.index = index
208 self.count = count
209 self.glfloat_array = new GLfloatArray(array)
210 end
211
212 fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array)
213 private fun attrib_pointer_intern(index, count: Int, array: GLfloatArray) `{
214 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
215 `}
216
217 fun enable do enable_intern(index)
218 private fun enable_intern(index: Int) `{ glEnableVertexAttribArray(index); `}
219
220 fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count)
221 private fun draw_arrays_triangles_intern(index, count: Int) `{
222 glDrawArrays(GL_TRIANGLES, index, count);
223 `}
224 end
225
226 # Low level array of `Float`
227 extern class GLfloatArray `{GLfloat *`}
228 new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{
229 int i;
230 int len = Array_of_Float_length(array);
231 GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
232 for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
233 return vertex_array;
234 `}
235 end
236
237 # An OpenGL ES 2.0 error code
238 extern class GLError `{ GLenum `}
239 fun is_ok: Bool do return is_no_error
240 fun is_no_error: Bool `{ return recv == GL_NO_ERROR; `}
241 fun is_invalid_enum: Bool `{ return recv == GL_INVALID_ENUM; `}
242 fun is_invalid_value: Bool `{ return recv == GL_INVALID_VALUE; `}
243 fun is_invalid_operation: Bool `{ return recv == GL_INVALID_OPERATION; `}
244 fun is_invalid_framebuffer_operation: Bool `{ return recv == GL_INVALID_FRAMEBUFFER_OPERATION; `}
245 fun is_out_of_memory: Bool `{ return recv == GL_OUT_OF_MEMORY; `}
246
247 redef fun to_s
248 do
249 if is_no_error then return "No error"
250 if is_invalid_enum then return "Invalid enum"
251 if is_invalid_value then return "Invalid value"
252 if is_invalid_operation then return "Invalid operation"
253 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
254 if is_out_of_memory then return "Out of memory"
255 return "Truely unknown error"
256 end
257 end
258
259 # Clear the color buffer with `r`, `g`, `b`, `a`
260 protected fun gl_clear_color(r, g, b, a: Float) `{ glClearColor(r, g, b, a); `}
261
262 # Set the viewport
263 protected fun gl_viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `}
264
265 # Direct call to `glClear`, call with a combinaison of `gl_clear_color_buffer`,
266 # `gl_stencil_buffer_bit` and `gl_color_buffer_bit`.
267 private fun gl_clear(flag: Int) `{ glClear(flag); `}
268
269 protected fun gl_depth_buffer_bit: Int do return 0x0100
270 protected fun gl_stencil_buffer_bit: Int do return 0x0400
271 protected fun gl_color_buffer_bit: Int do return 0x4000
272
273 protected fun gl_clear_color_buffer do gl_clear(gl_color_buffer_bit)
274 protected fun gl_clear_depth_buffer do gl_clear(gl_depth_buffer_bit)
275 protected fun gl_clear_stencil_buffer do gl_clear(gl_stencil_buffer_bit)
276
277 protected fun gl_error: GLError `{ return glGetError(); `}
278 protected fun assert_no_gl_error
279 do
280 var error = gl_error
281 if not error.is_ok then
282 print "GL error: {error}"
283 abort
284 end
285 end
286
287 # Query the boolean value at `key`
288 private fun gl_get_bool(key: Int): Bool `{
289 GLboolean val;
290 glGetBooleanv(key, &val);
291 return val == GL_TRUE;
292 `}
293
294 # Query the floating point value at `key`
295 private fun gl_get_float(key: Int): Float `{
296 GLfloat val;
297 glGetFloatv(key, &val);
298 return val;
299 `}
300
301 # Query the integer value at `key`
302 private fun gl_get_int(key: Int): Int `{
303 GLint val;
304 glGetIntegerv(key, &val);
305 return val;
306 `}
307
308 # Does this driver support shader compilation?
309 #
310 # Should always return `true` in OpenGL ES 2.0 and 3.0.
311 fun gl_shader_compiler: Bool do return gl_get_bool(0x8DFA)