Exemple #1
0
 def createShader(self, shaderName, shaderType, shaderSrc):
     """
     Create, load, and compile a shader.
     """
     shader = glCreateShaderObjectARB(shaderType)
     glShaderSourceARB(shader, shaderSrc)
     try:
         glCompileShaderARB(shader)    # Checks status, raises error if bad.
     except:
         self.error = True
         types = {GL_VERTEX_SHADER:"vertex", GL_FRAGMENT_SHADER:"fragment"}
         print ("\n%s %s shader program compilation error" % 
                (shaderName, types[shaderType]))
         print glGetInfoLogARB(shader)
         pass
     return shader
Exemple #2
0
 def createShader(self, shaderName, shaderType, shaderSrc):
     """
     Create, load, and compile a shader.
     """
     shader = glCreateShaderObjectARB(shaderType)
     glShaderSourceARB(shader, shaderSrc)
     try:
         glCompileShaderARB(shader)    # Checks status, raises error if bad.
     except:
         self.error = True
         types = {GL_VERTEX_SHADER:"vertex", GL_FRAGMENT_SHADER:"fragment"}
         print ("\n%s %s shader program compilation error" %
                (shaderName, types[shaderType]))
         print glGetInfoLogARB(shader)
         pass
     return shader
Exemple #3
0
    def __init__(self, shaderName, shaderVertSrc, shaderFragSrc):

        # note: on any error, we set self.error, print a message, and return.
        # exceptions will be caught by caller and also set self.error,
        # but result in less specific printed error messages.

        # Configure the max constant RAM used for a "uniform" transforms block.
        if UNIFORM_XFORMS:
            global N_CONST_XFORMS
            oldNCX = N_CONST_XFORMS
            maxComponents = glGetInteger(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB)
            N_CONST_XFORMS = min(
                N_CONST_XFORMS,
                # Each matrix has 16 components. Leave slack for other vars too.
                (maxComponents - _VERTEX_SHADER_GPU_VAR_SLACK) / 16)
            if N_CONST_XFORMS <= 0:
                print (
                    "N_CONST_XFORMS not positive, is %d. %d max components." %
                    (N_CONST_XFORMS, maxComponents))

                # Now, we think this means we should use display lists instead.
                print "error: not enough shader constant memory"
                self.error = True
                return

            elif N_CONST_XFORMS == oldNCX:
                print ("N_CONST_XFORMS unchanged at %d. %d max components." %
                       (N_CONST_XFORMS, maxComponents))
            else:
                print (
                    "N_CONST_XFORMS changed to %d, was %d. %d max components." %
                       (N_CONST_XFORMS, oldNCX, maxComponents))
                pass
            pass
        
        # Version statement has to come first in GLSL source.
        prefix = """// requires GLSL version 1.20
                    #version 120
                    """
        # Insert preprocessor constants before both shader source code strings
        # (using a constant number of lines, to preserve GLSL line numbers)
        if UNIFORM_XFORMS:
            prefix += "#define UNIFORM_XFORMS\n"
            prefix += "#define N_CONST_XFORMS %d\n" % N_CONST_XFORMS
        elif TEXTURE_XFORMS:
            prefix += "#define TEXTURE_XFORMS\n"
            prefix += "\n"
        else:
            prefix += "\n\n"
            pass

        # GLSL on the nVidia GeForce 7000 only supports constant array
        # subscripts, and subscripting by a loop index variable.
        if not debug_pref("GLPane: shaders with only constant subscripts?",
                      Choice_boolean_True, prefs_key = True):
            prefix += "#define FULL_SUBSCRIPTING\n"
        else:
            prefix += "\n" # To keep the shader line numbers unchanged.
            pass

        if debug_pref("GLPane: simulate GLSL syntax error (next session)",
                      Choice_boolean_False, prefs_key = True):
            prefix += "}\n"

        # remove whitespace before and after each prefix line [bruce 090306]
        prefix = '\n'.join( [line.strip() for line in prefix.split('\n')] )
        assert prefix[-1] == '\n'
        
        # Pass the source strings to the shader compiler.
        self.vertShader = self.createShader(shaderName, GL_VERTEX_SHADER,
                                            prefix + shaderVertSrc)
        self.fragShader = self.createShader(shaderName, GL_FRAGMENT_SHADER,
                                            prefix + shaderFragSrc)
        if self.error:          # May be set by createShader.
            return              # Can't do anything good after an error.
        # Link the compiled shaders into a shader program.
        self.progObj = glCreateProgramObjectARB()
        glAttachObjectARB(self.progObj, self.vertShader)
        glAttachObjectARB(self.progObj, self.fragShader)
        try:
            glLinkProgramARB(self.progObj) # Checks status, raises error if bad.
        except:
            self.error = True
            print shaderName, "shader program link error"
            print glGetInfoLogARB(self.progObj)
            return              # Can't do anything good after an error.
        
        # Optional, may be useful for debugging.
        glValidateProgramARB(self.progObj)
        status = glGetObjectParameterivARB(self.progObj, GL_VALIDATE_STATUS)
        if (not status):
            self.error = True
            print "Shader program validation error"
            print glGetInfoLogARB(self.progObj)
            return              # Can't do anything good after an error.

        return
