doc: Commands tests use `test_frontend`
[nit.git] / lib / gamnit / programs.nit
index 1a995ef..6efc68b 100644 (file)
@@ -93,7 +93,7 @@ class Attribute
 
                var native = native_float_array
                if native == null or array.length > native.length then
-                       if native != null then native.destroy
+                       if native != null then native.finalize
                        native = new GLfloatArray.from(array)
                        self.native_float_array = native
                else
@@ -162,6 +162,14 @@ class UniformBool
        fun uniform(val: Bool) do uniform_1i(location, if val then 1 else 0)
 end
 
+# Shader uniform of GLSL type `int`
+class UniformInt
+       super Uniform
+
+       # Set this uniform value
+       fun uniform(val: Int) do uniform_1i(location, val)
+end
+
 # Shader uniform of GLSL type `vec4`
 class UniformFloat
        super Uniform
@@ -230,6 +238,7 @@ end
 class InactiveUniform
        super InactiveVariable
        super UniformBool
+       super UniformInt
        super UniformFloat
        super UniformSampler2D
        super UniformVec2
@@ -408,6 +417,8 @@ abstract class GamnitProgram
                        var uniform
                        if typ == gl_BOOL then
                                uniform = new UniformBool(gl_program, name, location, size)
+                       else if typ == gl_INT then
+                               uniform = new UniformInt(gl_program, name, location, size)
                        else if typ == gl_SAMPLER_2D then
                                uniform = new UniformSampler2D(gl_program, name, location, size)
                        else if typ == gl_FLOAT then
@@ -428,6 +439,23 @@ abstract class GamnitProgram
                end
        end
 
+       # Diagnose possible problems with the shaders of the program
+       #
+       # Lists to the console inactive uniforms and attributes.
+       # These may not be problematic but they can help to debug the program.
+       fun diagnose
+       do
+               if gl_program == null then compile_and_link
+
+               print "# Diagnose {class_name}"
+               for k,v in uniforms do
+                       if not v.is_active then print "* Uniform {v.name} is inactive"
+               end
+               for k,v in attributes do
+                       if not v.is_active then print "* Attribute {v.name} is inactive"
+               end
+       end
+
        # Attributes of this program organized by name
        #
        # Active attributes are gathered at `compile_and_link`.
@@ -456,7 +484,9 @@ abstract class GamnitProgram
        do
                if deleted then return
 
-               glDeleteProgram gl_program.as(not null)
+               var gl_program = gl_program
+               if gl_program != null then glDeleteProgram gl_program
+
                deleted = true
        end
 end
@@ -538,8 +568,8 @@ redef extern class NativeGLfloatArray
        # Overwrite this matrix with the identity matrix
        fun set_identity
        do
-               for i in 4.times do
-                       for j in 4.times do
+               for i in [0..4[ do
+                       for j in [0..4[ do
                                matrix_set(i, j, if i == j then 1.0 else 0.0)
                        end
                end
@@ -556,8 +586,8 @@ redef class Matrix
        # Copy content of this matrix to a `NativeGLfloatArray`
        fun fill_native(native: NativeGLfloatArray)
        do
-               for i in width.times do
-                       for j in height.times do
+               for i in [0..width[ do
+                       for j in [0..height[ do
                                native.matrix_set(i, j, self[i, j])
                        end
                end