shdrlib 0.1.0

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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# Troubleshooting Guide


Solutions to common shdrlib issues.

## Table of Contents

- [Installation Issues]#installation-issues
- [Runtime Errors]#runtime-errors
- [Shader Compilation]#shader-compilation
- [Rendering Issues]#rendering-issues
- [Performance Problems]#performance-problems
- [Platform-Specific]#platform-specific

---

## Installation Issues


### Rust Version Too Old


**Error:**
```
error: package `shdrlib` requires rustc 1.82.0 or newer
```

**Solution:**
```bash
rustup update stable
rustc --version  # Verify 1.82+
```

### Vulkan Loader Not Found


**Error:**
```
Error: VulkanLibraryLoadFailure
```

**Solutions:**

**Windows:**
1. Install latest graphics drivers
2. Install [Vulkan SDK]https://vulkan.lunarg.com/
3. Restart your system

**Linux:**
```bash
# Ubuntu/Debian

sudo apt install libvulkan1 vulkan-tools

# Fedora

sudo dnf install vulkan-loader vulkan-tools

# Arch

sudo pacman -S vulkan-icd-loader vulkan-tools
```

**macOS:**
```bash
brew install --cask vulkan-sdk
# Or download from vulkan.lunarg.com

```

**Verify:**
```bash
vulkaninfo | head -20
```

### Missing Validation Layers (Debug Only)


**Error:**
```
WARN: Validation layers requested but not available
```

**Solutions:**

This only affects debug builds. Either:

**Option 1: Install validation layers**
```bash
# Linux

sudo apt install vulkan-validationlayers

# macOS

# Included in Vulkan SDK


# Windows

# Included in Vulkan SDK

```

**Option 2: Build in release mode**
```bash
cargo build --release
```

### Compilation Errors


**Error:**
```
error: cannot find crate `ash`
```

**Solution:**
```bash
cargo add ash
```

---

## Runtime Errors


### No Suitable Physical Device


**Error:**
```
Error: NoSuitableDevice
```

**Causes:**
1. No GPU with Vulkan 1.3 support
2. Outdated graphics drivers
3. Running in VM without GPU pass-through

**Solutions:**

**Check Vulkan support:**
```bash
vulkaninfo | grep "apiVersion"
# Should show 1.3 or higher
```

**Update drivers:**
- **NVIDIA:** Download from nvidia.com
- **AMD:** Download from amd.com
- **Intel:** Download from intel.com

**Check available devices:**
```bash
vulkaninfo --summary
```

### Out of Memory


**Error:**
```
Error: VulkanError(ERROR_OUT_OF_DEVICE_MEMORY)
```

**Causes:**
- Creating too many large textures
- Memory leak (not destroying resources)
- GPU VRAM exhausted

**Solutions:**

**1. Destroy unused resources:**
```rust
// Resources are automatically dropped when they go out of scope
{
    let texture = renderer.create_texture(...)?;
    // Use texture
} // Automatically destroyed here
```

**2. Check resource sizes:**
```rust
// Instead of:
let huge_texture = renderer.create_texture(8192, 8192, ...)?; // 256MB!

// Try:
let texture = renderer.create_texture(2048, 2048, ...)?; // 16MB
```

**3. Profile memory usage:**
```bash
# Use GPU monitoring tools

nvidia-smi  # NVIDIA
radeontop   # AMD
intel_gpu_top  # Intel
```

### Device Lost


**Error:**
```
Error: VulkanError(ERROR_DEVICE_LOST)
```

**Causes:**
- GPU driver crash
- Infinite loop in shader
- Invalid GPU commands
- Hardware issue

**Solutions:**

**1. Check validation errors:**
```bash
# Run in debug mode

cargo run
# Check for validation warnings before the crash

```

**2. Simplify shaders:**
```glsl
// Remove complex logic
// Add early returns
// Check for infinite loops
```

**3. Update drivers:**
- Latest GPU drivers often fix crashes

**4. Check hardware:**
```bash
# Run GPU stress test

vkcube  # Should run without crashes
```

---

## Shader Compilation


### GLSL Syntax Errors


**Error:**
```
Error: ShaderCompilationFailed("unexpected token")
```

**Solutions:**

**1. Check GLSL version:**
```glsl
#version 450  // Required for Vulkan

```

**2. Common mistakes:**
```glsl
// ❌ Wrong
uniform mat4 transform;  // Missing layout

// ✅ Correct
layout(set = 0, binding = 0) uniform Transform {
    mat4 matrix;
} transform;
```

