use crate::runtime::{PhysicsBody, PhysicsSolver, SimulationClass};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PhysicsExecution {
Disabled,
StaticCollider,
GameplayCpu,
GpuBuiltIn(ComputeShaderType),
GpuCustom(String),
InvalidCustomShader,
}
#[must_use]
pub fn physics_execution(body: &PhysicsBody) -> PhysicsExecution {
match body.simulation {
SimulationClass::None => PhysicsExecution::Disabled,
SimulationClass::Static => PhysicsExecution::StaticCollider,
SimulationClass::Cpu => PhysicsExecution::GameplayCpu,
SimulationClass::Gpu => match body.solver {
PhysicsSolver::Full => {
PhysicsExecution::GpuBuiltIn(ComputeShaderType::FullPhysics)
}
PhysicsSolver::Simplified => {
PhysicsExecution::GpuBuiltIn(ComputeShaderType::MidPhysic)
}
PhysicsSolver::NoCollision => {
PhysicsExecution::GpuBuiltIn(ComputeShaderType::NoCollision)
}
PhysicsSolver::Space => {
PhysicsExecution::GpuBuiltIn(ComputeShaderType::Space)
}
PhysicsSolver::Custom => body.custom_shader.clone().map_or(
PhysicsExecution::InvalidCustomShader,
PhysicsExecution::GpuCustom,
),
},
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ShaderBindings {
pub needs_read_buffer: bool, pub needs_write_buffer: bool, pub needs_grid_counts: bool, pub needs_grid_objects: bool, pub needs_big_indices: bool, }
impl ShaderBindings {
pub fn basic() -> Self {
Self {
needs_read_buffer: true,
needs_write_buffer: true,
needs_grid_counts: false,
needs_grid_objects: false,
needs_big_indices: false,
}
}
pub fn grid_build() -> Self {
Self {
needs_read_buffer: true,
needs_write_buffer: true,
needs_grid_counts: true,
needs_grid_objects: true,
needs_big_indices: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ComputeShaderType {
#[default]
FullPhysics,
MidPhysic,
Static,
NoCollision,
GridBuild,
Space,
Empty,
Cull,
GridCollision,
}
#[allow(non_upper_case_globals)]
impl ComputeShaderType {
#[deprecated(note = "renamed to `ComputeShaderType::GridCollision`")]
pub const Test: Self = Self::GridCollision;
}
impl ComputeShaderType {
pub fn sort_key(&self) -> u32 {
match self {
ComputeShaderType::FullPhysics => 0,
ComputeShaderType::MidPhysic => 1,
ComputeShaderType::Static => 2,
ComputeShaderType::NoCollision => 3,
ComputeShaderType::GridBuild => 4,
ComputeShaderType::Empty => 5,
ComputeShaderType::Cull => 6,
ComputeShaderType::GridCollision => 7,
ComputeShaderType::Space => 8,
}
}
pub fn needs_bindings(&self) -> ShaderBindings {
match self {
ComputeShaderType::GridBuild => ShaderBindings::grid_build(),
ComputeShaderType::GridCollision => ShaderBindings::grid_build(),
_ => ShaderBindings::basic(),
}
}
}