浏览代码

Prepare NormalBuffer support in forward

/main
sebastienlagarde 6 年前
当前提交
5fd64011
共有 6 个文件被更改,包括 117 次插入30 次删除
  1. 46
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.hlsl
  2. 6
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader
  3. 24
      com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.hlsl
  4. 4
      com.unity.render-pipelines.high-definition/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.hlsl
  5. 15
      com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassDepthOnly.hlsl
  6. 52
      com.unity.render-pipelines.high-definition/HDRP/Material/NormalBuffer.hlsl

46
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.hlsl


#define CLEAR_COAT_IOR 1.5
#define CLEAR_COAT_IETA (1.0 / CLEAR_COAT_IOR) // IETA is the inverse eta which is the ratio of IOR of two interface
#define CLEAR_COAT_F0 0.04 // IORToFresnel0(CLEAR_COAT_IOR)
#define CLEAR_COAT_ROUGHNESS 0.001
#define CLEAR_COAT_ROUGHNESS 0.01
#define CLEAR_COAT_PERCEPTUAL_ROUGHNESS RoughnessToPerceptualRoughness(CLEAR_COAT_ROUGHNESS)
//-----------------------------------------------------------------------------

return sssData;
}
NormalData ConvertSurfaceDataToNormalData(SurfaceData surfaceData)
{
NormalData normalData;
// When using clear cloat we want to use the coat normal for the various deferred effect
// as it is the most dominant one
if (HasFeatureFlag(surfaceData.materialFeatures, MATERIALFEATUREFLAGS_LIT_CLEAR_COAT))
{
normalData.normalWS = surfaceData.coatNormalWS;
normalData.perceptualSmoothness = CLEAR_COAT_ROUGHNESS;
}
else
{
normalData.normalWS = surfaceData.normalWS;
normalData.perceptualSmoothness = surfaceData.perceptualSmoothness;
}
return normalData;
}
//-----------------------------------------------------------------------------
// conversion function for forward
//-----------------------------------------------------------------------------

// Warning: the contents are later overwritten for Standard and SSS!
outGBuffer0 = float4(surfaceData.baseColor, surfaceData.specularOcclusion);
// The sign of the Z component of the normal MUST round-trip through the G-Buffer, otherwise
// the reconstruction of the tangent frame for anisotropic GGX creates a seam along the Z axis.
// The constant was eye-balled to not cause artifacts.
// TODO: find a proper solution. E.g. we could re-shuffle the faces of the octahedron
// s.t. the sign of the Z component round-trips.
const float seamThreshold = 1.0/1024.0;
surfaceData.normalWS.z = CopySign(max(seamThreshold, abs(surfaceData.normalWS.z)), surfaceData.normalWS.z);
// RT1 - 8:8:8:8
// Our tangent encoding is based on our normal.
float2 octNormalWS = PackNormalOctQuadEncode(surfaceData.normalWS);
float3 packNormalWS = PackFloat2To888(saturate(octNormalWS * 0.5 + 0.5));
// We store perceptualRoughness instead of roughness because it is perceptually linear.
outGBuffer1 = float4(packNormalWS, PerceptualSmoothnessToPerceptualRoughness(surfaceData.perceptualSmoothness));
// This encode normalWS and PerceptualSmoothness into GBuffer1
EncodeIntoNormalBuffer(ConvertSurfaceDataToNormalData(surfaceData), positionSS, outGBuffer1);
// RT2 - 8:8:8:8
uint materialFeatureId;

// Decompress feature-agnostic data from the G-Buffer.
float3 baseColor = inGBuffer0.rgb;
float3 packNormalWS = inGBuffer1.rgb;
float2 octNormalWS = Unpack888ToFloat2(packNormalWS);
bsdfData.normalWS = UnpackNormalOctQuadEncode(octNormalWS * 2.0 - 1.0);
bsdfData.perceptualRoughness = inGBuffer1.a;
NormalData normalData;
DecodeFromNormalBuffer(inGBuffer1, positionSS, normalData);
bsdfData.normalWS = normalData.normalWS;
bsdfData.perceptualRoughness = normalData.perceptualRoughness;
bakeDiffuseLighting = inGBuffer3.rgb;

6
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader


ZWrite On
ColorMask 0
//ColorMask 0
HLSLPROGRAM

#include "../../ShaderPass/ShaderPassDepthOnly.hlsl"
ENDHLSL
// In deferred, depth only pass don't output anything.
// In forward it output the normal buffer
#pragma multi_compile _ OUTPUT_NORMAL_BUFFER
}
Pass

24
com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.hlsl


// Definition
//-----------------------------------------------------------------------------
// Required for SSS
#define GBufferType0 float4
// Needed for MATERIAL_FEATURE_MASK_FLAGS.
#include "../../Lighting/LightLoop/LightLoop.cs.hlsl"

sssData.diffusionProfile = surfaceData.diffusionProfile;
return sssData;
}
NormalData ConvertSurfaceDataToNormalData(SurfaceData surfaceData)
{
NormalData normalData;
// When using clear cloat we want to use the coat normal for the various deferred effect
// as it is the most dominant one
if (HasFeatureFlag(surfaceData.materialFeatures, MATERIALFEATUREFLAGS_STACK_LIT_COAT))
{
normalData.normalWS = surfaceData.coatNormalWS;
normalData.perceptualSmoothness = surfaceData.coatPerceptualSmoothness;
}
else
{
normalData.normalWS = surfaceData.normalWS;
// Do average mix in case of dual lobe
normalData.perceptualSmoothness = lerp(surfaceData.perceptualSmoothnessA, surfaceData.perceptualSmoothnessB, surfaceData.lobeMix);
}
return normalData;
}
//-----------------------------------------------------------------------------