**3. Check extension support:**
```glsl
// Some extensions not supported by naga
#extension GL_ARB_xxx : enable  // May not work


// Use Vulkan-compatible GLSL instead
```

### Unsupported GLSL Features


**Error:**
```
Error: UnsupportedShaderFeature
```

**Cause:** naga doesn't support all GLSL extensions

**Solutions:**

**1. Use GLSL 450 core profile:**
```glsl
#version 450 core

// Avoid extensions
```

**2. Check naga compatibility:**
- Most GLSL 4.50 features supported
- Some extensions not available
- Ray tracing: limited support

**3. Use SPIR-V directly:**
```rust
// Pre-compile with glslc
// glslc shader.glsl -o shader.spv

// Load SPIR-V
let spirv = std::fs::read("shader.spv")?;
let shader = Shader::from_spirv(&device, &spirv, stage)?;
```

### Reflection Errors


**Error:**
```
Error: ReflectionFailed
```

**Cause:** Can't extract bindings/push constants from SPIR-V

**Solutions:**

**1. Use explicit layouts:**
```glsl
// ✅ Explicit
layout(set = 0, binding = 0) uniform sampler2D tex;
layout(set = 0, binding = 1) uniform UBO { mat4 proj; } ubo;

// ❌ Implicit layouts may fail reflection
uniform sampler2D tex;
```

**2. Disable reflection (advanced):**
```rust
// Use manual descriptor layouts
// Don't rely on shader reflection
```

---

## Rendering Issues


### Black Screen / Nothing Rendering


**Checklist:**

**1. Check validation errors:**
```bash
cargo run  # Debug mode enables validation
```

**2. Verify shaders:**
```glsl
// Add debug output
layout(location = 0) out vec4 outColor;
void main() {
    outColor = vec4(1.0, 0.0, 0.0, 1.0);  // Bright red
}
```

**3. Check viewport/scissor:**
```rust
frame.set_viewport(0.0, 0.0, width as f32, height as f32);
frame.set_scissor(0, 0, width, height);
```

**4. Verify pipeline:**
```rust
// Make sure pipeline is bound
frame.bind_pipeline(pipeline)?;
```

**5. Check draw calls:**
```rust
frame.draw(vertex_count, 1, 0, 0);  // At least 3 vertices for triangle
```

### Flickering / Stuttering


**Causes:**
- Synchronization issues
- Frame pacing problems
- GPU starvation

**Solutions:**

**1. Check frame synchronization:**
```rust
// Use proper frame pacing
runtime.begin_frame()?;
// ... render ...
runtime.end_frame(&Default::default())?;
```

**2. Enable VSync** (if using swapchain)

**3. Profile frame time:**
```rust
let start = std::time::Instant::now();
// ... render ...
println!("Frame time: {:?}", start.elapsed());
```

### Wrong Colors / Corruption


**Causes:**
- Wrong image format
- Incorrect data layout
- Missing color attachment

**Solutions:**

**1. Check format:**
```rust
// Make sure formats match
let texture = renderer.create_texture(
    256, 256,
    Format::R8G8B8A8_UNORM  // Common format
)?;
```

**2. Verify data layout:**
```rust
#[repr(C)]  // Required for GPU data

struct Vertex {
    pos: [f32; 2],
    color: [f32; 3],
}
```

**3. Check color attachment:**
```rust
PipelineBuilder::new()
    .color_attachment_formats(vec![Format::R8G8B8A8_UNORM])
    // Must match render target format
```

---

## Performance Problems


### Low FPS / Slow Rendering


**Profile first:**

**1. Check GPU vs CPU bottleneck:**
```rust
// Measure GPU time
let query_pool = /* create timestamp queries */;

// Measure CPU time
let start = Instant::now();
// ... render ...
println!("CPU time: {:?}", start.elapsed());
```

**2. Common CPU issues:**
- Too many draw calls → batch geometry
- Recreating resources → cache pipelines/buffers
- Synchronization stalls → overlap work

**3. Common GPU issues:**
- Overdraw → optimize triangle order
- Shader complexity → profile with RenderDoc
- Memory bandwidth → compress textures

### High Memory Usage


**1. Check texture sizes:**
```rust
// 4K texture = 64MB uncompressed
// Use compressed formats or smaller sizes
```

**2. Destroy unused resources:**
```rust
// Resources auto-drop, but you can be explicit
drop(old_texture);
```

**3. Profile allocations:**
```bash
# Use memory profiler

cargo build --release
valgrind --tool=massif ./target/release/my_app
```

### Compilation is Slow


**1. Use incremental compilation** (default)

