diff --git a/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.d.glsl b/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.d.glsl index d7b276ed4..59af255bb 100644 --- a/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.d.glsl +++ b/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.d.glsl @@ -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); } \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.glsl b/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.glsl index ca3de198c..830df1031 100644 --- a/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.glsl +++ b/Assets/BuiltIn/Shader/Basic/GLSL/TextureToneFS.glsl @@ -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); } diff --git a/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.d.hlsl b/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.d.hlsl index cad17df51..87af0200f 100644 --- a/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.d.hlsl +++ b/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.d.hlsl @@ -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); } \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.hlsl b/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.hlsl index cabf56fd7..54b3d2fae 100644 --- a/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.hlsl +++ b/Assets/BuiltIn/Shader/Basic/HLSL/TextureToneFS.hlsl @@ -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); } diff --git a/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.d.glsl b/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.d.glsl new file mode 100644 index 000000000..eda426f5e --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.d.glsl @@ -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); +} \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.glsl b/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.glsl new file mode 100644 index 000000000..ad736ebf4 --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.glsl @@ -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); +} diff --git a/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectVS.glsl b/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectVS.glsl new file mode 100644 index 000000000..988621004 --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectVS.glsl @@ -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; +} \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.d.hlsl b/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.d.hlsl new file mode 100644 index 000000000..f6c35545f --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.d.hlsl @@ -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); +} \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.hlsl b/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.hlsl new file mode 100644 index 000000000..749a1edec --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.hlsl @@ -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); +} diff --git a/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectVS.hlsl b/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectVS.hlsl new file mode 100644 index 000000000..ae93243b8 --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectVS.hlsl @@ -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; +} \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/PostProcessing/PostEffect.xml b/Assets/BuiltIn/Shader/PostProcessing/PostEffect.xml new file mode 100644 index 000000000..0b956a3a9 --- /dev/null +++ b/Assets/BuiltIn/Shader/PostProcessing/PostEffect.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.d.glsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.d.glsl index 44b9a8060..10de0a6ab 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.d.glsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.d.glsl @@ -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); } \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.glsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.glsl index 5568bee45..96fa42a6e 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.glsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/GLSL/SGLitFS.glsl @@ -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); } diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.d.hlsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.d.hlsl index 282db8391..bfb9394a6 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.d.hlsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.d.hlsl @@ -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); } \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.hlsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.hlsl index a84bd1dcd..00cc4f4be 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.hlsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Forward/HLSL/SGLitFS.hlsl @@ -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); } diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/LibSG.glsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/LibSG.glsl index 84bae0d72..e0e10a56a 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/LibSG.glsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/LibSG.glsl @@ -56,5 +56,5 @@ vec3 SG( // IBL reflection // ... - return linearRGB(color); + return color; } diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightBakeFS.glsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightBakeFS.glsl index bad526c48..4f9e4d012 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightBakeFS.glsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightBakeFS.glsl @@ -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) { diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightFS.glsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightFS.glsl index 11733820d..b613636bd 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightFS.glsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/GLSL/SGDirectionalLightFS.glsl @@ -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) { diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/LibSG.hlsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/LibSG.hlsl index 7ea2e3678..f0798918a 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/LibSG.hlsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/LibSG.hlsl @@ -56,5 +56,5 @@ float3 SG( // IBL reflection // ... - return linearRGB(color); + return color; } \ No newline at end of file diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightBakeFS.hlsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightBakeFS.hlsl index 365f30bdc..3f30a8199 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightBakeFS.hlsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightBakeFS.hlsl @@ -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 { diff --git a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightFS.hlsl b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightFS.hlsl index ae64ce8f4..b47ecd0ae 100644 --- a/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightFS.hlsl +++ b/Assets/BuiltIn/Shader/SpecularGlossiness/Lighting/HLSL/SGDirectionalLightFS.hlsl @@ -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 { diff --git a/Projects/Skylicht/Engine/Source/Material/Shader/CShaderManager.cpp b/Projects/Skylicht/Engine/Source/Material/Shader/CShaderManager.cpp index 256548591..2f4eeb991 100644 --- a/Projects/Skylicht/Engine/Source/Material/Shader/CShaderManager.cpp +++ b/Projects/Skylicht/Engine/Source/Material/Shader/CShaderManager.cpp @@ -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() @@ -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() diff --git a/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.cpp b/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.cpp index 1fda50117..ee5120fab 100644 --- a/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.cpp +++ b/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.cpp @@ -28,7 +28,9 @@ This file is part of the "Skylicht Engine". namespace Skylicht { - CPostProcessorRP::CPostProcessorRP() + CPostProcessorRP::CPostProcessorRP() : + m_currentLum(NULL), + m_lastLum(NULL) { } @@ -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) @@ -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) @@ -75,6 +100,8 @@ namespace Skylicht luminanceMapGeneration(color); + m_currentLum->regenerateMipMapLevels(); + driver->setRenderTarget(finalTarget, false, false); float renderW = (float)m_size.Width; @@ -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; } } \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.h b/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.h index 177ae4b9e..123da0521 100644 --- a/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.h +++ b/Projects/Skylicht/Engine/Source/RenderPipeline/CPostProcessorRP.h @@ -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();