Exemple #4
0
    def __init__(self, shaderName, shaderVertSrc, shaderFragSrc):

        # note: on any error, we set self.error, print a message, and return.
        # exceptions will be caught by caller and also set self.error,
        # but result in less specific printed error messages.

        # Configure the max constant RAM used for a "uniform" transforms block.
        if UNIFORM_XFORMS:
            global N_CONST_XFORMS
            oldNCX = N_CONST_XFORMS
            maxComponents = glGetInteger(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB)
            N_CONST_XFORMS = min(
                N_CONST_XFORMS,
                # Each matrix has 16 components. Leave slack for other vars too.
                (maxComponents - _VERTEX_SHADER_GPU_VAR_SLACK) / 16)
            if N_CONST_XFORMS <= 0:
                print (
                    "N_CONST_XFORMS not positive, is %d. %d max components." %
                    (N_CONST_XFORMS, maxComponents))

                # Now, we think this means we should use display lists instead.
                print "error: not enough shader constant memory"
                self.error = True
                return

            elif N_CONST_XFORMS == oldNCX:
                print ("N_CONST_XFORMS unchanged at %d. %d max components." %
                       (N_CONST_XFORMS, maxComponents))
            else:
                print (
                    "N_CONST_XFORMS changed to %d, was %d. %d max components." %
                       (N_CONST_XFORMS, oldNCX, maxComponents))
                pass
            pass

        # Version statement has to come first in GLSL source.
        prefix = """// requires GLSL version 1.20
                    #version 120
                    """
        # Insert preprocessor constants before both shader source code strings
        # (using a constant number of lines, to preserve GLSL line numbers)
        if UNIFORM_XFORMS:
            prefix += "#define UNIFORM_XFORMS\n"
            prefix += "#define N_CONST_XFORMS %d\n" % N_CONST_XFORMS
        elif TEXTURE_XFORMS:
            prefix += "#define TEXTURE_XFORMS\n"
            prefix += "\n"
        else:
            prefix += "\n\n"
            pass

        # GLSL on the nVidia GeForce 7000 only supports constant array
        # subscripts, and subscripting by a loop index variable.
        if not debug_pref("GLPane: shaders with only constant subscripts?",
                      Choice_boolean_True, prefs_key = True):
            prefix += "#define FULL_SUBSCRIPTING\n"
        else:
            prefix += "\n" # To keep the shader line numbers unchanged.
            pass

        if debug_pref("GLPane: simulate GLSL syntax error (next session)",
                      Choice_boolean_False, prefs_key = True):
            prefix += "}\n"

        # remove whitespace before and after each prefix line [bruce 090306]
        prefix = '\n'.join( [line.strip() for line in prefix.split('\n')] )
        assert prefix[-1] == '\n'

        # Pass the source strings to the shader compiler.
        self.vertShader = self.createShader(shaderName, GL_VERTEX_SHADER,
                                            prefix + shaderVertSrc)
        self.fragShader = self.createShader(shaderName, GL_FRAGMENT_SHADER,
                                            prefix + shaderFragSrc)
        if self.error:          # May be set by createShader.
            return              # Can't do anything good after an error.
        # Link the compiled shaders into a shader program.
        self.progObj = glCreateProgramObjectARB()
        glAttachObjectARB(self.progObj, self.vertShader)
        glAttachObjectARB(self.progObj, self.fragShader)
        try:
            glLinkProgramARB(self.progObj) # Checks status, raises error if bad.
        except:
            self.error = True
            print shaderName, "shader program link error"
            print glGetInfoLogARB(self.progObj)
            return              # Can't do anything good after an error.

        # Optional, may be useful for debugging.
        glValidateProgramARB(self.progObj)
        status = glGetObjectParameterivARB(self.progObj, GL_VALIDATE_STATUS)
        if (not status):
            self.error = True
            print "Shader program validation error"
            print glGetInfoLogARB(self.progObj)
            return              # Can't do anything good after an error.

        return