Beispiel #1
0
    def _flip_pressure_buffer(self):
        tmp = self.PRESSURE_READ
        self.PRESSURE_READ = self.PRESSURE_WRITE
        self.PRESSURE_WRITE = tmp

        bgfx.setBuffer(
            TemplateConstants.PRESSURE_IN.value,
            self._pressure_buffer[self.PRESSURE_READ],
            bgfx.Access.Read,
        )
        bgfx.setBuffer(
            TemplateConstants.PRESSURE_OUT.value,
            self._pressure_buffer[self.PRESSURE_WRITE],
            bgfx.Access.Write,
        )
Beispiel #2
0
    def _flip_buffer(self):
        tmp = self.PARTICLES_IN
        self.PARTICLES_IN = self.PARTICLES_OUT
        self.PARTICLES_OUT = tmp

        bgfx.setBuffer(
            TemplateConstants.PARTICLES_IN.value,
            self._particles_buffer[self.PARTICLES_IN],
            bgfx.Access.Read,
        )
        bgfx.setBuffer(
            TemplateConstants.PARTICLES_OUT.value,
            self._particles_buffer[self.PARTICLES_OUT],
            bgfx.Access.Write,
        )
Beispiel #3
0
    def _flip_velocity_buffer(self):
        tmp = self.VELOCITY_READ
        self.VELOCITY_READ = self.VELOCITY_WRITE
        self.VELOCITY_WRITE = tmp

        bgfx.setBuffer(
            TemplateConstants.VELOCITY_IN.value,
            self._velocity_buffer[self.VELOCITY_READ],
            bgfx.Access.Read,
        )
        bgfx.setBuffer(
            TemplateConstants.VELOCITY_OUT.value,
            self._velocity_buffer[self.VELOCITY_WRITE],
            bgfx.Access.Write,
        )
Beispiel #4
0
    def _init_compute_kernels(self):
        bgfx.setUniform(
            self.particle_size_uniform,
            as_void_ptr((c_float * 2)(self._particle_size[0],
                                      self._particle_size[1])),
        )
        bgfx.setUniform(
            self.velocity_size_uniform,
            as_void_ptr((c_float * 2)(self._velocity_size[0],
                                      self._velocity_size[1])),
        )

        bgfx.setBuffer(
            TemplateConstants.PARTICLES_IN.value,
            self._particles_buffer[self.PARTICLES_IN],
            bgfx.Access.Write,
        )
        bgfx.setBuffer(
            TemplateConstants.PARTICLES_OUT.value,
            self._particles_buffer[self.PARTICLES_OUT],
            bgfx.Access.Write,
        )
Beispiel #5
0
    def _init_compute_kernels(self):
        bgfx.setUniform(self.size_uniform,
                        as_void_ptr((c_float * 2)(self._width, self._height)))

        bgfx.setBuffer(1, self._velocity_buffer[self.VELOCITY_READ],
                       bgfx.Access.Read)
        bgfx.setBuffer(2, self._velocity_buffer[self.VELOCITY_WRITE],
                       bgfx.Access.Write)

        bgfx.setBuffer(3, self._pressure_buffer[self.PRESSURE_READ],
                       bgfx.Access.Read)
        bgfx.setBuffer(4, self._pressure_buffer[self.PRESSURE_WRITE],
                       bgfx.Access.Write)

        bgfx.setBuffer(5, self._divergence_buffer, bgfx.Access.ReadWrite)
        bgfx.setBuffer(6, self._vorticity_buffer, bgfx.Access.ReadWrite)
        bgfx.setBuffer(7, self._obstacles_buffer, bgfx.Access.ReadWrite)
