slosh2d 0.6.0

Cross-platform GPU 2D Material Point Method implementation.
struct SimulationParams_std140_0
{
    @align(16) gravity_0 : vec2<f32>,
    @align(8) padding_0 : f32,
    @align(4) dt_0 : f32,
};

@binding(0) @group(0) var<uniform> entryPointParams_params_0 : SimulationParams_std140_0;
struct GridGeneric_std430_0
{
    @align(4) num_active_blocks_0 : u32,
    @align(4) cell_width_0 : f32,
    @align(4) hmap_capacity_0 : u32,
    @align(4) capacity_0 : u32,
};

@binding(1) @group(0) var<storage, read> entryPointParams_grid_0 : array<GridGeneric_std430_0>;

struct BodyMassProperties_std430_0
{
    @align(8) inv_inertia_0 : f32,
    @align(8) inv_mass_0 : vec2<f32>,
    @align(8) com_0 : vec2<f32>,
};

@binding(2) @group(0) var<storage, read> entryPointParams_local_mprops_0 : array<BodyMassProperties_std430_0>;

struct Rot2_std430_0
{
    @align(8) cos_sin_0 : vec2<f32>,
};

struct Sim2_std430_0
{
    @align(8) rotation_0 : Rot2_std430_0,
    @align(8) translation_0 : vec2<f32>,
    @align(8) scale_0 : f32,
};

@binding(3) @group(0) var<storage, read_write> entryPointParams_poses_0 : array<Sim2_std430_0>;

struct BodyVelocity_std430_0
{
    @align(8) linear_0 : vec2<f32>,
    @align(8) angular_0 : f32,
};

@binding(4) @group(0) var<storage, read_write> entryPointParams_vels_0 : array<BodyVelocity_std430_0>;

@binding(5) @group(0) var<storage, read_write> entryPointParams_mprops_0 : array<BodyMassProperties_std430_0>;

struct IntegerImpulse_std430_0
{
    @align(8) com_1 : vec2<f32>,
    @align(8) linear_1 : vec2<i32>,
    @align(8) angular_1 : i32,
};

@binding(6) @group(0) var<storage, read_write> entryPointParams_incremental_impulses_0 : array<IntegerImpulse_std430_0>;

@binding(7) @group(0) var<storage, read> entryPointParams_poses_1 : array<Sim2_std430_0>;

@binding(8) @group(0) var<storage, read> entryPointParams_local_mprops_1 : array<BodyMassProperties_std430_0>;

@binding(9) @group(0) var<storage, read_write> entryPointParams_mprops_1 : array<BodyMassProperties_std430_0>;

@binding(10) @group(0) var<storage, read_write> entryPointParams_incremental_impulses_1 : array<IntegerImpulse_std430_0>;

fn int2flt_0( i_0 : i32) -> f32
{
    return f32(i_0) / 1.0e+05f;
}

struct Impulse_0
{
     linear_2 : vec2<f32>,
     angular_2 : f32,
};

fn Impulse_x24init_0( linear_3 : vec2<f32>,  angular_3 : f32) -> Impulse_0
{
    var _S1 : Impulse_0;
    _S1.linear_2 = linear_3;
    _S1.angular_2 = angular_3;
    return _S1;
}

fn int_impulse_to_float_0( imp_0 : ptr<function, IntegerImpulse_std430_0>) -> Impulse_0
{
    return Impulse_x24init_0(vec2<f32>(int2flt_0((*imp_0).linear_1.x), int2flt_0((*imp_0).linear_1.y)), int2flt_0((*imp_0).angular_1));
}

struct IntegerImpulse_0
{
     com_1 : vec2<f32>,
     linear_1 : vec2<i32>,
     angular_1 : i32,
};

fn IntegerImpulse_x24init_0( com_2 : vec2<f32>,  linear_4 : vec2<i32>,  angular_4 : i32) -> IntegerImpulse_0
{
    var _S2 : IntegerImpulse_0;
    _S2.com_1 = com_2;
    _S2.linear_1 = linear_4;
    _S2.angular_1 = angular_4;
    return _S2;
}

struct BodyVelocity_0
{
     linear_0 : vec2<f32>,
     angular_0 : f32,
};

fn BodyVelocity_x24init_0( linear_5 : vec2<f32>,  angular_5 : f32) -> BodyVelocity_0
{
    var _S3 : BodyVelocity_0;
    _S3.linear_0 = linear_5;
    _S3.angular_0 = angular_5;
    return _S3;
}

fn apply_impulse_0( mprops_0 : ptr<function, BodyMassProperties_std430_0>,  velocity_0 : ptr<function, BodyVelocity_std430_0>,  imp_1 : Impulse_0) -> BodyVelocity_0
{
    return BodyVelocity_x24init_0((*velocity_0).linear_0 + (*mprops_0).inv_mass_0 * imp_1.linear_2, (*velocity_0).angular_0 + imp_1.angular_2 * (*mprops_0).inv_inertia_0);
}

