e842317bd6d639f01ab294ed5d102b5206a39f3f
[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
41 in "C Header" `{
42 #include <GLES2/gl2.h>
43 `}
44
45 # OpenGL ES program to which we attach shaders
46 extern class GLProgram `{GLuint`}
47 # Create a new program
48 #
49 # The newly created instance should be checked using `is_ok`.
50 new `{ return glCreateProgram(); `}
51
52 # Is this a valid program?
53 fun is_ok: Bool `{ return glIsProgram(self); `}
54
55 # Attach a `shader` to this program
56 fun attach_shader(shader: GLShader) `{ glAttachShader(self, shader); `}
57
58 # Set the location for the attribute by `name`
59 fun bind_attrib_location(index: Int, name: String) import String.to_cstring `{
60 GLchar *c_name = String_to_cstring(name);
61 glBindAttribLocation(self, index, c_name);
62 `}
63
64 # Get the location of the attribute by `name`
65 #
66 # Returns `-1` if there is no active attribute named `name`.
67 fun attrib_location(name: String): Int import String.to_cstring `{
68 GLchar *c_name = String_to_cstring(name);
69 return glGetAttribLocation(self, c_name);
70 `}
71
72 # Get the location of the uniform by `name`
73 #
74 # Returns `-1` if there is no active uniform named `name`.
75 fun uniform_location(name: String): Int import String.to_cstring `{
76 GLchar *c_name = String_to_cstring(name);
77 return glGetUniformLocation(self, c_name);
78 `}
79
80 # Query information on this program
81 fun query(pname: Int): Int `{
82 int val;
83 glGetProgramiv(self, pname, &val);
84 return val;
85 `}
86
87 # Try to link this program
88 #
89 # Check result using `in_linked` and `info_log`.
90 fun link `{ glLinkProgram(self); `}
91
92 # Is this program linked?
93 fun is_linked: Bool do return query(0x8B82) != 0
94
95 # Use this program for the following operations
96 fun use `{ glUseProgram(self); `}
97
98 # Delete this program
99 fun delete `{ glDeleteProgram(self); `}
100
101 # Has this program been deleted?
102 fun is_deleted: Bool do return query(0x8B80) != 0
103
104 # Validate whether this program can be executed in the current OpenGL state
105 #
106 # Check results using `is_validated` and `info_log`.
107 fun validate `{ glValidateProgram(self); `}
108
109 # Boolean result of `validate`, must be called after `validate`
110 fun is_validated: Bool do return query(0x8B83) != 0
111
112 # Retrieve the information log of this program
113 #
114 # Useful with `link` and `validate`
115 fun info_log: String import NativeString.to_s `{
116 int size;
117 glGetProgramiv(self, GL_INFO_LOG_LENGTH, &size);
118 GLchar *msg = malloc(size);
119 glGetProgramInfoLog(self, size, NULL, msg);
120 return NativeString_to_s(msg);
121 `}
122
123 # Number of active uniform in this program
124 #
125 # This should be the number of uniforms declared in all shader, except
126 # unused uniforms which may have been optimized out.
127 fun n_active_uniforms: Int do return query(0x8B86)
128
129 # Length of the longest uniform name in this program, including `\n`
130 fun active_uniform_max_length: Int do return query(0x8B87)
131
132 # Number of active attributes in this program
133 #
134 # This should be the number of uniforms declared in all shader, except
135 # unused uniforms which may have been optimized out.
136 fun n_active_attributes: Int do return query(0x8B89)
137
138 # Length of the longest uniform name in this program, including `\n`
139 fun active_attribute_max_length: Int do return query(0x8B8A)
140
141 # Number of shaders attached to this program
142 fun n_attached_shaders: Int do return query(0x8B85)
143
144 # Name of the active attribute at `index`
145 fun active_attrib_name(index: Int): String
146 do
147 var max_size = active_attribute_max_length
148 return active_attrib_name_native(index, max_size).to_s
149 end
150 private fun active_attrib_name_native(index, max_size: Int): NativeString `{
151 // We get more values than we need, for compatibility. At least the
152 // NVidia driver tries to fill them even if NULL.
153
154 char *name = malloc(max_size);
155 int size;
156 GLenum type;
157 glGetActiveAttrib(self, index, max_size, NULL, &size, &type, name);
158 return name;
159 `}
160
161 # Size of the active attribute at `index`
162 fun active_attrib_size(index: Int): Int `{
163 int size;
164 GLenum type;
165 glGetActiveAttrib(self, index, 0, NULL, &size, &type, NULL);
166 return size;
167 `}
168
169 # Type of the active attribute at `index`
170 #
171 # May only be float related data types (single float, vectors and matrix).
172 fun active_attrib_type(index: Int): GLFloatDataType `{
173 int size;
174 GLenum type;
175 glGetActiveAttrib(self, index, 0, NULL, &size, &type, NULL);
176 return type;
177 `}
178
179 # Name of the active uniform at `index`
180 fun active_uniform_name(index: Int): String
181 do
182 var max_size = active_attribute_max_length
183 return active_uniform_name_native(index, max_size).to_s
184 end
185 private fun active_uniform_name_native(index, max_size: Int): NativeString `{
186 char *name = malloc(max_size);
187 int size;
188 GLenum type;
189 glGetActiveUniform(self, index, max_size, NULL, &size, &type, name);
190 return name;
191 `}
192
193 # Size of the active uniform at `index`
194 fun active_uniform_size(index: Int): Int `{
195 int size;
196 GLenum type;
197 glGetActiveUniform(self, index, 0, NULL, &size, &type, NULL);
198 return size;
199 `}
200
201 # Type of the active uniform at `index`
202 #
203 # May be any data type supported by OpenGL ES 2.0 shaders.
204 fun active_uniform_type(index: Int): GLDataType `{
205 int size;
206 GLenum type = 0;
207 glGetActiveUniform(self, index, 0, NULL, &size, &type, NULL);
208 return type;
209 `}
210 end
211
212 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
213 extern class GLShader `{GLuint`}
214 # Set the source of the shader
215 fun source=(code: NativeString) `{
216 glShaderSource(self, 1, (GLchar const **)&code, NULL);
217 `}
218
219 # Source of the shader, if available
220 #
221 # Returns `null` if the source is not available, usually when the shader
222 # was created from a binary file.
223 fun source: nullable String
224 do
225 var size = query(0x8B88)
226 if size == 0 then return null
227 return source_native(size).to_s
228 end
229
230 private fun source_native(size: Int): NativeString `{
231 GLchar *code = malloc(size);
232 glGetShaderSource(self, size, NULL, code);
233 return code;
234 `}
235
236 # Query information on this shader
237 protected fun query(pname: Int): Int `{
238 int val;
239 glGetShaderiv(self, pname, &val);
240 return val;
241 `}
242
243 # Try to compile `source` into a binary GPU program
244 #
245 # Check the result using `is_compiled` and `info_log`
246 fun compile `{ glCompileShader(self); `}
247
248 # Has this shader been compiled?
249 fun is_compiled: Bool do return query(0x8B81) != 0
250
251 # Delete this shader
252 fun delete `{ glDeleteShader(self); `}
253
254 # Has this shader been deleted?
255 fun is_deleted: Bool do return query(0x8B80) != 0
256
257 # Is this a valid shader?
258 fun is_ok: Bool `{ return glIsShader(self); `}
259
260 # Retrieve the information log of this shader
261 #
262 # Useful with `link` and `validate`
263 fun info_log: String import NativeString.to_s `{
264 int size;
265 glGetShaderiv(self, GL_INFO_LOG_LENGTH, &size);
266 GLchar *msg = malloc(size);
267 glGetShaderInfoLog(self, size, NULL, msg);
268 return NativeString_to_s(msg);
269 `}
270 end
271
272 # An OpenGL ES 2.0 fragment shader
273 extern class GLFragmentShader
274 super GLShader
275
276 # Create a new fragment shader
277 #
278 # The newly created instance should be checked using `is_ok`.
279 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
280 end
281
282 # An OpenGL ES 2.0 vertex shader
283 extern class GLVertexShader
284 super GLShader
285
286 # Create a new fragment shader
287 #
288 # The newly created instance should be checked using `is_ok`.
289 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
290 end
291
292 # An array of `Float` associated to a program variable
293 class VertexArray
294 var index: Int
295
296 # Number of data per vertex
297 var count: Int
298
299 protected var glfloat_array: GLfloatArray
300
301 init(index, count: Int, array: Array[Float])
302 do
303 self.index = index
304 self.count = count
305 self.glfloat_array = new GLfloatArray(array)
306 end
307
308 fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array)
309 private fun attrib_pointer_intern(index, count: Int, array: GLfloatArray) `{
310 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
311 `}
312
313 fun enable do enable_intern(index)
314 private fun enable_intern(index: Int) `{ glEnableVertexAttribArray(index); `}
315
316 fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count)
317 private fun draw_arrays_triangles_intern(index, count: Int) `{
318 glDrawArrays(GL_TRIANGLES, index, count);
319 `}
320 end
321
322 # Low level array of `Float`
323 extern class GLfloatArray `{GLfloat *`}
324 new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{
325 int i;
326 int len = Array_of_Float_length(array);
327 GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
328 for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
329 return vertex_array;
330 `}
331 end
332
333 # General type for OpenGL enumerations
334 extern class GLEnum `{ GLenum `}
335
336 redef fun hash `{ return self; `}
337
338 redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
339 end
340
341 # An OpenGL ES 2.0 error code
342 extern class GLError
343 super GLEnum
344
345 # Is there no error?
346 fun is_ok: Bool do return is_no_error
347
348 # Is this not an error?
349 fun is_no_error: Bool `{ return self == GL_NO_ERROR; `}
350
351 fun is_invalid_enum: Bool `{ return self == GL_INVALID_ENUM; `}
352 fun is_invalid_value: Bool `{ return self == GL_INVALID_VALUE; `}
353 fun is_invalid_operation: Bool `{ return self == GL_INVALID_OPERATION; `}
354 fun is_invalid_framebuffer_operation: Bool `{ return self == GL_INVALID_FRAMEBUFFER_OPERATION; `}
355 fun is_out_of_memory: Bool `{ return self == GL_OUT_OF_MEMORY; `}
356
357 redef fun to_s
358 do
359 if is_no_error then return "No error"
360 if is_invalid_enum then return "Invalid enum"
361 if is_invalid_value then return "Invalid value"
362 if is_invalid_operation then return "Invalid operation"
363 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
364 if is_out_of_memory then return "Out of memory"
365 return "Truely unknown error"
366 end
367 end
368
369 fun assert_no_gl_error
370 do
371 var error = gl.error
372 if not error.is_ok then
373 print "GL error: {error}"
374 abort
375 end
376 end
377
378 # Texture minifying and magnifying function
379 extern class GLTextureFilter
380 super GLEnum
381 end
382
383 fun gl_NEAREST: GLTextureFilter `{ return GL_NEAREST; `}
384 fun gl_LINEAR: GLTextureFilter `{ return GL_LINEAR; `}
385 fun gl_NEAREST_MIPMAP_NEAREST: GLTextureFilter `{ return GL_NEAREST_MIPMAP_NEAREST; `}
386 fun gl_LINEAR_MIPMAP_NEAREST: GLTextureFilter `{ return GL_LINEAR_MIPMAP_NEAREST; `}
387 fun gl_NEAREST_MIPMAP_NINEAR: GLTextureFilter `{ return GL_NEAREST_MIPMAP_LINEAR; `}
388 fun gl_LINEAR_MIPMAP_LINEAR: GLTextureFilter `{ return GL_LINEAR_MIPMAP_LINEAR; `}
389
390 # Wrap parameter of a texture
391 #
392 # Used by: `tex_parameter_wrap_*`
393 extern class GLTextureWrap
394 super GLEnum
395
396 new clamp_to_edge `{ return GL_CLAMP_TO_EDGE; `}
397 new mirrored_repeat `{ return GL_MIRRORED_REPEAT; `}
398 new repeat `{ return GL_REPEAT; `}
399 end
400
401 # Target texture
402 #
403 # Used by: `tex_parameter_*`
404 extern class GLTextureTarget
405 super GLEnum
406
407 new flat `{ return GL_TEXTURE_2D; `}
408 new cube_map `{ return GL_TEXTURE_CUBE_MAP; `}
409 end
410
411 # A server-side capability
412 class GLCap
413
414 # TODO private init
415
416 # Internal OpenGL integer for this capability
417 private var val: Int
418
419 # Enable this server-side capability
420 fun enable do enable_native(val)
421 private fun enable_native(cap: Int) `{ glEnable(cap); `}
422
423 # Disable this server-side capability
424 fun disable do disable_native(val)
425 private fun disable_native(cap: Int) `{ glDisable(cap); `}
426
427 redef fun hash do return val
428 redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
429 end
430 redef class Sys
431 private var gles = new GLES is lazy
432 end
433
434 # Entry points to OpenGL ES 2.0 services
435 fun gl: GLES do return sys.gles
436
437 # OpenGL ES 2.0 services
438 class GLES
439
440 # Clear the color buffer to `red`, `green`, `blue` and `alpha`
441 fun clear_color(red, green, blue, alpha: Float) `{
442 glClearColor(red, green, blue, alpha);
443 `}
444
445 # Set the viewport
446 fun viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `}
447
448 # Specify mapping of depth values from normalized device coordinates to window coordinates
449 #
450 # Default at `gl_depth_range(0.0, 1.0)`
451 fun depth_range(near, far: Float) `{ glDepthRangef(near, far); `}
452
453 # Define front- and back-facing polygons
454 #
455 # Front-facing polygons are clockwise if `value`, counter-clockwise otherwise.
456 fun front_face=(value: Bool) `{ glFrontFace(value? GL_CW: GL_CCW); `}
457
458 # Specify whether front- or back-facing polygons can be culled, default is `back` only
459 #
460 # One or both of `front` or `back` must be `true`. If you want to deactivate culling
461 # use `(new GLCap.cull_face).disable`.
462 #
463 # Require: `front or back`
464 fun cull_face(front, back: Bool)
465 do
466 assert not (front or back)
467 cull_face_native(front, back)
468 end
469
470 private fun cull_face_native(front, back: Bool) `{
471 glCullFace(front? back? GL_FRONT_AND_BACK: GL_BACK: GL_FRONT);
472 `}
473
474 # Clear the `buffer`
475 fun clear(buffer: GLBuffer) `{ glClear(buffer); `}
476
477 # Last error from OpenGL ES 2.0
478 fun error: GLError `{ return glGetError(); `}
479
480 # Query the boolean value at `key`
481 private fun get_bool(key: Int): Bool `{
482 GLboolean val;
483 glGetBooleanv(key, &val);
484 return val == GL_TRUE;
485 `}
486
487 # Query the floating point value at `key`
488 private fun get_float(key: Int): Float `{
489 GLfloat val;
490 glGetFloatv(key, &val);
491 return val;
492 `}
493
494 # Query the integer value at `key`
495 private fun get_int(key: Int): Int `{
496 GLint val;
497 glGetIntegerv(key, &val);
498 return val;
499 `}
500
501 # Does this driver support shader compilation?
502 #
503 # Should always return `true` in OpenGL ES 2.0 and 3.0.
504 fun shader_compiler: Bool do return get_bool(0x8DFA)
505
506 # Enable or disable writing into the depth buffer
507 fun depth_mask(value: Bool) `{ glDepthMask(value); `}
508
509 # Set the scale and units used to calculate depth values
510 fun polygon_offset(factor, units: Float) `{ glPolygonOffset(factor, units); `}
511
512 # Specify the width of rasterized lines
513 fun line_width(width: Float) `{ glLineWidth(width); `}
514
515 # Set the pixel arithmetic for the blending operations
516 #
517 # Defaultvalues before assignation:
518 # * `src_factor`: `GLBlendFactor::one`
519 # * `dst_factor`: `GLBlendFactor::zero`
520 fun blend_func(src_factor, dst_factor: GLBlendFactor) `{
521 glBlendFunc(src_factor, dst_factor);
522 `}
523
524 # Specify the value used for depth buffer comparisons
525 #
526 # Default value is `GLDepthFunc::less`
527 #
528 # Foreign: glDepthFunc
529 fun depth_func(func: GLDepthFunc) `{ glDepthFunc(func); `}
530
531 # Copy a block of pixels from the framebuffer of `fomat` and `typ` at `data`
532 #
533 # Foreign: glReadPixel
534 fun read_pixels(x, y, width, height: Int, format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
535 glReadPixels(x, y, width, height, format, typ, data);
536 `}
537
538 # Set the texture minifying function
539 #
540 # Foreign: glTexParameter with GL_TEXTURE_MIN_FILTER
541 fun tex_parameter_min_filter(target: GLTextureTarget, value: GLTextureFilter) `{
542 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, value);
543 `}
544
545 # Set the texture magnification function
546 #
547 # Foreign: glTexParameter with GL_TEXTURE_MAG_FILTER
548 fun tex_parameter_mag_filter(target: GLTextureTarget, value: GLTextureFilter) `{
549 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, value);
550 `}
551
552 # Set the texture wrap parameter for coordinates _s_
553 #
554 # Foreign: glTexParameter with GL_TEXTURE_WRAP_S
555 fun tex_parameter_wrap_s(target: GLTextureTarget, value: GLTextureWrap) `{
556 glTexParameteri(target, GL_TEXTURE_WRAP_S, value);
557 `}
558
559 # Set the texture wrap parameter for coordinates _t_
560 #
561 # Foreign: glTexParameter with GL_TEXTURE_WRAP_T
562 fun tex_parameter_wrap_t(target: GLTextureTarget, value: GLTextureWrap) `{
563 glTexParameteri(target, GL_TEXTURE_WRAP_T, value);
564 `}
565
566 # Render primitives from array data
567 #
568 # Foreign: glDrawArrays
569 fun draw_arrays(mode: GLDrawMode, from, count: Int) `{ glDrawArrays(mode, from, count); `}
570
571 # OpenGL server-side capabilities
572 var capabilities = new GLCapabilities is lazy
573 end
574
575 # Bind `framebuffer` to a framebuffer target
576 #
577 # In OpenGL ES 2.0, `target` must be `gl_FRAMEBUFFER`.
578 fun glBindFramebuffer(target: GLFramebufferTarget, framebuffer: Int) `{
579 glBindFramebuffer(target, framebuffer);
580 `}
581
582 # Target of `glBindFramebuffer`
583 extern class GLFramebufferTarget
584 super GLEnum
585 end
586
587 # Target both reading and writing on the framebuffer with `glBindFramebuffer`
588 fun gl_FRAMEBUFFER: GLFramebufferTarget `{ return GL_FRAMEBUFFER; `}
589
590 # Bind `renderbuffer` to a renderbuffer target
591 #
592 # In OpenGL ES 2.0, `target` must be `gl_RENDERBUFFER`.
593 fun glBindRenderbuffer(target: GLRenderbufferTarget, renderbuffer: Int) `{
594 glBindRenderbuffer(target, renderbuffer);
595 `}
596
597 # Target of `glBindRenderbuffer`
598 extern class GLRenderbufferTarget
599 super GLEnum
600 end
601
602 # Target a renderbuffer with `glBindRenderbuffer`
603 fun gl_RENDERBUFFER: GLRenderbufferTarget `{ return GL_RENDERBUFFER; `}
604
605 # Specify implementation specific hints
606 fun glHint(target: GLHintTarget, mode: GLHintMode) `{
607 glHint(target, mode);
608 `}
609
610 # Completeness status of a framebuffer object
611 fun glCheckFramebufferStatus(target: GLFramebufferTarget): GLFramebufferStatus `{
612 return glCheckFramebufferStatus(target);
613 `}
614
615 # Return value of `glCheckFramebufferStatus`
616 extern class GLFramebufferStatus
617 super GLEnum
618
619 redef fun to_s
620 do
621 if self == gl_FRAMEBUFFER_COMPLETE then return "complete"
622 if self == gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT then return "incomplete attachment"
623 if self == gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS then return "incomplete dimension"
624 if self == gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT then return "incomplete missing attachment"
625 if self == gl_FRAMEBUFFER_UNSUPPORTED then return "unsupported"
626 return "unknown"
627 end
628 end
629
630 # The framebuffer is complete
631 fun gl_FRAMEBUFFER_COMPLETE: GLFramebufferStatus `{
632 return GL_FRAMEBUFFER_COMPLETE;
633 `}
634
635 # Not all framebuffer attachment points are framebuffer attachment complete
636 fun gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: GLFramebufferStatus `{
637 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
638 `}
639
640 # Not all attached images have the same width and height
641 fun gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: GLFramebufferStatus `{
642 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
643 `}
644
645 # No images are attached to the framebuffer
646 fun gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: GLFramebufferStatus `{
647 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
648 `}
649
650 # The combination of internal formats of the attached images violates an implementation-dependent set of restrictions
651 fun gl_FRAMEBUFFER_UNSUPPORTED: GLFramebufferStatus `{
652 return GL_FRAMEBUFFER_UNSUPPORTED;
653 `}
654
655 # Hint target for `glHint`
656 extern class GLHintTarget
657 super GLEnum
658 end
659
660 # Indicates the quality of filtering when generating mipmap images
661 fun gl_GENERATE_MIPMAP_HINT: GLHintTarget `{ return GL_GENERATE_MIPMAP_HINT; `}
662
663 # Hint mode for `glHint`
664 extern class GLHintMode
665 super GLEnum
666 end
667
668 # The most efficient option should be chosen
669 fun gl_FASTEST: GLHintMode `{ return GL_FASTEST; `}
670
671 # The most correct, or highest quality, option should be chosen
672 fun gl_NICEST: GLHintMode `{ return GL_NICEST; `}
673
674 # No preference
675 fun gl_DONT_CARE: GLHintMode `{ return GL_DONT_CARE; `}
676
677 # Entry point to OpenGL server-side capabilities
678 class GLCapabilities
679
680 # GL capability: blend the computed fragment color values
681 #
682 # Foreign: GL_BLEND
683 var blend: GLCap is lazy do return new GLCap(0x0BE2)
684
685 # GL capability: cull polygons based of their winding in window coordinates
686 #
687 # Foreign: GL_CULL_FACE
688 var cull_face: GLCap is lazy do return new GLCap(0x0B44)
689
690 # GL capability: do depth comparisons and update the depth buffer
691 #
692 # Foreign: GL_DEPTH_TEST
693 var depth_test: GLCap is lazy do return new GLCap(0x0B71)
694
695 # GL capability: dither color components or indices before they are written to the color buffer
696 #
697 # Foreign: GL_DITHER
698 var dither: GLCap is lazy do return new GLCap(0x0BE2)
699
700 # GL capability: add an offset to depth values of a polygon fragment before depth test
701 #
702 # Foreign: GL_POLYGON_OFFSET_FILL
703 var polygon_offset_fill: GLCap is lazy do return new GLCap(0x8037)
704
705 # GL capability: compute a temporary coverage value where each bit is determined by the alpha value at the corresponding location
706 #
707 # Foreign: GL_SAMPLE_ALPHA_TO_COVERAGE
708 var sample_alpha_to_coverage: GLCap is lazy do return new GLCap(0x809E)
709
710 # GL capability: AND the fragment coverage with the temporary coverage value
711 #
712 # Foreign: GL_SAMPLE_COVERAGE
713 var sample_coverage: GLCap is lazy do return new GLCap(0x80A0)
714
715 # GL capability: discard fragments that are outside the scissor rectangle
716 #
717 # Foreign: GL_SCISSOR_TEST
718 var scissor_test: GLCap is lazy do return new GLCap(0x0C11)
719
720 # GL capability: do stencil testing and update the stencil buffer
721 #
722 # Foreign: GL_STENCIL_TEST
723 var stencil_test: GLCap is lazy do return new GLCap(0x0B90)
724 end
725
726 # Float related data types of OpenGL ES 2.0 shaders
727 #
728 # Only data types supported by shader attributes, as seen with
729 # `GLProgram::active_attrib_type`.
730 extern class GLFloatDataType
731 super GLEnum
732
733 fun is_float: Bool `{ return self == GL_FLOAT; `}
734 fun is_float_vec2: Bool `{ return self == GL_FLOAT_VEC2; `}
735 fun is_float_vec3: Bool `{ return self == GL_FLOAT_VEC3; `}
736 fun is_float_vec4: Bool `{ return self == GL_FLOAT_VEC4; `}
737 fun is_float_mat2: Bool `{ return self == GL_FLOAT_MAT2; `}
738 fun is_float_mat3: Bool `{ return self == GL_FLOAT_MAT3; `}
739 fun is_float_mat4: Bool `{ return self == GL_FLOAT_MAT4; `}
740
741 # Instances of `GLFloatDataType` can be equal to instances of `GLDataType`
742 redef fun ==(o)
743 do
744 return o != null and o isa GLFloatDataType and o.hash == self.hash
745 end
746 end
747
748 # All data types of OpenGL ES 2.0 shaders
749 #
750 # These types can be used by shader uniforms, as seen with
751 # `GLProgram::active_uniform_type`.
752 extern class GLDataType
753 super GLFloatDataType
754
755 fun is_int: Bool `{ return self == GL_INT; `}
756 fun is_int_vec2: Bool `{ return self == GL_INT_VEC2; `}
757 fun is_int_vec3: Bool `{ return self == GL_INT_VEC3; `}
758 fun is_int_vec4: Bool `{ return self == GL_INT_VEC4; `}
759 fun is_bool: Bool `{ return self == GL_BOOL; `}
760 fun is_bool_vec2: Bool `{ return self == GL_BOOL_VEC2; `}
761 fun is_bool_vec3: Bool `{ return self == GL_BOOL_VEC3; `}
762 fun is_bool_vec4: Bool `{ return self == GL_BOOL_VEC4; `}
763 fun is_sampler_2d: Bool `{ return self == GL_SAMPLER_2D; `}
764 fun is_sampler_cube: Bool `{ return self == GL_SAMPLER_CUBE; `}
765 end
766
767 # Kind of primitives to render with `GLES::draw_arrays`
768 extern class GLDrawMode
769 super GLEnum
770
771 new points `{ return GL_POINTS; `}
772 new line_strip `{ return GL_LINE_STRIP; `}
773 new line_loop `{ return GL_LINE_LOOP; `}
774 new lines `{ return GL_LINES; `}
775 new triangle_strip `{ return GL_TRIANGLE_STRIP; `}
776 new triangle_fan `{ return GL_TRIANGLE_FAN; `}
777 new triangles `{ return GL_TRIANGLES; `}
778 end
779
780 # Pixel arithmetic for blending operations
781 #
782 # Used by `GLES::blend_func`
783 extern class GLBlendFactor
784 super GLEnum
785
786 new zero `{ return GL_ZERO; `}
787 new one `{ return GL_ONE; `}
788 new src_color `{ return GL_SRC_COLOR; `}
789 new one_minus_src_color `{ return GL_ONE_MINUS_SRC_COLOR; `}
790 new dst_color `{ return GL_DST_COLOR; `}
791 new one_minus_dst_color `{ return GL_ONE_MINUS_DST_COLOR; `}
792 new src_alpha `{ return GL_SRC_ALPHA; `}
793 new one_minus_src_alpha `{ return GL_ONE_MINUS_SRC_ALPHA; `}
794 new dst_alpha `{ return GL_DST_ALPHA; `}
795 new one_minus_dst_alpha `{ return GL_ONE_MINUS_DST_ALPHA; `}
796 new constant_color `{ return GL_CONSTANT_COLOR; `}
797 new one_minus_constant_color `{ return GL_ONE_MINUS_CONSTANT_COLOR; `}
798 new constant_alpha `{ return GL_CONSTANT_ALPHA; `}
799 new one_minus_constant_alpha `{ return GL_ONE_MINUS_CONSTANT_ALPHA; `}
800
801 # Used for destination only
802 new src_alpha_saturate `{ return GL_SRC_ALPHA_SATURATE; `}
803 end
804
805 # Condition under which a pixel will be drawn
806 #
807 # Used by `GLES::depth_func`
808 extern class GLDepthFunc
809 super GLEnum
810
811 new never `{ return GL_NEVER; `}
812 new less `{ return GL_LESS; `}
813 new equal `{ return GL_EQUAL; `}
814 new lequal `{ return GL_LEQUAL; `}
815 new greater `{ return GL_GREATER; `}
816 new not_equal `{ return GL_NOTEQUAL; `}
817 new gequal `{ return GL_GEQUAL; `}
818 new always `{ return GL_ALWAYS; `}
819 end
820
821 # Format of pixel data
822 #
823 # Used by `GLES::read_pixels`
824 extern class GLPixelFormat
825 super GLEnum
826
827 new alpha `{ return GL_ALPHA; `}
828 new rgb `{ return GL_RGB; `}
829 new rgba `{ return GL_RGBA; `}
830 end
831
832 # Data type of pixel data
833 #
834 # Used by `GLES::read_pixels`
835 extern class GLPixelType
836 super GLEnum
837
838 new unsigned_byte `{ return GL_UNSIGNED_BYTE; `}
839 new unsigned_short_5_6_5 `{ return GL_UNSIGNED_SHORT_5_6_5; `}
840 new unsigned_short_4_4_4_4 `{ return GL_UNSIGNED_SHORT_4_4_4_4; `}
841 new unsigned_short_5_5_5_1 `{ return GL_UNSIGNED_SHORT_5_5_5_1; `}
842 end
843
844 # Set of buffers as a bitwise OR mask, used by `GLES::clear`
845 #
846 # ~~~
847 # var buffers = (new GLBuffer).color.depth
848 # gl.clear buffers
849 # ~~~
850 extern class GLBuffer `{ GLbitfield `}
851 # Get an empty set of buffers
852 new `{ return 0; `}
853
854 # Add the color buffer to the returned buffer set
855 fun color: GLBuffer `{ return self | GL_COLOR_BUFFER_BIT; `}
856
857 # Add the depth buffer to the returned buffer set
858 fun depth: GLBuffer `{ return self | GL_DEPTH_BUFFER_BIT; `}
859
860 # Add the stencil buffer to the returned buffer set
861 fun stencil: GLBuffer `{ return self | GL_STENCIL_BUFFER_BIT; `}
862 end