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 def bind_display_shader(): self.bind_display_space_shader(depsgraph.scene_eval) if self.display_draw is None or self.display_draw.resolution != resolution: self.display_draw = DisplayDraw(bind_display_shader, resolution) #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.load_scene(context, depsgraph) 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.display_draw.draw(bind_display_shader, fbo, render_texture) if self.get_pipeline().needs_more_samples(): self.tag_redraw() 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())
def __init__(self, bind_display_shader, resolution): # Generate dummy float image buffer self.resolution = resolution width, height = resolution bind_display_shader() 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): # Get viewport resolution resolution = context.region.width, context.region.height def bind_display_shader(): self.bind_display_space_shader(depsgraph.scene_eval) if self.display_draw is None or self.display_draw.resolution != resolution: self.display_draw = DisplayDraw(bind_display_shader, resolution) #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.load_scene(context, depsgraph) 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.display_draw.draw(bind_display_shader, fbo, render_texture) if self.get_pipeline().needs_more_samples(): self.tag_redraw() GL.glDeleteVertexArrays(1, VAO)