Beispiel #6
0
    def update(self, time_delta: float):
        if self.simulate:

            self._init_compute_kernels()
            self._update_params(time_delta)

            # Init boundaries
            if self.has_borders:
                bgfx.dispatch(
                    0,
                    self._init_boundaries_kernel,
                    self._num_groups_x,
                    self._num_groups_y,
                    1,
                )

            # Advect
            bgfx.dispatch(
                0,
                self._advect_velocity_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )
            self._flip_velocity_buffer()

            # Vorticity confinement 1 - Calculate vorticity
            bgfx.dispatch(
                0,
                self._calc_vorticity_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )

            # Vorticity confinement 2 - Apply vorticity force
            bgfx.dispatch(
                0,
                self._apply_vorticity_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )
            self._flip_velocity_buffer()

            # Viscosity
            if self.viscosity > 0.0:
                bgfx.dispatch(
                    0,
                    self._viscosity_kernel,
                    self._num_groups_x,
                    self._num_groups_y,
                    1,
                )
                self._flip_velocity_buffer()

            # Divergence
            bgfx.dispatch(0, self._divergence_kernel, self._num_groups_x,
                          self._num_groups_y, 1)

            # Clear pressure
            bgfx.setBuffer(
                TemplateConstants.GENERIC.value,
                self._pressure_buffer[self.PRESSURE_READ],
                bgfx.Access.ReadWrite,
            )
            bgfx.dispatch(0, self._clear_buffer_kernel, self._num_groups_x,
                          self._num_groups_y, 1)
            bgfx.setBuffer(
                TemplateConstants.PRESSURE_IN.value,
                self._pressure_buffer[self.PRESSURE_READ],
                bgfx.Access.Read,
            )

            # Poisson
            for _ in range(self.iterations):
                bgfx.dispatch(0, self._poisson_kernel, self._num_groups_x,
                              self._num_groups_y, 1)
                self._flip_pressure_buffer()

            # Subtract gradient
            bgfx.dispatch(
                0,
                self._subtract_gradient_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )
            self._flip_velocity_buffer()

            # Clear obstacles
            bgfx.setBuffer(
                TemplateConstants.GENERIC.value,
                self._obstacles_buffer,
                bgfx.Access.ReadWrite,
            )
            bgfx.dispatch(0, self._clear_buffer_kernel, self._num_groups_x,
                          self._num_groups_y, 1)
            bgfx.setBuffer(
                TemplateConstants.OBSTACLES.value,
                self._obstacles_buffer,
                bgfx.Access.ReadWrite,
            )
Beispiel #7
0
    def update(self, dt):
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()

        ImGuiExtra.begin_frame(int(mouse_x), int(mouse_y), buttons_states, 0,
                               self.fb_width, self.fb_height)
        show_properties_dialog(self.fb_width, self.fb_height, self.hidpi)
        self._create_imgui_config_dialog()

        ImGuiExtra.end_frame()

        at = (c_float * 3)(*[0.0, 0.0, 0.0])
        eye = (c_float * 3)(*[0.0, 0.0, 10.0])
        up = (c_float * 3)(*[0.0, 1.0, 0.0])

        view = look_at(eye, at, up)
        projection = proj(11.4, 1, 0.1, 100.0)

        bgfx.setViewRect(0, 0, 0, self.fb_width, self.fb_height)

        bgfx.setViewTransform(0, as_void_ptr(view), as_void_ptr(projection))

        bgfx.setVertexBuffer(0, self.vertex_buffer, 0, 4)
        bgfx.setIndexBuffer(self.index_buffer, 0, cube_indices.size)

        bgfx.setState(BGFX_STATE_DEFAULT)
        bgfx.setImage(0, self.output_texture, 0, bgfx.Access.Write)

        self.fluid_simulator.add_circle_obstacle((0.5, 0.5), 40.0)

        self.fluid_simulator.update(dt)
        self.particle_area.update(dt)

        if glfw.get_mouse_button(self.window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS\
                and not ImGui.GetIO().WantCaptureMouse:
            n_mouse_x, n_mouse_y = self._get_normalized_mouse_coords(
                mouse_x, mouse_y)
            n_old_mouse_x, n_old_mouse_y = self._get_normalized_mouse_coords(
                self.old_mouse_x, self.old_mouse_y)
            vel_x = n_mouse_x - n_old_mouse_x
            vel_y = n_mouse_y - n_old_mouse_y
            # if vel_x != 0 and vel_y != 0:
            self.fluid_simulator.add_velocity((n_mouse_x, n_mouse_y),
                                              (vel_x * 10, vel_y * 10), 32.0)
            self.particle_area.add_particles((n_mouse_x, n_mouse_y),
                                             self.particles_diameter,
                                             self.particles_strength)

        self.old_mouse_x = mouse_x
        self.old_mouse_y = mouse_y

        bgfx.setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A
                      | BGFX_STATE_BLEND_ALPHA)

        bgfx.dispatch(0, self.cs_program, self.fb_width // 16,
                      self.fb_height // 16)
        bgfx.setTexture(0, self.texture_uniform, self.output_texture)
        bgfx.submit(0, self.main_program, 0, False)

        if self.show_quiver_plot_overlay.value:
            bgfx.setBuffer(1, self.fluid_simulator.get_velocity_buffer(),
                           bgfx.Access.Read)
            bgfx.setUniform(
                self.window_size_uniform,
                as_void_ptr((c_float * 2)(self.fb_width, self.fb_height)),
            )
            bgfx.setUniform(
                self.velocity_size_uniform,
                as_void_ptr((c_float * 2)(self.fluid_simulator.width,
                                          self.fluid_simulator.height)),
            )
            bgfx.setUniform(
                self.arrow_tile_size_uniform,
                as_void_ptr((c_float * 1)(self.quiver_plot_resolutions[
                    self.quiver_plot_resolution.value])),
            )
            bgfx.submit(0, self.quiver_program, 0, False)

        bgfx.frame()