Example #1
0
 def _declare_getParams(self, name, type, cls, arraysize, default):
     """Helper method for declare().
     
     Turns the arguments into a params "tuple". See declare() for a description 
     of the arguments.
     The return value is either a params object as returned by slparams()
     or an old-style params tuple.
     """
     # Create a "dummy shader" which will be passed to slparams to parse
     # the declaration in name
     shd = "surface spam(%s) {}" % name
     try:
         # Force a syntax error when name contains no declaration
         if " " not in name:
             raise slparams.SyntaxError()
         slinfo = slparams.slparams(StringIO.StringIO(shd))
         shdtype, shdname, params = slinfo[0]
     except slparams.SyntaxError, e:
         # Check if name is only a single name or if there was an attempt
         # to specify the entire declaration
         invalid = " []():;'\"'"
         for inv in invalid:
             if inv in name:
                 raise ValueError('Invalid declaration: "%s"' % name)
         # It's probably really just the name, so use the remaining
         # arguments to create a parameter tuple...
         if cls is None:
             cls = "uniform"
         if type is None:
             raise ValueError('No type for parameter "%s" specified' % name)
         if type not in ["float", "string", "color", "point", "vector", "normal", "matrix"]:
             raise ValueError('Invalid type for parameter "%s": %s' % (name, type))
         params = [("", cls, type, arraysize, name, "", str(default))]
Example #2
0
 def _declare_getParams(self, name, type, cls, arraysize, default):
     """Helper method for declare().
     
     Turns the arguments into a params "tuple". See declare() for a description 
     of the arguments.
     The return value is either a params object as returned by slparams()
     or an old-style params tuple.
     """
     # Create a "dummy shader" which will be passed to slparams to parse
     # the declaration in name
     shd = "surface spam(%s) {}"%name
     try:
         # Force a syntax error when name contains no declaration
         if " " not in name:
             raise slparams.SyntaxError()
         slinfo = slparams.slparams(StringIO.StringIO(shd))
         shdtype, shdname, params = slinfo[0]
     except slparams.SyntaxError, e:
         # Check if name is only a single name or if there was an attempt
         # to specify the entire declaration
         invalid = " []():;'\"'"
         for inv in invalid:
             if inv in name:
                 raise ValueError('Invalid declaration: "%s"'%name)
         # It's probably really just the name, so use the remaining
         # arguments to create a parameter tuple...
         if cls is None:
             cls = "uniform"
         if type is None:
             raise ValueError('No type for parameter "%s" specified'%name)
         if type not in ["float", "string", "color", "point", "vector",
                         "normal", "matrix"]:
             raise ValueError('Invalid type for parameter "%s": %s'%(name, type))
         params = [("", cls, type, arraysize, name, "", str(default))]
Example #3
0
    def __init__(
        self,
        shader=None,
        transform=mat4(1),
        cpp=None,
        cpperrstream=sys.stderr,
        includedirs=None,
        defines=None,
        params=None,
        **keyargs
    ):
        """Constructor.

        The first argument is the shader name or file name. cpp determines
        the preprocessor that should be used when extracting parameters.
        cpperrstream is used to output errors from the preprocessor. 
        includedirs is a list of strings that contain directories where to
        look for include files. defines is a list of tuples (name, value)
        that specify the predefined symbols to use (see the function 
        slparams.slparams() for details).
        params can be used to declare parameters if the shader source
        is not available. The value must be a dictionary that contains
        token/value pairs. The token may contain an inline declaration.
        Any additional keyword argument is also considered to be a shader
        parameter. However, this parameter cannot have an inline declaration,
        so it is recommended to declare the parameter afterwards using
        the declare() method.
        
        \param name (\c str) Shader file name or shader name
        """

        # Shader file or None
        self.filename = None
        # Shader name (without path and extension)
        self.shadername = None
        # Shader type (surface, displacement, ...)
        self.shadertype = None
        # Shader transformation
        self.transform = transform
        # Shader parameters as dictionary.
        # Key:Parameter name   Value:Declaration
        self.shaderparams = {}

        # String parameters are not stored as slots because they can either
        # contain a string or a RenderPass object
        # str_params: Key:Parameter name /  Value:Value
        self.str_params = {}

        # Params for which there is no declaration
        # Key: Param name   Value:Value
        self.undeclared = keyargs

        if params != None:
            for param in params:
                f = param.split()
                # Was the parameter an empty string? then ignore
                if len(f) == 0:
                    continue
                # Add the param to the 'undeclared' dict
                # (even if there is a declaration this will ensure that the
                # value is taken as default value during the declaration)
                paramname = f[-1]
                if paramname not in self.undeclared:
                    self.undeclared[paramname] = params[param]
                if len(f) > 1:
                    self.declare(param)

        # If there is no extension then name is just the shader name
        # and not the shader file
        if shader != None:
            if os.path.splitext(shader)[1] == "":
                self.shadername = shader
            else:
                self.filename = shader

        # Read the shader parameters from the shader source file...
        if self.filename != None:
            slinfo = slparams.slparams(
                shader, cpp=cpp, cpperrstream=cpperrstream, includedirs=includedirs, defines=defines
            )
            if len(slinfo) == 0:
                raise ValueError("no shader found in %s" % shader)
            if len(slinfo) > 1:
                print("WARNING: There is more than one shader in %s" % shader)

            # Declare the variables...
            self.shadertype, self.shadername, params = slinfo[0]
            for p in params:
                cls = p[1]
                type = p[2]
                arraysize = p[3]
                name = p[4]
                default = p[6]
                self.declare(name, type, cls, arraysize, default)
