slosh2d 0.6.0

Cross-platform GPU 2D Material Point Method implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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(0) @group(0) var<storage, read> entryPointParams_grid_0 : array<GridGeneric_std430_0>;

struct BlockVirtualId_std430_0
{
    @align(8) id_0 : vec2<i32>,
};

struct ActiveBlockHeaderGeneric_std430_0
{
    @align(8) virtual_id_0 : BlockVirtualId_std430_0,
    @align(8) first_particle_0 : u32,
    @align(4) num_particles_0 : u32,
};

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

struct Shape_std430_0
{
    @align(16) a_0 : vec4<f32>,
    @align(16) b_0 : vec4<f32>,
};

@binding(2) @group(0) var<storage, read> entryPointParams_collision_shapes_0 : array<Shape_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> entryPointParams_collision_shape_poses_0 : array<Sim2_std430_0>;

struct NodeCdf_std430_0
{
    @align(4) distance_0 : f32,
    @align(4) affinities_0 : u32,
    @align(4) closest_id_0 : u32,
};

struct Node_std430_0
{
    @align(16) momentum_velocity_mass_0 : vec3<f32>,
    @align(4) cdf_0 : NodeCdf_std430_0,
};

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

struct BlockHeaderId_0
{
     id_1 : u32,
};

fn BlockHeaderId_x24init_0( i_0 : u32) -> BlockHeaderId_0
{
    var _S1 : BlockHeaderId_0;
    _S1.id_1 = i_0;
    return _S1;
}

struct BlockPhysicalId_0
{
     id_2 : u32,
};

fn BlockPhysicalId_x24init_0( i_1 : u32) -> BlockPhysicalId_0
{
    var _S2 : BlockPhysicalId_0;
    _S2.id_2 = i_1;
    return _S2;
}

fn block_header_id_to_physical_id_0( hid_0 : BlockHeaderId_0) -> BlockPhysicalId_0
{
    return BlockPhysicalId_x24init_0(hid_0.id_1 * u32(64));
}

struct NodePhysicalId_0
{
     id_3 : u32,
};

fn NodePhysicalId_x24init_0( i_2 : u32) -> NodePhysicalId_0
{
    var _S3 : NodePhysicalId_0;
    _S3.id_3 = i_2;
    return _S3;
}

fn node_id_0( pid_0 : BlockPhysicalId_0,  shift_in_block_0 : vec2<u32>) -> NodePhysicalId_0
{
    return NodePhysicalId_x24init_0(pid_0.id_2 + shift_in_block_0.x + shift_in_block_0.y * u32(8));
}

struct NodeCdf_0
{
     distance_0 : f32,
     affinities_0 : u32,
     closest_id_0 : u32,
};

fn NodeCdf_x24init_0( distance_1 : f32,  affinities_1 : u32,  closest_id_1 : u32) -> NodeCdf_0
{
    var _S4 : NodeCdf_0;
    _S4.distance_0 = distance_1;
    _S4.affinities_0 = affinities_1;
    _S4.closest_id_0 = closest_id_1;
    return _S4;
}

fn Shape_shape_type_0( this_0 : ptr<function, Shape_std430_0>) -> u32
{
    return bitcast<u32>((*this_0).a_0.w);
}

fn Shape_shape_type_1( this_1 : ptr<function, Shape_std430_0>) -> u32
{
    return bitcast<u32>((*this_1).a_0.w);
}

struct Ball_0
{
     radius_0 : f32,
};

fn Ball_x24init_0( radius_1 : f32) -> Ball_0
{
    var _S5 : Ball_0;
    _S5.radius_0 = radius_1;
    return _S5;
}

fn Shape_to_ball_0( this_2 : ptr<function, Shape_std430_0>) -> Ball_0
{
    return Ball_x24init_0((*this_2).a_0.x);
}

fn Rot2_inv_mul_vec_0( this_3 : ptr<function, Rot2_std430_0>,  v_0 : vec2<f32>) -> vec2<f32>
{
    var _S6 : f32 = (*this_3).cos_sin_0.x;
    var _S7 : f32 = v_0.x;
    var _S8 : f32 = (*this_3).cos_sin_0.y;
    var _S9 : f32 = v_0.y;
    return vec2<f32>(_S6 * _S7 + _S8 * _S9, - _S8 * _S7 + _S6 * _S9);
}

