mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-09 21:47:47 +00:00
tint/writer/msl: Generate an array<T,N> helper
And remove the WrapArraysInStructs transform. Wrapping arrays in structures becomes troublesome for `const` arrays, as currently WGSL does not allow `const` structures. MSL 2.0+ has a builtin array<> helper, but we're targetting MSL 1.2, so we have to emit our own. Fortunately, it can be done with a few lines of templated code. This produces significantly cleaner output. Change-Id: Ifc92ef21e09befa252a07c856c4b5afdc51cc2e4 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94540 Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Ben Clayton <bclayton@chromium.org> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
3c054304a8
commit
f47887d207
@@ -1,6 +1,19 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
|
||||
template<typename T, size_t N>
|
||||
struct tint_array {
|
||||
const constant T& operator[](size_t i) const constant { return elements[i]; }
|
||||
device T& operator[](size_t i) device { return elements[i]; }
|
||||
const device T& operator[](size_t i) const device { return elements[i]; }
|
||||
thread T& operator[](size_t i) thread { return elements[i]; }
|
||||
const thread T& operator[](size_t i) const thread { return elements[i]; }
|
||||
threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
|
||||
const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
|
||||
T elements[N];
|
||||
};
|
||||
|
||||
struct tint_symbol_1 {
|
||||
float2 a_particlePos [[attribute(0)]];
|
||||
float2 a_particleVel [[attribute(1)]];
|
||||
@@ -54,12 +67,8 @@ struct SimParams {
|
||||
/* 0x0018 */ float rule3Scale;
|
||||
};
|
||||
|
||||
struct tint_array_wrapper {
|
||||
/* 0x0000 */ Particle arr[5];
|
||||
};
|
||||
|
||||
struct Particles {
|
||||
/* 0x0000 */ tint_array_wrapper particles;
|
||||
/* 0x0000 */ tint_array<Particle, 5> particles;
|
||||
};
|
||||
|
||||
void comp_main_inner(uint3 gl_GlobalInvocationID, device Particles* const tint_symbol_4, const constant SimParams* const tint_symbol_5, device Particles* const tint_symbol_6) {
|
||||
@@ -67,8 +76,8 @@ void comp_main_inner(uint3 gl_GlobalInvocationID, device Particles* const tint_s
|
||||
if ((index >= 5u)) {
|
||||
return;
|
||||
}
|
||||
float2 vPos = (*(tint_symbol_4)).particles.arr[index].pos;
|
||||
float2 vVel = (*(tint_symbol_4)).particles.arr[index].vel;
|
||||
float2 vPos = (*(tint_symbol_4)).particles[index].pos;
|
||||
float2 vVel = (*(tint_symbol_4)).particles[index].vel;
|
||||
float2 cMass = float2(0.0f);
|
||||
float2 cVel = float2(0.0f);
|
||||
float2 colVel = float2(0.0f);
|
||||
@@ -80,8 +89,8 @@ void comp_main_inner(uint3 gl_GlobalInvocationID, device Particles* const tint_s
|
||||
if ((i == index)) {
|
||||
continue;
|
||||
}
|
||||
pos = float2((*(tint_symbol_4)).particles.arr[i].pos).xy;
|
||||
vel = float2((*(tint_symbol_4)).particles.arr[i].vel).xy;
|
||||
pos = float2((*(tint_symbol_4)).particles[i].pos).xy;
|
||||
vel = float2((*(tint_symbol_4)).particles[i].vel).xy;
|
||||
if ((distance(pos, vPos) < (*(tint_symbol_5)).rule1Distance)) {
|
||||
cMass = (cMass + pos);
|
||||
cMassCount = as_type<int>((as_type<uint>(cMassCount) + as_type<uint>(1)));
|
||||
@@ -115,8 +124,8 @@ void comp_main_inner(uint3 gl_GlobalInvocationID, device Particles* const tint_s
|
||||
if ((vPos[1] > 1.0f)) {
|
||||
vPos[1] = -1.0f;
|
||||
}
|
||||
(*(tint_symbol_6)).particles.arr[index].pos = vPos;
|
||||
(*(tint_symbol_6)).particles.arr[index].vel = vVel;
|
||||
(*(tint_symbol_6)).particles[index].pos = vPos;
|
||||
(*(tint_symbol_6)).particles[index].vel = vVel;
|
||||
}
|
||||
|
||||
kernel void comp_main(device Particles* tint_symbol_7 [[buffer(1)]], const constant SimParams* tint_symbol_8 [[buffer(0)]], device Particles* tint_symbol_9 [[buffer(2)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]]) {
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
struct tint_array_wrapper {
|
||||
float2 arr[3];
|
||||
|
||||
template<typename T, size_t N>
|
||||
struct tint_array {
|
||||
const constant T& operator[](size_t i) const constant { return elements[i]; }
|
||||
device T& operator[](size_t i) device { return elements[i]; }
|
||||
const device T& operator[](size_t i) const device { return elements[i]; }
|
||||
thread T& operator[](size_t i) thread { return elements[i]; }
|
||||
const thread T& operator[](size_t i) const thread { return elements[i]; }
|
||||
threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
|
||||
const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
|
||||
T elements[N];
|
||||
};
|
||||
|
||||
constant tint_array_wrapper pos = {.arr={float2(0.0f, 0.5f), float2(-0.5f), float2(0.5f, -0.5f)}};
|
||||
constant tint_array<float2, 3> pos = tint_array<float2, 3>{float2(0.0f, 0.5f), float2(-0.5f), float2(0.5f, -0.5f)};
|
||||
|
||||
struct tint_symbol {
|
||||
float4 value [[position]];
|
||||
};
|
||||
|
||||
float4 vtx_main_inner(uint VertexIndex) {
|
||||
return float4(pos.arr[VertexIndex], 0.0f, 1.0f);
|
||||
return float4(pos[VertexIndex], 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
vertex tint_symbol vtx_main(uint VertexIndex [[vertex_id]]) {
|
||||
|
||||
Reference in New Issue
Block a user