Skip to content

Commit 9440d93

Browse files
committed
feat: split renderPipeline and computePipeline
1 parent f3e1fa7 commit 9440d93

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/GpuWrapper.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export class GpuWrapper<T extends Layout> {
2929
root
3030
getTexture
3131
format
32-
pipeline
32+
renderPipeline
33+
computePipeline
3334
bindGroup
3435
buffers
3536

@@ -77,7 +78,7 @@ export class GpuWrapper<T extends Layout> {
7778
this.buffers as any,
7879
)
7980
if (vertShader && fragShader) {
80-
this.pipeline = device.createRenderPipeline({
81+
this.renderPipeline = device.createRenderPipeline({
8182
layout: device.createPipelineLayout({
8283
bindGroupLayouts: [root.unwrap(bgLayout)]
8384
}),
@@ -106,8 +107,9 @@ export class GpuWrapper<T extends Layout> {
106107
topology: "triangle-strip",
107108
},
108109
})
109-
} else if (compShader) {
110-
this.pipeline = device.createComputePipeline({
110+
}
111+
if (compShader) {
112+
this.computePipeline = device.createComputePipeline({
111113
layout: device.createPipelineLayout({
112114
bindGroupLayouts: [root.unwrap(bgLayout)]
113115
}),
@@ -122,15 +124,16 @@ export class GpuWrapper<T extends Layout> {
122124
})
123125
},
124126
})
125-
} else {
126-
throw new Error("Need shader")
127127
}
128128
}
129129
beforeDrawFinish(_commandEncoder: GPUCommandEncoder) {}
130130
draw(...params: Parameters<GPURenderPassEncoder["draw"]>) {
131131
if (!this.getTexture) {
132132
throw new Error("No texture provided")
133133
}
134+
if (!this.renderPipeline) {
135+
throw new Error("No renderPipeline")
136+
}
134137
const textureView = this.getTexture().createView()
135138
const renderPassDescriptor: GPURenderPassDescriptor = {
136139
colorAttachments: [
@@ -145,7 +148,7 @@ export class GpuWrapper<T extends Layout> {
145148

146149
const commandEncoder = this.root.device.createCommandEncoder()
147150
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor)
148-
passEncoder.setPipeline(this.pipeline)
151+
passEncoder.setPipeline(this.renderPipeline)
149152
passEncoder.setBindGroup(0, this.root.unwrap(this.bindGroup))
150153
passEncoder.draw(...params)
151154
passEncoder.end()
@@ -155,9 +158,12 @@ export class GpuWrapper<T extends Layout> {
155158
this.root.device.queue.submit([commandEncoder.finish()])
156159
}
157160
compute(x: number, y?: number, z?: number) {
161+
if (!this.computePipeline) {
162+
throw new Error("No computePipeline")
163+
}
158164
const encoder = this.root.device.createCommandEncoder()
159165
const pass = encoder.beginComputePass()
160-
pass.setPipeline(this.pipeline)
166+
pass.setPipeline(this.computePipeline)
161167
pass.setBindGroup(0, this.root.unwrap(this.bindGroup))
162168
pass.dispatchWorkgroups(x, y, z)
163169
pass.end()

0 commit comments

Comments
 (0)