Oak HLSL Parser
A high-performance High Level Shading Language (HLSL) parser for Rust, built with the Oak parser combinator framework. Parse DirectX shader code with comprehensive AST generation and error handling.
Overview
Oak HLSL provides robust parsing capabilities for High Level Shading Language files, supporting vertex shaders, pixel shaders, compute shaders, and all major HLSL constructs. Built on the Oak parser combinator framework, it delivers excellent performance and detailed error messages.
Features
- ✅ Complete HLSL Support: Parse shaders, functions, structs, constants, and semantics
- ✅ Modern Rust API: Type-safe parsing with comprehensive error handling
- ✅ High Performance: Built on the efficient Oak parser combinator framework
- ✅ Rich AST: Detailed Abstract Syntax Tree with source location tracking
- ✅ Extensible: Easy to extend for custom HLSL dialects
- ✅ Well Tested: Comprehensive test suite with real-world examples
Quick Start
Parsing Examples
Basic Vertex Shader Parsing
use ;
use HLSLLanguage;
Complex Pixel Shader with Textures
use ;
use HLSLLanguage;
Advanced Features
Compute Shaders
Oak HLSL supports parsing compute shaders with thread group semantics:
let source = r#"
RWTexture2D<float4> gOutput : register(u0);
[numthreads(8, 8, 1)]
void main(uint3 id : SV_DispatchThreadID) {
float4 color = float4(id.x / 255.0, id.y / 255.0, 0.5, 1.0);
gOutput[id.xy] = color;
}
"#;
Techniques and Passes
Parse effect files with techniques and passes:
let source = r#"
technique11 Main {
pass P0 {
SetVertexShader(CompileShader(vs_5_0, VSMain()));
SetPixelShader(CompileShader(ps_5_0, PSMain()));
SetGeometryShader(NULL);
}
}
"#;
AST Structure
The parser generates a rich AST with the following main node types:
HLSLFile- Root node containing the entire fileStruct- Structure definitions with semanticsFunction- Shader functions with return semanticsVariable- Variable declarations with types and semanticsTechnique- Technique blocks with passesPass- Render pass definitionsExpression- HLSL expressions and operatorsStatement- Control flow and assignment statements
Performance
Oak HLSL is designed for high performance:
- Zero-copy parsing where possible
- Streaming support for large shader files
- Efficient memory usage with minimal allocations
- Fast error recovery for better developer experience
Integration
Oak HLSL integrates seamlessly with the Oak ecosystem:
use ;
use HLSLLanguage;
// Use with other Oak parsers
let mut parser = new;
let result = parser.parse;
Examples
More examples can be found in the examples directory:
- Vertex shader parsing
- Pixel shader parsing
- Compute shader parsing
- Effect file parsing
- Error handling
Contributing
We welcome contributions! Please see our Contributing Guide for details.