Files
wgpu/tests/out/msl/boids.msl
T
Jim Blandy 3fdd8592fc [wgsl-in] Rework Load Rule handling and indirection.
Make the parser code more closely follow the spec's grammar around
`unary_expression`, `postfix_expression`, and `singular_expression`.

Change the handling of postfix expressions (indexing, member/component access,
and swizzling) to apply the indirection at the appropriate time, resulting in
code improvements on all output formats. For example, where we used to generate
the following MSL:

    metal::float4 _e13 = bar.matrix[3];
    float b = _e13.x;

we now generate, simply:

    float b = bar.matrix[3].x;

Propagate WGSL reference types correctly, so that parenthesizing expressions no
longer causes the Load Rule to be applied.

Together with #1332 (already landed), this is a replacement for #1312, and
unblocks #1352.

Fixes #1351.
2021-09-14 16:17:57 -04:00

160 lines
4.4 KiB
Plaintext

// language: metal2.0
#include <metal_stdlib>
#include <simd/simd.h>
struct _mslBufferSizes {
metal::uint size1;
metal::uint size2;
};
constexpr constant unsigned NUM_PARTICLES = 1500u;
struct Particle {
metal::float2 pos;
metal::float2 vel;
};
struct SimParams {
float deltaT;
float rule1Distance;
float rule2Distance;
float rule3Distance;
float rule1Scale;
float rule2Scale;
float rule3Scale;
};
typedef Particle type3[1];
struct Particles {
type3 particles;
};
struct main1Input {
};
kernel void main1(
metal::uint3 global_invocation_id [[thread_position_in_grid]]
, constant SimParams& params [[buffer(0)]]
, constant Particles& particlesSrc [[buffer(1)]]
, device Particles& particlesDst [[buffer(2)]]
, constant _mslBufferSizes& _buffer_sizes [[buffer(3)]]
) {
metal::float2 vPos;
metal::float2 vVel;
metal::float2 cMass;
metal::float2 cVel;
metal::float2 colVel;
int cMassCount = 0;
int cVelCount = 0;
metal::float2 pos;
metal::float2 vel;
metal::uint i = 0u;
uint index = global_invocation_id.x;
if (index >= NUM_PARTICLES) {
return;
}
metal::float2 _e10 = particlesSrc.particles[index].pos;
vPos = _e10;
metal::float2 _e15 = particlesSrc.particles[index].vel;
vVel = _e15;
cMass = metal::float2(0.0, 0.0);
cVel = metal::float2(0.0, 0.0);
colVel = metal::float2(0.0, 0.0);
bool loop_init = true;
while(true) {
if (!loop_init) {
metal::uint _e86 = i;
i = _e86 + 1u;
}
loop_init = false;
metal::uint _e37 = i;
if (_e37 >= NUM_PARTICLES) {
break;
}
metal::uint _e39 = i;
if (_e39 == index) {
continue;
}
metal::uint _e42 = i;
metal::float2 _e45 = particlesSrc.particles[_e42].pos;
pos = _e45;
metal::uint _e47 = i;
metal::float2 _e50 = particlesSrc.particles[_e47].vel;
vel = _e50;
metal::float2 _e51 = pos;
metal::float2 _e52 = vPos;
float _e55 = params.rule1Distance;
if (metal::distance(_e51, _e52) < _e55) {
metal::float2 _e57 = cMass;
metal::float2 _e58 = pos;
cMass = _e57 + _e58;
int _e60 = cMassCount;
cMassCount = _e60 + 1;
}
metal::float2 _e63 = pos;
metal::float2 _e64 = vPos;
float _e67 = params.rule2Distance;
if (metal::distance(_e63, _e64) < _e67) {
metal::float2 _e69 = colVel;
metal::float2 _e70 = pos;
metal::float2 _e71 = vPos;
colVel = _e69 - (_e70 - _e71);
}
metal::float2 _e74 = pos;
metal::float2 _e75 = vPos;
float _e78 = params.rule3Distance;
if (metal::distance(_e74, _e75) < _e78) {
metal::float2 _e80 = cVel;
metal::float2 _e81 = vel;
cVel = _e80 + _e81;
int _e83 = cVelCount;
cVelCount = _e83 + 1;
}
}
int _e89 = cMassCount;
if (_e89 > 0) {
metal::float2 _e92 = cMass;
int _e93 = cMassCount;
metal::float2 _e97 = vPos;
cMass = (_e92 / metal::float2(static_cast<float>(_e93))) - _e97;
}
int _e99 = cVelCount;
if (_e99 > 0) {
metal::float2 _e102 = cVel;
int _e103 = cVelCount;
cVel = _e102 / metal::float2(static_cast<float>(_e103));
}
metal::float2 _e107 = vVel;
metal::float2 _e108 = cMass;
float _e110 = params.rule1Scale;
metal::float2 _e113 = colVel;
float _e115 = params.rule2Scale;
metal::float2 _e118 = cVel;
float _e120 = params.rule3Scale;
vVel = ((_e107 + (_e108 * _e110)) + (_e113 * _e115)) + (_e118 * _e120);
metal::float2 _e123 = vVel;
metal::float2 _e125 = vVel;
vVel = metal::normalize(_e123) * metal::clamp(metal::length(_e125), 0.0, 0.10000000149011612);
metal::float2 _e131 = vPos;
metal::float2 _e132 = vVel;
float _e134 = params.deltaT;
vPos = _e131 + (_e132 * _e134);
float _e138 = vPos.x;
if (_e138 < -1.0) {
vPos.x = 1.0;
}
float _e144 = vPos.x;
if (_e144 > 1.0) {
vPos.x = -1.0;
}
float _e150 = vPos.y;
if (_e150 < -1.0) {
vPos.y = 1.0;
}
float _e156 = vPos.y;
if (_e156 > 1.0) {
vPos.y = -1.0;
}
metal::float2 _e164 = vPos;
particlesDst.particles[index].pos = _e164;
metal::float2 _e168 = vVel;
particlesDst.particles[index].vel = _e168;
return;
}