shdrlib 0.1.2

A three-tiered Vulkan shader compilation and rendering framework built in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Contributing to shdrlib


Thank you for your interest in contributing to shdrlib! This document provides guidelines and instructions for contributing to the project.

## Table of Contents


- [Code of Conduct]#code-of-conduct
- [Getting Started]#getting-started
- [Development Workflow]#development-workflow
- [Project Architecture]#project-architecture
- [Coding Standards]#coding-standards
- [Testing Requirements]#testing-requirements
- [Documentation Guidelines]#documentation-guidelines
- [Submitting Changes]#submitting-changes
- [Tier-Specific Guidelines]#tier-specific-guidelines

---

## Code of Conduct


This project follows the Rust Code of Conduct. Please be respectful and constructive in all interactions.

---

## Getting Started


### Prerequisites


- **Rust**: 1.82+ (Edition 2024)
- **Vulkan SDK**: 1.3+ (for validation layers during development)
- **Git**: For version control

### Setting Up Your Development Environment


1. **Clone the repository**:
   ```bash
   git clone https://github.com/paulburnettjones-wq/shdrlib.git

   cd shdrlib

   ```

2. **Build the project**:
   ```bash
   cargo build

   ```

3. **Run tests**:
   ```bash
   cargo test

   ```

4. **Run examples**:
   ```bash
   cargo run --bin ex_01_triangle_100_lines

   ```

5. **Generate documentation**:
   ```bash
   cargo doc --open

   ```

---

## Development Workflow


### Branching Strategy


- `main` - Stable, production-ready code
- `develop` - Integration branch for features
- `feature/*` - Feature development branches
- `fix/*` - Bug fix branches
- `docs/*` - Documentation improvement branches

### Typical Workflow


1. Fork the repository
2. Create a feature branch: `git checkout -b feature/your-feature-name`
3. Make your changes
4. Write/update tests
5. Update documentation
6. Run tests: `cargo test`
7. Run clippy: `cargo clippy -- -D warnings`
8. Format code: `cargo fmt`
9. Commit with descriptive messages
10. Push to your fork
11. Open a Pull Request

---

## Project Architecture


shdrlib uses a **three-tier architecture**. Understanding this structure is crucial for contributions:

### CORE Tier (Tier 0)

- **Purpose**: Thin wrappers around Vulkan/ash
- **Philosophy**: Minimal abstraction, maximum control
- **Safety**: Manual lifetime management (can cause UB if misused)
- **Location**: `src/core/`

### EX Tier (Tier 1)

- **Purpose**: Safe, ergonomic managers with explicit configuration
- **Philosophy**: 4x-8x code reduction, zero-cost abstractions
- **Safety**: Guaranteed memory safety via Rust ownership
- **Location**: `src/ex/`

### EZ Tier (Tier 2)

- **Purpose**: High-level abstractions with intelligent defaults
- **Philosophy**: Beginner-friendly, rapid prototyping
- **Safety**: Foolproof APIs
- **Location**: `src/ez/` (currently planned)

**See `DEVELOPMENT_PLAN.md` for detailed architecture documentation.**

---

## Coding Standards


### Rust Style


- Follow the [Rust API Guidelines]https://rust-lang.github.io/api-guidelines/
- Use `cargo fmt` with default settings
- Use `cargo clippy` and address all warnings
- Maximum line length: 100 characters (soft limit)

### Naming Conventions


```rust
// Types: PascalCase
pub struct RuntimeManager { ... }
pub enum ShaderStage { ... }

// Functions/methods: snake_case
pub fn create_shader(&self) -> Result<Shader> { ... }

// Constants: SCREAMING_SNAKE_CASE
pub const MAX_FRAMES_IN_FLIGHT: u32 = 2;

// Newtype IDs: PascalCase with suffix
pub struct ShaderId(usize);
pub struct PipelineId(usize);
```

### Error Handling


- Use `thiserror` for all error types
- Provide context in error messages
- Implement `From` conversions for error propagation
- Never use `unwrap()` or `expect()` in library code
- Document error conditions in rustdoc

