# This file is part of NIT ( http://www.nitlanguage.org ). # # Copyright 2014 Alexis Laferrière # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # OpenGL graphics rendering library for embedded systems, version 2.0 # # This is a low-level wrapper, it can be useful for developers already familiar # with the C API of OpenGL. Most developers will prefer to use higher level # wrappers such as `mnit` and `gammit`. # # Defines the annotations `glsl_vertex_shader` and `glsl_fragment_shader` # applicable on string literals to check shader code using `glslangValidator`. # The tool must be in PATH. It can be downloaded from # https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/ # # Most services of this module are a direct wrapper of the underlying # C library. If a method or class is not documented in Nit, refer to # the official documentation by the Khronos Group at: # http://www.khronos.org/opengles/sdk/docs/man/ module glesv2 is pkgconfig new_annotation glsl_vertex_shader new_annotation glsl_fragment_shader end in "C Header" `{ #include `} # OpenGL ES program to which we attach shaders extern class GLProgram `{GLuint`} # Create a new program # # The newly created instance should be checked using `is_ok`. new `{ return glCreateProgram(); `} # Is this a valid program? fun is_ok: Bool `{ return glIsProgram(recv); `} # Attach a `shader` to this program fun attach_shader(shader: GLShader) `{ glAttachShader(recv, shader); `} # Set the location for the attribute by `name` fun bind_attrib_location(index: Int, name: String) import String.to_cstring `{ GLchar *c_name = String_to_cstring(name); glBindAttribLocation(recv, index, c_name); `} # Get the location of the attribute by `name` # # Returns `-1` if there is no active attribute named `name`. fun attrib_location(name: String): Int import String.to_cstring `{ GLchar *c_name = String_to_cstring(name); return glGetAttribLocation(recv, c_name); `} # Get the location of the uniform by `name` # # Returns `-1` if there is no active uniform named `name`. fun uniform_location(name: String): Int import String.to_cstring `{ GLchar *c_name = String_to_cstring(name); return glGetUniformLocation(recv, c_name); `} # Query information on this program fun query(pname: Int): Int `{ int val; glGetProgramiv(recv, pname, &val); return val; `} # Try to link this program # # Check result using `in_linked` and `info_log`. fun link `{ glLinkProgram(recv); `} # Is this program linked? fun is_linked: Bool do return query(0x8B82) != 0 # Use this program for the following operations fun use `{ glUseProgram(recv); `} # Delete this program fun delete `{ glDeleteProgram(recv); `} # Has this program been deleted? fun is_deleted: Bool do return query(0x8B80) != 0 # Validate whether this program can be executed in the current OpenGL state # # Check results using `is_validated` and `info_log`. fun validate `{ glValidateProgram(recv); `} # Boolean result of `validate`, must be called after `validate` fun is_validated: Bool do return query(0x8B83) != 0 # Retrieve the information log of this program # # Useful with `link` and `validate` fun info_log: String import NativeString.to_s `{ int size; glGetProgramiv(recv, GL_INFO_LOG_LENGTH, &size); GLchar *msg = malloc(size); glGetProgramInfoLog(recv, size, NULL, msg); return NativeString_to_s(msg); `} # Number of active uniform in this program # # This should be the number of uniforms declared in all shader, except # unused uniforms which may have been optimized out. fun n_active_uniforms: Int do return query(0x8B86) # Length of the longest uniform name in this program, including `\n` fun active_uniform_max_length: Int do return query(0x8B87) # Number of active attributes in this program # # This should be the number of uniforms declared in all shader, except # unused uniforms which may have been optimized out. fun n_active_attributes: Int do return query(0x8B89) # Length of the longest uniform name in this program, including `\n` fun active_attribute_max_length: Int do return query(0x8B8A) # Number of shaders attached to this program fun n_attached_shaders: Int do return query(0x8B85) # Name of the active attribute at `index` fun active_attrib_name(index: Int): String do var max_size = active_attribute_max_length return active_attrib_name_native(index, max_size).to_s end private fun active_attrib_name_native(index, max_size: Int): NativeString `{ char *name = malloc(max_size); glGetActiveAttrib(recv, index, max_size, NULL, NULL, NULL, name); return name; `} # Size of the active attribute at `index` fun active_attrib_size(index: Int): Int `{ int size; glGetActiveAttrib(recv, index, 0, NULL, NULL, &size, NULL); return size; `} # Type of the active attribute at `index` # # May only be float related data types (single float, vectors and matrix). fun active_attrib_type(index: Int): GLFloatDataType `{ GLenum type; glGetActiveAttrib(recv, index, 0, NULL, &type, NULL, NULL); return type; `} # Name of the active uniform at `index` fun active_uniform_name(index: Int): String do var max_size = active_attribute_max_length return active_uniform_name_native(index, max_size).to_s end private fun active_uniform_name_native(index, max_size: Int): NativeString `{ char *name = malloc(max_size); glGetActiveUniform(recv, index, max_size, NULL, NULL, NULL, name); return name; `} # Size of the active uniform at `index` fun active_uniform_size(index: Int): Int `{ int size; glGetActiveUniform(recv, index, 0, NULL, NULL, &size, NULL); return size; `} # Type of the active uniform at `index` # # May be any data type supported by OpenGL ES 2.0 shaders. fun active_uniform_type(index: Int): GLDataType `{ GLenum type; glGetActiveUniform(recv, index, 0, NULL, &type, NULL, NULL); return type; `} end # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader` extern class GLShader `{GLuint`} # Set the source of the shader fun source=(code: NativeString) `{ glShaderSource(recv, 1, (GLchar const **)&code, NULL); `} # Source of the shader, if available # # Returns `null` if the source is not available, usually when the shader # was created from a binary file. fun source: nullable String do var size = query(0x8B88) if size == 0 then return null return source_native(size).to_s end private fun source_native(size: Int): NativeString `{ GLchar *code = malloc(size); glGetShaderSource(recv, size, NULL, code); return code; `} # Query information on this shader protected fun query(pname: Int): Int `{ int val; glGetShaderiv(recv, pname, &val); return val; `} # Try to compile `source` into a binary GPU program # # Check the result using `is_compiled` and `info_log` fun compile `{ glCompileShader(recv); `} # Has this shader been compiled? fun is_compiled: Bool do return query(0x8B81) != 0 # Delete this shader fun delete `{ glDeleteShader(recv); `} # Has this shader been deleted? fun is_deleted: Bool do return query(0x8B80) != 0 # Is this a valid shader? fun is_ok: Bool `{ return glIsShader(recv); `} # Retrieve the information log of this shader # # Useful with `link` and `validate` fun info_log: String import NativeString.to_s `{ int size; glGetShaderiv(recv, GL_INFO_LOG_LENGTH, &size); GLchar *msg = malloc(size); glGetShaderInfoLog(recv, size, NULL, msg); return NativeString_to_s(msg); `} end # An OpenGL ES 2.0 fragment shader extern class GLFragmentShader super GLShader # Create a new fragment shader # # The newly created instance should be checked using `is_ok`. new `{ return glCreateShader(GL_FRAGMENT_SHADER); `} end # An OpenGL ES 2.0 vertex shader extern class GLVertexShader super GLShader # Create a new fragment shader # # The newly created instance should be checked using `is_ok`. new `{ return glCreateShader(GL_VERTEX_SHADER); `} end # An array of `Float` associated to a program variable class VertexArray var index: Int # Number of data per vertex var count: Int protected var glfloat_array: GLfloatArray init(index, count: Int, array: Array[Float]) do self.index = index self.count = count self.glfloat_array = new GLfloatArray(array) end fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array) private fun attrib_pointer_intern(index, count: Int, array: GLfloatArray) `{ glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array); `} fun enable do enable_intern(index) private fun enable_intern(index: Int) `{ glEnableVertexAttribArray(index); `} fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count) private fun draw_arrays_triangles_intern(index, count: Int) `{ glDrawArrays(GL_TRIANGLES, index, count); `} end # Low level array of `Float` extern class GLfloatArray `{GLfloat *`} new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{ int i; int len = Array_of_Float_length(array); GLfloat *vertex_array = malloc(sizeof(GLfloat)*len); for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i); return vertex_array; `} end # General type for OpenGL enumerations extern class GLEnum `{ GLenum `} redef fun hash `{ return recv; `} redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash end # An OpenGL ES 2.0 error code extern class GLError super GLEnum fun is_ok: Bool do return is_no_error fun is_no_error: Bool `{ return recv == GL_NO_ERROR; `} fun is_invalid_enum: Bool `{ return recv == GL_INVALID_ENUM; `} fun is_invalid_value: Bool `{ return recv == GL_INVALID_VALUE; `} fun is_invalid_operation: Bool `{ return recv == GL_INVALID_OPERATION; `} fun is_invalid_framebuffer_operation: Bool `{ return recv == GL_INVALID_FRAMEBUFFER_OPERATION; `} fun is_out_of_memory: Bool `{ return recv == GL_OUT_OF_MEMORY; `} redef fun to_s do if is_no_error then return "No error" if is_invalid_enum then return "Invalid enum" if is_invalid_value then return "Invalid value" if is_invalid_operation then return "Invalid operation" if is_invalid_framebuffer_operation then return "invalid framebuffer operation" if is_out_of_memory then return "Out of memory" return "Truely unknown error" end end protected fun assert_no_gl_error do var error = gl.error if not error.is_ok then print "GL error: {error}" abort end end redef class Sys private var gles = new GLES is lazy end # Entry points to OpenGL ES 2.0 services fun gl: GLES do return sys.gles # OpenGL ES 2.0 services class GLES # Clear the color buffer to `red`, `green`, `blue` and `alpha` fun clear_color(red, green, blue, alpha: Float) `{ glClearColor(red, green, blue, alpha); `} # Set the viewport fun viewport(x, y, width, height: Int) `{ glViewport(x, y, width, height); `} # Clear the `buffer` fun clear(buffer: GLBuffer) `{ glClear(buffer); `} # Last error from OpenGL ES 2.0 fun error: GLError `{ return glGetError(); `} # Query the boolean value at `key` private fun get_bool(key: Int): Bool `{ GLboolean val; glGetBooleanv(key, &val); return val == GL_TRUE; `} # Query the floating point value at `key` private fun get_float(key: Int): Float `{ GLfloat val; glGetFloatv(key, &val); return val; `} # Query the integer value at `key` private fun get_int(key: Int): Int `{ GLint val; glGetIntegerv(key, &val); return val; `} # Does this driver support shader compilation? # # Should always return `true` in OpenGL ES 2.0 and 3.0. fun shader_compiler: Bool do return get_bool(0x8DFA) end # Float related data types of OpenGL ES 2.0 shaders # # Only data types supported by shader attributes, as seen with # `GLProgram::active_attrib_type`. extern class GLFloatDataType super GLEnum fun is_float: Bool `{ return recv == GL_FLOAT; `} fun is_float_vec2: Bool `{ return recv == GL_FLOAT_VEC2; `} fun is_float_vec3: Bool `{ return recv == GL_FLOAT_VEC3; `} fun is_float_vec4: Bool `{ return recv == GL_FLOAT_VEC4; `} fun is_float_mat2: Bool `{ return recv == GL_FLOAT_MAT2; `} fun is_float_mat3: Bool `{ return recv == GL_FLOAT_MAT3; `} fun is_float_mat4: Bool `{ return recv == GL_FLOAT_MAT4; `} # Instances of `GLFloatDataType` can be equal to instances of `GLDataType` redef fun ==(o) do return o != null and o isa GLFloatDataType and o.hash == self.hash end end # All data types of OpenGL ES 2.0 shaders # # These types can be used by shader uniforms, as seen with # `GLProgram::active_uniform_type`. extern class GLDataType super GLFloatDataType fun is_int: Bool `{ return recv == GL_INT; `} fun is_int_vec2: Bool `{ return recv == GL_INT_VEC2; `} fun is_int_vec3: Bool `{ return recv == GL_INT_VEC3; `} fun is_int_vec4: Bool `{ return recv == GL_INT_VEC4; `} fun is_bool: Bool `{ return recv == GL_BOOL; `} fun is_bool_vec2: Bool `{ return recv == GL_BOOL_VEC2; `} fun is_bool_vec3: Bool `{ return recv == GL_BOOL_VEC3; `} fun is_bool_vec4: Bool `{ return recv == GL_BOOL_VEC4; `} fun is_sampler_2d: Bool `{ return recv == GL_SAMPLER_2D; `} fun is_sampler_cube: Bool `{ return recv == GL_SAMPLER_CUBE; `} end # Set of buffers as a bitwise OR mask, used by `GLES::clear` # # ~~~ # var buffers = (new GLBuffer).color.depth # gl.clear buffers # ~~~ extern class GLBuffer `{ GLbitfield `} # Get an empty set of buffers new `{ return 0; `} # Add the color buffer to the returned buffer set fun color: GLBuffer `{ return recv | GL_COLOR_BUFFER_BIT; `} # Add the depth buffer to the returned buffer set fun depth: GLBuffer `{ return recv | GL_DEPTH_BUFFER_BIT; `} # Add the stencil buffer to the returned buffer set fun stencil: GLBuffer `{ return recv | GL_STENCIL_BUFFER_BIT; `} end