-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathworker.html
More file actions
296 lines (270 loc) · 8.19 KB
/
worker.html
File metadata and controls
296 lines (270 loc) · 8.19 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stats-GL - Worker Texture Demo</title>
<link rel="help" href="/llms.txt" type="text/plain" title="LLM Instructions">
<style>
body {
margin: 0;
overflow: hidden;
background: #111;
font-family: system-ui, -apple-system, sans-serif;
}
.code-overlay {
position: absolute;
bottom: 0;
left: 0;
padding: 14px;
background-color: #1e1e1ef8;
color: #d4d4d4;
overflow: auto;
margin: 0;
font-family: 'Courier New', Courier, monospace;
@media screen and (max-width: 608px) {
display: none;
}
}
.nav-tabs {
display: flex;
gap: 1px;
margin-top: -14px;
position: absolute;
right: 0;
margin-bottom: 14px;
font-family: system-ui, -apple-system, sans-serif;
}
.nav-tab {
background: #1e1e1ef8;
color: #d4d4d4;
padding: 4px 8px;
text-decoration: none;
font-size: 12px;
transition: background-color 0.2s;
}
.nav-tab:hover {
background: #3d3d3d;
}
.nav-tab.active {
background: #3d3d3d;
}
pre {
margin: 0;
}
.keyword {
color: #569cd6;
}
.comment {
color: #6a9955;
}
.string {
color: #ce9178;
}
::selection {
background: #264f78;
}
#footer {
position: absolute;
bottom: 0;
right: 0;
color: #fff;
font-size: 14px;
padding: 14px;
text-align: right;
background-color: #1e1e1ef8;
}
#footer a {
color: #fff;
font-weight: bold;
text-decoration: none;
}
#render-canvas {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 512px;
height: 512px;
display: none;
}
#render-canvas.visible {
display: block;
}
.controls {
position: absolute;
top: 10px;
right: 10px;
display: flex;
gap: 8px;
z-index: 100;
}
.controls button {
background: #333;
color: #fff;
border: none;
padding: 8px 12px;
cursor: pointer;
font-size: 12px;
}
.controls button:hover {
background: #555;
}
#stress-main {
background: #2d5a2d;
}
#stress-main:hover {
background: #3d7a3d;
}
#stress-main.active {
opacity: 0.6;
}
#stress-worker {
background: #8a5a2d;
}
#stress-worker:hover {
background: #aa6a3d;
}
#stress-worker.active {
opacity: 0.6;
}
.info {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #666;
font-size: 14px;
text-align: center;
}
</style>
</head>
<body>
<canvas id="render-canvas" width="512" height="512"></canvas>
<div class="controls">
<button id="stress-main">Stress Main</button>
<button id="stress-worker">Stress Worker</button>
<button id="toggle-btn">Show Canvas</button>
</div>
<div class="info" id="info-text">
Rendering in OffscreenCanvas worker<br>
Texture previews transferred via ImageBitmap
</div>
<div class="code-overlay">
<div class="nav-tabs">
<a href="/" class="nav-tab">Three.js</a>
<a href="/webgl.html" class="nav-tab">WebGL</a>
<a href="/webgpu.html" class="nav-tab">WebGPU</a>
<a href="/worker.html" class="nav-tab active">Worker</a>
<a href="/worker_tsl.html" class="nav-tab">Worker TSL</a>
</div>
<pre>
<span class="comment">// Worker Texture Demo</span>
<span class="comment">// Textures captured in worker, transferred to main</span>
<span class="comment">// Worker side:</span>
<span class="keyword">const</span> bitmap = <span class="keyword">await</span> profiler.captureTexture({
framebuffer, width, height
});
postMessage(
{ type: <span class="string">'texture'</span>, name, bitmap, width, height },
[bitmap] <span class="comment">// Transferable</span>
);
<span class="comment">// Main thread:</span>
worker.onmessage = (e) => {
<span class="keyword">if</span> (e.data.type === <span class="string">'texture'</span>) {
stats.setTextureBitmap(
e.data.name,
e.data.bitmap,
e.data.width,
e.data.height
);
}
};</pre>
</div>
<div id="footer">
Made with love by
<a href="https://twitter.com/onirenaud" target="_blank">@onirenaud</a>
-
<a href="https://twitter.com/utsuboco" target="_blank">@utsuboco</a>
</div>
<script type="module">
import Stats from './dist/main.js';
// Initialize stats (no canvas - worker handles rendering)
const stats = new Stats({ trackGPU: true, trackHz: true });
document.body.appendChild(stats.dom);
// Create texture preview panels
stats.addTexturePanel('Color');
stats.addTexturePanel('Luma');
// Get canvas and transfer control to offscreen
const canvas = document.getElementById('render-canvas');
const offscreen = canvas.transferControlToOffscreen();
// Toggle canvas visibility (visible by default)
const toggleBtn = document.getElementById('toggle-btn');
const infoText = document.getElementById('info-text');
let canvasVisible = true;
canvas.classList.add('visible');
infoText.style.display = 'none';
toggleBtn.textContent = 'Hide Canvas';
toggleBtn.addEventListener('click', () => {
canvasVisible = !canvasVisible;
canvas.classList.toggle('visible', canvasVisible);
infoText.style.display = canvasVisible ? 'none' : 'block';
toggleBtn.textContent = canvasVisible ? 'Hide Canvas' : 'Show Canvas';
});
// Stress test state
let stressMain = false;
let stressWorkerFlag = false;
// Heavy computation function (1M iterations)
// Note: We assign to window to prevent tree-shaking
function heavyComputation() {
let result = 0;
for (let i = 0; i < 1000000; i++) {
result += Math.sqrt(i) * Math.sin(i);
}
window.__stressResult = result;
}
// Stress buttons
const stressMainBtn = document.getElementById('stress-main');
const stressWorkerBtn = document.getElementById('stress-worker');
stressMainBtn.addEventListener('click', () => {
stressMain = !stressMain;
stressMainBtn.classList.toggle('active', stressMain);
stressMainBtn.textContent = stressMain ? 'Stop Main' : 'Stress Main';
});
stressWorkerBtn.addEventListener('click', () => {
stressWorkerFlag = !stressWorkerFlag;
stressWorkerBtn.classList.toggle('active', stressWorkerFlag);
stressWorkerBtn.textContent = stressWorkerFlag ? 'Stop Worker' : 'Stress Worker';
worker.postMessage({ type: 'stress', enabled: stressWorkerFlag });
});
// Create worker
const worker = new Worker(
new URL('./demo/worker.js', import.meta.url),
{ type: 'module' }
);
// Handle messages from worker
worker.onmessage = (e) => {
const { type, data, name, bitmap, width, height } = e.data;
if (type === 'stats') {
// Update stats from worker data (don't call update here, main loop handles it)
stats.setData(data);
} else if (type === 'texture') {
// Update texture panel with transferred bitmap
stats.setTextureBitmap(name, bitmap, width, height);
}
};
// Transfer canvas to worker
worker.postMessage({ type: 'init', canvas: offscreen }, [offscreen]);
// Main thread loop for stress test
function mainLoop() {
stats.begin();
if (stressMain) {
heavyComputation();
}
stats.update();
requestAnimationFrame(mainLoop);
}
mainLoop();
</script>
</body>
</html>