Skip to content

Commit

Permalink
#149 Implement depth from Deferred (try improve SSR)
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jun 3, 2022
1 parent 5fdcb36 commit 859696d
Show file tree
Hide file tree
Showing 30 changed files with 74 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/SSR/GLSL/LibSSR.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ vec3 SSR(const vec3 baseColor, const vec3 position, const vec3 reflection, const
z = clamp(z, 0.0, 1.0);

// convert 3d position to 2d texture coord
vec3 color = textureLod(uTexLastFrame, projectedCoord.xy, mipLevel).rgb;
vec3 color = textureLod(uTexLastFrame, projectedCoord.xy, 0).rgb;

// edge factor
vec2 dCoords = smoothstep(vec2(0.0, 0.0), vec2(0.5, 0.5), abs(vec2(0.5, 0.5) - projectedCoord.xy));
Expand Down
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/SSR/HLSL/LibSSR.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ float3 SSR(const float3 baseColor, const float3 position, const float3 reflectio
z = clamp(z, 0.0, 1.0);

// convert 3d position to 2d texture coord
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb;
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, 0).rgb;

// edge factor
float2 dCoords = smoothstep(float2(0.0, 0.0), float2(0.5, 0.5), abs(float2(0.5, 0.5) - projectedCoord.xy));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="MATERIAL_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="MATERIAL_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="MATERIAL_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform float uSpec;
uniform float uGloss;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in float vTangentW;

Expand All @@ -17,7 +17,7 @@ layout(location = 3) out vec4 SG;
void main(void)
{
Diffuse = uColor;
Position = vec4(vWorldPosition, 1.0);
Position = vWorldPosition;
Normal = vec4(vWorldNormal, 1.0);
SG = vec4(uSpec, uGloss, 1.0, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ uniform float uSpec;
uniform float uGloss;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in float vTangentW;

Expand All @@ -20,7 +20,7 @@ void main(void)
vec3 baseMap = texture(uTexDiffuse, vTexCoord0.xy).xyz;

Diffuse = vec4(baseMap, 1.0);
Position = vec4(vWorldPosition, 1.0);
Position = vWorldPosition;
Normal = vec4(vWorldNormal, 1.0);
SG = vec4(uSpec, uGloss, 1.0, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uniform float uSpec;
uniform float uGloss;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in vec3 vWorldTangent;
in vec3 vWorldBinormal;
Expand All @@ -30,7 +30,7 @@ void main(void)
n = normalize(n);

Diffuse = vec4(baseMap, 1.0);
Position = vec4(vWorldPosition, 1.0);
Position = vWorldPosition;
Normal = vec4(n, 1.0);
SG = vec4(uSpec, uGloss, 1.0, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ in vec2 inTexCoord0;

uniform mat4 uMvpMatrix;
uniform mat4 uWorldMatrix;
uniform mat4 uView;
uniform vec4 uUVScale;

out vec3 vWorldNormal;
out vec3 vWorldPosition;
out vec4 vWorldPosition;

out vec2 vTexCoord0;

void main(void)
{
vWorldPosition = (uWorldMatrix*inPosition).xyz;
vWorldPosition = uWorldMatrix*inPosition;

vec4 sampleFragPos = uView * vWorldPosition;
vWorldPosition.w = sampleFragPos.z;

vec4 worldNormal = uWorldMatrix * vec4(inNormal, 0.0);
vWorldNormal = normalize(worldNormal.xyz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uniform sampler2D uTexSpec;
uniform float uGloss;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in vec3 vWorldTangent;
in vec3 vWorldBinormal;
Expand All @@ -31,7 +31,7 @@ void main(void)
n = normalize(n);

Diffuse = vec4(baseMap, 1.0);
Position = vec4(vWorldPosition, 1.0);
Position = vWorldPosition;
Normal = vec4(n, 1.0);
SG = vec4(specMap.r, uGloss, 1.0, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform sampler2D uTexNormal;
uniform sampler2D uTexSpecGloss;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in vec3 vWorldTangent;
in vec3 vWorldBinormal;
Expand All @@ -29,7 +29,7 @@ void main(void)
n = normalize(n);

Diffuse = vec4(baseMap, 1.0);
Position = vec4(vWorldPosition, 1.0);
Position = vWorldPosition;
Normal = vec4(n, 1.0);
SG = vec4(sgMap, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ uniform sampler2D uTexMask;
uniform float uCutoff;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in vec3 vWorldTangent;
in vec3 vWorldBinormal;
Expand Down Expand Up @@ -36,7 +36,7 @@ void main(void)
n = normalize(n);

Diffuse = vec4(baseMap, 1.0);
Position = vec4(vWorldPosition, 1.0);
Position = vWorldPosition;
Normal = vec4(n, 1.0);
SG = vec4(sgMap.r, max(sgMap.g, 0.01), sgMap.b, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ in vec2 inTangentW;

uniform mat4 uMvpMatrix;
uniform mat4 uWorldMatrix;
uniform mat4 uView;
uniform vec4 uUVScale;

out vec3 vWorldNormal;
out vec3 vWorldPosition;
out vec4 vWorldPosition;
out vec3 vWorldTangent;
out vec3 vWorldBinormal;

Expand All @@ -20,7 +21,10 @@ out float vTangentW;

void main(void)
{
vWorldPosition = (uWorldMatrix*inPosition).xyz;
vWorldPosition = uWorldMatrix*inPosition;

vec4 sampleFragPos = uView * vWorldPosition;
vWorldPosition.w = sampleFragPos.z;

vec4 worldNormal = uWorldMatrix * vec4(inNormal, 0.0);
vec4 worldTangent = uWorldMatrix * vec4(inTangent, 0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
};

Expand All @@ -26,7 +26,7 @@ PS_OUTPUT main(PS_INPUT input)
PS_OUTPUT output;

output.Diffuse = uColor;
output.Position = float4(input.worldPosition, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(input.worldNormal, 1.0);
output.SG = float4(uSpec, uGloss, 0.0, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
};

Expand All @@ -30,7 +30,7 @@ PS_OUTPUT main(PS_INPUT input)
float3 baseMap = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0).xyz;

output.Diffuse = float4(baseMap, 1.0);
output.Position = float4(input.worldPosition, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(input.worldNormal, 1.0);
output.SG = float4(uSpec, uGloss, 0.0, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
Expand Down Expand Up @@ -44,7 +44,7 @@ PS_OUTPUT main(PS_INPUT input)
n = normalize(n);

output.Diffuse = float4(baseMap, 1.0);
output.Position = float4(input.worldPosition, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(n, 1.0);
output.SG = float4(uSpec, uGloss, 0.0, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
};

cbuffer cbPerObject
{
float4x4 uMvpMatrix;
float4x4 uWorldMatrix;
float4x4 uView;
float4 uUVScale;
};

Expand All @@ -30,7 +31,10 @@ VS_OUTPUT main(VS_INPUT input)
float4 worldPos = mul(input.pos, uWorldMatrix);
float4 worldNormal = mul(float4(input.norm, 0.0), uWorldMatrix);

output.worldPosition = worldPos.xyz;
float4 sampleFragPos = mul(worldPos, uView);
float sampleDepth = sampleFragPos.z;

output.worldPosition = float4(worldPos.xyz, sampleDepth);
output.worldNormal = normalize(worldNormal.xyz);

return output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
Expand Down Expand Up @@ -47,7 +47,7 @@ PS_OUTPUT main(PS_INPUT input)
n = normalize(n);

output.Diffuse = float4(baseMap, 1.0);
output.Position = float4(input.worldPosition, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(n, 1.0);
output.SG = float4(specMap.r, uGloss, 0.0, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
Expand Down Expand Up @@ -42,7 +42,7 @@ PS_OUTPUT main(PS_INPUT input)
n = normalize(n);

output.Diffuse = float4(baseMap, 1.0);
output.Position = float4(input.worldPosition, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(n, 1.0);
output.SG = float4(sgMap, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
Expand Down Expand Up @@ -54,7 +54,7 @@ PS_OUTPUT main(PS_INPUT input)
n = normalize(n);

output.Diffuse = float4(baseMap, 1.0);
output.Position = float4(input.worldPosition, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(n, 1.0);
output.SG = float4(sgMap.r, max(sgMap.g, 0.01), sgMap.b, 1.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
Expand All @@ -24,6 +24,7 @@ cbuffer cbPerObject
{
float4x4 uMvpMatrix;
float4x4 uWorldMatrix;
float4x4 uView;
float4 uUVScale;
};

Expand All @@ -38,7 +39,10 @@ VS_OUTPUT main(VS_INPUT input)
float4 worldNormal = mul(float4(input.norm, 0.0), uWorldMatrix);
float4 worldTangent = mul(float4(input.tangent, 0.0), uWorldMatrix);

output.worldPosition = worldPos.xyz;
float4 sampleFragPos = mul(worldPos, uView);
float sampleDepth = sampleFragPos.z;

output.worldPosition = float4(worldPos.xyz, sampleDepth);
output.worldNormal = normalize(worldNormal.xyz);
output.worldTangent = normalize(worldTangent.xyz);
output.worldBinormal = normalize(cross(worldNormal.xyz, worldTangent.xyz));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="MATERIAL_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="MATERIAL_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="MATERIAL_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ vec3 SG(
void main(void)
{
vec3 albedo = texture(uTexAlbedo, varTexCoord0.xy).rgb;
vec3 position = texture(uTexPosition, varTexCoord0.xy).xyz;
vec4 posdepth = texture(uTexPosition, varTexCoord0.xy);
vec3 position = posdepth.xyz;
vec3 normal = texture(uTexNormal, varTexCoord0.xy).xyz;
vec3 data = texture(uTexData, varTexCoord0.xy).rgb;
vec4 light = texture(uTexLight, varTexCoord0.xy);
Expand Down
Loading

0 comments on commit 859696d

Please sign in to comment.