fn x2A_0( r_0 : ptr<function, Rot2_std430_0>,  v_0 : vec2<f32>) -> vec2<f32>
{
    var _S4 : f32 = (*r_0).cos_sin_0.x;
    var _S5 : f32 = v_0.x;
    var _S6 : f32 = (*r_0).cos_sin_0.y;
    var _S7 : f32 = v_0.y;
    return vec2<f32>(_S4 * _S5 - _S6 * _S7, _S6 * _S5 + _S4 * _S7);
}

struct Rot2_0
{
     cos_sin_0 : vec2<f32>,
};

fn x2A_1( r_1 : Rot2_0,  v_1 : vec2<f32>) -> vec2<f32>
{
    var _S8 : f32 = r_1.cos_sin_0.x;
    var _S9 : f32 = v_1.x;
    var _S10 : f32 = r_1.cos_sin_0.y;
    var _S11 : f32 = v_1.y;
    return vec2<f32>(_S8 * _S9 - _S10 * _S11, _S10 * _S9 + _S8 * _S11);
}

fn Sim2_mul_pt_0( this_0 : ptr<function, Sim2_std430_0>,  pt_0 : vec2<f32>) -> vec2<f32>
{
    var _S12 : Rot2_std430_0 = (*this_0).rotation_0;
    var _S13 : vec2<f32> = x2A_0(&(_S12), pt_0 * vec2<f32>((*this_0).scale_0));
    return _S13 + (*this_0).translation_0;
}

fn Rot2_x24init_0( cos_sin_1 : vec2<f32>) -> Rot2_0
{
    var _S14 : Rot2_0;
    _S14.cos_sin_0 = cos_sin_1;
    return _S14;
}

fn Rot2_from_angle_0( angle_0 : f32) -> Rot2_0
{
    return Rot2_x24init_0(vec2<f32>(cos(angle_0), sin(angle_0)));
}

fn x2A_2( lhs_0 : Rot2_0,  rhs_0 : ptr<function, Rot2_std430_0>) -> Rot2_0
{
    var _S15 : f32 = lhs_0.cos_sin_0.x;
    var _S16 : f32 = (*rhs_0).cos_sin_0.x;
    var _S17 : f32 = lhs_0.cos_sin_0.y;
    var _S18 : f32 = (*rhs_0).cos_sin_0.y;
    return Rot2_x24init_0(vec2<f32>(_S15 * _S16 - _S17 * _S18, _S17 * _S16 + _S15 * _S18));
}

struct Sim2_0
{
     rotation_0 : Rot2_0,
     translation_0 : vec2<f32>,
     scale_0 : f32,
};

fn Sim2_x24init_0( rotation_1 : Rot2_0,  translation_1 : vec2<f32>,  scale_1 : f32) -> Sim2_0
{
    var _S19 : Sim2_0;
    _S19.rotation_0 = rotation_1;
    _S19.translation_0 = translation_1;
    _S19.scale_0 = scale_1;
    return _S19;
}

fn integrate_velocity_0( pose_0 : ptr<function, Sim2_std430_0>,  vels_0 : BodyVelocity_0,  local_com_0 : vec2<f32>,  dt_1 : f32) -> Sim2_0
{
    var _S20 : vec2<f32> = Sim2_mul_pt_0(&((*pose_0)), local_com_0);
    var _S21 : Rot2_0 = Rot2_from_angle_0(vels_0.angular_0 * dt_1);
    var _S22 : vec2<f32> = _S20 + x2A_1(_S21, (*pose_0).translation_0 - _S20) * vec2<f32>((*pose_0).scale_0) + vels_0.linear_0 * vec2<f32>(dt_1);
    var _S23 : Rot2_std430_0 = (*pose_0).rotation_0;
    var _S24 : Rot2_0 = x2A_2(_S21, &(_S23));
    return Sim2_x24init_0(_S24, _S22, (*pose_0).scale_0);
}

fn getCount_0() -> i32
{
    var _S25 : vec2<u32> = vec2<u32>(arrayLength(&entryPointParams_vels_0), 16);
    return i32(_S25.x);
}

