Skip to content

Commit

Permalink
feat: #86 move tone mapping to post process shader
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 22, 2020
1 parent 5de3fa0 commit 3636063
Show file tree
Hide file tree
Showing 24 changed files with 193 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ out vec4 FragColor;
void main(void)
{
vec4 result = texture(uTexDiffuse, varTexCoord0.xy) * varColor;
FragColor = vec4(linearRGB(sRGB(result.rgb)), result.a);
FragColor = vec4(sRGB(result.rgb), result.a);
}
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ vec3 linearRGB(vec3 color)
void main(void)
{
vec4 result = texture(uTexDiffuse, varTexCoord0.xy) * varColor;
FragColor = vec4(linearRGB(sRGB(result.rgb)), result.a);
FragColor = vec4(sRGB(result.rgb), result.a);
}
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.d.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ struct PS_INPUT
float4 main(PS_INPUT input) : SV_TARGET
{
float4 result = input.color * uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
return float4(linearRGB(sRGB(result.rgb)), result.a);
return float4(sRGB(result.rgb), result.a);
}
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ float3 linearRGB(float3 color)
float4 main(PS_INPUT input) : SV_TARGET
{
float4 result = input.color * uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
return float4(linearRGB(sRGB(result.rgb)), result.a);
return float4(sRGB(result.rgb), result.a);
}
15 changes: 15 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.d.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
precision mediump float;

uniform sampler2D uTexDiffuse;

in vec2 varTexCoord0;
in vec4 varColor;
out vec4 FragColor;

#include "LibToneMapping.glsl"

void main(void)
{
float4 color = texture(uTexDiffuse, varTexCoord0.xy);
FragColor = float4(linearRGB(color.rgb), color.a);
}
20 changes: 20 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
precision mediump float;
uniform sampler2D uTexDiffuse;
in vec2 varTexCoord0;
in vec4 varColor;
out vec4 FragColor;
const float gamma = 2.2;
const float invGamma = 1.0/2.2;
vec3 sRGB(vec3 color)
{
return pow(color, vec3(gamma));
}
vec3 linearRGB(vec3 color)
{
return pow(color, vec3(invGamma));
}
void main(void)
{
float4 color = texture(uTexDiffuse, varTexCoord0.xy);
FragColor = float4(linearRGB(color.rgb), color.a);
}
16 changes: 16 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectVS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
in vec4 inPosition;
in vec4 inColor;
in vec3 inNormal;
in vec2 inTexCoord0;

uniform mat4 uMvpMatrix;

out vec2 varTexCoord0;
out vec4 varColor;

void main(void)
{
varTexCoord0 = inTexCoord0;
varColor = inColor/255.0;
gl_Position = uMvpMatrix * inPosition;
}
17 changes: 17 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.d.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Texture2D uTexDiffuse : register(t0);
SamplerState uTexDiffuseSampler : register(s0);

struct PS_INPUT
{
float4 pos : SV_POSITION;
float4 color : COLOR0;
float2 tex0 : TEXCOORD0;
};

#include "LibToneMapping.hlsl"

float4 main(PS_INPUT input) : SV_TARGET
{
float4 color = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
return float4(linearRGB(color.rgb), color.a);
}
23 changes: 23 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Texture2D uTexDiffuse : register(t0);
SamplerState uTexDiffuseSampler : register(s0);
struct PS_INPUT
{
float4 pos : SV_POSITION;
float4 color : COLOR0;
float2 tex0 : TEXCOORD0;
};
static const float gamma = 2.2;
static const float invGamma = 1.0/2.2;
float3 sRGB(float3 color)
{
return pow(color, gamma);
}
float3 linearRGB(float3 color)
{
return pow(color, invGamma);
}
float4 main(PS_INPUT input) : SV_TARGET
{
float4 color = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
return float4(linearRGB(color.rgb), color.a);
}
31 changes: 31 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectVS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
struct VS_INPUT
{
float4 pos: POSITION;
float3 norm: NORMAL;
float4 color: COLOR;
float2 tex0: TEXCOORD0;
};

struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float4 color : COLOR0;
float2 tex0 : TEXCOORD0;
};

