lib/glesv2: move some services out of `VertexArray` as per the new style
[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 # Enable this vertex attribute array
314 fun enable do glEnableVertexAttribArray(index)
315
316 # Disable this vertex attribute array
317 fun disable do glDisableVertexAttribArray(index)
318 end
319
320 # Enable the generic vertex attribute array at `index`
321 fun glEnableVertexAttribArray(index: Int) `{ glEnableVertexAttribArray(index); `}
322
323 # Disable the generic vertex attribute array at `index`
324 fun glDisableVertexAttribArray(index: Int) `{ glDisableVertexAttribArray(index); `}
325
326 # Render primitives from array data
327 fun glDrawArrays(mode: GLDrawMode, from, count: Int) `{ glDrawArrays(mode, from, count); `}
328
329 # Low level array of `Float`
330 extern class GLfloatArray `{GLfloat *`}
331 new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{
332 int i;
333 int len = Array_of_Float_length(array);
334 GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
335 for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
336 return vertex_array;
337 `}
338 end
339
340 # General type for OpenGL enumerations
341 extern class GLEnum `{ GLenum `}
342
343 redef fun hash `{ return self; `}
344
345 redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
346 end
347
348 # An OpenGL ES 2.0 error code
349 extern class GLError
350 super GLEnum
351
352 # Is there no error?
353 fun is_ok: Bool do return is_no_error
354
355 # Is this not an error?
356 fun is_no_error: Bool `{ return self == GL_NO_ERROR; `}
357
358 fun is_invalid_enum: Bool `{ return self == GL_INVALID_ENUM; `}
359 fun is_invalid_value: Bool `{ return self == GL_INVALID_VALUE; `}
360 fun is_invalid_operation: Bool `{ return self == GL_INVALID_OPERATION; `}
361 fun is_invalid_framebuffer_operation: Bool `{ return self == GL_INVALID_FRAMEBUFFER_OPERATION; `}
362 fun is_out_of_memory: Bool `{ return self == GL_OUT_OF_MEMORY; `}
363
364 redef fun to_s
365 do
366 if is_no_error then return "No error"
367 if is_invalid_enum then return "Invalid enum"
368 if is_invalid_value then return "Invalid value"
369 if is_invalid_operation then return "Invalid operation"
370 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
371 if is_out_of_memory then return "Out of memory"
372 return "Truely unknown error"
373 end
374 end
375
376 fun assert_no_gl_error
377 do
378 var error = gl.error
379 if not error.is_ok then
380 print "GL error: {error}"
381 abort
382 end
383 end
384
385 # Texture minifying function
386 #
387 # Used by: `GLES::tex_parameter_min_filter`
388 extern class GLTextureMinFilter
389 super GLEnum
390
391 new nearest `{ return GL_NEAREST; `}
392 new linear `{ return GL_LINEAR; `}
393 end
394
395 # Texture magnification function
396 #
397 # Used by: `GLES::tex_parameter_mag_filter`
398 extern class GLTextureMagFilter
399 super GLEnum
400
401 new nearest `{ return GL_NEAREST; `}
402 new linear `{ return GL_LINEAR; `}
403 new nearest_mipmap_nearest `{ return GL_NEAREST_MIPMAP_NEAREST; `}
404 new linear_mipmap_nearest `{ return GL_LINEAR_MIPMAP_NEAREST; `}
405 new nearest_mipmap_linear `{ return GL_NEAREST_MIPMAP_LINEAR; `}
406 new linear_mipmap_linear `{ return GL_LINEAR_MIPMAP_LINEAR; `}
407 end
408
409 # Wrap parameter of a texture
410 #
411 # Used by: `tex_parameter_wrap_*`
412 extern class GLTextureWrap
413 super GLEnum
414
415 new clamp_to_edge `{ return GL_CLAMP_TO_EDGE; `}
416 new mirrored_repeat `{ return GL_MIRRORED_REPEAT; `}
417 new repeat `{ return GL_REPEAT; `}
418 end
419
420 # Target texture
421 #
422 # Used by: `tex_parameter_*`
423 extern class GLTextureTarget
424 super GLEnum
425
426 new flat `{ return GL_TEXTURE_2D; `}
427 new cube_map `{ return GL_TEXTURE_CUBE_MAP; `}
428 end
429
430 # A server-side capability
431 class GLCap
432
433 # TODO private init
434
435 # Internal OpenGL integer for this capability
436 private var val: Int
437
438 # Enable this server-side capability
439 fun enable do enable_native(val)
440 private fun enable_native(cap: Int) `{ glEnable(cap); `}
441
442 # Disable this server-side capability
443 fun disable do disable_native(val)
444 private fun disable_native(cap: Int) `{ glDisable(cap); `}
445
446 redef fun hash do return val
447 redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
448 end
449 redef class Sys
450 private var gles = new GLES is lazy
451 end
452
453 # Entry points to OpenGL ES 2.0 services
454 fun gl: GLES do return sys.gles
455
456 # OpenGL ES 2.0 services
457 class GLES
458
459 # Clear the color buffer to `red`, `green`, `blue` and `alpha`
460 fun clear_color(red, green, blue, alpha: Float) `{
461 glClearColor(red, green, blue, alpha);
462 `}
463
464 # Set the viewport
465 fun viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `}
466
467 # Specify mapping of depth values from normalized device coordinates to window coordinates
468 #
469 # Default at `gl_depth_range(0.0, 1.0)`
470 fun depth_range(near, far: Float) `{ glDepthRangef(near, far); `}
471
472 # Define front- and back-facing polygons
473 #
474 # Front-facing polygons are clockwise if `value`, counter-clockwise otherwise.
475 fun front_face=(value: Bool) `{ glFrontFace(value? GL_CW: GL_CCW); `}
476
477 # Specify whether front- or back-facing polygons can be culled, default is `back` only
478 #
479 # One or both of `front` or `back` must be `true`. If you want to deactivate culling
480 # use `(new GLCap.cull_face).disable`.
481 #
482 # Require: `front or back`
483 fun cull_face(front, back: Bool)
484 do
485 assert not (front or back)
486 cull_face_native(front, back)
487 end
488
489 private fun cull_face_native(front, back: Bool) `{
490 glCullFace(front? back? GL_FRONT_AND_BACK: GL_BACK: GL_FRONT);
491 `}
492
493 # Clear the `buffer`
494 fun clear(buffer: GLBuffer) `{ glClear(buffer); `}
495
496 # Last error from OpenGL ES 2.0
497 fun error: GLError `{ return glGetError(); `}
498
499 # Query the boolean value at `key`
500 private fun get_bool(key: Int): Bool `{
501 GLboolean val;
502 glGetBooleanv(key, &val);
503 return val == GL_TRUE;
504 `}
505
506 # Query the floating point value at `key`
507 private fun get_float(key: Int): Float `{
508 GLfloat val;
509 glGetFloatv(key, &val);
510 return val;
511 `}
512
513 # Query the integer value at `key`
514 private fun get_int(key: Int): Int `{
515 GLint val;
516 glGetIntegerv(key, &val);
517 return val;
518 `}
519
520 # Does this driver support shader compilation?
521 #
522 # Should always return `true` in OpenGL ES 2.0 and 3.0.
523 fun shader_compiler: Bool do return get_bool(0x8DFA)
524
525 # Enable or disable writing into the depth buffer
526 fun depth_mask(value: Bool) `{ glDepthMask(value); `}
527
528 # Set the scale and units used to calculate depth values
529 fun polygon_offset(factor, units: Float) `{ glPolygonOffset(factor, units); `}
530
531 # Specify the width of rasterized lines
532 fun line_width(width: Float) `{ glLineWidth(width); `}
533
534 # Set the pixel arithmetic for the blending operations
535 #
536 # Defaultvalues before assignation:
537 # * `src_factor`: `GLBlendFactor::one`
538 # * `dst_factor`: `GLBlendFactor::zero`
539 fun blend_func(src_factor, dst_factor: GLBlendFactor) `{
540 glBlendFunc(src_factor, dst_factor);
541 `}
542
543 # Specify the value used for depth buffer comparisons
544 #
545 # Default value is `GLDepthFunc::less`
546 #
547 # Foreign: glDepthFunc
548 fun depth_func(func: GLDepthFunc) `{ glDepthFunc(func); `}
549
550 # Copy a block of pixels from the framebuffer of `fomat` and `typ` at `data`
551 #
552 # Foreign: glReadPixel
553 fun read_pixels(x, y, width, height: Int, format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
554 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
555 `}
556
557 # Set the texture minifying function
558 #
559 # Foreign: glTexParameter with GL_TEXTURE_MIN_FILTER
560 fun tex_parameter_min_filter(target: GLTextureTarget, value: GLTextureMinFilter) `{
561 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, value);
562 `}
563
564 # Set the texture magnification function
565 #
566 # Foreign: glTexParameter with GL_TEXTURE_MAG_FILTER
567 fun tex_parameter_mag_filter(target: GLTextureTarget, value: GLTextureMagFilter) `{
568 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, value);
569 `}
570
571 # Set the texture wrap parameter for coordinates _s_
572 #
573 # Foreign: glTexParameter with GL_TEXTURE_WRAP_S
574 fun tex_parameter_wrap_s(target: GLTextureTarget, value: GLTextureWrap) `{
575 glTexParameteri(target, GL_TEXTURE_WRAP_S, value);
576 `}
577
578 # Set the texture wrap parameter for coordinates _t_
579 #
580 # Foreign: glTexParameter with GL_TEXTURE_WRAP_T
581 fun tex_parameter_wrap_t(target: GLTextureTarget, value: GLTextureWrap) `{
582 glTexParameteri(target, GL_TEXTURE_WRAP_T, value);
583 `}
584
585 # Render primitives from array data
586 #
587 # Foreign: glDrawArrays
588 fun draw_arrays(mode: GLDrawMode, from, count: Int) `{ glDrawArrays(mode, from, count); `}
589
590 # OpenGL server-side capabilities
591 var capabilities = new GLCapabilities is lazy
592 end
593
594 # Bind `framebuffer` to a framebuffer target
595 #
596 # In OpenGL ES 2.0, `target` must be `gl_FRAMEBUFFER`.
597 fun glBindFramebuffer(target: GLFramebufferTarget, framebuffer: Int) `{
598 glBindFramebuffer(target, framebuffer);
599 `}
600
601 # Target of `glBindFramebuffer`
602 extern class GLFramebufferTarget
603 super GLEnum
604 end
605
606 # Target both reading and writing on the framebuffer with `glBindFramebuffer`
607 fun gl_FRAMEBUFFER: GLFramebufferTarget `{ return GL_FRAMEBUFFER; `}
608
609 # Bind `renderbuffer` to a renderbuffer target
610 #
611 # In OpenGL ES 2.0, `target` must be `gl_RENDERBUFFER`.
612 fun glBindRenderbuffer(target: GLRenderbufferTarget, renderbuffer: Int) `{
613 glBindRenderbuffer(target, renderbuffer);
614 `}
615
616 # Target of `glBindRenderbuffer`
617 extern class GLRenderbufferTarget
618 super GLEnum
619 end
620
621 # Target a renderbuffer with `glBindRenderbuffer`
622 fun gl_RENDERBUFFER: GLRenderbufferTarget `{ return GL_RENDERBUFFER; `}
623
624 # Specify implementation specific hints
625 fun glHint(target: GLHintTarget, mode: GLHintMode) `{
626 glHint(target, mode);
627 `}
628
629 # Completeness status of a framebuffer object
630 fun glCheckFramebufferStatus(target: GLFramebufferTarget): GLFramebufferStatus `{
631 return glCheckFramebufferStatus(target);
632 `}
633
634 # Return value of `glCheckFramebufferStatus`
635 extern class GLFramebufferStatus
636 super GLEnum
637
638 redef fun to_s
639 do
640 if self == gl_FRAMEBUFFER_COMPLETE then return "complete"
641 if self == gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT then return "incomplete attachment"
642 if self == gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS then return "incomplete dimension"
643 if self == gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT then return "incomplete missing attachment"
644 if self == gl_FRAMEBUFFER_UNSUPPORTED then return "unsupported"
645 return "unknown"
646 end
647 end
648
649 # The framebuffer is complete
650 fun gl_FRAMEBUFFER_COMPLETE: GLFramebufferStatus `{
651 return GL_FRAMEBUFFER_COMPLETE;
652 `}
653
654 # Not all framebuffer attachment points are framebuffer attachment complete
655 fun gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: GLFramebufferStatus `{
656 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
657 `}
658
659 # Not all attached images have the same width and height
660 fun gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: GLFramebufferStatus `{
661 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
662 `}
663
664 # No images are attached to the framebuffer
665 fun gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: GLFramebufferStatus `{
666 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
667 `}
668
669 # The combination of internal formats of the attached images violates an implementation-dependent set of restrictions
670 fun gl_FRAMEBUFFER_UNSUPPORTED: GLFramebufferStatus `{
671 return GL_FRAMEBUFFER_UNSUPPORTED;
672 `}
673
674 # Hint target for `glHint`
675 extern class GLHintTarget
676 super GLEnum
677 end
678
679 # Indicates the quality of filtering when generating mipmap images
680 fun gl_GENERATE_MIPMAP_HINT: GLHintTarget `{ return GL_GENERATE_MIPMAP_HINT; `}
681
682 # Hint mode for `glHint`
683 extern class GLHintMode
684 super GLEnum
685 end
686
687 # The most efficient option should be chosen
688 fun gl_FASTEST: GLHintMode `{ return GL_FASTEST; `}
689
690 # The most correct, or highest quality, option should be chosen
691 fun gl_NICEST: GLHintMode `{ return GL_NICEST; `}
692
693 # No preference
694 fun gl_DONT_CARE: GLHintMode `{ return GL_DONT_CARE; `}
695
696 # Entry point to OpenGL server-side capabilities
697 class GLCapabilities
698
699 # GL capability: blend the computed fragment color values
700 #
701 # Foreign: GL_BLEND
702 var blend: GLCap is lazy do return new GLCap(0x0BE2)
703
704 # GL capability: cull polygons based of their winding in window coordinates
705 #
706 # Foreign: GL_CULL_FACE
707 var cull_face: GLCap is lazy do return new GLCap(0x0B44)
708
709 # GL capability: do depth comparisons and update the depth buffer
710 #
711 # Foreign: GL_DEPTH_TEST
712 var depth_test: GLCap is lazy do return new GLCap(0x0B71)
713
714 # GL capability: dither color components or indices before they are written to the color buffer
715 #
716 # Foreign: GL_DITHER
717 var dither: GLCap is lazy do return new GLCap(0x0BE2)
718
719 # GL capability: add an offset to depth values of a polygon fragment before depth test
720 #
721 # Foreign: GL_POLYGON_OFFSET_FILL
722 var polygon_offset_fill: GLCap is lazy do return new GLCap(0x8037)
723
724 # GL capability: compute a temporary coverage value where each bit is determined by the alpha value at the corresponding location
725 #
726 # Foreign: GL_SAMPLE_ALPHA_TO_COVERAGE
727 var sample_alpha_to_coverage: GLCap is lazy do return new GLCap(0x809E)
728
729 # GL capability: AND the fragment coverage with the temporary coverage value
730 #
731 # Foreign: GL_SAMPLE_COVERAGE
732 var sample_coverage: GLCap is lazy do return new GLCap(0x80A0)
733
734 # GL capability: discard fragments that are outside the scissor rectangle
735 #
736 # Foreign: GL_SCISSOR_TEST
737 var scissor_test: GLCap is lazy do return new GLCap(0x0C11)
738
739 # GL capability: do stencil testing and update the stencil buffer
740 #
741 # Foreign: GL_STENCIL_TEST
742 var stencil_test: GLCap is lazy do return new GLCap(0x0B90)
743 end
744
745 # Float related data types of OpenGL ES 2.0 shaders
746 #
747 # Only data types supported by shader attributes, as seen with
748 # `GLProgram::active_attrib_type`.
749 extern class GLFloatDataType
750 super GLEnum
751
752 fun is_float: Bool `{ return self == GL_FLOAT; `}
753 fun is_float_vec2: Bool `{ return self == GL_FLOAT_VEC2; `}
754 fun is_float_vec3: Bool `{ return self == GL_FLOAT_VEC3; `}
755 fun is_float_vec4: Bool `{ return self == GL_FLOAT_VEC4; `}
756 fun is_float_mat2: Bool `{ return self == GL_FLOAT_MAT2; `}
757 fun is_float_mat3: Bool `{ return self == GL_FLOAT_MAT3; `}
758 fun is_float_mat4: Bool `{ return self == GL_FLOAT_MAT4; `}
759
760 # Instances of `GLFloatDataType` can be equal to instances of `GLDataType`
761 redef fun ==(o)
762 do
763 return o != null and o isa GLFloatDataType and o.hash == self.hash
764 end
765 end
766
767 # All data types of OpenGL ES 2.0 shaders
768 #
769 # These types can be used by shader uniforms, as seen with
770 # `GLProgram::active_uniform_type`.
771 extern class GLDataType
772 super GLFloatDataType
773
774 fun is_int: Bool `{ return self == GL_INT; `}
775 fun is_int_vec2: Bool `{ return self == GL_INT_VEC2; `}
776 fun is_int_vec3: Bool `{ return self == GL_INT_VEC3; `}
777 fun is_int_vec4: Bool `{ return self == GL_INT_VEC4; `}
778 fun is_bool: Bool `{ return self == GL_BOOL; `}
779 fun is_bool_vec2: Bool `{ return self == GL_BOOL_VEC2; `}
780 fun is_bool_vec3: Bool `{ return self == GL_BOOL_VEC3; `}
781 fun is_bool_vec4: Bool `{ return self == GL_BOOL_VEC4; `}
782 fun is_sampler_2d: Bool `{ return self == GL_SAMPLER_2D; `}
783 fun is_sampler_cube: Bool `{ return self == GL_SAMPLER_CUBE; `}
784 end
785
786 # Kind of primitives to render with `GLES::draw_arrays`
787 extern class GLDrawMode
788 super GLEnum
789
790 new points `{ return GL_POINTS; `}
791 new line_strip `{ return GL_LINE_STRIP; `}
792 new line_loop `{ return GL_LINE_LOOP; `}
793 new lines `{ return GL_LINES; `}
794 new triangle_strip `{ return GL_TRIANGLE_STRIP; `}
795 new triangle_fan `{ return GL_TRIANGLE_FAN; `}
796 new triangles `{ return GL_TRIANGLES; `}
797 end
798
799 # Pixel arithmetic for blending operations
800 #
801 # Used by `GLES::blend_func`
802 extern class GLBlendFactor
803 super GLEnum
804
805 new zero `{ return GL_ZERO; `}
806 new one `{ return GL_ONE; `}
807 new src_color `{ return GL_SRC_COLOR; `}
808 new one_minus_src_color `{ return GL_ONE_MINUS_SRC_COLOR; `}
809 new dst_color `{ return GL_DST_COLOR; `}
810 new one_minus_dst_color `{ return GL_ONE_MINUS_DST_COLOR; `}
811 new src_alpha `{ return GL_SRC_ALPHA; `}
812 new one_minus_src_alpha `{ return GL_ONE_MINUS_SRC_ALPHA; `}
813 new dst_alpha `{ return GL_DST_ALPHA; `}
814 new one_minus_dst_alpha `{ return GL_ONE_MINUS_DST_ALPHA; `}
815 new constant_color `{ return GL_CONSTANT_COLOR; `}
816 new one_minus_constant_color `{ return GL_ONE_MINUS_CONSTANT_COLOR; `}
817 new constant_alpha `{ return GL_CONSTANT_ALPHA; `}
818 new one_minus_constant_alpha `{ return GL_ONE_MINUS_CONSTANT_ALPHA; `}
819
820 # Used for destination only
821 new src_alpha_saturate `{ return GL_SRC_ALPHA_SATURATE; `}
822 end
823
824 # Condition under which a pixel will be drawn
825 #
826 # Used by `GLES::depth_func`
827 extern class GLDepthFunc
828 super GLEnum
829
830 new never `{ return GL_NEVER; `}
831 new less `{ return GL_LESS; `}
832 new equal `{ return GL_EQUAL; `}
833 new lequal `{ return GL_LEQUAL; `}
834 new greater `{ return GL_GREATER; `}
835 new not_equal `{ return GL_NOTEQUAL; `}
836 new gequal `{ return GL_GEQUAL; `}
837 new always `{ return GL_ALWAYS; `}
838 end
839
840 # Format of pixel data
841 #
842 # Used by `GLES::read_pixels`
843 extern class GLPixelFormat
844 super GLEnum
845
846 new alpha `{ return GL_ALPHA; `}
847 new rgb `{ return GL_RGB; `}
848 new rgba `{ return GL_RGBA; `}
849 end
850
851 # Data type of pixel data
852 #
853 # Used by `GLES::read_pixels`
854 extern class GLPixelType
855 super GLEnum
856
857 new unsigned_byte `{ return GL_UNSIGNED_BYTE; `}
858 new unsigned_short_5_6_5 `{ return GL_UNSIGNED_SHORT_5_6_5; `}
859 new unsigned_short_4_4_4_4 `{ return GL_UNSIGNED_SHORT_4_4_4_4; `}
860 new unsigned_short_5_5_5_1 `{ return GL_UNSIGNED_SHORT_5_5_5_1; `}
861 end
862
863 # Set of buffers as a bitwise OR mask, used by `GLES::clear`
864 #
865 # ~~~
866 # var buffers = (new GLBuffer).color.depth
867 # gl.clear buffers
868 # ~~~
869 extern class GLBuffer `{ GLbitfield `}
870 # Get an empty set of buffers
871 new `{ return 0; `}
872
873 # Add the color buffer to the returned buffer set
874 fun color: GLBuffer `{ return self | GL_COLOR_BUFFER_BIT; `}
875
876 # Add the depth buffer to the returned buffer set
877 fun depth: GLBuffer `{ return self | GL_DEPTH_BUFFER_BIT; `}
878
879 # Add the stencil buffer to the returned buffer set
880 fun stencil: GLBuffer `{ return self | GL_STENCIL_BUFFER_BIT; `}
881 end