Skip to content

Commit 924797a

Browse files
authored
Fixed the sub surface mask not being taken into account when computing ray traced sub surface scattering (1247255) (#488)
* Fixed the sub surface mask not being taken into account when computing ray traced sub surface scattering (1247255) * Add missing inverse exposure multiplier
1 parent bb0953a commit 924797a

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
739739
- Improved performance of reflection probe management when using a lot of probes.
740740
- Ignoring the disable SSR flags for recursive rendering.
741741
- 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.
742+
- Fixed the sub surface mask not being taken into account when computing ray traced sub surface scattering.
742743

743744
## [7.1.1] - 2019-09-05
744745

com.unity.render-pipelines.high-definition/Runtime/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,6 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c
288288
int numTilesXHR = (hdCamera.actualWidth + (areaTileSize - 1)) / areaTileSize;
289289
int numTilesYHR = (hdCamera.actualHeight + (areaTileSize - 1)) / areaTileSize;
290290

291-
// Clear the integration texture first
292-
cmd.SetComputeTextureParam(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, HDShaderIDs._RaytracedShadowIntegration, diffuseBufferRT);
293-
cmd.DispatchCompute(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, numTilesXHR, numTilesYHR, hdCamera.viewCount);
294-
295291
// Fetch the volume overrides that we shall be using
296292
RayTracingShader subSurfaceShader = m_Asset.renderPipelineRayTracingResources.subSurfaceRayTracing;
297293
RayTracingSettings rayTracingSettings = hdCamera.volumeStack.GetComponent<RayTracingSettings>();
@@ -302,8 +298,13 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c
302298
RTHandle intermediateBuffer1 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA1);
303299
RTHandle intermediateBuffer2 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA2);
304300
RTHandle intermediateBuffer3 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA3);
301+
RTHandle intermediateBuffer4 = GetRayTracingBuffer(InternalRayTracingBuffers.RGBA4);
305302
RTHandle directionBuffer = GetRayTracingBuffer(InternalRayTracingBuffers.Direction);
306303

304+
// Clear the integration texture first
305+
cmd.SetComputeTextureParam(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, HDShaderIDs._RaytracedShadowIntegration, intermediateBuffer4);
306+
cmd.DispatchCompute(m_ScreenSpaceShadowsCS, m_ClearShadowTexture, numTilesXHR, numTilesYHR, hdCamera.viewCount);
307+
307308
// Grab the acceleration structure for the target camera
308309
RayTracingAccelerationStructure accelerationStructure = RequestAccelerationStructure();
309310

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

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

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

381382
// Apply temporal filtering to the buffer
382383
HDTemporalFilter temporalFilter = GetTemporalFilter();
383-
temporalFilter.DenoiseBuffer(cmd, hdCamera, diffuseBufferRT, subsurfaceHistory, intermediateBuffer0, singleChannel: false, historyValidity: historyValidity);
384+
temporalFilter.DenoiseBuffer(cmd, hdCamera, intermediateBuffer4, subsurfaceHistory, intermediateBuffer0, singleChannel: false, historyValidity: historyValidity);
385+
386+
// Now based on the mask, we need to blend the subsurface and the diffuse lighting
387+
ComputeShader rayTracingSubSurfaceCS = m_Asset.renderPipelineRayTracingResources.subSurfaceRayTracingCS;
388+
int m_CombineSubSurfaceKernel = rayTracingSubSurfaceCS.FindKernel("BlendSubSurfaceData");
389+
cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._SubSurfaceLightingBuffer, intermediateBuffer0);
390+
cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._DiffuseLightingTextureRW, diffuseBufferRT);
391+
cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._SSSBufferTexture, m_SSSColor);
392+
cmd.DispatchCompute(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, numTilesXHR, numTilesYHR, hdCamera.viewCount);
384393

385394
// Push this version of the texture for debug
386-
PushFullScreenDebugTexture(hdCamera, cmd, intermediateBuffer0, FullScreenDebugMode.RayTracedSubSurface);
395+
PushFullScreenDebugTexture(hdCamera, cmd, diffuseBufferRT, FullScreenDebugMode.RayTracedSubSurface);
387396

