Skip to content

Commit

Permalink
#86 Implement SSR (HLSL)
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 15, 2020
1 parent 3da23d1 commit e7cfb87
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 11 deletions.
80 changes: 80 additions & 0 deletions Assets/BuiltIn/Shader/SSR/HLSL/LibSSR.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#define LENGTH 20.0

float3 SSR(const float3 position, const float3 reflection, const float roughness)
{
float4 projectedCoord;

float3 beginPosition;
float3 endPosition;

float3 rayPosition = position;
float4 viewPosition;

float3 dir = reflection * LENGTH;

float mipLevel = roughness * 7.0;

// base color
// convert 3d position to 2d texture coord
viewPosition = mul(float4(position, 1.0), uView);
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection);
projectedCoord.xy = projectedCoord.xy / projectedCoord.w;
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5);

// default reflect color is from position
float3 baseColor = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb;

// ray test
for (int i = 6; i >= 0; --i)
{
// begin ray
beginPosition = rayPosition;

// end ray
endPosition = rayPosition + dir;

// mid ray
rayPosition += dir * 0.5;

// convert 3d position to 2d texture coord
viewPosition = mul(float4(rayPosition, 1.0), uView);
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection);
projectedCoord.xy = projectedCoord.xy / projectedCoord.w;
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5);

float3 testPosition = uTexPosition.Sample(uTexPositionSampler, projectedCoord.xy).xyz;

float3 d1 = testPosition - beginPosition;
float lengthSQ1 = d1.x*d1.x + d1.y*d1.y + d1.z*d1.z;

float3 d2 = testPosition - endPosition;
float lengthSQ2 = d2.x*d2.x + d2.y*d2.y + d2.z*d2.z;

// beginPosition is nearer
if (lengthSQ1 < lengthSQ2)
{
rayPosition = beginPosition;
}

// binary search test
dir *= 0.5;
}

float z = mul(float4(reflection, 0.0), uView).z;
z = clamp(z, 0.0, 1.0);

// convert 3d position to 2d texture coord
viewPosition = mul(float4(rayPosition, 1.0), uView);
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection);
projectedCoord.xy = projectedCoord.xy / projectedCoord.w;
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5);

// default reflect color is from position
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb;

// edge factor
float2 dCoords = smoothstep(float2(0.0, 0.0), float2(0.5, 0.5), abs(float2(0.5, 0.5) - projectedCoord.xy));
float screenEdgefactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0);

return lerp(baseColor * 0.7, color, screenEdgefactor * z);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
static const float PI = 3.1415926;

#include "LibSolverMetallic.hlsl"
#if defined(ENABLE_SSR)
#include "../../../SSR/HLSL/LibSSR.hlsl"
#endif
#include "../../../PostProcessing/HLSL/LibToneMapping.hlsl"