**2. Reduce dependencies:**
```toml
# Only include what you need

[dependencies]
shdrlib = { version = "0.1", default-features = false }
```

**3. Use `cargo check`** for fast iteration:
```bash
cargo check  # Fast syntax checking
cargo build  # Full build when ready
```

---

## Platform-Specific


### Windows


**Issue: "vulkan-1.dll not found"**

**Solution:**
- Update graphics drivers
- Install Vulkan SDK
- Check `C:\Windows\System32\vulkan-1.dll` exists

**Issue: Multiple GPUs**

**Solution:**
```rust
// Select specific GPU via physical device
// shdrlib defaults to first discrete GPU
```

### Linux


**Issue: Permission denied for GPU**

**Solution:**
```bash
# Add user to video/render group

sudo usermod -a -G video,render $USER
# Log out and back in

```

**Issue: Wayland vs X11**

**Solution:**
```bash
# Force X11 if Wayland has issues

GDK_BACKEND=x11 cargo run
```

**Issue: Mesa driver too old**

**Solution:**
```bash
# Update Mesa

sudo apt update && sudo apt upgrade mesa-*

# Or use PPA for latest

sudo add-apt-repository ppa:kisak/kisak-mesa
sudo apt update && sudo apt upgrade
```

### macOS


**Issue: MoltenVK not found**

**Solution:**
```bash
brew install --cask vulkan-sdk
# Or download from vulkan.lunarg.com

```

**Issue: Validation layers verbose/broken**

**Solution:**
```bash
# Disable validation on macOS if issues

cargo build --release  # No validation in release
```

**Issue: Metal-specific limitations**

**Solution:**
- Some Vulkan features unavailable
- Check MoltenVK compatibility: https://github.com/KhronosGroup/MoltenVK

---

## Debugging Tips


### Enable Verbose Logging


```rust
// Set environment variable
std::env::set_var("RUST_LOG", "shdrlib=trace");
```

Or:
```bash
RUST_LOG=shdrlib=trace cargo run
```

### Use RenderDoc


1. Download RenderDoc: https://renderdoc.org/
2. Launch your app through RenderDoc
3. Capture frame (F12)
4. Inspect:
   - Draw calls
   - Pipeline state
   - Shaders
   - Textures

### Validation Layers


Debug builds automatically enable validation. Read messages carefully!

```
VUID-vkCmdDraw-None-02859: Shader requires VkPhysicalDeviceFeatures::geometryShader
```

This tells you:
- What's wrong (geometry shader not enabled)
- Where to look (VkPhysicalDeviceFeatures)
- How to fix (enable feature)

### Minimal Reproduction


Narrow down issues:

```rust
// Start with simplest case
let renderer = EzRenderer::new()?;
println!("✅ Setup works");

let pipeline = renderer.quick_pipeline(VERT, FRAG)?;
println!("✅ Pipeline works");

renderer.render_frame(|frame| {
    frame.bind_pipeline(pipeline)?;
    frame.draw(3, 1, 0, 0);
    Ok(())
})?;
println!("✅ Rendering works");
```

Find which line fails, then focus on that.

---

## Getting Help


### Before asking:


1. ✅ Check this troubleshooting guide
2. ✅ Read [FAQ]faq.md
3. ✅ Search [GitHub Issues]https://github.com/paulburnettjones-wq/shdrlib/issues
4. ✅ Try minimal reproduction

### When asking:


Include:
- **OS and version** (Windows 11, Ubuntu 22.04, etc.)
- **Rust version** (`rustc --version`)
- **GPU and driver** (`vulkaninfo | head`)
- **shdrlib version** (Cargo.toml)
- **Minimal code** to reproduce
- **Full error message** with backtrace

**Good issue report:**
```markdown
**OS:** Windows 11
**Rust:** 1.82.0
**GPU:** NVIDIA RTX 3060, Driver 536.23
**shdrlib:** 0.1.0

**Problem:** Black screen when rendering triangle

**Code:**
```rust
[minimal reproduction]
```

**Error:**
```
[full error message]
```
```

**Bad issue report:**
```
it doesnt work help
```

### Where to ask:


- 🐛 **Bugs:** [GitHub Issues]https://github.com/paulburnettjones-wq/shdrlib/issues
- 💬 **Questions:** [GitHub Discussions]https://github.com/paulburnettjones-wq/shdrlib/discussions
- 📖 **Docs unclear:** Open docs issue or PR

---

**Still stuck?** Open a [GitHub Discussion](https://github.com/paulburnettjones-wq/shdrlib/discussions) with details!

---

**Last Updated:** October 30, 2025