def initProgram(self): # create unique shader program id self.program_id = glCreateProgram() # load and compile individual shaders vertsource = self.loadShader(self.vertpath) fragsource = self.loadShader(self.fragpath) vert_id = self.getShader(vertsource, GL_VERTEX_SHADER) frag_id = self.getShader(fragsource, GL_FRAGMENT_SHADER) # if it's ok, attach them to shader program glAttachShader(self.program_id, vert_id) glAttachShader(self.program_id, frag_id) self.analyze( vertsource ) # in case of new (+#330 ;D) shaders - nothing will happen. # link program means make program obj with created executables for different programmable processors for shaders, # that were attached. glLinkProgram(self.program_id) # if something went wrong if glGetProgramiv(self.program_id, GL_LINK_STATUS) != GL_TRUE: info = glGetProgramInfoLog(self.program_id) glDeleteProgram(self.program_id) # they should be deleted anyway glDeleteShader(vert_id) glDeleteShader(frag_id) raise RuntimeError("Error in program linking: %s" % info) # shaders are attached, program is linked -> full shader program with compiled executables is ready, # no need in individual shaders ids, i suppose glDeleteShader(vert_id) glDeleteShader(frag_id)
def __init__(self, vertex, fragment): """ Parameters ---------- vertex : str String containing shader source code for the vertex shader fragment : str String containing shader source code for the fragment shader """ self.program_id = glCreateProgram() vs_id = self.add_shader(vertex, GL_VERTEX_SHADER) frag_id = self.add_shader(fragment, GL_FRAGMENT_SHADER) glAttachShader(self.program_id, vs_id) glAttachShader(self.program_id, frag_id) glLinkProgram(self.program_id) if glGetProgramiv(self.program_id, GL_LINK_STATUS) != GL_TRUE: info = glGetProgramInfoLog(self.program_id) glDeleteProgram(self.program_id) glDeleteShader(vs_id) glDeleteShader(frag_id) raise RuntimeError('Error linking program: %s' % (info)) glDeleteShader(vs_id) glDeleteShader(frag_id)
def init_shaders(self,vtx=None,frg=None): self.program_id = glCreateProgram() if vtx: self.VTX.source = vtx self.VTX.fname = None #print self.VTX.source self.VTX.id = self.add_shader(self.VTX.source, GL_VERTEX_SHADER) if frg: self.FRG.source = frg self.FRG.fname = None self.FRG.id = self.add_shader(self.FRG.source, GL_FRAGMENT_SHADER) glAttachShader(self.program_id, self.VTX.id) glAttachShader(self.program_id, self.FRG.id) glLinkProgram(self.program_id) if glGetProgramiv(self.program_id, GL_LINK_STATUS) != GL_TRUE: info = glGetProgramInfoLog(self.program_id) glDeleteProgram(self.program_id) glDeleteShader(self.VTX.id) glDeleteShader(self.FRG.id) raise RuntimeError('Error linking program: %s' % (info)) glDeleteShader(self.VTX.id) glDeleteShader(self.FRG.id)
def __init__(self, archivo_vert, archivo_frag, nombre_vert="Vertex Shader", nombre_frag="Fragment Shader"): print("Shader creado") # Creación de shaders y programa self.vshader = glCreateShader(GL_VERTEX_SHADER) self.fshader = glCreateShader(GL_FRAGMENT_SHADER) self.programa = glCreateProgram() # Cargamos los datos de los archivos en los shaders self.cargar_datos(self.vshader, archivo_vert) self.cargar_datos(self.fshader, archivo_frag) # Compilamos los shaders resultado_vert = self.compilar(self.vshader, nombre_vert) resultado_frag = self.compilar(self.fshader, nombre_frag) # Error en la compilación if (resultado_vert != 1) or (resultado_frag != 1): self.destruir() # Sin errores hasta ahora else: # Enlazamos los shaders al programa glAttachShader(self.programa, self.vshader) glAttachShader(self.programa, self.fshader) # Enlazamos el programa glLinkProgram(self.programa) # Desenlazaos y eliminamos los shaders glDetachShader(self.programa, self.vshader) glDetachShader(self.programa, self.fshader) glDeleteShader(self.vshader) glDeleteShader(self.fshader) # Errores durante el enlace resultado_progr = glGetProgramiv(self.programa, GL_LINK_STATUS) if resultado_progr != 1: self.destruir()
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 createShader(vSource, fSource): #Creo y compilo el vertex shader vProgram = ShaderManager.compileProgram(vSource, GL_VERTEX_SHADER) #Creo y compilo el fragment shader fProgram = ShaderManager.compileProgram(fSource, GL_FRAGMENT_SHADER) #Creo un programa de shading vacio en memoria de video shader = glCreateProgram() #Le adjunto el codigo objeto del vertex shader compilado glAttachShader(shader, vProgram) #Le adjunto el codigo objeto del fragment shader compilado glAttachShader(shader, fProgram) #Intento linkear el programa para generar un ejecutable en memoria de video glLinkProgram(shader) #Chequeo si la ejecucion del linkeo del programa fue exitosa if glGetProgramiv(shader, GL_LINK_STATUS) != GL_TRUE: #Si falla, imprimo el mensaje de error y libero los recursos print(glGetProgramInfoLog(shader)) glDeleteProgram(shader) return 0 #Una vez que el programa fue linkeado, haya sido exitoso o no, ya no necesito los shaders #individuales compilados, asi que libero sus recursos glDeleteShader(vProgram) glDeleteShader(fProgram) return shader
def linkProgram(self): glLinkProgram(self.program_id) program_log = glGetProgramInfoLog(self.program_id) if program_log: raise RuntimeError("shader_program\n%s" % program_log) for shader in self._shaders: glDeleteShader(shader) self._shaders = []
def link(self): """ Link the shader program. Return True if the linking was successful, False otherwise. Also reload the uniform cache is successful """ glLinkProgram(self.pid) if self.link_status == GL_TRUE: self.uniforms.reload() self.attributes.reload() return True
def initialize_program(*shaders): program = glCreateProgram() for shader in shaders: glAttachShader(program, shader) glLinkProgram(program) for shader in shaders: dispose_shader(shader) result = glGetProgramiv(program, GL_LINK_STATUS) if result == 0: raise RuntimeError(glGetProgramInfoLog(program)) return program
def make_program(vertex_shader, fragment_shader): program = glCreateProgram() glAttachShader(program, vertex_shader) glAttachShader(program, fragment_shader) glLinkProgram(program) retval = ctypes.c_int() glGetProgramiv(program, GL_LINK_STATUS, retval) if not retval: print >> sys.stderr, "Failed to link shader program." print glGetProgramInfoLog(program) glDeleteProgram(program) raise Exception("Failed to link shader program.") return program
def CreateShader(self, VertexShader, FragmentShader): program = glCreateProgram() vs = self.CompileShader(GL_VERTEX_SHADER, VertexShader) fs = self.CompileShader(GL_FRAGMENT_SHADER, FragmentShader) glAttachShader(program, vs) glAttachShader(program, fs) glLinkProgram(program) glValidateProgram(program) glDeleteShader(vs) glDeleteShader(fs) return program
def link(self): self.__programId = glCreateProgram() for shader in self.shaders: shader.compile() glAttachShader(self.__programId, shader.getId()) glLinkProgram(self.__programId) for shader in self.shaders: shader.delete() self.shaders.clear() if glGetProgramiv(self.__programId, GL_LINK_STATUS) != GL_TRUE: info = glGetProgramInfoLog(self.__programId) self.delete() raise RuntimeError(f'Error in program linking: {info}')
def __init__(self, vertex_source=None, fragment_source=None): self.program = glCreateProgram() if vertex_source: self.vertex_shader = self.create_shader( vertex_source, GL_VERTEX_SHADER) glAttachShader(self.program, self.vertex_shader) if fragment_source: self.fragment_shader = self.create_shader( fragment_source, GL_FRAGMENT_SHADER) glAttachShader(self.program, self.fragment_shader) glLinkProgram(self.program) message = self.get_program_log(self.program) if message: pymt_logger.debug('Shader: shader program message: %s' % message)
def change_shader(self, vertex=None, fragment=None): """Change vertex and fragment shader of current program. Needs indices of shaders from the list. """ changed = False if vertex is not None: changed |= self.__attach_shader( self.__shaders[GL_VERTEX_SHADER][vertex], GL_VERTEX_SHADER) if fragment is not None: changed |= self.__attach_shader( self.__shaders[GL_FRAGMENT_SHADER][fragment], GL_FRAGMENT_SHADER) if not changed: return glLinkProgram(self.__program) assert glGetProgramiv(self.__program, GL_LINK_STATUS) == GL_TRUE
def __init__(self, shaderPaths=[]): self.shader_program = glCreateProgram() self.shader_objects = {} for (shader_type, path) in shaderPaths: self.shader_objects[shader_type] = self.loadAndCompileShaderObject( shader_type, path) glAttachShader(self.shader_program, self.shader_objects[shader_type]) glLinkProgram(self.shader_program) if glGetProgramiv(self.shader_program, GL_LINK_STATUS) != GL_TRUE: info = glGetProgramInfoLog(self.shader_program) glDeleteProgram(self.shader_program) for shader in self.shader_objects: glDeleteShader(shader) raise RuntimeError("Error in program linking: %s" % info)
def _createProgram(self, shaders): prog = None try: prog = glCreateProgram() for shader in shaders: glAttachShader(prog, shader) glLinkProgram(prog) if glGetProgramiv(prog, GL_LINK_STATUS) != GL_TRUE: info = glGetProgramInfoLog(prog) raise Exception, "Unable to link program. Info log:\n%s" % (info) return prog except Exception: if prog != None: glDeleteProgram(prog) raise
def createProgram(self): vertexShader = self.createShader(self.vertexSource, GL_VERTEX_SHADER) fragmentShader = self.createShader(self.fragmentSource, GL_FRAGMENT_SHADER) program = glCreateProgram() glAttachShader(program, vertexShader) glAttachShader(program, fragmentShader) glLinkProgram(program) self.status[GL_LINK_STATUS] = glGetProgramiv(program, GL_LINK_STATUS) self.infoLog[GL_LINK_STATUS] = glGetProgramInfoLog(program) glDeleteShader(vertexShader) glDeleteShader(fragmentShader) return program
def __init__(self, vertex_shader_code: str, fragment_shader_code: str) -> None: self.__vertex_shader_id: int = self._load_shader( vertex_shader_code, GL_VERTEX_SHADER) self.__fragment_shader_id: int = self._load_shader( fragment_shader_code, GL_FRAGMENT_SHADER) self.__program_id: int = glCreateProgram() self.__location_transformation_matrix: int = -1 self.__location_view_matrix: int = -1 self.__location_projection_matrix: int = -1 glAttachShader(self.__program_id, self.__vertex_shader_id) glAttachShader(self.__program_id, self.__fragment_shader_id) self._bind_attributes() glLinkProgram(self.__program_id) glValidateProgram(self.__program_id) self._get_uniform_locations()
def compile_shader(vertex_shader_source, fragment_shader_source): vertex_shader = load_shader(GL_VERTEX_SHADER, vertex_shader_source) fragment_shader = load_shader(GL_FRAGMENT_SHADER, fragment_shader_source) program = glCreateProgram() if program == 0: raise ShaderException() glAttachShader(program, vertex_shader) glAttachShader(program, fragment_shader) glLinkProgram(program) if glGetProgramiv(program, GL_LINK_STATUS, None) == GL_FALSE: info_log = glGetProgramInfoLog(program) glDeleteProgram(program) raise ShaderException(info_log) return program
def update(self, *shaders): # TODO: for now this is actually create, not update self._shaders += shaders for shader in self._shaders: glAttachShader(self._hnd, shader.hnd) logging.info('glLinkProgram(%d)', self._hnd) glLinkProgram(self._hnd) logging.info('glGetProgramInfoLog(%d) -> %s', self._hnd, glGetProgramInfoLog(self._hnd).decode().strip()) self._uniforms = {} self._attributes = bidict() for shader in self._shaders: for name in shader.uniforms(): self._uniforms[name] = glGetUniformLocation(self._hnd, name) for name, location in shader.attributes(): if location in self._attributes.inv: raise KeyError('duplication attribute location', location, name) self._attributes[name] = location
def __init__(self, vertex, fragment, number_of_buffers=0, number_of_textures=0): """Initialize program with shaders.""" self.__program = glCreateProgram() self.__current_shaders = {} self.__shaders = { GL_VERTEX_SHADER: [], GL_FRAGMENT_SHADER: [] } self.__depth_map_fbo = None self.__attributes = [] self.__initiate_shaders(vertex, fragment) self.change_shader(0, 0) glLinkProgram(self.__program) assert glGetProgramiv(self.__program, GL_LINK_STATUS) == GL_TRUE self.__initiate_buffers(number_of_buffers) self.__initiate_textures(number_of_textures)
def on_realize(self, area): # We need to make the context current if we want to # call GL API area.make_current() context = area.get_context() if (area.get_error() != None): return fragment_shader = shaders.compileShader(FRAGMENT_SOURCE, GL_FRAGMENT_SHADER) vertex_shader = shaders.compileShader(VERTEX_SOURCE, GL_VERTEX_SHADER) self.shaderContent.shader_prog = shaders.compileProgram( fragment_shader, vertex_shader) glLinkProgram(self.shaderContent.shader_prog) self.vertex_array_object = glGenVertexArrays(1) glBindVertexArray(self.vertex_array_object) # Generate buffers to hold our vertices self.vertex_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer) self.position = glGetAttribLocation(self.shaderContent.shader_prog, 'position') self.time_l = glGetUniformLocation(self.shaderContent.shader_prog, 'time') print(self.time_l) # glBindAttribLocation(self.shaderContent.shader_prog, self.time, 'time') glEnableVertexAttribArray(self.position) glVertexAttribPointer(index=self.position, size=4, type=GL_FLOAT, normalized=False, stride=0, pointer=ctypes.c_void_p(0)) glBufferData(GL_ARRAY_BUFFER, 192, self.vertices, GL_STATIC_DRAW) glBindVertexArray(0) glDisableVertexAttribArray(self.position) glBindBuffer(GL_ARRAY_BUFFER, 0) self.on_render(self.shaderContent) return True
def __init__(self, vertex_source=None, fragment_source=None): print("In Shader File") self.program = glCreateProgram() vertex_file = open(vertex_source, "r") fragment_file = open(fragment_source, "r") vertex = vertex_file.read() fragment = fragment_file.read() vertex_file.close() fragment_file.close() if vertex_source: self.vertex_shader = self.create_shader(vertex, GL_VERTEX_SHADER) glAttachShader(self.program, self.vertex_shader) if fragment_source: self.fragment_shader = self.create_shader(fragment, GL_FRAGMENT_SHADER) glAttachShader(self.program, self.fragment_shader) glLinkProgram(self.program) message = self.get_program_log(self.program) if message: print('Init Shader: shader program message: %s' % message)
def __init__(self, vertex, fragment, number_of_buffers=0, number_of_textures=0): """Initialize program with shaders.""" self.__program = glCreateProgram() self.__current_shaders = {} self.__shaders = {GL_VERTEX_SHADER: [], GL_FRAGMENT_SHADER: []} self.__depth_map_fbo = None self.__attributes = [] if not isinstance(vertex, list): vertex = [vertex] for v in vertex: self.__load_shader(get_shader_path(v), GL_VERTEX_SHADER) if not isinstance(fragment, list): fragment = [fragment] for f in fragment: self.__load_shader(get_shader_path(f), GL_FRAGMENT_SHADER) self.change_shader(0, 0) glLinkProgram(self.__program) assert glGetProgramiv(self.__program, GL_LINK_STATUS) == GL_TRUE self.__vao_id = glGenVertexArrays(1) glBindVertexArray(self.__vao_id) if number_of_buffers == 1: self.__vbo_id = [glGenBuffers(number_of_buffers)] elif number_of_buffers > 1: self.__vbo_id = glGenBuffers(number_of_buffers) if number_of_textures == 1: self.__textures_ids = [glGenTextures(1)] elif number_of_textures > 1: self.__textures_ids = glGenTextures(number_of_textures) self.__textures = []