void RenderRayTracedDirectionalScreenSpaceShadow(CommandBuffer cmd, HDCamera hdCamera) { // Request the intermediate buffers we shall be using RTHandle outputShadowBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA0); RTHandle directionBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.Direction); RTHandle velocityBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.R1); RTHandle distanceBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.Distance); // Ray trace for shadow evaluation RTShadowDirectionalTraceParameters rtsdtParams = PrepareRTShadowDirectionalTraceParameters(hdCamera, m_CurrentSunLightAdditionalLightData); RTShadowDirectionalTraceResources rtsdtResources = PrepareSSSDirectionalTraceResources(velocityBuffer, directionBuffer, distanceBuffer, outputShadowBuffer); ExecuteSSSDirectionalTrace(cmd, rtsdtParams, rtsdtResources); // If required, denoise the shadow if (m_CurrentSunLightAdditionalLightData.filterTracedShadow && rtsdtParams.softShadow) { DenoiseDirectionalScreenSpaceShadow(cmd, hdCamera, velocityBuffer, distanceBuffer, outputShadowBuffer); } // Write the result texture to the screen space shadow buffer int dirShadowIndex = m_CurrentSunLightDirectionalLightData.screenSpaceShadowIndex & (int)LightDefinitions.s_ScreenSpaceShadowIndexMask; WriteScreenSpaceShadowParameters wsssParams = PrepareWriteScreenSpaceShadowParameters(hdCamera, dirShadowIndex, m_CurrentSunLightAdditionalLightData.colorShadow ? ScreenSpaceShadowType.Color : ScreenSpaceShadowType.GrayScale); WriteScreenSpaceShadowResources wsssResources = PrepareWriteScreenSpaceShadowResources(outputShadowBuffer); ExecuteWriteScreenSpaceShadow(cmd, wsssParams, wsssResources); }
RTShadowDirectionalTraceResources PrepareSSSDirectionalTraceResources(RTHandle velocityBuffer, RTHandle directionBuffer, RTHandle distanceBuffer, RTHandle outputShadowBuffer) { RTShadowDirectionalTraceResources rtsdtResources = new RTShadowDirectionalTraceResources(); // Input Buffers rtsdtResources.depthStencilBuffer = m_SharedRTManager.GetDepthStencilBuffer(); rtsdtResources.normalBuffer = m_SharedRTManager.GetNormalBuffer(); // Intermediate buffers rtsdtResources.directionBuffer = directionBuffer; // Debug textures RayCountManager rayCountManager = GetRayCountManager(); rtsdtResources.rayCountTexture = rayCountManager.GetRayCountTexture(); // Output buffers rtsdtResources.velocityBuffer = velocityBuffer; rtsdtResources.distanceBuffer = distanceBuffer; rtsdtResources.outputShadowBuffer = outputShadowBuffer; return(rtsdtResources); }
void RenderRayTracedDirectionalScreenSpaceShadow(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle depthBuffer, TextureHandle normalBuffer, TextureHandle motionVetorsBuffer, TextureHandle rayCountTexture, TextureHandle screenSpaceShadowArray) { TextureHandle directionalShadow; TextureHandle velocityBuffer; TextureHandle distanceBuffer; RTShadowDirectionalTraceParameters rtsdtParams = PrepareRTShadowDirectionalTraceParameters(hdCamera, m_CurrentSunLightAdditionalLightData); using (var builder = renderGraph.AddRenderPass <RTSDirectionalTracePassData>("Directional RT Shadow", out var passData, ProfilingSampler.Get(HDProfileId.RaytracingDirectionalLightShadow))) { passData.parameters = rtsdtParams; // Input Buffer passData.depthStencilBuffer = builder.UseDepthBuffer(depthBuffer, DepthAccess.Read); passData.normalBuffer = builder.ReadTexture(normalBuffer); passData.directionBuffer = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "Direction Buffer" }); // Debug buffers passData.rayCountTexture = builder.ReadTexture(builder.WriteTexture(rayCountTexture)); // Output Buffers passData.velocityBuffer = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8_SNorm, enableRandomWrite = true, clearBuffer = true, name = "Velocity Buffer" }))); passData.distanceBuffer = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R32_SFloat, enableRandomWrite = true, clearBuffer = true, name = "Distance Buffer" }))); passData.outputShadowBuffer = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, clearBuffer = true, name = "RT Directional Shadow" }))); builder.SetRenderFunc( (RTSDirectionalTracePassData data, RenderGraphContext context) => { RTShadowDirectionalTraceResources resources = new RTShadowDirectionalTraceResources(); resources.depthStencilBuffer = data.depthStencilBuffer; resources.normalBuffer = data.normalBuffer; resources.directionBuffer = data.directionBuffer; resources.rayCountTexture = data.rayCountTexture; resources.velocityBuffer = data.velocityBuffer; resources.distanceBuffer = data.distanceBuffer; resources.outputShadowBuffer = data.outputShadowBuffer; ExecuteSSSDirectionalTrace(context.cmd, data.parameters, resources); }); directionalShadow = passData.outputShadowBuffer; velocityBuffer = passData.velocityBuffer; distanceBuffer = passData.distanceBuffer; } // If required, denoise the shadow if (m_CurrentSunLightAdditionalLightData.filterTracedShadow && rtsdtParams.softShadow) { directionalShadow = DenoiseDirectionalScreenSpaceShadow(renderGraph, hdCamera, depthBuffer, normalBuffer, motionVetorsBuffer, directionalShadow, velocityBuffer, distanceBuffer); } int dirShadowIndex = m_CurrentSunLightDirectionalLightData.screenSpaceShadowIndex & (int)LightDefinitions.s_ScreenSpaceShadowIndexMask; ScreenSpaceShadowType shadowType = m_CurrentSunLightAdditionalLightData.colorShadow? ScreenSpaceShadowType.Color: ScreenSpaceShadowType.GrayScale; // Write the result texture to the screen space shadow buffer WriteScreenSpaceShadow(renderGraph, hdCamera, directionalShadow, screenSpaceShadowArray, dirShadowIndex, shadowType); }
static void ExecuteSSSDirectionalTrace(CommandBuffer cmd, RTShadowDirectionalTraceParameters rtsdtParams, RTShadowDirectionalTraceResources rtsdtResources) { // Inject the ray-tracing sampling data BlueNoise.BindDitheredTextureSet(cmd, rtsdtParams.ditheredTextureSet); // Evaluate the dispatch parameters int shadowTileSize = 8; int numTilesX = (rtsdtParams.texWidth + (shadowTileSize - 1)) / shadowTileSize; int numTilesY = (rtsdtParams.texHeight + (shadowTileSize - 1)) / shadowTileSize; // Clear the integration texture cmd.SetComputeTextureParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.clearShadowKernel, HDShaderIDs._RaytracedShadowIntegration, rtsdtResources.outputShadowBuffer); cmd.DispatchCompute(rtsdtParams.screenSpaceShadowCS, rtsdtParams.clearShadowKernel, numTilesX, numTilesY, rtsdtParams.viewCount); cmd.SetComputeTextureParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.clearShadowKernel, HDShaderIDs._RaytracedShadowIntegration, rtsdtResources.velocityBuffer); cmd.DispatchCompute(rtsdtParams.screenSpaceShadowCS, rtsdtParams.clearShadowKernel, numTilesX, numTilesY, rtsdtParams.viewCount); cmd.SetComputeTextureParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.clearShadowKernel, HDShaderIDs._RaytracedShadowIntegration, rtsdtResources.distanceBuffer); cmd.DispatchCompute(rtsdtParams.screenSpaceShadowCS, rtsdtParams.clearShadowKernel, numTilesX, numTilesY, rtsdtParams.viewCount); // Grab and bind the acceleration structure for the target camera cmd.SetRayTracingAccelerationStructure(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._RaytracingAccelerationStructureName, rtsdtParams.accelerationStructure); // Make sure the right closest hit/any hit will be triggered by using the right multi compile CoreUtils.SetKeyword(cmd, "TRANSPARENT_COLOR_SHADOW", rtsdtParams.colorShadow); // Define which ray generation shaders we shall be using string directionaLightShadowShader = rtsdtParams.colorShadow ? m_RayGenDirectionalColorShadowSingleName : m_RayGenDirectionalShadowSingleName; // Loop through the samples of this frame for (int sampleIdx = 0; sampleIdx < rtsdtParams.numShadowSamples; ++sampleIdx) { // Update global Constant Buffer rtsdtParams.shaderVariablesRayTracingCB._RaytracingSampleIndex = sampleIdx; rtsdtParams.shaderVariablesRayTracingCB._RaytracingNumSamples = rtsdtParams.numShadowSamples; ConstantBuffer.PushGlobal(cmd, rtsdtParams.shaderVariablesRayTracingCB, HDShaderIDs._ShaderVariablesRaytracing); // Bind the light & sampling data cmd.SetComputeBufferParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.directionalShadowSample, HDShaderIDs._DirectionalLightDatas, rtsdtResources.lightData); // Input Buffer cmd.SetComputeTextureParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.directionalShadowSample, HDShaderIDs._DepthTexture, rtsdtResources.depthStencilBuffer); cmd.SetComputeTextureParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.directionalShadowSample, HDShaderIDs._NormalBufferTexture, rtsdtResources.normalBuffer); // Output buffer cmd.SetComputeTextureParam(rtsdtParams.screenSpaceShadowCS, rtsdtParams.directionalShadowSample, HDShaderIDs._RaytracingDirectionBuffer, rtsdtResources.directionBuffer); // Generate a new direction cmd.DispatchCompute(rtsdtParams.screenSpaceShadowCS, rtsdtParams.directionalShadowSample, numTilesX, numTilesY, rtsdtParams.viewCount); // Define the shader pass to use for the shadow pass cmd.SetRayTracingShaderPass(rtsdtParams.screenSpaceShadowRT, "VisibilityDXR"); // Set ray count texture cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._RayCountTexture, rtsdtResources.rayCountTexture); // Input buffers cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._DepthTexture, rtsdtResources.depthStencilBuffer); cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._NormalBufferTexture, rtsdtResources.normalBuffer); cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._RaytracingDirectionBuffer, rtsdtResources.directionBuffer); // Output buffer cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, rtsdtParams.colorShadow ? HDShaderIDs._RaytracedColorShadowIntegration : HDShaderIDs._RaytracedShadowIntegration, rtsdtResources.outputShadowBuffer); cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._VelocityBuffer, rtsdtResources.velocityBuffer); cmd.SetRayTracingTextureParam(rtsdtParams.screenSpaceShadowRT, HDShaderIDs._RaytracingDistanceBufferRW, rtsdtResources.distanceBuffer); // Evaluate the visibility cmd.DispatchRays(rtsdtParams.screenSpaceShadowRT, directionaLightShadowShader, (uint)rtsdtParams.texWidth, (uint)rtsdtParams.texHeight, (uint)rtsdtParams.viewCount); } // Now that we are done with the ray tracing bit, disable the multi compile that was potentially enabled CoreUtils.SetKeyword(cmd, "TRANSPARENT_COLOR_SHADOW", false); }