Skip to content

Commit 5946c73

Browse files
author
Evergreen
committed
[Port] [6000.4] [UUM-137604] UnifiedRT: Fixed RayTracingRenderPipelineResources needing to be public for Player build + doc improvements
1 parent b37bcc8 commit 5946c73

6 files changed

Lines changed: 137 additions & 17 deletions

File tree

Packages/com.unity.render-pipelines.core/Documentation~/UnifiedRayTracing/create-ray-tracing-context.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Follow these steps:
99
## Load the ray tracing resources
1010
The [`RayTracingContext`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingContext) needs a few utility shaders that the [`RayTracingResources`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingResources) object supplies. You can load these resources in several different ways.
1111

12-
If your project uses SRP (Scriptable Render Pipeline), load the resources via [`RayTracingResources.LoadFromRenderPipelineResources`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingContext.LoadFromRenderPipelineResources()). This always works in the Editor.
12+
If your project uses SRP (Scriptable Render Pipeline), load the resources via [`RayTracingResources.LoadFromRenderPipelineResources`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingResources.LoadFromRenderPipelineResources()). This always works in the Editor.
1313
```C#
1414
var rtResources = new RayTracingResources();
1515
bool result = rtResources.LoadFromRenderPipelineResources();
@@ -49,9 +49,12 @@ rtResources.LoadFromAssetBundle(asssetBundle);
4949
## Create the context
5050
Once the [`RayTracingResources`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingResources) are loaded, use them to create the [`RayTracingContext`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingContext).
5151
```C#
52-
// Choose a backend
53-
var backend = RayTracingContext.IsBackendSupported(RayTracingBackend.Hardware) ? RayTracingBackend.Hardware : RayTracingBackend.Compute;
52+
var context = new RayTracingContext(rtResources);
53+
```
54+
55+
By default, Unity checks if the device supports hardware ray tracing, and selects either hardware ray tracing or compute shaders. To manually select the backend, pass in a `RayTracingBackend`. For example:
5456

55-
// Create the context
57+
```C#
58+
var backend = RayTracingBackend.Compute;
5659
var context = new RayTracingContext(backend, rtResources);
5760
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Release your ray tracing objects
2+
3+
You must free up the memory your ray tracing objects use after you finish with them.
4+
5+
Dispose of the following:
6+
7+
- Scratch buffers. Use [`GraphicsBuffer.Dispose`](xref:UnityEngine.GraphicsBuffer.Dispose()) after you've executed the final command buffer.
8+
- Ray tracing acceleration structures. Use [`IRayTracingAccelStruct.Dispose`](xref:UnityEngine.Rendering.UnifiedRayTracing.IRayTracingAccelStruct.Dispose()) after you've executed the final command buffer.
9+
- Ray tracing context. Use [`RayTracingContext.Dispose`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingContext.Dispose()) after you dispose of all its acceleration structures.
10+
11+
**Note**: Unity automatically disposes of [`RayTracingResources`](xref:UnityEngine.Rendering.UnifiedRayTracing.RayTracingResources) and [`IRayTracingShader`](xref:UnityEngine.Rendering.UnifiedRayTracing.IRayTracingShader).
12+
13+
For example:
14+
15+
```C#
16+
RayTracingContext rtContext = new RayTracingContext(rtResources);
17+
IRayTracingShader rtShader = rtContext.LoadRayTracingShader("Assets/yourShader.urtshader");
18+
IRayTracingAccelStruct rtAccelStruct = rtContext.CreateAccelerationStructure(new AccelerationStructureOptions());
19+
GraphicsBuffer rtScratchBuffer = RayTracingHelper.CreateScratchBufferForBuild(rtAccelStruct);
20+
21+
// ... create and dispatch your ray tracing command buffers ...
22+
23+
rtScratchBuffer.Dispose();
24+
rtAccelStruct.Dispose();
25+
rtContext.Dispose();
26+
```

Packages/com.unity.render-pipelines.core/Documentation~/UnifiedRayTracing/unified-ray-tracing-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ The `UnifiedRayTracing` API enables you to write ray tracing code that can execu
1212
|[Execute your ray tracing code](execute-shader.md)|How to execute your ray tracing shader.|
1313
|[Sample code](trace-camera-rays-full-sample.md)|Complete code example showcasing tracing rays from the scene's camera.|
1414
|[Unified ray tracing shader code reference](shader-code-reference.md)|API reference for the unified ray tracing shader code.|
15-
15+
|[Release your ray tracing objects](release-resources.md)|Learn how to release your ray tracing resources.|

Packages/com.unity.render-pipelines.core/Documentation~/UnifiedRayTracing/workflow.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ To use the API to trace rays in Unity, follow these steps:
55
3. Create a shader.
66
4. Build your acceleration structure.
77
5. Execute your shader.
8+
6. Dispose of your objects.
89

910
## Create the ray tracing context
1011

