-
Notifications
You must be signed in to change notification settings - Fork 2
/
webgpu-hdr.html
192 lines (169 loc) · 6.52 KB
/
webgpu-hdr.html
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
181
182
183
184
185
186
187
188
189
190
191
192
<html>
<head>
<script>
var current_screen_details;
var rendering_hdr_headroom = 1;
function onRenderingHdrHeadroomSliderChanged() {
rendering_hdr_headroom = Math.pow(2.0, RenderingHdrHeadroomSlider.value);
RenderingHdrHeadroomSliderLabel.innerText = 'Rendering HDR headroom: ' + rendering_hdr_headroom.toFixed(2);
drawWebGPUCanvas();
drawWebGLCanvas();
}
function onCurrentScreenDetailsChanged() {
let screen_hdr_headroom = current_screen_details.highDynamicRangeHeadroom;
if (SetRenderingFromScreen.checked && screen_hdr_headroom) {
RenderingHdrHeadroomSlider.value = Math.log(screen_hdr_headroom) / Math.log(2.0);
onRenderingHdrHeadroomSliderChanged();
}
document.getElementById('DisplayValue').innerText =
'Screen HDR headroom: ' + screen_hdr_headroom.toFixed(2) + '.';
}
// Clear the canvas to rendering_hdr_headroom using WebGPU.
async function drawWebGPUCanvas() {
const canvas = document.getElementById('WebGPUCanvas');
const adapter = await navigator.gpu?.requestAdapter();
const device = await adapter?.requestDevice();
const context = canvas.getContext('webgpu')
if (!device || !context) {
console.error("Failed to initialize WebGPU");
return;
}
context.configure({
device: device,
format: 'rgba16float',
colorSpace: 'srgb-linear',
usage: GPUTextureUsage.RENDER_ATTACHMENT,
hdrOptions: {mode:'extended'},
});
let v = rendering_hdr_headroom;
const renderPassDescriptor = {
colorAttachments: [
{
view: context.getCurrentTexture().createView(),
clearValue: { r:v, g:v, b:v, a: 1.0 },
loadOp: 'clear',
storeOp: 'store',
},
],
};
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setViewport(0, 0, 32, 32, 0, 1);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
}
// Clear the canvas to rendering_hdr_headroom using WebGL.
async function drawWebGLCanvas() {
const canvas = document.getElementById('WebGLCanvas');
const gl = canvas.getContext('webgl2')
if (!gl) {
console.error("Failed to initialize WebGL");
return;
}
if (`configureHighDynamicRange` in canvas) {
canvas.configureHighDynamicRange({ mode:'extended' });
}
let ext = gl.getExtension('EXT_color_buffer_half_float');
gl.drawingBufferStorage(gl.RGBA16F, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.drawingBufferColorSpace = 'srgb-linear';
// Create the program to draw a point sprite.
let program = null;
{
let compileShader = function(gl, vertCode, fragCode) {
let vertShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertShader, vertCode);
gl.compileShader(vertShader);
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(vertShader));
let fragShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragShader, fragCode);
gl.compileShader(fragShader);
if (!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(fragShader));
let shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertShader);
gl.attachShader(shaderProgram, fragShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS))
throw new Error(gl.getProgramInfoLog(program));
return shaderProgram;
}
let vs = `attribute vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}`;
let fs = `precision mediump float;
uniform float color;
void main() {
gl_FragColor = vec4(color, color, color, 1.0);
}`;
program = compileShader(gl, vs, fs);
}
gl.useProgram(program);
// Draw using that program.
{
let vertices = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertices);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, 1,1, -1,1]), gl.STATIC_DRAW);
let indices = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0,1,2, 0,2,3]), gl.STATIC_DRAW);
let positionLocation = gl.getAttribLocation(program, 'position');
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLocation);
gl.uniform1f(gl.getUniformLocation(program, 'color'), rendering_hdr_headroom);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
}
function main() {
// Configure screen information listener.
ButtonDetails.addEventListener('click', async function screenDetails() {
const screens = await window.getScreenDetails();
current_screen_details = screens.currentScreen;
onCurrentScreenDetailsChanged();
screens.addEventListener('currentscreenchange', (event) => {
current_screen_details = screens.currentScreen;
onCurrentScreenDetailsChanged();
});
})
// Configure rendering slider.
RenderingHdrHeadroomSlider.addEventListener('input', onRenderingHdrHeadroomSliderChanged, false);
drawWebGPUCanvas();
drawWebGLCanvas();
}
</script>
</head>
<body onload='main()'>
<h1>Screen details</h1>
<p id="DisplayValue">Screen HDR headroom: (unknown)</p>
<p><button type="button" id="ButtonDetails">Query screen HDR headroom</button></p>
<h1>Rendering settings</h1>
<p>
<label id="RenderingHdrHeadroomSliderLabel" for="RenderingHdrHeadroomSlider">Rendering HDR headroom: 1.00</label>.
<label id="SetRenderingFromScreenlabel" for="SetRenderingFromScreen">Set from screen:</label>
<input type="checkbox" id="SetRenderingFromScreen" name="SetRenderingFromScreen" checked onclick="onCurrentScreenDetailsChanged();">
</p>
<p><input id="RenderingHdrHeadroomSlider" type="range" min="-1" max="5" step="0.1" value="0" list="tickmarks""/></p>
<datalist id="tickmarks">
<option value="-1"></option>
<option value="0"></option>
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
<option value="4"></option>
</datalist>
</body>
<h1>WebGPU rendering</h1>
<p>
This requires that "Experimental Web Platform features" and "Unsafe WebGPU" be enabled in chrome://flags.
</p>
<p>
WebGPU canvas that clears to HDR headroom:
</p>
<canvas id="WebGPUCanvas" style="width:32px; height:32px; border:1px solid black;"></canvas>
<h1>WebGL rendering</h1>
<p>
WebGL canvas that clears to HDR headroom:
</p>
<canvas id="WebGLCanvas" style="width:32px; height:32px; border:1px solid black;"></canvas>
</html>