|
| 1 | +# Scene Definitions |
| 2 | + |
| 3 | +Define scenes in TOML files instead of hardcoded shaders. |
| 4 | + |
| 5 | +## Relevant Files |
| 6 | + |
| 7 | +- `crates/shader/src/scene.rs` - Current hardcoded scene functions |
| 8 | +- `crates/rtx-obj/src/lib.rs` - `Sphere`, `Quad`, `List` primitives |
| 9 | +- `crates/rtx-mat/src/lib.rs` - `Lambertian`, `Metal`, `Dielectric`, `DiffuseLight` materials |
| 10 | +- `crates/rtx-tex/src/lib.rs` - `SolidTexture` and texture table |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +Currently scenes are hardcoded as shader entry points (`cornell_box_fs`, `two_spheres_fs`, etc.). Each scene function builds primitives, materials, and textures at shader runtime. This works but: |
| 15 | +- Adding a scene requires recompiling the shader |
| 16 | +- Scene parameters can't be tweaked without code changes |
| 17 | +- Benchmarks and renders must reference shader entry point names |
| 18 | + |
| 19 | +Scene TOML files would allow defining scenes declaratively. The host parses the TOML and uploads scene data to the GPU via buffers, replacing the hardcoded scene functions with a single generic renderer. |
| 20 | + |
| 21 | +## Status |
| 22 | + |
| 23 | +- [ ] Design TOML schema for scenes |
| 24 | +- [ ] GPU buffer layout for scene data |
| 25 | +- [ ] Host-side TOML parsing and buffer upload |
| 26 | +- [ ] Generic shader entry point that reads from buffers |
| 27 | +- [ ] Migrate existing scenes to TOML format |
| 28 | + |
| 29 | +### Future Enhancements |
| 30 | + |
| 31 | +- [ ] Live reload: watch TOML files and re-upload on change |
| 32 | +- [ ] Scene includes: reference other TOML files for reusable object groups |
| 33 | +- [ ] Procedural generation: loops/randomization in scene definition |
| 34 | + |
| 35 | +## Scene Definition Format |
| 36 | + |
| 37 | +Scenes live in `scenes/<name>.toml`: |
| 38 | + |
| 39 | +```toml |
| 40 | +[camera] |
| 41 | +# Default camera for this scene (can be overridden by render/benchmark) |
| 42 | +position = [278.0, 278.0, -800.0] |
| 43 | +look_at = [278.0, 278.0, 0.0] |
| 44 | +vfov = 40.0 |
| 45 | +focus_dist = 10.0 |
| 46 | +defocus_angle = 0.0 |
| 47 | + |
| 48 | +[[materials]] |
| 49 | +name = "white" |
| 50 | +type = "lambertian" |
| 51 | +color = [0.73, 0.73, 0.73] |
| 52 | + |
| 53 | +[[materials]] |
| 54 | +name = "red" |
| 55 | +type = "lambertian" |
| 56 | +color = [0.65, 0.05, 0.05] |
| 57 | + |
| 58 | +[[materials]] |
| 59 | +name = "light" |
| 60 | +type = "diffuse_light" |
| 61 | +color = [15.0, 15.0, 15.0] |
| 62 | + |
| 63 | +[[materials]] |
| 64 | +name = "glass" |
| 65 | +type = "dielectric" |
| 66 | +refraction_index = 1.5 |
| 67 | + |
| 68 | +[[materials]] |
| 69 | +name = "mirror" |
| 70 | +type = "metal" |
| 71 | +color = [0.8, 0.8, 0.8] |
| 72 | +fuzz = 0.0 |
| 73 | + |
| 74 | +[[objects]] |
| 75 | +type = "quad" |
| 76 | +corner = [555.0, 0.0, 0.0] |
| 77 | +u = [0.0, 555.0, 0.0] |
| 78 | +v = [0.0, 0.0, 555.0] |
| 79 | +material = "white" |
| 80 | + |
| 81 | +[[objects]] |
| 82 | +type = "sphere" |
| 83 | +center = [190.0, 90.0, 190.0] |
| 84 | +radius = 90.0 |
| 85 | +material = "glass" |
| 86 | +``` |
| 87 | + |
| 88 | +### Material Types |
| 89 | + |
| 90 | +| Type | Fields | |
| 91 | +|------|--------| |
| 92 | +| `lambertian` | `color` | |
| 93 | +| `metal` | `color`, `fuzz` (0.0-1.0) | |
| 94 | +| `dielectric` | `refraction_index` | |
| 95 | +| `diffuse_light` | `color` (can exceed 1.0 for brightness) | |
| 96 | + |
| 97 | +### Object Types |
| 98 | + |
| 99 | +| Type | Fields | |
| 100 | +|------|--------| |
| 101 | +| `sphere` | `center`, `radius`, `material` | |
| 102 | +| `quad` | `corner`, `u`, `v`, `material` | |
| 103 | + |
| 104 | +## Integration with Renders and Benchmarks |
| 105 | + |
| 106 | +Render and benchmark TOMLs would reference scenes by name or define inline: |
| 107 | + |
| 108 | +**Reference by name:** |
| 109 | +```toml |
| 110 | +scene = "cornell_box" # loads scenes/cornell_box.toml |
| 111 | + |
| 112 | +[camera] |
| 113 | +position = [278.0, 278.0, -800.0] |
| 114 | +look_at = [278.0, 278.0, 0.0] |
| 115 | + |
| 116 | +[quality] |
| 117 | +samples = 100 |
| 118 | +bounces = 50 |
| 119 | +``` |
| 120 | + |
| 121 | +**Inline scene definition:** |
| 122 | +```toml |
| 123 | +[camera] |
| 124 | +position = [13.0, 2.0, 3.0] |
| 125 | +look_at = [0.0, 0.0, 0.0] |
| 126 | + |
| 127 | +[quality] |
| 128 | +samples = 50 |
| 129 | +bounces = 10 |
| 130 | + |
| 131 | +[scene] |
| 132 | +[[scene.materials]] |
| 133 | +name = "ground" |
| 134 | +type = "lambertian" |
| 135 | +color = [0.5, 0.5, 0.5] |
| 136 | + |
| 137 | +[[scene.objects]] |
| 138 | +type = "sphere" |
| 139 | +center = [0.0, -1000.0, 0.0] |
| 140 | +radius = 1000.0 |
| 141 | +material = "ground" |
| 142 | +``` |
| 143 | + |
| 144 | +## GPU Buffer Design |
| 145 | + |
| 146 | +Scene data must be uploaded to the GPU since TOML is parsed on the host. Rough layout: |
| 147 | + |
| 148 | +- **Material buffer**: array of material structs (kind + parameters) |
| 149 | +- **Object buffer**: array of object structs (kind + geometry + material index) |
| 150 | +- **Texture buffer**: (future) image data for texture mapping |
| 151 | + |
| 152 | +The shader would have a single entry point that reads these buffers instead of calling scene-specific functions. This requires: |
| 153 | +1. Defining fixed-size buffer layouts (max objects, max materials) |
| 154 | +2. Or using storage buffers with dynamic sizing (if rust-gpu supports) |
| 155 | + |
| 156 | +## Open Questions |
| 157 | + |
| 158 | +- Max object/material counts? Fixed compile-time limits vs dynamic? |
| 159 | +- How to handle the `List<NS, NQ>` const generics with dynamic scene sizes? |
| 160 | +- Should hardcoded scenes remain as fallbacks or be fully replaced? |
| 161 | +- Texture support: inline color only, or path to image files? |
0 commit comments