Version: 2022.1
Experimental: this API is experimental and might be changed or removed in the future.

RayTracingShader.Dispatch

切换到手册
public void Dispatch (string rayGenFunctionName, int width, int height, int depth, Camera camera);

参数

rayGenFunctionName The name of the ray generation shader.
width 光线生成着色器线程网格的宽度。
height 光线生成着色器线程网格的高度。
depth 光线生成着色器线程网格的深度。
camera Optional parameter used to setup camera-related built-in shader variables.

描述

分发 RayTracingShader

When a ray generation shader is executed, the GPU launches a 3D grid of threads. In HLSL, the values of width, height and depth can be retrieved using DispatchRaysDimensions() function. To retrieve the index of the ray generation shader invocation, DispatchRaysIndex() HLSL function can be used.

Width, height and depth must be above zero and width*height*depth <= 2^30.

When 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.

#include "UnityShaderVariables.cginc"

#pragma max_recursion_depth 1

// Input RaytracingAccelerationStructure g_SceneAccelStruct; float g_Zoom; //Mathf.Tan(Mathf.Deg2Rad * Camera.main.fieldOfView * 0.5f)

// Output RWTexture2D<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; }

This is a simple ray generation shader example where a ray direction is calculated based on the current 2D thread index returned by the DispatchRaysIndex() function. The output will contain a white color value if there is a ray/triangle intersection or a black color value if nothing is hit (MainMissShader is executed). In this example, unity_CameraToWorld built-in shader variable is used and in order for this value to be correctly set, a Camera has to be specified as argument for the Dispatch function.