388397
// Combine it with the rest of the lighting
389-
m_CombineLightingPass.SetTexture(HDShaderIDs._IrradianceSource, intermediateBuffer0);
398+
m_CombineLightingPass.SetTexture(HDShaderIDs._IrradianceSource, diffuseBufferRT );
390399
HDUtils.DrawFullScreen(cmd, m_CombineLightingPass, colorBufferRT, depthStencilBufferRT, shaderPassId: 1);
391400
}
392401
}

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRayTracingResources.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ partial class HDRenderPipelineRayTracingResources : ScriptableObject
4848
// Sub-Surface Scattering
4949
[Reload("Runtime/RenderPipeline/Raytracing/Shaders/RayTracingSubSurface.raytrace")]
5050
public RayTracingShader subSurfaceRayTracing;
51+
[Reload("Runtime/RenderPipeline/Raytracing/Shaders/SubSurface/RayTracingSubSurface.compute")]
52+
public ComputeShader subSurfaceRayTracingCS;
5153

5254
// Denoising
5355
[Reload("Runtime/RenderPipeline/Raytracing/Shaders/Denoising/TemporalFilter.compute")]

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ static class HDShaderIDs
601601
public static readonly int _DirectionTextureRW = Shader.PropertyToID("_DirectionTextureRW");
602602
public static readonly int _PositionTextureRW = Shader.PropertyToID("_PositionTextureRW");
603603
public static readonly int _DiffuseLightingTextureRW = Shader.PropertyToID("_DiffuseLightingTextureRW");
604+
public static readonly int _SubSurfaceLightingBuffer = Shader.PropertyToID("_SubSurfaceLightingBuffer");
604605

605606
// Accumulation
606607
public static readonly int _AccumulationFrameIndex = Shader.PropertyToID("_AccumulationFrameIndex");

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingManager.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ internal enum InternalRayTracingBuffers
3939
RGBA0,
4040
RGBA1,
4141
RGBA2,
42-
RGBA3
42+
RGBA3,
43+
RGBA4
4344
}
4445

4546
class HDRayTracingLights
@@ -107,6 +108,7 @@ public partial class HDRenderPipeline
107108
RTHandle m_RayTracingIntermediateBufferRGBA1;
108109
RTHandle m_RayTracingIntermediateBufferRGBA2;
109110
RTHandle m_RayTracingIntermediateBufferRGBA3;
111+
RTHandle m_RayTracingIntermediateBufferRGBA4;
110112