// adding constant buffer for transform matrices
cbuffer cbPerObject
{
float4x4 uMvpMatrix;
};

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;

output.pos = mul(input.pos, uMvpMatrix);
output.color = input.color;
output.tex0 = input.tex0;

return output;
}
14 changes: 14 additions & 0 deletions Assets/BuiltIn/Shader/PostProcessing/PostEffect.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<shaderConfig name="PostEffect" baseShader="SOLID">
<uniforms>
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
</vs>
<fs>
<uniform name="uTexDiffuse" type="DEFAULT_VALUE" value="0" float="1" directX="false"/>
</fs>
</uniforms>
<customUI>
</customUI>
<shader type="GLSL" vs="GLSL/PostEffectVS.glsl" fs="GLSL/PostEffectFS.glsl"/>
<shader type="HLSL" vs="HLSL/PostEffectVS.hlsl" fs="HLSL/PostEffectFS.hlsl"/>
</shaderConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ void main(void)
vec3 reflection = -normalize(reflect(vWorldViewDir, n));
color += textureLod(uTexReflect, reflection, roughness * 8.0).xyz * specularColor * metallic;

FragColor = vec4(linearRGB(color), diffuseMap.a);
FragColor = vec4(color, diffuseMap.a);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ void main(void)
color += ambientLighting * diffuseColor / PI;
vec3 reflection = -normalize(reflect(vWorldViewDir, n));
color += textureLod(uTexReflect, reflection, roughness * 8.0).xyz * specularColor * metallic;
FragColor = vec4(linearRGB(color), diffuseMap.a);
FragColor = vec4(color, diffuseMap.a);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ float4 main(PS_INPUT input) : SV_TARGET
float3 reflection = -normalize(reflect(input.worldViewDir, n));
color += sRGB(uTexReflect.SampleLevel(uTexReflectSampler, reflection, roughness * 8).xyz) * specularColor * metallic;

return float4(linearRGB(color), diffuseMap.a);
return float4(color, diffuseMap.a);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ float4 main(PS_INPUT input) : SV_TARGET
color += ambientLighting * diffuseColor / PI;
float3 reflection = -normalize(reflect(input.worldViewDir, n));
color += sRGB(uTexReflect.SampleLevel(uTexReflectSampler, reflection, roughness * 8).xyz) * specularColor * metallic;
return float4(linearRGB(color), diffuseMap.a);
return float4(color, diffuseMap.a);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ vec3 SG(
// IBL reflection
// ...

return linearRGB(color);
return color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ vec3 SG(
vec3 directionalLight = NdotL * directionLightColor * visibility;
vec3 color = (directionalLight + pointLightColor) * diffuseColor * directMultiplier + specular * specularColor * visibility + light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return linearRGB(color);
return color;
}
void main(void)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ vec3 SG(
vec3 directionalLight = NdotL * directionLightColor * visibility;
vec3 color = (directionalLight + pointLightColor) * diffuseColor * directMultiplier + specular * specularColor * visibility + light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return linearRGB(color);
return color;
}
void main(void)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ float3 SG(
// IBL reflection
// ...

return linearRGB(color);
return color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ float3 SG(
float3 directionalLight = NdotL * directionLightColor * visibility;
float3 color = (directionalLight + pointLightColor) * diffuseColor * directMultiplier + specular * specularColor * visibility + light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return linearRGB(color);
return color;
}
float4 main(PS_INPUT input) : SV_TARGET
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ float3 SG(
float3 directionalLight = NdotL * directionLightColor * visibility;
float3 color = (directionalLight + pointLightColor) * diffuseColor * directMultiplier + specular * specularColor * visibility + light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return linearRGB(color);
return color;
}
float4 main(PS_INPUT input) : SV_TARGET
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace Skylicht
loadShader("BuiltIn/Shader/Lightmap/LightmapUV.xml");
loadShader("BuiltIn/Shader/Lightmap/IndirectTest.xml");
loadShader("BuiltIn/Shader/Lightmap/LightmapVertex.xml");

loadShader("BuiltIn/Shader/PostProcessing/PostEffect.xml");
}

void CShaderManager::initSGDeferredShader()
Expand All @@ -107,7 +109,7 @@ namespace Skylicht
loadShader("BuiltIn/Shader/SpecularGlossiness/Lighting/SGDirectionalLightBake.xml");
loadShader("BuiltIn/Shader/SpecularGlossiness/Lighting/SGPointLight.xml");
loadShader("BuiltIn/Shader/SpecularGlossiness/Lighting/SGPointLightShadow.xml");
loadShader("BuiltIn/Shader/SpecularGlossiness/Forward/SH.xml");
loadShader("BuiltIn/Shader/SpecularGlossiness/Forward/SH.xml");
}

