Skip to content

Fixed the sub surface mask not being taken into account when computing ray traced sub surface scattering (1247255) #488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Improved performance of reflection probe management when using a lot of probes.
- Ignoring the disable SSR flags for recursive rendering.
- Removed logic in the UI to disable parameters for contact shadows and fog volume components as it was going against the concept of the volume system.
- Fixed the sub surface mask not being taken into account when computing ray traced sub surface scattering.

## [7.1.1] - 2019-09-05

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c
int numTilesXHR = (hdCamera.actualWidth + (areaTileSize - 1)) / areaTileSize;
int numTilesYHR = (hdCamera.actualHeight + (areaTileSize - 1)) / areaTileSize;

// Clear the integration texture first
cmd.SetComputeTextureParam(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, HDShaderIDs._RaytracedShadowIntegration, diffuseBufferRT);
cmd.DispatchCompute(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, numTilesXHR, numTilesYHR, hdCamera.viewCount);

// Fetch the volume overrides that we shall be using
RayTracingShader subSurfaceShader = m_Asset.renderPipelineRayTracingResources.subSurfaceRayTracing;
RayTracingSettings rayTracingSettings = hdCamera.volumeStack.GetComponent<RayTracingSettings>();
Expand All @@ -302,8 +298,13 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c
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);

// Clear the integration texture first
cmd.SetComputeTextureParam(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, HDShaderIDs._RaytracedShadowIntegration, intermediateBuffer4);
cmd.DispatchCompute(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, numTilesXHR, numTilesYHR, hdCamera.viewCount);

// Grab the acceleration structure for the target camera
RayTracingAccelerationStructure accelerationStructure = RequestAccelerationStructure();

Expand Down Expand Up @@ -358,7 +359,7 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c
cmd.SetComputeTextureParam(deferredRayTracing, currentKernel, HDShaderIDs._DiffuseLightingTextureRW, intermediateBuffer3);

// Bind the output texture (it is used for accumulation read and write)
cmd.SetComputeTextureParam(deferredRayTracing, currentKernel, HDShaderIDs._RaytracingLitBufferRW, diffuseBufferRT);
cmd.SetComputeTextureParam(deferredRayTracing, currentKernel, HDShaderIDs._RaytracingLitBufferRW, intermediateBuffer4);

// Compute the Lighting
cmd.DispatchCompute(deferredRayTracing, currentKernel, numTilesXHR, numTilesYHR, hdCamera.viewCount);
Expand All @@ -380,13 +381,21 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c

// Apply temporal filtering to the buffer
HDTemporalFilter temporalFilter = GetTemporalFilter();
temporalFilter.DenoiseBuffer(cmd, hdCamera, diffuseBufferRT, subsurfaceHistory, intermediateBuffer0, singleChannel: false, historyValidity: historyValidity);
temporalFilter.DenoiseBuffer(cmd, hdCamera, intermediateBuffer4, subsurfaceHistory, intermediateBuffer0, singleChannel: false, historyValidity: historyValidity);

// Now based on the mask, we need to blend the subsurface and the diffuse lighting
ComputeShader rayTracingSubSurfaceCS = m_Asset.renderPipelineRayTracingResources.subSurfaceRayTracingCS;
int m_CombineSubSurfaceKernel = rayTracingSubSurfaceCS.FindKernel("BlendSubSurfaceData");
cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._SubSurfaceLightingBuffer, intermediateBuffer0);
cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._DiffuseLightingTextureRW, diffuseBufferRT);
cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._SSSBufferTexture, m_SSSColor);
cmd.DispatchCompute(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, numTilesXHR, numTilesYHR, hdCamera.viewCount);

// Push this version of the texture for debug
PushFullScreenDebugTexture(hdCamera, cmd, intermediateBuffer0, FullScreenDebugMode.RayTracedSubSurface);
PushFullScreenDebugTexture(hdCamera, cmd, diffuseBufferRT, FullScreenDebugMode.RayTracedSubSurface);

