def loadCompute(self, source): """ Loads a compute shader """ content = self._handleIncludes(source) result = Shader.makeCompute(Shader.SLGLSL, content) self._writeDebugShader("Compute-" + str(source), content) self._clearIncludeStack() return result
def __Call(self, func_name, **kwargs): """Receive python call and redirect request to relevant function in image shader library; return modified image.""" # Copy self.__Lines (need to keep orig for further calls) and # add "#define" statement to top to trigger compilation of # relevant "#ifdef/def" function block in shader. lines = list(self.__LINES) lines.insert(2, "#define {}".format(func_name)) # Assemble lines into shader str and compile. shader_str = "".join(lines) self.__NP.setShader(Shader.makeCompute(Shader.SL_GLSL, shader_str)) # Set block size from workgroup size. block_x = int(self.x_size/self.workgroup_size.x) block_y = int(self.y_size/self.workgroup_size.y) block_z = int(self.z_size/self.workgroup_size.z) block_size = LVector3i(block_x,block_y,block_z) # Create mod_tex for GPU. with TimeIt() as prep_timer: mod_img = PNMImage(self.x_size, self.y_size, 4) mod_tex = Texture() mod_tex.load(mod_img) mod_tex.setMinfilter(Texture.FTLinear) mod_tex.setFormat(self.img_format) self.prepare_time += prep_timer.total_time # Pass textures to shader. self.__NP.setShaderInput("ref_tex", self.__ref_tex) self.__NP.setShaderInput("mod_tex", mod_tex) # Set any additional required inputs for this function. for input_name, input_val in list(kwargs.items()): if type(input_val) == PNMImage: input_val = self.__get_Texture(input_val) self.__NP.setShaderInput(input_name, input_val) # Call function in shader library. shader_attrib = self.__NP.getAttrib(ShaderAttrib) with TimeIt() as proc_timer: base.graphicsEngine.dispatch_compute(block_size, shader_attrib, self.__gsg) self.process_time += proc_timer.total_time # Extract modified texture from GPU. with TimeIt() as extract_timer: base.graphicsEngine.extractTextureData(mod_tex, self.__gsg) self.extract_time += extract_timer.total_time return mod_tex
def _createClearShader(self): """ Internal method to create the compute shader which will clear the texture """ shader = """ #version 150 #extension GL_ARB_compute_shader : enable #extension GL_ARB_shader_image_load_store : enable layout (local_size_x = 16, local_size_y = 16) in; uniform int layers; uniform writeonly image2D destination; uniform vec4 clearColor; void main() { ivec2 coord = ivec2(gl_GlobalInvocationID.xy); imageStore(destination, coord, clearColor); } """ return Shader.makeCompute(Shader.SLGLSL, shader)