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

Update to latest webgpu spec #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
"@webgpu/types": "^0.1.7",
"@webgpu/types": "^0.1.49",
"eslint": "^8.2.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^15.0.0",
Expand Down
24 changes: 12 additions & 12 deletions src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export class Renderer {
colorAttachments: [],
depthStencilAttachment: {
view: depthTexture.createView(),
depthLoadValue: 1.0,
depthStoreOp: 'store',
stencilLoadValue: 0,
stencilStoreOp: 'store',
depthClearValue: 1.0,
depthLoadOp: 'clear',
depthStoreOp: 'store'
},
};

Expand All @@ -57,7 +56,7 @@ export class Renderer {
canvas.clientWidth * devicePixelRatio,
canvas.clientHeight * devicePixelRatio,
];
context.configure({ device, format: contextFormat, size });
context.configure({ device, format: contextFormat });
depthTexture.destroy();
depthTexture = device.createTexture({
size,
Expand All @@ -83,7 +82,8 @@ export class Renderer {
this.renderPassDesc.colorAttachments = [
{
view: this.context.getCurrentTexture().createView(),
loadValue: { r: 0.3, g: 0.5, b: 0.7, a: 1 },
clearValue: [0, 0, 0, 0],
loadOp: 'clear',
storeOp: 'store',
},
];
Expand All @@ -106,7 +106,7 @@ export class Renderer {
});
});

passEncoder.endPass();
passEncoder.end();
this.device.queue.submit([commandEncoder.finish()]);

this.stats.end();
Expand Down Expand Up @@ -161,14 +161,14 @@ export async function createRenderer(canvas: HTMLCanvasElement) {
const adapter = await entry.requestAdapter();
const device = await adapter!.requestDevice();
const context = canvas.getContext('webgpu');
const format = context!.getPreferredFormat(adapter!);

canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;

const format = navigator.gpu.getPreferredCanvasFormat();
context!.configure({
device,
format,
size: [
canvas.clientWidth * devicePixelRatio,
canvas.clientHeight * devicePixelRatio,
],
});
return new Renderer(canvas, device, context!, format);
}
3 changes: 2 additions & 1 deletion src/renderer/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class Scene {
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, // eslint-disable-line no-bitwise
});
const eyeBuffer = device.createBuffer({
size: 4 * 3,
size: 4 * 4,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, // eslint-disable-line no-bitwise
});
const cameraBindGroup = device.createBindGroup({
Expand Down Expand Up @@ -177,6 +177,7 @@ export default class Scene {
}
});
}

(this.presetCamera || this.userCamera).update(device, passEncoder);
}