// Combine it with the rest of the lighting
m_CombineLightingPass.SetTexture(HDShaderIDs._IrradianceSource, intermediateBuffer0);
m_CombineLightingPass.SetTexture(HDShaderIDs._IrradianceSource, diffuseBufferRT );
HDUtils.DrawFullScreen(cmd, m_CombineLightingPass, colorBufferRT, depthStencilBufferRT, shaderPassId: 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ partial class HDRenderPipelineRayTracingResources : ScriptableObject
// Sub-Surface Scattering
[Reload("Runtime/RenderPipeline/Raytracing/Shaders/RayTracingSubSurface.raytrace")]
public RayTracingShader subSurfaceRayTracing;
[Reload("Runtime/RenderPipeline/Raytracing/Shaders/SubSurface/RayTracingSubSurface.compute")]
public ComputeShader subSurfaceRayTracingCS;

// Denoising
[Reload("Runtime/RenderPipeline/Raytracing/Shaders/Denoising/TemporalFilter.compute")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ static class HDShaderIDs
public static readonly int _DirectionTextureRW = Shader.PropertyToID("_DirectionTextureRW");
public static readonly int _PositionTextureRW = Shader.PropertyToID("_PositionTextureRW");
public static readonly int _DiffuseLightingTextureRW = Shader.PropertyToID("_DiffuseLightingTextureRW");
public static readonly int _SubSurfaceLightingBuffer = Shader.PropertyToID("_SubSurfaceLightingBuffer");

// Accumulation
public static readonly int _AccumulationFrameIndex = Shader.PropertyToID("_AccumulationFrameIndex");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ internal enum InternalRayTracingBuffers
RGBA0,
RGBA1,
RGBA2,
RGBA3
RGBA3,
RGBA4
}

class HDRayTracingLights
Expand Down Expand Up @@ -107,6 +108,7 @@ public partial class HDRenderPipeline
RTHandle m_RayTracingIntermediateBufferRGBA1;
RTHandle m_RayTracingIntermediateBufferRGBA2;
RTHandle m_RayTracingIntermediateBufferRGBA3;
RTHandle m_RayTracingIntermediateBufferRGBA4;

internal void InitRayTracingManager()
{
Expand Down Expand Up @@ -142,6 +144,8 @@ internal void ReleaseRayTracingManager()
RTHandles.Release(m_RayTracingIntermediateBufferRGBA2);
if (m_RayTracingIntermediateBufferRGBA3 != null)
RTHandles.Release(m_RayTracingIntermediateBufferRGBA3);
if (m_RayTracingIntermediateBufferRGBA4 != null)
RTHandles.Release(m_RayTracingIntermediateBufferRGBA4);

if (m_RayTracingLightCluster != null)
m_RayTracingLightCluster.ReleaseResources();
Expand Down Expand Up @@ -740,6 +744,11 @@ internal RTHandle AllocateBuffer(InternalRayTracingBuffers bufferID)
m_RayTracingIntermediateBufferRGBA3 = RTHandles.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, enableRandomWrite: true, useDynamicScale: true, useMipMap: false, name: "RayTracingIntermediateBufferRGBA3");
return m_RayTracingIntermediateBufferRGBA3;
}
case InternalRayTracingBuffers.RGBA4:
{
m_RayTracingIntermediateBufferRGBA4 = RTHandles.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, enableRandomWrite: true, useDynamicScale: true, useMipMap: false, name: "RayTracingIntermediateBufferRGBA4");
return m_RayTracingIntermediateBufferRGBA4;
}
default:
return null;
}
Expand Down Expand Up @@ -769,6 +778,8 @@ internal RTHandle GetRayTracingBuffer(InternalRayTracingBuffers bufferID)
return m_RayTracingIntermediateBufferRGBA2 != null ? m_RayTracingIntermediateBufferRGBA2 : AllocateBuffer(InternalRayTracingBuffers.RGBA2);
case InternalRayTracingBuffers.RGBA3:
return m_RayTracingIntermediateBufferRGBA3 != null ? m_RayTracingIntermediateBufferRGBA3 : AllocateBuffer(InternalRayTracingBuffers.RGBA3);
case InternalRayTracingBuffers.RGBA4:
return m_RayTracingIntermediateBufferRGBA4 != null ? m_RayTracingIntermediateBufferRGBA4 : AllocateBuffer(InternalRayTracingBuffers.RGBA4);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma kernel BlendSubSurfaceData

#pragma only_renderers d3d11

// Given that this pass does not use the shadow algorithm multi-compile, we need to define SHADOW_LOW to quite the shadow algorithm error
#define SHADOW_LOW

// HDRP generic includes
#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.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/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/Lit/Lit.hlsl"

// The buffer of sub surface scattering that was computed used ray tracing
TEXTURE2D_X(_SubSurfaceLightingBuffer);
// This buffer holds the diffuse lighting without the SSS contribution
RW_TEXTURE2D_X(float4, _DiffuseLightingTextureRW);

#define RAYTRACING_SUB_SURFACE_TILE_SIZE 8

[numthreads(RAYTRACING_SUB_SURFACE_TILE_SIZE, RAYTRACING_SUB_SURFACE_TILE_SIZE, 1)]
void BlendSubSurfaceData(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);

// Compute the pixel position to process
uint2 currentCoord = groupId * RAYTRACING_SUB_SURFACE_TILE_SIZE + groupThreadId;

// Read the SSS Data
SSSData sssData;
DECODE_FROM_SSSBUFFER(currentCoord, sssData);

// Compute the albedo color to use based on the scattering mode
int profileIndex = sssData.diffusionProfileIndex;
uint texturingMode = GetSubsurfaceScatteringTexturingMode(profileIndex);
float3 albedo = ApplySubsurfaceScatteringTexturingMode(texturingMode, sssData.diffuseColor);

// Blend and we are done
_DiffuseLightingTextureRW[COORD_TEXTURE2D_X(currentCoord)] = float4(lerp(_DiffuseLightingTextureRW[COORD_TEXTURE2D_X(currentCoord)].xyz * albedo * GetInverseCurrentExposureMultiplier()
, _SubSurfaceLightingBuffer[COORD_TEXTURE2D_X(currentCoord)].xyz
, sssData.subsurfaceMask), 1.0);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ MonoBehaviour:
type: 3}
subSurfaceRayTracing: {fileID: 4807578003741378534, guid: b29a18f967c92364492508dddf78cff7,
type: 3}
subSurfaceRayTracingCS: {fileID: 7200000, guid: 4e5684a8dba46fe42a47642f9b0a6b89,
type: 3}
temporalFilterCS: {fileID: 7200000, guid: 741979ff70f7bd6489fbcb464280ecff, type: 3}
simpleDenoiserCS: {fileID: 7200000, guid: 74a980f1da9a4f842996035350fe756c, type: 3}
diffuseDenoiserCS: {fileID: 7200000, guid: b4ed2382141619f40af1f743a84ccaea, type: 3}
Expand Down