float3 SG(
const float3 baseColor,
const float spec,
const float gloss,
const float3 position,
const float3 worldViewDir,
const float3 worldLightDir,
const float3 worldNormal,
Expand Down Expand Up @@ -55,7 +59,10 @@ float3 SG(
color += indirectColor * diffuseColor * indirectMultiplier / PI;

// IBL reflection
// ...
#if defined(ENABLE_SSR)
float3 reflection = -normalize(reflect(worldViewDir, worldNormal));
color = color + sRGB(SSR(position, reflection, roughness)) * metallic * specularColor;
#endif

return color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ float4 main(PS_INPUT input) : SV_TARGET
albedo,
data.r,
data.g,
position,
viewDir,
uLightDirection.xyz,
normal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ float3 SG(
const float3 baseColor,
const float spec,
const float gloss,
const float3 position,
const float3 worldViewDir,
const float3 worldLightDir,
const float3 worldNormal,
Expand Down Expand Up @@ -160,6 +161,7 @@ float4 main(PS_INPUT input) : SV_TARGET
albedo,
data.r,
data.g,
position,
viewDir,
uLightDirection.xyz,
normal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ cbuffer cbPerFrame
float3 uLightMultiplier;
float3 uShadowDistance;
float4x4 uShadowMatrix[3];
float4x4 uProjection;
float4x4 uView;
};

#define ENABLE_SSR

#include "LibShadow.hlsl"
#include "LibSG.hlsl"

Expand Down Expand Up @@ -72,6 +76,7 @@ float4 main(PS_INPUT input) : SV_TARGET
albedo,
data.r,
data.g,
position,
viewDir,
uLightDirection.xyz,
normal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ cbuffer cbPerFrame
float3 uLightMultiplier;
float3 uShadowDistance;
float4x4 uShadowMatrix[3];
float4x4 uProjection;
float4x4 uView;
};
float texture2DCompare(float3 uv, float compare) {
float depth = uShadowMap.SampleLevel(uShadowMapSampler, uv, 0).r;
Expand Down Expand Up @@ -80,6 +82,51 @@ float solveMetallic(float3 diffuse, float3 specular, float oneMinusSpecularStren
float D = b * b - 4.0 * a * c;
return clamp((-b + sqrt(D)) / (2.0 * a), 0.0, 1.0);
}
float3 SSR(const float3 position, const float3 reflection, const float roughness)
{
float4 projectedCoord;
float3 beginPosition;
float3 endPosition;
float3 rayPosition = position;
float4 viewPosition;
float3 dir = reflection * 20.0;
float mipLevel = roughness * 7.0;
viewPosition = mul(float4(position, 1.0), uView);
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection);
projectedCoord.xy = projectedCoord.xy / projectedCoord.w;
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5);
float3 baseColor = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb;
for (int i = 6; i >= 0; --i)
{
beginPosition = rayPosition;
endPosition = rayPosition + dir;
rayPosition += dir * 0.5;
viewPosition = mul(float4(rayPosition, 1.0), uView);
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection);
projectedCoord.xy = projectedCoord.xy / projectedCoord.w;
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5);
float3 testPosition = uTexPosition.Sample(uTexPositionSampler, projectedCoord.xy).xyz;
float3 d1 = testPosition - beginPosition;
float lengthSQ1 = d1.x*d1.x + d1.y*d1.y + d1.z*d1.z;
float3 d2 = testPosition - endPosition;
float lengthSQ2 = d2.x*d2.x + d2.y*d2.y + d2.z*d2.z;
if (lengthSQ1 < lengthSQ2)
{
rayPosition = beginPosition;
}
dir *= 0.5;
}
float z = mul(float4(reflection, 0.0), uView).z;
z = clamp(z, 0.0, 1.0);
viewPosition = mul(float4(rayPosition, 1.0), uView);
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection);
projectedCoord.xy = projectedCoord.xy / projectedCoord.w;
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5);
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb;
float2 dCoords = smoothstep(float2(0.0, 0.0), float2(0.5, 0.5), abs(float2(0.5, 0.5) - projectedCoord.xy));
float screenEdgefactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0);
return lerp(baseColor * 0.7, color, screenEdgefactor * z);
}
static const float gamma = 2.2;
static const float invGamma = 1.0 / 2.2;
float3 sRGB(float3 color)
Expand All @@ -94,6 +141,7 @@ float3 SG(
const float3 baseColor,
const float spec,
const float gloss,
const float3 position,
const float3 worldViewDir,
const float3 worldLightDir,
const float3 worldNormal,
Expand Down Expand Up @@ -126,6 +174,8 @@ float3 SG(
float3 directionalLight = NdotL * directionLightColor * visibility;
float3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor + specular * specularColor * visibility + light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
float3 reflection = -normalize(reflect(worldViewDir, worldNormal));
color = color + sRGB(SSR(position, reflection, roughness)) * metallic * specularColor;
return color;
}
float4 main(PS_INPUT input) : SV_TARGET
Expand All @@ -152,6 +202,7 @@ float4 main(PS_INPUT input) : SV_TARGET
albedo,
data.r,
data.g,
position,
viewDir,
uLightDirection.xyz,
normal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<uniform name="uLightMultiplier" type="SHADER_VEC3" value="0.0" float="3"/>
<uniform name="uShadowDistance" type="SHADOW_MAP_DISTANCE" value="0" float="3"/>
<uniform name="uShadowMatrix" type="SHADOW_MAP_MATRIX" value="0" float="16" array="3" matrix="true"/>
<uniform name="uProjection" type="DEFERRED_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
</fs>
</uniforms>
<resources>
Expand Down
12 changes: 3 additions & 9 deletions Projects/Skylicht/Engine/Source/Material/Shader/CShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This file is part of the "Skylicht Engine".
#include "ShaderCallback/CShaderShadow.h"
#include "ShaderCallback/CShaderSH.h"
#include "ShaderCallback/CShaderParticle.h"
#include "ShaderCallback/CShaderDeferred.h"

namespace Skylicht
{
Expand All @@ -50,6 +51,7 @@ namespace Skylicht
addCallback<CShaderShadow>();
addCallback<CShaderSH>();
addCallback<CShaderParticle>();
addCallback<CShaderDeferred>();
}

CShader::~CShader()
Expand Down Expand Up @@ -1052,21 +1054,13 @@ namespace Skylicht
{
}
break;
case DEFERRED_VIEW:
{
}
break;
case DEFERRED_PROJECTION:
{
}
break;
*/
case CUSTOM_VALUE:
{
// todo
// set params in callback
}
break;
*/
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
!@
MIT License
Copyright (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CShaderDeferred.h"

namespace Skylicht
{
core::matrix4 CShaderDeferred::s_projection;
core::matrix4 CShaderDeferred::s_view;

CShaderDeferred::CShaderDeferred()
{

}

CShaderDeferred::~CShaderDeferred()
{

}

void CShaderDeferred::OnSetConstants(CShader *shader, SUniform *uniform, IMaterialRenderer* matRender, bool vertexShader)
{
switch (uniform->Type)
{
case DEFERRED_VIEW:
{
if (vertexShader == true)
matRender->setShaderVariable(uniform->UniformShaderID, s_view.pointer(), uniform->SizeOfUniform, video::EST_VERTEX_SHADER);
else
matRender->setShaderVariable(uniform->UniformShaderID, s_view.pointer(), uniform->SizeOfUniform, video::EST_PIXEL_SHADER);
}
break;
case DEFERRED_PROJECTION:
{
if (vertexShader == true)
matRender->setShaderVariable(uniform->UniformShaderID, s_projection.pointer(), uniform->SizeOfUniform, video::EST_VERTEX_SHADER);
else
matRender->setShaderVariable(uniform->UniformShaderID, s_projection.pointer(), uniform->SizeOfUniform, video::EST_PIXEL_SHADER);
}
break;
default:
break;
}
}
}
Loading

0 comments on commit e7cfb87

Please sign in to comment.