2.9 KiB
2.9 KiB
Metal Implementation Plan
This document outlines the steps required to complete the Metal-based GPU ray tracer, transitioning from the current scaffolding to a fully functional renderer.
1. Material System Integration
The most critical gap is the lack of material data on the GPU.
- Data Synchronization: Ensure
struct MaterialParameterinres/shaders/Common.slangmatches the C++ memory layout forMaterial. - Buffer Implementation: Complete
MetalScene::createRayTracingHierarchyto:- Allocate and populate
materialsBuffer. - Map each model in
modelRefsBufferto a specific material index.
- Allocate and populate
- Shader Retrieval: In
ClosestHit.slang, implement the lookup:MaterialParameter mat = pParams.materialData[m.materialIndex];.
2. Shader Completion
The Slang shaders currently contain placeholders and incomplete lighting logic.
Miss.slang: Implement a miss shader that returns a default environment color (e.g., a dark navy or simple sky gradient) to prevent black backgrounds on missed rays.ClosestHit.slang:- Replace
// TOOD:with actual material attribute fetching. - Refine the BRDF application: connect the fetched albedo, specular, and emissive values to the lighting loops (Directional/Point lights).
- Fix indirect illumination recursion: ensure the payload correctly accumulates light across multiple bounces without exponential energy gain/loss.
- Replace
RayGen.slang: Verify that theradianceAccumulatorhandles sample averaging correctly to support progressive rendering and anti-aliasing.
3. Resource & Buffer Management
Ensure all data flows from the CPU scene description to the Metal compute pipeline.
- Parameter Blocks: Fully utilize
ParameterBlock<RaytracingParams>for all global scene data (lights, camera, acceleration structure) to minimize binding overhead. - Texture Support:
- Implement a mechanism in
MetalSceneto upload textures toid<MTLTexture>. - Expand
RaytracingParamsto include access to these textures within the shaders for albedo/normal mapping.
- Implement a mechanism in
4. Performance & Robustness
- Acceleration Structure: The current compaction logic is good; ensure it is called whenever geometry changes.
- Memory Safety: Add validation for buffer sizes and alignment, especially when bridging C++
glmtypes to Slang/Metal types. - Debugging: Enable Metal API validation during development to catch illegal memory access or incorrect resource usage in the compute kernel.
5. Milestones
- Milestone 1: Basic Geometry: Render unlit, solid-colored geometry using
ClosestHitand a basicMissshader. - Milestone 2: Basic Lighting: Implement diffuse shading with a single directional light.
- Milestone 3: Full Material System: Integrate textures and multiple material types.
- Milestone 4: Global Illumination: Complete recursive bounce logic for indirect lighting.