1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # OpenGL graphics rendering library for embedded systems, version 2.0
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`.
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/
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/
34 new_annotation glsl_vertex_shader
35 new_annotation glsl_fragment_shader
40 #include <GLES2/gl2.h>
43 # OpenGL ES program to which we attach shaders
44 extern class GLProgram `{GLuint`}
45 # Create a new program
47 # The newly created instance should be checked using `is_ok
`.
48 new `{ return glCreateProgram(); `}
50 # Is this a valid program?
51 fun is_ok
: Bool `{ return glIsProgram(recv); `}
53 # Attach a `shader
` to this program
54 fun attach_shader(shader: GLShader) `{ glAttachShader(recv, shader); `}
56 # Set the location for the attribute by `name`
57 fun bind_attrib_location
(index
: Int, name
: String) import String.to_cstring
`{
58 GLchar *c_name = String_to_cstring(name);
59 glBindAttribLocation(recv, index, c_name);
62 # Get the location of the attribute by `name`
64 # Returns `-1` if there is no active attribute named `name`.
65 fun attrib_location
(name
: String): Int import String.to_cstring
`{
66 GLchar *c_name = String_to_cstring(name);
67 return glGetAttribLocation(recv, c_name);
71 # Query information on this program
72 fun query
(pname
: Int): Int `{
74 glGetProgramiv(recv, pname, &val);
78 # Try to link this program
80 # Check result using `in_linked` and `info_log`.
81 fun link
`{ glLinkProgram(recv); `}
83 # Is this program linked?
84 fun is_linked: Bool do return query(0x8B82) != 0
86 # Use this program for the following operations
87 fun use `{ glUseProgram(recv); `}
90 fun delete
`{ glDeleteProgram(recv); `}
92 # Has this program been deleted?
93 fun is_deleted: Bool do return query(0x8B80) != 0
95 # Validate whether this program can be executed in the current OpenGL state
97 # Check results using `is_validated
` and `info_log
`.
98 fun validate `{ glValidateProgram(recv); `}
100 # Boolean result of `validate`, must be called after `validate`
101 fun is_validated
: Bool do return query
(0x8B83) != 0
103 # Retrieve the information log of this program
105 # Useful with `link` and `validate`
106 fun info_log
: String import NativeString.to_s
`{
108 glGetProgramiv(recv, GL_INFO_LOG_LENGTH, &size);
109 GLchar *msg = malloc(size);
110 glGetProgramInfoLog(recv, size, NULL, msg);
111 return NativeString_to_s(msg);
115 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
116 extern class GLShader `{GLuint`}
117 # Set the source of the shader
118 fun source=(code: String) import String.to_cstring, String.length `{
119 GLchar *c_code
= String_to_cstring(code
);
120 glShaderSource
(recv
, 1, (const
GLchar * const
*)&c_code
, NULL);
123 # Source of the shader, if available
125 # Returns `null` if the source is not available, usually when the shader
126 # was created from a binary file.
127 fun source: nullable String
129 var size = query(0x8B88)
130 if size == 0 then return null
131 return source_native(size).to_s
134 private fun source_native(size: Int): NativeString `{
135 GLchar *code
= malloc
(size
);
136 glGetShaderSource
(recv
, size
, NULL, code
);
140 # Query information on this shader
141 protected fun query(pname: Int): Int `{
143 glGetShaderiv
(recv
, pname
, &val
);
147 # Try to compile `source
` into a binary GPU program
149 # Check the result using `is_compiled
` and `info_log
`
150 fun compile `{ glCompileShader(recv); `}
152 # Has this shader been compiled?
153 fun is_compiled
: Bool do return query
(0x8B81) != 0
156 fun delete
`{ glDeleteShader(recv); `}
158 # Has this shader been deleted?
159 fun is_deleted: Bool do return query(0x8B80) != 0
161 # Is this a valid shader?
162 fun is_ok: Bool `{ return glIsShader(recv); `}
164 # Retrieve the information log of this shader
166 # Useful with `link` and `validate`
167 fun info_log
: String import NativeString.to_s
`{
169 glGetShaderiv(recv, GL_INFO_LOG_LENGTH, &size);
170 GLchar *msg = malloc(size);
171 glGetShaderInfoLog(recv, size, NULL, msg);
172 return NativeString_to_s(msg);
176 # An OpenGL ES 2.0 fragment shader
177 extern class GLFragmentShader
180 # Create a new fragment shader
182 # The newly created instance should be checked using `is_ok`.
183 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
186 # An OpenGL ES 2.0 vertex shader
187 extern class GLVertexShader
190 # Create a new fragment shader
192 # The newly created instance should be checked using `is_ok
`.
193 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
196 # An array of `Float` associated to a program variable
200 # Number of data per vertex
203 protected var glfloat_array
: GLfloatArray
205 init(index
, count
: Int, array
: Array[Float])
209 self.glfloat_array
= new GLfloatArray(array
)
212 fun attrib_pointer
do attrib_pointer_intern
(index
, count
, glfloat_array
)
213 private fun attrib_pointer_intern
(index
, count
: Int, array
: GLfloatArray) `{
214 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
217 fun enable
do enable_intern
(index
)
218 private fun enable_intern
(index
: Int) `{ glEnableVertexAttribArray(index); `}
220 fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count)
221 private fun draw_arrays_triangles_intern(index, count: Int) `{
222 glDrawArrays
(GL_TRIANGLES, index
, count
);
226 # Low level array of `Float`
227 extern class GLfloatArray `{GLfloat *`}
228 new (array
: Array[Float]) import Array[Float].length
, Array[Float].[] `{
230 int len = Array_of_Float_length(array);
231 GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
232 for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
237 # An OpenGL ES 2.0 error code
238 extern class GLError `{ GLenum `}
239 fun is_ok: Bool do return is_no_error
240 fun is_no_error: Bool `{ return recv == GL_NO_ERROR; `}
241 fun is_invalid_enum
: Bool `{ return recv == GL_INVALID_ENUM; `}
242 fun is_invalid_value: Bool `{ return recv == GL_INVALID_VALUE; `}
243 fun is_invalid_operation
: Bool `{ return recv == GL_INVALID_OPERATION; `}
244 fun is_invalid_framebuffer_operation: Bool `{ return recv == GL_INVALID_FRAMEBUFFER_OPERATION; `}
245 fun is_out_of_memory
: Bool `{ return recv == GL_OUT_OF_MEMORY; `}
249 if is_no_error then return "No error"
250 if is_invalid_enum then return "Invalid enum"
251 if is_invalid_value then return "Invalid value"
252 if is_invalid_operation then return "Invalid operation"
253 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
254 if is_out_of_memory then return "Out of memory"
255 return "Truely unknown error"
259 # Clear the color buffer with `r
`, `g
`, `b
`, `a
`
260 protected fun gl_clear_color(r, g, b, a: Float) `{ glClearColor(r, g, b, a); `}
263 protected fun gl_viewport
(x
, y
, width
, height
: Int) `{ glViewport(x, y, width, height); `}
265 # Direct call to `glClear
`, call with a combinaison of `gl_clear_color_buffer
`,
266 # `gl_stencil_buffer_bit
` and `gl_color_buffer_bit
`.
267 private fun gl_clear(flag: Int) `{ glClear(flag); `}
269 protected fun gl_depth_buffer_bit
: Int do return 0x0100
270 protected fun gl_stencil_buffer_bit
: Int do return 0x0400
271 protected fun gl_color_buffer_bit
: Int do return 0x4000
273 protected fun gl_clear_color_buffer
do gl_clear
(gl_color_buffer_bit
)
274 protected fun gl_clear_depth_buffer
do gl_clear
(gl_depth_buffer_bit
)
275 protected fun gl_clear_stencil_buffer
do gl_clear
(gl_stencil_buffer_bit
)
277 protected fun gl_error
: GLError `{ return glGetError(); `}
278 protected fun assert_no_gl_error
281 if not error.is_ok then
282 print "GL error: {error}"
287 # Query the boolean value at `key
`
288 private fun gl_get_bool(key: Int): Bool `{
290 glGetBooleanv
(key
, &val
);
291 return val
== GL_TRUE;
294 # Query the floating point value at `key
`
295 private fun gl_get_float(key: Int): Float `{
297 glGetFloatv
(key
, &val
);
301 # Query the integer value at `key
`
302 private fun gl_get_int(key: Int): Int `{
304 glGetIntegerv
(key
, &val
);
308 # Does this driver support shader compilation?
310 # Should always return `true` in OpenGL ES 2.0 and 3.0.
311 fun gl_shader_compiler: Bool do return gl_get_bool(0x8DFA)