fn Sim2_inv_mul_pt_0( this_4 : ptr<function, Sim2_std430_0>,  pt_0 : vec2<f32>) -> vec2<f32>
{
    var _S10 : Rot2_std430_0 = (*this_4).rotation_0;
    var _S11 : vec2<f32> = Rot2_inv_mul_vec_0(&(_S10), pt_0 - (*this_4).translation_0);
    return _S11 / vec2<f32>((*this_4).scale_0);
}

struct ProjectionResult_0
{
     point_0 : vec2<f32>,
     is_inside_0 : bool,
};

fn ProjectionResult_x24init_0( point_1 : vec2<f32>,  is_inside_1 : bool) -> ProjectionResult_0
{
    var _S12 : ProjectionResult_0;
    _S12.point_0 = point_1;
    _S12.is_inside_0 = is_inside_1;
    return _S12;
}

fn Ball_project_local_point_on_boundary_0( this_5 : Ball_0,  pt_1 : vec2<f32>) -> ProjectionResult_0
{
    var _S13 : f32 = length(pt_1);
    return ProjectionResult_x24init_0(select(vec2<f32>(0.0f, this_5.radius_0), pt_1 * vec2<f32>((this_5.radius_0 / _S13)), _S13 != 0.0f), _S13 <= (this_5.radius_0));
}

fn x2A_0( r_0 : ptr<function, Rot2_std430_0>,  v_1 : vec2<f32>) -> vec2<f32>
{
    var _S14 : f32 = (*r_0).cos_sin_0.x;
    var _S15 : f32 = v_1.x;
    var _S16 : f32 = (*r_0).cos_sin_0.y;
    var _S17 : f32 = v_1.y;
    return vec2<f32>(_S14 * _S15 - _S16 * _S17, _S16 * _S15 + _S14 * _S17);
}

fn Sim2_mul_pt_0( this_6 : ptr<function, Sim2_std430_0>,  pt_2 : vec2<f32>) -> vec2<f32>
{
    var _S18 : Rot2_std430_0 = (*this_6).rotation_0;
    var _S19 : vec2<f32> = x2A_0(&(_S18), pt_2 * vec2<f32>((*this_6).scale_0));
    return _S19 + (*this_6).translation_0;
}

fn Ball_project_point_on_boundary_0( this_7 : Ball_0,  pose_0 : ptr<function, Sim2_std430_0>,  pt_3 : vec2<f32>) -> ProjectionResult_0
{
    var _S20 : vec2<f32> = Sim2_inv_mul_pt_0(&((*pose_0)), pt_3);
    var result_0 : ProjectionResult_0 = Ball_project_local_point_on_boundary_0(this_7, _S20);
    var _S21 : vec2<f32> = Sim2_mul_pt_0(&((*pose_0)), result_0.point_0);
    result_0.point_0 = _S21;
    return result_0;
}

struct Cuboid_0
{
     halfExtents_0 : vec2<f32>,
};

fn Cuboid_x24init_0( halfExtents_1 : vec2<f32>) -> Cuboid_0
{
    var _S22 : Cuboid_0;
    _S22.halfExtents_0 = halfExtents_1;
    return _S22;
}

fn Shape_to_cuboid_0( this_8 : ptr<function, Shape_std430_0>) -> Cuboid_0
{
    return Cuboid_x24init_0((*this_8).a_0.xy);
}

fn Cuboid_project_local_point_0( this_9 : Cuboid_0,  pt_4 : vec2<f32>) -> vec2<f32>
{
    var _S23 : vec2<f32> = vec2<f32>(0.0f);
    return pt_4 + (max((vec2<f32>(0) - this_9.halfExtents_0) - pt_4, _S23) - max(pt_4 - this_9.halfExtents_0, _S23));
}

fn Cuboid_project_local_point_on_boundary_0( this_10 : Cuboid_0,  pt_5 : vec2<f32>) -> ProjectionResult_0
{
    var _S24 : vec2<f32> = Cuboid_project_local_point_0(this_10, pt_5);
    var _S25 : vec2<f32> = vec2<f32>((vec2<i32>(sign((pt_5)))));
    var _S26 : vec2<f32> = _S25 + (abs(_S25) - vec2<f32>(1.0f));
    var _S27 : vec2<f32> = this_10.halfExtents_0 - _S26 * pt_5;
    var _S28 : f32 = _S27.x;
    var _S29 : f32 = _S27.y;
    var _S30 : bool = (all((pt_5 == _S24)));
    return ProjectionResult_x24init_0(select(_S24, pt_5 + select(vec2<f32>(0.0f, _S29 * _S26.y), vec2<f32>(_S28 * _S26.x, 0.0f), _S28 <= _S29), _S30), _S30);
}

