def _get_active_attributes(self): """ Extract active attributes from GPU """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_ATTRIBUTES) attributes = [] # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])""") for i in range(count): name, size, gtype = gl.glGetActiveAttrib(self.handle, i) # This checks if the attribute is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When attribute is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'),i) attributes.append((name, gtype)) else: attributes.append((name, gtype)) return attributes
def _get_active_uniforms(self): """ Extract active uniforms from GPU """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_UNIFORMS) # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])\s*""") uniforms = [] for i in range(count): name, size, gtype = gl.glGetActiveUniform(self.handle, i) name = name.decode() # This checks if the uniform is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When uniform is an array, size corresponds to the highest used index if m: name = m.group("name") if size >= 1: for i in range(size): name = "%s[%d]" % (m.group("name"), i) uniforms.append((name, gtype)) else: uniforms.append((name, gtype)) return uniforms
def active_attributes(self): """ List of active attributes requested from GPU (read only). .. note:: An inactive attribute is an attribute that has been declared but that is not actually used in the shader. Example: .. code:: attribute vec3 normal; # Inactive attribute vec3 position; # Active void main() { gl_Position = vec4(position, 1.0); } """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_ATTRIBUTES) attributes = [] # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])""") for i in range(count): name, size, gtype = gl.glGetActiveAttrib(self.handle, i) name = name.decode() # This checks if the attribute is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When attribute is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'), i) attributes.append((name, gtype)) else: attributes.append((name, gtype)) return attributes
def active_attributes(self): """ List of active attributes requested from GPU (read only). .. note:: An inactive attribute is an attribute that has been declared but that is not actually used in the shader. Example: .. code:: attribute vec3 normal; # Inactive attribute vec3 position; # Active void main() { gl_Position = vec4(position, 1.0); } """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_ATTRIBUTES) attributes = [] # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])""") for i in range(count): name, size, gtype = gl.glGetActiveAttrib(self.handle, i) name = name.decode() # This checks if the attribute is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When attribute is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'),i) attributes.append((name, gtype)) else: attributes.append((name, gtype)) return attributes
def active_uniforms(self): """ List of active uniform requested from GPU (read only). .. note:: An inactive uniform is a uniform that has been declared but that is not actually used in the shader. Example: .. code:: uniform vec3 color; # Inactive void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); } """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_UNIFORMS) # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])\s*""") uniforms = [] for i in range(count): name, size, gtype = gl.glGetActiveUniform(self.handle, i) name = name.decode() # This checks if the uniform is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When uniform is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'), i) uniforms.append((name, gtype)) else: uniforms.append((name, gtype)) return uniforms
def active_uniforms(self): """ List of active uniform requested from GPU (read only). .. note:: An inactive uniform is a uniform that has been declared but that is not actually used in the shader. Example: .. code:: uniform vec3 color; # Inactive void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); } """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_UNIFORMS) # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])\s*""") uniforms = [] for i in range(count): name, size, gtype = gl.glGetActiveUniform(self.handle, i) name = name.decode() # This checks if the uniform is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When uniform is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'),i) uniforms.append((name, gtype)) else: uniforms.append((name, gtype)) return uniforms
def _create(self): """ Build (link) the program and checks everything's ok. A GL context must be available to be able to build (link) """ log.debug("GPU: Creating program") # Check if program has been created if self._handle <= 0: self._handle = gl.glCreateProgram() if not self._handle: raise ValueError("Cannot create program object") self._build_shaders(self._handle) log.debug("GPU: Linking program") # Link the program gl.glLinkProgram(self._handle) if not gl.glGetProgramiv(self._handle, gl.GL_LINK_STATUS): print(gl.glGetProgramInfoLog(self._handle)) raise ValueError('Linking error') # Activate uniforms active_uniforms = [name for (name, gtype) in self.active_uniforms] for uniform in self._uniforms.values(): if uniform.name in active_uniforms: uniform.active = True else: uniform.active = False # Activate attributes active_attributes = [name for (name, gtype) in self.active_attributes] for attribute in self._attributes.values(): if attribute.name in active_attributes: attribute.active = True else: attribute.active = False
def _create(self): """ Build (link) the program and checks everything's ok. A GL context must be available to be able to build (link) """ log.debug("GPU: Creating program") # Check if program has been created if self._handle <= 0: self._handle = gl.glCreateProgram() if not self._handle: raise ValueError("Cannot create program object") self._build_shaders(self._handle) log.debug("GPU: Linking program") # Link the program gl.glLinkProgram(self._handle) if not gl.glGetProgramiv(self._handle, gl.GL_LINK_STATUS): print(gl.glGetProgramInfoLog(self._handle)) raise ValueError('Linking error') # Activate uniforms active_uniforms = [name for (name,gtype) in self.active_uniforms] for uniform in self._uniforms.values(): if uniform.name in active_uniforms: uniform.active = True else: uniform.active = False # Activate attributes active_attributes = [name for (name,gtype) in self.active_attributes] for attribute in self._attributes.values(): if attribute.name in active_attributes: attribute.active = True else: attribute.active = False