@zephyr3d/device 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base_types.js +602 -0
- package/dist/base_types.js.map +1 -0
- package/dist/builder/ast.js +2135 -0
- package/dist/builder/ast.js.map +1 -0
- package/dist/builder/base.js +477 -0
- package/dist/builder/base.js.map +1 -0
- package/dist/builder/builtinfunc.js +4101 -0
- package/dist/builder/builtinfunc.js.map +1 -0
- package/dist/builder/constructors.js +173 -0
- package/dist/builder/constructors.js.map +1 -0
- package/dist/builder/errors.js +148 -0
- package/dist/builder/errors.js.map +1 -0
- package/dist/builder/programbuilder.js +2323 -0
- package/dist/builder/programbuilder.js.map +1 -0
- package/dist/builder/reflection.js +60 -0
- package/dist/builder/reflection.js.map +1 -0
- package/dist/builder/types.js +1563 -0
- package/dist/builder/types.js.map +1 -0
- package/dist/device.js +515 -0
- package/dist/device.js.map +1 -0
- package/dist/gpuobject.js +2047 -0
- package/dist/gpuobject.js.map +1 -0
- package/dist/helpers/drawtext.js +187 -0
- package/dist/helpers/drawtext.js.map +1 -0
- package/dist/helpers/font.js +189 -0
- package/dist/helpers/font.js.map +1 -0
- package/dist/helpers/glyphmanager.js +121 -0
- package/dist/helpers/glyphmanager.js.map +1 -0
- package/dist/helpers/textureatlas.js +170 -0
- package/dist/helpers/textureatlas.js.map +1 -0
- package/dist/index.d.ts +3873 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/timer.js +38 -0
- package/dist/timer.js.map +1 -0
- package/dist/uniformdata.js +147 -0
- package/dist/uniformdata.js.map +1 -0
- package/dist/vertexdata.js +135 -0
- package/dist/vertexdata.js.map +1 -0
- package/package.json +69 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/device.js
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import { CPUTimer } from './timer.js';
|
|
2
|
+
import { getVertexAttribFormat, getVertexFormatSize, makeVertexBufferType, GPUResourceUsageFlags } from './gpuobject.js';
|
|
3
|
+
import './builder/ast.js';
|
|
4
|
+
import './builder/types.js';
|
|
5
|
+
import { ProgramBuilder } from './builder/programbuilder.js';
|
|
6
|
+
import { DeviceGPUObjectAddedEvent, DeviceGPUObjectRemovedEvent, DeviceResizeEvent } from './base_types.js';
|
|
7
|
+
import '@zephyr3d/base';
|
|
8
|
+
import { DrawText } from './helpers/drawtext.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base class for rendering device
|
|
12
|
+
* @public
|
|
13
|
+
*/ class BaseDevice {
|
|
14
|
+
_canvas;
|
|
15
|
+
_canvasClientWidth;
|
|
16
|
+
_canvasClientHeight;
|
|
17
|
+
_gpuObjectList;
|
|
18
|
+
_gpuMemCost;
|
|
19
|
+
_disposeObjectList;
|
|
20
|
+
_beginFrameTime;
|
|
21
|
+
_endFrameTime;
|
|
22
|
+
_frameInfo;
|
|
23
|
+
_cpuTimer;
|
|
24
|
+
_gpuTimer;
|
|
25
|
+
_runningLoop;
|
|
26
|
+
_fpsCounter;
|
|
27
|
+
_runLoopFunc;
|
|
28
|
+
_backend;
|
|
29
|
+
_beginFrameCounter;
|
|
30
|
+
_programBuilder;
|
|
31
|
+
_stateStack;
|
|
32
|
+
constructor(cvs, backend){
|
|
33
|
+
this._backend = backend;
|
|
34
|
+
this._gpuObjectList = {
|
|
35
|
+
textures: [],
|
|
36
|
+
samplers: [],
|
|
37
|
+
buffers: [],
|
|
38
|
+
programs: [],
|
|
39
|
+
framebuffers: [],
|
|
40
|
+
vertexArrayObjects: [],
|
|
41
|
+
bindGroups: []
|
|
42
|
+
};
|
|
43
|
+
this._canvas = cvs;
|
|
44
|
+
this._canvas.setAttribute('tabindex', '1');
|
|
45
|
+
this._canvasClientWidth = cvs.clientWidth;
|
|
46
|
+
this._canvasClientHeight = cvs.clientHeight;
|
|
47
|
+
this._gpuMemCost = 0;
|
|
48
|
+
this._disposeObjectList = [];
|
|
49
|
+
this._beginFrameTime = 0;
|
|
50
|
+
this._endFrameTime = 0;
|
|
51
|
+
this._runLoopFunc = null;
|
|
52
|
+
this._frameInfo = {
|
|
53
|
+
frameCounter: 0,
|
|
54
|
+
frameTimestamp: 0,
|
|
55
|
+
elapsedTimeCPU: 0,
|
|
56
|
+
elapsedTimeGPU: 0,
|
|
57
|
+
elapsedFrame: 0,
|
|
58
|
+
elapsedOverall: 0,
|
|
59
|
+
FPS: 0,
|
|
60
|
+
drawCalls: 0,
|
|
61
|
+
computeCalls: 0,
|
|
62
|
+
nextFrameCall: []
|
|
63
|
+
};
|
|
64
|
+
this._programBuilder = new ProgramBuilder(this);
|
|
65
|
+
this._cpuTimer = new CPUTimer();
|
|
66
|
+
this._gpuTimer = null;
|
|
67
|
+
this._runningLoop = null;
|
|
68
|
+
this._fpsCounter = {
|
|
69
|
+
time: 0,
|
|
70
|
+
frame: 0
|
|
71
|
+
};
|
|
72
|
+
this._stateStack = [];
|
|
73
|
+
this._beginFrameCounter = 0;
|
|
74
|
+
this._registerEventHandlers();
|
|
75
|
+
}
|
|
76
|
+
get backend() {
|
|
77
|
+
return this._backend;
|
|
78
|
+
}
|
|
79
|
+
get videoMemoryUsage() {
|
|
80
|
+
return this._gpuMemCost;
|
|
81
|
+
}
|
|
82
|
+
get frameInfo() {
|
|
83
|
+
return this._frameInfo;
|
|
84
|
+
}
|
|
85
|
+
get isRendering() {
|
|
86
|
+
return this._runningLoop !== null;
|
|
87
|
+
}
|
|
88
|
+
get canvas() {
|
|
89
|
+
return this._canvas;
|
|
90
|
+
}
|
|
91
|
+
get type() {
|
|
92
|
+
return this._backend.typeName();
|
|
93
|
+
}
|
|
94
|
+
get runLoopFunction() {
|
|
95
|
+
return this._runLoopFunc;
|
|
96
|
+
}
|
|
97
|
+
get programBuilder() {
|
|
98
|
+
return this._programBuilder;
|
|
99
|
+
}
|
|
100
|
+
setFont(fontName) {
|
|
101
|
+
DrawText.setFont(this, fontName);
|
|
102
|
+
}
|
|
103
|
+
drawText(text, x, y, color) {
|
|
104
|
+
DrawText.drawText(this, text, color, x, y);
|
|
105
|
+
}
|
|
106
|
+
disposeObject(obj, remove = true) {
|
|
107
|
+
if (obj) {
|
|
108
|
+
if (remove) {
|
|
109
|
+
this.removeGPUObject(obj);
|
|
110
|
+
}
|
|
111
|
+
if (!obj.disposed) {
|
|
112
|
+
if (this.isContextLost()) {
|
|
113
|
+
obj.destroy();
|
|
114
|
+
} else {
|
|
115
|
+
this._disposeObjectList.push(obj);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
obj.dispatchEvent(null, 'disposed');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async restoreObject(obj) {
|
|
122
|
+
if (obj && obj.disposed && !this.isContextLost()) {
|
|
123
|
+
await obj.restore();
|
|
124
|
+
if (obj.restoreHandler) {
|
|
125
|
+
await obj.restoreHandler(obj);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
enableGPUTimeRecording(enable) {
|
|
130
|
+
if (enable && !this._gpuTimer) {
|
|
131
|
+
this._gpuTimer = this.createGPUTimer();
|
|
132
|
+
} else if (!enable) {
|
|
133
|
+
this._gpuTimer?.end();
|
|
134
|
+
this._gpuTimer = null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
beginFrame() {
|
|
138
|
+
if (this._beginFrameCounter === 0) {
|
|
139
|
+
for (const obj of this._disposeObjectList){
|
|
140
|
+
obj.destroy();
|
|
141
|
+
}
|
|
142
|
+
this._disposeObjectList = [];
|
|
143
|
+
}
|
|
144
|
+
this._beginFrameCounter++;
|
|
145
|
+
this._beginFrameTime = this._cpuTimer.now();
|
|
146
|
+
this.updateFrameInfo();
|
|
147
|
+
return this.onBeginFrame();
|
|
148
|
+
}
|
|
149
|
+
endFrame() {
|
|
150
|
+
if (this._beginFrameCounter > 0) {
|
|
151
|
+
this._beginFrameCounter--;
|
|
152
|
+
if (this._beginFrameCounter === 0) {
|
|
153
|
+
this._endFrameTime = this._cpuTimer.now();
|
|
154
|
+
this.onEndFrame();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
getVertexAttribFormat(semantic, dataType, componentCount) {
|
|
159
|
+
return getVertexAttribFormat(semantic, dataType, componentCount);
|
|
160
|
+
}
|
|
161
|
+
createInterleavedVertexBuffer(attribFormats, data, options) {
|
|
162
|
+
if (options && options.usage && options.usage !== 'vertex') {
|
|
163
|
+
console.error(`createVertexBuffer() failed: options.usage must be 'vertex' or not set`);
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
let size = 0;
|
|
167
|
+
for (const format of attribFormats){
|
|
168
|
+
size += getVertexFormatSize(format);
|
|
169
|
+
}
|
|
170
|
+
const vertexBufferType = makeVertexBufferType(data.byteLength / size >> 0, ...attribFormats);
|
|
171
|
+
const opt = Object.assign({
|
|
172
|
+
usage: 'vertex',
|
|
173
|
+
dynamic: false,
|
|
174
|
+
managed: true,
|
|
175
|
+
storage: false
|
|
176
|
+
}, options || {});
|
|
177
|
+
if (opt.storage) {
|
|
178
|
+
opt.dynamic = false;
|
|
179
|
+
opt.managed = false;
|
|
180
|
+
}
|
|
181
|
+
if (opt.dynamic) {
|
|
182
|
+
opt.managed = false;
|
|
183
|
+
}
|
|
184
|
+
return this.createStructuredBuffer(vertexBufferType, opt, data);
|
|
185
|
+
}
|
|
186
|
+
createVertexBuffer(attribFormat, data, options) {
|
|
187
|
+
if (options && options.usage && options.usage !== 'vertex') {
|
|
188
|
+
console.error(`createVertexBuffer() failed: options.usage must be 'vertex' or not set`);
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const count = getVertexFormatSize(attribFormat);
|
|
192
|
+
const vertexBufferType = makeVertexBufferType(data.byteLength / count >> 0, attribFormat);
|
|
193
|
+
const opt = Object.assign({
|
|
194
|
+
usage: 'vertex',
|
|
195
|
+
dynamic: false,
|
|
196
|
+
managed: true,
|
|
197
|
+
storage: false
|
|
198
|
+
}, options || {});
|
|
199
|
+
if (opt.storage) {
|
|
200
|
+
opt.dynamic = false;
|
|
201
|
+
opt.managed = false;
|
|
202
|
+
}
|
|
203
|
+
if (opt.dynamic) {
|
|
204
|
+
opt.managed = false;
|
|
205
|
+
}
|
|
206
|
+
return this.createStructuredBuffer(vertexBufferType, opt, data);
|
|
207
|
+
}
|
|
208
|
+
draw(primitiveType, first, count) {
|
|
209
|
+
this._frameInfo.drawCalls++;
|
|
210
|
+
this._draw(primitiveType, first, count);
|
|
211
|
+
}
|
|
212
|
+
drawInstanced(primitiveType, first, count, numInstances) {
|
|
213
|
+
this._frameInfo.drawCalls++;
|
|
214
|
+
this._drawInstanced(primitiveType, first, count, numInstances);
|
|
215
|
+
}
|
|
216
|
+
compute(workgroupCountX, workgroupCountY, workgroupCountZ) {
|
|
217
|
+
this._frameInfo.computeCalls++;
|
|
218
|
+
this._compute(workgroupCountX, workgroupCountY, workgroupCountZ);
|
|
219
|
+
}
|
|
220
|
+
runNextFrame(f) {
|
|
221
|
+
if (f) {
|
|
222
|
+
this._frameInfo.nextFrameCall.push(f);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
cancelNextFrameCall(f) {
|
|
226
|
+
const index = this._frameInfo.nextFrameCall.indexOf(f);
|
|
227
|
+
if (index >= 0) {
|
|
228
|
+
this._frameInfo.nextFrameCall.splice(index, 1);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exitLoop() {
|
|
232
|
+
if (this._runningLoop) {
|
|
233
|
+
cancelAnimationFrame(this._runningLoop);
|
|
234
|
+
this._runningLoop = null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
runLoop(func) {
|
|
238
|
+
if (this._runningLoop !== null) {
|
|
239
|
+
console.error('Device.runLoop() can not be nested');
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (!func) {
|
|
243
|
+
console.error('Device.runLoop() argment error');
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const that = this;
|
|
247
|
+
that._runLoopFunc = func;
|
|
248
|
+
(function entry() {
|
|
249
|
+
that._runningLoop = requestAnimationFrame(entry);
|
|
250
|
+
if (that.beginFrame()) {
|
|
251
|
+
that._runLoopFunc(that);
|
|
252
|
+
that.endFrame();
|
|
253
|
+
}
|
|
254
|
+
})();
|
|
255
|
+
}
|
|
256
|
+
pushDeviceStates() {
|
|
257
|
+
this._stateStack.push({
|
|
258
|
+
windowOrderReversed: this.isWindingOrderReversed(),
|
|
259
|
+
framebuffer: this.getFramebuffer(),
|
|
260
|
+
viewport: this.getViewport(),
|
|
261
|
+
scissor: this.getScissor(),
|
|
262
|
+
program: this.getProgram(),
|
|
263
|
+
renderStateSet: this.getRenderStates(),
|
|
264
|
+
vertexLayout: this.getVertexLayout(),
|
|
265
|
+
bindGroups: [
|
|
266
|
+
this.getBindGroup(0),
|
|
267
|
+
this.getBindGroup(1),
|
|
268
|
+
this.getBindGroup(2),
|
|
269
|
+
this.getBindGroup(3)
|
|
270
|
+
]
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
popDeviceStates() {
|
|
274
|
+
if (this._stateStack.length === 0) {
|
|
275
|
+
console.error('Device.popDeviceStates(): stack is empty');
|
|
276
|
+
} else {
|
|
277
|
+
const top = this._stateStack.pop();
|
|
278
|
+
this.setFramebuffer(top.framebuffer);
|
|
279
|
+
this.setViewport(top.viewport);
|
|
280
|
+
this.setScissor(top.scissor);
|
|
281
|
+
this.setProgram(top.program);
|
|
282
|
+
this.setRenderStates(top.renderStateSet);
|
|
283
|
+
this.setVertexLayout(top.vertexLayout);
|
|
284
|
+
this.setBindGroup(0, ...top.bindGroups[0]);
|
|
285
|
+
this.setBindGroup(1, ...top.bindGroups[1]);
|
|
286
|
+
this.setBindGroup(2, ...top.bindGroups[2]);
|
|
287
|
+
this.setBindGroup(3, ...top.bindGroups[3]);
|
|
288
|
+
this.reverseVertexWindingOrder(top.windowOrderReversed);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
getGPUObjects() {
|
|
292
|
+
return this._gpuObjectList;
|
|
293
|
+
}
|
|
294
|
+
getGPUObjectById(uid) {
|
|
295
|
+
for (const list of [
|
|
296
|
+
this._gpuObjectList.textures,
|
|
297
|
+
this._gpuObjectList.samplers,
|
|
298
|
+
this._gpuObjectList.buffers,
|
|
299
|
+
this._gpuObjectList.framebuffers,
|
|
300
|
+
this._gpuObjectList.programs,
|
|
301
|
+
this._gpuObjectList.vertexArrayObjects
|
|
302
|
+
]){
|
|
303
|
+
for (const obj of list){
|
|
304
|
+
if (obj.uid === uid) {
|
|
305
|
+
return obj;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
screenToDevice(val) {
|
|
312
|
+
return this.getFramebuffer() ? val : Math.round(val * this.getScale());
|
|
313
|
+
}
|
|
314
|
+
deviceToScreen(val) {
|
|
315
|
+
return this.getFramebuffer() ? val : Math.round(val / this.getScale());
|
|
316
|
+
}
|
|
317
|
+
buildRenderProgram(options) {
|
|
318
|
+
return this._programBuilder.buildRenderProgram(options);
|
|
319
|
+
}
|
|
320
|
+
buildComputeProgram(options) {
|
|
321
|
+
return this._programBuilder.buildComputeProgram(options);
|
|
322
|
+
}
|
|
323
|
+
addGPUObject(obj) {
|
|
324
|
+
const list = this.getGPUObjectList(obj);
|
|
325
|
+
if (list && list.indexOf(obj) < 0) {
|
|
326
|
+
list.push(obj);
|
|
327
|
+
this.dispatchEvent(new DeviceGPUObjectAddedEvent(obj));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
removeGPUObject(obj) {
|
|
331
|
+
const list = this.getGPUObjectList(obj);
|
|
332
|
+
if (list) {
|
|
333
|
+
const index = list.indexOf(obj);
|
|
334
|
+
if (index >= 0) {
|
|
335
|
+
list.splice(index, 1);
|
|
336
|
+
this.dispatchEvent(new DeviceGPUObjectRemovedEvent(obj));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
updateVideoMemoryCost(delta) {
|
|
341
|
+
this._gpuMemCost += delta;
|
|
342
|
+
}
|
|
343
|
+
_onresize() {
|
|
344
|
+
if (this._canvasClientWidth !== this._canvas.clientWidth || this._canvasClientHeight !== this._canvas.clientHeight) {
|
|
345
|
+
this._canvasClientWidth = this._canvas.clientWidth;
|
|
346
|
+
this._canvasClientHeight = this._canvas.clientHeight;
|
|
347
|
+
this.dispatchEvent(new DeviceResizeEvent(this._canvasClientWidth, this._canvasClientHeight));
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
_registerEventHandlers() {
|
|
351
|
+
const canvas = this._canvas;
|
|
352
|
+
const that = this;
|
|
353
|
+
if (window.ResizeObserver) {
|
|
354
|
+
new window.ResizeObserver((entries)=>{
|
|
355
|
+
that._onresize();
|
|
356
|
+
}).observe(canvas, {});
|
|
357
|
+
} else {
|
|
358
|
+
if (window.MutationObserver) {
|
|
359
|
+
new MutationObserver(function(mutations) {
|
|
360
|
+
if (mutations.length > 0) {
|
|
361
|
+
that._onresize();
|
|
362
|
+
}
|
|
363
|
+
}).observe(canvas, {
|
|
364
|
+
attributes: true,
|
|
365
|
+
attributeFilter: [
|
|
366
|
+
'style'
|
|
367
|
+
]
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
window.addEventListener('resize', ()=>{
|
|
371
|
+
this._onresize();
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
updateFrameInfo() {
|
|
376
|
+
this._frameInfo.frameCounter++;
|
|
377
|
+
this._frameInfo.drawCalls = 0;
|
|
378
|
+
this._frameInfo.computeCalls = 0;
|
|
379
|
+
const now = this._beginFrameTime;
|
|
380
|
+
if (this._frameInfo.frameTimestamp === 0) {
|
|
381
|
+
this._frameInfo.frameTimestamp = now;
|
|
382
|
+
this._frameInfo.elapsedTimeCPU = 0;
|
|
383
|
+
this._frameInfo.elapsedTimeGPU = 0;
|
|
384
|
+
this._frameInfo.elapsedFrame = 0;
|
|
385
|
+
this._frameInfo.elapsedOverall = 0;
|
|
386
|
+
this._frameInfo.FPS = 0;
|
|
387
|
+
this._fpsCounter.time = now;
|
|
388
|
+
this._fpsCounter.frame = this._frameInfo.frameCounter;
|
|
389
|
+
if (this._gpuTimer) {
|
|
390
|
+
this._gpuTimer.begin();
|
|
391
|
+
}
|
|
392
|
+
} else {
|
|
393
|
+
this._frameInfo.elapsedFrame = now - this._frameInfo.frameTimestamp;
|
|
394
|
+
this._frameInfo.elapsedOverall += this._frameInfo.elapsedFrame;
|
|
395
|
+
let gpuTime = 0;
|
|
396
|
+
let cpuTime = 0;
|
|
397
|
+
if (this._endFrameTime !== 0) {
|
|
398
|
+
gpuTime = now - this._endFrameTime;
|
|
399
|
+
cpuTime = this._endFrameTime - this._frameInfo.frameTimestamp;
|
|
400
|
+
}
|
|
401
|
+
this._frameInfo.frameTimestamp = now;
|
|
402
|
+
if (now >= this._fpsCounter.time + 1000) {
|
|
403
|
+
this._frameInfo.FPS = (this._frameInfo.frameCounter - this._fpsCounter.frame) * 1000 / (now - this._fpsCounter.time);
|
|
404
|
+
this._fpsCounter.time = now;
|
|
405
|
+
this._fpsCounter.frame = this._frameInfo.frameCounter;
|
|
406
|
+
this._frameInfo.elapsedTimeGPU = gpuTime;
|
|
407
|
+
this._frameInfo.elapsedTimeCPU = cpuTime;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
for (const f of this._frameInfo.nextFrameCall){
|
|
411
|
+
f();
|
|
412
|
+
}
|
|
413
|
+
this._frameInfo.nextFrameCall.length = 0;
|
|
414
|
+
}
|
|
415
|
+
getGPUObjectList(obj) {
|
|
416
|
+
let list = null;
|
|
417
|
+
if (obj.isTexture()) {
|
|
418
|
+
list = this._gpuObjectList.textures;
|
|
419
|
+
} else if (obj.isSampler()) {
|
|
420
|
+
list = this._gpuObjectList.samplers;
|
|
421
|
+
} else if (obj.isBuffer()) {
|
|
422
|
+
list = this._gpuObjectList.buffers;
|
|
423
|
+
} else if (obj.isFramebuffer()) {
|
|
424
|
+
list = this._gpuObjectList.framebuffers;
|
|
425
|
+
} else if (obj.isProgram()) {
|
|
426
|
+
list = this._gpuObjectList.programs;
|
|
427
|
+
} else if (obj.isVertexLayout()) {
|
|
428
|
+
list = this._gpuObjectList.vertexArrayObjects;
|
|
429
|
+
} else if (obj.isBindGroup()) {
|
|
430
|
+
list = this._gpuObjectList.bindGroups;
|
|
431
|
+
}
|
|
432
|
+
return list;
|
|
433
|
+
}
|
|
434
|
+
invalidateAll() {
|
|
435
|
+
for (const list of [
|
|
436
|
+
this._gpuObjectList.buffers,
|
|
437
|
+
this._gpuObjectList.textures,
|
|
438
|
+
this._gpuObjectList.samplers,
|
|
439
|
+
this._gpuObjectList.programs,
|
|
440
|
+
this._gpuObjectList.framebuffers,
|
|
441
|
+
this._gpuObjectList.vertexArrayObjects,
|
|
442
|
+
this._gpuObjectList.bindGroups
|
|
443
|
+
]){
|
|
444
|
+
for (const obj of list){
|
|
445
|
+
this.disposeObject(obj, false);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (this.isContextLost()) {
|
|
449
|
+
for (const obj of this._disposeObjectList){
|
|
450
|
+
obj.destroy();
|
|
451
|
+
}
|
|
452
|
+
this._disposeObjectList = [];
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
async reloadAll() {
|
|
456
|
+
const promises = [];
|
|
457
|
+
for (const list of [
|
|
458
|
+
this._gpuObjectList.buffers,
|
|
459
|
+
this._gpuObjectList.textures,
|
|
460
|
+
this._gpuObjectList.samplers,
|
|
461
|
+
this._gpuObjectList.programs,
|
|
462
|
+
this._gpuObjectList.framebuffers,
|
|
463
|
+
this._gpuObjectList.vertexArrayObjects,
|
|
464
|
+
this._gpuObjectList.bindGroups
|
|
465
|
+
]){
|
|
466
|
+
// obj.reload() may change the list, so make a copy first
|
|
467
|
+
for (const obj of list.slice()){
|
|
468
|
+
promises.push(obj.reload());
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
Promise.all(promises);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
parseTextureOptions(options) {
|
|
475
|
+
const noMipmapFlag = options?.samplerOptions?.mipFilter === 'none' ? GPUResourceUsageFlags.TF_NO_MIPMAP : 0;
|
|
476
|
+
const writableFlag = options?.writable ? GPUResourceUsageFlags.TF_WRITABLE : 0;
|
|
477
|
+
const dynamicFlag = options?.dynamic ? GPUResourceUsageFlags.DYNAMIC : 0;
|
|
478
|
+
return noMipmapFlag | writableFlag | dynamicFlag;
|
|
479
|
+
}
|
|
480
|
+
parseBufferOptions(options, defaultUsage) {
|
|
481
|
+
const usage = options?.usage || defaultUsage;
|
|
482
|
+
let usageFlag;
|
|
483
|
+
switch(usage){
|
|
484
|
+
case 'uniform':
|
|
485
|
+
usageFlag = GPUResourceUsageFlags.BF_UNIFORM;
|
|
486
|
+
options.managed = false;
|
|
487
|
+
options.dynamic = options.dynamic ?? true;
|
|
488
|
+
break;
|
|
489
|
+
case 'vertex':
|
|
490
|
+
usageFlag = GPUResourceUsageFlags.BF_VERTEX;
|
|
491
|
+
break;
|
|
492
|
+
case 'index':
|
|
493
|
+
usageFlag = GPUResourceUsageFlags.BF_INDEX;
|
|
494
|
+
break;
|
|
495
|
+
case 'read':
|
|
496
|
+
usageFlag = GPUResourceUsageFlags.BF_READ;
|
|
497
|
+
options.managed = false;
|
|
498
|
+
break;
|
|
499
|
+
case 'write':
|
|
500
|
+
usageFlag = GPUResourceUsageFlags.BF_WRITE;
|
|
501
|
+
options.managed = false;
|
|
502
|
+
break;
|
|
503
|
+
default:
|
|
504
|
+
usageFlag = 0;
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
const storageFlag = options?.storage ?? false ? GPUResourceUsageFlags.BF_STORAGE : 0;
|
|
508
|
+
const dynamicFlag = options?.dynamic ?? false ? GPUResourceUsageFlags.DYNAMIC : 0;
|
|
509
|
+
const managedFlag = dynamicFlag === 0 && (options?.managed ?? true) ? GPUResourceUsageFlags.MANAGED : 0;
|
|
510
|
+
return usageFlag | storageFlag | dynamicFlag | managedFlag;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export { BaseDevice };
|
|
515
|
+
//# sourceMappingURL=device.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|