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:
Corentin Wallez 2020-11-26 16:39:46 +00:00 committed by Commit Bot service account
parent 303c5c2d29
commit 4814bdbdea
6 changed files with 180 additions and 155 deletions

View File

@ -103,13 +103,13 @@ void init() {
gl_Position = vec4(xpos, ypos, 0.0, 1.0); gl_Position = vec4(xpos, ypos, 0.0, 1.0);
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[location(0)]] var<out> FragColor : vec4<f32>;
#version 450 [[location(0)]] var<in> v_color : vec4<f32>;
layout(location = 0) out vec4 fragColor;
layout(location = 0) in vec4 v_color; [[stage(fragment)]] fn main() -> void {
void main() { FragColor = v_color;
fragColor = v_color; return;
})"); })");
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(

View File

@ -37,23 +37,26 @@ void init() {
wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480); wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480);
const char* vs = const char* vs =
"#version 450\n" "[[builtin(vertex_idx)]] var<in> VertexIndex : u32;\n"
"const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n" "[[builtin(position)]] var<out> Position : vec4<f32>;\n"
"void main() {\n" "const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(\n"
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n" " vec2<f32>( 0.0, 0.5),\n"
" vec2<f32>(-0.5, -0.5),\n"
" vec2<f32>( 0.5, -0.5)\n"
");\n"
"[[stage(vertex)]] fn main() -> void {\n"
" Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);\n"
" return;\n"
"}\n"; "}\n";
WGPUShaderModule vsModule = WGPUShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, vs).Release();
utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs)
.Release();
const char* fs = const char* fs =
"#version 450\n" "[[location(0)]] var<out> fragColor : vec4<f32>;\n"
"layout(location = 0) out vec4 fragColor;\n" "[[stage(fragment)]] fn main() -> void {\n"
"void main() {\n" " fragColor = vec4<f32>(1.0, 0.0, 0.0, 1.0);\n"
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" " return;\n"
"}\n"; "}\n";
WGPUShaderModule fsModule = WGPUShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, fs).Release();
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release();
{ {
WGPURenderPipelineDescriptor descriptor = {}; WGPURenderPipelineDescriptor descriptor = {};
@ -87,7 +90,7 @@ void init() {
descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl); descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
WGPUVertexStateDescriptor vertexState = {}; WGPUVertexStateDescriptor vertexState = {};
vertexState.indexFormat = WGPUIndexFormat_Uint32; vertexState.indexFormat = WGPUIndexFormat_Undefined;
vertexState.vertexBufferCount = 0; vertexState.vertexBufferCount = 0;
vertexState.vertexBuffers = nullptr; vertexState.vertexBuffers = nullptr;
descriptor.vertexState = &vertexState; descriptor.vertexState = &vertexState;

View File

@ -96,25 +96,30 @@ void initBuffers() {
void initRender() { void initRender() {
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( utils::CreateShaderModuleFromWGSL(device, R"(
#version 450 [[location(0)]] var<in> a_particlePos : vec2<f32>;
layout(location = 0) in vec2 a_particlePos; [[location(1)]] var<in> a_particleVel : vec2<f32>;
layout(location = 1) in vec2 a_particleVel; [[location(2)]] var<in> a_pos : vec2<f32>;
layout(location = 2) in vec2 a_pos; [[builtin(position)]] var<out> Position : vec4<f32>;
void main() {
float angle = -atan(a_particleVel.x, a_particleVel.y); [[stage(vertex)]]
vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle), fn main() -> void {
a_pos.x * sin(angle) + a_pos.y * cos(angle)); var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y);
gl_Position = vec4(pos + a_particlePos, 0, 1); 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 = wgpu::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( utils::CreateShaderModuleFromWGSL(device, R"(
#version 450 [[location(0)]] var<out> FragColor : vec4<f32>;
layout(location = 0) out vec4 fragColor; [[stage(fragment)]]
void main() { fn main() -> void {
fragColor = vec4(1.0); FragColor = vec4<f32>(1.0, 1.0, 1.0, 1.0);
return;
} }
)"); )");
@ -147,92 +152,99 @@ void initRender() {
void initSim() { void initSim() {
wgpu::ShaderModule module = wgpu::ShaderModule module =
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"( utils::CreateShaderModuleFromWGSL(device, R"(
#version 450 [[block]] struct Particle {
[[offset(0)]] pos : vec2<f32>;
struct Particle { [[offset(8)]] vel : vec2<f32>;
vec2 pos;
vec2 vel;
}; };
[[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 { # https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
float deltaT; [[stage(compute)]]
float rule1Distance; fn main() -> void {
float rule2Distance; var index : u32 = GlobalInvocationID.x;
float rule3Distance; if (index >= params.particleCount) {
float rule1Scale; return;
float rule2Scale; }
float rule3Scale; var vPos : vec2<f32> = particlesA.particles[index].pos;
int particleCount; var vVel : vec2<f32> = particlesA.particles[index].vel;
} params; 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 { for (var i : u32 = 0u; i < params.particleCount; i = i + 1u) {
Particle particles[1000]; if (i == index) {
} particlesA; 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; pos = particlesA.particles[i].pos.xy;
vel = particlesA.particles[i].vel.xy; vel = particlesA.particles[i].vel.xy;
if (distance(pos, vPos) < params.rule1Distance) { if (distance(pos, vPos) < params.rule1Distance) {
cMass += pos; cMass = cMass + pos;
cMassCount++; cMassCount = cMassCount + 1u;
} }
if (distance(pos, vPos) < params.rule2Distance) { if (distance(pos, vPos) < params.rule2Distance) {
colVel -= (pos - vPos); colVel = colVel - (pos - vPos);
} }
if (distance(pos, vPos) < params.rule3Distance) { if (distance(pos, vPos) < params.rule3Distance) {
cVel += vel; cVel = cVel + vel;
cVelCount++; cVelCount = cVelCount + 1u;
} }
} }
if (cMassCount > 0) {
cMass = cMass / cMassCount - vPos;
}
if (cVelCount > 0) {
cVel = cVel / cVelCount;
}
vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale; if (cMassCount > 0u) {
cMass = (cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos;
}
// clamp velocity for a more pleasing simulation. 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
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1); vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
# kinematic update
vPos = vPos + (vVel * params.deltaT);
// kinematic update # Wrap around boundary
vPos += vVel * params.deltaT; if (vPos.x < -1.0) {
vPos.x = 1.0;
// Wrap around boundary }
if (vPos.x < -1.0) vPos.x = 1.0; if (vPos.x > 1.0) {
if (vPos.x > 1.0) 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; 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; particlesB.particles[index].pos = vPos;
// Write back
particlesB.particles[index].vel = vVel; particlesB.particles[index].vel = vVel;
return;
} }
)"); )");

View File

@ -95,23 +95,23 @@ void init() {
initBuffers(); initBuffers();
initTextures(); initTextures();
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( [[builtin(position)]] var<out> Position : vec4<f32>;
#version 450 [[location(0)]] var<in> pos : vec4<f32>;
layout(location = 0) in vec4 pos; [[stage(vertex)]] fn main() -> void {
void main() { Position = pos;
gl_Position = pos; return;
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[builtin(frag_coord)]] var<in> FragCoord : vec4<f32>;
#version 450 [[set(0), binding(0)]] var<uniform_constant> mySampler: sampler;
layout(set = 0, binding = 0) uniform sampler mySampler; [[set(0), binding(1)]] var<uniform_constant> myTexture : texture_sampled_2d<f32>;
layout(set = 0, binding = 1) uniform texture2D myTexture;
layout(location = 0) out vec4 fragColor; [[location(0)]] var<out> FragColor : vec4<f32>;
void main() { [[stage(fragment)]] fn main() -> void {
fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0)); FragColor = textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(640.0, 480.0));
return;
})"); })");
auto bgl = utils::MakeBindGroupLayout( auto bgl = utils::MakeBindGroupLayout(

View File

@ -101,40 +101,46 @@ void init() {
initBuffers(); initBuffers();
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( [[block]] struct Camera {
#version 450 [[offset(0)]] view : mat4x4<f32>;
layout(set = 0, binding = 0) uniform cameraData { [[offset(64)]] proj : mat4x4<f32>;
mat4 view;
mat4 proj;
} camera;
layout(set = 0, binding = 1) uniform modelData {
mat4 modelMatrix;
}; };
layout(location = 0) in vec3 pos; [[set(0), binding(0)]] var<uniform> camera : Camera;
layout(location = 1) in vec3 col;
layout(location = 2) out vec3 f_col; [[block]] struct Model {
void main() { [[offset(0)]] matrix : mat4x4<f32>;
};
[[set(0), binding(1)]] var<uniform> model : Model;
[[location(0)]] var<in> pos : vec3<f32>;
[[location(1)]] var<in> col : vec3<f32>;
[[location(2)]] var<out> f_col : vec3<f32>;
[[builtin(position)]] var<out> Position : vec4<f32>;
[[stage(vertex)]] fn main() -> void {
f_col = col; f_col = col;
gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0); Position = camera.proj * camera.view * model.matrix * vec4<f32>(pos, 1.0);
return;
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[location(0)]] var<out> FragColor : vec4<f32>;
#version 450 [[location(2)]] var<in> f_col : vec3<f32>;
layout(location = 2) in vec3 f_col;
layout(location = 0) out vec4 fragColor; [[stage(fragment)]] fn main() -> void {
void main() { FragColor = vec4<f32>(f_col, 1.0);
fragColor = vec4(f_col, 1.0); return;
})"); })");
wgpu::ShaderModule fsReflectionModule = wgpu::ShaderModule fsReflectionModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[location(0)]] var<out> FragColor : vec4<f32>;
#version 450 [[location(2)]] var<in> f_col : vec3<f32>;
layout(location = 2) in vec3 f_col;
layout(location = 0) out vec4 fragColor; [[stage(fragment)]] fn main() -> void {
void main() { FragColor = vec4<f32>(mix(f_col, vec3<f32>(0.5, 0.5, 0.5), vec3<f32>(0.5, 0.5, 0.5)), 1.0);
fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0); return;
})"); })");
utils::ComboVertexStateDescriptor vertexState; utils::ComboVertexStateDescriptor vertexState;

View File

@ -312,19 +312,23 @@ int main(int argc, const char* argv[]) {
// The hacky pipeline to render a triangle. // The hacky pipeline to render a triangle.
utils::ComboRenderPipelineDescriptor pipelineDesc(device); utils::ComboRenderPipelineDescriptor pipelineDesc(device);
pipelineDesc.vertexStage.module = pipelineDesc.vertexStage.module = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( [[builtin(vertex_idx)]] var<in> VertexIndex : u32;
#version 450 [[builtin(position)]] var<out> Position : vec4<f32>;
const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f)); const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
void main() { vec2<f32>( 0.0, 0.5),
gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0); vec2<f32>(-0.5, -0.5),
vec2<f32>( 0.5, -0.5)
);
[[stage(vertex)]] fn main() -> void {
Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
return;
})"); })");
pipelineDesc.cFragmentStage.module = pipelineDesc.cFragmentStage.module = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[location(0)]] var<out> fragColor : vec4<f32>;
#version 450 [[stage(fragment)]] fn main() -> void {
layout(location = 0) out vec4 fragColor; fragColor = vec4<f32>(1.0, 0.0, 0.0, 1.0);
void main() { return;
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
})"); })");
pipelineDesc.colorStateCount = 1; pipelineDesc.colorStateCount = 1;
// BGRA shouldn't be hardcoded. Consider having a map[format -> pipeline]. // BGRA shouldn't be hardcoded. Consider having a map[format -> pipeline].