Skip to content

Commit

Permalink
Deploying to gh-pages from @ 55d24f4 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Feb 1, 2024
1 parent dc0fcd5 commit ab600d2
Show file tree
Hide file tree
Showing 139 changed files with 603 additions and 19,490 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"cSpell.words": [
"multisampled",
"unfilterable",
"unpadded"
]
}
32 changes: 0 additions & 32 deletions CHANGELIST.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
# Change List

### 1.3.0

* Add `makeBindGroupLayoutDescriptors`

### 1.2.0

* Add `getSizeOfUnsizedArrayElement`

### 1.1.0

* Make `generateMipmap` support compatibility mode

### 1.0.0

* switch primitive functions to use named parameters.

### 0.15.0

* add `setIntrinsicsToView`

### 0.14.3

* Fixes for vec2 typos

### 0.14.2

* Handle bool issue fix

### 0.14.0

* Use latest wgsl_reflect

### 0.13.0

* Support making views of unsized arrays
Expand Down
154 changes: 13 additions & 141 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

See [here](https://greggman.github.io/webgpu-utils/docs)

* [ChangeList](https://github.com/greggman/webgpu-utils/CHANGELIST.md)
* [Migration Notes](https://github.com/greggman/webgpu-utils/migration.md)

## Random useful things for WebGPU

As I do more WebGPU I find I need more and more helpers to make things
Expand Down Expand Up @@ -42,7 +39,7 @@ const myUniformValues = makeStructuredView(defs.uniforms.myUniforms);

// create the correct sized buffer
const uniformBuffer = device.createBuffer({
size: myUniformValues.arrayBuffer.byteLength,
size: myUniformBuffer.arrayBuffer.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});

Expand Down Expand Up @@ -203,68 +200,6 @@ 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 All @@ -275,7 +210,7 @@ const pipeline = device.createRenderPipeline({

## Notes about structured data

### The first level of an array of intrinsic types is flattened by default.
### The first level of an array of intrinsic types is flattened.

Example:

Expand Down Expand Up @@ -311,28 +246,6 @@ The reason it's this way is it's common to make large arrays of `f32`, `u32`,
`vec2f`, `vec3f`, `vec4f` etc. We wouldn't want every element of an array to
have its own typedarray view.

You can configure this per type by calling `setIntrinsicsToView`.
The configuration is global. Given th example above

```js
const code = `
@group(0) @binding(0) var<uniform> uni1: array<vec3f, 4>;
@group(0) @binding(1) var<uniform> uni2: array<array<vec3f, 3>, 4>;
`;
const defs = makeShaderDataDefinitions(code);
setIntrinsicsToView(['vec3f']);
const uni1 = makeStructuredView(defs.uniforms.uni1);

uni1.set([
[1, 2, 3], // uni1[0]
[4, 5, 6], // uni1[1]
...
]);
```

Or to put it another way, in the default case, `uni1.views is a Float32Array(16)`.
In the 2nd case it's an array of 4 `Float32Array` each 3 elements big

### arrays of intrinsics can be set by arrays of arrays

```js
Expand Down Expand Up @@ -363,58 +276,20 @@ Example:

```js
const code = `
@group(0) @binding(0) var<storage> buf1: array<vec3f>; // unsized array
`;
const defs = makeShaderDataDefinitions(code);
const buf1 = makeStructuredView(defs.storages.buf1, new ArrayBuffer(4 * 16));

// buf1.views will be a Float32Array representing 4 vec3fs
```

Note: If you have a complex array element type you can call
`getSizeOfUnsizedArrayElement` to get its size. Example:

```js
const code = `
struct Light {
intensity: f32,
direction: vec3f,
};
@group(0) @binding(7) var<storage> lights: array<Light>;
@group(0) @binding(0) var<uniform> uni1: array<vec3f>; // unsized array
`;
const defs = makeShaderDataDefinitions(code);
const {size} = getSizeOfUnsizedArrayElement(defs.storages.lights);
const numLights = 4;
const buf1 = makeStructuredView(
defs.storages.lights, new ArrayBuffer(numLights * size));
```
const uni1 = makeStructuredView(defs.uniforms.uni1, new ArrayBuffer(4 * 16));

Similarly if you are using an unsized array as the last member of a struct
you might do this

```js
const code = `
struct Kernel {
amount: f32,
entries: array<vec3f>,
};
@group(0) @binding(7) var<storage> conv: Kernel;
`;
const defs = makeShaderDataDefinitions(code);
const {size: elemSize} = getSizeOfUnsizedArrayElement(defs.storages.conv);
const numKernelEntries = 4;
const size = defs.storages.conv.size + numKernelEntries * elemSize;
const buf1 = makeStructuredView(
defs.storages.conv, new ArrayBuffer(size));
)
// uni.views will be a Float32Array representing 4 vec3fs
```

## Usage

* include from the net

```js
import { createTextureFromImage } from 'https://greggman.github.io/webgpu-utils/dist/1.x/webgpu-utils.module.js'
import { createTextureFromImage } from 'https://greggman.github.io/webgpu-utils/dist/0.x/webgpu-utils.module.js'

...
```
Expand All @@ -433,25 +308,21 @@ import { createTextureFromImage } from 'webgpu-utils';
...
```

## <a id="examples"></a> Examples

* [2d-array texture](examples/2d-array.html)
* [cube](examples/cube.html)
* [cube-map](examples/cube-map.html)
* [instancing](examples/instancing.html)
* [primitives](examples/primitives.html)

## Development

```
git clone https://github.com/greggman/webgpu-utils.git
cd webgpu-utils
npm ci
npm install
npm start
```

This will run rollup in watch mode, building from typescript into
`dist/1.x/webgpu-utils.js` and start a server
`dist/0.x/webgpu-utils.js`.

```
npx servez
```

Now open [`http://localhost:8080/test/`](http://localhost:8080/test/) to run tests.

Expand All @@ -462,3 +333,4 @@ Super thanks to Brendan Duncan for [wgsl-reflect](https://github.com/brendan-dun
## License

[MIT](LICENSE.md)

16 changes: 0 additions & 16 deletions build/tools/serve.js

This file was deleted.

8 changes: 4 additions & 4 deletions dist/0.x/attribute-utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export type ArrayUnion = number | number[] | TypedArray | FullArraySpec;
* Each array can be 1 of 4 things. A native JavaScript array, a TypedArray, a number, a {@link FullArraySpec}
*
* If it's a native array then, if the name of the array is `indices` the data will be converted
* to a `Uint32Array`, otherwise a `Float32Array`. Use a TypedArray or a {@link FullArraySpec} to choose a different type.
* The {@link FullArraySpec} `type` is only used if it's not already a TypedArray
* to a `Uint32Array`, otherwise a `Float32Array. Use a TypedArray or a FullArraySpec to choose a different type.
* The FullArraySpec type is only used if it's not already a TypedArray
*
* If it's a native array or a TypedArray or if `numComponents` in a {@link FullArraySpec} is not
* specified it will be guessed. If the name contains 'coord', 'texture' or 'uv' then numComponents will be 2.
* specified it will be guess. If the name contains 'coord', 'texture' or 'uv' then numComponents will be 2.
* If the name contains 'color' or 'colour' then numComponents will be 4. Otherwise it's 3.
*
* For attribute formats, guesses are made based on type and number of components. The guess is
* For attribute formats, guesses are made based on type at number of components. The guess is
* based on this table where (d) is the default for that type if `normalize` is not specified
*
* | Type | .. | normalize |
Expand Down
54 changes: 1 addition & 53 deletions dist/0.x/buffer-views.d.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,5 @@
import { StructDefinition, TypeDefinition, VariableDefinition } from './data-definitions.js';
import { TypedArrayConstructor, TypedArray } from './typed-arrays.js';
type TypeDef = {
numElements: number;
align: number;
size: number;
type: string;
View: TypedArrayConstructor;
flatten?: boolean;
pad?: readonly number[];
};
declare const typeInfo: {
readonly [K: string]: TypeDef;
};
export type kType = Extract<keyof typeof typeInfo, string>;
export declare const kTypes: readonly kType[];
/**
* Set which intrinsic types to make views for.
*
* Example:
*
* Given a an array of intrinsics like this
* `array<vec3, 200>`
*
* The default is to create a single `Float32Array(4 * 200)`
* because creating 200 `Float32Array` views is not usually
* what you want.
*
* If you do want individual views then you'd call
* `setIntrinsicsToView(['vec3f`])` and now you get
* an array of 200 `Float32Array`s.
*
* Note: `setIntrinsicsToView` always sets ALL types. The list you
* pass it is the types you want views created for, all other types
* will be reset to do the default. In other words
*
* ```js
* setIntrinsicsToView(['vec3f`])
* setIntrinsicsToView(['vec2f`])
* ```
*
* Only `vec2f` will have views created. `vec3f` has been reset to the default by
* the second call
*
* You can pass in `true` as the 2nd parameter to make it set which types
* to flatten and all others will be set to have views created.
*
* To reset all types to the default call it with no arguments
*
* @param types array of types to make views for
* @param flatten whether to flatten or expand the specified types.
*/
export declare function setIntrinsicsToView(types?: readonly kType[], flatten?: boolean): void;
import { TypedArray } from './typed-arrays.js';
export type TypedArrayOrViews = TypedArray | Views | Views[];
export interface Views {
[x: string]: TypedArrayOrViews;
Expand Down Expand Up @@ -204,4 +153,3 @@ export declare function setTypedValues(typeDef: TypeDefinition, data: any, array
* @param offset An offset in the arrayBuffer to start at.
*/
export declare function setStructuredValues(varDef: VariableDefinition, data: any, arrayBuffer: ArrayBuffer, offset?: number): void;
export {};
4 changes: 0 additions & 4 deletions dist/0.x/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export declare const roundUpToMultipleOf: (v: number, multiple: number) => number;
export declare function keysOf<T extends string>(obj: {
[k in T]: unknown;
}): readonly T[];
export declare function range<T>(count: number, fn: (i: number) => T): T[];
Loading

0 comments on commit ab600d2

Please sign in to comment.