shdrlib
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.
Overview
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:
- Multi-language shader support (WGSL, GLSL, SPIR-V)
- Automatic GPU device and queue management
- Pipeline builder pattern for render configuration
- Asset management system for shaders and pipelines
- Offscreen and window-based rendering support
Architecture
The library is organized into three primary modules:
Frontend (front/)
Handles shader parsing and compilation to Naga IR:
compiler.rs: Parses WGSL, GLSL, and SPIR-V into Naga's intermediate representationstorage.rs: ProvidesShaderObjectwrapper for compiled shader data
Backend (back/)
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 configuration
Managers (managers/)
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 rendering
Installation
Add shdrlib to your Cargo.toml:
[]
= "0.2.0"
Usage
Basic Example
use ;
Window Rendering
For window-based rendering with winit, see examples/window_demo.rs.
Supported Shader Languages
- WGSL: WebGPU Shading Language (recommended)
- GLSL: OpenGL Shading Language
- SPIR-V: Binary intermediate representation
Examples
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 withwinit
Run an example:
Design Goals
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:
- Raw wgpu setup: ~150-200 lines for basic rendering
- shdrlib setup: ~10 lines of actual configuration code
The library is suitable for:
- Rapid prototyping of shader effects
- Educational purposes and shader experimentation
- Applications requiring simplified GPU access
- Research projects involving shader compilation pipelines
Dependencies
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)
Testing
Run the test suite:
Tests verify shader compilation, GPU device initialization, and pipeline creation.
License
This project is licensed under the MIT License. It’s provided as-is
Contributing
Contributions are welcome. Please ensure code follows existing patterns and includes appropriate tests.
Acknowledgments
Built on the wgpu and naga projects, which provide the underlying GPU abstraction and shader infrastructure.