fn Cuboid_project_point_on_boundary_0( this_11 : Cuboid_0,  pose_1 : ptr<function, Sim2_std430_0>,  pt_6 : vec2<f32>) -> ProjectionResult_0
{
    var _S31 : vec2<f32> = Sim2_inv_mul_pt_0(&((*pose_1)), pt_6);
    var result_1 : ProjectionResult_0 = Cuboid_project_local_point_on_boundary_0(this_11, _S31);
    var _S32 : vec2<f32> = Sim2_mul_pt_0(&((*pose_1)), result_1.point_0);
    result_1.point_0 = _S32;
    return result_1;
}

struct Segment_0
{
     a_1 : vec2<f32>,
     b_1 : vec2<f32>,
};

fn Segment_x24init_0( a_2 : vec2<f32>,  b_2 : vec2<f32>) -> Segment_0
{
    var _S33 : Segment_0;
    _S33.a_1 = a_2;
    _S33.b_1 = b_2;
    return _S33;
}

struct Capsule_0
{
     segment_0 : Segment_0,
     radius_2 : f32,
};

fn Capsule_x24init_0( segment_1 : Segment_0,  radius_3 : f32) -> Capsule_0
{
    var _S34 : Capsule_0;
    _S34.segment_0 = segment_1;
    _S34.radius_2 = radius_3;
    return _S34;
}

fn Shape_to_capsule_0( this_12 : ptr<function, Shape_std430_0>) -> Capsule_0
{
    return Capsule_x24init_0(Segment_x24init_0((*this_12).a_0.xy, (*this_12).b_0.xy), (*this_12).b_0.w);
}

fn Segment_project_local_point_0( this_13 : Segment_0,  pt_7 : vec2<f32>) -> vec2<f32>
{
    var _S35 : vec2<f32> = this_13.b_1 - this_13.a_1;
    var _S36 : f32 = dot(_S35, pt_7 - this_13.a_1);
    var _S37 : f32 = dot(_S35, _S35);
    if(_S36 <= 0.0f)
    {
        return this_13.a_1;
    }
    else
    {
        if(_S36 >= _S37)
        {
            return this_13.b_1;
        }
        else
        {
            return this_13.a_1 + _S35 * vec2<f32>((_S36 / _S37));
        }
    }
}

fn any_orthogonal_vector_0( v_2 : vec2<f32>) -> vec2<f32>
{
    return vec2<f32>(v_2.y, - v_2.x);
}

fn Capsule_project_local_point_on_boundary_0( this_14 : Capsule_0,  pt_8 : vec2<f32>) -> ProjectionResult_0
{
    var _S38 : vec2<f32> = Segment_project_local_point_0(this_14.segment_0, pt_8);
    var _S39 : vec2<f32> = pt_8 - _S38;
    var _S40 : f32 = length(_S39);
    if(_S40 > 0.0f)
    {
        return ProjectionResult_x24init_0(_S38 + _S39 * vec2<f32>((this_14.radius_2 / _S40)), _S40 <= (this_14.radius_2));
    }
    else
    {
        var _S41 : vec2<f32> = this_14.segment_0.b_1 - this_14.segment_0.a_1;
        return ProjectionResult_x24init_0(_S38 + any_orthogonal_vector_0(_S41 / vec2<f32>(length(_S41))) * vec2<f32>(this_14.radius_2), true);
    }
}

fn Capsule_project_point_on_boundary_0( this_15 : Capsule_0,  pose_2 : ptr<function, Sim2_std430_0>,  pt_9 : vec2<f32>) -> ProjectionResult_0
{
    var _S42 : vec2<f32> = Sim2_inv_mul_pt_0(&((*pose_2)), pt_9);
    var result_2 : ProjectionResult_0 = Capsule_project_local_point_on_boundary_0(this_15, _S42);
    var _S43 : vec2<f32> = Sim2_mul_pt_0(&((*pose_2)), result_2.point_0);
    result_2.point_0 = _S43;
    return result_2;
}

