2021-09-09 20:42:29 -04:00

73 lines
3.1 KiB
Plaintext

#pragma kernel DebugLightCluster
#pragma only_renderers d3d11
#define DEBUG_LIGHT_CLUSTER_TILE_SIZE 8
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/ShaderVariablesRaytracingLightLoop.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingLightCluster.hlsl"
// Color gradient texture used for the lightcount heatmap
TEXTURE2D(_DebugColorGradientTexture);
// The output texture for the cluster debug
RW_TEXTURE2D_X(float4, _DebutLightClusterTexture);
[numthreads(DEBUG_LIGHT_CLUSTER_TILE_SIZE, DEBUG_LIGHT_CLUSTER_TILE_SIZE, 1)]
void DebugLightCluster(uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
{
// Fetch the current pixel coordinate
uint2 currentPixelCoordinate = groupId * DEBUG_LIGHT_CLUSTER_TILE_SIZE + groupThreadId;
// Convert this to a world space position
float depth = LoadCameraDepth(currentPixelCoordinate.xy);
// If this is a background set it to black
if (depth == UNITY_RAW_FAR_CLIP_VALUE)
{
_DebutLightClusterTexture[COORD_TEXTURE2D_X(currentPixelCoordinate)] = float4(0.0, 0.0, 0.0, 1.0f);
return;
}
// Compute the real world space position of this pixel
PositionInputs posInput = GetPositionInput(currentPixelCoordinate, 1.0 / _ScreenSize.xy, depth, UNITY_MATRIX_I_VP, GetWorldToViewMatrix(), 0);
// If this position is outisde of the cluster, color is gray
if(!PointInsideCluster(posInput.positionWS))
{
_DebutLightClusterTexture[COORD_TEXTURE2D_X(currentPixelCoordinate)] = float4(0.1, 0.1, 0.1, 1.0f);
return;
}
// Compute the grid position
float3 floatingGridPosition = (posInput.positionWS - _MinClusterPos) / (_MaxClusterPos - _MinClusterPos) * float3(64.0, 64.0, 32.0);
// Convert it to an int3
uint3 gridPosition = (uint3)floatingGridPosition;
// Deduce the cell index
uint cellIndex = GetClusterCellIndex(posInput.positionWS);
// Fetch the light count
uint numLights = GetTotalLightClusterCellCount(cellIndex);
// If this pixel has no lights, let the color to gray
if(numLights == 0)
{
_DebutLightClusterTexture[COORD_TEXTURE2D_X(currentPixelCoordinate)] = float4(0.1, 0.1, 0.1, 1.0f);
return;
}
// Compute the color of this pixel
float linearDepth = Linear01Depth(depth, _ZBufferParams);
float lightFillPercentage = (float)numLights / (float)_LightPerCellCount;
float3 cellColor = lerp(float3(0.0, 1.0, 0.0), float3(1.0, 1.0, 0.0), clamp(lightFillPercentage, 0.0, 1.0)) * linearDepth;
cellColor.xyz = numLights >= _LightPerCellCount ? float3(0.7, 0.0, 0.0) : cellColor.xyz;
// Return the color
_DebutLightClusterTexture[COORD_TEXTURE2D_X(currentPixelCoordinate)] = float4(cellColor, 1.0);
}