Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetNativeBufferPtr()

Returns a native pointer to the top-level acceleration structure (TLAS) buffer as it is represented in the native graphics API, for example an `ID3D12Resource`.
Read time 2 minutesLast updated 6 days ago

Definition

public nint GetNativeBufferPtr()

Returns

Type

Description

IntPtrThe pointer to the underlying graphics API TLAS buffer.

Remarks

Use this method to enable binding the TLAS buffer to shaders from native code plugins.
If you call RayTracingAccelerationStructure.Build, the underlying TLAS buffer might change, for example if number of ray tracing instances can't fit into the existing buffer storing the TLAS data. Call RayTracingAccelerationStructure.GetNativeBufferPtr to get the new native pointer after a build operation.
If you call the function from a non-main thread or if the acceleration structure was never built then the function returns
null
.
Note that calling this method will synchronize with the rendering thread which is a slow operation.
using UnityEngine;using UnityEngine.Rendering;[ExecuteInEditMode]public class RayTracingExample : MonoBehaviour{ private RayTracingAccelerationStructure rtas = null; private int rayTracingInstanceCount = -1; void OnDisable() { if (rtas != null) { rtas.Release(); rtas = null; rayTracingInstanceCount = -1; } } private void Update() { if (!SystemInfo.supportsRayTracing) { Debug.Log("Ray Tracing is not supported by this GPU or by the current graphics API."); return; } if (rtas == null) { RayTracingAccelerationStructure.Settings settings = new RayTracingAccelerationStructure.Settings(); settings.rayTracingModeMask = RayTracingAccelerationStructure.RayTracingModeMask.Everything; settings.managementMode = RayTracingAccelerationStructure.ManagementMode.Automatic; settings.layerMask = 255; rtas = new RayTracingAccelerationStructure(settings); } rtas.Build(); // The native pointer of the TLAS can change if the amount of ray tracing instances grows from frame to frame. if (rayTracingInstanceCount < (int)rtas.GetInstanceCount()) { rayTracingInstanceCount = (int)rtas.GetInstanceCount(); Debug.Log("Native TLAS resource is: " + rtas.GetNativeBufferPtr()); } }}
The script in this example configures an acceleration structure based on the contents of the Scene, builds the acceleration structure and displays the native pointer of the TLAS in the console.