docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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.Dispose after you've executed the final command buffer.
    • Ray tracing acceleration structures. Use IRayTracingAccelStruct.Dispose after you've executed the final command buffer.
    • Ray tracing context. Use RayTracingContext.Dispose after 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();
    
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)