def set_code(self, code, source=None): """ Set the code for this shader. Parameters ---------- code : str The GLSL source code, or a filename that contains the code. source : str A specifier where the code came from. If not given, "<string>" is used, or the filename where the code is loaded from. Optional. """ if not is_string(code): raise TypeError('Code must be a string (%s)' % type(code)) # Set code and source if os.path.isfile(code): with open(code, 'rb') as file: self._code = file.read().decode('utf-8') self._source = os.path.basename(code) else: self._code = code self._source = '<string>' # Set given source? if source is not None: if not is_string(source): raise TypeError('Source must be a string (%s)' % type(source)) self._source = source # Set flags self._need_update = True
def __getitem__(self, key): """ Create a view on this buffer. """ if not is_string(key): raise ValueError("Can only get access to a named field") # Get dtype, e.g. ('x', '<f4', 2) so it has the vsize! dtype = self._dtype[key] # not .base! offset = self._dtype.fields[key][1] return VertexBufferView(dtype, base=self, offset=offset)
def _get_error(self, errors, indentation=0): """ Get linking error in a somewhat nicer format. """ # Init if not is_string(errors): errors = errors.decode('utf-8', 'replace') # Parse lines results = [line for line in errors.splitlines() if line] # Add indentation and return results = [' '*indentation + r for r in results] return '\n'.join(results)
def _get_error(self, errors, indentation=0): """ Get linking error in a somewhat nicer format. """ # Init if not is_string(errors): errors = errors.decode('utf-8', 'replace') # Parse lines results = [line for line in errors.splitlines() if line] # Add indentation and return results = [' ' * indentation + r for r in results] return '\n'.join(results)
def code(self, code): """ Set the code of the shader. """ if not is_string(code): raise TypeError('code must be a string (%s)' % type(code)) # Set code and source if os.path.exists(code): with open(code) as file: self._code = file.read() self._source = os.path.basename(code) else: self._code = code self._source = '<string>' # Set flags self._need_update = True
def __init__(self, data, target): """ Initialize the buffer """ Buffer.__init__(self, target) # Default offset is 0, only really used for View self._offset = 0 # Allow initialization with a string or a tuple that described dtype if is_string(data): data = np.dtype(data) elif isinstance(data, tuple): data = np.dtype([data]) # Initialize if isinstance(data, np.ndarray): # Fix dtype, vsize, stride. Initialize count array_info = self._parse_array(data) self._dtype, self._vsize, self._stride, self._count = array_info # Set data now self.set_data(data) elif isinstance(data, np.dtype): # Fix dtype, vsize, stride. Initialize count self._dtype, self._vsize, self._stride = self._parse_dtype(data) self._count = 0 else: raise ValueError("DataBuffer needs array of dtype to initialize.") # todo: assume vsize =1 if e.g. np.float32 is passed? # # Check vsize (some dtypes do not specify vsize) # if not self.vsize: # raise ValueError('Could not determine vsize for %s.' % # self.__class__.__name__) # Check data type if self.dtype.fields: for name in self.dtype.names: dtype = self.dtype[name].base if dtype.name not in self.DTYPE2GTYPE: raise TypeError("Data type not allowed for %s: %s" % (self.__class__.__name__, dtype.name) ) else: if self.dtype.name not in self.DTYPE2GTYPE: raise TypeError("Data type not allowed for %s: %s" % (self.__class__.__name__, self.dtype.name) )
def __init__(self, data, target): """ Initialize the buffer """ Buffer.__init__(self, target) # Default offset is 0, only really used for View self._offset = 0 # Allow initialization with a string or a tuple that described dtype if is_string(data): data = np.dtype(data) elif isinstance(data, tuple): data = np.dtype([data]) # Initialize if isinstance(data, np.ndarray): # Fix dtype, vsize, stride. Initialize count array_info = self._parse_array(data) self._dtype, self._vsize, self._stride, self._count = array_info # Set data now self.set_data(data) elif isinstance(data, np.dtype): # Fix dtype, vsize, stride. Initialize count self._dtype, self._vsize, self._stride = self._parse_dtype(data) self._count = 0 else: raise ValueError("DataBuffer needs array of dtype to initialize.") # todo: assume vsize =1 if e.g. np.float32 is passed? # # Check vsize (some dtypes do not specify vsize) # if not self.vsize: # raise ValueError('Could not determine vsize for %s.' % # self.__class__.__name__) # Check data type if self.dtype.fields: for name in self.dtype.names: dtype = self.dtype[name].base if dtype.name not in self.DTYPE2GTYPE: raise TypeError("Data type not allowed for %s: %s" % (self.__class__.__name__, dtype.name)) else: if self.dtype.name not in self.DTYPE2GTYPE: raise TypeError("Data type not allowed for %s: %s" % (self.__class__.__name__, self.dtype.name))
def __init__(self, target, data): """ Initialize the buffer """ Buffer.__init__(self, target) # Default offset is 0, only really used for View self._offset = 0 # Allow smart initialziatin if is_string(data): data = np.dtype(data) # with a string, e.g. "float32" elif isinstance(data, tuple): data = np.dtype([data]) # With a tuple, e.g. ('a', np.float32, 3) elif isinstance(data, list): data = np.dtype(data) # With a list of the above tuples elif isinstance(data, type) and issubclass(data, np.generic): data = np.dtype(data) # With e.g. np.float32 # Initialize if isinstance(data, np.ndarray): # Fix dtype, vsize, stride. Initialize count array_info = self._parse_array(data) self._dtype, self._vsize, self._stride, self._count = array_info # Set data now if not isinstance(self, (ClientVertexBuffer, ClientElementBuffer)): self.set_data(data) elif isinstance(data, np.dtype): # Fix dtype, vsize, stride. Initialize count self._dtype, self._vsize, self._stride = self._parse_dtype(data) self._count = 0 else: raise ValueError("DataBuffer needs array or dtype to initialize.") # Check data type if self.dtype.fields: for name in self.dtype.names: dtype = self.dtype[name].base if dtype.name not in self.DTYPE2GTYPE: raise TypeError("Data type not allowed for %s: %s" % (self.__class__.__name__, dtype.name) ) else: if self.dtype.name not in self.DTYPE2GTYPE: raise TypeError("Data type not allowed for %s: %s" % (self.__class__.__name__, self.dtype.name) )
def _get_error(self, errors, indentation=0): """ Get error and show the faulty line + some context Parameters ---------- error : str An error string as returned byt the compilation process lineno: int Line where error occurs """ # Init if not is_string(errors): errors = errors.decode('utf-8', 'replace') results = [] lines = None if self._code: lines = [line.strip() for line in self._code.split('\n')] for error in errors.split('\n'): # Strip; skip empy lines error = error.strip() if not error: continue # Separate line number from description (if we can) linenr, error = self._parse_error(error) if None in (linenr, lines): results.append('%s' % error) else: results.append('on line %i: %s' % (linenr, error)) if linenr>0 and linenr < len(lines): results.append(' %s' % lines[linenr-1]) # Add indentation and return results = [' '*indentation + r for r in results] return '\n'.join(results)
def __init__(self, vert=None, frag=None): GLObject.__init__(self) # Manage enabled state (i.e. activated) self._active = False self._activated_objects = [] # List of varying names to use for feedback self._feedback_vars = [] # Inis lists of shaders self._verts = [] self._frags = [] # Containers for uniforms and attributes. # name -> Variable self._attributes = {} self._uniforms = {} # Keep track of which names are active (queried after linking) # name -> location self._active_attributes = {} self._active_uniforms = {} # Keep track of number of vertices self._vertex_count = None shaders = [] # Get all vertex shaders if vert is None: pass elif isinstance(vert, VertexShader): shaders.append(vert) elif is_string(vert): shaders.append(VertexShader(vert)) elif isinstance(vert, (list, tuple)): for shader in vert: if is_string(shader): shader = VertexShader(shader) shaders.append(shader) else: raise ValueError('Invalid value for VertexShader "%r"' % vert) # Get all fragment shaders if frag is None: pass elif isinstance(frag, FragmentShader): shaders.append(frag) elif is_string(frag): shaders.append(FragmentShader(frag)) elif isinstance(frag, (list, tuple)): for shader in frag: if is_string(shader): shader = FragmentShader(shader) shaders.append(shader) else: raise ValueError('Invalid value for FragmentShader "%r"' % frag) # Attach shaders now if shaders: self.attach(*shaders)