Skip to content

Commit

Permalink
Deploying to gh-pages from @ 47f4665 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Jan 2, 2024
1 parent 6dc49c4 commit dc0fcd5
Show file tree
Hide file tree
Showing 98 changed files with 2,893 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"cSpell.words": [
"multisampled",
"unfilterable",
"unpadded"
]
}
4 changes: 4 additions & 0 deletions CHANGELIST.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change List

### 1.3.0

* Add `makeBindGroupLayoutDescriptors`

### 1.2.0

* Add `getSizeOfUnsizedArrayElement`
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,68 @@ passEncoder.setIndexBuffer(bi.indexBuffer, bi.indexFormat);
passEncoder.drawIndexed(bi.numElements);
```

### Create `GPUBindGroupLayoutDescriptors` from WGSL code

```js
import {
makeShaderDataDefinitions,
makeBindGroupLayoutDescriptors,
} from 'webgpu-utils';

const code = `
@group(0) @binding(0) var<uniform> mat: mat4x4f;
struct MyVSOutput {
@builtin(position) position: vec4f,
@location(1) texcoord: vec2f,
};
@vertex
fn myVSMain(v: MyVSInput) -> MyVSOutput {
var vsOut: MyVSOutput;
vsOut.position = mat * v.position;
vsOut.texcoord = v.texcoord;
return vsOut;
}
@group(0) @binding(2) var diffuseSampler: sampler;
@group(0) @binding(3) var diffuseTexture: texture_2d<f32>;
@fragment
fn myFSMain(v: MyVSOutput) -> @location(0) vec4f {
return textureSample(diffuseTexture, diffuseSampler, v.texcoord);
}
`;

const module = device.createShaderModule({code});
const defs = wgh.makeShaderDataDefinitions(code);

const pipelineDesc = {
vertex: {
module,
entryPoint: 'myVSMain',
buffers: bufferLayouts,
},
fragment: {
module,
entryPoint: 'myFSMain',
targets: [
{format: presentationFormat},
],
},
};

const descriptors = wgh.makeBindGroupLayoutDescriptors(defs, pipelineDesc);
const group0Layout = device.createBindGroupLayout(descriptors[0]);
const layout = device.createPipelineLayout({
bindGroupLayouts: [group0Layout],
});
const pipeline = device.createRenderPipeline({
layout,
...pipelineDesc,
});
```

## Examples:

* [Cube](examples/cube.html)
Expand Down
4 changes: 2 additions & 2 deletions dist/1.x/buffer-views.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export declare function setStructuredValues(varDef: VariableDefinition, data: an
*
* Generally you only need size. Example:
*
*
* ```js
* const code = `
* struct Foo {
Expand All @@ -246,7 +245,8 @@ export declare function setStructuredValues(varDef: VariableDefinition, data: an
* defs.storages.f,
* new ArrayBuffer(defs.storages.f.size + size * numElements));
* ```
* @param varDef A variable definition provided by @link {makeShaderDataDefinitions}
*
* @param varDef A variable definition provided by @link {makeShaderDataDefinitions}
* @returns the size, align, and unalignedSize in bytes of the unsized array element in this type definition.
* If there is no unsized array, size = 0.
*/
Expand Down
50 changes: 50 additions & 0 deletions dist/1.x/data-definitions.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="dist" />
export type FieldDefinition = {
offset: number;
type: TypeDefinition;
Expand Down Expand Up @@ -35,11 +36,60 @@ export type StructDefinitions = {
export type VariableDefinitions = {
[x: string]: VariableDefinition;
};
export type Resource = {
name: string;
group: number;
entry: GPUBindGroupLayoutEntry;
};
export type EntryPoint = {
stage: GPUShaderStageFlags;
resources: Resource[];
};
export type EntryPoints = {
[x: string]: EntryPoint;
};
type ShaderDataDefinitions = {
uniforms: VariableDefinitions;
storages: VariableDefinitions;
structs: StructDefinitions;
entryPoints: EntryPoints;
};
/**
* This should be compatible with GPUProgramableStage
*/
export type ProgrammableStage = {
entryPoint?: string;
};
/**
* Compatible with GPURenderPipelineDescriptor and GPUComputePipelineDescriptor
*/
export type PipelineDescriptor = {
vertex?: ProgrammableStage;
fragment?: ProgrammableStage;
compute?: ProgrammableStage;
};
/**
* Gets GPUBindGroupLayoutDescriptors for the given pipeline.
*
* Important: Assumes you pipeline is valid (it doesn't check for errors).
*
* Note: In WebGPU some layouts must be specified manually. For example an unfiltered-float
* sampler can not be derived since it is unknown at compile time pipeline creation time
* which texture you'll use.
*
* MAINTENANCE_TODO: Add example
*
* @param defs ShaderDataDefinitions or an array of ShaderDataDefinitions as
* returned from @link {makeShaderDataDefinitions}. If an array more than 1
* definition it's assumed the vertex shader is in the first and the fragment
* shader in the second.
* @param desc A PipelineDescriptor. You should be able to pass in the same object you passed
* to `createRenderPipeline` or `createComputePipeline`.
* @returns An array of GPUBindGroupLayoutDescriptors which you can pass, one at a time, to
* `createBindGroupLayout`. Note: the array will be sparse if there are gaps in group
* numbers. Note: Each GPUBindGroupLayoutDescriptor.entries will be sorted by binding.
*/
export declare function makeBindGroupLayoutDescriptors(defs: ShaderDataDefinitions | ShaderDataDefinitions[], desc: PipelineDescriptor): GPUBindGroupLayoutDescriptor[];
/**
* Given a WGSL shader, returns data definitions for structures,
* uniforms, and storage buffers
Expand Down
Loading

0 comments on commit dc0fcd5

Please sign in to comment.