Example:
```rust
#[derive(Error, Debug)]

pub enum MyError {
    #[error("Operation failed: {0}")]
    OperationFailed(String),
    
    #[error("Invalid configuration: {0}")]
    InvalidConfig(#[from] ConfigError),
}
```

### Documentation


- All public items must have rustdoc comments
- Include examples for complex APIs
- Document safety requirements for unsafe code
- Link to related items using `[Type]` or `[function]`

Example:
```rust
/// Creates a new shader from GLSL source code.
///
/// This function compiles the GLSL source to SPIR-V using naga,
/// then creates a Vulkan shader module.
///
/// # Arguments
///
/// * `device` - The logical device to create the shader on
/// * `source` - GLSL source code as a string
/// * `stage` - The shader stage (vertex, fragment, etc.)
///
/// # Examples
///
/// ```no_run
/// # use shdrlib::core::*;
/// let shader = Shader::from_glsl(
///     &device,
///     "#version 450\nvoid main() {}",
///     ShaderStage::Vertex
/// )?;
/// # Ok::<(), ShaderError>(())
/// ```
///
/// # Errors
///
/// Returns `ShaderError::CompilationFailed` if GLSL is invalid.
pub fn from_glsl(...) -> Result<Self, ShaderError> { ... }
```

### Inlining


- Use `#[inline]` for small accessor methods
- Use `#[inline]` for wrapper methods in EX tier
- Use `#[inline(always)]` sparingly, only for critical hot paths
- Document reasoning for `#[inline(always)]` usage

---

## Testing Requirements


### Test Coverage


- **All public APIs must have tests**
- **All error paths should be tested**
- Aim for 80%+ code coverage

### Test Organization


```rust
#[cfg(test)]

mod tests {
    use super::*;
    
    #[test]
    fn test_basic_functionality() {
        // Arrange
        let input = setup_test_data();
        
        // Act
        let result = function_under_test(input);
        
        // Assert
        assert!(result.is_ok());
    }
    
    #[test]
    fn test_error_handling() {
        let result = function_with_invalid_input();
        assert!(matches!(result, Err(MyError::InvalidInput(_))));
    }
}
```

### Running Tests


```bash
# Run all tests

cargo test

# Run specific tier tests

cargo test --lib core::
cargo test --lib ex::

# Run with output

cargo test -- --nocapture

# Run specific test

cargo test test_name
```

### Integration Tests


- Demo programs serve as integration tests
- Ensure all demos compile and run without errors
- Add new demos for significant new features

---

## Documentation Guidelines


### Markdown Files


- Follow existing structure in `md/` directory
- Use proper heading hierarchy
- Include code examples that compile
- Keep line length reasonable (~100 chars)

### Rustdoc

- First sentence should be a brief summary
- Use proper markdown formatting
- Include code examples for public APIs
- Document panics, errors, and safety requirements

### Updating Documentation


When making changes, update:
- [ ] Inline rustdoc comments
- [ ] Relevant markdown files in `md/`
- [ ] README.md if public API changes
- [ ] CHANGELOG.md with your changes
- [ ] Demo programs if behavior changes

---

## Submitting Changes


### Pull Request Process


1. **Ensure all tests pass**: `cargo test`
2. **Run clippy**: `cargo clippy -- -D warnings`
3. **Format code**: `cargo fmt --check`
4. **Update CHANGELOG.md** under "Unreleased" section
5. **Update documentation** as needed
6. **Write clear PR description** explaining:
   - What changed and why
   - Which tier(s) are affected
   - Any breaking changes
   - Related issues (if any)

### PR Title Format


```
[TIER] Brief description of change

Examples:
[CORE] Add support for dynamic rendering
[EX] Implement compute pipeline builder
[EZ] Add quick_texture helper method
[DOCS] Update EX tier usage guide
[FIX] Correct drop order in ShaderManager
```

### Commit Message Guidelines


