-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu-optimizer.js
More file actions
147 lines (123 loc) · 5.18 KB
/
gpu-optimizer.js
File metadata and controls
147 lines (123 loc) · 5.18 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
import * as THREE from 'three';
export class GPUOptimizer {
constructor(renderer) {
this.renderer = renderer;
this.textureCache = new Map();
this.geometryCache = new Map();
this.materialCache = new Map();
this.renderQueue = [];
this.lastCleanup = 0;
// GPU memory monitoring
this.gpuMemoryUsage = {
textures: 0,
geometries: 0,
materials: 0,
total: 0
};
}
// Reduce GPU blocking by managing texture memory
optimizeTextures() {
// Use smaller texture sizes for distant objects
const textureOptimizer = {
maxTextureSize: 512, // Reduce from default 2048
minTextureSize: 64,
createOptimizedTexture(originalTexture, distance) {
const cacheKey = `${originalTexture.uuid}_${distance}`;
if (this.textureCache.has(cacheKey)) {
return this.textureCache.get(cacheKey);
}
// Use smaller textures for distant objects
let size = distance > 50 ? 64 : distance > 25 ? 128 : 256;
const canvas = document.createElement('canvas');
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext('2d');
// Simple solid color instead of complex texture for distant objects
if (distance > 50) {
ctx.fillStyle = originalTexture.color || '#888888';
ctx.fillRect(0, 0, size, size);
} else {
// Use original but downscaled
if (originalTexture.image) {
ctx.drawImage(originalTexture.image, 0, 0, size, size);
}
}
const optimizedTexture = new THREE.CanvasTexture(canvas);
optimizedTexture.minFilter = THREE.LinearFilter;
optimizedTexture.magFilter = THREE.LinearFilter;
this.textureCache.set(cacheKey, optimizedTexture);
return optimizedTexture;
}
};
return textureOptimizer;
}
// Implement GPU memory cleanup
cleanupGPUMemory() {
const now = performance.now();
if (now - this.lastCleanup < 5000) return; // Cleanup every 5 seconds
// Clean unused textures
for (const [key, texture] of this.textureCache) {
if (texture.refCount === 0) {
texture.dispose();
this.textureCache.delete(key);
}
}
// Clean unused geometries
for (const [key, geometry] of this.geometryCache) {
if (geometry.refCount === 0) {
geometry.dispose();
this.geometryCache.delete(key);
}
}
// Force WebGL context cleanup
if (this.renderer.info.memory.textures > 50) {
this.renderer.renderLists.dispose();
this.renderer.dispose();
}
this.lastCleanup = now;
}
// Reduce draw calls by batching
createInstancedGeometry(baseGeometry, positions, scales) {
const instancedGeometry = new THREE.InstancedBufferGeometry();
instancedGeometry.copy(baseGeometry);
// Instance positions
const instancePositions = new Float32Array(positions.length * 3);
positions.forEach((pos, i) => {
instancePositions[i * 3] = pos.x;
instancePositions[i * 3 + 1] = pos.y;
instancePositions[i * 3 + 2] = pos.z;
});
// Instance scales
const instanceScales = new Float32Array(scales);
instancedGeometry.setAttribute('instancePosition',
new THREE.InstancedBufferAttribute(instancePositions, 3));
instancedGeometry.setAttribute('instanceScale',
new THREE.InstancedBufferAttribute(instanceScales, 1));
instancedGeometry.instanceCount = positions.length;
return instancedGeometry;
}
// Optimize renderer settings for performance
configureRenderer() {
// Reduce GPU load
this.renderer.shadowMap.enabled = false; // Disable shadows for performance
this.renderer.shadowMap.type = THREE.BasicShadowMap;
this.renderer.antialias = false; // Disable AA for performance
this.renderer.powerPreference = "high-performance";
// Reduce precision for mobile/low-end devices
this.renderer.capabilities.precision = 'mediump';
// Optimize rendering
this.renderer.sortObjects = false; // Skip sorting for performance
this.renderer.autoClear = false; // Manual clearing for control
}
// Monitor GPU performance
getGPUStats() {
const info = this.renderer.info;
return {
drawCalls: info.render.calls,
triangles: info.render.triangles,
textures: info.memory.textures,
geometries: info.memory.geometries,
memoryUsage: this.gpuMemoryUsage.total
};
}
}