def _deactivate(self): """Deactivate the program.""" gl.glUseProgram(0) for uniform in self._uniforms.values(): uniform.deactivate() for attribute in self._attributes.values(): attribute.deactivate() log.debug("GPU: Deactivating program (id=%d)" % self._id)
def _deactivate(self): """Deactivate the program.""" gl.glUseProgram(0) for uniform in self._uniforms.values(): uniform.deactivate() # Need fix when dealing with vertex arrays (only need to active the array) for attribute in self._attributes.values(): attribute.deactivate() log.debug("GPU: Deactivating program (id=%d)" % self._id)
def _activate(self): """Activate the program as part of current rendering state.""" log.debug("GPU: Activating program (id=%d)" % self._id) gl.glUseProgram(self.handle) for uniform in self._uniforms.values(): if uniform.active: uniform.activate() for attribute in self._attributes.values(): if attribute.active: attribute.activate()
def _activate(self): """Activate the program as part of current rendering state.""" log.debug("GPU: Activating program (id=%d)" % self._id) gl.glUseProgram(self.handle) for uniform in self._uniforms.values(): if uniform.active: uniform.activate() # Need fix when dealing with vertex arrays (only need to active the array) for attribute in self._attributes.values(): if attribute.active: attribute.activate()
def render(self, draw_data): # perf: local for faster access io = self.io display_width, display_height = io.display_size fb_width = int(display_width * io.display_fb_scale[0]) fb_height = int(display_height * io.display_fb_scale[1]) if fb_width == 0 or fb_height == 0: return draw_data.scale_clip_rects(*io.display_fb_scale) # backup GL state # todo: provide cleaner version of this backup-restore code last_program = gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM) last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D) last_active_texture = gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE) last_array_buffer = gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING) last_element_array_buffer = gl.glGetIntegerv( gl.GL_ELEMENT_ARRAY_BUFFER_BINDING) last_vertex_array = gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING) last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC) last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST) last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB) last_blend_equation_alpha = gl.glGetIntegerv( gl.GL_BLEND_EQUATION_ALPHA) last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX) last_enable_blend = gl.glIsEnabled(gl.GL_BLEND) last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE) last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST) last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendEquation(gl.GL_FUNC_ADD) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDisable(gl.GL_CULL_FACE) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_SCISSOR_TEST) #gl.glActiveTexture(gl.GL_TEXTURE0) gl.glViewport(0, 0, int(fb_width), int(fb_height)) ortho_projection = (ctypes.c_float * 16)(2.0 / display_width, 0.0, 0.0, 0.0, 0.0, 2.0 / -display_height, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0) self.prog["ProjMtx"] = ortho_projection for commands in draw_data.commands_lists: idx_buffer_offset = 0 array_type = c_ubyte * commands.vtx_buffer_size * imgui.VERTEX_SIZE data_carray = array_type.from_address(commands.vtx_buffer_data) if not ( imgui.VERTEX_BUFFER_POS_OFFSET == 0 and \ imgui.VERTEX_BUFFER_UV_OFFSET == 8 and \ imgui.VERTEX_BUFFER_COL_OFFSET == 16 ): log.error( "GlumpyRenderer.render(): imgui vertex buffer layout has changed ! notify the developers ..." ) return #TODO: this is a bit convoluted; Imgui delivers uint8 colors, but glumpy wants float32's dtype = [('Position', np.float32, 2), ('UV', np.float32, 2), ('Color', np.uint8, 4)] vao_content = np.frombuffer(data_carray, dtype=dtype) dtype2 = [('Position', np.float32, 2), ('UV', np.float32, 2), ('Color', np.float32, 4)] vao_content_f = np.zeros(vao_content.shape, dtype=dtype2) for i, val in enumerate(vao_content): vao_content_f[i] = vao_content[i] vao_content_f[i]['Color'] /= 255 v_array = vao_content_f.view(gloo.VertexArray) self.prog.bind(v_array) if imgui.INDEX_SIZE == 1: dtype = np.uint8 if imgui.INDEX_SIZE == 2: dtype = np.uint16 if imgui.INDEX_SIZE == 4: dtype = np.uint32 # gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._elements_handle) # # todo: check this (sizes) # gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, commands.idx_buffer_size * imgui.INDEX_SIZE, ctypes.c_void_p(commands.idx_buffer_data), gl.GL_STREAM_DRAW) array_type = c_ubyte * commands.idx_buffer_size * imgui.INDEX_SIZE data_carray = array_type.from_address(commands.idx_buffer_data) idx_content = np.frombuffer(data_carray, dtype=dtype) for command in commands.commands: # TODO: ImGui Images will not work yet, homogenizing texture id # allocation by imgui/glumpy is likely a larger issue # #accessing command.texture_id crashes the prog # #gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id ) x, y, z, w = command.clip_rect gl.glScissor(int(x), int(fb_height - w), int(z - x), int(w - y)) idx_array = idx_content[idx_buffer_offset:( idx_buffer_offset + command.elem_count)].view( gloo.IndexBuffer) self.prog.draw(mode=gl.GL_TRIANGLES, indices=idx_array) idx_buffer_offset += command.elem_count # restore modified GL state gl.glUseProgram(last_program) gl.glActiveTexture(last_active_texture) gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture) gl.glBindVertexArray(last_vertex_array) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer) gl.glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha) gl.glBlendFunc(last_blend_src, last_blend_dst) if last_enable_blend: gl.glEnable(gl.GL_BLEND) else: gl.glDisable(gl.GL_BLEND) if last_enable_cull_face: gl.glEnable(gl.GL_CULL_FACE) else: gl.glDisable(gl.GL_CULL_FACE) if last_enable_depth_test: gl.glEnable(gl.GL_DEPTH_TEST) else: gl.glDisable(gl.GL_DEPTH_TEST) if last_enable_scissor_test: gl.glEnable(gl.GL_SCISSOR_TEST) else: gl.glDisable(gl.GL_SCISSOR_TEST) gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2], last_viewport[3]) gl.glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2], last_scissor_box[3]) log.debug("----------------------end---------------------------------")