def link(self): glLinkProgram(self.handle) if self.was_link_successful(): self.linked = True else: self.log.warn("Could not link shader program")
def setup_program(): ''' Create the glsl program ''' vertex_shader = b''' attribute vec2 position; attribute vec4 color; varying vec4 var_color; void main() { gl_Position = vec4(position, 0.0, 1.0); var_color = color; } ''' fragment_shader = b''' varying vec4 var_color; void main() { gl_FragColor = var_color; } ''' program = gl.glCreateProgram() gl.glAttachShader(program, shader(gl.GL_VERTEX_SHADER, vertex_shader)) gl.glAttachShader(program, shader(gl.GL_FRAGMENT_SHADER, fragment_shader)) gl.glLinkProgram(program) gl.glUseProgram(program) return program
def __init__(self, *shaders: Shader): self.name = gl.glCreateProgram() for shader in shaders: gl.glAttachShader(self.name, shader.name) gl.glLinkProgram(self.name) success = gl.GLint(0) gl.glGetProgramiv(self.name, gl.GL_LINK_STATUS, byref(success)) if not success: log_length = gl.GLint(0) gl.glGetProgramiv(self.name, gl.GL_INFO_LOG_LENGTH, byref(log_length)) log_buffer = create_string_buffer(log_length.value) gl.glGetProgramInfoLog(self.name, log_length.value, None, log_buffer) self.logger.error("Error linking program %s, error # %d", self.name, success.value) self.logger.error("---Message---") for line in log_buffer.value.decode("ascii").splitlines(): self.logger.error("Program: " + line) self.logger.error("------") raise RuntimeError("Linking program failed.") # free resources for shader in shaders: gl.glDeleteShader(shader.name)
def _link(self): for shader in self: gl.glAttachShader(self.handle, shader.handle); # link the program gl.glLinkProgram(self.handle) temp = c_int(0) # retrieve the link status gl.glGetProgramiv(self.handle, gl.GL_LINK_STATUS, byref(temp)) # if linking failed, print the log if not temp: # retrieve the log length gl.glGetProgramiv(self.handle, gl.GL_INFO_LOG_LENGTH, byref(temp)) # create a buffer for the log buffer = create_string_buffer(temp.value) # retrieve the log text gl.glGetProgramInfoLog(self.handle, temp, None, buffer) # print the log to the console raise GLSLError(buffer.value) # Query maximum uniform name length AUL = gl.GLint() gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_UNIFORM_MAX_LENGTH, byref(AUL)) self._ACTIVE_UNIFORM_MAX_LENGTH = AUL.value self._update_uniform_types()
def _create_device_objects(self): # save state last_texture = gl.GLint() gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D, byref(last_texture)) last_array_buffer = gl.GLint() gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING, byref(last_array_buffer)) last_vertex_array = gl.GLint() gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING, byref(last_vertex_array)) self._shader_handle = gl.glCreateProgram() # note: no need to store shader parts handles after linking vertex_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER) fragment_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) gl.glShaderSource(vertex_shader, 1, make_string_buffer(self.VERTEX_SHADER_SRC), None) gl.glShaderSource(fragment_shader, 1, make_string_buffer(self.FRAGMENT_SHADER_SRC), None) gl.glCompileShader(vertex_shader) gl.glCompileShader(fragment_shader) gl.glAttachShader(self._shader_handle, vertex_shader) gl.glAttachShader(self._shader_handle, fragment_shader) gl.glLinkProgram(self._shader_handle) # note: after linking shaders can be removed gl.glDeleteShader(vertex_shader) gl.glDeleteShader(fragment_shader) self._attrib_location_tex = gl.glGetUniformLocation(self._shader_handle, create_string_buffer(b"Texture")) self._attrib_proj_mtx = gl.glGetUniformLocation(self._shader_handle, create_string_buffer(b"ProjMtx")) self._attrib_location_position = gl.glGetAttribLocation(self._shader_handle, create_string_buffer(b"Position")) self._attrib_location_uv = gl.glGetAttribLocation(self._shader_handle, create_string_buffer(b"UV")) self._attrib_location_color = gl.glGetAttribLocation(self._shader_handle, create_string_buffer(b"Color")) self._vbo_handle = gl.GLuint() gl.glGenBuffers(1, byref(self._vbo_handle)) self._elements_handle = gl.GLuint() gl.glGenBuffers(1, byref(self._elements_handle)) self._vao_handle = gl.GLuint() gl.glGenVertexArrays(1, byref(self._vao_handle)) gl.glBindVertexArray(self._vao_handle) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._vbo_handle) gl.glEnableVertexAttribArray(self._attrib_location_position) gl.glEnableVertexAttribArray(self._attrib_location_uv) gl.glEnableVertexAttribArray(self._attrib_location_color) gl.glVertexAttribPointer(self._attrib_location_position, 2, gl.GL_FLOAT, gl.GL_FALSE, imgui.VERTEX_SIZE, c_void_p(imgui.VERTEX_BUFFER_POS_OFFSET)) gl.glVertexAttribPointer(self._attrib_location_uv, 2, gl.GL_FLOAT, gl.GL_FALSE, imgui.VERTEX_SIZE, c_void_p(imgui.VERTEX_BUFFER_UV_OFFSET)) gl.glVertexAttribPointer(self._attrib_location_color, 4, gl.GL_UNSIGNED_BYTE, gl.GL_TRUE, imgui.VERTEX_SIZE, c_void_p(imgui.VERTEX_BUFFER_COL_OFFSET)) # restore state gl.glBindTexture(gl.GL_TEXTURE_2D, cast((c_int*1)(last_texture), POINTER(c_uint)).contents) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, cast((c_int*1)(last_array_buffer), POINTER(c_uint)).contents) gl.glBindVertexArray(cast((c_int*1)(last_vertex_array), POINTER(c_uint)).contents)
def __init__(self, ec, fill_color, line_color, line_width, line_loop): self._ec = ec self._line_width = line_width self._line_loop = line_loop # whether or not lines drawn are looped # initialize program and shaders from pyglet import gl self._program = gl.glCreateProgram() vertex = gl.glCreateShader(gl.GL_VERTEX_SHADER) buf = create_string_buffer(tri_vert.encode('ASCII')) ptr = cast(pointer(pointer(buf)), POINTER(POINTER(c_char))) gl.glShaderSource(vertex, 1, ptr, None) gl.glCompileShader(vertex) _check_log(vertex, gl.glGetShaderInfoLog) fragment = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) buf = create_string_buffer(tri_frag.encode('ASCII')) ptr = cast(pointer(pointer(buf)), POINTER(POINTER(c_char))) gl.glShaderSource(fragment, 1, ptr, None) gl.glCompileShader(fragment) _check_log(fragment, gl.glGetShaderInfoLog) gl.glAttachShader(self._program, vertex) gl.glAttachShader(self._program, fragment) gl.glLinkProgram(self._program) _check_log(self._program, gl.glGetProgramInfoLog) gl.glDetachShader(self._program, vertex) gl.glDetachShader(self._program, fragment) gl.glUseProgram(self._program) # Prepare buffers and bind attributes loc = gl.glGetUniformLocation(self._program, b'u_view') view = ec.window_size_pix view = np.diag([2. / view[0], 2. / view[1], 1., 1.]) view[-1, :2] = -1 view = view.astype(np.float32).ravel() gl.glUniformMatrix4fv(loc, 1, False, (c_float * 16)(*view)) self._counts = dict() self._colors = dict() self._buffers = dict() self._points = dict() self._tris = dict() for kind in ('line', 'fill'): self._counts[kind] = 0 self._colors[kind] = (0., 0., 0., 0.) self._buffers[kind] = dict(array=gl.GLuint()) gl.glGenBuffers(1, pointer(self._buffers[kind]['array'])) self._buffers['fill']['index'] = gl.GLuint() gl.glGenBuffers(1, pointer(self._buffers['fill']['index'])) gl.glUseProgram(0) self.set_fill_color(fill_color) self.set_line_color(line_color)
def __init__(self, vertex_shader, fragment_shader): self.handle = gl.glCreateProgram() logging.debug('GLProgram.__init__: self.handle = %r', self.handle) gl.glAttachShader(self.handle, shader(gl.GL_VERTEX_SHADER, vertex_shader)) gl.glAttachShader(self.handle, shader(gl.GL_FRAGMENT_SHADER, fragment_shader)) gl.glLinkProgram(self.handle) #call use() early so some programm errors are caught early self.use()
def link_program(program): gl.glLinkProgram(program) length = gl.GLint(0) gl.glGetProgramiv(program, gl.GL_INFO_LOG_LENGTH, ctypes.byref(length)) log_buffer = ctypes.create_string_buffer(length.value) gl.glGetProgramInfoLog(program, length, None, log_buffer) for line in log_buffer.value[:length.value].decode('ascii').splitlines(): print('GLSL: ' + line)
def link_program(program): ''' link a glsl program and print error messages.''' gl.glLinkProgram(program) length = gl.GLint(0) gl.glGetProgramiv(program, gl.GL_INFO_LOG_LENGTH, ctypes.byref(length)) log_buffer = ctypes.create_string_buffer(length.value) gl.glGetProgramInfoLog(program, length, None, log_buffer) for line in log_buffer.value[:length.value].decode('ascii').splitlines(): print('GLSL: ' + line)
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 __init__(self, vertex_shader: Shader, fragment_shader: Shader, attributes=(), uniforms=None): """ The process of creating a shader can be divided into 6 steps: 1. Create an ID. 2. Attach shaders. 3. Bind attributes. 4. Link program. 5. Validate program. 6. Bind uniforms. Args: vertex_shader: fragment_shader: attributes: uniforms: A dictionary with the names of the uniform as the key, and the uniform (glsl value) as value. """ if uniforms is None: uniforms = {} handle = -1 attribute_location = {} uniform_location = {} try: handle = glCreateProgram() glAttachShader(handle, vertex_shader) glAttachShader(handle, fragment_shader) for index, name in enumerate(attributes): glBindAttribLocation(handle, index, c_string(name)) attribute_location[name] = index glLinkProgram(handle) glValidateProgram(handle) for uniform in uniforms: uniform_location[uniform] = glGetUniformLocation( handle, c_string(uniform)) except GLException: debug_program(handle) super(Program, self).__init__(handle) self.vertex_shader = vertex_shader self.fragment_shader = fragment_shader self.attribute_location = attribute_location self.uniform_location = uniform_location self.uniforms = uniforms if CHECK_ERROR: debug_program(self)
def __init__(self, vertex_source, fragment_source): self.program = gl.glCreateProgram() self.vertex_shader = self.create_shader(vertex_source, gl.GL_VERTEX_SHADER) self.fragment_shader = self.create_shader(fragment_source, gl.GL_FRAGMENT_SHADER) gl.glAttachShader(self.program, self.vertex_shader) gl.glAttachShader(self.program, self.fragment_shader) gl.glLinkProgram(self.program) message = self.get_program_log(self.program) if message: raise ShaderException(message)
def link(glo): """Link a shader program""" gl.glLinkProgram(glo) status = c_int() gl.glGetProgramiv(glo, gl.GL_LINK_STATUS, status) if not status.value: length = c_int() gl.glGetProgramiv(glo, gl.GL_INFO_LOG_LENGTH, length) log = c_buffer(length.value) gl.glGetProgramInfoLog(glo, len(log), None, log) raise ShaderException("Program link error: {}".format( log.value.decode()))
def link(self): """link the program""" gl.glLinkProgram(self.id) # Check if linking was successful. If not, print the log. link_status = c_int(0) gl.glGetProgramiv(self.id, gl.GL_LINK_STATUS, byref(link_status)) if not link_status: gl.glGetProgramiv(self.id, gl.GL_INFO_LOG_LENGTH, byref(link_status)) # retrieve the log length buffer = create_string_buffer(link_status.value) # create a buffer for the log gl.glGetProgramInfoLog(self.id, link_status, None, buffer) # retrieve the log text print(buffer.value) # print the log to the console
def compile(self): self.id = gl.glCreateProgram() for shader in self.shaders: shader.compile() gl.glAttachShader(self.id, shader.id) gl.glLinkProgram(self.id) message = self._get_message() if not self.get_link_status(): raise LinkError(message) return message
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) self.detach(*self.shaders(), delete=False ) # always! detach shaders after link successful or not if self.link_status == GL_TRUE: self.uniforms.reload() self.attributes.reload() return True
def link(self): if self._shaders_empty: raise RuntimeError("No shaders attached to program") gl.glLinkProgram(self.gl_id) rc = gl.GLint(0) gl.glGetProgramiv(self.gl_id, gl.GL_LINK_STATUS, ctypes.byref(rc)) if not rc: gl.glGetProgramiv(self.gl_id, gl.GL_INFO_LOG_LENGTH, ctypes.byref(rc)) buffer = ctypes.create_string_buffer(rc.value) gl.glGetProgramInfoLog(self.gl_id, rc, None, buffer) raise GLObjectException(buffer.value.decode()) else: self._done_link = True
def link(self): gl.glLinkProgram(self.__value) err_status = ct.c_int() gl.glGetProgramiv(self.__value, gl.GL_LINK_STATUS, ct.byref(err_status)) if err_status.value != gl.GL_TRUE: log_length = ct.c_int() gl.glGetProgramiv(self.__value, gl.GL_INFO_LOG_LENGTH, ct.byref(log_length)) log_buf = (ct.c_char * (log_length.value))() log_buf_ptr = ct.cast(ct.pointer(log_buf), ct.c_char_p) gl.glGetProgramInfoLog(self.__value, log_length.value, None, log_buf_ptr) raise GLException("Program failed to link:\n%s" % log_buf.value)
def link(self): """link the program""" gl.glLinkProgram(self.id) # Check if linking was successful. If not, print the log. link_status = c_int(0) gl.glGetProgramiv(self.id, gl.GL_LINK_STATUS, byref(link_status)) if not link_status: gl.glGetProgramiv(self.id, gl.GL_INFO_LOG_LENGTH, byref(link_status)) # retrieve the log length buffer = create_string_buffer( link_status.value) # create a buffer for the log gl.glGetProgramInfoLog(self.id, link_status, None, buffer) # retrieve the log text print(buffer.value) # print the log to the console
def __init__(self, vertex, fragment, version=None): self._pid = gl.glCreateProgram() self._vertex_source = preprocess_shader(vertex, 'vert', version) self._fragment_source = preprocess_shader(fragment, 'frag', version) self.compile_vertex_shader() self.compile_fragment_shader() gl.glAttachShader(self._pid, self._vid) gl.glAttachShader(self._pid, self._fid) gl.glLinkProgram(self._pid) self._uniforms = {} self._attributes = {}
def __init__(self, source): self.source = source self.shader_no = gl.glCreateShader(self.shader_type) if not self.shader_no: raise Exception("could not create shader") prog = (c_char_p * 1)(source + chr(0)) length = (c_int * 1)(0) gl.glShaderSource(self.shader_no, 1, cast(prog, POINTER(POINTER(c_char))), cast(0, POINTER(c_int))) gl.glCompileShader(self.shader_no) self.program_no = gl.glCreateProgram() if not self.program_no: raise Exception("could not create program") gl.glAttachShader(self.program_no, self.shader_no) gl.glLinkProgram(self.program_no)
def __init__(self, *shaders: Shader): self.prog_id = prog_id = gl.glCreateProgram() shaders_id = [] for shader_code, shader_type in shaders: shader = compile_shader(shader_code, shader_type) gl.glAttachShader(self.prog_id, shader) shaders_id.append(shader) gl.glLinkProgram(self.prog_id) for shader in shaders_id: # Flag shaders for deletion. Will only be deleted once detached from program. gl.glDeleteShader(shader) self._uniforms: Dict[str, Uniform] = {} self._introspect_uniforms() weakref.finalize(self, Program._delete, shaders_id, prog_id)
def link(self): gl.glLinkProgram(self.handle) # Retrieve the link status status = c_int(0) gl.glGetProgramiv(self.handle, gl.GL_LINK_STATUS, byref(status)) # If linking failed, get log and abort. if not status: #Retrieve the log and pass it up with an exception. gl.glGetProgramiv(self.handle, gl.GL_INFO_LOG_LENGTH, byref(status)) log = create_string_buffer(status.value) gl.glGetProgramInfoLog(self.handle, status, None, log) raise Exception("Linking shaders failed {0}".format(log.value)) else: self.linked = True
def link(self): # link the program glLinkProgram(self.handle) temp = c_int(0) # retrieve the link status glGetProgramiv(self.handle, GL_LINK_STATUS, byref(temp)) # if linking failed, print the log if not temp: # retrieve the log length glGetProgramiv(self.handle, GL_INFO_LOG_LENGTH, byref(temp)) # create a buffer for the log buffer = create_string_buffer(temp.value) # retrieve the log text glGetProgramInfoLog(self.handle, temp, None, buffer) # print the log to the console self.log.error(f"{buffer.value}") else: # all is well, so we are linked self.linked = True
def link(self): """Main steps to link the program: 1. glLinkProgram: linke the shaders in the program to create an executable 2. glGetProgramiv: retrieve the link status 3. glGetProgramInfoLog: print the error log if link failed """ gl.glLinkProgram(self.program) link_status = gl.GLint(0) gl.glGetProgramiv(self.program, gl.GL_LINK_STATUS, ct.byref(link_status)) if not link_status: info_length = gl.GLint(0) gl.glGetProgramiv(self.program, gl.GL_INFO_LOG_LENGTH, ct.byref(info_length)) error_info = ct.create_string_buffer(info_length.value) gl.glGetProgramInfoLog(self.program, info_length, None, error_info) print(error_info.value)
def link(self): """link the program, making it the active shader. .. note:: Shader.bind() is preferred here, because link() Requires the Shader to be compiled already. """ gl.glLinkProgram(self.id) # Check if linking was successful. If not, print the log. link_status = c_int(0) gl.glGetProgramiv(self.id, gl.GL_LINK_STATUS, byref(link_status)) if not link_status: gl.glGetProgramiv(self.id, gl.GL_INFO_LOG_LENGTH, byref(link_status)) # retrieve the log length buffer = create_string_buffer( link_status.value) # create a buffer for the log gl.glGetProgramInfoLog(self.id, link_status, None, buffer) # retrieve the log text print(buffer.value) # print the log to the console self.is_linked = True
def link_program(program_name): ''' link a glsl program and print error messages. ''' gl.glLinkProgram(program_name) success = gl.GLint(0) gl.glGetProgramiv(program_name, gl.GL_LINK_STATUS, ctypes.byref(success)) length = gl.GLint(0) gl.glGetProgramiv(program_name, gl.GL_INFO_LOG_LENGTH, ctypes.byref(length)) log_buffer = ctypes.create_string_buffer(length.value) gl.glGetProgramInfoLog(program_name, length, None, log_buffer) log_message = log_buffer.value[:length.value].decode('ascii').strip() if log_message: sys.stderr.write(log_message + '\n') if not success: raise ValueError('Linking of the shader program failed.')
def link(self): """ Main steps to link the program: 1. glLinkProgram: linke the shaders in the program to create an executable 2. glGetProgramiv: retrieve the link status 3. glGetProgramInfoLog: print the error log if link failed """ gl.glLinkProgram(self.program) link_status = gl.GLint(0) gl.glGetProgramiv(self.program, gl.GL_LINK_STATUS, ct.byref(link_status)) if not link_status: info_length = gl.GLint(0) gl.glGetProgramiv(self.program, gl.GL_INFO_LOG_LENGTH, ct.byref(info_length)) error_info = ct.create_string_buffer(info_length.value) gl.glGetProgramInfoLog(self.program, info_length, None, error_info) print(error_info.value)
def link(self): # link the program glLinkProgram(self.handle) temp = c_int(0) # retrieve the link status glGetProgramiv(self.handle, GL_LINK_STATUS, byref(temp)) # if linking failed, print the log if not temp: # retrieve the log length glGetProgramiv(self.handle, GL_INFO_LOG_LENGTH, byref(temp)) # create a buffer for the log buffer = create_string_buffer(temp.value) # retrieve the log text glGetProgramInfoLog(self.handle, temp, None, buffer) # print the log to the console print(buffer.value) else: # all is well, so we are linked self.linked = True
def __init__(self, vert_path, frag_path): self.program = gl.glCreateProgram() # create vertex shader self.vert_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER) create_shader(self.vert_shader, vert_path) gl.glAttachShader(self.program, self.vert_shader) # create fragment shader self.frag_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) create_shader(self.frag_shader, frag_path) gl.glAttachShader(self.program, self.frag_shader) # link program and clean up gl.glLinkProgram(self.program) gl.glDeleteShader(self.vert_shader) gl.glDeleteShader(self.frag_shader)
def build_program(name): """build_program(name) Loads and compiles the shaders and afterwards link them into a program.""" # Compile the shaders vs = load_shader(name, 'v') fs = load_shader(name, 'f') # Create and link the program program = gl.glCreateProgram() gl.glAttachShader(program, vs) gl.glAttachShader(program, fs) gl.glLinkProgram(program) # If everything is ok -- return the program check_program(program) return program
def __init__(self, *shaders): pid = False try: pid = glCreateProgramObjectARB() except GLException: raise ShaderError("Can't create program object!") for s in shaders: glAttachObjectARB(pid, s.sid) glLinkProgram(pid) v = GLint(0) glGetProgramiv(pid, GL_LINK_STATUS, byref(v)) if not v: log = _get_infolog(pid) e = ShaderError("Error linking program!") e.infolog = log raise e self.pid = pid self.uniform = _UniformAccesser(self) self.attrib = _AttributeAccesser(self)
def _compile_shader(vs_src, fs_src): vs = gl.glCreateShader(gl.GL_VERTEX_SHADER) vs_src_buf = c.create_string_buffer(vs_src) c_vs_src = c.cast(c.pointer(vs_src_buf), c.POINTER(c.c_char)) i = c.c_int32(len(vs_src)) gl.glShaderSource(vs, 1, c_vs_src, c.byref(i)) gl.glCompileShader(vs) fs = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) fs_src_buf = c.create_string_buffer(fs_src) c_fs_src = c.cast(c.pointer(fs_src_buf), c.POINTER(c.c_char)) i = c.c_int32(len(fs_src)) gl.glShaderSource(fs, 1, c_fs_src, c.byref(i)) gl.glCompileShader(fs) program = gl.glCreateProgram() gl.glAttachShader(program, vs) gl.glAttachShader(program, fs) gl.glLinkProgram(program) gl.glDeleteShader(vs) gl.glDeleteShader(fs) return program
def __init__(self, shaders, attributes, uniforms): vertex_shader = shaders[0] fragment_shader = shaders[1] vertex_handle = glCreateShader(GL_VERTEX_SHADER) glShaderSource( vertex_handle, 1, cast(pointer(pointer(create_string_buffer(vertex_shader))), POINTER(POINTER(GLchar))), None) glCompileShader(vertex_handle) fragment_handle = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource( fragment_handle, 1, cast(pointer(pointer(create_string_buffer(fragment_shader))), POINTER(POINTER(GLchar))), None) glCompileShader(fragment_handle) # Create attributes. attribute_mapping = [] for attribute in attributes: attribute_mapping.append(create_string_buffer(attribute)) try: # Create program. program_handle = glCreateProgram() glAttachShader(program_handle, vertex_handle) glAttachShader(program_handle, fragment_handle) for index, name in enumerate(attributes): # CHANGED glBindAttribLocation(program_handle, index, name) glLinkProgram(program_handle) glValidateProgram(program_handle) glUseProgram(program_handle) except GLException: # Print errors. status = GLint() glGetShaderiv(vertex_handle, GL_INFO_LOG_LENGTH, byref(status)) output = create_string_buffer(status.value) glGetShaderInfoLog(vertex_handle, status, None, output) print(output.value.decode('utf-8')) status = GLint() glGetShaderiv(fragment_handle, GL_INFO_LOG_LENGTH, byref(status)) output = create_string_buffer(status.value) glGetShaderInfoLog(fragment_handle, status, None, output) print(output.value.decode('utf-8')) status = GLint() glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, byref( status)) # Getting the number of char in info log to 'status' output = create_string_buffer(status.value) # status.value) glGetProgramInfoLog(program_handle, status, None, output) print(output.value.decode('utf-8')) # # Get uniform location. # uniform_mapping = {} # for uniform in uniforms: # name = create_string_buffer(uniform) # location = glGetUniformLocation(program_handle, cast(pointer(name), POINTER(GLchar))) # uniform_mapping[uniform] = location active_shaders = GLint() glGetProgramiv(program_handle, GL_ACTIVE_UNIFORMS, active_shaders) buffer_size = GLsizei(255) data_type = GLenum(0) string_buffer = create_string_buffer(buffer_size.value) name = c_char_p(addressof(string_buffer)) uniform_mapping = {} for index in range(active_shaders.value): glGetActiveUniform(program_handle, index, buffer_size, None, None, byref(data_type), name) if name.value in uniforms: location = glGetUniformLocation( program_handle, cast(pointer(name), POINTER(GLchar))) uniform = Uniform(name.value, location, data_type.value) uniform_mapping[name.value] = uniform super().__init__(program_handle) self.uniforms = uniform_mapping self.attributes = attribute_mapping
def create(cls, vertex_source, fragment_source, attributes, uniforms): number_of_string = 1 # Create vertex shader. vertex_handle = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vertex_handle, number_of_string, c_pointer_to_char_pointers(vertex_source), None) glCompileShader(vertex_handle) # Create fragment shader. fragment_handle = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fragment_handle, number_of_string, c_pointer_to_char_pointers(fragment_source), None) glCompileShader(fragment_handle) # Create attributes. attribute_mapping = [] for attribute in attributes: attribute_mapping.append(c_string(attribute)) try: # Create program. program_handle = glCreateProgram() glAttachShader(program_handle, vertex_handle) glAttachShader(program_handle, fragment_handle) for index, name in enumerate(attribute_mapping): glBindAttribLocation(program_handle, index, name) glLinkProgram(program_handle) glValidateProgram(program_handle) glUseProgram(program_handle) except GLException as error: # Print vertex shader errors. status = GLint() glGetShaderiv(vertex_handle, GL_INFO_LOG_LENGTH, byref(status)) output = create_string_buffer(status.value) glGetShaderInfoLog(vertex_handle, status, None, output) print(output.value.decode('utf-8')) # Print fragment shader errors. status = GLint() glGetShaderiv(fragment_handle, GL_INFO_LOG_LENGTH, byref(status)) output = create_string_buffer(status.value) glGetShaderInfoLog(fragment_handle, status, None, output) print(output.value.decode('utf-8')) # Print program errors. status = GLint() glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, byref( status)) # Getting the number of char in info log to 'status' output = create_string_buffer(status.value) # status.value) glGetProgramInfoLog(program_handle, status, None, output) print(output.value.decode('utf-8')) raise error # Get uniform location. uniform_mapping = {} for uniform in uniforms: name = c_string(uniform) location = glGetUniformLocation( program_handle, cast(pointer(name), POINTER(c_char))) uniform_mapping[uniform] = location return cls(program_handle, uniform_mapping)
def __init__(self, vertex_shader, fragment_shader): self.handle = gl.glCreateProgram() gl.glAttachShader(self.handle, shader(gl.GL_VERTEX_SHADER, vertex_shader)) gl.glAttachShader(self.handle, shader(gl.GL_FRAGMENT_SHADER, fragment_shader)) gl.glLinkProgram(self.handle) self.use() # early error
gl.glGetShaderInfoLog(shader, len(log), None, log) raise Exception('Error compiling shader: ' + log.value.decode('utf8')) return shader #===== main =====# # window window = pyglet.window.Window(width=640, height=480, vsync=True) # shader program = gl.glCreateProgram() gl.glAttachShader(program, compile_shader(gl.GL_VERTEX_SHADER, vert_shader_src)) gl.glAttachShader(program, compile_shader(gl.GL_FRAGMENT_SHADER, frag_shader_src)) gl.glLinkProgram(program) # uniforms u_origin = gl.glGetUniformLocation(program, b'uOrigin') u_zoom = gl.glGetUniformLocation(program, b'uZoom') origin = [0, 0] zoom = [1, 1] # attributes a_position = gl.glGetAttribLocation(program, b'aPosition') a_color = gl.glGetAttribLocation(program, b'aColor') gl.glEnableVertexAttribArray(a_position) gl.glEnableVertexAttribArray(a_color)
def __init__(self, shaders, attributes, uniforms): # Create vertex shader. vertex_shader = shaders[0] vertex_handle = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vertex_handle, 1, c_string_array(vertex_shader), None) glCompileShader(vertex_handle) # Create fragment shader. fragment_shader = shaders[1] fragment_handle = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fragment_handle, 1, c_string_array(fragment_shader), None) glCompileShader(fragment_handle) try: # Create program. program_handle = glCreateProgram() # Attach shaders glAttachShader(program_handle, vertex_handle) glAttachShader(program_handle, fragment_handle) # Bind attributes. for index, name in enumerate(attributes): glBindAttribLocation(program_handle, index, c_string(name)) # Link, validate and use. glLinkProgram(program_handle) glValidateProgram(program_handle) glUseProgram(program_handle) except GLException: # Print errors. status = GLint() glGetShaderiv(vertex_handle, GL_INFO_LOG_LENGTH, byref(status)) output = create_string_buffer(status.value) glGetShaderInfoLog(vertex_handle, status, None, output) print(output.value.decode('utf-8')) status = GLint() glGetShaderiv(fragment_handle, GL_INFO_LOG_LENGTH, byref(status)) output = create_string_buffer(status.value) glGetShaderInfoLog(fragment_handle, status, None, output) print(output.value.decode('utf-8')) status = GLint() glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, byref( status)) # Getting the number of char in info log to 'status' output = create_string_buffer(status.value) # status.value) glGetProgramInfoLog(program_handle, status, None, output) print(output.value.decode('utf-8')) # Query uniform data. active_uniforms = GLint() glGetProgramiv(program_handle, GL_ACTIVE_UNIFORMS, active_uniforms) buffer_size = GLsizei(255) data_type = GLenum(0) string_buffer = create_string_buffer(buffer_size.value) name = c_char_p(addressof(string_buffer)) uniform_mapping = {} for index in range(active_uniforms.value): glGetActiveUniform(program_handle, index, buffer_size, None, None, byref(data_type), name) if name.value in uniforms: location = glGetUniformLocation( program_handle, cast(pointer(name), POINTER(GLchar))) uniform = Uniform(name.value, location, data_type.value) uniform_mapping[name.value] = uniform self.id = GLuint(program_handle) self.uniforms = uniform_mapping self.attributes = attributes
def program(*shaders): p = gl.glCreateProgram() for s in shaders: gl.glAttachShader(p, s) gl.glLinkProgram(p) return check_shader_or_program_status(p)
def link(self): gl.glLinkProgram(self.program_no) gl.glGetProgramiv(self.program_no, gl.GL_LINK_STATUS, self.link_status) if not self.link_status: raise gl.GLSLException(_infolog(self.program_no, log_type="program"))