@@ -53,3 +54,5 @@ rtShader.Dispatch(cmd, traceScratchBuffer, threadCountX, threadCountY, threadCou
5354

5455
For more information, refer to [Execute your ray tracing code](execute-shader.md).
5556

57+
## Dispose of your objects
58+
For more information, refer to [Release your ray tracing objects](release-resources.md).

Packages/com.unity.render-pipelines.core/Runtime/UnifiedRayTracing/RayTracingContext.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ static public GraphicsBuffer CreateScratchBufferForTrace(IRayTracingShader shade
332332
/// Resizes a scratch buffer if its size doesn't fit the requirement of <see cref="IRayTracingShader.Dispatch"/>.
333333
/// </summary>
334334
/// <remarks>
335-
/// The resize is accomplished by disposing of the GraphicsBuffer and instanciating a new one at the proper size.
335+
/// Unity resizes the buffer by disposing of the `GraphicsBuffer` and instantiating a new one with the proper size.
336+
/// **Important:** If you reference the current <paramref name="scratchBuffer"/> in a command buffer, you must
337+
/// only call this method after you submit the command buffer. See <see cref="Graphics.ExecuteCommandBuffer"/> or <see cref="ScriptableRenderContext.ExecuteCommandBuffer"/>.
336338
/// </remarks>
337339
/// <param name="shader">The shader that will be passed to <see cref="IRayTracingShader.Dispatch"/>.</param>
338340
/// <param name="dispatchWidth">Number of threads in the X dimension that will be passed to <see cref="IRayTracingShader.Dispatch"/>.</param>
@@ -362,7 +364,9 @@ static public void ResizeScratchBufferForTrace(
362364
/// Resizes a scratch buffer if its size doesn't fit the requirement of <see cref="IRayTracingAccelStruct.Build"/>.
363365
/// </summary>
364366
/// <remarks>
365-
/// The resize is accomplished by disposing of the GraphicsBuffer and instanciating a new one at the proper size.
367+
/// Unity resizes the buffer by disposing of the `GraphicsBuffer` and instantiating a new one with the proper size.
368+
/// **Important:** If you reference the current <paramref name="scratchBuffer"/> in a command buffer, you must
369+
/// only call this method after you submit the command buffer. See <see cref="Graphics.ExecuteCommandBuffer"/> or <see cref="ScriptableRenderContext.ExecuteCommandBuffer"/>.
366370
/// </remarks>
367371
/// <param name="accelStruct">The acceleration structure that will be passed to <see cref="IRayTracingAccelStruct.Build"/>.</param>
368372
/// <param name="scratchBuffer">The scratch buffer.</param>

Packages/com.unity.render-pipelines.core/Runtime/UnifiedRayTracing/RayTracingResources.cs

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
namespace UnityEngine.Rendering.UnifiedRayTracing
77
{
8+
/// <summary>
9+
/// Resource class that handles the serialization of UnifiedRayTracing's utility shaders in projects using a Scriptable Render Pipeline.
10+
/// </summary>
11+
/// <remarks>
12+
/// This class is used internally by <see cref="RayTracingResources.LoadFromRenderPipelineResources"/>
13+
/// </remarks>
814
[Scripting.APIUpdating.MovedFrom(
915
autoUpdateAPI: true,
1016
sourceNamespace: "UnityEngine.Rendering.UnifiedRayTracing",
@@ -13,10 +19,13 @@ namespace UnityEngine.Rendering.UnifiedRayTracing
1319
[Serializable]
1420
[SupportedOnRenderPipeline()]
1521
[Categorization.CategoryInfo(Name = "R: Unified Ray Tracing", Order = 1000), HideInInspector]
16-
sealed class RayTracingRenderPipelineResources : IRenderPipelineResources
22+
public sealed class RayTracingRenderPipelineResources : IRenderPipelineResources
1723
{
1824
[SerializeField, HideInInspector] int m_Version = 1;
1925

26+
/// <summary>
27+
/// The version number of the resources container.
28+
/// </summary>
2029
public int version
2130
{
2231
get => m_Version;
@@ -49,54 +58,84 @@ public int version
4958
[SerializeField, ResourcePath("Runtime/UnifiedRayTracing/Compute/RadeonRays/kernels/scatter.compute")]
5059
ComputeShader m_Scatter;
5160

61+
/// <summary>
62+
/// Compute shader for geometry pool operations.
63+
/// </summary>
5264
public ComputeShader GeometryPoolKernels
5365
{
5466
get => m_GeometryPoolKernels;
5567
set => this.SetValueAndNotify(ref m_GeometryPoolKernels, value, nameof(m_GeometryPoolKernels));
5668
}
5769

70+
/// <summary>
71+
/// Compute shader for buffer copy operations.
72+
/// </summary>
5873
public ComputeShader CopyBuffer
5974
{
6075
get => m_CopyBuffer;
6176
set => this.SetValueAndNotify(ref m_CopyBuffer, value, nameof(m_CopyBuffer));
6277
}
6378

79+
/// <summary>
80+
/// Compute shader for copying vertex position data.
81+
/// </summary>
6482
public ComputeShader CopyPositions
6583
{
6684
get => m_CopyPositions;
6785
set => this.SetValueAndNotify(ref m_CopyPositions, value, nameof(m_CopyPositions));
6886
}
6987

88+
/// <summary>
89+
/// Compute shader for radix sort operations.
90+
/// </summary>
7091
public ComputeShader BitHistogram
7192
{
7293
get => m_BitHistogram;
7394
set => this.SetValueAndNotify(ref m_BitHistogram, value, nameof(m_BitHistogram));
7495
}
7596

97+
/// <summary>
98+
/// Compute shader for prefix sum operations.
99+
/// </summary>
76100
public ComputeShader BlockReducePart
77101
{
78102
get => m_BlockReducePart;
79103
set => this.SetValueAndNotify(ref m_BlockReducePart, value, nameof(m_BlockReducePart));
80104
}
81105

106+
/// <summary>
107+
/// Compute shader for prefix sum operations.
108+
/// </summary>
82109
public ComputeShader BlockScan
83110
{
84111
get => m_BlockScan;
85112
set => this.SetValueAndNotify(ref m_BlockScan, value, nameof(m_BlockScan));
86113
}
87114

115+
/// <summary>
116+
/// Compute shader for building a BVH.
117+
/// </summary>
88118
public ComputeShader BuildHlbvh
89119
{
90120
get => m_BuildHlbvh;
91121
set => this.SetValueAndNotify(ref m_BuildHlbvh, value, nameof(m_BuildHlbvh));
92122
}
93123

124+
/// <summary>
125+
/// Compute shader for BVH restructuring.
126+
/// </summary>
127+
/// <remarks>
128+
/// Used to optimize the BVH structure after initial construction.
129+
/// </remarks>
94130
public ComputeShader RestructureBvh
95131
{
96132
get => m_RestructureBvh;
97133
set => this.SetValueAndNotify(ref m_RestructureBvh, value, nameof(m_RestructureBvh));
98134
}
99135

136+
/// <summary>
137+
/// Compute shader for radix sort operations.
138+
/// </summary>
100139
public ComputeShader Scatter
101140
{
102141
get => m_Scatter;
@@ -107,17 +146,62 @@ public ComputeShader Scatter
107146
/// <summary>
108147
/// Utility shaders needed by a <see cref="RayTracingContext"/> to operate.
109148
/// </summary>
149+
/// <remarks>
150+
/// This class holds compute shaders required for unified ray tracing operations,
151+
/// including geometry pool management and BVH construction kernels.
152+
/// </remarks>
110153
public class RayTracingResources
111154
{
112-
internal ComputeShader geometryPoolKernels { get; set; }
113-
internal ComputeShader copyBuffer { get; set; }
114-
internal ComputeShader copyPositions { get; set; }
115-
internal ComputeShader bitHistogram { get; set; }
116-
internal ComputeShader blockReducePart { get; set; }
117-
internal ComputeShader blockScan { get; set; }
118-
internal ComputeShader buildHlbvh { get; set; }
119-
internal ComputeShader restructureBvh { get; set; }
120-
internal ComputeShader scatter { get; set; }
155+
/// <summary>
156+
/// Compute shader for geometry pool operations.
157+
/// </summary>
158+
public ComputeShader geometryPoolKernels { get; set; }
159+
160+
/// <summary>
161+
/// Compute shader for buffer copy operations.
162+
/// </summary>
163+
public ComputeShader copyBuffer { get; set; }
164+
165+
/// <summary>
166+
/// Compute shader for copying vertex position data.
167+
/// </summary>
168+
public ComputeShader copyPositions { get; set; }
169+
170+
/// <summary>
171+
/// Compute shader for radix sort operations.
172+
/// </summary>
173+
public ComputeShader bitHistogram { get; set; }
174+
175+
/// <summary>
176+
/// Compute shader for radix sort operations.
177+
/// </summary>
178+
public ComputeShader scatter { get; set; }
179+
180+
/// <summary>
181+
/// Compute shader for prefix sum operations.
182+
/// </summary>
183+
public ComputeShader blockReducePart { get; set; }
184+
185+
/// <summary>
186+
/// Compute shader for prefix sum operations.
187+
/// </summary>
188+
public ComputeShader blockScan { get; set; }
189+
190+
/// <summary>
191+
/// Compute shader for building a BVH.
192+
/// </summary>
193+
public ComputeShader buildHlbvh { get; set; }
194+
195+
/// <summary>
196+
/// Compute shader for BVH restructuring.
197+
/// </summary>
198+
/// <remarks>
199+
/// Used to optimize the BVH structure after initial construction.
200+
/// </remarks>
201+
public ComputeShader restructureBvh { get; set; }
202+
203+
204+
121205

122206
#if UNITY_EDITOR
123207
/// <summary>

0 commit comments

Comments
 (0)