void RenderSubsurfaceScatteringRT(HDCamera hdCamera, CommandBuffer cmd, RTHandle colorBufferRT, RTHandle diffuseBufferRT, RTHandle depthStencilBufferRT, RTHandle depthTextureRT) { using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SubsurfaceScattering))) { // Grab the SSS params var settings = hdCamera.volumeStack.GetComponent <SubSurfaceScattering>(); // Fetch all the intermediate buffers that we need (too much of them to be fair) RTHandle intermediateBuffer0 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA0); RTHandle intermediateBuffer1 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA1); RTHandle intermediateBuffer2 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA2); RTHandle intermediateBuffer3 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA3); RTHandle intermediateBuffer4 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA4); RTHandle directionBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.Direction); // Evaluate the lighting for the samples that we need to SSSRayTracingParameters sssrtParams = PrepareSSSRayTracingParameters(hdCamera, settings); SSSRayTracingResources sssrtResources = PrepareSSSRayTracingResources(m_SSSColor, intermediateBuffer0, intermediateBuffer1, intermediateBuffer2, intermediateBuffer3, directionBuffer, intermediateBuffer4); ExecuteRTSubsurfaceScattering(cmd, sssrtParams, sssrtResources); // Grab the history buffer RTHandle subsurfaceHistory = hdCamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.RayTracedSubSurface) ?? hdCamera.AllocHistoryFrameRT((int)HDCameraFrameHistoryType.RayTracedSubSurface, SubSurfaceHistoryBufferAllocatorFunction, 1); // Check if we need to invalidate the history float historyValidity = EvaluateHistoryValidity(hdCamera); // Apply temporal filtering to the signal HDTemporalFilter temporalFilter = GetTemporalFilter(); TemporalFilterParameters tfParameters = temporalFilter.PrepareTemporalFilterParameters(hdCamera, false, historyValidity); RTHandle validationBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.R0); TemporalFilterResources tfResources = temporalFilter.PrepareTemporalFilterResources(hdCamera, validationBuffer, intermediateBuffer4, subsurfaceHistory, intermediateBuffer0); HDTemporalFilter.DenoiseBuffer(cmd, tfParameters, tfResources); // Combine the result with the rest of the lighting SSSCombineParameters ssscParams = PrepareSSSCombineParameters(hdCamera); SSSCombineResources ssscResources = PrepareSSSCombineResources(m_SSSColor, colorBufferRT, diffuseBufferRT, intermediateBuffer0, ssscParams.validSSGI); ExecuteCombineSubsurfaceScattering(cmd, ssscParams, ssscResources); // Push this version of the texture for debug PushFullScreenDebugTexture(hdCamera, cmd, diffuseBufferRT, FullScreenDebugMode.RayTracedSubSurface); } }
static void ExecuteCombineSubsurfaceScattering(CommandBuffer cmd, SSSCombineParameters ssscParams, SSSCombineResources ssscResources) { // Evaluate the dispatch parameters int numTilesXHR = (ssscParams.texWidth + (s_sssTileSize - 1)) / s_sssTileSize; int numTilesYHR = (ssscParams.texHeight + (s_sssTileSize - 1)) / s_sssTileSize; cmd.SetComputeTextureParam(ssscParams.rayTracingSubSurfaceCS, ssscParams.combineSSSKernel, HDShaderIDs._SubSurfaceLightingBuffer, ssscResources.subsurfaceBuffer); cmd.SetComputeTextureParam(ssscParams.rayTracingSubSurfaceCS, ssscParams.combineSSSKernel, HDShaderIDs._DiffuseLightingTextureRW, ssscResources.diffuseLightingBuffer); cmd.SetComputeTextureParam(ssscParams.rayTracingSubSurfaceCS, ssscParams.combineSSSKernel, HDShaderIDs._SSSBufferTexture, ssscResources.sssColor); if (ssscParams.validSSGI) { cmd.SetComputeTextureParam(ssscParams.rayTracingSubSurfaceCS, ssscParams.combineSSSKernel, HDShaderIDs._IndirectDiffuseLightingBuffer, ssscResources.ssgiBuffer); } cmd.DispatchCompute(ssscParams.rayTracingSubSurfaceCS, ssscParams.combineSSSKernel, numTilesXHR, numTilesYHR, ssscParams.viewCount); // Combine it with the rest of the lighting ssscParams.combineLightingMat.SetTexture(HDShaderIDs._IrradianceSource, ssscResources.diffuseLightingBuffer); HDUtils.DrawFullScreen(cmd, ssscParams.combineLightingMat, ssscResources.outputColorBuffer, ssscResources.depthStencilBuffer, shaderPassId: 1); }
SSSCombineParameters PrepareSSSCombineParameters(HDCamera hdCamera) { SSSCombineParameters ssscParams = new SSSCombineParameters(); // Camera parameters ssscParams.texWidth = hdCamera.actualWidth; ssscParams.texHeight = hdCamera.actualHeight; ssscParams.viewCount = hdCamera.viewCount; // Generation parameters ssscParams.validSSGI = GetIndirectDiffuseMode(hdCamera) != IndirectDiffuseMode.Off; // Required kernels ssscParams.combineSSSKernel = ssscParams.validSSGI ? m_CombineSubSurfaceWithGIKernel : m_CombineSubSurfaceKernel; // Other parameters ssscParams.rayTracingSubSurfaceCS = m_Asset.renderPipelineRayTracingResources.subSurfaceRayTracingCS; ssscParams.combineLightingMat = m_CombineLightingPass; return(ssscParams); }