This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPU_EXAMPLES.rs
More file actions
424 lines (353 loc) · 14.1 KB
/
GPU_EXAMPLES.rs
File metadata and controls
424 lines (353 loc) · 14.1 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Examples: Using GPU Detection & Optimization in HyprBrowser
// =============================================================================
// EXAMPLE 1: Basic GPU Detection
// =============================================================================
#[cfg(test)]
mod example_gpu_detection {
use crate::gpu_detect::{GpuInfo, GpuTier, GpuSettings};
#[tokio::test]
async fn detect_gpu_at_startup() {
// This is automatically called in Application::new()
let gpu_info = GpuInfo::detect().await;
println!("GPU Detection Result:");
println!(" Tier: {:?}", gpu_info.tier);
println!(" Adapter: {}", gpu_info.adapter_name);
println!(" Backend: {:?}", gpu_info.backend);
println!(" Max Texture: {}", gpu_info.max_texture_size);
println!(" Max Buffer: {}", gpu_info.max_uniform_buffer_size);
// Auto-generate settings based on GPU tier
let settings = GpuSettings::for_gpu(&gpu_info);
println!("\nAuto-Generated Settings:");
println!(" Effect Intensity: {:.0}%", settings.effect_intensity * 100.0);
println!(" Particle Count: {}", settings.particle_count);
println!(" Target FPS: {}", settings.animation_fps_target);
println!(" Reduce Transparency: {}", settings.reduce_transparency_effects);
}
}
// =============================================================================
// EXAMPLE 2: Conditional Feature Rendering
// =============================================================================
#[cfg(test)]
mod example_feature_detection {
use crate::gpu_detect::{GpuInfo, ShaderFeature};
fn render_advanced_snow(gpu_info: &GpuInfo) {
if gpu_info.supports_shader_feature(ShaderFeature::AdvancedSnow) {
// Full advanced snow with complex particles
println!("✓ Rendering advanced snow effect");
render_shader_snow(500); // Full particles
} else {
// Simplified snow fallback
println!("✓ Rendering simplified snow effect");
render_basic_snow(150); // Reduced particles
}
}
fn render_shader_snow(particle_count: usize) {
println!(" Particle Count: {}", particle_count);
println!(" Physics: Full");
println!(" Lighting: Per-particle");
}
fn render_basic_snow(particle_count: usize) {
println!(" Particle Count: {}", particle_count);
println!(" Physics: Simplified");
println!(" Lighting: Global");
}
#[test]
fn example() {
let gpu_info = crate::gpu_detect::GpuInfo {
tier: crate::gpu_detect::GpuTier::LowPower,
adapter_name: "Intel UHD Graphics 630".to_string(),
backend: wgpu::BackendBit::DX12,
max_texture_size: 16384,
max_uniform_buffer_size: 65536,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
};
render_advanced_snow(&gpu_info);
}
}
// =============================================================================
// EXAMPLE 3: Dynamic Effect Adjustment
// =============================================================================
#[cfg(test)]
mod example_dynamic_adjustment {
use crate::gpu_detect::GpuSettings;
struct EffectManager {
settings: GpuSettings,
frame_time_ms: f64,
}
impl EffectManager {
fn update(&mut self) {
// Monitor frame time and dynamically adjust
if self.frame_time_ms > 33.0 {
// Dropping below 30 FPS on iGPU
println!("⚠ Frame time high: {:.2}ms", self.frame_time_ms);
self.reduce_effects();
} else if self.frame_time_ms < 20.0 {
// Good headroom, could increase effects
println!("✓ Frame time good: {:.2}ms", self.frame_time_ms);
}
}
fn reduce_effects(&mut self) {
println!("Reducing visual effects...");
self.settings.effect_intensity = (self.settings.effect_intensity - 0.1).max(0.1);
self.settings.particle_count = (self.settings.particle_count / 2).max(30);
println!(
" New intensity: {:.0}% | Particles: {}",
self.settings.effect_intensity * 100.0,
self.settings.particle_count
);
}
}
#[test]
fn example() {
let settings = crate::gpu_detect::GpuSettings {
enable_snow_effect: true,
effect_intensity: 0.6,
particle_count: 150,
animation_fps_target: 45,
reduce_transparency_effects: true,
};
let mut manager = EffectManager {
settings,
frame_time_ms: 35.0, // Simulating slow frame
};
manager.update();
}
}
// =============================================================================
// EXAMPLE 4: GPU-Aware Module Development
// =============================================================================
#[cfg(test)]
mod example_module_gpu_optimization {
use crate::gpu_detect::{GpuInfo, GpuSettings};
struct CustomModule {
name: String,
gpu_settings: Option<GpuSettings>,
}
impl CustomModule {
fn initialize_with_gpu(&mut self, gpu_info: &GpuInfo) {
let settings = GpuSettings::for_gpu(gpu_info);
self.gpu_settings = Some(settings);
println!("Module '{}' initialized with GPU settings:", self.name);
println!(" Tier: {:?}", gpu_info.tier);
println!(" Effect Intensity: {:.0}%", settings.effect_intensity * 100.0);
println!(" Particles: {}", settings.particle_count);
self.adjust_rendering();
}
fn adjust_rendering(&self) {
if let Some(settings) = &self.gpu_settings {
if settings.effect_intensity < 0.5 {
println!(" → Using lightweight shaders");
println!(" → Disabling particle effects");
} else if settings.effect_intensity < 0.8 {
println!(" → Using standard shaders");
println!(" → Reduced particle count");
} else {
println!(" → Using advanced shaders");
println!(" → Full particle effects");
}
}
}
}
#[test]
fn example() {
let gpu_info = crate::gpu_detect::GpuInfo {
tier: crate::gpu_detect::GpuTier::LowPower,
adapter_name: "AMD Radeon Graphics".to_string(),
backend: wgpu::BackendBit::VULKAN,
max_texture_size: 8192,
max_uniform_buffer_size: 65536,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
};
let mut module = CustomModule {
name: "CustomEffects".to_string(),
gpu_settings: None,
};
module.initialize_with_gpu(&gpu_info);
}
}
// =============================================================================
// EXAMPLE 5: Benchmarking GPU Performance
// =============================================================================
#[cfg(test)]
mod example_benchmarking {
use crate::gpu_benchmark::GpuBenchmark;
#[tokio::test]
async fn run_benchmark_suite() {
println!("Running comprehensive GPU benchmarks...\n");
// Benchmark iGPU
let mut igpu_bench =
GpuBenchmark::new(crate::gpu_detect::GpuTier::LowPower);
let _ = igpu_bench.run_all().await;
println!("{}", igpu_bench.report());
// Benchmark discrete GPU
let mut dgpu_bench =
GpuBenchmark::new(crate::gpu_detect::GpuTier::HighPerformance);
let _ = dgpu_bench.run_all().await;
println!("{}", dgpu_bench.report());
}
}
// =============================================================================
// EXAMPLE 6: Snow Effect with GPU Optimization
// =============================================================================
#[cfg(test)]
mod example_snow_optimization {
use crate::snow::SnowEffect;
use crate::gpu_detect::GpuSettings;
fn create_snow_effect_for_gpu(settings: &GpuSettings) -> SnowEffect {
println!("Creating snow effect:");
println!(" Particle Count: {}", settings.particle_count);
println!(" Effect Intensity: {:.0}%", settings.effect_intensity * 100.0);
// Create snow with GPU-optimized particle count
let snow = SnowEffect::with_particle_count(5, settings.particle_count as u64);
println!(" Status: ✓ Ready");
snow
}
#[test]
fn example() {
// iGPU settings
let igpu_settings = crate::gpu_detect::GpuSettings {
enable_snow_effect: true,
effect_intensity: 0.6,
particle_count: 150,
animation_fps_target: 45,
reduce_transparency_effects: true,
};
let snow = create_snow_effect_for_gpu(&igpu_settings);
println!("Snow particles: {}", snow.particles.len());
println!("\n---\n");
// dGPU settings
let dgpu_settings = crate::gpu_detect::GpuSettings {
enable_snow_effect: true,
effect_intensity: 1.0,
particle_count: 500,
animation_fps_target: 60,
reduce_transparency_effects: false,
};
let snow = create_snow_effect_for_gpu(&dgpu_settings);
println!("Snow particles: {}", snow.particles.len());
}
}
// =============================================================================
// EXAMPLE 7: Memory-Efficient Particle System for iGPU
// =============================================================================
#[cfg(test)]
mod example_efficient_particles {
use crate::gpu_detect::{GpuInfo, GpuTier};
struct ParticleSystem {
max_particles: usize,
memory_per_particle: usize,
}
impl ParticleSystem {
fn new_for_gpu(gpu_info: &GpuInfo) -> Self {
// Calculate max particles based on GPU and limits
let memory_available = match gpu_info.tier {
GpuTier::HighPerformance => 100 * 1024 * 1024, // 100 MB
GpuTier::LowPower => 20 * 1024 * 1024, // 20 MB (shared system RAM)
GpuTier::Software => 5 * 1024 * 1024, // 5 MB
};
let memory_per_particle = 32; // bytes: pos(8) + vel(8) + size(4) + opacity(4) + padding(8)
let max_particles = memory_available / memory_per_particle;
println!("ParticleSystem initialized for {:?}:", gpu_info.tier);
println!(" Memory available: {:.1} MB", memory_available as f64 / 1024.0 / 1024.0);
println!(" Memory per particle: {} bytes", memory_per_particle);
println!(" Max particles: {}", max_particles);
ParticleSystem {
max_particles,
memory_per_particle,
}
}
fn get_optimal_count(&self) -> usize {
// Use 80% of max to leave headroom
(self.max_particles as f64 * 0.8) as usize
}
}
#[test]
fn example() {
let igpu_info = crate::gpu_detect::GpuInfo {
tier: GpuTier::LowPower,
adapter_name: "Intel UHD".to_string(),
backend: wgpu::BackendBit::DX12,
max_texture_size: 16384,
max_uniform_buffer_size: 65536,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
};
let system = ParticleSystem::new_for_gpu(&igpu_info);
println!("\nOptimal particle count: {}\n", system.get_optimal_count());
let dgpu_info = crate::gpu_detect::GpuInfo {
tier: GpuTier::HighPerformance,
adapter_name: "NVIDIA GeForce".to_string(),
backend: wgpu::BackendBit::VULKAN,
max_texture_size: 16384,
max_uniform_buffer_size: 65536,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
};
let system = ParticleSystem::new_for_gpu(&dgpu_info);
println!("Optimal particle count: {}", system.get_optimal_count());
}
}
// =============================================================================
// EXAMPLE 8: Performance Monitoring Loop
// =============================================================================
#[cfg(test)]
mod example_perf_monitoring {
use std::time::Instant;
struct PerformanceMonitor {
frame_times: Vec<f64>,
max_samples: usize,
}
impl PerformanceMonitor {
fn new() -> Self {
PerformanceMonitor {
frame_times: Vec::new(),
max_samples: 120, // Last 2 seconds @ 60 FPS
}
}
fn record_frame(&mut self, frame_time_ms: f64) {
self.frame_times.push(frame_time_ms);
if self.frame_times.len() > self.max_samples {
self.frame_times.remove(0);
}
}
fn avg_frame_time(&self) -> f64 {
if self.frame_times.is_empty() {
0.0
} else {
self.frame_times.iter().sum::<f64>() / self.frame_times.len() as f64
}
}
fn max_frame_time(&self) -> f64 {
self.frame_times.iter().cloned().fold(0.0, f64::max)
}
fn fps(&self) -> f64 {
if self.avg_frame_time() > 0.0 {
1000.0 / self.avg_frame_time()
} else {
0.0
}
}
fn report(&self) {
println!("Performance Report:");
println!(" Avg Frame Time: {:.2} ms", self.avg_frame_time());
println!(" Max Frame Time: {:.2} ms", self.max_frame_time());
println!(" Average FPS: {:.1}", self.fps());
}
}
#[test]
fn example() {
let mut monitor = PerformanceMonitor::new();
// Simulate frame times
for i in 0..120 {
let frame_time = if i < 60 {
22.0 // 45 FPS
} else {
16.7 // 60 FPS
};
monitor.record_frame(frame_time);
}
monitor.report();
}
}