Expand Down
48 changes: 24 additions & 24 deletions src/shaders/frag.wgsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,40 @@ export default function frag(primitive: Primitive, material: any) {

return /* wgsl */ `

[[block]] struct Camera
struct Camera
{
eye: vec3<f32>;
eye: vec4<f32>,
};
[[group(0), binding(1)]] var<uniform> camera: Camera;
@group(0) @binding(1) var<uniform> camera: Camera;

${
baseColorTexture
? `[[group(1), binding(1)]] var texSampler: sampler;
[[group(1), binding(2)]] var tex: texture_2d<f32>; /* wgsl */`
? `@group(1) @binding(1) var texSampler: sampler;
@group(1) @binding(2) var tex: texture_2d<f32>; /* wgsl */`
: ''
}
${
metallicRoughnessTexture
? `[[group(1), binding(3)]] var metalRoughSampler: sampler;
[[group(1), binding(4)]] var metalRoughTex: texture_2d<f32>; /* wgsl */`
? `@group(1) @binding(3) var metalRoughSampler: sampler;
@group(1) @binding(4) var metalRoughTex: texture_2d<f32>; /* wgsl */`
: ''
}
${
normalTexture
? `[[group(1), binding(5)]] var normalSampler: sampler;
[[group(1), binding(6)]] var normalTex: texture_2d<f32>; /* wgsl */`
? `@group(1) @binding(5) var normalSampler: sampler;
@group(1) @binding(6) var normalTex: texture_2d<f32>; /* wgsl */`
: ''
}
${
occlusionTexture
? `[[group(1), binding(7)]] var occlusionSampler: sampler;
[[group(1), binding(8)]] var occlusionTex: texture_2d<f32>; /* wgsl */`
? `@group(1) @binding(7) var occlusionSampler: sampler;
@group(1) @binding(8) var occlusionTex: texture_2d<f32>; /* wgsl */`
: ''
}
${
emissiveTexture
? `[[group(1), binding(9)]] var emissiveSampler: sampler;
[[group(1), binding(10)]] var emissiveTex: texture_2d<f32>; /* wgsl */`
? `@group(1) @binding(9) var emissiveSampler: sampler;
@group(1) @binding(10) var emissiveTex: texture_2d<f32>; /* wgsl */`
: ''
}

Expand All @@ -74,7 +74,7 @@ export default function frag(primitive: Primitive, material: any) {
return vec4<f32>(pow(color.rgb, vec3<f32>(2.2)), color.a);
}

let pi: f32 = 3.141592653589793;
const pi: f32 = 3.141592653589793;

fn blinnPhong(color: vec3<f32>,
l: vec3<f32>,
Expand Down Expand Up @@ -127,30 +127,30 @@ export default function frag(primitive: Primitive, material: any) {
return ndotl * (diffuse + specular) * 2.0 + color * 0.1;
}

[[stage(fragment)]]
fn main([[location(0)]] vNormal: vec3<f32>,
[[location(1)]] worldPos: vec3<f32>,
@fragment
fn main(@location(0) vNormal: vec3<f32>,
@location(1) worldPos: vec3<f32>,
${
hasUV
? `[[location(${(location += 1)})]] uv: vec2<f32>, /* wgsl */`
? `@location(${(location += 1)}) uv: vec2<f32>, /* wgsl */`
: ''
}
${
hasUV1
? `[[location(${(location += 1)})]] uv1: vec2<f32>, /* wgsl */`
? `@location(${(location += 1)}) uv1: vec2<f32>, /* wgsl */`
: ''
}
${
hasTangent
? `[[location(${(location += 1)})]] tangent: vec3<f32>,
[[location(${(location += 1)})]] bitangent: vec3<f32>, /* wgsl */`
? `@location(${(location += 1)}) tangent: vec3<f32>,
@location(${(location += 1)}) bitangent: vec3<f32>, /* wgsl */`
: ''
}
${
hasVertexColor
? `[[location(${(location += 1)})]] vColor: vec4<f32>, /* wgsl */`
? `@location(${(location += 1)}) vColor: vec4<f32>, /* wgsl */`
: ''
}) -> [[location(0)]] vec4<f32>
}) -> @location(0) vec4<f32>
{
let lightDir = normalize(vec3<f32>(2.0, 4.0, 3.0));

Expand Down Expand Up @@ -212,7 +212,7 @@ export default function frag(primitive: Primitive, material: any) {
: ''
}

let viewDir = normalize(camera.eye - worldPos);
let viewDir = normalize(camera.eye.xyz - worldPos);
${
material.doubleSided
? `if (dot(normal, viewDir) < 0.0)
Expand Down
48 changes: 24 additions & 24 deletions src/shaders/vert.wgsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,72 @@ export default function vert(primitive: Primitive, instanceCount: number) {

return /* wgsl */ `

[[block]] struct Camera
struct Camera
{
projView: mat4x4<f32>;
projView: mat4x4<f32>,
};
[[group(0), binding(0)]] var<uniform> camera: Camera;
@group(0) @binding(0) var<uniform> camera: Camera;

struct Model {
matrix: mat4x4<f32>;
invTr: mat4x4<f32>;
matrix: mat4x4<f32>,
invTr: mat4x4<f32>,
};
[[block]] struct Models
struct Models
{
model: [[stride(128)]] array<Model, ${instanceCount}>;
model: array<Model, ${instanceCount}>,
};
[[group(1), binding(0)]] var<uniform> models: Models;
@group(1) @binding(0) var<uniform> models: Models;

struct VertexOutput
{
[[builtin(position)]] position: vec4<f32>;
[[location(0)]] normal: vec3<f32>;
[[location(1)]] worldPos: vec3<f32>;
@builtin(position) position: vec4<f32>,
@location(0) normal: vec3<f32>,
@location(1) worldPos: vec3<f32>,
${
hasUV
? `[[location(${(outLocation += 1)})]] uv: vec2<f32>; /* wgsl */`
? `@location(${(outLocation += 1)}) uv: vec2<f32>, /* wgsl */`
: ''
}
${
hasUV1
? `[[location(${(outLocation += 1)})]] uv1: vec2<f32>; /* wgsl */`
? `@location(${(outLocation += 1)}) uv1: vec2<f32>, /* wgsl */`
: ''
}
${
hasTangent
? `[[location(${(outLocation += 1)})]] tangent: vec3<f32>;
[[location(${(outLocation += 1)})]] bitangent: vec3<f32>; /* wgsl */`
? `@location(${(outLocation += 1)}) tangent: vec3<f32>,
@location(${(outLocation += 1)}) bitangent: vec3<f32>, /* wgsl */`
: ''
}
${
hasVertexColor
? `[[location(${(outLocation += 1)})]] color: vec4<f32>; /* wgsl */`
? `@location(${(outLocation += 1)}) color: vec4<f32>, /* wgsl */`
: ''
}
};

[[stage(vertex)]]
fn main([[builtin(instance_index)]] instanceIndex : u32,
[[location(0)]] pos: vec3<f32>,
[[location(1)]] normal: vec3<f32>,
@vertex
fn main(@builtin(instance_index) instanceIndex : u32,
@location(0) pos: vec3<f32>,
@location(1) normal: vec3<f32>,
${
hasUV
? `[[location(${(inLocation += 1)})]] uv: vec2<f32>, /* wgsl */`
? `@location(${(inLocation += 1)}) uv: vec2<f32>, /* wgsl */`
: ''
}
${
hasUV1
? `[[location(${(inLocation += 1)})]] uv1: vec2<f32>, /* wgsl */`
? `@location(${(inLocation += 1)}) uv1: vec2<f32>, /* wgsl */`
: ''
}
${
hasTangent
? `[[location(${(inLocation += 1)})]] tangent: vec4<f32>, /* wgsl */`
? `@location(${(inLocation += 1)}) tangent: vec4<f32>, /* wgsl */`
: ''
}
${
hasVertexColor
? `[[location(${(inLocation += 1)})]] color: vec4<f32>, /* wgsl */`
? `@location(${(inLocation += 1)}) color: vec4<f32>, /* wgsl */`
: ''
}) -> VertexOutput
{
Expand Down