c78dc723122cd24abdcfa8f3e6722bb1962a8add
[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 ldflags("-lGLESv2")@android
37 end
38
39 import android::aware
40 intrude import c
41
42 in "C Header" `{
43 #include <GLES2/gl2.h>
44 `}
45
46 # OpenGL ES program to which we attach shaders
47 extern class GLProgram `{GLuint`}
48 # Create a new program
49 #
50 # The newly created instance should be checked using `is_ok`.
51 new `{ return glCreateProgram(); `}
52
53 # Is this a valid program?
54 fun is_ok: Bool `{ return glIsProgram(self); `}
55
56 # Attach a `shader` to this program
57 fun attach_shader(shader: GLShader) `{ glAttachShader(self, shader); `}
58
59 # Set the location for the attribute by `name`
60 fun bind_attrib_location(index: Int, name: String) import String.to_cstring `{
61 GLchar *c_name = String_to_cstring(name);
62 glBindAttribLocation(self, index, c_name);
63 `}
64
65 # Get the location of the attribute by `name`
66 #
67 # Returns `-1` if there is no active attribute named `name`.
68 fun attrib_location(name: String): Int import String.to_cstring `{
69 GLchar *c_name = String_to_cstring(name);
70 return glGetAttribLocation(self, c_name);
71 `}
72
73 # Get the location of the uniform by `name`
74 #
75 # Returns `-1` if there is no active uniform named `name`.
76 fun uniform_location(name: String): Int import String.to_cstring `{
77 GLchar *c_name = String_to_cstring(name);
78 return glGetUniformLocation(self, c_name);
79 `}
80
81 # Query information on this program
82 fun query(pname: Int): Int `{
83 int val;
84 glGetProgramiv(self, pname, &val);
85 return val;
86 `}
87
88 # Try to link this program
89 #
90 # Check result using `in_linked` and `info_log`.
91 fun link `{ glLinkProgram(self); `}
92
93 # Is this program linked?
94 fun is_linked: Bool do return query(0x8B82) != 0
95
96 # Use this program for the following operations
97 fun use `{ glUseProgram(self); `}
98
99 # Delete this program
100 fun delete `{ glDeleteProgram(self); `}
101
102 # Has this program been deleted?
103 fun is_deleted: Bool do return query(0x8B80) != 0
104
105 # Validate whether this program can be executed in the current OpenGL state
106 #
107 # Check results using `is_validated` and `info_log`.
108 fun validate `{ glValidateProgram(self); `}
109
110 # Boolean result of `validate`, must be called after `validate`
111 fun is_validated: Bool do return query(0x8B83) != 0
112
113 # Retrieve the information log of this program
114 #
115 # Useful with `link` and `validate`
116 fun info_log: String import NativeString.to_s `{
117 int size;
118 glGetProgramiv(self, GL_INFO_LOG_LENGTH, &size);
119 GLchar *msg = malloc(size);
120 glGetProgramInfoLog(self, size, NULL, msg);
121 return NativeString_to_s(msg);
122 `}
123
124 # Number of active uniform in this program
125 #
126 # This should be the number of uniforms declared in all shader, except
127 # unused uniforms which may have been optimized out.
128 fun n_active_uniforms: Int do return query(0x8B86)
129
130 # Length of the longest uniform name in this program, including `\n`
131 fun active_uniform_max_length: Int do return query(0x8B87)
132
133 # Number of active attributes in this program
134 #
135 # This should be the number of uniforms declared in all shader, except
136 # unused uniforms which may have been optimized out.
137 fun n_active_attributes: Int do return query(0x8B89)
138
139 # Length of the longest uniform name in this program, including `\n`
140 fun active_attribute_max_length: Int do return query(0x8B8A)
141
142 # Number of shaders attached to this program
143 fun n_attached_shaders: Int do return query(0x8B85)
144
145 # Name of the active attribute at `index`
146 fun active_attrib_name(index: Int): String
147 do
148 var max_size = active_attribute_max_length
149 return active_attrib_name_native(index, max_size).to_s
150 end
151 private fun active_attrib_name_native(index, max_size: Int): NativeString `{
152 // We get more values than we need, for compatibility. At least the
153 // NVidia driver tries to fill them even if NULL.
154
155 char *name = malloc(max_size);
156 int size;
157 GLenum type;
158 glGetActiveAttrib(self, index, max_size, NULL, &size, &type, name);
159 return name;
160 `}
161
162 # Size of the active attribute at `index`
163 fun active_attrib_size(index: Int): Int `{
164 int size;
165 GLenum type;
166 glGetActiveAttrib(self, index, 0, NULL, &size, &type, NULL);
167 return size;
168 `}
169
170 # Type of the active attribute at `index`
171 #
172 # May only be float related data types (single float, vectors and matrix).
173 fun active_attrib_type(index: Int): GLFloatDataType `{
174 int size;
175 GLenum type;
176 glGetActiveAttrib(self, index, 0, NULL, &size, &type, NULL);
177 return type;
178 `}
179
180 # Name of the active uniform at `index`
181 fun active_uniform_name(index: Int): String
182 do
183 var max_size = active_attribute_max_length
184 return active_uniform_name_native(index, max_size).to_s
185 end
186 private fun active_uniform_name_native(index, max_size: Int): NativeString `{
187 char *name = malloc(max_size);
188 int size;
189 GLenum type;
190 glGetActiveUniform(self, index, max_size, NULL, &size, &type, name);
191 return name;
192 `}
193
194 # Size of the active uniform at `index`
195 fun active_uniform_size(index: Int): Int `{
196 int size;
197 GLenum type;
198 glGetActiveUniform(self, index, 0, NULL, &size, &type, NULL);
199 return size;
200 `}
201
202 # Type of the active uniform at `index`
203 #
204 # May be any data type supported by OpenGL ES 2.0 shaders.
205 fun active_uniform_type(index: Int): GLDataType `{
206 int size;
207 GLenum type = 0;
208 glGetActiveUniform(self, index, 0, NULL, &size, &type, NULL);
209 return type;
210 `}
211 end
212
213 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
214 extern class GLShader `{GLuint`}
215 # Set the source of the shader
216 fun source=(code: NativeString) `{
217 glShaderSource(self, 1, (GLchar const **)&code, NULL);
218 `}
219
220 # Source of the shader, if available
221 #
222 # Returns `null` if the source is not available, usually when the shader
223 # was created from a binary file.
224 fun source: nullable String
225 do
226 var size = query(0x8B88)
227 if size == 0 then return null
228 return source_native(size).to_s
229 end
230
231 private fun source_native(size: Int): NativeString `{
232 GLchar *code = malloc(size);
233 glGetShaderSource(self, size, NULL, code);
234 return code;
235 `}
236
237 # Query information on this shader
238 protected fun query(pname: Int): Int `{
239 int val;
240 glGetShaderiv(self, pname, &val);
241 return val;
242 `}
243
244 # Try to compile `source` into a binary GPU program
245 #
246 # Check the result using `is_compiled` and `info_log`
247 fun compile `{ glCompileShader(self); `}
248
249 # Has this shader been compiled?
250 fun is_compiled: Bool do return query(0x8B81) != 0
251
252 # Delete this shader
253 fun delete `{ glDeleteShader(self); `}
254
255 # Has this shader been deleted?
256 fun is_deleted: Bool do return query(0x8B80) != 0
257
258 # Is this a valid shader?
259 fun is_ok: Bool `{ return glIsShader(self); `}
260
261 # Retrieve the information log of this shader
262 #
263 # Useful with `link` and `validate`
264 fun info_log: String import NativeString.to_s `{
265 int size;
266 glGetShaderiv(self, GL_INFO_LOG_LENGTH, &size);
267 GLchar *msg = malloc(size);
268 glGetShaderInfoLog(self, size, NULL, msg);
269 return NativeString_to_s(msg);
270 `}
271 end
272
273 # An OpenGL ES 2.0 fragment shader
274 extern class GLFragmentShader
275 super GLShader
276
277 # Create a new fragment shader
278 #
279 # The newly created instance should be checked using `is_ok`.
280 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
281 end
282
283 # An OpenGL ES 2.0 vertex shader
284 extern class GLVertexShader
285 super GLShader
286
287 # Create a new fragment shader
288 #
289 # The newly created instance should be checked using `is_ok`.
290 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
291 end
292
293 # An array of `Float` associated to a program variable
294 class VertexArray
295 var index: Int
296
297 # Number of data per vertex
298 var count: Int
299
300 protected var glfloat_array: NativeGLfloatArray
301
302 init(index, count: Int, array: Array[Float])
303 do
304 self.index = index
305 self.count = count
306 self.glfloat_array = new NativeGLfloatArray(array.length)
307 for k in [0..array.length[ do
308 glfloat_array[k] = array[k]
309 end
310 end
311
312 fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array)
313 private fun attrib_pointer_intern(index, count: Int, array: NativeGLfloatArray) `{
314 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
315 `}
316
317 # Enable this vertex attribute array
318 fun enable do glEnableVertexAttribArray(index)
319
320 # Disable this vertex attribute array
321 fun disable do glDisableVertexAttribArray(index)
322 end
323
324 # Enable the generic vertex attribute array at `index`
325 fun glEnableVertexAttribArray(index: Int) `{ glEnableVertexAttribArray(index); `}
326
327 # Disable the generic vertex attribute array at `index`
328 fun glDisableVertexAttribArray(index: Int) `{ glDisableVertexAttribArray(index); `}
329
330 # Render primitives from array data
331 fun glDrawArrays(mode: GLDrawMode, from, count: Int) `{ glDrawArrays(mode, from, count); `}
332
333 # Define an array of generic vertex attribute data
334 fun glVertexAttribPointer(index, size: Int, typ: GLDataType, normalized: Bool, stride: Int, array: NativeGLfloatArray) `{
335 glVertexAttribPointer(index, size, typ, normalized, stride, array);
336 `}
337
338 # Specify the value of a generic vertex attribute
339 fun glVertexAttrib1f(index: Int, x: Float) `{ glVertexAttrib1f(index, x); `}
340
341 # Specify the value of a generic vertex attribute
342 fun glVertexAttrib2f(index: Int, x, y: Float) `{ glVertexAttrib2f(index, x, y); `}
343
344 # Specify the value of a generic vertex attribute
345 fun glVertexAttrib3f(index: Int, x, y, z: Float) `{ glVertexAttrib3f(index, x, y, z); `}
346
347 # Specify the value of a generic vertex attribute
348 fun glVertexAttrib4f(index: Int, x, y, z, w: Float) `{ glVertexAttrib4f(index, x, y, z, w); `}
349
350 # Specify the value of a uniform variable for the current program object
351 fun glUniform1i(index, x: Int) `{ glUniform1i(index, x); `}
352
353 # Specify the value of a uniform variable for the current program object
354 fun glUniform2i(index, x, y: Int) `{ glUniform2i(index, x, y); `}
355
356 # Specify the value of a uniform variable for the current program object
357 fun glUniform3i(index, x, y, z: Int) `{ glUniform3i(index, x, y, z); `}
358
359 # Specify the value of a uniform variable for the current program object
360 fun glUniform4i(index, x, y, z, w: Int) `{ glUniform4i(index, x, y, z, w); `}
361
362 # TODO glUniform*f
363
364 # Low level array of `Float`
365 class GLfloatArray
366 super CArray[Float]
367 redef type NATIVE: NativeGLfloatArray
368
369 init do native_array = new NativeGLfloatArray(length)
370
371 # Create with the content of `array`
372 new from(array: Array[Float])
373 do
374 var arr = new GLfloatArray(array.length)
375 arr.fill_from array
376 return arr
377 end
378
379 # Fill with the content of `array`
380 fun fill_from(array: Array[Float])
381 do
382 assert length >= array.length
383 for k in [0..array.length[ do
384 self[k] = array[k]
385 end
386 end
387 end
388
389 # An array of `GLfloat` in C (`GLfloat*`)
390 extern class NativeGLfloatArray `{ GLfloat* `}
391 super NativeCArray
392 redef type E: Float
393
394 new(size: Int) `{ return calloc(size, sizeof(GLfloat)); `}
395
396 redef fun [](index) `{ return self[index]; `}
397 redef fun []=(index, val) `{ self[index] = val; `}
398
399 redef fun +(offset) `{ return self + offset; `}
400 end
401
402 # General type for OpenGL enumerations
403 extern class GLEnum `{ GLenum `}
404
405 redef fun hash `{ return self; `}
406
407 redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
408 end
409
410 # An OpenGL ES 2.0 error code
411 extern class GLError
412 super GLEnum
413
414 # Is there no error?
415 fun is_ok: Bool do return is_no_error
416
417 # Is this not an error?
418 fun is_no_error: Bool `{ return self == GL_NO_ERROR; `}
419
420 fun is_invalid_enum: Bool `{ return self == GL_INVALID_ENUM; `}
421 fun is_invalid_value: Bool `{ return self == GL_INVALID_VALUE; `}
422 fun is_invalid_operation: Bool `{ return self == GL_INVALID_OPERATION; `}
423 fun is_invalid_framebuffer_operation: Bool `{ return self == GL_INVALID_FRAMEBUFFER_OPERATION; `}
424 fun is_out_of_memory: Bool `{ return self == GL_OUT_OF_MEMORY; `}
425
426 redef fun to_s
427 do
428 if is_no_error then return "No error"
429 if is_invalid_enum then return "Invalid enum"
430 if is_invalid_value then return "Invalid value"
431 if is_invalid_operation then return "Invalid operation"
432 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
433 if is_out_of_memory then return "Out of memory"
434 return "Truely unknown error"
435 end
436 end
437
438 fun assert_no_gl_error
439 do
440 var error = gl.error
441 if not error.is_ok then
442 print "GL error: {error}"
443 abort
444 end
445 end
446
447 # Texture unit, the number of texture units is implementation dependent
448 extern class GLTextureUnit
449 super GLEnum
450 end
451
452 fun gl_TEXTURE0: GLTextureUnit `{ return GL_TEXTURE0; `}
453 fun gl_TEXTURE1: GLTextureUnit `{ return GL_TEXTURE1; `}
454 fun gl_TEXTURE2: GLTextureUnit `{ return GL_TEXTURE2; `}
455 fun gl_TEXTURE3: GLTextureUnit `{ return GL_TEXTURE3; `}
456 fun gl_TEXTURE4: GLTextureUnit `{ return GL_TEXTURE4; `}
457 fun gl_TEXTURE5: GLTextureUnit `{ return GL_TEXTURE5; `}
458 fun gl_TEXTURE6: GLTextureUnit `{ return GL_TEXTURE6; `}
459 fun gl_TEXTURE7: GLTextureUnit `{ return GL_TEXTURE7; `}
460 fun gl_TEXTURE8: GLTextureUnit `{ return GL_TEXTURE8; `}
461 fun gl_TEXTURE9: GLTextureUnit `{ return GL_TEXTURE9; `}
462 fun gl_TEXTURE10: GLTextureUnit `{ return GL_TEXTURE10; `}
463 fun gl_TEXTURE11: GLTextureUnit `{ return GL_TEXTURE11; `}
464 fun gl_TEXTURE12: GLTextureUnit `{ return GL_TEXTURE12; `}
465 fun gl_TEXTURE13: GLTextureUnit `{ return GL_TEXTURE13; `}
466 fun gl_TEXTURE14: GLTextureUnit `{ return GL_TEXTURE14; `}
467 fun gl_TEXTURE15: GLTextureUnit `{ return GL_TEXTURE15; `}
468 fun gl_TEXTURE16: GLTextureUnit `{ return GL_TEXTURE16; `}
469 fun gl_TEXTURE17: GLTextureUnit `{ return GL_TEXTURE17; `}
470 fun gl_TEXTURE18: GLTextureUnit `{ return GL_TEXTURE18; `}
471 fun gl_TEXTURE19: GLTextureUnit `{ return GL_TEXTURE19; `}
472 fun gl_TEXTURE20: GLTextureUnit `{ return GL_TEXTURE20; `}
473 fun gl_TEXTURE21: GLTextureUnit `{ return GL_TEXTURE21; `}
474 fun gl_TEXTURE22: GLTextureUnit `{ return GL_TEXTURE22; `}
475 fun gl_TEXTURE23: GLTextureUnit `{ return GL_TEXTURE23; `}
476 fun gl_TEXTURE24: GLTextureUnit `{ return GL_TEXTURE24; `}
477 fun gl_TEXTURE25: GLTextureUnit `{ return GL_TEXTURE25; `}
478 fun gl_TEXTURE26: GLTextureUnit `{ return GL_TEXTURE26; `}
479 fun gl_TEXTURE27: GLTextureUnit `{ return GL_TEXTURE27; `}
480 fun gl_TEXTURE28: GLTextureUnit `{ return GL_TEXTURE28; `}
481 fun gl_TEXTURE29: GLTextureUnit `{ return GL_TEXTURE29; `}
482 fun gl_TEXTURE30: GLTextureUnit `{ return GL_TEXTURE30; `}
483 fun gl_TEXTURE31: GLTextureUnit `{ return GL_TEXTURE31; `}
484
485 # Texture unit at `offset` after `gl_TEXTURE0`
486 fun gl_TEXTURE(offset: Int): GLTextureUnit `{ return GL_TEXTURE0 + offset; `}
487
488 # Generate `n` texture names
489 fun glGenTextures(n: Int): Array[Int]
490 do
491 var array = new CIntArray(n)
492 native_glGenTextures(n, array.native_array)
493 var a = array.to_a
494 array.destroy
495 return a
496 end
497
498 private fun native_glGenTextures(n: Int, textures: NativeCIntArray) `{
499 glGenTextures(n, (GLuint*)textures);
500 `}
501
502 # Select server-side active texture unit
503 fun glActiveTexture(texture: GLTextureUnit) `{ glActiveTexture(texture); `}
504
505 # Bind the named `texture` to a `target`
506 fun glBindTexture(target: GLTextureTarget, texture: Int) `{
507 glBindTexture(target, texture);
508 `}
509
510 # Delete named textures
511 fun glDeleteTextures(textures: SequenceRead[Int])
512 do
513 var n = textures.length
514 var array = new CIntArray.from(textures)
515 native_glDeleteTextures(n, array.native_array)
516 array.destroy
517 end
518
519 private fun native_glDeleteTextures(n: Int, textures: NativeCIntArray) `{
520 glDeleteTextures(n, (const GLuint *)textures);
521 `}
522
523 # Does `name` corresponds to a texture?
524 fun glIsTexture(name: Int): Bool `{ return glIsTexture(name); `}
525
526 # Set pixel storage modes
527 fun glPixelStorei(parameter: GLPack, val: Int) `{ glPixelStorei(parameter, val); `}
528
529 # Symbolic name of the parameter to be set with `glPixelStorei`
530 extern class GLPack
531 super GLEnum
532 end
533
534 # Parameter to specify the alignment requirements for the start of each pixel row in memory
535 fun gl_PACK_ALIGNEMENT: GLPack `{ return GL_PACK_ALIGNMENT; `}
536
537 # Parameter to specify the alignment requirements for the start of each pixel row in memory
538 fun gl_UNPACK_ALIGNEMENT: GLPack `{ return GL_UNPACK_ALIGNMENT; `}
539
540 # TODO GL_PACK_ROW_LENGTH, GL_PACK_IMAGE_HEIGHT, GL_PACK_SKIP_PIXELS, GL_PACK_SKIP_ROWS, GL_PACK_SKIP_IMAGES
541 # GL_UNPACK_ROW_LENGTH, GL_UNPACK_IMAGE_HEIGHT, GL_UNPACK_SKIP_PIXELS, GL_UNPACK_SKIP_ROWS, GL_UNPACK_SKIP_IMAGES
542
543 # Specify a two-dimensional texture image
544 fun glTexImage2D(target: GLTextureTarget, level, internalformat, width, height, border: Int,
545 format: GLPixelFormat, typ: GLPixelType, data: NativeCByteArray) `{
546 glTexImage2D(target, level, internalformat, width, height, border, format, typ, data);
547 `}
548
549 # Specify a two-dimensional texture subimage
550 fun glTexSubImage2D(target: GLTextureTarget,
551 level, xoffset, yoffset, width, height, border: Int,
552 format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
553 glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, typ, data);
554 `}
555
556 # Copy pixels into a 2D texture image
557 fun glCopyTexImage2D(target: GLTextureTarget, level: Int, internalformat: GLPixelFormat,
558 x, y, width, height, border: Int) `{
559 glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);
560 `}
561
562 # Copy a two-dimensional texture subimage
563 fun glCopyTexSubImage2D(target: GLTextureTarget, level, xoffset, yoffset, x, y, width, height: Int) `{
564 glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
565 `}
566
567 # Texture minifying and magnifying function
568 extern class GLTextureFilter
569 super GLEnum
570 end
571
572 fun gl_NEAREST: GLTextureFilter `{ return GL_NEAREST; `}
573 fun gl_LINEAR: GLTextureFilter `{ return GL_LINEAR; `}
574 fun gl_NEAREST_MIPMAP_NEAREST: GLTextureFilter `{ return GL_NEAREST_MIPMAP_NEAREST; `}
575 fun gl_LINEAR_MIPMAP_NEAREST: GLTextureFilter `{ return GL_LINEAR_MIPMAP_NEAREST; `}
576 fun gl_NEAREST_MIPMAP_NINEAR: GLTextureFilter `{ return GL_NEAREST_MIPMAP_LINEAR; `}
577 fun gl_LINEAR_MIPMAP_LINEAR: GLTextureFilter `{ return GL_LINEAR_MIPMAP_LINEAR; `}
578
579 # Wrap parameter of a texture
580 #
581 # Used by: `tex_parameter_wrap_*`
582 extern class GLTextureWrap
583 super GLEnum
584
585 new clamp_to_edge `{ return GL_CLAMP_TO_EDGE; `}
586 new mirrored_repeat `{ return GL_MIRRORED_REPEAT; `}
587 new repeat `{ return GL_REPEAT; `}
588 end
589
590 # Target texture
591 extern class GLTextureTarget
592 super GLEnum
593 end
594
595 # Two-dimensional texture
596 fun gl_TEXTURE_2D: GLTextureTarget `{ return GL_TEXTURE_2D; `}
597
598 # Cube map texture
599 fun gl_TEXTURE_CUBE_MAP: GLTextureTarget `{ return GL_TEXTURE_CUBE_MAP; `}
600
601 # TODO GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
602 # GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
603
604 # A server-side capability
605 class GLCap
606
607 # TODO private init
608
609 # Internal OpenGL integer for this capability
610 private var val: Int
611
612 # Enable this server-side capability
613 fun enable do enable_native(val)
614 private fun enable_native(cap: Int) `{ glEnable(cap); `}
615
616 # Disable this server-side capability
617 fun disable do disable_native(val)
618 private fun disable_native(cap: Int) `{ glDisable(cap); `}
619
620 redef fun hash do return val
621 redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
622 end
623
624 # Generate `n` renderbuffer object names
625 fun glGenRenderbuffers(n: Int): Array[Int]
626 do
627 var array = new CIntArray(n)
628 native_glGenRenderbuffers(n, array.native_array)
629 var a = array.to_a
630 array.destroy
631 return a
632 end
633
634 private fun native_glGenRenderbuffers(n: Int, renderbuffers: NativeCIntArray) `{
635 glGenRenderbuffers(n, (GLuint *)renderbuffers);
636 `}
637
638 # Determine if `name` corresponds to a renderbuffer object
639 fun glIsRenderbuffer(name: Int): Bool `{
640 return glIsRenderbuffer(name);
641 `}
642
643 # Delete named renderbuffer objects
644 fun glDeleteRenderbuffers(renderbuffers: SequenceRead[Int])
645 do
646 var n = renderbuffers.length
647 var array = new CIntArray.from(renderbuffers)
648 native_glDeleteRenderbuffers(n, array.native_array)
649 array.destroy
650 end
651
652 private fun native_glDeleteRenderbuffers(n: Int, renderbuffers: NativeCIntArray) `{
653 return glDeleteRenderbuffers(n, (const GLuint *)renderbuffers);
654 `}
655
656 # Attach a renderbuffer object to a framebuffer object
657 fun glFramebufferRenderbuffer(target: GLFramebufferTarget, attachment: GLAttachment,
658 renderbuffertarget: GLRenderbufferTarget, renderbuffer: Int) `{
659 glFramebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer);
660 `}
661
662 # Establish data storage, `format` and dimensions of the `target` renderbuffer object's image
663 fun glRenderbufferStorage(target: GLRenderbufferTarget, format: GLRenderbufferFormat, width, height: Int) `{
664 glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
665 `}
666
667 # Format for a renderbuffer
668 extern class GLRenderbufferFormat
669 super GLEnum
670 end
671
672 # 4 red, 4 green, 4 blue, 4 alpha bits format
673 fun gl_RGBA4: GLRenderbufferFormat `{ return GL_RGBA4; `}
674
675 # 5 red, 6 green, 5 blue bits format
676 fun gl_RGB565: GLRenderbufferFormat `{ return GL_RGB565; `}
677
678 # 5 red, 5 green, 5 blue, 1 alpha bits format
679 fun gl_RGB_A1: GLRenderbufferFormat `{ return GL_RGB5_A1; `}
680
681 # 16 depth bits format
682 fun gl_DEPTH_COMPNENT16: GLRenderbufferFormat `{ return GL_DEPTH_COMPONENT16; `}
683
684 # 8 stencil bits format
685 fun gl_STENCIL_INDEX8: GLRenderbufferFormat `{ return GL_STENCIL_INDEX8; `}
686
687 # Renderbuffer attachment point to a framebuffer
688 extern class GLAttachment
689 super GLEnum
690 end
691
692 # First color attachment point
693 fun gl_COLOR_ATTACHMENT0: GLAttachment `{ return GL_COLOR_ATTACHMENT0; `}
694
695 # Depth attachment point
696 fun gl_DEPTH_ATTACHMENT: GLAttachment `{ return GL_DEPTH_ATTACHMENT; `}
697
698 # Stencil attachment
699 fun gl_STENCIL_ATTACHMENT: GLAttachment `{ return GL_STENCIL_ATTACHMENT; `}
700
701 redef class Sys
702 private var gles = new GLES is lazy
703 end
704
705 # Entry points to OpenGL ES 2.0 services
706 fun gl: GLES do return sys.gles
707
708 # OpenGL ES 2.0 services
709 class GLES
710
711 # Clear the color buffer to `red`, `green`, `blue` and `alpha`
712 fun clear_color(red, green, blue, alpha: Float) `{
713 glClearColor(red, green, blue, alpha);
714 `}
715
716 # Set the viewport
717 fun viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `}
718
719 # Specify mapping of depth values from normalized device coordinates to window coordinates
720 #
721 # Default at `gl_depth_range(0.0, 1.0)`
722 fun depth_range(near, far: Float) `{ glDepthRangef(near, far); `}
723
724 # Define front- and back-facing polygons
725 #
726 # Front-facing polygons are clockwise if `value`, counter-clockwise otherwise.
727 fun front_face=(value: Bool) `{ glFrontFace(value? GL_CW: GL_CCW); `}
728
729 # Specify whether front- or back-facing polygons can be culled, default is `back` only
730 #
731 # One or both of `front` or `back` must be `true`. If you want to deactivate culling
732 # use `(new GLCap.cull_face).disable`.
733 #
734 # Require: `front or back`
735 fun cull_face(front, back: Bool)
736 do
737 assert not (front or back)
738 cull_face_native(front, back)
739 end
740
741 private fun cull_face_native(front, back: Bool) `{
742 glCullFace(front? back? GL_FRONT_AND_BACK: GL_BACK: GL_FRONT);
743 `}
744
745 # Clear the `buffer`
746 fun clear(buffer: GLBuffer) `{ glClear(buffer); `}
747
748 # Last error from OpenGL ES 2.0
749 fun error: GLError `{ return glGetError(); `}
750
751 # Query the boolean value at `key`
752 private fun get_bool(key: Int): Bool `{
753 GLboolean val;
754 glGetBooleanv(key, &val);
755 return val == GL_TRUE;
756 `}
757
758 # Query the floating point value at `key`
759 private fun get_float(key: Int): Float `{
760 GLfloat val;
761 glGetFloatv(key, &val);
762 return val;
763 `}
764
765 # Query the integer value at `key`
766 private fun get_int(key: Int): Int `{
767 GLint val;
768 glGetIntegerv(key, &val);
769 return val;
770 `}
771
772 # Does this driver support shader compilation?
773 #
774 # Should always return `true` in OpenGL ES 2.0 and 3.0.
775 fun shader_compiler: Bool do return get_bool(0x8DFA)
776
777 # Enable or disable writing into the depth buffer
778 fun depth_mask(value: Bool) `{ glDepthMask(value); `}
779
780 # Set the scale and units used to calculate depth values
781 fun polygon_offset(factor, units: Float) `{ glPolygonOffset(factor, units); `}
782
783 # Specify the width of rasterized lines
784 fun line_width(width: Float) `{ glLineWidth(width); `}
785
786 # Set the pixel arithmetic for the blending operations
787 #
788 # Defaultvalues before assignation:
789 # * `src_factor`: `GLBlendFactor::one`
790 # * `dst_factor`: `GLBlendFactor::zero`
791 fun blend_func(src_factor, dst_factor: GLBlendFactor) `{
792 glBlendFunc(src_factor, dst_factor);
793 `}
794
795 # Specify the value used for depth buffer comparisons
796 #
797 # Default value is `GLDepthFunc::less`
798 #
799 # Foreign: glDepthFunc
800 fun depth_func(func: GLDepthFunc) `{ glDepthFunc(func); `}
801
802 # Copy a block of pixels from the framebuffer of `fomat` and `typ` at `data`
803 #
804 # Foreign: glReadPixel
805 fun read_pixels(x, y, width, height: Int, format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
806 glReadPixels(x, y, width, height, format, typ, data);
807 `}
808
809 # Set the texture minifying function
810 #
811 # Foreign: glTexParameter with GL_TEXTURE_MIN_FILTER
812 fun tex_parameter_min_filter(target: GLTextureTarget, value: GLTextureFilter) `{
813 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, value);
814 `}
815
816 # Set the texture magnification function
817 #
818 # Foreign: glTexParameter with GL_TEXTURE_MAG_FILTER
819 fun tex_parameter_mag_filter(target: GLTextureTarget, value: GLTextureFilter) `{
820 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, value);
821 `}
822
823 # Set the texture wrap parameter for coordinates _s_
824 #
825 # Foreign: glTexParameter with GL_TEXTURE_WRAP_S
826 fun tex_parameter_wrap_s(target: GLTextureTarget, value: GLTextureWrap) `{
827 glTexParameteri(target, GL_TEXTURE_WRAP_S, value);
828 `}
829
830 # Set the texture wrap parameter for coordinates _t_
831 #
832 # Foreign: glTexParameter with GL_TEXTURE_WRAP_T
833 fun tex_parameter_wrap_t(target: GLTextureTarget, value: GLTextureWrap) `{
834 glTexParameteri(target, GL_TEXTURE_WRAP_T, value);
835 `}
836
837 # Render primitives from array data
838 #
839 # Foreign: glDrawArrays
840 fun draw_arrays(mode: GLDrawMode, from, count: Int) `{ glDrawArrays(mode, from, count); `}
841
842 # OpenGL server-side capabilities
843 var capabilities = new GLCapabilities is lazy
844 end
845
846 # Bind `framebuffer` to a framebuffer target
847 #
848 # In OpenGL ES 2.0, `target` must be `gl_FRAMEBUFFER`.
849 fun glBindFramebuffer(target: GLFramebufferTarget, framebuffer: Int) `{
850 glBindFramebuffer(target, framebuffer);
851 `}
852
853 # Target of `glBindFramebuffer`
854 extern class GLFramebufferTarget
855 super GLEnum
856 end
857
858 # Target both reading and writing on the framebuffer with `glBindFramebuffer`
859 fun gl_FRAMEBUFFER: GLFramebufferTarget `{ return GL_FRAMEBUFFER; `}
860
861 # Bind `renderbuffer` to a renderbuffer target
862 #
863 # In OpenGL ES 2.0, `target` must be `gl_RENDERBUFFER`.
864 fun glBindRenderbuffer(target: GLRenderbufferTarget, renderbuffer: Int) `{
865 glBindRenderbuffer(target, renderbuffer);
866 `}
867
868 # Target of `glBindRenderbuffer`
869 extern class GLRenderbufferTarget
870 super GLEnum
871 end
872
873 # Target a renderbuffer with `glBindRenderbuffer`
874 fun gl_RENDERBUFFER: GLRenderbufferTarget `{ return GL_RENDERBUFFER; `}
875
876 # Specify implementation specific hints
877 fun glHint(target: GLHintTarget, mode: GLHintMode) `{
878 glHint(target, mode);
879 `}
880
881 # Generate and fill set of mipmaps for the texture object `target`
882 fun glGenerateMipmap(target: GLTextureTarget) `{ glGenerateMipmap(target); `}
883
884 # Bind the named `buffer` object
885 fun glBindBuffer(target: GLArrayBuffer, buffer: Int) `{ glBindBuffer(target, buffer); `}
886
887 # Target to which bind the buffer with `glBindBuffer`
888 extern class GLArrayBuffer
889 super GLEnum
890 end
891
892 # Array buffer target
893 fun gl_ARRAY_BUFFER: GLArrayBuffer `{ return GL_ARRAY_BUFFER; `}
894
895 # Element array buffer
896 fun gl_ELEMENT_ARRAY_BUFFER: GLArrayBuffer `{ return GL_ELEMENT_ARRAY_BUFFER; `}
897
898 # Completeness status of a framebuffer object
899 fun glCheckFramebufferStatus(target: GLFramebufferTarget): GLFramebufferStatus `{
900 return glCheckFramebufferStatus(target);
901 `}
902
903 # Return value of `glCheckFramebufferStatus`
904 extern class GLFramebufferStatus
905 super GLEnum
906
907 redef fun to_s
908 do
909 if self == gl_FRAMEBUFFER_COMPLETE then return "complete"
910 if self == gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT then return "incomplete attachment"
911 if self == gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS then return "incomplete dimension"
912 if self == gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT then return "incomplete missing attachment"
913 if self == gl_FRAMEBUFFER_UNSUPPORTED then return "unsupported"
914 return "unknown"
915 end
916 end
917
918 # The framebuffer is complete
919 fun gl_FRAMEBUFFER_COMPLETE: GLFramebufferStatus `{
920 return GL_FRAMEBUFFER_COMPLETE;
921 `}
922
923 # Not all framebuffer attachment points are framebuffer attachment complete
924 fun gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: GLFramebufferStatus `{
925 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
926 `}
927
928 # Not all attached images have the same width and height
929 fun gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: GLFramebufferStatus `{
930 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
931 `}
932
933 # No images are attached to the framebuffer
934 fun gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: GLFramebufferStatus `{
935 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
936 `}
937
938 # The combination of internal formats of the attached images violates an implementation-dependent set of restrictions
939 fun gl_FRAMEBUFFER_UNSUPPORTED: GLFramebufferStatus `{
940 return GL_FRAMEBUFFER_UNSUPPORTED;
941 `}
942
943 # Hint target for `glHint`
944 extern class GLHintTarget
945 super GLEnum
946 end
947
948 # Indicates the quality of filtering when generating mipmap images
949 fun gl_GENERATE_MIPMAP_HINT: GLHintTarget `{ return GL_GENERATE_MIPMAP_HINT; `}
950
951 # Hint mode for `glHint`
952 extern class GLHintMode
953 super GLEnum
954 end
955
956 # The most efficient option should be chosen
957 fun gl_FASTEST: GLHintMode `{ return GL_FASTEST; `}
958
959 # The most correct, or highest quality, option should be chosen
960 fun gl_NICEST: GLHintMode `{ return GL_NICEST; `}
961
962 # No preference
963 fun gl_DONT_CARE: GLHintMode `{ return GL_DONT_CARE; `}
964
965 # Attach a level of a texture object as a logical buffer to the currently bound framebuffer object
966 fun glFramebufferTexture2D(target: GLFramebufferTarget, attachment: GLAttachment,
967 texture_target: GLTextureTarget, texture, level: Int) `{
968 glFramebufferTexture2D(target, attachment, texture_target, texture, level);
969 `}
970
971 # Entry point to OpenGL server-side capabilities
972 class GLCapabilities
973
974 # GL capability: blend the computed fragment color values
975 #
976 # Foreign: GL_BLEND
977 var blend: GLCap is lazy do return new GLCap(0x0BE2)
978
979 # GL capability: cull polygons based of their winding in window coordinates
980 #
981 # Foreign: GL_CULL_FACE
982 var cull_face: GLCap is lazy do return new GLCap(0x0B44)
983
984 # GL capability: do depth comparisons and update the depth buffer
985 #
986 # Foreign: GL_DEPTH_TEST
987 var depth_test: GLCap is lazy do return new GLCap(0x0B71)
988
989 # GL capability: dither color components or indices before they are written to the color buffer
990 #
991 # Foreign: GL_DITHER
992 var dither: GLCap is lazy do return new GLCap(0x0BE2)
993
994 # GL capability: add an offset to depth values of a polygon fragment before depth test
995 #
996 # Foreign: GL_POLYGON_OFFSET_FILL
997 var polygon_offset_fill: GLCap is lazy do return new GLCap(0x8037)
998
999 # GL capability: compute a temporary coverage value where each bit is determined by the alpha value at the corresponding location
1000 #
1001 # Foreign: GL_SAMPLE_ALPHA_TO_COVERAGE
1002 var sample_alpha_to_coverage: GLCap is lazy do return new GLCap(0x809E)
1003
1004 # GL capability: AND the fragment coverage with the temporary coverage value
1005 #
1006 # Foreign: GL_SAMPLE_COVERAGE
1007 var sample_coverage: GLCap is lazy do return new GLCap(0x80A0)
1008
1009 # GL capability: discard fragments that are outside the scissor rectangle
1010 #
1011 # Foreign: GL_SCISSOR_TEST
1012 var scissor_test: GLCap is lazy do return new GLCap(0x0C11)
1013
1014 # GL capability: do stencil testing and update the stencil buffer
1015 #
1016 # Foreign: GL_STENCIL_TEST
1017 var stencil_test: GLCap is lazy do return new GLCap(0x0B90)
1018 end
1019
1020 # Float related data types of OpenGL ES 2.0 shaders
1021 #
1022 # Only data types supported by shader attributes, as seen with
1023 # `GLProgram::active_attrib_type`.
1024 extern class GLFloatDataType
1025 super GLEnum
1026
1027 fun is_float: Bool `{ return self == GL_FLOAT; `}
1028 fun is_float_vec2: Bool `{ return self == GL_FLOAT_VEC2; `}
1029 fun is_float_vec3: Bool `{ return self == GL_FLOAT_VEC3; `}
1030 fun is_float_vec4: Bool `{ return self == GL_FLOAT_VEC4; `}
1031 fun is_float_mat2: Bool `{ return self == GL_FLOAT_MAT2; `}
1032 fun is_float_mat3: Bool `{ return self == GL_FLOAT_MAT3; `}
1033 fun is_float_mat4: Bool `{ return self == GL_FLOAT_MAT4; `}
1034
1035 # Instances of `GLFloatDataType` can be equal to instances of `GLDataType`
1036 redef fun ==(o)
1037 do
1038 return o != null and o isa GLFloatDataType and o.hash == self.hash
1039 end
1040 end
1041
1042 # All data types of OpenGL ES 2.0 shaders
1043 #
1044 # These types can be used by shader uniforms, as seen with
1045 # `GLProgram::active_uniform_type`.
1046 extern class GLDataType
1047 super GLFloatDataType
1048
1049 fun is_int: Bool `{ return self == GL_INT; `}
1050 fun is_int_vec2: Bool `{ return self == GL_INT_VEC2; `}
1051 fun is_int_vec3: Bool `{ return self == GL_INT_VEC3; `}
1052 fun is_int_vec4: Bool `{ return self == GL_INT_VEC4; `}
1053 fun is_bool: Bool `{ return self == GL_BOOL; `}
1054 fun is_bool_vec2: Bool `{ return self == GL_BOOL_VEC2; `}
1055 fun is_bool_vec3: Bool `{ return self == GL_BOOL_VEC3; `}
1056 fun is_bool_vec4: Bool `{ return self == GL_BOOL_VEC4; `}
1057 fun is_sampler_2d: Bool `{ return self == GL_SAMPLER_2D; `}
1058 fun is_sampler_cube: Bool `{ return self == GL_SAMPLER_CUBE; `}
1059 end
1060
1061 # Kind of primitives to render with `GLES::draw_arrays`
1062 extern class GLDrawMode
1063 super GLEnum
1064
1065 new points `{ return GL_POINTS; `}
1066 new line_strip `{ return GL_LINE_STRIP; `}
1067 new line_loop `{ return GL_LINE_LOOP; `}
1068 new lines `{ return GL_LINES; `}
1069 new triangle_strip `{ return GL_TRIANGLE_STRIP; `}
1070 new triangle_fan `{ return GL_TRIANGLE_FAN; `}
1071 new triangles `{ return GL_TRIANGLES; `}
1072 end
1073
1074 # Pixel arithmetic for blending operations
1075 #
1076 # Used by `GLES::blend_func`
1077 extern class GLBlendFactor
1078 super GLEnum
1079
1080 new zero `{ return GL_ZERO; `}
1081 new one `{ return GL_ONE; `}
1082 new src_color `{ return GL_SRC_COLOR; `}
1083 new one_minus_src_color `{ return GL_ONE_MINUS_SRC_COLOR; `}
1084 new dst_color `{ return GL_DST_COLOR; `}
1085 new one_minus_dst_color `{ return GL_ONE_MINUS_DST_COLOR; `}
1086 new src_alpha `{ return GL_SRC_ALPHA; `}
1087 new one_minus_src_alpha `{ return GL_ONE_MINUS_SRC_ALPHA; `}
1088 new dst_alpha `{ return GL_DST_ALPHA; `}
1089 new one_minus_dst_alpha `{ return GL_ONE_MINUS_DST_ALPHA; `}
1090 new constant_color `{ return GL_CONSTANT_COLOR; `}
1091 new one_minus_constant_color `{ return GL_ONE_MINUS_CONSTANT_COLOR; `}
1092 new constant_alpha `{ return GL_CONSTANT_ALPHA; `}
1093 new one_minus_constant_alpha `{ return GL_ONE_MINUS_CONSTANT_ALPHA; `}
1094
1095 # Used for destination only
1096 new src_alpha_saturate `{ return GL_SRC_ALPHA_SATURATE; `}
1097 end
1098
1099 # Condition under which a pixel will be drawn
1100 #
1101 # Used by `GLES::depth_func`
1102 extern class GLDepthFunc
1103 super GLEnum
1104
1105 new never `{ return GL_NEVER; `}
1106 new less `{ return GL_LESS; `}
1107 new equal `{ return GL_EQUAL; `}
1108 new lequal `{ return GL_LEQUAL; `}
1109 new greater `{ return GL_GREATER; `}
1110 new not_equal `{ return GL_NOTEQUAL; `}
1111 new gequal `{ return GL_GEQUAL; `}
1112 new always `{ return GL_ALWAYS; `}
1113 end
1114
1115 # Format of pixel data
1116 #
1117 # Used by `GLES::read_pixels`
1118 extern class GLPixelFormat
1119 super GLEnum
1120
1121 new alpha `{ return GL_ALPHA; `}
1122 new rgb `{ return GL_RGB; `}
1123 new rgba `{ return GL_RGBA; `}
1124 end
1125
1126 # Data type of pixel data
1127 #
1128 # Used by `GLES::read_pixels`
1129 extern class GLPixelType
1130 super GLEnum
1131
1132 new unsigned_byte `{ return GL_UNSIGNED_BYTE; `}
1133 new unsigned_short_5_6_5 `{ return GL_UNSIGNED_SHORT_5_6_5; `}
1134 new unsigned_short_4_4_4_4 `{ return GL_UNSIGNED_SHORT_4_4_4_4; `}
1135 new unsigned_short_5_5_5_1 `{ return GL_UNSIGNED_SHORT_5_5_5_1; `}
1136 end
1137
1138 # Set of buffers as a bitwise OR mask, used by `GLES::clear`
1139 #
1140 # ~~~
1141 # var buffers = (new GLBuffer).color.depth
1142 # gl.clear buffers
1143 # ~~~
1144 extern class GLBuffer `{ GLbitfield `}
1145 # Get an empty set of buffers
1146 new `{ return 0; `}
1147
1148 # Add the color buffer to the returned buffer set
1149 fun color: GLBuffer `{ return self | GL_COLOR_BUFFER_BIT; `}
1150
1151 # Add the depth buffer to the returned buffer set
1152 fun depth: GLBuffer `{ return self | GL_DEPTH_BUFFER_BIT; `}
1153
1154 # Add the stencil buffer to the returned buffer set
1155 fun stencil: GLBuffer `{ return self | GL_STENCIL_BUFFER_BIT; `}
1156 end