-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathtonemapper.ts
More file actions
96 lines (91 loc) · 2.48 KB
/
tonemapper.ts
File metadata and controls
96 lines (91 loc) · 2.48 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
import Common from './common';
import tonemapperWGSL from './tonemapper.wgsl';
/**
* Tonemapper implements a tonemapper to convert a linear-light framebuffer to
* a gamma-correct, tonemapped framebuffer used for presentation.
*/
export default class Tonemapper {
private readonly bindGroup: GPUBindGroup;
private readonly pipeline: GPUComputePipeline;
private readonly width: number;
private readonly height: number;
private readonly kWorkgroupSizeX = 16;
private readonly kWorkgroupSizeY = 16;
constructor(
device: GPUDevice,
common: Common,
input: GPUTexture,
output: GPUTexture
) {
this.width = input.width;
this.height = input.height;
const bindGroupLayout = device.createBindGroupLayout({
label: 'Tonemapper.bindGroupLayout',
entries: [
{
// input
binding: 0,
visibility: GPUShaderStage.COMPUTE,
texture: {
viewDimension: '2d',
},
},
{
// output
binding: 1,
visibility: GPUShaderStage.COMPUTE,
storageTexture: {
access: 'write-only',
format: output.format,
viewDimension: '2d',
},
},
],
});
this.bindGroup = device.createBindGroup({
label: 'Tonemapper.bindGroup',
layout: bindGroupLayout,
entries: [
{
// input
binding: 0,
resource: input,
},
{
// output
binding: 1,
resource: output,
},
],
});
const mod = device.createShaderModule({
code:
tonemapperWGSL.replace('{OUTPUT_FORMAT}', output.format) + common.wgsl,
});
const pipelineLayout = device.createPipelineLayout({
label: 'Tonemap.pipelineLayout',
bindGroupLayouts: [bindGroupLayout],
});
this.pipeline = device.createComputePipeline({
label: 'Tonemap.pipeline',
layout: pipelineLayout,
compute: {
module: mod,
constants: {
WorkgroupSizeX: this.kWorkgroupSizeX,
WorkgroupSizeY: this.kWorkgroupSizeY,
},
},
});
}
run(commandEncoder: GPUCommandEncoder) {
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setBindGroup(0, this.bindGroup);
passEncoder.setPipeline(this.pipeline);
passEncoder.dispatchWorkgroups(
Math.ceil(this.width / this.kWorkgroupSizeX),
Math.ceil(this.height / this.kWorkgroupSizeY)
);
passEncoder.end();
}
}