Skip to content

Commit

Permalink
[Shader]Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
haolange committed Jan 6, 2021
1 parent 2fda521 commit f5b7086
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 220 deletions.
4 changes: 2 additions & 2 deletions Runtime/RenderFeature/PyramidColorGenerator.meta
Git LFS file not shown
4 changes: 2 additions & 2 deletions Runtime/RenderFeature/PyramidDepthGenerator.meta
Git LFS file not shown
4 changes: 2 additions & 2 deletions Runtime/RenderFeature/ScreenSpaceGlobalIllumination.meta
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ void Raytracing_ScreenTrace (uint3 id : SV_DispatchThreadID)
UAV_ScreenIrradiance[id.xy] = 0;
return;
}
float3 ScreenPos = GetScreenSpacePos(UV, SceneDepth);
float3 WorldPos = GetWorldSpacePos(ScreenPos, Matrix_InvViewProj);
float3 ViewPos = GetViewSpacePos(ScreenPos, Matrix_InvProj);
float3 NDCPos = GetNDCPos(UV, SceneDepth);
float3 WorldPos = GetWorldSpacePos(NDCPos, Matrix_InvViewProj);
float3 ViewPos = GetViewSpacePos(NDCPos, Matrix_InvProj);
float3 ViewDir = GetViewDir(WorldPos, ViewPos);
float3 WorldNormal = SRV_GBufferNormal.SampleLevel(Global_point_clamp_sampler, UV, 0).xyz * 2 - 1;
float3x3 TangentToWorld = GetTangentBasis(WorldNormal);
Expand All @@ -123,20 +123,22 @@ void Raytracing_ScreenTrace (uint3 id : SV_DispatchThreadID)
float Out_Occlusion = 0;
float4 Out_ColorMask = 0;

float3 BentNormal = 0;
//float3 BentNormal = 0;

[loop]
for (uint i = 0; i < (uint)SSGi_NumRays; i++)
{
//float2 Hash = Halton(i, Random);
float2 Hash = Hammersley16(i, (uint)SSGi_NumRays, Random);
float3 LightDir_TS = CosineSampleHemisphere(Hash).xyz;
float3 LightDir_WS = mul(LightDir_TS, TangentToWorld);
float3 LightDir_VS = mul((float3x3)(Matrix_WorldToView), LightDir_WS);
//BentNormal += LightDir_WS;

float Level;
float3 HitUVz;

float3 RayStartScreen = ScreenPos;
float3 RayStartScreen = NDCPos;
float4 RayEndScreen = mul( Matrix_Proj, float4(LightDir_VS, 0) ) + float4(RayStartScreen, 1);
RayEndScreen.xyz = (RayEndScreen.xyz / max(RayEndScreen.w, 1e-4));
float3 RayDepthScreen = 0.5 * (RayStartScreen + mul( Matrix_Proj, float4(0, 0, 1, 0) ).xyz);
Expand All @@ -158,23 +160,23 @@ void Raytracing_ScreenTrace (uint3 id : SV_DispatchThreadID)
float3 SampleColor = SRV_PyramidColor.SampleLevel(Global_point_clamp_sampler, HitUVz.xy, 0).rgb;
SampleColor.rgb *= rcp( 1 + Luminance(SampleColor.rgb) );
Out_ColorMask += float4(SampleColor.rgb, GetScreenFadeBord(HitUVz.xy, 0.1));
} else {
BentNormal += LightDir_WS;

//BentNormal = 0;
}
}

/*Out_Occlusion /= SSGi_NumRays;
Out_Occlusion /= SSGi_NumRays;
Out_ColorMask /= SSGi_NumRays;
Out_ColorMask.rgb *= rcp( 1 - Luminance(Out_ColorMask.rgb) ) * (1 - Out_Occlusion) * SSGi_Intensity;
UAV_ScreenIrradiance[id.xy] = Out_ColorMask * Out_ColorMask.a;*/
UAV_ScreenIrradiance[id.xy] = Out_ColorMask * Out_ColorMask.a;



