-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix scissor (#35) * fix: size in storage bindings can be undefined * chore: commit changeset * chore(release): bump version (#36) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
173aa2e
commit afa93c4
Showing
6 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
DeviceContribution, | ||
VertexStepMode, | ||
Format, | ||
TransparentWhite, | ||
Buffer, | ||
Bindings, | ||
BufferUsage, | ||
} from '../../src'; | ||
import { initExample } from './utils'; | ||
|
||
/** | ||
* Use Compute Shader with WebGPU | ||
* @see https://webgpu.github.io/webgpu-samples/samples/computeBoids#main.ts | ||
*/ | ||
|
||
export async function render( | ||
deviceContribution: DeviceContribution, | ||
$canvas: HTMLCanvasElement, | ||
useRAF = true, | ||
) { | ||
// create swap chain and get device | ||
const swapChain = await deviceContribution.createSwapChain($canvas); | ||
swapChain.configureSwapChain($canvas.width, $canvas.height); | ||
const device = swapChain.getDevice(); | ||
|
||
const computeProgram = device.createProgram({ | ||
compute: { | ||
wgsl: ` | ||
`, | ||
}, | ||
}); | ||
|
||
const program = device.createProgram({ | ||
compute: { | ||
wgsl: ` | ||
@binding(0) @group(0) var<storage, read_write> input : array<i32>; | ||
@binding(1) @group(0) var<storage, read_write> output : array<i32>; | ||
@compute @workgroup_size(8, 8) | ||
fn main( | ||
@builtin(global_invocation_id) global_id : vec3<u32> | ||
) { | ||
var index = global_id.x; | ||
output[index] = input[index] + output[index]; | ||
} | ||
`, | ||
}, | ||
}); | ||
|
||
const inputBuffer = device.createBuffer({ | ||
usage: BufferUsage.STORAGE | BufferUsage.COPY_SRC, | ||
viewOrSize: new Int32Array([1, 2, 3, 4]), | ||
}); | ||
const outputBuffer = device.createBuffer({ | ||
usage: BufferUsage.STORAGE | BufferUsage.COPY_SRC, | ||
viewOrSize: new Int32Array([1, 2, 3, 4]), | ||
}); | ||
|
||
const pipeline = device.createComputePipeline({ | ||
inputLayout: null, | ||
program, | ||
}); | ||
const bindings = device.createBindings({ | ||
pipeline, | ||
storageBufferBindings: [ | ||
{ | ||
binding: 0, | ||
buffer: inputBuffer, | ||
}, | ||
{ | ||
binding: 1, | ||
buffer: outputBuffer, | ||
}, | ||
], | ||
}); | ||
|
||
const computePass = device.createComputePass(); | ||
computePass.setPipeline(pipeline); | ||
computePass.setBindings(bindings); | ||
computePass.dispatchWorkgroups(1); | ||
device.submitPass(computePass); | ||
|
||
const readback = device.createReadback(); | ||
const output = await readback.readBuffer(outputBuffer); | ||
console.log(output); | ||
|
||
return () => { | ||
program.destroy(); | ||
device.destroy(); | ||
|
||
// For debug. | ||
device.checkForLeaks(); | ||
}; | ||
} | ||
|
||
export async function Add2Vectors($container: HTMLDivElement) { | ||
return initExample($container, render, { | ||
targets: ['webgpu'], | ||
default: 'webgpu', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters