mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-13 23:26:24 +00:00
Port most of the samples to WGSL
Animometer's fragment shaders remains unported because it isn't clear how to use modf in WGSL. Bug: dawn:572 Change-Id: I29aa0ee657b813e7308f0300addd1d5795bfc16d Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/33821 Reviewed-by: Ben Clayton <bclayton@google.com> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
303c5c2d29
commit
4814bdbdea
@@ -96,25 +96,30 @@ void initBuffers() {
|
||||
|
||||
void initRender() {
|
||||
wgpu::ShaderModule vsModule =
|
||||
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
|
||||
#version 450
|
||||
layout(location = 0) in vec2 a_particlePos;
|
||||
layout(location = 1) in vec2 a_particleVel;
|
||||
layout(location = 2) in vec2 a_pos;
|
||||
void main() {
|
||||
float angle = -atan(a_particleVel.x, a_particleVel.y);
|
||||
vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
|
||||
a_pos.x * sin(angle) + a_pos.y * cos(angle));
|
||||
gl_Position = vec4(pos + a_particlePos, 0, 1);
|
||||
utils::CreateShaderModuleFromWGSL(device, R"(
|
||||
[[location(0)]] var<in> a_particlePos : vec2<f32>;
|
||||
[[location(1)]] var<in> a_particleVel : vec2<f32>;
|
||||
[[location(2)]] var<in> a_pos : vec2<f32>;
|
||||
[[builtin(position)]] var<out> Position : vec4<f32>;
|
||||
|
||||
[[stage(vertex)]]
|
||||
fn main() -> void {
|
||||
var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y);
|
||||
var pos : vec2<f32> = vec2<f32>(
|
||||
(a_pos.x * cos(angle)) - (a_pos.y * sin(angle)),
|
||||
(a_pos.x * sin(angle)) + (a_pos.y * cos(angle)));
|
||||
Position = vec4<f32>(pos + a_particlePos, 0.0, 1.0);
|
||||
return;
|
||||
}
|
||||
)");
|
||||
|
||||
wgpu::ShaderModule fsModule =
|
||||
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
|
||||
#version 450
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
void main() {
|
||||
fragColor = vec4(1.0);
|
||||
utils::CreateShaderModuleFromWGSL(device, R"(
|
||||
[[location(0)]] var<out> FragColor : vec4<f32>;
|
||||
[[stage(fragment)]]
|
||||
fn main() -> void {
|
||||
FragColor = vec4<f32>(1.0, 1.0, 1.0, 1.0);
|
||||
return;
|
||||
}
|
||||
)");
|
||||
|
||||
@@ -147,92 +152,99 @@ void initRender() {
|
||||
|
||||
void initSim() {
|
||||
wgpu::ShaderModule module =
|
||||
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"(
|
||||
#version 450
|
||||
|
||||
struct Particle {
|
||||
vec2 pos;
|
||||
vec2 vel;
|
||||
utils::CreateShaderModuleFromWGSL(device, R"(
|
||||
[[block]] struct Particle {
|
||||
[[offset(0)]] pos : vec2<f32>;
|
||||
[[offset(8)]] vel : vec2<f32>;
|
||||
};
|
||||
[[block]] struct SimParams {
|
||||
[[offset(0)]] deltaT : f32;
|
||||
[[offset(4)]] rule1Distance : f32;
|
||||
[[offset(8)]] rule2Distance : f32;
|
||||
[[offset(12)]] rule3Distance : f32;
|
||||
[[offset(16)]] rule1Scale : f32;
|
||||
[[offset(20)]] rule2Scale : f32;
|
||||
[[offset(24)]] rule3Scale : f32;
|
||||
[[offset(28)]] particleCount : u32;
|
||||
};
|
||||
[[block]] struct Particles {
|
||||
[[offset(0)]] particles : [[stride(16)]] array<Particle>;
|
||||
};
|
||||
[[binding(0), set(0)]] var<uniform> params : SimParams;
|
||||
[[binding(1), set(0)]] var<storage_buffer> particlesA : [[access(read)]] Particles;
|
||||
[[binding(2), set(0)]] var<storage_buffer> particlesB : [[access(read_write)]] Particles;
|
||||
[[builtin(global_invocation_id)]] var<in> GlobalInvocationID : vec3<u32>;
|
||||
|
||||
layout(std140, set = 0, binding = 0) uniform SimParams {
|
||||
float deltaT;
|
||||
float rule1Distance;
|
||||
float rule2Distance;
|
||||
float rule3Distance;
|
||||
float rule1Scale;
|
||||
float rule2Scale;
|
||||
float rule3Scale;
|
||||
int particleCount;
|
||||
} params;
|
||||
# https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
||||
[[stage(compute)]]
|
||||
fn main() -> void {
|
||||
var index : u32 = GlobalInvocationID.x;
|
||||
if (index >= params.particleCount) {
|
||||
return;
|
||||
}
|
||||
var vPos : vec2<f32> = particlesA.particles[index].pos;
|
||||
var vVel : vec2<f32> = particlesA.particles[index].vel;
|
||||
var cMass : vec2<f32> = vec2<f32>(0.0, 0.0);
|
||||
var cVel : vec2<f32> = vec2<f32>(0.0, 0.0);
|
||||
var colVel : vec2<f32> = vec2<f32>(0.0, 0.0);
|
||||
var cMassCount : u32 = 0u;
|
||||
var cVelCount : u32 = 0u;
|
||||
var pos : vec2<f32>;
|
||||
var vel : vec2<f32>;
|
||||
|
||||
layout(std140, set = 0, binding = 1) buffer ParticlesA {
|
||||
Particle particles[1000];
|
||||
} particlesA;
|
||||
for (var i : u32 = 0u; i < params.particleCount; i = i + 1u) {
|
||||
if (i == index) {
|
||||
continue;
|
||||
}
|
||||
|
||||
layout(std140, set = 0, binding = 2) buffer ParticlesB {
|
||||
Particle particles[1000];
|
||||
} particlesB;
|
||||
|
||||
void main() {
|
||||
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
||||
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
if (index >= params.particleCount) { return; }
|
||||
|
||||
vec2 vPos = particlesA.particles[index].pos;
|
||||
vec2 vVel = particlesA.particles[index].vel;
|
||||
|
||||
vec2 cMass = vec2(0.0, 0.0);
|
||||
vec2 cVel = vec2(0.0, 0.0);
|
||||
vec2 colVel = vec2(0.0, 0.0);
|
||||
int cMassCount = 0;
|
||||
int cVelCount = 0;
|
||||
|
||||
vec2 pos;
|
||||
vec2 vel;
|
||||
for (int i = 0; i < params.particleCount; ++i) {
|
||||
if (i == index) { continue; }
|
||||
pos = particlesA.particles[i].pos.xy;
|
||||
vel = particlesA.particles[i].vel.xy;
|
||||
|
||||
if (distance(pos, vPos) < params.rule1Distance) {
|
||||
cMass += pos;
|
||||
cMassCount++;
|
||||
cMass = cMass + pos;
|
||||
cMassCount = cMassCount + 1u;
|
||||
}
|
||||
if (distance(pos, vPos) < params.rule2Distance) {
|
||||
colVel -= (pos - vPos);
|
||||
colVel = colVel - (pos - vPos);
|
||||
}
|
||||
if (distance(pos, vPos) < params.rule3Distance) {
|
||||
cVel += vel;
|
||||
cVelCount++;
|
||||
cVel = cVel + vel;
|
||||
cVelCount = cVelCount + 1u;
|
||||
}
|
||||
}
|
||||
if (cMassCount > 0) {
|
||||
cMass = cMass / cMassCount - vPos;
|
||||
}
|
||||
if (cVelCount > 0) {
|
||||
cVel = cVel / cVelCount;
|
||||
|
||||
if (cMassCount > 0u) {
|
||||
cMass = (cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos;
|
||||
}
|
||||
|
||||
vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
|
||||
if (cVelCount > 0u) {
|
||||
cVel = cVel / vec2<f32>(f32(cVelCount), f32(cVelCount));
|
||||
}
|
||||
vVel = vVel + (cMass * params.rule1Scale) + (colVel * params.rule2Scale) +
|
||||
(cVel * params.rule3Scale);
|
||||
|
||||
// clamp velocity for a more pleasing simulation.
|
||||
# clamp velocity for a more pleasing simulation
|
||||
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
|
||||
# kinematic update
|
||||
vPos = vPos + (vVel * params.deltaT);
|
||||
|
||||
// kinematic update
|
||||
vPos += vVel * params.deltaT;
|
||||
|
||||
// Wrap around boundary
|
||||
if (vPos.x < -1.0) vPos.x = 1.0;
|
||||
if (vPos.x > 1.0) vPos.x = -1.0;
|
||||
if (vPos.y < -1.0) vPos.y = 1.0;
|
||||
if (vPos.y > 1.0) vPos.y = -1.0;
|
||||
# Wrap around boundary
|
||||
if (vPos.x < -1.0) {
|
||||
vPos.x = 1.0;
|
||||
}
|
||||
if (vPos.x > 1.0) {
|
||||
vPos.x = -1.0;
|
||||
}
|
||||
if (vPos.y < -1.0) {
|
||||
vPos.y = 1.0;
|
||||
}
|
||||
if (vPos.y > 1.0) {
|
||||
vPos.y = -1.0;
|
||||
}
|
||||
|
||||
# Write back
|
||||
particlesB.particles[index].pos = vPos;
|
||||
|
||||
// Write back
|
||||
particlesB.particles[index].vel = vVel;
|
||||
return;
|
||||
}
|
||||
)");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user