-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlight.ts
180 lines (155 loc) · 6.06 KB
/
light.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Buffer A (Buffer A, keyboard, RGBA Noise Medium) (//Stores vars)
Buffer B (Buffer A, Buffer b, RGBA Noise Medium, CubeMap a) (Temporal ReSTIR)
Buffer C (Buffer A, Buffer B, RGBA Noise Medium, CubeMap a
Buffer D (Buffer A, Buffer B, Buffer C, Buffer D) (contains lightning, Temporal accumulation)
CubeMap A (Buffer A, Nothing , RGBA Noise Medium, CubeMap A) (scene storage)
Image.frag (Buffer A, Buffer C, Buffer D, CubeMap A)
*/
import {GPU} from "../webgpu/gpu";
import {Texture} from "../webgpu/texture";
import {Buffer} from "../webgpu/buffer";
import {GPUAbstractRunner, RunnerType} from "../AbstractGPURunner";
import {Render} from "../render/render";
import {LightScene} from "./scene/scene";
import {ShowError} from "../ui";
export class LightPropagation extends GPUAbstractRunner {
width: number
height: number
render: Render
scene: LightScene
textureDest: Texture
textureSrc: Texture
bind_group_layout: GPUBindGroupLayout
bind_group: GPUBindGroup
scene_bind_group_layout: GPUBindGroupLayout
scene_bind_group: GPUBindGroup
pipeline_layout: GPUPipelineLayout
compute_pipeline: GPUComputePipeline
shader: GPUProgrammableStage
stagingBuffer: Buffer
stagingData: Float32Array
constructor() {
super()
this.width = GPU.viewport.width
this.height = GPU.viewport.height
}
getType(): RunnerType {
return RunnerType.ASYNCANIM
}
async Destroy() {
this.textureDest.destroy()
this.textureSrc.destroy()
}
async Init() {
console.log("Create Texture")
this.textureDest = GPU.CreateStorageTextureArray(this.width, this.height, 3, "rgba16float")
this.textureSrc = GPU.CreateStorageTextureArray(this.width, this.height, 3, "rgba16float")
this.stagingBuffer = GPU.CreateUniformBuffer(4 * 4) // must be a multiple of 16 bytes
this.stagingData = new Float32Array(4)
this.scene = new LightScene(this.stagingBuffer)
try {
await this.scene.Init()
} catch (e) {
ShowError("Creation of Scene failed", e as Error)
throw e
}
console.log("Create Render")
this.render = new Render(
[this.textureDest, this.scene.emitter],
[],
"scripts/light/common.wgsl", "scripts/light/distance.wgsl", "scripts/light/aces-tone-mapping.wgsl")
await this.render.Init()
this.shader = await GPU.CreateShaderFromURL("scripts/light/common.wgsl", "scripts/light/distance.wgsl", "scripts/light/propagate.wgsl")
this.bind_group_layout = GPU.device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
texture: {
sampleType: "unfilterable-float",
viewDimension: "2d-array"
}
}, {
binding: 1,
visibility: GPUShaderStage.COMPUTE,
storageTexture: {
access: "write-only",
format: this.textureDest.format,
viewDimension: "2d-array"
}
}, {
binding: 2,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: "uniform"
}
}]
})
this.bind_group = GPU.device.createBindGroup({
layout: this.bind_group_layout,
entries: [{
binding: 0,
resource: this.textureSrc.textureView
},{
binding: 1,
resource: this.textureDest.textureView
}, {
binding: 2,
resource: this.stagingBuffer.resource
}]
})
this.scene_bind_group_layout = GPU.device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
texture: {
sampleType: "unfilterable-float",
viewDimension: "2d-array"
}
}]
})
this.scene_bind_group = GPU.device.createBindGroup({
layout: this.scene_bind_group_layout,
entries: [{
binding: 0,
resource: this.scene.emitter.textureView
}]
})
this.pipeline_layout = GPU.device.createPipelineLayout({
bindGroupLayouts: [this.bind_group_layout, this.scene_bind_group_layout]
})
this.compute_pipeline = GPU.device.createComputePipeline({
layout: this.pipeline_layout,
compute: this.shader
})
}
GetCommandBuffer(): GPUCommandBuffer {
this.stagingData[0] = GPU.mouseCoordinate.x; // set iMouseX
this.stagingData[1] = GPU.mouseCoordinate.y; // set iMouseY
this.stagingData[2] = GPU.mouseCoordinate.wheel;
this.stagingData[3] += 1.; // increase iFrame
GPU.device.queue.writeBuffer(this.stagingBuffer.buffer, 0, this.stagingData)
let encoder: GPUCommandEncoder = GPU.CreateCommandEncoder();
for(let i = 0; i < 10; i++) {
let pass: GPUComputePassEncoder = encoder.beginComputePass();
pass.setBindGroup(0, this.bind_group);
pass.setBindGroup(1, this.scene_bind_group);
pass.setPipeline(this.compute_pipeline);
pass.dispatchWorkgroups(this.width / 8, this.height / 8);
pass.end();
encoder.copyTextureToTexture(
{texture: this.textureDest.texture},
{texture: this.textureSrc.texture},
[this.width, this.height, this.textureSrc.depth])
}
return GPU.FinishCommandEncoder(encoder)
}
async Run() {
GPU.device.queue.submit([this.scene.GetCommandBuffer(), this.GetCommandBuffer()]);
}
Render() {
GPU.device.queue.submit([this.render.getCommandBuffer()]);
}
}