Пример #1
0
    def updateTracersCS(self):
        """ Update tracers position using the compute shader provided by
            the graphic card for computing.
        """
        self.updateProgram.bind()
        self.updateProgram.setUniforms((
            ('u_field', 0),
            ('u_tracers', 1),
            ('periodic', self.periodic),
            ('u_rand_seed', np.random.random()),
            ('u_speed_factor', self.speedFactor),
            ('u_decay', self.decay),
            ('u_decay_boost', self.decayBoost),
            ('fieldScaling', self.fieldScaling),
            ('u_fieldRes', self._fieldAsRGB.shape),
        ))

        gl.glBindImageTexture(0, self._currentTracersPos._handle, 0,
                              gl.GL_FALSE, 0, gl.GL_READ_ONLY, gl.GL_RGBA8)
        gl.glBindImageTexture(1, self._nextTracersPos._handle, 0, gl.GL_FALSE,
                              0, gl.GL_WRITE_ONLY, gl.GL_RGBA8)

        gl.glDispatchCompute(int(self.tracersRes / WORKGROUP_SIZE),
                             int(self.tracersRes / WORKGROUP_SIZE), 1)
        ## Lock memory access until image process ends
        gl.glMemoryBarrier(gl.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)
        self._currentTracersPos = self._nextTracersPos

        self.resetRenderingTarget()
Пример #2
0
def main():
    if not glfw.init():
        raise ValueError("Failed to initialize glfw")

    glfw.window_hint(glfw.CONTEXT_CREATION_API, glfw.NATIVE_CONTEXT_API)
    glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API)
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True)
    glfw.window_hint(glfw.RESIZABLE, True)
    glfw.window_hint(glfw.DOUBLEBUFFER, True)
    glfw.window_hint(glfw.DEPTH_BITS, 24)
    glfw.window_hint(glfw.SAMPLES, 4)

    window = glfw.create_window(SCR_WIDTH, SCR_HEIGHT, "Python Compute Shader Demo", None, None)
    if not window:
        glfw.terminate()
        raise ValueError("Failed to create window")

    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_event_callback)
    glfw.set_cursor_pos_callback(window, mouse_event_callback)
    glfw.set_mouse_button_callback(window, mouse_button_callback)
    glfw.set_window_size_callback(window, window_resize_callback)

    CSG = shader.ComputeShader(RES("splat.glsl"))
    SplatProgram = shader.Shader(RES("splat.vert"), RES("splat.frag"))

    some_UAV = gl.glGenTextures(1)
    gl.glActiveTexture(gl.GL_TEXTURE0)
    gl.glBindTexture(gl.GL_TEXTURE_2D, some_UAV)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, SCR_WIDTH, SCR_HEIGHT, 0, gl.GL_RGBA, gl.GL_FLOAT, None)
    gl.glBindImageTexture(0, some_UAV, 0, gl.GL_FALSE, 0, gl.GL_READ_WRITE, gl.GL_RGBA32F)

    vao = gl.glGenVertexArrays(1)
    gl.glBindVertexArray(vao)

    gl.glDisable(gl.GL_CULL_FACE)
    gl.glDisable(gl.GL_DEPTH_TEST)

    while not glfw.window_should_close(window):
        CSG.use()
        gl.glDispatchCompute(SCR_WIDTH // 8, SCR_HEIGHT // 8, 1)
        gl.glMemoryBarrier(gl.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)

        SplatProgram.use()
        SplatProgram.set_int("Whatever", 0)
        gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3)

        glfw.swap_buffers(window)
        glfw.poll_events()

    glfw.terminate()
