def add_shader(self, source, shader_type): """ Helper function for compiling a GLSL shader Parameters ---------- source : str String containing shader source code shader_type : valid OpenGL shader type Type of shader to compile Returns ------- value : int Identifier for shader if compilation is successful """ try: shader_id = glCreateShader(shader_type) glShaderSource(shader_id, source) glCompileShader(shader_id) if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE: info = glGetShaderInfoLog(shader_id) raise RuntimeError('Shader compilation failed: %s' % (info)) return shader_id except: glDeleteShader(shader_id) raise
def create(self): if not (self.status & Resource.LOADED): return vs = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vs, self.vs_str) glCompileShader(vs) if glGetShaderiv(vs, GL_COMPILE_STATUS) != GL_TRUE: raise RuntimeError(glGetShaderInfoLog(vs)) fs = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fs, self.fs_str) glCompileShader(fs) if glGetShaderiv(fs, GL_COMPILE_STATUS) != GL_TRUE: raise RuntimeError(glGetShaderInfoLog(fs)) program = glCreateProgram() glAttachShader(program, vs) glAttachShader(program, fs) glLinkProgram(program) if glGetProgramiv(program, GL_LINK_STATUS) != GL_TRUE: raise RuntimeError(glGetProgramInfoLog(program)) glValidateProgram(program) validation = glGetProgramiv(program, GL_VALIDATE_STATUS) if validation == GL_FALSE: raise RuntimeError(glGetProgramInfoLog(program)) # todo: check for errrors before setting status self.program = program self.status |= Resource.CREATED
def initialize_shader(shader_type, shader_source): shader = glCreateShader(shader_type) glShaderSource(shader, shader_source) glCompileShader(shader) result = glGetShaderiv(shader, GL_COMPILE_STATUS) if result == 0: raise RuntimeError(glGetShaderInfoLog(shader)) return shader
def _load_shader(shader_code: str, shader_type: int) -> int: shader_id: int = glCreateShader(shader_type) glShaderSource(shader_id, shader_code) glCompileShader(shader_id) if glGetShaderiv(shader_id, GL_COMPILE_STATUS) == GL_FALSE: raise RuntimeError("Could not complie shader!") return shader_id
def __load_shader(self, shader_filename, shader_type): """Load shader of specific type from file.""" shader_source = '' with open(shader_filename) as shader_file: shader_source = shader_file.read() assert shader_source shader_id = glCreateShader(shader_type) self.__shaders[shader_type].append(shader_id) glShaderSource(shader_id, shader_source) glCompileShader(shader_id)
def cargar_datos(self, shader, archivo_shader): """Carga el archivo y devuelve los datos del shader. shader = shader a cargar los datos archivo_shader = archivo con los datos fuente para el shader""" # Abrimos el archivo y obtenemos los datos archivo = open(archivo_shader,"r") data = archivo.read() # Cerramos el archivo archivo.close() # Asignamos los datos al shader glShaderSource(shader, [data])
def createShader(self, source, shaderType): shader = glCreateShader(shaderType) glShaderSource(shader, source, None) glCompileShader(shader) self.status[shaderType] = glGetShaderiv(shader, GL_COMPILE_STATUS) self.infoLog[shaderType] = glGetShaderInfoLog(shader) return shader
def make_shader(shadertype, source): shader = glCreateShader(shadertype) glShaderSource(shader, source) glCompileShader(shader) retval = ctypes.c_uint(GL_UNSIGNED_INT) glGetShaderiv(shader, GL_COMPILE_STATUS, retval) if not retval: print >> sys.stderr, "Failed to compile shader." print glGetShaderInfoLog(shader) glDeleteShader(shader) raise Exception("Failed to compile shader.") return shader
def getShader(self, shader_source, shader_type): try: shader_id = glCreateShader(shader_type) glShaderSource(shader_id, shader_source) glCompileShader(shader_id) if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE: info = glGetShaderInfoLog(shader_id) raise RuntimeError('Shader compilation failed:\n %s' % info) return shader_id except: glDeleteShader(shader_id) raise
def create_shader(self, source, shadertype): shader = glCreateShader(shadertype) # PyOpenGL bug ? He's waiting for a list of string, not a string # on some card, it failed :) # if isinstance(source, basestring): # source = [source] glShaderSource(shader, source) glCompileShader(shader) message = self.get_shader_log(shader) if message: print(' Create Shader: shader message: %s' % message) return shader
def create_shader(self, source, shadertype): shader = glCreateShader(shadertype) # PyOpenGL bug ? He's waiting for a list of string, not a string # on some card, it failed :) if isinstance(source, basestring): source = [source] glShaderSource(shader, source) glCompileShader(shader) message = self.get_shader_log(shader) if message: pymt_logger.debug('Shader: shader message: %s' % message) return shader
def CompileShader(self, type, source): id = glCreateShader(type) glShaderSource(id, source) glCompileShader(id) result = glGetShaderiv(id, GL_COMPILE_STATUS) if (result==GL_FALSE): message = glGetShaderInfoLog(id).decode('utf-8') shadertype = "Vertex" if type==GL_VERTEX_SHADER else "Fragment" print(f"Failed to compile {shadertype} shader!") print(message) glDeleteShader(id) return 0 return id
def add_shader(self, source, shader_type): try: #print "TEST" #print source shader_id = glCreateShader(shader_type) glShaderSource(shader_id, source) glCompileShader(shader_id) if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE: info = glGetShaderInfoLog(shader_id) raise RuntimeError('Shader compilation failed: %s' % (info)) return shader_id except: glDeleteShader(shader_id) raise
def _createShader(self, shadertype, source): try: shader = None shader = glCreateShader(shadertype) glShaderSource(shader, source) glCompileShader(shader) if glGetShaderiv(shader, GL_COMPILE_STATUS) != GL_TRUE: info = glGetShaderInfoLog(shader) raise Exception, "Unable to compile shader. Infolog:\n%s" % (info,) return shader except Exception: # Cleanup on exception if shader != None: glDeleteShader(shader) raise
def loadAndCompileShaderObject(self, shader_type, path): if not os.path.exists(path): raise RuntimeError('Shader source not found at: %s' % path) source_file = open(path) shader_source = source_file.read() source_file.close() shader_object = glCreateShader(shader_type) glShaderSource(shader_object, shader_source) glCompileShader(shader_object) if glGetShaderiv(shader_object, GL_COMPILE_STATUS) != GL_TRUE: info = glGetShaderInfoLog(shader_object) raise RuntimeError('Shader compilation failed:\n %s' % info) return shader_object
def attachShader(self,type,source): shader = glCreateShader(type) glShaderSource(shader,source) glCompileShader(shader) shader_log = glGetShaderInfoLog(shader) if shader_log: if type == GL_FRAGMENT_SHADER: str_type = "GL_FRAGMENT_SHADER" elif type == GL_VERTEX_SHADER: str_type = "GL_VERTEX_SHADER" elif type == GL_GEOMETRY_SHADER: str_type = "GL_GEOMETRY_SHADER" else: str_type = "unkown shader type %s" % str(type) raise RuntimeError("%s\n%s" % (str_type, shader_log)) glAttachShader(self.program_id, shader) self._shaders.append(shader)
def load_shader(shader_type, source): shader = glCreateShader(shader_type) if shader == 0: raise ShaderException() glShaderSource(shader, source) glCompileShader(shader) if glGetShaderiv(shader, GL_COMPILE_STATUS, None) == GL_FALSE: info_log = glGetShaderInfoLog(shader) glDeleteShader(shader) raise ShaderException(info_log) return shader
def _compile(self): logging.info('glShaderSource(%d, ...)', self._hnd) glShaderSource(self._hnd, self._source) logging.info('glCompileShader(%d)', self._hnd) glCompileShader(self._hnd) if not glGetShaderiv(self._hnd, GL_COMPILE_STATUS): logging.error('glCompileShader failed') compile_log = glGetShaderInfoLog(self._hnd).decode('utf-8') glDeleteShader(self._hnd) if self._path == '': print_source(self._source) pretty_print_log(compile_log, self._path) raise RuntimeError('shader failed to compile', compile_log)
def compileProgram(path, type): #Leo el codigo fuente desde el archivo sourceFile = open(path, "r") source = sourceFile.read() #Creo un shader vacio en memoria de video, del tipo indicado #En la variable shader queda almacenado un indice que nos va a permitir identificar este shader de ahora en mas shader = glCreateShader(type) #Le adjunto el codigo fuente leido desde el archivo glShaderSource(shader, source) #Intento compilarlo glCompileShader(shader) #Con la funcion glGelShaderiv puedo obtener el estado del compilador de shaders #En este caso le pido el stado de la ultima compilacion ejecutada if glGetShaderiv(shader, GL_COMPILE_STATUS) != GL_TRUE: #Si la compilacion falla, muestro el error y retorno 0 (shader nulo) print(path + ': ' + glGetShaderInfoLog(shader)) #Me aseguro de liberar los recursos que reserve en memoria de vide, ya que no los voy a usar glDeleteShader(shader) return 0 else: return shader
def source(self, src): #glShaderSource(self.sid, 1, shader_source(src.encode('UTF-8')), null_c_int) src = src.encode('UTF-8') #print(self,src) #shaders.compileShader(src) glShaderSource(self.sid, src)