111113
internal void InitRayTracingManager()
112114
{
@@ -142,6 +144,8 @@ internal void ReleaseRayTracingManager()
142144
RTHandles.Release(m_RayTracingIntermediateBufferRGBA2);
143145
if (m_RayTracingIntermediateBufferRGBA3 != null)
144146
RTHandles.Release(m_RayTracingIntermediateBufferRGBA3);
147+
if (m_RayTracingIntermediateBufferRGBA4 != null)
148+
RTHandles.Release(m_RayTracingIntermediateBufferRGBA4);
145149

146150
if (m_RayTracingLightCluster != null)
147151
m_RayTracingLightCluster.ReleaseResources();
@@ -740,6 +744,11 @@ internal RTHandle AllocateBuffer(InternalRayTracingBuffers bufferID)
740744
m_RayTracingIntermediateBufferRGBA3 = RTHandles.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, enableRandomWrite: true, useDynamicScale: true, useMipMap: false, name: "RayTracingIntermediateBufferRGBA3");
741745
return m_RayTracingIntermediateBufferRGBA3;
742746
}
747+
case InternalRayTracingBuffers.RGBA4:
748+
{
749+
m_RayTracingIntermediateBufferRGBA4 = RTHandles.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, enableRandomWrite: true, useDynamicScale: true, useMipMap: false, name: "RayTracingIntermediateBufferRGBA4");
750+
return m_RayTracingIntermediateBufferRGBA4;
751+
}
743752
default:
744753
return null;
745754
}
@@ -769,6 +778,8 @@ internal RTHandle GetRayTracingBuffer(InternalRayTracingBuffers bufferID)
769778
return m_RayTracingIntermediateBufferRGBA2 != null ? m_RayTracingIntermediateBufferRGBA2 : AllocateBuffer(InternalRayTracingBuffers.RGBA2);
770779
case InternalRayTracingBuffers.RGBA3:
771780
return m_RayTracingIntermediateBufferRGBA3 != null ? m_RayTracingIntermediateBufferRGBA3 : AllocateBuffer(InternalRayTracingBuffers.RGBA3);
781+
case InternalRayTracingBuffers.RGBA4:
782+
return m_RayTracingIntermediateBufferRGBA4 != null ? m_RayTracingIntermediateBufferRGBA4 : AllocateBuffer(InternalRayTracingBuffers.RGBA4);
772783
default:
773784
return null;
774785
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma kernel BlendSubSurfaceData
2+
3+
#pragma only_renderers d3d11
4+
5+
// Given that this pass does not use the shadow algorithm multi-compile, we need to define SHADOW_LOW to quite the shadow algorithm error
6+
#define SHADOW_LOW
7+
8+
// HDRP generic includes
9+
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl"
10+
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
11+
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
12+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
13+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl"
14+
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/Sampling.hlsl"
15+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/Lighting.hlsl"
16+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
17+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl"
18+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl"
19+
20+
// The buffer of sub surface scattering that was computed used ray tracing
21+
TEXTURE2D_X(_SubSurfaceLightingBuffer);
22+
// This buffer holds the diffuse lighting without the SSS contribution
23+
RW_TEXTURE2D_X(float4, _DiffuseLightingTextureRW);
24+
25+
#define RAYTRACING_SUB_SURFACE_TILE_SIZE 8
26+
27+
[numthreads(RAYTRACING_SUB_SURFACE_TILE_SIZE, RAYTRACING_SUB_SURFACE_TILE_SIZE, 1)]
28+
void BlendSubSurfaceData(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
29+
{
30+
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
31+
32+
// Compute the pixel position to process
33+
uint2 currentCoord = groupId * RAYTRACING_SUB_SURFACE_TILE_SIZE + groupThreadId;
34+
35+
// Read the SSS Data
36+
SSSData sssData;
37+
DECODE_FROM_SSSBUFFER(currentCoord, sssData);
38+
39+
// Compute the albedo color to use based on the scattering mode
40+
int profileIndex = sssData.diffusionProfileIndex;
41+
uint texturingMode = GetSubsurfaceScatteringTexturingMode(profileIndex);
42+
float3 albedo = ApplySubsurfaceScatteringTexturingMode(texturingMode, sssData.diffuseColor);
43+
44+
// Blend and we are done
45+
_DiffuseLightingTextureRW[COORD_TEXTURE2D_X(currentCoord)] = float4(lerp(_DiffuseLightingTextureRW[COORD_TEXTURE2D_X(currentCoord)].xyz * albedo * GetInverseCurrentExposureMultiplier()
46+
, _SubSurfaceLightingBuffer[COORD_TEXTURE2D_X(currentCoord)].xyz
47+
, sssData.subsurfaceMask), 1.0);
48+
}

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/SubSurface/RayTracingSubSurface.compute.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.render-pipelines.high-definition/Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ MonoBehaviour:
3838
type: 3}
3939
subSurfaceRayTracing: {fileID: 4807578003741378534, guid: b29a18f967c92364492508dddf78cff7,
4040
type: 3}
41+
subSurfaceRayTracingCS: {fileID: 7200000, guid: 4e5684a8dba46fe42a47642f9b0a6b89,
42+
type: 3}
4143
temporalFilterCS: {fileID: 7200000, guid: 741979ff70f7bd6489fbcb464280ecff, type: 3}
4244
simpleDenoiserCS: {fileID: 7200000, guid: 74a980f1da9a4f842996035350fe756c, type: 3}
4345
diffuseDenoiserCS: {fileID: 7200000, guid: b4ed2382141619f40af1f743a84ccaea, type: 3}

0 commit comments

Comments
 (0)