Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port shaders to WGSL #4

Draft
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion blank.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
const renderPass = commandEncoder.beginRenderPass({
colorAttachments: [{
view: textureView,
loadValue: [0, 0, 0, 1]
loadValue: [0, 0, 0, 1],
storeOp: "store",
}]
});
renderPass.endPass();
Expand Down
67 changes: 32 additions & 35 deletions cube.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
const presentationFormat = context.getPreferredFormat(adapter);
context.configure({
device,
format: presentationFormat
format: presentationFormat,
});

////////////////////////////////////////
Expand Down Expand Up @@ -134,43 +134,41 @@
// Create render pipeline
///////////////////////////

const vs = `
#version 450

layout(location=0) in vec4 position;
layout(location=1) in vec3 normal;

layout(set=0, binding=0) uniform SceneUniforms {
mat4 mvpMatrix;
const shader = `
struct SceneUniforms {
mvpMatrix: mat4x4<f32>;
};
[[group(0), binding(0)]] var<uniform> uniforms: SceneUniforms;

layout(location=0) out vec3 vNormal;

void main() {
vNormal = normal;
gl_Position = mvpMatrix * position;
}
`.trim();

const fs = `
#version 450
struct Varyings {
[[builtin(position)]] pos: vec4<f32>;
[[location(0)]] normal: vec3<f32>;
};

layout(location=0) in vec3 vNormal;

layout(location=0) out vec4 fragColor;

void main() {
fragColor = vec4(abs(vNormal), 1.0);
}
[[stage(vertex)]]
fn main_vs(
[[location(0)]] position: vec4<f32>,
[[location(1)]] normal: vec3<f32>,
) -> Varyings {
return Varyings(uniforms.mvpMatrix * position, normal);
}

[[stage(fragment)]]
fn main_fs(
in: Varyings
) -> [[location(0)]] vec4<f32> {
return vec4<f32>(abs(in.normal), 1.0);
}
`.trim();

const shader_module = device.createShaderModule({
code: shader,
});
const pipeline = device.createRenderPipeline({
layout: device.createPipelineLayout({bindGroupLayouts: [sceneUniformBindGroupLayout]}),
vertex: {
module: device.createShaderModule({
code: glslang.compileGLSL(vs, "vertex")
}),
entryPoint: "main",
module: shader_module,
entryPoint: "main_vs",
buffers: [
{
arrayStride: 12,
Expand All @@ -191,10 +189,8 @@
]
},
fragment: {
module: device.createShaderModule({
code: glslang.compileGLSL(fs, "fragment")
}),
entryPoint: "main",
module: shader_module,
entryPoint: "main_fs",
targets: [{
format: presentationFormat
}]
Expand Down Expand Up @@ -222,7 +218,8 @@
const renderPassDescription = {
colorAttachments: [{
view: context.getCurrentTexture().createView(),
loadValue: [0, 0, 0, 1]
loadValue: [0, 0, 0, 1],
storeOp: "store",
}],
depthStencilAttachment: {
view: depthTexture.createView(),
Expand Down
61 changes: 27 additions & 34 deletions triangle.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,38 +97,32 @@
// Create render pipeline
///////////////////////////

const vs = `
#version 450

layout(location=0) in vec4 position;
layout(location=1) in vec4 color;

layout(location=0) out vec4 vColor;

void main() {
vColor = color;
gl_Position = position;
}
`.trim();

const fs = `
#version 450

layout(location=0) in vec4 vColor;

layout(location=0) out vec4 fragColor;

void main() {
fragColor = vColor;
}
`.trim();
const shader = device.createShaderModule({
code: `
struct Varyings {
[[builtin(position)]] position: vec4<f32>;
[[location(0)]] color: vec4<f32>;
};

[[stage(vertex)]]
fn main_vs(
[[location(0)]] position: vec4<f32>,
[[location(1)]] color: vec4<f32>,
) -> Varyings {
return Varyings(position, color);
}

[[stage(fragment)]]
fn main_fs(in: Varyings) -> [[location(0)]] vec4<f32> {
return in.color;
}
`.trim(),
});

const pipeline = device.createRenderPipeline({
vertex: {
module: device.createShaderModule({
code: glslang.compileGLSL(vs, "vertex")
}),
entryPoint: "main",
module: shader,
entryPoint: "main_vs",
buffers: [
{
arrayStride: 8,
Expand All @@ -149,10 +143,8 @@
]
},
fragment: {
module: device.createShaderModule({
code: glslang.compileGLSL(fs, "fragment")
}),
entryPoint: "main",
module: shader,
entryPoint: "main_fs",
targets: [{
format: presentationFormat
}]
Expand All @@ -172,7 +164,8 @@
const renderPass = commandEncoder.beginRenderPass({
colorAttachments: [{
view: textureView,
loadValue: [0, 0, 0, 1]
loadValue: [0, 0, 0, 1],
storeOp: "store",
}]
});
renderPass.setPipeline(pipeline);
Expand Down