DispatchRays
Adds a command to execute a RayTracingShader.
Read time 7 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Rendering
- Assembly: UnityEngine.CoreModule
DispatchRays(RayTracingShader, string, uint, uint, uint, Camera)
Adds a command to execute a RayTracingShader.
public void DispatchRays(RayTracingShader rayTracingShader, string rayGenName, uint width, uint height, uint depth, Camera camera = null)
Parameters
RayTracingShader to execute.
The name of the ray generation shader.
The width of the ray generation shader thread grid.
The height of the ray generation shader thread grid.
The depth of the ray generation shader thread grid.
Optional parameter used to setup camera-related built-in shader variables.
Remarks
When the command buffer executes, the GPU launches the threads of the ray generation shader you specify as an argument for this method.
Retrieve the width, height, and depth values with the HLSL function. Retrieve the ray generation shader invocation index values with the HLSL function.
DispatchRaysDimensions()DispatchRaysIndex()Width, height and depth must be above zero and widthheightdepth <= 2^30. When using the and arguments for an indirect CommandBuffer.DispatchRays call, the buffer with arguments, , has to contain three integer numbers at given values representing the dispatch dimensions: width, height and depth.
argsBufferargsOffsetargsBufferargsOffsetWhen an optional Camera is specified as parameter, the built-in shader variables related to the Camera, Screen and Time are set up. Check Built-in shader variables for a complete list of these variables.
A RayTracingShader only supports the following shader types: ray generation, miss and callable.
You can use the following pragma directives in a raytrace file:
- - how many times you can recursively call
#pragma max_recursion_depth <value>in HLSL. A value of 1 means you can callTraceRayfrom ray generation shaders only. Exceeding the declared recursion depth will cause undefined behavior, including GPU crashes.TraceRay - - embeds Program Database (PBD) files into shader binaries to make shader debugging available in debugging tools like PIX.
#pragma enable_ray_tracing_shader_debug_symbols - - compile this shader program only for given graphics APIs. For a list of values, see Targeting graphics APIs and platforms in HLSL.
#pragma only_renderers <values> - - do not compile this shader program for given graphics APIs. For a list of values, see Targeting graphics APIs and platforms in HLSL.
#pragma exclude_renderers <values> - - the minimum GPU features with which this shader is compatible. Replace \<value\> with one of the values:
#pragma require <values>,native16bit,int64.int64bufferatomics - - disables ray payload size compatibility checks between different ray tracing shader types. Use this carefully, because removing the check can cause corrupt ray payload data when mixing incompatible ray tracing shaders using different ray payloads.
#pragma disable_ray_payload_size_checks
#include "UnityShaderVariables.cginc"#pragma max_recursion_depth 1// InputRaytracingAccelerationStructure g_SceneAccelStruct;float g_Zoom; //Mathf.Tan(Mathf.Deg2Rad * Camera.main.fieldOfView * 0.5f)// OutputRWTexture2D<float4> g_Output;struct RayPayload{ float4 color;};[shader("miss")]void MainMissShader(inout RayPayload payload : SV_RayPayload){ payload.color = float4(0, 0, 0, 1);}[shader("raygeneration")]void MainRayGenShader(){ uint2 launchIndex = DispatchRaysIndex().xy; uint2 launchDim = DispatchRaysDimensions().xy; float2 frameCoord = float2(launchIndex.x, launchDim.y - launchIndex.y - 1) + float2(0.5, 0.5); float2 ndcCoords = frameCoord / float2(launchDim.x - 1, launchDim.y - 1); ndcCoords = ndcCoords * 2 - float2(1, 1); ndcCoords = ndcCoords * g_Zoom; float aspectRatio = (float)launchDim.x / (float)launchDim.y; float3 viewDirection = normalize(float3(ndcCoords.x * aspectRatio, ndcCoords.y, 1)); // Rotate the ray from view space to world space. float3 rayDirection = normalize(mul((float3x3)unity_CameraToWorld, viewDirection)); RayDesc ray; ray.Origin = _WorldSpaceCameraPos; ray.Direction = rayDirection; ray.TMin = 0.0f; ray.TMax = 1000.0f; RayPayload payload; payload.color = float4(1, 1, 1, 1); uint missShaderIndex = 0; TraceRay(g_SceneAccelStruct, 0, 0xFF, 0, 1, missShaderIndex, ray, payload); g_Output[frameCoord] = payload.color;}
In this ray generation shader example, you calculate a ray direction based on the 2D thread index returned by the function. The output is white if there is a ray/triangle intersection. When there is no intersection, the GPU executes MainMissShader and the output is black. This example uses the built-in shader variable. You must specify a Camera as an argument for the function to set this value correctly.
DispatchRaysIndex()unity_CameraToWorldDispatchRaysAdditional Resources: SystemInfo.supportsRayTracingShaders, RayTracingAccelerationStructure, RayTracingShader.DispatchIndirect.
DispatchRays(RayTracingShader, string, GraphicsBuffer, uint, Camera)
Adds a command to execute a RayTracingShader.
public void DispatchRays(RayTracingShader rayTracingShader, string rayGenName, GraphicsBuffer argsBuffer, uint argsOffset, Camera camera = null)
Parameters
RayTracingShader to execute.
The name of the ray generation shader.
Buffer containing dispatch dimensions for indirect DispatchRays.
The byte offset into argsBuffer where the dispatch dimensions start.
Optional parameter used to setup camera-related built-in shader variables.
Remarks
When the command buffer executes, the GPU launches the threads of the ray generation shader you specify as an argument for this method.
Retrieve the width, height, and depth values with the HLSL function. Retrieve the ray generation shader invocation index values with the HLSL function.
DispatchRaysDimensions()DispatchRaysIndex()Width, height and depth must be above zero and widthheightdepth <= 2^30. When using the and arguments for an indirect CommandBuffer.DispatchRays call, the buffer with arguments, , has to contain three integer numbers at given values representing the dispatch dimensions: width, height and depth.
argsBufferargsOffsetargsBufferargsOffsetWhen an optional Camera is specified as parameter, the built-in shader variables related to the Camera, Screen and Time are set up. Check Built-in shader variables for a complete list of these variables.
A RayTracingShader only supports the following shader types: ray generation, miss and callable.
You can use the following pragma directives in a raytrace file:
- - how many times you can recursively call
#pragma max_recursion_depth <value>in HLSL. A value of 1 means you can callTraceRayfrom ray generation shaders only. Exceeding the declared recursion depth will cause undefined behavior, including GPU crashes.TraceRay - - embeds Program Database (PBD) files into shader binaries to make shader debugging available in debugging tools like PIX.
#pragma enable_ray_tracing_shader_debug_symbols - - compile this shader program only for given graphics APIs. For a list of values, see Targeting graphics APIs and platforms in HLSL.
#pragma only_renderers <values> - - do not compile this shader program for given graphics APIs. For a list of values, see Targeting graphics APIs and platforms in HLSL.
#pragma exclude_renderers <values> - - the minimum GPU features with which this shader is compatible. Replace \<value\> with one of the values:
#pragma require <values>,native16bit,int64.int64bufferatomics - - disables ray payload size compatibility checks between different ray tracing shader types. Use this carefully, because removing the check can cause corrupt ray payload data when mixing incompatible ray tracing shaders using different ray payloads.
#pragma disable_ray_payload_size_checks
#include "UnityShaderVariables.cginc"#pragma max_recursion_depth 1// InputRaytracingAccelerationStructure g_SceneAccelStruct;float g_Zoom; //Mathf.Tan(Mathf.Deg2Rad * Camera.main.fieldOfView * 0.5f)// OutputRWTexture2D<float4> g_Output;struct RayPayload{ float4 color;};[shader("miss")]void MainMissShader(inout RayPayload payload : SV_RayPayload){ payload.color = float4(0, 0, 0, 1);}[shader("raygeneration")]void MainRayGenShader(){ uint2 launchIndex = DispatchRaysIndex().xy; uint2 launchDim = DispatchRaysDimensions().xy; float2 frameCoord = float2(launchIndex.x, launchDim.y - launchIndex.y - 1) + float2(0.5, 0.5); float2 ndcCoords = frameCoord / float2(launchDim.x - 1, launchDim.y - 1); ndcCoords = ndcCoords * 2 - float2(1, 1); ndcCoords = ndcCoords * g_Zoom; float aspectRatio = (float)launchDim.x / (float)launchDim.y; float3 viewDirection = normalize(float3(ndcCoords.x * aspectRatio, ndcCoords.y, 1)); // Rotate the ray from view space to world space. float3 rayDirection = normalize(mul((float3x3)unity_CameraToWorld, viewDirection)); RayDesc ray; ray.Origin = _WorldSpaceCameraPos; ray.Direction = rayDirection; ray.TMin = 0.0f; ray.TMax = 1000.0f; RayPayload payload; payload.color = float4(1, 1, 1, 1); uint missShaderIndex = 0; TraceRay(g_SceneAccelStruct, 0, 0xFF, 0, 1, missShaderIndex, ray, payload); g_Output[frameCoord] = payload.color;}
In this ray generation shader example, you calculate a ray direction based on the 2D thread index returned by the function. The output is white if there is a ray/triangle intersection. When there is no intersection, the GPU executes MainMissShader and the output is black. This example uses the built-in shader variable. You must specify a Camera as an argument for the function to set this value correctly.
DispatchRaysIndex()unity_CameraToWorldDispatchRaysAdditional Resources: SystemInfo.supportsRayTracingShaders, RayTracingAccelerationStructure, RayTracingShader.DispatchIndirect.