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