Port Animometer's vertex shader to WGSL
Bug: dawn:572 Change-Id: I21093b5bd09cb319cdf92616cd95e64935fcd1ed Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/33822 Auto-Submit: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Austin Eng <enga@chromium.org> Reviewed-by: Ben Clayton <bclayton@google.com> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
parent
3b3ab0217e
commit
266bce86b4
|
@ -56,51 +56,56 @@ void init() {
|
||||||
640, 480);
|
640, 480);
|
||||||
|
|
||||||
wgpu::ShaderModule vsModule =
|
wgpu::ShaderModule vsModule =
|
||||||
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
|
utils::CreateShaderModuleFromWGSL(device, R"(
|
||||||
#version 450
|
[[block]] struct Constants {
|
||||||
|
scale : f32;
|
||||||
|
time : f32;
|
||||||
|
offsetX : f32;
|
||||||
|
offsetY : f32;
|
||||||
|
scalar : f32;
|
||||||
|
scalarOffset : f32;
|
||||||
|
};
|
||||||
|
[[set(0), binding(0)]] var<uniform> c : Constants;
|
||||||
|
|
||||||
layout(std140, set = 0, binding = 0) uniform Constants {
|
[[location(0)]] var<out> v_color : vec4<f32>;
|
||||||
float scale;
|
[[builtin(vertex_idx)]] var<in> VertexIndex : u32;
|
||||||
float time;
|
[[builtin(position)]] var<out> Position : vec4<f32>;
|
||||||
float offsetX;
|
|
||||||
float offsetY;
|
|
||||||
float scalar;
|
|
||||||
float scalarOffset;
|
|
||||||
} c;
|
|
||||||
|
|
||||||
layout(location = 0) out vec4 v_color;
|
[[stage(vertex)]] fn main() -> void {
|
||||||
|
var positions : array<vec4<f32>, 3> = array<vec4<f32>, 3>(
|
||||||
|
vec4<f32>( 0.0, 0.1, 0.0, 1.0),
|
||||||
|
vec4<f32>(-0.1, -0.1, 0.0, 1.0),
|
||||||
|
vec4<f32>( 0.1, -0.1, 0.0, 1.0)
|
||||||
|
);
|
||||||
|
|
||||||
const vec4 positions[3] = vec4[3](
|
var colors : array<vec4<f32>, 3> = array<vec4<f32>, 3>(
|
||||||
vec4( 0.0f, 0.1f, 0.0f, 1.0f),
|
vec4<f32>(1.0, 0.0, 0.0, 1.0),
|
||||||
vec4(-0.1f, -0.1f, 0.0f, 1.0f),
|
vec4<f32>(0.0, 1.0, 0.0, 1.0),
|
||||||
vec4( 0.1f, -0.1f, 0.0f, 1.0f)
|
vec4<f32>(0.0, 0.0, 1.0, 1.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
const vec4 colors[3] = vec4[3](
|
var position : vec4<f32> = positions[VertexIndex];
|
||||||
vec4(1.0f, 0.0f, 0.0f, 1.0f),
|
var color : vec4<f32> = colors[VertexIndex];
|
||||||
vec4(0.0f, 1.0f, 0.0f, 1.0f),
|
|
||||||
vec4(0.0f, 0.0f, 1.0f, 1.0f)
|
|
||||||
);
|
|
||||||
|
|
||||||
void main() {
|
// TODO(dawn:572): Revisit once modf has been reworked in WGSL.
|
||||||
vec4 position = positions[gl_VertexIndex];
|
var fade : f32 = c.scalarOffset + c.time * c.scalar / 10.0;
|
||||||
vec4 color = colors[gl_VertexIndex];
|
fade = fade - floor(fade);
|
||||||
|
|
||||||
float fade = mod(c.scalarOffset + c.time * c.scalar / 10.0, 1.0);
|
|
||||||
if (fade < 0.5) {
|
if (fade < 0.5) {
|
||||||
fade = fade * 2.0;
|
fade = fade * 2.0;
|
||||||
} else {
|
} else {
|
||||||
fade = (1.0 - fade) * 2.0;
|
fade = (1.0 - fade) * 2.0;
|
||||||
}
|
}
|
||||||
float xpos = position.x * c.scale;
|
|
||||||
float ypos = position.y * c.scale;
|
var xpos : f32 = position.x * c.scale;
|
||||||
float angle = 3.14159 * 2.0 * fade;
|
var ypos : f32 = position.y * c.scale;
|
||||||
float xrot = xpos * cos(angle) - ypos * sin(angle);
|
const angle : f32 = 3.14159 * 2.0 * fade;
|
||||||
float yrot = xpos * sin(angle) + ypos * cos(angle);
|
const xrot : f32 = xpos * cos(angle) - ypos * sin(angle);
|
||||||
|
const yrot : f32 = xpos * sin(angle) + ypos * cos(angle);
|
||||||
xpos = xrot + c.offsetX;
|
xpos = xrot + c.offsetX;
|
||||||
ypos = yrot + c.offsetY;
|
ypos = yrot + c.offsetY;
|
||||||
v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color;
|
|
||||||
gl_Position = vec4(xpos, ypos, 0.0, 1.0);
|
v_color = vec4<f32>(fade, 1.0 - fade, 0.0, 1.0) + color;
|
||||||
|
Position = vec4<f32>(xpos, ypos, 0.0, 1.0);
|
||||||
})");
|
})");
|
||||||
|
|
||||||
wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
|
wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
|
||||||
|
@ -109,7 +114,6 @@ void init() {
|
||||||
|
|
||||||
[[stage(fragment)]] fn main() -> void {
|
[[stage(fragment)]] fn main() -> void {
|
||||||
FragColor = v_color;
|
FragColor = v_color;
|
||||||
return;
|
|
||||||
})");
|
})");
|
||||||
|
|
||||||
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(
|
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(
|
||||||
|
|
Loading…
Reference in New Issue