```
Brief summary (50 chars or less)

More detailed explanation if needed. Wrap at 72 characters.
Include motivation for change and contrast with previous behavior.

- Bullet points are okay
- Use present tense: "Add feature" not "Added feature"
- Reference issues: "Fixes #123" or "Related to #456"
```

---

## Tier-Specific Guidelines

### CORE Tier Contributions

**Philosophy**: Keep it minimal and explicit

- DO: Provide thin wrappers around Vulkan objects
- DO: Use safe Rust where possible
- DO: Document lifetime requirements clearly
- DON'T: Add lifetime management or ownership tracking
- DON'T: Add convenience features (those go in EX tier)
- DON'T: Make assumptions about usage patterns

**Example Pattern**:
```rust
pub struct MyVulkanWrapper {
    handle: vk::MyVulkanHandle,
    device: Arc<Device>, // Only store what's needed for Drop
}

impl MyVulkanWrapper {
    pub fn new(device: &Arc<Device>, info: &CreateInfo) -> Result<Self> {
        // Minimal validation, then call Vulkan
        let handle = unsafe {
            device.handle().create_my_vulkan_object(info, None)
                .map_err(|e| MyError::CreationFailed(e))?
        };
        Ok(Self { handle, device: Arc::clone(device) })
    }
    
    #[inline]
    pub fn handle(&self) -> vk::MyVulkanHandle {
        self.handle
    }
}

impl Drop for MyVulkanWrapper {
    fn drop(&mut self) {
        unsafe {
            self.device.handle().destroy_my_vulkan_object(self.handle, None);
        }
    }
}
```

### EX Tier Contributions


**Philosophy**: Ergonomic, explicit, and safe

- DO: Provide safe lifetime management via Arc
- DO: Use builder patterns for complex configuration
- DO: Add helper utilities for common patterns
- DO: Use newtype pattern for type safety
- DO: Keep all configuration explicit (no hidden defaults)
- DON'T: Add magic behavior or hidden state
- DON'T: Sacrifice performance for convenience
- DON'T: Make assumptions about what users want

**Example Pattern**:
```rust
pub struct Manager {
    resources: Vec<Resource>,
    device: Arc<Device>, // Arc for safe sharing
}

impl Manager {
    #[inline]
    pub fn device(&self) -> Arc<Device> {
        Arc::clone(&self.device)
    }
    
    pub fn add_resource(&mut self, config: ResourceConfig) -> Result<ResourceId> {
        // Explicit configuration, no defaults
        let resource = Resource::new(&self.device, &config)?;
        self.resources.push(resource);
        Ok(ResourceId(self.resources.len() - 1))
    }
}
```

### EZ Tier Contributions (Future)


**Philosophy**: Simple, with intelligent defaults

- DO: Provide one-liner setup where possible
- DO: Use intelligent defaults for common cases
- DO: Focus on learning and rapid prototyping
- DO: Maintain ability to drop down to EX/CORE
- DON'T: Sacrifice correctness for simplicity
- DON'T: Lock users into limited APIs

---

## Review Process


### What Reviewers Look For


1. **Correctness**: Does the code work as intended?
2. **Safety**: Are there any memory safety issues?
3. **Performance**: Does it maintain zero-cost abstractions?
4. **Tests**: Are all paths tested?
5. **Documentation**: Is it clear and complete?
6. **Style**: Does it follow project conventions?
7. **Tier Alignment**: Does it fit the tier's philosophy?

### Response Time


- Initial review: Within 1 week
- Follow-up reviews: Within 3 days
- Merge: After approval + CI passes

---

## Getting Help


### Resources


- **Documentation**: `md/` directory and rustdoc
- **Examples**: `demos/` directory
- **Architecture**: `DEVELOPMENT_PLAN.md`
- **Status**: `PROJECT_STATUS.md`

### Questions?


- Open a GitHub Discussion
- Tag relevant maintainers
- Check existing issues and documentation

---

## License


By contributing to shdrlib, you agree that your contributions will be dual-licensed under MIT and Apache-2.0, consistent with the project's licensing.

---

Thank you for contributing to shdrlib.