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);
})");
wgpu::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
layout(location = 0) in vec4 v_color;
void main() {
fragColor = v_color;
wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
[[location(0)]] var<out> FragColor : vec4<f32>;
[[location(0)]] var<in> v_color : vec4<f32>;
[[stage(fragment)]] fn main() -> void {
FragColor = v_color;
return;
})");
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(

View File

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

View File

@ -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;
}
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);
# 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;
}
)");

View File

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

View File

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

View File

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