void CShaderManager::initSkylichtEngineShader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ This file is part of the "Skylicht Engine".

namespace Skylicht
{
CPostProcessorRP::CPostProcessorRP()
CPostProcessorRP::CPostProcessorRP() :
m_currentLum(NULL),
m_lastLum(NULL)
{

}
Expand All @@ -47,13 +49,16 @@ namespace Skylicht

// init size of framebuffer
m_size = core::dimension2du((u32)w, (u32)h);
m_lumSize = core::dimension2du(1024, 1024);

core::dimension2du lumSize(1024, 1024);
m_luminance[0] = driver->addRenderTargetTexture(lumSize, "lum_0", ECF_R16F);
m_luminance[1] = driver->addRenderTargetTexture(lumSize, "lum_1", ECF_R16F);
m_luminance[0] = driver->addRenderTargetTexture(m_lumSize, "lum_0", ECF_R16F);
m_luminance[1] = driver->addRenderTargetTexture(m_lumSize, "lum_1", ECF_R16F);

// init final pass shader
m_finalPass.MaterialType = shaderMgr->getShaderIDByName("TextureColor");
m_finalPass.MaterialType = shaderMgr->getShaderIDByName("PostEffect");

// init lum pass shader
m_lumPass.MaterialType = shaderMgr->getShaderIDByName("Luminance");
}

void CPostProcessorRP::render(ITexture *target, CCamera *camera, CEntityManager *entityManager, const core::recti& viewport)
Expand All @@ -66,7 +71,27 @@ namespace Skylicht

void CPostProcessorRP::luminanceMapGeneration(ITexture *color)
{
if (m_lastLum == m_luminance[0])
{
m_currentLum = m_luminance[1];
m_lastLum = m_luminance[0];
}
else
{
m_currentLum = m_luminance[0];
m_lastLum = m_luminance[1];
}

IVideoDriver * driver = getVideoDriver();
driver->setRenderTarget(m_currentLum, true, true);

m_lumPass.setTexture(0, color);

float w = (float)m_lumSize.Width;
float h = (float)m_lumSize.Height;

beginRender2D(w, h);
renderBufferToTarget(0.0f, 0.0f, w, h, m_finalPass);
}

void CPostProcessorRP::postProcessing(ITexture *finalTarget, ITexture *color, ITexture *normal, ITexture *position, const core::recti& viewport)
Expand All @@ -75,6 +100,8 @@ namespace Skylicht

luminanceMapGeneration(color);

m_currentLum->regenerateMipMapLevels();

driver->setRenderTarget(finalTarget, false, false);

float renderW = (float)m_size.Width;
Expand All @@ -90,8 +117,12 @@ namespace Skylicht
m_finalPass.setTexture(0, color);
m_finalPass.setTexture(1, normal);
m_finalPass.setTexture(2, position);
m_finalPass.setTexture(3, m_currentLum);
m_finalPass.setTexture(4, m_lastLum);

beginRender2D(renderW, renderH);
renderBufferToTarget(0.0f, 0.0f, renderW, renderH, m_finalPass);

m_lastLum = m_currentLum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ namespace Skylicht
{
protected:
core::dimension2du m_size;
core::dimension2du m_lumSize;

ITexture *m_luminance[2];
ITexture *m_lastLum;
ITexture *m_currentLum;

SMaterial m_finalPass;
SMaterial m_lumPass;

public:
CPostProcessorRP();
Expand Down

0 comments on commit 3636063

Please sign in to comment.