ruviz 0.4.0

High-performance 2D plotting library for Rust
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! GPU backend foundation with wgpu
//! High-performance GPU-accelerated rendering for massive datasets

use crate::core::error::PlottingError;
use crate::data::platform::get_platform_optimizer;
use std::sync::{Arc, Mutex, OnceLock};

/// GPU-specific error types
#[allow(clippy::enum_variant_names)] // All variants describe failure modes
#[derive(Debug, thiserror::Error)]
pub enum GpuError {
    #[error("GPU initialization failed: {0}")]
    InitializationFailed(String),
    #[error("Buffer operation failed: {0}")]
    BufferOperationFailed(String),
    #[error("Operation failed: {0}")]
    OperationFailed(String),
    #[error("Buffer creation failed: {0}")]
    BufferCreationFailed(String),
}

/// GPU operation result type
pub type GpuResult<T> = Result<T, GpuError>;

pub mod buffer;
pub mod compute;
pub mod device;
pub mod memory;
pub mod pipeline;
pub mod renderer;

pub use buffer::{BufferManager, BufferUsage, GpuBuffer};
pub use compute::{AggregationParams, ComputeManager, ComputeStats, TransformParams};
pub use device::{DeviceSelector, GpuDevice, GpuDeviceInfo};
pub use memory::{GpuMemoryPool, GpuMemoryStats, PooledGpuBuffer};
pub use pipeline::{ComputePipeline, PipelineCache, RenderPipeline};
pub use renderer::{GpuRenderer, GpuRendererStats, GpuVertex};

// Error types are already defined in this module, no need to re-export

/// GPU backend capabilities and configuration
#[derive(Clone)]
pub struct GpuBackend {
    device: Arc<GpuDevice>,
    buffer_manager: Arc<Mutex<BufferManager>>,
    pipeline_cache: Arc<Mutex<PipelineCache>>,
    capabilities: GpuCapabilities,
    config: GpuConfig,
}

/// GPU capabilities detected at runtime
#[derive(Debug, Clone)]
pub struct GpuCapabilities {
    /// Maximum texture size (width/height)
    pub max_texture_size: u32,
    /// Maximum buffer size in bytes
    pub max_buffer_size: u64,
    /// Maximum number of compute workgroups
    pub max_compute_workgroups: [u32; 3],
    /// Available memory in bytes
    pub memory_size: Option<u64>,
    /// Supports compute shaders
    pub supports_compute: bool,
    /// Supports storage textures
    pub supports_storage_textures: bool,
    /// Supports timestamp queries
    pub supports_timestamps: bool,
    /// Maximum number of concurrent render passes
    pub max_render_targets: u32,
    /// Supported texture formats
    pub supported_formats: Vec<wgpu::TextureFormat>,
}

/// GPU backend configuration
#[derive(Debug, Clone)]
pub struct GpuConfig {
    /// Enable GPU acceleration
    pub enable_gpu: bool,
    /// Preferred backend (Vulkan, Metal, DX12, GL)
    pub preferred_backend: Option<wgpu::Backends>,
    /// Memory usage limit as fraction of total GPU memory
    pub memory_limit_fraction: f32,
    /// Enable debug validation layers
    pub enable_validation: bool,
    /// Enable GPU profiling
    pub enable_profiling: bool,
    /// Force specific power preference
    pub power_preference: wgpu::PowerPreference,
    /// Required features
    pub required_features: wgpu::Features,
    /// Required limits
    pub required_limits: wgpu::Limits,
}

impl Default for GpuConfig {
    fn default() -> Self {
        Self {
            enable_gpu: true,
            preferred_backend: None,    // Auto-detect best backend
            memory_limit_fraction: 0.8, // Use 80% of GPU memory
            enable_validation: cfg!(debug_assertions),
            enable_profiling: false,
            power_preference: wgpu::PowerPreference::HighPerformance,
            required_features: wgpu::Features::empty(),
            required_limits: wgpu::Limits::default(),
        }
    }
}

impl GpuBackend {
    /// Initialize GPU backend with automatic device selection
    pub async fn new() -> Result<Self, PlottingError> {
        Self::with_config(GpuConfig::default()).await
    }

