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)
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, )
def add_triangle_obstacle(self, p1: tuple, p2: tuple, p3: tuple, static=False): if self.simulate: self._init_compute_kernels() bgfx.setUniform(self.p1_uniform, as_void_ptr((c_float * 2)(p1[0], p1[1]))) bgfx.setUniform(self.p2_uniform, as_void_ptr((c_float * 2)(p2[0], p2[1]))) bgfx.setUniform(self.p3_uniform, as_void_ptr((c_float * 2)(p3[0], p3[1]))) bgfx.setUniform( self.static_uniform, as_void_ptr((c_float * 1)(1.0 if static else 0.0)) ) bgfx.dispatch( 0, self._add_triangle_obstacle_kernel, self._num_groups_x, self._num_groups_y, 1, )
def add_particles(self, position: tuple, radius: float, strength: float): if self.simulate: self._init_compute_kernels() bgfx.setUniform( self.position_uniform, as_void_ptr((c_float * 2)(position[0], position[1])), ) bgfx.setUniform(self.value_uniform, as_void_ptr((c_float * 1)(strength))) bgfx.setUniform(self.radius_uniform, as_void_ptr((c_float * 1)(radius))) bgfx.dispatch(0, self._add_particles_kernel, self._num_groups_x, self._num_groups_x, 1) self._flip_buffer()
def add_velocity(self, position: tuple, velocity: tuple, radius: float): if self.simulate: self._init_compute_kernels() bgfx.setUniform( self.position_uniform, as_void_ptr((c_float * 2)(position[0], position[1])), ) bgfx.setUniform( self.value_uniform, as_void_ptr((c_float * 2)(velocity[0], velocity[1])) ) bgfx.setUniform(self.radius_uniform, as_void_ptr((c_float * 1)(radius))) bgfx.dispatch( 0, self._add_velocity_kernel, self._num_groups_x, self._num_groups_y, 1 ) self._flip_velocity_buffer()
def add_circle_obstacle(self, position: tuple, radius: float, static=False): if self.simulate: self._init_compute_kernels() bgfx.setUniform( self.position_uniform, as_void_ptr((c_float * 2)(position[0], position[1])), ) bgfx.setUniform(self.radius_uniform, as_void_ptr((c_float * 1)(radius))) bgfx.setUniform( self.static_uniform, as_void_ptr((c_float * 1)(1.0 if static else 0.0)) ) bgfx.dispatch( 0, self._add_circle_obstacle_kernel, self._num_groups_x, self._num_groups_y, 1, )
def update(self, time_delta: float): self._init_compute_kernels() if self.simulate: bgfx.setUniform(self.dissipation_uniform, as_void_ptr((c_float * 1)(self.dissipation))) bgfx.setUniform(self.elapsed_time_uniform, as_void_ptr((c_float * 1)(time_delta))) bgfx.setUniform(self.speed_uniform, as_void_ptr((c_float * 1)(self.speed))) bgfx.dispatch( 0, self._advect_particles_kernel, self._num_groups_x, self._num_groups_y, 1, ) self._flip_buffer()
def _update_params(self, time_delta: float): bgfx.setUniform( self.elapsed_time_uniform, as_void_ptr((c_float * 1)(time_delta)) ) bgfx.setUniform(self.speed_uniform, as_void_ptr((c_float * 1)(self.speed))) bgfx.setUniform( self.dissipation_uniform, as_void_ptr((c_float * 1)(self.dissipation)) ) bgfx.setUniform( self.vorticity_scale_uniform, as_void_ptr((c_float * 1)(self.vorticity)) ) if self._viscosity > 0.0: centre_factor = 1.0 / self.viscosity stencil_factor = 1.0 / (4.0 + centre_factor) bgfx.setUniform( self.alpha_uniform, as_void_ptr((c_float * 1)(centre_factor)) ) bgfx.setUniform( self.rbeta_uniform, as_void_ptr((c_float * 1)(stencil_factor)) )