| Crates.io | shdrlib |
| lib.rs | shdrlib |
| version | 0.2.0 |
| created_at | 2025-10-26 23:58:58.584994+00 |
| updated_at | 2025-11-13 08:01:55.360315+00 |
| description | High-level shader compilation and rendering library built on wgpu and naga |
| homepage | |
| repository | https://github.com/Geezy9/shdrlib2 |
| max_upload_size | |
| id | 1902032 |
| size | 111,773 |
A high-level shader compilation and rendering library for Rust, built on top of wgpu and naga. shdrlib abstracts away the complexity of GPU programming, allowing developers to focus on shader logic rather than boilerplate setup.
shdrlib provides a streamlined API for shader compilation, pipeline management, and GPU-accelerated rendering. The library handles device initialization, shader validation, pipeline configuration, and render execution with minimal user intervention.
Key Features:
The library is organized into three primary modules:
front/)Handles shader parsing and compilation to Naga IR:
compiler.rs: Parses WGSL, GLSL, and SPIR-V into Naga's intermediate representationstorage.rs: Provides ShaderObject wrapper for compiled shader databack/)Manages wgpu integration:
device.rs: GPU device, queue, and adapter initializationshader.rs: Conversion from Naga IR to wgpu shader modulespipeline.rs: Builder pattern for render pipeline configurationmanagers/)High-level API for asset and runtime management:
asset_manager.rs: Stores and manages shaders and pipelines by nameruntime_manager.rs: Executes render passes and manages surface renderingAdd shdrlib to your Cargo.toml:
[dependencies]
shdrlib = "0.2.0"
use shdrlib::{AssetManager, RuntimeManager, Language};
fn main() {
// Define your shader
let shader_source = r#"
@vertex
fn vs_main(@builtin(vertex_index) idx: u32) -> @builtin(position) vec4<f32> {
var positions = array<vec2<f32>, 3>(
vec2(0.0, 0.5), vec2(-0.5, -0.5), vec2(0.5, -0.5)
);
return vec4<f32>(positions[idx], 0.0, 1.0);
}
@fragment
fn fs_main() -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 0.0, 0.5, 1.0);
}
"#;
// Initialize asset manager (handles GPU setup automatically)
let mut assets = AssetManager::new();
// vertex example
assets.add_shader(
"vertex",
shader_source,
naga::ShaderStage::Vertex,
Language::WGSL,
).unwrap();
// fragment example
assets.add_shader(
"fragment",
shader_source,
naga::ShaderStage::Fragment,
Language::WGSL,
).unwrap();
// Create render pipeline
assets.create_pipeline(
"main_pipeline",
"vertex",
Some("fragment"),
vec![],
wgpu::TextureFormat::Rgba8UnormSrgb,
).unwrap();
// Render to texture
let runtime = RuntimeManager::new(&assets);
let texture = runtime.render_to_texture(
"main_pipeline",
800,
600,
wgpu::TextureFormat::Rgba8UnormSrgb,
wgpu::Color::BLACK,
3,
).unwrap();
println!("Rendered texture: {}x{}", texture.width(), texture.height());
}
For window-based rendering with winit, see examples/window_demo.rs.
The repository includes several examples demonstrating different use cases:
minimal_usage.rs: Minimal offscreen rendering exampletriangle_demo.rs: Complete offscreen render with pixel readbackwindow_demo.rs: Real-time window rendering with winitRun an example:
cargo run --example minimal_usage
shdrlib aims to reduce the verbosity of GPU programming without sacrificing control. The library handles initialization and resource management while exposing key configuration options for pipeline customization.
Comparison to raw wgpu:
The library is suitable for:
wgpu (v22): Cross-platform GPU APInaga (v22): Shader translation and validationpollster (v0.3): Blocking executor for async operationswinit (v0.30): Window creation (optional, for examples)Run the test suite:
cargo test
Tests verify shader compilation, GPU device initialization, and pipeline creation.
This project is licensed under the MIT License. It’s provided as-is
Contributions are welcome. Please ensure code follows existing patterns and includes appropriate tests.
Built on the wgpu and naga projects, which provide the underlying GPU abstraction and shader infrastructure.