Example #4
0
    def __init__(self,
                 shader = None,
                 transform = mat4(1),
                 cpp = None,
                 cpperrstream = sys.stderr,
                 includedirs = None,
                 defines = None,
                 params = None,
                 **keyargs):
        """Constructor.

        The first argument is the shader name or file name. cpp determines
        the preprocessor that should be used when extracting parameters.
        cpperrstream is used to output errors from the preprocessor. 
        includedirs is a list of strings that contain directories where to
        look for include files. defines is a list of tuples (name, value)
        that specify the predefined symbols to use (see the function 
        slparams.slparams() for details).
        params can be used to declare parameters if the shader source
        is not available. The value must be a dictionary that contains
        token/value pairs. The token may contain an inline declaration.
        Any additional keyword argument is also considered to be a shader
        parameter. However, this parameter cannot have an inline declaration,
        so it is recommended to declare the parameter afterwards using
        the declare() method.
        
        \param name (\c str) Shader file name or shader name
        """
      
        # Shader file or None
        self.filename = None
        # Shader name (without path and extension)
        self.shadername = None
        # Shader type (surface, displacement, ...)
        self.shadertype = None
        # Shader transformation
        self.transform = transform
        # Shader parameters as dictionary.
        # Key:Parameter name   Value:Declaration
        self.shaderparams = {}

        # String parameters are not stored as slots because they can either
        # contain a string or a RenderPass object
        # str_params: Key:Parameter name /  Value:Value
        self.str_params = {}

        # Params for which there is no declaration
        # Key: Param name   Value:Value
        self.undeclared = keyargs

        if params!=None:
            for param in params:
                f = param.split()
                # Was the parameter an empty string? then ignore
                if len(f)==0:
                    continue
                # Add the param to the 'undeclared' dict
                # (even if there is a declaration this will ensure that the
                # value is taken as default value during the declaration)
                paramname = f[-1]
                if paramname not in self.undeclared:
                    self.undeclared[paramname] = params[param]
                if len(f)>1:
                    self.declare(param)
            
        
        # If there is no extension then name is just the shader name
        # and not the shader file
        if shader!=None:
            if os.path.splitext(shader)[1]=="":
                self.shadername = shader
            else:
                self.filename = shader

        # Read the shader parameters from the shader source file...
        if self.filename!=None:
            slinfo = slparams.slparams(shader, cpp=cpp, cpperrstream=cpperrstream, includedirs=includedirs, defines=defines)
            if len(slinfo)==0:
                raise ValueError("no shader found in %s"%shader)
            if len(slinfo)>1:
                print("WARNING: There is more than one shader in %s"%shader)

            # Declare the variables...
            self.shadertype, self.shadername, params = slinfo[0]
            for p in params:
                cls = p[1]
                type = p[2]
                arraysize = p[3]
                name = p[4]
                default = p[6]
                self.declare(name, type, cls, arraysize, default)