    /// Initialize GPU backend with custom configuration
    pub async fn with_config(config: GpuConfig) -> Result<Self, PlottingError> {
        if !config.enable_gpu {
            return Err(PlottingError::FeatureNotEnabled {
                feature: "GPU acceleration".to_string(),
                operation: "GPU backend initialization".to_string(),
            });
        }

        // Create wgpu instance with appropriate backends
        let instance = Self::create_instance(&config)?;

        // Select and initialize device
        let device = GpuDevice::new(&instance, &config).await?;
        let capabilities = Self::detect_capabilities(&device)?;

        // Validate minimum requirements
        Self::validate_capabilities(&capabilities, &config)?;

        // Initialize buffer manager with platform-optimized settings
        let platform_optimizer = get_platform_optimizer();
        let hints = platform_optimizer.get_performance_hints();
        let buffer_manager = BufferManager::new(&device, &capabilities, &hints)?;

        // Initialize pipeline cache
        let pipeline_cache = PipelineCache::new();

        Ok(Self {
            device: Arc::new(device),
            buffer_manager: Arc::new(Mutex::new(buffer_manager)),
            pipeline_cache: Arc::new(Mutex::new(pipeline_cache)),
            capabilities,
            config,
        })
    }

    /// Create wgpu instance with platform-appropriate backends
    fn create_instance(config: &GpuConfig) -> Result<wgpu::Instance, PlottingError> {
        let backends = config.preferred_backend.unwrap_or_else(|| {
            // Select best backend for platform
            #[cfg(target_os = "windows")]
            return wgpu::Backends::DX12 | wgpu::Backends::VULKAN;

            #[cfg(target_os = "macos")]
            return wgpu::Backends::METAL;

            #[cfg(target_os = "linux")]
            return wgpu::Backends::VULKAN | wgpu::Backends::GL;

            #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
            return wgpu::Backends::all();
        });

        let instance_desc = wgpu::InstanceDescriptor {
            backends,
            flags: if config.enable_validation {
                wgpu::InstanceFlags::DEBUG | wgpu::InstanceFlags::VALIDATION
            } else {
                wgpu::InstanceFlags::default()
            },
            memory_budget_thresholds: Default::default(),
            backend_options: Default::default(),
            display: None,
        };

        Ok(wgpu::Instance::new(instance_desc))
    }

    /// Detect GPU capabilities
    fn detect_capabilities(device: &GpuDevice) -> Result<GpuCapabilities, PlottingError> {
        let limits = device.limits();
        let features = device.features();

        Ok(GpuCapabilities {
            max_texture_size: limits.max_texture_dimension_2d,
            max_buffer_size: limits.max_buffer_size,
            max_compute_workgroups: [
                limits.max_compute_workgroups_per_dimension,
                limits.max_compute_workgroups_per_dimension,
                limits.max_compute_workgroups_per_dimension,
            ],
            memory_size: None,      // wgpu doesn't expose memory info directly
            supports_compute: true, // Assume compute shader support for now
            supports_storage_textures: features
                .contains(wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY),
            supports_timestamps: features.contains(wgpu::Features::TIMESTAMP_QUERY),
            max_render_targets: limits.max_color_attachments,
            supported_formats: Self::get_supported_formats(device),
        })
    }

    /// Get list of supported texture formats
    fn get_supported_formats(device: &GpuDevice) -> Vec<wgpu::TextureFormat> {
        let common_formats = [
            wgpu::TextureFormat::R8Unorm,
            wgpu::TextureFormat::Rg8Unorm,
            wgpu::TextureFormat::Rgba8Unorm,
            wgpu::TextureFormat::Rgba8UnormSrgb,
            wgpu::TextureFormat::R16Float,
            wgpu::TextureFormat::Rg16Float,
            wgpu::TextureFormat::Rgba16Float,
            wgpu::TextureFormat::R32Float,
            wgpu::TextureFormat::Rg32Float,
            wgpu::TextureFormat::Rgba32Float,
            wgpu::TextureFormat::Bgra8Unorm,
            wgpu::TextureFormat::Bgra8UnormSrgb,
        ];

        common_formats
            .into_iter()
            .filter(|&format| {
                device
                    .adapter()
                    .get_texture_format_features(format)
                    .allowed_usages
                    .contains(
                        wgpu::TextureUsages::RENDER_ATTACHMENT
                            | wgpu::TextureUsages::TEXTURE_BINDING,
                    )
            })
            .collect()
    }

