docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class RayTracingContext

    Entry point for the UnifiedRayTracing API.

    Inheritance
    object
    RayTracingContext
    Implements
    IDisposable
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: UnityEngine.Rendering.UnifiedRayTracing
    Assembly: Unity.UnifiedRayTracing.Runtime.dll
    Syntax
    public sealed class RayTracingContext : IDisposable
    Remarks

    It provides functionality to:

    • load shader code (CreateRayTracingShader)
    • create an acceleration structure (CreateAccelerationStructure) that represents the geometry to be ray traced against.
    Once these objects have been created, the shader code can be executed by calling IRayTracingShader.Dispatch Before calling Dispose() on a RayTracingContext, all IRayTracingAccelStruct that have been created by a RayTracingContext must be disposed as well.

    Constructors

    RayTracingContext(RayTracingBackend, RayTracingResources)

    Creates a RayTracingContext.

    Declaration
    public RayTracingContext(RayTracingBackend backend, RayTracingResources resources)
    Parameters
    Type Name Description
    RayTracingBackend backend

    The chosen backend.

    RayTracingResources resources

    The resources (provides the various shaders the context needs to operate).

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown when the supplied backend is not supported.

    RayTracingContext(RayTracingResources)

    Creates a RayTracingContext.

    Declaration
    public RayTracingContext(RayTracingResources resources)
    Parameters
    Type Name Description
    RayTracingResources resources

    The resources (provides the various shaders the context needs to operate).

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown when no supported backend is available.

    Properties

    BackendType

    The RayTracingBackend this context was created with.

    Declaration
    public RayTracingBackend BackendType { get; }
    Property Value
    Type Description
    RayTracingBackend

    Resources

    RayTracingResources object this context has been created with.

    Declaration
    public RayTracingResources Resources { get; }
    Property Value
    Type Description
    RayTracingResources

    Methods

    CreateAccelerationStructure(AccelerationStructureOptions)

    Creates a IRayTracingAccelStruct.

    Declaration
    public IRayTracingAccelStruct CreateAccelerationStructure(AccelerationStructureOptions options)
    Parameters
    Type Name Description
    AccelerationStructureOptions options

    Options for quality/performance trade-offs for the returned acceleration structure

    Returns
    Type Description
    IRayTracingAccelStruct

    The acceleration structure.

    CreateRayTracingShader(Object)

    Creates a IRayTracingShader.

    Declaration
    public IRayTracingShader CreateRayTracingShader(Object shader)
    Parameters
    Type Name Description
    Object shader

    The ComputeShader or RayTracingShader asset.

    Returns
    Type Description
    IRayTracingShader

    The unified ray tracing shader.

    Remarks

    Depending on the chosen backend, the shader parameter needs to be either a ComputeShader or RayTracingShader.

    Dispose()

    Disposes the RaytracingContext.

    Declaration
    public void Dispose()
    Remarks

    Before calling this, all IRayTracingAccelStruct that have been created with this RayTracingContext must be disposed as well.

    GetRequiredTraceScratchBufferSizeInBytes(uint, uint, uint)

    Returns the minimum size that is required by the scratchBuffer parameter of Dispatch(CommandBuffer, GraphicsBuffer, uint, uint, uint).

    Declaration
    public ulong GetRequiredTraceScratchBufferSizeInBytes(uint width, uint height, uint depth)
    Parameters
    Type Name Description
    uint width

    Number of threads in the X dimension.

    uint height

    Number of threads in the Y dimension.

    uint depth

    Number of threads in the Z dimension.

    Returns
    Type Description
    ulong

    The size in bytes.

    GetScratchBufferStrideInBytes()

    Required stride for the creation of the scratchBuffers used by Dispatch(CommandBuffer, GraphicsBuffer, uint, uint, uint) and Build(CommandBuffer, GraphicsBuffer).

    Declaration
    public static uint GetScratchBufferStrideInBytes()
    Returns
    Type Description
    uint

    The required stride.

    IsBackendSupported(RayTracingBackend)

    Checks if the specified backend is supported on the current GPU.

    Declaration
    public static bool IsBackendSupported(RayTracingBackend backend)
    Parameters
    Type Name Description
    RayTracingBackend backend

    The backend.

    Returns
    Type Description
    bool

    Whether the specified bakend is supported.

    LoadRayTracingShader(string)

    Creates a unified ray tracing shader from .urtshader asset file.

    Declaration
    public IRayTracingShader LoadRayTracingShader(string fileName)
    Parameters
    Type Name Description
    string fileName

    Path to the .urtshader shader asset file to load.

    Returns
    Type Description
    IRayTracingShader

    The unified ray tracing shader.

    Remarks
    • This API works only in the Unity Editor, not at runtime.
    • The path must be relative to the project folder, for example: "Assets/Stuff/myshader.urtshader".
    • A .urtshader asset file is imported in the Editor as 2 shaders: a ComputeShader and a RayTracingShader. LoadRayTracingShader loads the one relevant one depending on the RayTracingContext's backend.

    LoadRayTracingShaderFromAssetBundle(AssetBundle, string)

    Creates a unified ray tracing shader from an AssetBundle.

    Declaration
    public IRayTracingShader LoadRayTracingShaderFromAssetBundle(AssetBundle assetBundle, string name)
    Parameters
    Type Name Description
    AssetBundle assetBundle

    The AssetBundle.

    string name

    The asset name with the .urtshader extension included.

    Returns
    Type Description
    IRayTracingShader

    The unified ray tracing shader.

    Implements

    IDisposable

    Extension Methods

    ReflectionUtils.GetField(object, string)
    ReflectionUtils.GetFields(object)
    ReflectionUtils.Invoke(object, string, params object[])
    ReflectionUtils.SetField(object, string, object)
    AnalyticsUtils.ToNestedColumnWithDefault<T>(T, T, bool)
    AnalyticsUtils.ToNestedColumn<T>(T, T)
    In This Article
    Back to top
    Copyright © 2025 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)