-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathraytracer.ts
More file actions
107 lines (100 loc) · 2.89 KB
/
raytracer.ts
File metadata and controls
107 lines (100 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import raytracerWGSL from './raytracer.wgsl';
import Common from './common';
import Radiosity from './radiosity';
/**
* Raytracer renders the scene using a software ray-tracing compute pipeline.
*/
export default class Raytracer {
private readonly common: Common;
private readonly framebuffer: GPUTexture;
private readonly pipeline: GPUComputePipeline;
private readonly bindGroup: GPUBindGroup;
private readonly kWorkgroupSizeX = 16;
private readonly kWorkgroupSizeY = 16;
constructor(
device: GPUDevice,
common: Common,
radiosity: Radiosity,
framebuffer: GPUTexture
) {
this.common = common;
this.framebuffer = framebuffer;
const bindGroupLayout = device.createBindGroupLayout({
label: 'Raytracer.bindGroupLayout',
entries: [
{
// lightmap
binding: 0,
visibility: GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE,
texture: { viewDimension: '2d-array' },
},
{
// sampler
binding: 1,
visibility: GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE,
sampler: {},
},
{
// framebuffer
binding: 2,
visibility: GPUShaderStage.COMPUTE,
storageTexture: {
access: 'write-only',
format: framebuffer.format,
viewDimension: '2d',
},
},
],
});
this.bindGroup = device.createBindGroup({
label: 'rendererBindGroup',
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: radiosity.lightmap,
},
{
binding: 1,
resource: device.createSampler({
addressModeU: 'clamp-to-edge',
addressModeV: 'clamp-to-edge',
addressModeW: 'clamp-to-edge',
magFilter: 'linear',
minFilter: 'linear',
}),
},
{
binding: 2,
resource: framebuffer,
},
],
});
this.pipeline = device.createComputePipeline({
label: 'raytracerPipeline',
layout: device.createPipelineLayout({
bindGroupLayouts: [common.uniforms.bindGroupLayout, bindGroupLayout],
}),
compute: {
module: device.createShaderModule({
code: raytracerWGSL + common.wgsl,
}),
constants: {
WorkgroupSizeX: this.kWorkgroupSizeX,
WorkgroupSizeY: this.kWorkgroupSizeY,
},
},
});
}
run(commandEncoder: GPUCommandEncoder) {
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(this.pipeline);
passEncoder.setBindGroup(0, this.common.uniforms.bindGroup);
passEncoder.setBindGroup(1, this.bindGroup);
passEncoder.dispatchWorkgroups(
Math.ceil(this.framebuffer.width / this.kWorkgroupSizeX),
Math.ceil(this.framebuffer.height / this.kWorkgroupSizeY)
);
passEncoder.end();
}
}