init wgpu + doc + err robustes
This commit is contained in:
@@ -3,6 +3,10 @@ name = "wsg-lib"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
|
||||
[dependencies]
|
||||
wgpu = "30.0.0" # Vérifiez la version la plus récente
|
||||
winit = "0.30" # Pour la gestion de la fenêtre
|
||||
thiserror = "2"
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
//! # Context Module
|
||||
//!
|
||||
//! Initializes the GPU, creates the surface, and holds the Device and Queue. It is static (created once at startup).
|
||||
|
||||
use std::sync::Arc;
|
||||
use wgpu::{Adapter, Device, Instance, Queue, Surface};
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::error::WsgError;
|
||||
|
||||
/// Represents the GPU context. Holds all WGPU objects needed for rendering.
|
||||
/// Created once at startup and shared across frames via Arc.
|
||||
pub struct Context {
|
||||
/// The entry point to wgpu — manages connections with graphics drivers (Vulkan, Metal, DX12).
|
||||
pub instance: Instance,
|
||||
/// Rendering target surface linking wgpu to the window (winit).
|
||||
pub surface: Surface<'static>,
|
||||
/// Physical or software GPU adapter selected by the user.
|
||||
pub adapter: Adapter,
|
||||
/// The engine core — creates buffers, textures, pipelines.
|
||||
pub device: Device,
|
||||
/// Command submission queue — drawing commands are sent here for execution.
|
||||
pub queue: Queue,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
/// Initializes the WGPU context. Creates the surface from the window, requests a device from the adapter,
|
||||
/// and stores all required objects (instance, surface, adapter, device, queue).
|
||||
/// Called once at application startup. Returns an error if GPU initialization fails.
|
||||
pub async fn new(window: Arc<Window>) -> Result<Self, WsgError> {
|
||||
// WGPU instance
|
||||
let instance = wgpu::Instance::default();
|
||||
|
||||
// Surface (bound to window lifecycle, so unsafe)
|
||||
let surface = instance
|
||||
.create_surface(window)
|
||||
.map_err(|e| WsgError::SurfaceCreation(e))?;
|
||||
|
||||
// Request for adapter (GPU)
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
compatible_surface: Some(&surface),
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.map_err(|_| WsgError::NoAdapter)?;
|
||||
|
||||
// Request for adapter device and queue
|
||||
let (device, queue) = adapter
|
||||
.request_device(&wgpu::DeviceDescriptor::default())
|
||||
.await
|
||||
.map_err(|_| WsgError::DeviceCreation)?;
|
||||
|
||||
Ok(Self {
|
||||
instance,
|
||||
surface,
|
||||
adapter,
|
||||
device,
|
||||
queue,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum WsgError {
|
||||
/// Error from the windowing system (winit)
|
||||
#[error("Window system error (winit)")]
|
||||
WindowSystem,
|
||||
|
||||
/// No suitable graphics adapter found
|
||||
#[error("No graphics adapter found")]
|
||||
NoAdapter,
|
||||
|
||||
/// Failed to create a WGPU device
|
||||
#[error("Failed to create WGPU device")]
|
||||
DeviceCreation,
|
||||
|
||||
/// Shader compilation or creation error
|
||||
#[error("Shader compilation or creation error: {0}")]
|
||||
ShaderError(String),
|
||||
|
||||
/// Internal WGPU error
|
||||
#[error("Internal WGPU error: {0}")]
|
||||
InternalWgpu(#[from] wgpu::RequestDeviceError),
|
||||
|
||||
/// Failed to create the rendering surface
|
||||
#[error("Failed to create rendering surface")]
|
||||
SurfaceCreation(wgpu::CreateSurfaceError),
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod context;
|
||||
pub mod error;
|
||||
pub mod renderer;
|
||||
|
||||
pub use error::WsgError; // Pour permettre un import direct du type
|
||||
|
||||
Reference in New Issue
Block a user