def __init__(self, resolution): self.resolution = resolution width, height = resolution shader_program = GL.gl_buffer(GL.GL_INT, 1) GL.glGetIntegerv(GL.GL_CURRENT_PROGRAM, shader_program) self.vertex_array = GL.gl_buffer(GL.GL_INT, 1) GL.glGenVertexArrays(1, self.vertex_array) GL.glBindVertexArray(self.vertex_array[0]) texturecoord_location = GL.glGetAttribLocation(shader_program[0], "texCoord") position_location = GL.glGetAttribLocation(shader_program[0], "pos") GL.glEnableVertexAttribArray(texturecoord_location) GL.glEnableVertexAttribArray(position_location) position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] position = GL.gl_buffer(GL.GL_FLOAT, len(position), position) texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] texcoord = GL.gl_buffer(GL.GL_FLOAT, len(texcoord), texcoord) self.vertex_buffer = GL.gl_buffer(GL.GL_INT, 2) GL.glGenBuffers(2, self.vertex_buffer) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer[0]) GL.glBufferData(GL.GL_ARRAY_BUFFER, 32, position, GL.GL_STATIC_DRAW) GL.glVertexAttribPointer(position_location, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, None) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer[1]) GL.glBufferData(GL.GL_ARRAY_BUFFER, 32, texcoord, GL.GL_STATIC_DRAW) GL.glVertexAttribPointer(texturecoord_location, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, None) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) GL.glBindVertexArray(0)
def view_draw(self, context, depsgraph): profiler = cProfile.Profile() global PROFILE if PROFILE: profiler.enable() if self.request_new_frame: self.profiling_data = io.StringIO() # Get viewport resolution resolution = context.region.width, context.region.height #Save FBO for later use fbo = GL.gl_buffer(GL.GL_INT, 1) GL.glGetIntegerv(GL.GL_FRAMEBUFFER_BINDING, fbo) # Generating a VAO before loading/rendering fixes an error where the viewport # freezes in edit mode. Why ??? I have no idea. ¯\_(ツ)_/¯ # TODO: try to load meshes the normal way, without lazy VAO generation. # (Would need a Pipeline refactor) VAO = GL.gl_buffer(GL.GL_INT, 1) GL.glGenVertexArrays(1, VAO) #render scene = self.get_scene(context, depsgraph, self.request_scene_update) self.request_scene_update = False render_texture = self.get_pipeline().render( resolution, scene, False, self.request_new_frame)['COLOR'] self.request_new_frame = False if MaltMaterial.INITIALIZED == False: #First viewport render can happen before initialization self.request_new_frame = True #Render to viewport self.bind_display_space_shader(depsgraph.scene_eval) if self.display_draw is None or self.display_draw.resolution != resolution: self.display_draw = DisplayDraw(resolution) self.display_draw.draw(fbo, render_texture) self.unbind_display_space_shader() if self.get_pipeline().needs_more_samples(): self.tag_redraw() reset_GL_state() GL.glDeleteVertexArrays(1, VAO) if PROFILE: profiler.disable() stats = pstats.Stats(profiler, stream=self.profiling_data) stats.strip_dirs() stats.sort_stats(pstats.SortKey.CUMULATIVE) stats.print_stats() if self.get_pipeline().needs_more_samples() == False: PROFILE = False with open(REPORT_PATH, 'w') as file: file.write(self.profiling_data.getvalue())