    /// Validate that GPU meets minimum requirements
    fn validate_capabilities(
        capabilities: &GpuCapabilities,
        config: &GpuConfig,
    ) -> Result<(), PlottingError> {
        // Check minimum texture size (need at least 4K for reasonable plots)
        if capabilities.max_texture_size < 4096 {
            return Err(PlottingError::UnsupportedGpuFeature(format!(
                "Maximum texture size {} is too small (minimum 4096)",
                capabilities.max_texture_size
            )));
        }

        // Check compute shader support if required
        if !capabilities.supports_compute
            && config.required_features.contains(wgpu::Features::empty())
        {
            return Err(PlottingError::UnsupportedGpuFeature(
                "Compute shaders required but not supported".to_string(),
            ));
        }

        // Check minimum buffer size (need at least 256MB for large datasets)
        const MIN_BUFFER_SIZE: u64 = 256 * 1024 * 1024; // 256MB
        if capabilities.max_buffer_size < MIN_BUFFER_SIZE {
            return Err(PlottingError::UnsupportedGpuFeature(format!(
                "Maximum buffer size {} is too small (minimum {})",
                capabilities.max_buffer_size, MIN_BUFFER_SIZE
            )));
        }

        Ok(())
    }

    /// Get GPU device reference
    pub fn device(&self) -> &Arc<GpuDevice> {
        &self.device
    }

    /// Get GPU queue
    pub fn queue(&self) -> &Arc<wgpu::Queue> {
        self.device.queue()
    }

    /// Get GPU capabilities
    pub fn capabilities(&self) -> &GpuCapabilities {
        &self.capabilities
    }

    /// Get GPU configuration
    pub fn config(&self) -> &GpuConfig {
        &self.config
    }

    /// Get buffer manager
    pub fn buffer_manager(&self) -> Arc<Mutex<BufferManager>> {
        Arc::clone(&self.buffer_manager)
    }

    /// Get pipeline cache
    pub fn pipeline_cache(&self) -> Arc<Mutex<PipelineCache>> {
        Arc::clone(&self.pipeline_cache)
    }

    /// Create render pass for plotting operations
    pub fn create_render_pass(
        &self,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
    ) -> Result<GpuRenderPass, PlottingError> {
        GpuRenderPass::new(&self.device, width, height, format, &self.capabilities)
    }

    /// Create compute manager for data processing
    pub fn create_compute_manager(&self) -> Result<ComputeManager, PlottingError> {
        if !self.capabilities.supports_compute {
            return Err(PlottingError::UnsupportedGpuFeature(
                "Compute shaders not supported".to_string(),
            ));
        }

        Ok(ComputeManager::new(
            Arc::clone(self.device.device()),
            Arc::clone(self.device.queue()),
        ))
    }

    /// Check if GPU backend is available and functional
    pub fn is_available(&self) -> bool {
        self.device.is_valid()
    }

    /// Get performance statistics
    pub fn get_stats(&self) -> GpuStats {
        let buffer_manager = self.buffer_manager.lock().unwrap();
        let pipeline_cache = self.pipeline_cache.lock().unwrap();

        GpuStats {
            device_info: self.device.info().clone(),
            buffer_stats: buffer_manager.get_stats(),
            pipeline_stats: pipeline_cache.get_stats(),
            memory_usage: buffer_manager.get_memory_usage(),
            active_passes: 0, // TODO: track active render/compute passes
        }
    }
}

/// GPU render pass for drawing operations
pub struct GpuRenderPass {
    texture: wgpu::Texture,
    view: wgpu::TextureView,
    format: wgpu::TextureFormat,
    width: u32,
    height: u32,
    depth_texture: Option<wgpu::Texture>,
    depth_view: Option<wgpu::TextureView>,
}

