// We need N bounces given that we want to support complex light paths #pragma max_recursion_depth 10 #define HAS_LIGHTLOOP // Include and define the shader pass #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl" #define SHADERPASS SHADERPASS_RAYTRACING // 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/Color.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/BSDF.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/PreIntegratedFGD/PreIntegratedFGD.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonLighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ImageBasedLighting.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialEvaluation.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.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/ShaderVariablesRaytracingLightLoop.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingLightCluster.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" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/RayTracingFallbackHierarchy.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) { float3 rayOrigin = WorldRayOrigin(); float3 rayDirection = WorldRayDirection(); float weight = 0.0f; if (RAYTRACINGFALLBACKHIERACHY_REFLECTION_PROBES & _RayTracingRayMissFallbackHierarchy) rayIntersection.color = RayTraceReflectionProbes(rayOrigin, rayDirection, weight); if((RAYTRACINGFALLBACKHIERACHY_SKY & _RayTracingRayMissFallbackHierarchy) && weight < 1.0) { rayIntersection.color += SAMPLE_TEXTURECUBE_ARRAY_LOD(_SkyTexture, s_trilinear_clamp_sampler, rayDirection, 0.0, 0).xyz * (1.0 - weight); weight = 1.0f; } if (weight > 0.0) ApplyFogAttenuation(rayOrigin, rayDirection, rayIntersection.color); } [shader("raygeneration")] void RayGenRenderer() { uint3 LaunchIndex = DispatchRaysIndex(); uint2 LaunchDim = DispatchRaysDimensions().xy; 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; // For the initial ray, we don't want to have any disparity between the rasterization and ray tracing (which objects are rendered) // thus we need to force the max distance to the far plane. rayDescriptor.TMin = _ProjectionParams.y; rayDescriptor.TMax = _ProjectionParams.z; // Adjust world-space position to match the RAS setup with XR single-pass and camera relative ApplyCameraRelativeXR(rayDescriptor.Origin); // Create and init the RayIntersection structure for this RayIntersection rayIntersection; rayIntersection.color = float3(0.0, 0.0, 0.0); 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); }