BentNormal = normalize(BentNormal) * 2 - 1;
float3 ReflectVector = -reflect(WorldNormal, normalize(WorldPos - _WorldSpaceCameraPos));
float ReflecOcclusion = ReflectionOcclusion(BentNormal, ReflectVector, 0.25, 0.5);
//BentNormal = normalize(BentNormal);
//float3 ReflectVector = -reflect(WorldNormal, normalize(WorldPos - _WorldSpaceCameraPos));
//float ReflecOcclusion = ReflectionOcclusion(BentNormal, ReflectVector, 0.25, 0.5);

//UAV_ScreenIrradiance[id.xy] = ReflecOcclusion;
//UAV_ScreenIrradiance[id.xy] = float4(BentNormal, 1);
UAV_ScreenIrradiance[id.xy] = dot(BentNormal, _WorldSpaceLightPos0.xyz);
//UAV_ScreenIrradiance[id.xy] = dot(BentNormal, _WorldSpaceLightPos0.xyz);
}
4 changes: 2 additions & 2 deletions Runtime/RenderFeature/ScreenSpaceReflection.meta
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void Raytracing_ScreenTrace (uint3 id : SV_DispatchThreadID)
UAV_ReflectionColorMask[id.xy] = 0;
return;
}
float3 ScreenPos = GetScreenSpacePos(UV, SceneDepth);
float3 WorldPos = GetWorldSpacePos(ScreenPos, Matrix_InvViewProj);
float3 ViewPos = GetViewSpacePos(ScreenPos, Matrix_InvProj);
float3 NDCPos = GetNDCPos(UV, SceneDepth);
float3 WorldPos = GetWorldSpacePos(NDCPos, Matrix_InvViewProj);
float3 ViewPos = GetViewSpacePos(NDCPos, Matrix_InvProj);
float3 ViewDir = GetViewDir(WorldPos, ViewPos);
float3 WorldNormal = SRV_GBufferNormal.SampleLevel(Global_point_clamp_sampler, UV, 0).xyz * 2 - 1;
float3 ViewNormal = mul( (float3x3)Matrix_WorldToView, WorldNormal );
Expand All @@ -132,7 +132,7 @@ void Raytracing_ScreenTrace (uint3 id : SV_DispatchThreadID)
float Level;
float3 HitUVz;

float3 RayStartScreen = ScreenPos;
float3 RayStartScreen = NDCPos;
float4 RayEndScreen = mul( Matrix_Proj, float4(LightDir_VS, 0) ) + float4(RayStartScreen, 1);
RayEndScreen.xyz = (RayEndScreen.xyz / max(RayEndScreen.w, 1e-4));
float3 RayDepthScreen = 0.5 * (RayStartScreen + mul( Matrix_Proj, float4(0, 0, 1, 0) ).xyz);
Expand Down
4 changes: 2 additions & 2 deletions Runtime/RenderFeature/SpatialTemporalVarianceFilter.meta
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ void SpatialFilter (uint3 id : SV_DispatchThreadID)
float3 WorldNormal = SRV_GBufferNormal.SampleLevel(Global_point_clamp_sampler, UV, 0).xyz * 2 - 1;
float Roughness = clamp( 1 - SRV_GBufferRoughness.SampleLevel(Global_point_clamp_sampler, UV, 0).a, 0.02, 1 );
float3 ViewNormal = mul( (float3x3)Matrix_WorldToView, WorldNormal );
float3 ScreenSpacePos = GetScreenSpacePos(UV, SceneDepth);
float3 WorldSpacePos = GetWorldSpacePos(ScreenSpacePos, Matrix_InvViewProj);
float3 ViewSpacePos = GetViewSpacePos(ScreenSpacePos, Matrix_InvProj);
float3 NDCPos = GetNDCPos(UV, SceneDepth);
float3 WorldSpacePos = GetWorldSpacePos(NDCPos, Matrix_InvViewProj);
float3 ViewSpacePos = GetViewSpacePos(NDCPos, Matrix_InvProj);