4
com.unity.render-pipelines.high-definition/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.hlsl


uint diffusionProfile;
};
#define SSSBufferType0 float4
#define SSSBufferType0 float4 // Must match GBufferType0 in deferred
// SSSBuffer texture declaration
TEXTURE2D(_SSSBufferTexture0);

}
// OUTPUT_SSSBUFFER start from SV_Target2 as SV_Target0 and SV_Target1 are used for lighting buffer
#define OUTPUT_SSSBUFFER(NAME) out GBufferType0 MERGE_NAME(NAME, 0) : SV_Target2
#define OUTPUT_SSSBUFFER(NAME) out SSSBufferType0 MERGE_NAME(NAME, 0) : SV_Target2
#define ENCODE_INTO_SSSBUFFER(SURFACE_DATA, UNPOSITIONSS, NAME) EncodeIntoSSSBuffer(ConvertSurfaceDataToSSSData(SURFACE_DATA), UNPOSITIONSS, MERGE_NAME(NAME, 0))
#define DECODE_FROM_SSSBUFFER(UNPOSITIONSS, SSS_DATA) DecodeFromSSSBuffer(UNPOSITIONSS, SSS_DATA)

15
com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassDepthOnly.hlsl


#endif // TESSELLATION_ON
void Frag( PackedVaryingsToPS packedInput,
#ifdef OUTPUT_NORMAL_BUFFER
OUTPUT_NORMALBUFFER(outNormalBuffer)
#else
#endif
#ifdef _DEPTHOFFSET_ON
, out float outputDepth : SV_Depth
#endif

BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
// TODO: handle cubemap shadow
outColor = float4(0.0, 0.0, 0.0, 0.0);
#ifdef OUTPUT_NORMAL_BUFFER
ENCODE_INTO_NORMALBUFFER(surfaceData, posInput.positionSS, outNormalBuffer);
#elif defined(SCENESELECTIONPASS)
#ifdef SCENESELECTIONPASS
outColor = float4(_ObjectId, _PassValue, 1, 1);
outColor = float4(_ObjectId, _PassValue, 1.0, 1.0);
#else
outColor = float4(0.0, 0.0, 0.0, 0.0);
#endif
}

52
com.unity.render-pipelines.high-definition/HDRP/Material/NormalBuffer.hlsl


// ----------------------------------------------------------------------------
// Encoding/decoding normal buffer functions
// ----------------------------------------------------------------------------
struct NormalData
{
float3 normalWS;
float perceptualSmoothness;
};
#define NormalBufferType0 float4 // Must match GBufferType1 in deferred
// SSSBuffer texture declaration
TEXTURE2D(_NormalBufferTexture0);
void EncodeIntoNormalBuffer(NormalData normalData, uint2 positionSS, out NormalBufferType0 outNormalBuffer0)
{
// The sign of the Z component of the normal MUST round-trip through the G-Buffer, otherwise
// the reconstruction of the tangent frame for anisotropic GGX creates a seam along the Z axis.
// The constant was eye-balled to not cause artifacts.
// TODO: find a proper solution. E.g. we could re-shuffle the faces of the octahedron
// s.t. the sign of the Z component round-trips.
const float seamThreshold = 1.0 / 1024.0;
normalData.normalWS.z = CopySign(max(seamThreshold, abs(normalData.normalWS.z)), normalData.normalWS.z);
// RT1 - 8:8:8:8
// Our tangent encoding is based on our normal.
float2 octNormalWS = PackNormalOctQuadEncode(normalData.normalWS);
float3 packNormalWS = PackFloat2To888(saturate(octNormalWS * 0.5 + 0.5));
// We store perceptualRoughness instead of roughness because it is perceptually linear.
outNormalBuffer0 = float4(packNormalWS, PerceptualSmoothnessToPerceptualRoughness(normalData.perceptualSmoothness));
}
void DecodeFromNormalBuffer(float4 normalBuffer, uint2 positionSS, out NormalData normalData)
{
float3 packNormalWS = normalBuffer.rgb;
float2 octNormalWS = Unpack888ToFloat2(packNormalWS);
normalData.normalWS = UnpackNormalOctQuadEncode(octNormalWS * 2.0 - 1.0);
normalData.perceptualRoughness = normalBuffer.a;
}
void DecodeFromNormalBuffer(uint2 positionSS, out NormalData normalData)
{
float4 normalBuffer = LOAD_TEXTURE2D(_NormalBufferTexture0, positionSS);
DecodeFromNormalBuffer(normalBuffer, positionSS, normalData);
}
// OUTPUT_NORMAL_NORMALBUFFER start from SV_Target0 as it is used during depth prepass where there is no color buffer
#define OUTPUT_NORMAL_NORMALBUFFER(NAME) out NormalBufferType0 MERGE_NAME(NAME, 0) : SV_Target0
#define ENCODE_INTO_NORMALBUFFER(SURFACE_DATA, UNPOSITIONSS, NAME) EncodeIntoNormalBuffer(ConvertSurfaceDataToNormalData(SURFACE_DATA), UNPOSITIONSS, MERGE_NAME(NAME, 0))
#define DECODE_FROM_NORMALBUFFER(UNPOSITIONSS, NORMAL_DATA) DecodeFromNormalBuffer(UNPOSITIONSS, NORMAL_DATA)
正在加载...
取消
保存