def bind(self, target, index=None): """Binds the buffer to a target, with an optional index for an indexed target. .. warning:: It is not allowed to bind two buffers (or one buffer twice) to the same target simultaneously. """ if index is None: GL.glBindBuffer(target, self.handle) else: GL.glBindBufferBase(target, index, self.handle) self.active_bindings.add(target) yield if index is None: GL.glBindBuffer(target, 0) else: GL.glBindBufferBase(target, index, 0) self.active_bindings.remove(target)
def __init__(self, data): self.data = data self.nbytes = sizeof(data) self.ubo = gl.glGenBuffers(1) self.binding = self.max_binding gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, self.ubo) gl.glBufferData(gl.GL_UNIFORM_BUFFER, self.nbytes, pointer(self.data), gl.GL_STREAM_DRAW) gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, self.binding, self.ubo) UniformBuffer.max_binding += 1
def begin_callback(callback_data): buffer_id = self.data.buffer_meshes.get_buffer_id() if buffer_id and not self.xfb_active: gl.glEnable(gl.GL_RASTERIZER_DISCARD) gl.glBindBufferBase(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffer_id) gl.glBeginTransformFeedback(gl.GL_TRIANGLES) self.xfb_active = True callback_data.upcall()
def __init__(self, data): if isinstance(data, np.ndarray): self.pdata = data self.nbytes = data.nbytes else: self.pdata = pointer(data) self.nbytes = sizeof(data) self.ubo = gl.glGenBuffers(1) self.binding = self.max_binding gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, self.ubo) gl.glBufferData(gl.GL_UNIFORM_BUFFER, self.nbytes, self.pdata, gl.GL_STREAM_DRAW) gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, self.binding, self.ubo) UniformBuffer.max_binding += 1
def display(self): GL.glEnable(GL.GL_CULL_FACE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClearColor(100, 100, 100, 100) #Render G-buffer self.frameBuffer_.bind() GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glUseProgram(self.shaderMesh_) GL.glBindFragDataLocation(self.shaderMesh_, 0, "outputPos") GL.glBindFragDataLocation(self.shaderMesh_, 1, "outputNormal") GL.glBindFragDataLocation(self.shaderMesh_, 2, "outputMat") GL.glBindFragDataLocation(self.shaderMesh_, 3, "outputLight") GL.glUniformMatrix4fv( self.viewMatrixUnif_, 1, GL.GL_TRUE, np.ascontiguousarray(self.viewMat_, dtype=np.float32)) GL.glUniformMatrix4fv( self.projMatrixUnif_, 1, GL.GL_TRUE, np.ascontiguousarray(self.projMat_, dtype=np.float32)) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.envMap_.frameBufferShadows_.get_shadow_map()) GL.glUniform1i(self.textSMUnif_, 0) GL.glUniform3f(self.camPosRenderUnif_, self.camera_.obs_[0], self.camera_.obs_[1], self.camera_.obs_[2]) GL.glUniform1i(self.numVLPUnif_, self.envMap_.numVPL_) GL.glUniform1i(self.numVLPAxisUnif_, self.envMap_.numPtsAxis_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 0, self.envMap_.vlpPosSSBO_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 1, self.envMap_.vlpIntSSBO_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 2, self.envMap_.vlpViewMatSSBO_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 3, self.envMap_.vlpProjMatSSBO_) if self.useFloor_: GL.glUniform3f(self.objColorUnif_, self.planeColor_[0], self.planeColor_[1], self.planeColor_[2]) self.floor_.render_mesh() GL.glUniform3f(self.objColorUnif_, self.objColor_[0], self.objColor_[1], self.objColor_[2]) self.mesh_.render_mesh() GL.glUseProgram(0) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) #Compute NN screenproc.computeAOTexture(self.gridSize_) #Render result. GL.glDisable(GL.GL_CULL_FACE) GL.glDisable(GL.GL_DEPTH_TEST) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) attachments = [GL.GL_COLOR_ATTACHMENT0] GL.glDrawBuffers(1, attachments) GL.glUseProgram(self.shaderRender_) GL.glBindFragDataLocation(self.shaderRender_, 0, "outputColor") GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.outTexture_) GL.glUniform1i(self.textUnif_, 0) GL.glActiveTexture(GL.GL_TEXTURE1) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(0)) GL.glUniform1i(self.textPosUnif_, 1) GL.glActiveTexture(GL.GL_TEXTURE2) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(1)) GL.glUniform1i(self.textNormalUnif_, 2) GL.glActiveTexture(GL.GL_TEXTURE3) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(2)) GL.glUniform1i(self.textMattUnif_, 3) GL.glActiveTexture(GL.GL_TEXTURE4) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(3)) GL.glUniform1i(self.textLightUnif_, 4) GL.glActiveTexture(GL.GL_TEXTURE0) if self.lighting_: GL.glUniform1f(self.directLightRenderUnif_, 0.0) else: GL.glUniform1f(self.directLightRenderUnif_, 1.0) if self.gi_: if self.sss_: GL.glUniform2f(self.giRenderUnif_, 1.0, 1.0) else: GL.glUniform2f(self.giRenderUnif_, 1.0, 0.0) else: GL.glUniform2f(self.giRenderUnif_, 0.0, 0.0) self.quad_.render_mesh() GL.glUseProgram(0) GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
def end_callback(callback_data): if self.xfb_active: gl.glEndTransformFeedback() gl.glBindBufferBase(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0) gl.glDisable(gl.GL_RASTERIZER_DISCARD) self.xfb_active = False callback_data.upcall()
def get_ub(listy, record_byte_size): ubo, binding_point_index = gen_ub() gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, binding_point_index, ubo) gl.glBufferData(gl.GL_UNIFORM_BUFFER, record_byte_size*len(listy), listy, gl.GL_DYNAMIC_DRAW) gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, 0) return ubo, binding_point_index
def cleanup(self): on_cleanup = self.config.get('on_cleanup') if on_cleanup: gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, self.binding, self.ssbo) data = gl.glGetBufferSubData(gl.GL_SHADER_STORAGE_BUFFER, 0, self.size) # uint8[size] exec(on_cleanup, dict(DATA=data)) gl.glBindBuffer(gl.GL_SHADER_STORAGE_BUFFER, 0) gl.glDeleteBuffers(1, self.ssbo)
def _update_region_buffer(self, target: RenderTarget): world_data_size = self._view.world.r_count * REGION_SIZE gl.glBindBuffer(gl.GL_SHADER_STORAGE_BUFFER, target.world_ssbo) gl.glBufferData(gl.GL_SHADER_STORAGE_BUFFER, world_data_size, data=None, usage=gl.GL_DYNAMIC_DRAW) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, WORLD_SSBO_BINDING, target.world_ssbo) asyncjob.submit( asyncjob.AsyncJobParameters(self._view.world.raw_regions(), self._view.world.r_count, self._update_region_sub_data, name="LoadWorld", batch_size=256))
def paintGL(self): # clear the buffer gl.glClear(gl.GL_COLOR_BUFFER_BIT) # Run the compute phase gl.glUseProgram(self.compute_program) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 0, self.vbo) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 1, self.ssbo) loc = gl.glGetUniformLocation(self.compute_program, 'N') gl.glUniform1ui(loc, self.count) loc = gl.glGetUniformLocation(self.compute_program, 'dt') gl.glUniform1f(loc, DT) loc = gl.glGetUniformLocation(self.compute_program, 'damping') gl.glUniform1f(loc, DAMPING) loc = gl.glGetUniformLocation(self.compute_program, 'rm') gl.glUniform1f(loc, RM) gl.glDispatchCompute(self.count, 1, 1) gl.glMemoryBarrier(gl.GL_SHADER_STORAGE_BARRIER_BIT) # Read back the modified data. #gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER,10, self.ssbo) #gl.glGetBufferSubData(gl.GL_SHADER_STORAGE_BUFFER, 0, # self.velocities.nbytes, self.velocities) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) # bind the VBO gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo) # tell OpenGL that the VBO contains an array of vertices # these vertices contain 4 single precision coordinates gl.glVertexPointer(4, gl.GL_FLOAT, 0, None) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.ssbo) gl.glVertexAttribPointer(1, 4, gl.GL_FLOAT, gl.GL_FALSE, 0, None) gl.glEnableVertexAttribArray(1); # Use our pipeline. gl.glUseProgram(self.render_program) loc = gl.glGetUniformLocation(self.render_program, 'aspect') gl.glUniform1f(loc, float(self.width) / self.height) # draw "count" points from the VBO gl.glDrawArrays(gl.GL_POINTS, 0, self.count)
def bind_ubo(self, ubo): gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, self._index, ubo._handle)
def on_begin_draw(self): gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, self.binding, self.ssbo)
def on_begin_draw(self): for binding, ssbo in zip(self.bindings, self.ssbos): gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, binding, ssbo)
def createubo(cls, size): ubo = cls(size, gl.GL_UNIFORM_BUFFER, gl.GL_STREAM_DRAW) ubo.binding = GLBuffer.ubo_max_binding GLBuffer.ubo_max_binding += 1 gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, ubo.binding, ubo.buf_id) return ubo
def bind_storage_buffer(ssbo, location): gl.glBindBuffer(gl.GL_SHADER_STORAGE_BUFFER, ssbo) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, location, ssbo)
def draw_callback(callback_data): buffer_id = self.buffer.get_buffer_id() gl.glBindBufferBase(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffer_id) gl.glDrawTransformFeedback(gl.GL_TRIANGLES, 0) callback_data.upcall()
def end(callback_data): if self.xfb_active: gl.glEndTransformFeedback() gl.glBindBufferBase(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0) gl.glDisable(gl.GL_RASTERIZER_DISCARD) self.xfb_active = False callback_data.upcall()
def initializeGL(self): """Initialize OpenGL, VBOs, upload data on the GPU, etc.""" # background color gl.glClearColor(0.8, 0.8, 0.8, 0) # Make initial data array. # compile the vertex shader vs = compile_shader(VERTEX, gl.GL_VERTEX_SHADER) # compile the geometry shader gs = compile_shader(GEOMETRY, gl.GL_GEOMETRY_SHADER) # compile the fragment shader fs = compile_shader(FRAGMENT, gl.GL_FRAGMENT_SHADER) # Link the programs. self.render_program = link_shaders(vs, gs, fs) # Compile the compute shader cs = compile_shader(COMPUTE, gl.GL_COMPUTE_SHADER) # Create the compute shader buffers. self.makeBuffers() #self.vbo = glvbo.VBO(self.attributes) self.vbo = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo) gl.glBufferData(gl.GL_ARRAY_BUFFER, self.attributes.nbytes, self.attributes, gl.GL_DYNAMIC_COPY) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) self.ssbo = gl.glGenBuffers(1) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 1, self.ssbo) gl.glBufferData(gl.GL_SHADER_STORAGE_BUFFER, self.velocities.nbytes, self.velocities, gl.GL_DYNAMIC_COPY) self.compute_program = link_shaders(cs)
def begin(callback_data): buffer_id = self.buffer.get_buffer_id() if buffer_id and not self.xfb_active: gl.glEnable(gl.GL_RASTERIZER_DISCARD) gl.glBindBufferBase(gl.GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffer_id) gl.glBeginTransformFeedback(gl.GL_TRIANGLES) self.xfb_active = True callback_data.upcall()
def paintGL(self): # clear the buffer gl.glClear(gl.GL_COLOR_BUFFER_BIT) # Run the compute phase i = 0 while (i * MAX_WORKGROUPS < self.count): upper = min(self.count, (i+1) * MAX_WORKGROUPS) data = self.valueBuffer[i*MAX_WORKGROUPS:upper] status = self.statusBuffer[i*MAX_WORKGROUPS:upper] gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 0, self.ssbos['data']) gl.glBufferData(gl.GL_SHADER_STORAGE_BUFFER, data.nbytes, data, gl.GL_DYNAMIC_COPY) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 1, self.ssbos['status']) gl.glBufferData(gl.GL_SHADER_STORAGE_BUFFER, status.nbytes, status, gl.GL_DYNAMIC_COPY) gl.glUseProgram(self.compute_program) gl.glDispatchCompute(min(MAX_WORKGROUPS, self.count - i * MAX_WORKGROUPS), 1, 1) gl.glMemoryBarrier(gl.GL_SHADER_STORAGE_BARRIER_BIT) # Read back the modified data. gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 0, self.ssbos['data']) gl.glGetBufferSubData(gl.GL_SHADER_STORAGE_BUFFER, 0, data.nbytes, self.valueBuffer[i*MAX_WORKGROUPS]) gl.glBindBufferBase(gl.GL_SHADER_STORAGE_BUFFER, 1, self.ssbos['status']) gl.glGetBufferSubData(gl.GL_SHADER_STORAGE_BUFFER, 0, status.nbytes, self.statusBuffer[i*MAX_WORKGROUPS]) i += 1 self.iterations += ITER_PER_CYCLE # bind the VBO self.vbo.bind() # tell OpenGL that the VBO contains an array of vertices gl.glEnableClientState(gl.GL_VERTEX_ARRAY) # these vertices contain 2 single precision coordinates gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.vbo) # Bind statuses gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.ssbos['status']) gl.glBufferData(gl.GL_ARRAY_BUFFER, self.statusBuffer.nbytes, self.statusBuffer, gl.GL_DYNAMIC_COPY) gl.glVertexAttribIPointer(1, 2, gl.GL_INT, 0, None) gl.glEnableVertexAttribArray(1); # Use our pipeline. gl.glUseProgram(self.render_program) # draw "count" points from the VBO gl.glDrawArrays(gl.GL_POINTS, 0, self.count) # Update the window title. self.parent().setWindowTitle("Iterations: %d; zoom; %f" % (self.iterations, self.zoom))
def BindToUniform(self, index: int): GL.glBindBufferBase(GL.GL_UNIFORM_BUFFER, index, self._id)