Exemplo n.º 1
0
 def test_freeing(self):
     " Test if programs are freed correctly "
     prog = ShaderProgram.new_program()
     vert, frag = self.get_objects()
     prog.attach(vert, frag)
     
     # Warning, borrowed_obj do not own the underlying shader so it will not be freed automatically
     borrowed_prog = ShaderProgram(glCreateProgram(), owned=False)
     
     pid = prog.pid
     pid2 = borrowed_prog.pid
     del prog
     del borrowed_prog
     gc.collect()
     
     self.assertEqual(glIsProgram(pid), GL_FALSE, 'Program was not freed')
     self.assertEqual(glIsProgram(pid2), GL_TRUE, 'Program was freed')
     self.assertFalse(vert.valid(), 'vert was not freed')
     self.assertFalse(frag.valid(), 'frag was not freed')
     
     # Free the program object
     glDeleteProgram(pid2)        
Exemplo n.º 2
0
    def _uniform_loc_storage_and_type(self, var):
        """Return the uniform location and a container that can
        store its value.

        Parameters
        ----------
        var : string
            Uniform name.

        """
        if var not in self.active_uniforms:
            raise GLSLError("Uniform '%s' is not active.  Make sure the "
                            "variable is used in the source code." % var)

        try:
            var_info = self._uniform_type_info[var]
        except KeyError:
            raise ValueError("Uniform variable '%s' is not defined in "
                             "shader source." % var)

        # If this is an array, how many values are involved?
        count = var_info['array']

        if var_info['kind'] in ['int']:
            data_type = gl.GLint
        else:
            data_type = gl.GLfloat

        assert gl.glIsProgram(self.handle) == True
        assert self.linked

        loc = gl.glGetUniformLocation(self.handle, var)

        if loc == -1:
            raise RuntimeError("Could not query uniform location "
                               "for '%s'." % var)

        storage = data_type * (count * var_info['size'])
        storage_nested = count * (data_type * var_info['size'])

        return loc, storage, storage_nested, data_type
Exemplo n.º 3
0
def check_shader_or_program_status(obj):
    '''checks if the given shader or program object has been compiled or linked successfully.
    Raises an exception if not and deletes the object.
    Returns the object otherwise'''

    if gl.glIsShader(obj):
        getiv = gl.glGetShaderiv
        getlog = gl.glGetShaderInfoLog
        statusflag = gl.GL_COMPILE_STATUS
        delete = gl.glDeleteShader
        ErrorClass = ShaderCompileError
    elif gl.glIsProgram(obj):
        getiv = gl.glGetProgramiv
        getlog = gl.glGetProgramInfoLog
        statusflag = gl.GL_LINK_STATUS
        delete = gl.glDeleteProgram
        ErrorClass = ProgramLinkError
    else:
        raise ValueError('object {} neither shader nor prorgam'.format(obj))

    ok = c_int(0)
    getiv(obj, statusflag, byref(ok))
    if not ok:
        errlen = c_int(0)
        getiv(obj, gl.GL_INFO_LOG_LENGTH, byref(errlen))
        errlen = errlen.value
        if errlen <= 0:
            error = 'unknown error'
        else:
            log = ctypes.create_string_buffer('', errlen)
            getlog(obj, errlen, None, log)
            error = log.value
        delete(obj)
        raise ErrorClass(error)
    else:
        return obj
Exemplo n.º 4
0
 def valid(self):
     """
         Check if the underlying program is valid.
         Return True if it is, False otherwise.
     """
     return glIsProgram(self.pid) == GL_TRUE