Oak GLSL Parser
A high-performance GLSL (OpenGL Shading Language) parser for Rust, built with the Oak parser combinator framework. Parse shader programs with comprehensive AST generation and error handling.
Overview
Oak GLSL provides robust parsing capabilities for GLSL shader files, supporting vertex, fragment, geometry, and compute shaders. Built on the Oak parser combinator framework, it delivers excellent performance and detailed error messages.
Features
- ✅ Complete GLSL Support: Parse vertex, fragment, geometry, and compute shaders
- ✅ 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 shader dialects
- ✅ Well Tested: Comprehensive test suite with real-world examples
Quick Start
Add Oak GLSL to your Cargo.toml:
📋 Parsing Examples
Basic Vertex Shader Parsing
use ;
use GLSLLanguage;
Advanced Fragment Shader with Functions
use ;
use GLSLLanguage;
Advanced Features
Compute Shader Parsing
Oak GLSL supports parsing compute shaders:
let source = r#"
#version 430
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(std430, binding = 0) buffer InputBuffer {
float data[];
} inputBuffer;
layout(std430, binding = 1) buffer OutputBuffer {
float result[];
} outputBuffer;
uniform float multiplier;
void main() {
uint index = gl_GlobalInvocationID.x;
if (index < inputBuffer.data.length()) {
outputBuffer.result[index] = inputBuffer.data[index] * multiplier;
}
}
"#;
Geometry Shader Support
Parse geometry shaders with input/output layout qualifiers:
let source = r#"
#version 330 core
layout(points) in;
layout(line_strip, max_vertices = 2) out;
uniform mat4 projection;
uniform float time;
void main() {
vec4 pos = gl_in[0].gl_Position;
gl_Position = projection * (pos + vec4(-0.1, 0.0, 0.0, 0.0));
EmitVertex();
gl_Position = projection * (pos + vec4(0.1, 0.0, 0.0, 0.0));
EmitVertex();
EndPrimitive();
}
"#;
Preprocessor Directives
Handle GLSL preprocessor directives:
let source = r#"
#version 450 core
#define MAX_LIGHTS 16
#define PI 3.14159265359
#ifdef USE_NORMAL_MAPPING
#extension GL_OES_standard_derivatives : enable
#endif
#if defined(USE_SHADOWS) && defined(USE_PCF)
#define SHADOW_FILTER_SIZE 3
#endif
// Shader code continues...
"#;
AST Structure
The parser generates a rich AST with the following main node types:
GLSLFile- Root node containing the entire shaderVersionDirective- GLSL version specificationExtensionDirective- Extension directivesPreprocessorDirective- Preprocessor commandsFunctionDefinition- Function definitionsStructDefinition- Struct type definitionsVariableDeclaration- Variable declarationsTypeQualifier- Type qualifiers (in, out, uniform, etc.)Expression- Mathematical and logical expressionsStatement- Control flow statements
Performance
Oak GLSL 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 GLSL integrates seamlessly with the Oak ecosystem:
use ;
use GLSLLanguage;
// Use with other Oak parsers
let mut parser = new;
let result = parser.parse;
Examples
More examples can be found in the examples directory:
- Basic vertex shader
- Fragment shader with lighting
- Compute shader
- Geometry shader
- Preprocessor directives
Contributing
We welcome contributions! Please see our Contributing Guide for details.