|
| 1 | +using NativeEngine; |
| 2 | + |
| 3 | +namespace Sandbox; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// A ray tracing shader, |
| 7 | +/// enabling advanced rendering techniques like real-time ray tracing for reflections, |
| 8 | +/// global illumination, and shadows. |
| 9 | +/// </summary> |
| 10 | +/// <seealso cref="GpuBuffer{T}"/> |
| 11 | +/// <seealso cref="ComputeShader"/> |
| 12 | +internal class RayTracingShader |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Attributes that are passed to the ray tracing shader on dispatch. |
| 16 | + /// </summary> |
| 17 | + public RenderAttributes Attributes { get; } = new RenderAttributes(); |
| 18 | + |
| 19 | + private Material RayTracingMaterial; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Create a ray tracing shader from the specified path. |
| 23 | + /// </summary> |
| 24 | + public RayTracingShader( string path ) |
| 25 | + { |
| 26 | + var material = Material.FromShader( path ); |
| 27 | + Assert.NotNull( material, $"Failed to load ray tracing shader material from path: {path}" ); |
| 28 | + RayTracingMaterial = material; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Dispatches the ray tracing shader using explicit thread counts. |
| 33 | + /// </summary> |
| 34 | + /// <remarks> |
| 35 | + /// The specified thread counts represent the dispatch dimensions for the ray generation shader. |
| 36 | + /// <para> |
| 37 | + /// When called outside a graphics context, the dispatch runs immediately. |
| 38 | + /// When called inside a graphics context, the dispatch runs async. |
| 39 | + /// </para> |
| 40 | + /// </remarks> |
| 41 | + /// <param name="attributes">Render attributes to use for this dispatch.</param> |
| 42 | + /// <param name="threadsX">The number of threads to dispatch in the X dimension.</param> |
| 43 | + /// <param name="threadsY">The number of threads to dispatch in the Y dimension.</param> |
| 44 | + /// <param name="threadsZ">The number of threads to dispatch in the Z dimension.</param> |
| 45 | + public void DispatchRaysWithAttributes( RenderAttributes attributes, int threadsX = 1, int threadsY = 1, int threadsZ = 1 ) |
| 46 | + { |
| 47 | + if ( threadsX < 1 ) throw new ArgumentException( $"Cannot be less than 1", nameof( threadsX ) ); |
| 48 | + if ( threadsY < 1 ) throw new ArgumentException( $"Cannot be less than 1", nameof( threadsY ) ); |
| 49 | + if ( threadsZ < 1 ) throw new ArgumentException( $"Cannot be less than 1", nameof( threadsZ ) ); |
| 50 | + |
| 51 | + // Dispatch ray tracing using RenderTools.TraceRays |
| 52 | + var mode = RayTracingMaterial.native.GetMode(); |
| 53 | + RenderTools.TraceRays( Graphics.Context, attributes.Get(), mode, (uint)threadsX, (uint)threadsY, (uint)threadsZ ); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Dispatches the ray tracing shader using the default attributes. |
| 58 | + /// </summary> |
| 59 | + public void DispatchRays( int threadsX = 1, int threadsY = 1, int threadsZ = 1 ) |
| 60 | + { |
| 61 | + DispatchRaysWithAttributes( Attributes, threadsX, threadsY, threadsZ ); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Dispatches the ray tracing shader by reading dispatch arguments from an indirect buffer. |
| 66 | + /// </summary> |
| 67 | + /// <remarks> |
| 68 | + /// <para> |
| 69 | + /// <paramref name="indirectBuffer"/> must be created with <see cref="GpuBuffer.UsageFlags.IndirectDrawArguments"/> |
| 70 | + /// and have an element size of 12 bytes (3 uint32 values for X, Y, Z dimensions). |
| 71 | + /// </para> |
| 72 | + /// <para> |
| 73 | + /// <paramref name="indirectElementOffset"/> is an element index into <paramref name="indirectBuffer"/>, not a byte offset. |
| 74 | + /// </para> |
| 75 | + /// <para> |
| 76 | + /// When called outside a graphics context, the dispatch runs immediately. |
| 77 | + /// When called inside a graphics context, the dispatch runs async. |
| 78 | + /// </para> |
| 79 | + /// </remarks> |
| 80 | + /// <param name="indirectBuffer">The GPU buffer containing one or more dispatch argument entries.</param> |
| 81 | + /// <param name="indirectElementOffset">The index of the dispatch arguments element to use (each element = 12 bytes).</param> |
| 82 | + public void DispatchRaysIndirect( GpuBuffer indirectBuffer, uint indirectElementOffset = 0 ) |
| 83 | + { |
| 84 | + DispatchRaysIndirectWithAttributes( Attributes, indirectBuffer, indirectElementOffset ); |
| 85 | + } |
| 86 | + |
| 87 | + /// <inheritdoc cref="DispatchRaysIndirect"/> |
| 88 | + public void DispatchRaysIndirectWithAttributes( RenderAttributes attributes, GpuBuffer indirectBuffer, uint indirectElementOffset = 0 ) |
| 89 | + { |
| 90 | + if ( !indirectBuffer.IsValid() ) |
| 91 | + throw new ArgumentException( $"Invalid buffer", nameof( indirectBuffer ) ); |
| 92 | + |
| 93 | + if ( indirectBuffer.ElementSize != 12 ) |
| 94 | + throw new ArgumentException( $"Buffer element size must be 12 bytes", nameof( indirectBuffer ) ); |
| 95 | + |
| 96 | + if ( indirectElementOffset >= indirectBuffer.ElementCount ) |
| 97 | + throw new ArgumentOutOfRangeException( nameof( indirectElementOffset ), "Indirect element offset exceeds buffer bounds" ); |
| 98 | + |
| 99 | + if ( !indirectBuffer.Usage.Contains( GpuBuffer.UsageFlags.IndirectDrawArguments ) ) |
| 100 | + throw new ArgumentException( $"Buffer must have the required usage flag '{GpuBuffer.UsageFlags.IndirectDrawArguments}'", nameof( indirectBuffer ) ); |
| 101 | + |
| 102 | + // Use RenderTools.TraceRaysIndirect when it becomes available, for now use the material mode directly |
| 103 | + var mode = RayTracingMaterial.native.GetMode(); |
| 104 | + RenderTools.TraceRaysIndirect( Graphics.Context, attributes.Get(), mode, indirectBuffer.native, indirectElementOffset * 12 ); |
| 105 | + } |
| 106 | +} |
0 commit comments