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_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 __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 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 new_program(cls): """ Create a new program. The object own the ressources """ pobj = super().__new__(cls) pobj.pid = c_uint(glCreateProgram()) pobj.uniforms = ShaderUniformAccessor(pobj) pobj.attributes = ShaderAttributeAccessor(pobj) pobj.owned = True return pobj
def __init__(self, vertex=None, geometry=None, fragment=None, link=False): self.program_id = glCreateProgram() self._shaders = [] if not vertex is None: self.attachShader(GL_VERTEX_SHADER, vertex) if not geometry is None: self.attachShader(GL_GEOMETRY_SHADER, geometry) if not fragment is None: self.attachShader(GL_FRAGMENT_SHADER, fragment) if link: self.linkProgram() self._in_use = 0
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, 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 __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 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 __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 __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 = []
def __init__(self): self.program_id = glCreateProgram() self._shaders = []
def _alloc(self): # pylint: disable=assignment-from-no-return self._hnd = glCreateProgram() logging.info('glCreateProgram() -> %d', self._hnd) if self._hnd == 0: raise ValueError('glCreateProgram failed')