float NumWeight;
float4 ReflecttionColor;
Expand All @@ -206,7 +206,7 @@ void SpatialFilter (uint3 id : SV_DispatchThreadID)

float4 HitUWVPDF = SRV_UWVPDF.SampleLevel(Global_bilinear_repeat_sampler, Offset_UV, 0);
float4 HitColorMask = SRV_ColorMask.SampleLevel(Global_bilinear_repeat_sampler, Offset_UV, 0);
float3 Hit_ViewSpacePos = GetViewSpacePos( GetScreenSpacePos(HitUWVPDF.xy, HitUWVPDF.z), Matrix_InvProj );
float3 Hit_ViewSpacePos = GetViewSpacePos( GetNDCPos(HitUWVPDF.xy, HitUWVPDF.z), Matrix_InvProj );

///Variance Accmulation
MomentA += HitColorMask.rgb;
Expand Down
2 changes: 1 addition & 1 deletion Shaders/BuiltIn/SSRCompositing.shader
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
float4 SpecularColor = tex2D(_CameraGBufferTexture1, UV);
float Roughness = clamp(1 - SpecularColor.a, 0.02, 1);

float3 ScreenPos = GetScreenSpacePos(UV, SceneDepth);
float3 ScreenPos = GetNDCPos(UV, SceneDepth);
float3 WorldPos = GetWorldSpacePos(ScreenPos, _Matrix_InvViewProj);
float3 ViewDir = GetViewDir(WorldPos, _WorldSpaceCameraPos);

Expand Down
174 changes: 34 additions & 140 deletions Shaders/Private/Common.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _BaseCommon_
#define _BaseCommon_


//#define float half
//#define float2 half2
//#define float3 half3
Expand Down Expand Up @@ -440,7 +441,7 @@ float Luma4(float3 Color)
return (Color.g * 2) + (Color.r + Color.b);
}

float Luminance(float3 rgb)
float CLuminance(float3 rgb)
{
return dot( rgb, float3(0.0396819152, 0.458021790, 0.00609653955) );
}
Expand All @@ -455,7 +456,7 @@ float HdrWeightY(float Color, float Exposure)
return rcp(Color * Exposure + 4);
}

float3 RGBToYCoCg(float3 RGB)
/*float3 RGBToYCoCg(float3 RGB)
{
float Y = dot(RGB, float3(1, 2, 1));
float Co = dot(RGB, float3(2, 0, -2));
Expand All @@ -475,6 +476,22 @@ float3 YCoCgToRGB(float3 YCoCg)
float G = Y + Cg;
float B = Y - Co - Cg;
return float3(R, G, B);
}*/

float3 RGBToYCbCr(float3 RGB) {
float Y = (0.299 * RGB.r) + (0.587 * RGB.g) + (0.114 * RGB.b);
float Cb = (RGB.b - Y) * 0.565;
float Cr = (RGB.r - Y) * 0.713;

return float3(Y, Cb, Cr);
}

float3 YCbCrToRGB(float3 YCbCr) {
float R = YCbCr.x + (1.403 * YCbCr.z);
float G = YCbCr.x - (0.344 * YCbCr.y) - (0.714 * YCbCr.z);
float B = YCbCr.x + (1.770 * YCbCr.y);

return float3(R, G, B);
}

Expand Down Expand Up @@ -533,20 +550,20 @@ float3 GetViewSpaceNormal(float3 normal, float4x4 _WToCMatrix)
return normalize(viewNormal);
}

float3 GetScreenSpacePos(float2 uv, float depth)
float3 GetNDCPos(float2 uv, float depth)
{
return float3(uv * 2 - 1, depth);
}

float3 GetWorldSpacePos(float3 ScreenPos, float4x4 Matrix_InvViewProj)
float3 GetWorldSpacePos(float3 NDCPos, float4x4 Matrix_InvViewProj)
{
float4 worldPos = mul( Matrix_InvViewProj, float4(ScreenPos, 1) );
float4 worldPos = mul( Matrix_InvViewProj, float4(NDCPos, 1) );
return worldPos.xyz / worldPos.w;
}