@compute
@workgroup_size(16, 1, 1)
fn update(@builtin(global_invocation_id) invocation_id_0 : vec3<u32>)
{
    var _S26 : u32 = invocation_id_0.x;
    var _S27 : i32 = getCount_0();
    if(_S26 < u32(_S27))
    {
        var _S28 : IntegerImpulse_std430_0 = entryPointParams_incremental_impulses_0[_S26];
        var _S29 : Impulse_0 = int_impulse_to_float_0(&(_S28));
        var _S30 : vec2<f32> = vec2<f32>(0.0f);
        var _S31 : IntegerImpulse_0 = IntegerImpulse_x24init_0(_S30, vec2<i32>(i32(0)), i32(0));
        entryPointParams_incremental_impulses_0[_S26].com_1 = _S31.com_1;
        entryPointParams_incremental_impulses_0[_S26].linear_1 = _S31.linear_1;
        entryPointParams_incremental_impulses_0[_S26].angular_1 = _S31.angular_1;
        var _S32 : BodyMassProperties_std430_0 = entryPointParams_mprops_0[_S26];
        var _S33 : BodyVelocity_std430_0 = entryPointParams_vels_0[_S26];
        var _S34 : BodyVelocity_0 = apply_impulse_0(&(_S32), &(_S33), _S29);
        var new_vel_0 : BodyVelocity_0 = _S34;
        var _S35 : f32 = length(new_vel_0.linear_0);
        var _S36 : f32 = length(new_vel_0.angular_0);
        var _S37 : f32 = 0.10000000149011612f * entryPointParams_grid_0[i32(0)].cell_width_0 / entryPointParams_params_0.dt_0;
        var _S38 : bool;
        if((length(_S29.linear_2)) != 0.0f)
        {
            _S38 = true;
        }
        else
        {
            var _S39 : f32 = length(_S29.angular_2);
            _S38 = _S39 != 0.0f;
        }
        if(_S38)
        {
            if(_S35 > _S37)
            {
                new_vel_0.linear_0 = new_vel_0.linear_0 * vec2<f32>((_S37 / _S35));
            }
            if(_S36 > 1.0f)
            {
                new_vel_0.angular_0 = new_vel_0.angular_0 * (1.0f / _S36);
            }
        }
        var _S40 : Sim2_std430_0 = entryPointParams_poses_0[_S26];
        var _S41 : Sim2_0 = integrate_velocity_0(&(_S40), new_vel_0, entryPointParams_local_mprops_0[_S26].com_0, entryPointParams_params_0.dt_0);
        new_vel_0.linear_0 = new_vel_0.linear_0 + entryPointParams_params_0.gravity_0 * vec2<f32>((entryPointParams_mprops_0[_S26].inv_mass_0) != _S30) * vec2<f32>(entryPointParams_params_0.dt_0);
        entryPointParams_vels_0[_S26].linear_0 = new_vel_0.linear_0;
        entryPointParams_vels_0[_S26].angular_0 = new_vel_0.angular_0;
        entryPointParams_poses_0[_S26].rotation_0.cos_sin_0 = _S41.rotation_0.cos_sin_0;
        entryPointParams_poses_0[_S26].translation_0 = _S41.translation_0;
        entryPointParams_poses_0[_S26].scale_0 = _S41.scale_0;
    }
    return;
}

struct BodyMassProperties_0
{
     inv_inertia_0 : f32,
     inv_mass_0 : vec2<f32>,
     com_0 : vec2<f32>,
};

fn BodyMassProperties_x24init_0( inv_inertia_1 : f32,  inv_mass_1 : vec2<f32>,  com_3 : vec2<f32>) -> BodyMassProperties_0
{
    var _S42 : BodyMassProperties_0;
    _S42.inv_inertia_0 = inv_inertia_1;
    _S42.inv_mass_0 = inv_mass_1;
    _S42.com_0 = com_3;
    return _S42;
}

fn update_mprops_0( pose_1 : ptr<function, Sim2_std430_0>,  local_mprops_0 : ptr<function, BodyMassProperties_std430_0>) -> BodyMassProperties_0
{
    var _S43 : vec2<f32> = Sim2_mul_pt_0(&((*pose_1)), (*local_mprops_0).com_0);
    return BodyMassProperties_x24init_0((*local_mprops_0).inv_inertia_0, (*local_mprops_0).inv_mass_0, _S43);
}

fn getCount_1() -> i32
{
    var _S44 : vec2<u32> = vec2<u32>(arrayLength(&entryPointParams_mprops_1), 24);
    return i32(_S44.x);
}

@compute
@workgroup_size(64, 1, 1)
fn update_world_mass_properties(@builtin(global_invocation_id) invocation_id_1 : vec3<u32>)
{
    var _S45 : u32 = invocation_id_1.x;
    var _S46 : i32 = getCount_1();
    if(_S45 < u32(_S46))
    {
        var _S47 : Sim2_std430_0 = entryPointParams_poses_1[_S45];
        var _S48 : BodyMassProperties_std430_0 = entryPointParams_local_mprops_1[_S45];
        var _S49 : BodyMassProperties_0 = update_mprops_0(&(_S47), &(_S48));
        entryPointParams_incremental_impulses_1[_S45].com_1 = _S49.com_0;
        entryPointParams_mprops_1[_S45].inv_inertia_0 = _S49.inv_inertia_0;
        entryPointParams_mprops_1[_S45].inv_mass_0 = _S49.inv_mass_0;
        entryPointParams_mprops_1[_S45].com_0 = _S49.com_0;
    }
    return;
}