Skip to content

Commit 41327a4

Browse files
authored
Fix Call commands for print shaders and general cleanup (#95)
* improve Error handling and prevent corrupting global scope * improve runnable entry point code * Make formatting, semicolon usage, and let usage consistent
1 parent c727896 commit 41327a4

File tree

9 files changed

+721
-909
lines changed

9 files changed

+721
-909
lines changed

compiler.ts

Lines changed: 89 additions & 87 deletions
Large diffs are not rendered by default.

compute.ts

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Bindings } from "./compiler";
22
import { ThreadGroupSize } from "./slang-wasm";
33

4-
export class ComputePipeline
5-
{
4+
export class ComputePipeline {
65
pipeline: GPUComputePipeline | undefined;
76
pipelineLayout: GPUPipelineLayout | "auto" | undefined;
87

@@ -16,28 +15,24 @@ export class ComputePipeline
1615
bindGroup: GPUBindGroup | undefined;
1716

1817
// thread group size (array of 3 integers)
19-
threadGroupSize: ThreadGroupSize | { x: number, y: number, z: number} | undefined;
18+
threadGroupSize: ThreadGroupSize | { x: number, y: number, z: number } | undefined;
2019

2120
// resource name (string) -> binding descriptor
2221
resourceBindings: Bindings | undefined;
2322

24-
constructor(device: GPUDevice)
25-
{
23+
constructor(device: GPUDevice) {
2624
this.device = device;
2725
}
2826

29-
setThreadGroupSize(size: ThreadGroupSize | { x: number, y: number, z: number})
30-
{
27+
setThreadGroupSize(size: ThreadGroupSize | { x: number, y: number, z: number }) {
3128
this.threadGroupSize = size;
3229
}
3330

34-
createPipelineLayout(resourceDescriptors: Bindings)
35-
{
31+
createPipelineLayout(resourceDescriptors: Bindings) {
3632
this.resourceBindings = resourceDescriptors;
3733

3834
const entries: GPUBindGroupLayoutEntry[] = [];
39-
for (const [name, binding] of this.resourceBindings)
40-
{
35+
for (const [name, binding] of this.resourceBindings) {
4136
entries.push(binding);
4237
}
4338
const bindGroupLayoutDescriptor: GPUBindGroupLayoutDescriptor = {
@@ -46,83 +41,73 @@ export class ComputePipeline
4641
};
4742

4843
const bindGroupLayout = this.device.createBindGroupLayout(bindGroupLayoutDescriptor);
49-
const layout = this.device.createPipelineLayout({bindGroupLayouts: [bindGroupLayout]});
44+
const layout = this.device.createPipelineLayout({ bindGroupLayouts: [bindGroupLayout] });
5045

5146
this.pipelineLayout = layout;
5247
}
5348

54-
createPipeline(shaderModule: GPUShaderModule, resources: Map<string, GPUTexture | GPUBuffer> | null)
55-
{
56-
if(this.pipelineLayout == undefined)
57-
throw new Error("Cannot create pipeline without layout")
49+
createPipeline(shaderModule: GPUShaderModule, resources: Map<string, GPUTexture | GPUBuffer> | null) {
50+
if (this.pipelineLayout == undefined)
51+
throw new Error("Cannot create pipeline without layout");
5852
const pipeline = this.device.createComputePipeline({
5953
label: 'compute pipeline',
6054
layout: this.pipelineLayout,
61-
compute: {module: shaderModule},
62-
});
55+
compute: { module: shaderModule },
56+
});
6357

6458
this.pipeline = pipeline;
65-
59+
6660
// If resources are provided, create the bind group right away
6761
if (resources)
6862
this.createBindGroup(resources);
6963
}
7064

71-
createBindGroup(allocatedResources: Map<string, GPUObjectBase>)
72-
{
73-
if(this.resourceBindings == undefined)
74-
throw new Error("No resource bindings")
75-
if(this.pipeline == undefined)
76-
throw new Error("No pipeline")
65+
createBindGroup(allocatedResources: Map<string, GPUObjectBase>) {
66+
if (this.resourceBindings == undefined)
67+
throw new Error("No resource bindings");
68+
if (this.pipeline == undefined)
69+
throw new Error("No pipeline");
7770

7871
const entries: GPUBindGroupEntry[] = [];
79-
for (const [name, resource] of allocatedResources)
80-
{
72+
for (const [name, resource] of allocatedResources) {
8173
const bindInfo = this.resourceBindings.get(name);
82-
83-
if (bindInfo)
84-
{
85-
if (bindInfo.buffer)
86-
{
87-
if(!(resource instanceof GPUBuffer)) {
88-
throw new Error("Invalid state")
74+
75+
if (bindInfo) {
76+
if (bindInfo.buffer) {
77+
if (!(resource instanceof GPUBuffer)) {
78+
throw new Error("Invalid state");
8979
}
90-
entries.push({binding: bindInfo.binding, resource: {buffer: resource}});
80+
entries.push({ binding: bindInfo.binding, resource: { buffer: resource } });
9181
}
92-
else if (bindInfo.storageTexture)
93-
{
94-
if(!(resource instanceof GPUTexture)) {
95-
throw new Error("Invalid state")
82+
else if (bindInfo.storageTexture) {
83+
if (!(resource instanceof GPUTexture)) {
84+
throw new Error("Invalid state");
9685
}
97-
entries.push({binding: bindInfo.binding, resource: resource.createView()});
86+
entries.push({ binding: bindInfo.binding, resource: resource.createView() });
9887
}
99-
else if (bindInfo.texture)
100-
{
101-
if(!(resource instanceof GPUTexture)) {
102-
throw new Error("Invalid state")
88+
else if (bindInfo.texture) {
89+
if (!(resource instanceof GPUTexture)) {
90+
throw new Error("Invalid state");
10391
}
104-
entries.push({binding: bindInfo.binding, resource: resource.createView()});
92+
entries.push({ binding: bindInfo.binding, resource: resource.createView() });
10593
}
10694
}
10795
}
10896

10997
// Check that all resources are bound
110-
if (entries.length != this.resourceBindings.size)
111-
{
98+
if (entries.length != this.resourceBindings.size) {
11299
let missingEntries = []
113100
// print out the names of the resources that aren't bound
114-
for (const [name, resource] of this.resourceBindings)
115-
{
101+
for (const [name, resource] of this.resourceBindings) {
116102
missingEntries = []
117-
if (!entries.find(entry => entry.binding == resource.binding))
118-
{
103+
if (!entries.find(entry => entry.binding == resource.binding)) {
119104
missingEntries.push(name);
120105
}
121106
}
122107

123108
throw new Error("Cannot create bind-group. The following resources are not bound: " + missingEntries.join(", "));
124109
}
125-
110+
126111
this.bindGroup = this.device.createBindGroup({
127112
layout: this.pipeline.getBindGroupLayout(0),
128113
entries: entries,

image_demo.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)