float3 GetViewSpacePos(float3 ScreenPos, float4x4 Matrix_InvProj)
float3 GetViewSpacePos(float3 NDCPos, float4x4 Matrix_InvProj)
{
float4 viewPos = mul(Matrix_InvProj, float4(ScreenPos, 1));
float4 viewPos = mul(Matrix_InvProj, float4(NDCPos, 1));
return viewPos.xyz / viewPos.w;
}

Expand All @@ -555,143 +572,20 @@ float3 GetViewDir(float3 worldPos, float3 ViewPos)
return normalize(worldPos - ViewPos);
}

float2 GetMotionVector(float SceneDepth, float2 inUV, float4x4 Matrix_InvViewProj, float4x4 _PrevViewProjectionMatrix, float4x4 _ViewProjectionMatrix)
{
float3 ScreenPos = GetScreenSpacePos(inUV, SceneDepth);
float4 worldPos = float4(GetWorldSpacePos(ScreenPos, Matrix_InvViewProj), 1);

float4 prevClipPos = mul(_PrevViewProjectionMatrix, worldPos);
float4 curClipPos = mul(_ViewProjectionMatrix, worldPos);

float2 prevHPos = prevClipPos.xy / prevClipPos.w;
float2 curHPos = curClipPos.xy / curClipPos.w;

float2 vPosPrev = (prevHPos.xy + 1) / 2;
float2 vPosCur = (curHPos.xy + 1) / 2;
return vPosCur - vPosPrev;
}

float2 UnitVectorToOctahedron( float3 N )
{
N.xy /= dot( 1, abs(N) );
if( N.z <= 0 ) {
N.xy = ( 1 - abs(N.yx) ) * ( N.xy >= 0 ? float2(1, 1) : float2(-1, -1) );
}
return N.xy;
}

float3 OctahedronToUnitVector( float2 Oct )
{
float3 N = float3( Oct, 1 - dot( 1, abs(Oct) ) );
if( N.z < 0 ) {
N.xy = ( 1 - abs(N.yx) ) * ( N.xy >= 0 ? float2(1, 1) : float2(-1, -1) );
}
return normalize(N);
}

float2 UnitVectorToHemiOctahedron( float3 N )
{
N.xy /= dot( 1, abs(N) );
return float2( N.x + N.y, N.x - N.y );
}

float3 HemiOctahedronToUnitVector( float2 Oct )
{
Oct = float2( Oct.x + Oct.y, Oct.x - Oct.y ) * 0.5;
float3 N = float3( Oct, 1 - dot( 1, abs(Oct) ) );
return normalize(N);
}

float3 Pack1212To888( float2 x )
float2 GetMotionVector(float SceneDepth, float2 inUV, float4x4 Matrix_InvViewProj, float4x4 Matrix_LastViewProj, float4x4 Matrix_ViewProj)
{
// Pack 12:12 to 8:8:8
#if 0
uint2 x1212 = (uint2)( x * 4095.0 );
uint2 High = x1212 >> 8;
uint2 Low = x1212 & 255;
uint3 x888 = uint3( Low, High.x | (High.y << 4) );
return x888 / 255.0;
#else
float2 x1212 = floor( x * 4095 );
float2 High = floor( x1212 / 256 ); // x1212 >> 8
float2 Low = x1212 - High * 256; // x1212 & 255
float3 x888 = float3( Low, High.x + High.y * 16 );
return saturate( x888 / 255 );
#endif
}
float3 NDCPos = GetNDCPos(inUV, SceneDepth);
float4 worldPos = float4(GetWorldSpacePos(NDCPos, Matrix_InvViewProj), 1);

