def link(self): if self.vertex_shader is not None: self.vertex_shader.compileFlat() if self.fragment_shader is not None: self.fragment_shader.compileFlat() self.program = gl.glCreateProgramObjectARB() if self.program == 0: raise GLSLException('failed to create program object') if self.vertex_shader is not None: self.vertex_shader.attachFlat(self.program) if self.fragment_shader is not None: self.fragment_shader.attachFlat(self.program) gl.glLinkProgramARB(self.program) link_status = c_int(0) gl.glGetObjectParameterivARB(self.program, gl.GL_OBJECT_LINK_STATUS_ARB, byref(link_status)) if link_status.value == 0: err = glsl_log(self.program) gl.glDeleteObjectARB(self.program) self.program = 0 raise GLSLException('failed to link shader', err) self.__class__._uloc_ = {} self.__class__._vloc_ = {} return self.program
def compileProgram(vertexSource=None, fragmentSource=None): """Create and compile a vertex and fragment shader pair from their sources (strings) """ def compileShader( source, shaderType ): """Compile shader source of given type (only needed by compileProgram)""" shader = GL.glCreateShaderObjectARB(shaderType) prog = c_char_p(source) length = c_int(-1) GL.glShaderSourceARB(shader, 1, cast(byref(prog), POINTER(POINTER(c_char))), byref(length)) GL.glCompileShaderARB(shader) #check for errors status = c_int() GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS, byref(status)) if not status.value: print_log(shader) GL.glDeleteShader(shader) raise ValueError, 'Shader compilation failed' return shader program = GL.glCreateProgramObjectARB() if vertexSource: vertexShader = compileShader( vertexSource, GL.GL_VERTEX_SHADER_ARB ) GL.glAttachObjectARB(program, vertexShader) if fragmentSource: fragmentShader = compileShader( fragmentSource, GL.GL_FRAGMENT_SHADER_ARB ) GL.glAttachObjectARB(program, fragmentShader) GL.glValidateProgramARB( program ) GL.glLinkProgramARB(program) if vertexShader: GL.glDeleteObjectARB(vertexShader) if fragmentShader: GL.glDeleteObjectARB(fragmentShader) return program
def __init__(self, vertexSource=None, fragmentSource=None): def compileShader(source, shaderType): """Compile shader source of given type (only needed by compileProgram) """ shader = GL.glCreateShaderObjectARB(shaderType) # if Py3 then we need to convert our (unicode) str into bytes for C if type(source) != bytes: source = source.encode() prog = c_char_p(source) length = c_int(-1) GL.glShaderSourceARB(shader, 1, cast(byref(prog), POINTER(POINTER(c_char))), byref(length)) GL.glCompileShaderARB(shader) # check for errors status = c_int() GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS, byref(status)) if not status.value: print_log(shader) GL.glDeleteShader(shader) raise ValueError('Shader compilation failed') return shader self.handle = GL.glCreateProgramObjectARB() if vertexSource: vertexShader = compileShader( vertexSource, GL.GL_VERTEX_SHADER_ARB ) GL.glAttachObjectARB(self.handle, vertexShader) if fragmentSource: fragmentShader = compileShader( fragmentSource, GL.GL_FRAGMENT_SHADER_ARB ) GL.glAttachObjectARB(self.handle, fragmentShader) GL.glValidateProgramARB(self.handle) GL.glLinkProgramARB(self.handle) if vertexShader: GL.glDeleteObjectARB(vertexShader) if fragmentShader: GL.glDeleteObjectARB(fragmentShader)
def compileProgram(vertexSource=None, fragmentSource=None): """Create and compile a vertex and fragment shader pair from their sources (strings) """ def compileShader(source, shaderType): """Compile shader source of given type (only needed by compileProgram)""" shader = GL.glCreateShaderObjectARB(shaderType) prog = c_char_p(source) length = c_int(-1) GL.glShaderSourceARB(shader, 1, cast(byref(prog), POINTER(POINTER(c_char))), byref(length)) GL.glCompileShaderARB(shader) #check for errors status = c_int() GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS, byref(status)) if not status.value: print_log(shader) GL.glDeleteShader(shader) raise ValueError, 'Shader compilation failed' return shader program = GL.glCreateProgramObjectARB() if vertexSource: vertexShader = compileShader(vertexSource, GL.GL_VERTEX_SHADER_ARB) GL.glAttachObjectARB(program, vertexShader) if fragmentSource: fragmentShader = compileShader(fragmentSource, GL.GL_FRAGMENT_SHADER_ARB) GL.glAttachObjectARB(program, fragmentShader) GL.glValidateProgramARB(program) GL.glLinkProgramARB(program) if vertexShader: GL.glDeleteObjectARB(vertexShader) if fragmentShader: GL.glDeleteObjectARB(fragmentShader) 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 compileProgram(vertex=None, fragment=None, attachments=[]): """Create and compile a vertex and fragment shader pair from their sources (strings) Usage:: prog = compileProgram(vertex, fragment, attachments=[]) where: - vertex is the main vertex shader source (or ShaderCode object) - fragment is the main fragment shader source (or ShaderCode object) - attachments is a list of ShaderCode objects, with valid shaderType set """ #Derived from shader compilation code in PsychoPy 1.65.02, with permission # from JWP to reuse under more permissive license def compileShader( source, shaderType ): """Compile shader source of given type (only needed by compileProgram)""" shader = gl.glCreateShaderObjectARB(shaderType) #were we given a source string or a ShaderCode object? if hasattr(source, 'src'): source = source.src prog = c_char_p(source) length = c_int(-1) gl.glShaderSourceARB(shader, 1, cast(byref(prog), POINTER(POINTER(c_char))), byref(length)) gl.glCompileShaderARB(shader) #check for errors status = c_int() gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS, byref(status)) if not status.value: # retrieve the log length gl.glGetShaderiv(shader, gl.GL_INFO_LOG_LENGTH, byref(status)) # create a buffer for the log buffer = create_string_buffer(status.value)#from ctypes # retrieve the log text gl.glGetProgramInfoLog(shader, status, None, buffer) # print the log to the console print buffer.value gl.glDeleteShader(shader) raise ValueError, 'Shader compilation failed' return shader program = gl.glCreateProgramObjectARB() #compile attachments before main vertex and frag progs for thisAttachment in attachments: attachment = compileShader(thisAttachment, thisAttachment.type) gl.glAttachObjectARB(program, attachment) if vertex: vertexShader = compileShader(vertex, VERT) gl.glAttachObjectARB(program, vertexShader) if fragment: fragmentShader = compileShader(fragment, FRAG) gl.glAttachObjectARB(program, fragmentShader) #compile the overall program gl.glValidateProgramARB( program ) gl.glLinkProgramARB(program) #cleanup if vertex: gl.glDeleteObjectARB(vertexShader) if fragment: gl.glDeleteObjectARB(fragmentShader) return program
def compileProgram(vertex=None, fragment=None, attachments=[]): """Create and compile a vertex and fragment shader pair from their sources (strings) Usage:: prog = compileProgram(vertex, fragment, attachments=[]) where: - vertex is the main vertex shader source (or ShaderCode object) - fragment is the main fragment shader source (or ShaderCode object) - attachments is a list of ShaderCode objects, with valid shaderType set """ #Derived from shader compilation code in PsychoPy 1.65.02, with permission # from JWP to reuse under more permissive license def compileShader(source, shaderType): """Compile shader source of given type (only needed by compileProgram)""" shader = gl.glCreateShaderObjectARB(shaderType) #were we given a source string or a ShaderCode object? if hasattr(source, 'src'): source = source.src prog = c_char_p(source) length = c_int(-1) gl.glShaderSourceARB(shader, 1, cast(byref(prog), POINTER(POINTER(c_char))), byref(length)) gl.glCompileShaderARB(shader) #check for errors status = c_int() gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS, byref(status)) if not status.value: # retrieve the log length gl.glGetShaderiv(shader, gl.GL_INFO_LOG_LENGTH, byref(status)) # create a buffer for the log buffer = create_string_buffer(status.value) #from ctypes # retrieve the log text gl.glGetProgramInfoLog(shader, status, None, buffer) # print the log to the console print buffer.value gl.glDeleteShader(shader) raise ValueError, 'Shader compilation failed' return shader program = gl.glCreateProgramObjectARB() #compile attachments before main vertex and frag progs for thisAttachment in attachments: attachment = compileShader(thisAttachment, thisAttachment.type) gl.glAttachObjectARB(program, attachment) if vertex: vertexShader = compileShader(vertex, VERT) gl.glAttachObjectARB(program, vertexShader) if fragment: fragmentShader = compileShader(fragment, FRAG) gl.glAttachObjectARB(program, fragmentShader) #compile the overall program gl.glValidateProgramARB(program) gl.glLinkProgramARB(program) #cleanup if vertex: gl.glDeleteObjectARB(vertexShader) if fragment: gl.glDeleteObjectARB(fragmentShader) return program