fn Shape_project_point_on_boundary_0( this_16 : ptr<function, Shape_std430_0>,  pose_3 : ptr<function, Sim2_std430_0>,  pt_10 : vec2<f32>) -> ProjectionResult_0
{
    var _S44 : u32 = Shape_shape_type_1(&((*this_16)));
    if(_S44 == u32(0))
    {
        var _S45 : Ball_0 = Shape_to_ball_0(&((*this_16)));
        var _S46 : ProjectionResult_0 = Ball_project_point_on_boundary_0(_S45, &((*pose_3)), pt_10);
        return _S46;
    }
    if(_S44 == u32(1))
    {
        var _S47 : Cuboid_0 = Shape_to_cuboid_0(&((*this_16)));
        var _S48 : ProjectionResult_0 = Cuboid_project_point_on_boundary_0(_S47, &((*pose_3)), pt_10);
        return _S48;
    }
    if(_S44 == u32(2))
    {
        var _S49 : Capsule_0 = Shape_to_capsule_0(&((*this_16)));
        var _S50 : ProjectionResult_0 = Capsule_project_point_on_boundary_0(_S49, &((*pose_3)), pt_10);
        return _S50;
    }
    return ProjectionResult_x24init_0(pt_10, false);
}

fn getCount_0() -> i32
{
    var _S51 : vec2<u32> = vec2<u32>(arrayLength(&entryPointParams_collision_shapes_0), 32);
    return i32(_S51.x);
}

fn collide_0( _S52 : f32,  _S53 : vec2<f32>) -> NodeCdf_0
{
    var cdf_1 : NodeCdf_0 = NodeCdf_x24init_0(1.0e+10f, u32(0), u32(4294967295));
    var _S54 : vec2<f32> = vec2<f32>((_S52 * 1.5f));
    var i_3 : u32 = u32(0);
    for(;;)
    {
        var _S55 : i32 = getCount_0();
        if(i_3 < u32(_S55))
        {
        }
        else
        {
            break;
        }
        var _S56 : Shape_std430_0 = entryPointParams_collision_shapes_0[i_3];
        var _S57 : Sim2_std430_0 = entryPointParams_collision_shape_poses_0[i_3];
        var _S58 : u32 = Shape_shape_type_0(&(_S56));
        var _S59 : bool;
        if(_S58 != u32(5))
        {
            _S59 = _S58 != u32(6);
        }
        else
        {
            _S59 = false;
        }
        if(_S59)
        {
            var _S60 : ProjectionResult_0 = Shape_project_point_on_boundary_0(&(_S56), &(_S57), _S53);
            var _S61 : vec2<f32> = _S60.point_0 - _S53;
            var _S62 : bool;
            if(_S60.is_inside_0)
            {
                _S62 = true;
            }
            else
            {
                _S62 = (all(((abs(_S61)) <= _S54)));
            }
            if(_S62)
            {
                var _S63 : f32 = length(_S61);
                cdf_1.closest_id_0 = select(cdf_1.closest_id_0, i_3, _S63 < (cdf_1.distance_0));
                cdf_1.distance_0 = min(cdf_1.distance_0, _S63);
                cdf_1.affinities_0 = ((cdf_1.affinities_0) | (((select(u32(1), u32(65537), _S60.is_inside_0) << (i_3)))));
            }
        }
        i_3 = i_3 + u32(1);
    }
    return cdf_1;
}

@compute
@workgroup_size(8, 8, 1)
fn grid_update(@builtin(workgroup_id) block_id_0 : vec3<u32>, @builtin(local_invocation_id) tid_0 : vec3<u32>, @builtin(local_invocation_index) tid_flat_0 : u32)
{
    var _S64 : u32 = block_id_0.x;
    var _S65 : vec2<u32> = tid_0.xy;
    var _S66 : NodeCdf_0 = collide_0(entryPointParams_grid_0[i32(0)].cell_width_0, vec2<f32>(entryPointParams_active_blocks_0[_S64].virtual_id_0.id_0 * vec2<i32>(i32(8)) + vec2<i32>(_S65)) * vec2<f32>(entryPointParams_grid_0[i32(0)].cell_width_0));
    entryPointParams_nodes_0[node_id_0(block_header_id_to_physical_id_0(BlockHeaderId_x24init_0(_S64)), _S65).id_3].cdf_0.distance_0 = _S66.distance_0;
    entryPointParams_nodes_0[node_id_0(block_header_id_to_physical_id_0(BlockHeaderId_x24init_0(_S64)), _S65).id_3].cdf_0.affinities_0 = _S66.affinities_0;
    entryPointParams_nodes_0[node_id_0(block_header_id_to_physical_id_0(BlockHeaderId_x24init_0(_S64)), _S65).id_3].cdf_0.closest_id_0 = _S66.closest_id_0;
    return;
}