impl GpuRenderPass {
    fn new(
        device: &GpuDevice,
        width: u32,
        height: u32,
        format: wgpu::TextureFormat,
        capabilities: &GpuCapabilities,
    ) -> Result<Self, PlottingError> {
        // Validate dimensions
        if width > capabilities.max_texture_size || height > capabilities.max_texture_size {
            return Err(PlottingError::GpuMemoryError {
                requested: (width * height * 4) as usize, // Assume RGBA format
                available: Some(
                    (capabilities.max_texture_size * capabilities.max_texture_size * 4) as usize,
                ),
            });
        }

        // Create main render texture
        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("Render Target"),
            size: wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());

        // Create depth buffer if supported
        let (depth_texture, depth_view) = if capabilities
            .supported_formats
            .contains(&wgpu::TextureFormat::Depth32Float)
        {
            let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
                label: Some("Depth Buffer"),
                size: wgpu::Extent3d {
                    width,
                    height,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Depth32Float,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
                view_formats: &[],
            });

            let depth_view = depth_texture.create_view(&wgpu::TextureViewDescriptor::default());
            (Some(depth_texture), Some(depth_view))
        } else {
            (None, None)
        };

        Ok(Self {
            texture,
            view,
            format,
            width,
            height,
            depth_texture,
            depth_view,
        })
    }

    pub fn color_view(&self) -> &wgpu::TextureView {
        &self.view
    }

    pub fn depth_view(&self) -> Option<&wgpu::TextureView> {
        self.depth_view.as_ref()
    }

    pub fn dimensions(&self) -> (u32, u32) {
        (self.width, self.height)
    }

    pub fn format(&self) -> wgpu::TextureFormat {
        self.format
    }
}

/// GPU performance statistics
#[derive(Debug, Clone)]
pub struct GpuStats {
    pub device_info: GpuDeviceInfo,
    pub buffer_stats: BufferStats,
    pub pipeline_stats: PipelineStats,
    pub memory_usage: u64,
    pub active_passes: u32,
}

#[derive(Debug, Clone)]
pub struct BufferStats {
    pub total_buffers: usize,
    pub total_memory: u64,
    pub active_buffers: usize,
    pub reused_buffers: usize,
}

#[derive(Debug, Clone)]
pub struct PipelineStats {
    pub total_pipelines: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
}

/// Global GPU backend instance
#[cfg(not(target_arch = "wasm32"))]
static GPU_BACKEND: OnceLock<Option<GpuBackend>> = OnceLock::new();

/// Create a fresh GPU backend instance without global caching.
pub async fn create_gpu_backend() -> Result<GpuBackend, PlottingError> {
    GpuBackend::new().await
}

/// Initialize GPU backend (call once at startup)
#[cfg(not(target_arch = "wasm32"))]
pub async fn initialize_gpu_backend() -> Result<(), PlottingError> {
    let backend = match create_gpu_backend().await {
        Ok(backend) => Some(backend),
        Err(e) => {
            log::warn!("Failed to initialize GPU backend: {}", e);
            None
        }
    };

    GPU_BACKEND
        .set(backend)
        .map_err(|_| PlottingError::GpuInitError {
            backend: "wgpu".to_string(),
            error: "Backend already initialized".to_string(),
        })?;

    Ok(())
}

/// Initialize GPU backend on wasm without storing a global backend.
#[cfg(target_arch = "wasm32")]
pub async fn initialize_gpu_backend() -> Result<(), PlottingError> {
    if let Err(err) = create_gpu_backend().await {
        log::warn!("Failed to initialize GPU backend: {}", err);
    }

    Ok(())
}

/// Get global GPU backend instance
#[cfg(not(target_arch = "wasm32"))]
pub fn get_gpu_backend() -> Option<&'static GpuBackend> {
    GPU_BACKEND.get().and_then(|backend| backend.as_ref())
}

/// No global GPU backend is cached on wasm targets.
#[cfg(target_arch = "wasm32")]
pub fn get_gpu_backend() -> Option<&'static GpuBackend> {
    None
}

/// Check if GPU acceleration is available
pub fn is_gpu_available() -> bool {
    get_gpu_backend().is_some_and(|backend| backend.is_available())
}

/// Get GPU capabilities if available
pub fn get_gpu_capabilities() -> Option<&'static GpuCapabilities> {
    get_gpu_backend().map(|backend| backend.capabilities())
}