Пример #3
0
 def bind(self, args):
     gl.glBindVertexArray(self.gl_vao)
     gl.glUseProgram(self.gl_program)
     gl.glBindImageTexture(
         0,
         self.gl_return_texture,
         0,
         gl.GL_FALSE,
         0,
         gl.GL_WRITE_ONLY,
         TYPE_MAP[self.function_data.return_type]['buffer_type']
     )
     gl.glUniform1i(0, 0)
     for i, (arg, value) in enumerate(zip(self.function_data.args, args)):
         if not isinstance(value, collections.abc.Iterable):
             value = (value,)
         TYPE_MAP[arg.type]['uniform'](i + 1, *value)
Пример #4
0
def main():
    global delta_time, last_frame, frames

    if not glfw.init():
        raise ValueError("Failed to initialize glfw")

    glfw.window_hint(glfw.CONTEXT_CREATION_API, glfw.NATIVE_CONTEXT_API)
    glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API)
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True)
    glfw.window_hint(glfw.DOUBLEBUFFER, True)
    glfw.window_hint(glfw.DEPTH_BITS, 24)
    glfw.window_hint(glfw.SAMPLES, 4)

    window = glfw.create_window(
        SCR_WIDTH, SCR_HEIGHT, "Python Compute Shader Demo: Pathtracer", None, None
    )
    if not window:
        glfw.terminate()
        raise ValueError("Failed to create window")

    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)
    glfw.set_scroll_callback(window, scroll_callback)

    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    # -- setup shaders
    compute_program = shader.ComputeShader(RES(f"tracer_cs.glsl"))
    quad_program = shader.Shader(RES("tracer_vs.glsl"), RES("tracer_fs.glsl"))

    # -- create vao with full screen quad
    quad_verts = [
        -1, -1,
         1, -1,
         1,  1,
         1,  1,
        -1,  1,
        -1, -1,
    ]
    quad_verts = (c_byte * len(quad_verts))(*quad_verts)

    vao = gl.glGenVertexArrays(1)
    vbo = gl.glGenBuffers(1)

    gl.glBindVertexArray(vao)
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
    gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(quad_verts), quad_verts, gl.GL_STATIC_DRAW)
    gl.glVertexAttribPointer(0, 2, gl.GL_BYTE, gl.GL_FALSE, 2 * sizeof(c_byte), c_void_p(0))
    gl.glEnableVertexAttribArray(0)
    gl.glBindVertexArray(0)

    trace_tex = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D, trace_tex)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, SCR_WIDTH, SCR_HEIGHT, 0, gl.GL_RGBA, gl.GL_FLOAT, None)
    gl.glBindTexture(gl.GL_TEXTURE_2D, 0)

    # -- initialize quad program uniforms
    quad_program.use()
    quad_program.set_int("trace_tex", 0)

    while not glfw.window_should_close(window):
        projection = Matrix44.perspective_projection(camera.zoom, SCR_WIDTH / SCR_HEIGHT, 1.0, 2.0)

        # -- time logic
        current_frame = glfw.get_time()
        delta_time = current_frame - last_frame
        last_frame = current_frame

        # -- input
        process_input(window)

        compute_program.use()
        compute_program.set_int("u_NumFrames", frames)
        compute_program.set_float("u_Accum", frames / (frames+1))
        compute_program.set_mat4("u_InvProjectionMat", projection.inverse)
        compute_program.set_mat4("u_InvViewMat", camera.get_view_matrix().inverse)

        gl.glBindImageTexture(0, trace_tex, 0, gl.GL_FALSE, 0, gl.GL_WRITE_ONLY, gl.GL_RGBA32F)
        gl.glDispatchCompute(SCR_WIDTH // 8, SCR_HEIGHT // 8, 1)

        gl.glBindImageTexture(0, 0, 0, gl.GL_FALSE, 0, gl.GL_READ_WRITE, gl.GL_RGBA32F)
        gl.glMemoryBarrier(gl.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)
        frames += 1

        quad_program.use()
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        gl.glBindVertexArray(vao)
        gl.glActiveTexture(gl.GL_TEXTURE0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, trace_tex)
        gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)

        glfw.swap_buffers(window)
        glfw.poll_events()

    glfw.terminate()