Skip to content

Commit b9607e6

Browse files
committed
make able to init without canvas
1 parent f840f87 commit b9607e6

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Lil as Lil_ } from "./src/Lil.ts"
66
import { initCanvas } from "./src/GpuWrapper.ts"
77

88
export abstract class Lil extends Lil_ {
9-
async init(canvas: HTMLCanvasElement) {
9+
async init(canvas?: HTMLCanvasElement) {
1010
const wrapper = await initCanvas({
1111
...this,
1212
canvas,

example/browser/MatMul.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class MatrixMul extends Lil {
7171
}
7272

7373
const matrixMul = new MatrixMul()
74-
const g = await matrixMul.init(document.querySelector("canvas")!)
74+
const g = await matrixMul.init()
7575

7676
g.compute(
7777
Math.ceil(matrixMul.firstMatrix.size.x / WORKGROUP_SIZE[0]),

src/DenoGpuWrapper.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ export class DenoGpuWrapper<T extends Layout> extends GpuWrapper<T> {
1616

1717
constructor(
1818
root: TgpuRoot,
19-
info: GpuWrapperInfo<T> & { width: number, height: number },
19+
info: GpuWrapperInfo<T> & { width?: number, height?: number },
2020
outputBuffer: GPUBuffer,
2121
) {
2222
super(root, info)
2323
this.outputBuffer = outputBuffer
24-
this.dimension = {
24+
if (info.width && info.height) this.dimension = {
2525
width: info.width,
2626
height: info.height,
2727
}
2828
}
2929

3030
override beforeDrawFinish(commandEncoder: GPUCommandEncoder) {
31+
if (!this.getTexture || !this.dimension) {
32+
throw new Error("No texture provided")
33+
}
3134
copyToBuffer(
3235
commandEncoder,
3336
this.getTexture(),
@@ -36,6 +39,9 @@ export class DenoGpuWrapper<T extends Layout> extends GpuWrapper<T> {
3639
)
3740
}
3841
async getImage() {
42+
if (!this.dimension) {
43+
throw new Error("No texture provided")
44+
}
3945
return await createPng(
4046
this.outputBuffer,
4147
this.dimension,

src/GpuWrapper.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import {
1212
} from "./types.ts"
1313

1414
export interface GpuWrapperInfo<T extends Layout> {
15-
getTexture(): GPUTexture,
15+
getTexture?(): GPUTexture,
1616
format?: GPUTextureFormat,
1717
vertShader?: string,
1818
fragShader?: string,
1919
layout?: T,
2020
}
2121
export interface GpuWrapperInfo<T extends Layout> {
22-
getTexture(): GPUTexture,
22+
getTexture?(): GPUTexture,
2323
format?: GPUTextureFormat,
2424
compShader?: string,
2525
layout?: T,
@@ -128,6 +128,9 @@ export class GpuWrapper<T extends Layout> {
128128
}
129129
beforeDrawFinish(_commandEncoder: GPUCommandEncoder) {}
130130
draw(...params: Parameters<GPURenderPassEncoder["draw"]>) {
131+
if (!this.getTexture) {
132+
throw new Error("No texture provided")
133+
}
131134
const textureView = this.getTexture().createView()
132135
const renderPassDescriptor: GPURenderPassDescriptor = {
133136
colorAttachments: [
@@ -164,8 +167,15 @@ export class GpuWrapper<T extends Layout> {
164167
}
165168

166169
export async function initCanvas<T extends Layout>
167-
(info: Omit<GpuWrapperInfo<T>, "getTexture"> & { canvas: HTMLCanvasElement }) {
170+
(info: Omit<GpuWrapperInfo<T>, "getTexture"> & { canvas?: HTMLCanvasElement }) {
168171
const root = await tgpu.init()
172+
173+
if (!info.canvas) {
174+
return new GpuWrapper(
175+
root,
176+
info,
177+
)
178+
}
169179
const ctx = info.canvas.getContext("webgpu") as unknown as GPUCanvasContext
170180

171181
ctx.configure({

0 commit comments

Comments
 (0)