Release your ray tracing objects
You must free up the memory your ray tracing objects use after you finish with them.
Dispose of the following:
- Scratch buffers. Use
GraphicsBuffer.Disposeafter you've executed the final command buffer. - Ray tracing acceleration structures. Use
IRayTracingAccelStruct.Disposeafter you've executed the final command buffer. - Ray tracing context. Use
RayTracingContext.Disposeafter you dispose of all its acceleration structures.
Note: Unity automatically disposes of RayTracingResources and IRayTracingShader.
For example:
RayTracingContext rtContext = new RayTracingContext(rtResources);
IRayTracingShader rtShader = rtContext.LoadRayTracingShader("Assets/yourShader.urtshader");
IRayTracingAccelStruct rtAccelStruct = rtContext.CreateAccelerationStructure(new AccelerationStructureOptions());
GraphicsBuffer rtScratchBuffer = RayTracingHelper.CreateScratchBufferForBuild(rtAccelStruct);
// ... create and dispatch your ray tracing command buffers ...
rtScratchBuffer.Dispose();
rtAccelStruct.Dispose();
rtContext.Dispose();