108 lines
5.2 KiB
Plaintext
108 lines
5.2 KiB
Plaintext
// We need N bounces given that we want to support complex light paths
|
|
#pragma max_recursion_depth 10
|
|
|
|
// WARNING: This define must be kept in sync with the c# code
|
|
#define RAYTRACING_MAX_RECURSION 10
|
|
|
|
// HDRP include
|
|
#define SHADER_TARGET 50
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/Sampling.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
|
|
|
|
// Raytracing includes
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/ShaderVariablesRaytracing.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingIntersection.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingSampling.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Common/AtmosphericScatteringRayTracing.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/RayCountManager.cs.hlsl"
|
|
|
|
// The target acceleration structure that we will evaluate the reflexion in
|
|
TEXTURE2D_X(_DepthTexture);
|
|
|
|
// Output structure of the reflection raytrace shader
|
|
TEXTURE2D_X(_RaytracingFlagMask);
|
|
RW_TEXTURE2D_X(float4, _CameraColorTextureRW);
|
|
RW_TEXTURE2D_X(float4, _RaytracingPrimaryDebug);
|
|
|
|
[shader("miss")]
|
|
void MissShaderRenderer(inout RayIntersection rayIntersection : SV_RayPayload)
|
|
{
|
|
rayIntersection.color = SAMPLE_TEXTURECUBE_ARRAY_LOD(_SkyTexture, s_trilinear_clamp_sampler, rayIntersection.incidentDirection, 0.0f, 0);
|
|
ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), rayIntersection.color);
|
|
}
|
|
|
|
[shader("raygeneration")]
|
|
void RayGenRenderer()
|
|
{
|
|
uint3 LaunchIndex = DispatchRaysIndex();
|
|
uint2 LaunchDim = DispatchRaysDimensions();
|
|
|
|
UNITY_XR_ASSIGN_VIEW_INDEX(LaunchIndex.z);
|
|
|
|
// Pixel coordinate of the current pixel
|
|
uint2 currentPixelCoord = uint2(LaunchIndex.x, LaunchIndex.y);
|
|
|
|
_RaytracingPrimaryDebug[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(0.0, 0.0, 0.0, 1.0);
|
|
|
|
|
|
// If we do not need to override the pixel, stop right away
|
|
if (_RaytracingFlagMask[COORD_TEXTURE2D_X(currentPixelCoord)].x < 0.5f)
|
|
return;
|
|
|
|
// Read the depth value
|
|
float depthValue = LOAD_TEXTURE2D_X(_DepthTexture, currentPixelCoord).x;
|
|
|
|
// Convert this to a world space position
|
|
PositionInputs posInput = GetPositionInput(currentPixelCoord, _ScreenSize.zw, depthValue, UNITY_MATRIX_I_VP, GetWorldToViewMatrix(), 0);
|
|
float3 originWS = GetPrimaryCameraPosition();
|
|
float3 incidentWS = normalize(posInput.positionWS - originWS);
|
|
|
|
// Create the ray descriptor for this pixel
|
|
RayDesc rayDescriptor;
|
|
rayDescriptor.Origin = originWS;
|
|
rayDescriptor.Direction = incidentWS;
|
|
rayDescriptor.TMin = _RaytracingCameraNearPlane;
|
|
rayDescriptor.TMax = _RaytracingRayMaxLength;
|
|
|
|
// Create and init the RayIntersection structure for this
|
|
RayIntersection rayIntersection;
|
|
rayIntersection.color = float3(0.0, 0.0, 0.0);
|
|
rayIntersection.incidentDirection = rayDescriptor.Direction;
|
|
rayIntersection.origin = rayDescriptor.Origin;
|
|
rayIntersection.t = 0.0f;
|
|
rayIntersection.remainingDepth = min(RAYTRACING_MAX_RECURSION, _RaytracingMaxRecursion) - 1;
|
|
rayIntersection.rayCount = 1;
|
|
rayIntersection.pixelCoord = currentPixelCoord;
|
|
|
|
// In order to achieve filtering for the textures, we need to compute the spread angle of the pixel
|
|
rayIntersection.cone.spreadAngle = _RaytracingPixelSpreadAngle;
|
|
rayIntersection.cone.width = 0.0f;
|
|
|
|
// Evaluate the ray intersection
|
|
TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_RECURSIVE_RENDERING, 0, 1, 0, rayDescriptor, rayIntersection);
|
|
|
|
// Count the number of rays that were traced
|
|
if (_RayCountEnabled > 0)
|
|
{
|
|
uint3 counterIdx = uint3(currentPixelCoord, INDEX_TEXTURE2D_ARRAY_X(RAYCOUNTVALUES_RECURSIVE));
|
|
_RayCountTexture[counterIdx] = _RayCountTexture[counterIdx] + rayIntersection.rayCount;
|
|
}
|
|
|
|
// Alright we are done :D
|
|
_RaytracingPrimaryDebug[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(rayIntersection.color * GetCurrentExposureMultiplier(), 1.0);
|
|
_CameraColorTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(rayIntersection.color * GetCurrentExposureMultiplier(), 1.0);
|
|
}
|
|
|
|
[shader("closesthit")]
|
|
void ClosestHitMain(inout RayIntersection rayIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes)
|
|
{
|
|
// When we do not hit any known closest hit, that means that no shader was specified for the target object meaning either it has nothing to do in the acceleration structure or we need to add raytracing subshaders to it
|
|
rayIntersection.color = float3(1.0, 0.0, 0.5);
|
|
}
|