-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmac-gpu-profiling.js
96 lines (77 loc) · 3.09 KB
/
mac-gpu-profiling.js
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
/**
* This script allows GPU Profiling on Mac using Xcode's GPU Frame Capture. Please read the instructions
* in the manual: https://developer.playcanvas.com/user-manual/optimization/gpu-profiling/
*/
var MacGPUProfiling = pc.createScript('MacGPUProfiling');
// Called once after all resources are loaded and initialized
MacGPUProfiling.prototype.initialize = function () {
this.isInitialized = false;
this.device = null;
this.context = null;
// this is not needed for WebGPU
if (this.app.graphicsDevice.isWebGPU) return;
// only needed on Mac
if (pc.platform.name !== 'osx') return;
// Create a new canvas for WebGPU with a smaller size
this.webgpuCanvas = document.createElement('canvas');
this.webgpuCanvas.width = 20;
this.webgpuCanvas.height = 20;
this.webgpuCanvas.style.position = 'absolute';
this.webgpuCanvas.style.top = '20px'; // Adjust position if needed
this.webgpuCanvas.style.left = '20px'; // Adjust position if needed
document.body.appendChild(this.webgpuCanvas);
// Start the asynchronous WebGPU initialization
this.initWebGPU();
};
// Async function for WebGPU initialization
MacGPUProfiling.prototype.initWebGPU = async function () {
// Check for WebGPU support
if (!navigator.gpu) {
console.error('WebGPU is not supported on this browser.');
return;
}
// Get WebGPU adapter and device
const adapter = await navigator.gpu.requestAdapter();
this.device = await adapter.requestDevice();
console.log('Created WebGPU device used for profiling');
// Create a WebGPU context for the new canvas
this.context = this.webgpuCanvas.getContext('webgpu');
// Configure the WebGPU context
const swapChainFormat = 'bgra8unorm';
this.context.configure({
device: this.device,
format: swapChainFormat
});
// Mark initialization as complete
this.isInitialized = true;
// Hook into the 'frameend' event
this.app.on('frameend', this.onFrameEnd, this);
};
// Called when the 'frameend' event is triggered
MacGPUProfiling.prototype.onFrameEnd = function () {
// If WebGPU is not initialized yet, do nothing
if (!this.isInitialized) return;
// Clear the WebGPU surface to red after WebGL rendering
this.clearToRed();
};
// Function to clear the WebGPU surface to red
MacGPUProfiling.prototype.clearToRed = function () {
// Get the current texture to render to
const textureView = this.context.getCurrentTexture().createView();
// Create a command encoder
const commandEncoder = this.device.createCommandEncoder();
// Create a render pass descriptor with a red background
const renderPassDescriptor = {
colorAttachments: [{
view: textureView,
clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }, // Red background
loadOp: 'clear',
storeOp: 'store'
}]
};
// render pass
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.end();
// Submit the commands to the GPU
this.device.queue.submit([commandEncoder.finish()]);
};