lib/glesv2: fix and simplify the implementation of `GLShader::source`
[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 # Defines the annotations `glsl_vertex_shader` and `glsl_fragment_shader`
20 # applicable on string literals to check shader code using `glslangValidator`.
21 # The tool must be in PATH. It can be downloaded from
22 # https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
23 #
24 # Most services of this module are a direct wrapper of the underlying
25 # C library. If a method or class is not documented in Nit, refer to
26 # the official documentation by the Khronos Group at:
27 # http://www.khronos.org/opengles/sdk/docs/man/
28 module glesv2 is
29 pkgconfig
30 new_annotation glsl_vertex_shader
31 new_annotation glsl_fragment_shader
32 end
33
34 in "C Header" `{
35 #include <GLES2/gl2.h>
36 `}
37
38 # Opengl ES program to which we attach shaders
39 extern class GLProgram `{GLuint`}
40 new `{ return glCreateProgram(); `}
41
42 fun is_ok: Bool `{ return glIsProgram(recv); `}
43
44 fun attach_shader(shader: GLShader) `{ glAttachShader(recv, shader); `}
45
46 fun bind_attrib_location(index: Int, name: String) import String.to_cstring `{
47 GLchar *c_name = String_to_cstring(name);
48 glBindAttribLocation(recv, index, c_name);
49 `}
50
51 fun attrib_location(name: String): Int import String.to_cstring `{
52 GLchar *c_name = String_to_cstring(name);
53 return glGetAttribLocation(recv, c_name);
54 `}
55
56 fun query(pname: Int): Int `{
57 int val;
58 glGetProgramiv(recv, pname, &val);
59 return val;
60 `}
61
62 fun link `{ glLinkProgram(recv); `}
63 fun is_linked: Bool do return query(0x8B82) != 0
64
65 fun use `{ glUseProgram(recv); `}
66
67 fun delete `{ glDeleteProgram(recv); `}
68 fun is_deleted: Bool do return query(0x8B80) != 0
69
70 fun validate `{ glValidateProgram(recv); `}
71 fun is_validated: Bool do return query(0x8B83) != 0
72
73 fun info_log: String import NativeString.to_s `{
74 int size;
75 glGetProgramiv(recv, GL_INFO_LOG_LENGTH, &size);
76 GLchar *msg = malloc(size);
77 glGetProgramInfoLog(recv, size, NULL, msg);
78 return NativeString_to_s(msg);
79 `}
80 end
81
82 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
83 extern class GLShader `{GLuint`}
84 fun source=(code: String) import String.to_cstring, String.length `{
85 GLchar *c_code = String_to_cstring(code);
86 glShaderSource(recv, 1, (const GLchar * const*)&c_code, NULL);
87 `}
88 fun source: nullable String
89 do
90 var size = query(0x8B88)
91 if size == 0 then return null
92 return source_native(size).to_s
93 end
94
95 private fun source_native(size: Int): NativeString `{
96 GLchar *code = malloc(size);
97 glGetShaderSource(recv, size, NULL, code);
98 return code;
99 `}
100
101 protected fun query(pname: Int): Int `{
102 int val;
103 glGetShaderiv(recv, pname, &val);
104 return val;
105 `}
106
107 fun compile `{ glCompileShader(recv); `}
108 fun is_compiled: Bool do return query(0x8B81) != 0
109
110 fun delete `{ glDeleteShader(recv); `}
111 fun is_deleted: Bool do return query(0x8B80) != 0
112
113 fun is_ok: Bool `{ return glIsShader(recv); `}
114
115 fun info_log: String import NativeString.to_s `{
116 int size;
117 glGetShaderiv(recv, GL_INFO_LOG_LENGTH, &size);
118 GLchar *msg = malloc(size);
119 glGetShaderInfoLog(recv, size, NULL, msg);
120 return NativeString_to_s(msg);
121 `}
122
123 end
124
125 extern class GLFragmentShader
126 super GLShader
127
128 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
129 end
130
131 extern class GLVertexShader
132 super GLShader
133
134 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
135 end
136
137 # An array of `Float` associated to a program variable
138 class VertexArray
139 var index: Int
140 var count: Int
141 protected var glfloat_array: GLfloatArray
142
143 init(index, count: Int, array: Array[Float])
144 do
145 self.index = index
146 self.count = count
147 self.glfloat_array = new GLfloatArray(array)
148 end
149
150 fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array)
151 private fun attrib_pointer_intern(index, count: Int, array: GLfloatArray) `{
152 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
153 `}
154
155 fun enable do enable_intern(index)
156 private fun enable_intern(index: Int) `{ glEnableVertexAttribArray(index); `}
157
158 fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count)
159 private fun draw_arrays_triangles_intern(index, count: Int) `{
160 glDrawArrays(GL_TRIANGLES, index, count);
161 `}
162 end
163
164 # Low level array of `Float`
165 extern class GLfloatArray `{GLfloat *`}
166 new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{
167 int i;
168 int len = Array_of_Float_length(array);
169 GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
170 for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
171 return vertex_array;
172 `}
173 end
174
175 extern class GLError `{ GLenum `}
176 fun is_ok: Bool do return is_no_error
177 fun is_no_error: Bool `{ return recv == GL_NO_ERROR; `}
178 fun is_invalid_enum: Bool `{ return recv == GL_INVALID_ENUM; `}
179 fun is_invalid_value: Bool `{ return recv == GL_INVALID_VALUE; `}
180 fun is_invalid_operation: Bool `{ return recv == GL_INVALID_OPERATION; `}
181 fun is_invalid_framebuffer_operation: Bool `{ return recv == GL_INVALID_FRAMEBUFFER_OPERATION; `}
182 fun is_out_of_memory: Bool `{ return recv == GL_OUT_OF_MEMORY; `}
183
184 redef fun to_s
185 do
186 if is_no_error then return "No error"
187 if is_invalid_enum then return "Invalid enum"
188 if is_invalid_value then return "Invalid value"
189 if is_invalid_operation then return "Invalid operation"
190 if is_invalid_framebuffer_operation then return "invalid framebuffer operation"
191 if is_out_of_memory then return "Out of memory"
192 return "Truely unknown error"
193 end
194 end
195
196 protected fun gl_clear_color(r, g, b, a: Float) `{ glClearColor(r, g, b, a); `}
197 protected fun gl_viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `}
198
199 # Direct call to `glClear`, call with a combinaison of `gl_clear_color_buffer`,
200 # `gl_stencil_buffer_bit` and `gl_color_buffer_bit`.
201 private fun gl_clear(flag: Int) `{ glClear(flag); `}
202
203 protected fun gl_depth_buffer_bit: Int do return 0x0100
204 protected fun gl_stencil_buffer_bit: Int do return 0x0400
205 protected fun gl_color_buffer_bit: Int do return 0x4000
206
207 protected fun gl_clear_color_buffer do gl_clear(gl_color_buffer_bit)
208 protected fun gl_clear_depth_buffer do gl_clear(gl_depth_buffer_bit)
209 protected fun gl_clear_stencil_buffer do gl_clear(gl_stencil_buffer_bit)
210
211 protected fun gl_error: GLError `{ return glGetError(); `}
212 protected fun assert_no_gl_error
213 do
214 var error = gl_error
215 if not error.is_ok then
216 print "GL error: {error}"
217 abort
218 end
219 end
220
221 private fun gl_get_bool(key: Int): Bool `{
222 GLboolean val;
223 glGetBooleanv(key, &val);
224 return val == GL_TRUE;
225 `}
226 private fun gl_get_float(key: Int): Float `{
227 GLfloat val;
228 glGetFloatv(key, &val);
229 return val;
230 `}
231 private fun gl_get_int(key: Int): Int `{
232 GLint val;
233 glGetIntegerv(key, &val);
234 return val;
235 `}
236
237 fun gl_shader_compiler: Bool do return gl_get_bool(0x8DFA)