float2 Pack888To1212( float3 x )
{
// Pack 8:8:8 to 12:12
#if 0
uint3 x888 = (uint3)( x * 255.0 );
uint High = x888.z >> 4;
uint Low = x888.z & 15;
uint2 x1212 = x888.xy | uint2( Low << 8, High << 8 );
return x1212 / 4095.0;
#else
float3 x888 = floor( x * 255 );
float High = floor( x888.z / 16 ); // x888.z >> 4
float Low = x888.z - High * 16; // x888.z & 15
float2 x1212 = x888.xy + float2( Low, High ) * 256;
return saturate( x1212 / 4095 );
#endif
}
float4 LastClipPos = mul(Matrix_LastViewProj, worldPos);
float4 CurClipPos = mul(Matrix_ViewProj, worldPos);

struct ThinGBufferData
{
float Roughness;
float Reflactance;
float3 WorldNormal;
float3 AlbedoColor;
};

float3 EncodeNormalDir( float3 N )
{
return Pack1212To888( UnitVectorToOctahedron( N ) * 0.5 + 0.5 );
}
float2 LastNDC = LastClipPos.xy / LastClipPos.w;
float2 CurNDC = CurClipPos.xy / CurClipPos.w;

float3 DecodeNormalDir( float3 N )
{
return OctahedronToUnitVector( Pack888To1212( N ) * 2 - 1 );
}

void EncodeIntGBuffer(ThinGBufferData GBufferData, out int EncodeData_GBufferA, out int EncodeData_GBufferB)
{
int2 EncodeNormal = int2(saturate( UnitVectorToOctahedron(GBufferData.WorldNormal) * 0.5 + 0.5) * 0xFFF);
int EncodeRoughness = int(saturate(GBufferData.Roughness) * 0xFF);
int3 EncodeAlbedo = int3(saturate(GBufferData.AlbedoColor) * 0xFF);
int EncodeReflactance = int(saturate(GBufferData.Reflactance) * 0xFF);

EncodeData_GBufferA = (EncodeNormal.x << 20) + (EncodeNormal.y << 8) + EncodeRoughness;
EncodeData_GBufferB = (EncodeAlbedo.x << 24) + (EncodeAlbedo.y << 16) + (EncodeAlbedo.z << 8) + EncodeReflactance;
}

void DecodeIntGBuffer(int EncodeData_GBufferA, int EncodeData_GBufferB, out ThinGBufferData GBufferData)
{
GBufferData.WorldNormal = OctahedronToUnitVector( (int2(EncodeData_GBufferA >> 20, EncodeData_GBufferA >> 8) & 0xFFF) / float(0xFFF) * 2 - 1);
GBufferData.Roughness = ((EncodeData_GBufferA >> 32) & 0xFF) / 255.0f;
GBufferData.AlbedoColor = (int3(EncodeData_GBufferB >> 24, EncodeData_GBufferB >> 16, EncodeData_GBufferB >> 8) & 0xFF) / 255.0f;
GBufferData.Reflactance = (EncodeData_GBufferB >> 24 & 0xFF) / 255.0f;
}

void EncodeFloatGBuffer(ThinGBufferData GBufferData, out float4 EncodeData_GBufferA, out float4 EncodeData_GBufferB)
{
float3 EncodeNormal = EncodeNormalDir(GBufferData.WorldNormal);
float EncodeRoughness = GBufferData.Roughness;
float3 EncodeAlbedo = GBufferData.AlbedoColor;
float EncodeReflactance = GBufferData.Reflactance;

EncodeData_GBufferA = float4(EncodeNormal, EncodeRoughness);
EncodeData_GBufferB = float4(EncodeAlbedo, EncodeReflactance);
}

void DecodeFloatGBuffer(float4 EncodeData_GBufferA, float4 EncodeData_GBufferB, out ThinGBufferData GBufferData)
{
GBufferData.WorldNormal = DecodeNormalDir(EncodeData_GBufferA.xyz);
GBufferData.Roughness = EncodeData_GBufferA.a;
GBufferData.AlbedoColor = EncodeData_GBufferB.rgb;
GBufferData.Reflactance = EncodeData_GBufferB.a;
float2 LastUV = LastNDC.xy * 0.5 + 0.5;
float2 CurUV = CurNDC.xy * 0.5 + 0.5;
return CurUV - LastUV;
}

#endif
Loading

0 comments on commit f5b7086

Please sign in to comment.