Skip to content

Commit

Permalink
feat: #86 Fix for opengl-es 3.0 shader, add test Luminance
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 22, 2020
1 parent 3636063 commit 6d0fd1d
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 30 deletions.
3 changes: 0 additions & 3 deletions Assets/BuiltIn/Shader/Basic/Luminance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
</fs>
</uniforms>
<customUI>
<ui control="UIGroup" name="Texture">
<ui control="UITexture" name="uTexDiffuse" autoReplace="_diff.tga"/>
</ui>
</customUI>
<shader type="GLSL" vs="GLSL/TextureColorVS.glsl" fs="GLSL/LuminanceFS.glsl"/>
<shader type="HLSL" vs="HLSL/TextureColorVS.hlsl" fs="HLSL/LuminanceFS.hlsl"/>
Expand Down
2 changes: 2 additions & 0 deletions Assets/BuiltIn/Shader/Lightmap/GLSL/IndirectTestFS.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
precision mediump float;
precision highp sampler2D;
precision highp sampler2DArray;

uniform sampler2DArray uTexLightmap;

Expand Down
2 changes: 2 additions & 0 deletions Assets/BuiltIn/Shader/Lightmap/GLSL/LightmapFS.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
precision mediump float;
precision highp sampler2D;
precision highp sampler2DArray;

uniform sampler2DArray uTexLightmap;

Expand Down
2 changes: 2 additions & 0 deletions Assets/BuiltIn/Shader/Lightmap/GLSL/LightmapUVFS.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
precision mediump float;
precision highp sampler2D;
precision highp sampler2DArray;

uniform sampler2DArray uTexLightmap;

Expand Down
20 changes: 17 additions & 3 deletions Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.d.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
precision mediump float;
precision highp sampler2D;
precision highp sampler2DArray;

uniform sampler2D uTexDiffuse;
uniform sampler2D uTexColor;
uniform sampler2D uTexCurrentLum;
uniform sampler2D uTexLastLum;

in vec2 varTexCoord0;
in vec4 varColor;
Expand All @@ -10,6 +14,16 @@ out vec4 FragColor;

void main(void)
{
float4 color = texture(uTexDiffuse, varTexCoord0.xy);
FragColor = float4(linearRGB(color.rgb), color.a);
vec4 color = texture(uTexColor, varTexCoord0.xy);

float currentLum = textureLod(uTexCurrentLum, varTexCoord0.xy, 10.0).x;

if (varTexCoord0.x < 0.5)
{
float c = currentLum;
FragColor = vec4(c, c, c, 1.0);
return;
}

FragColor = vec4(linearRGB(color.rgb), color.a);
}
17 changes: 14 additions & 3 deletions Assets/BuiltIn/Shader/PostProcessing/GLSL/PostEffectFS.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
precision mediump float;
uniform sampler2D uTexDiffuse;
precision highp sampler2D;
precision highp sampler2DArray;
uniform sampler2D uTexColor;
uniform sampler2D uTexCurrentLum;
uniform sampler2D uTexLastLum;
in vec2 varTexCoord0;
in vec4 varColor;
out vec4 FragColor;
Expand All @@ -15,6 +19,13 @@ vec3 linearRGB(vec3 color)
}
void main(void)
{
float4 color = texture(uTexDiffuse, varTexCoord0.xy);
FragColor = float4(linearRGB(color.rgb), color.a);
vec4 color = texture(uTexColor, varTexCoord0.xy);
float currentLum = textureLod(uTexCurrentLum, varTexCoord0.xy, 10.0).x;
if (varTexCoord0.x < 0.5)
{
float c = currentLum;
FragColor = vec4(c, c, c, 1.0);
return;
}
FragColor = vec4(linearRGB(color.rgb), color.a);
}
43 changes: 40 additions & 3 deletions Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.d.hlsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Texture2D uTexDiffuse : register(t0);
SamplerState uTexDiffuseSampler : register(s0);
Texture2D uTexColor : register(t0);
SamplerState uTexColorSampler : register(s0);

Texture2D uTexCurrentLum : register(t1);
SamplerState uTexCurrentLumSampler : register(s1);

Texture2D uTexLastLum : register(t2);
SamplerState uTexLastLumSampler : register(s2);

struct PS_INPUT
{
Expand All @@ -10,8 +16,39 @@ struct PS_INPUT

#include "LibToneMapping.hlsl"

float CalcLuminance(float3 color)
{
return max(dot(color, float3(0.299, 0.587, 0.114)), 0.0001);
}

float4 main(PS_INPUT input) : SV_TARGET
{
float4 color = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
float4 color = uTexColor.Sample(uTexColorSampler, input.tex0);

float currentLum = uTexCurrentLum.SampleLevel(uTexCurrentLumSampler, input.tex0, 10.0).x;
// float lastLum = uTexLastLum.SampleLevel(uTexLastLumSampler, input.tex0, 10.0).x;

if (input.tex0.x < 0.5)
{
float c = currentLum;
return float4(c, c, c, 1.0);
}

/*
float adaptedLum = lastLum + (currentLum - lastLum) * 0.1;
float pixelLuminance = CalcLuminance(color.rgb);
float avgLuminance = max(adaptedLum, 0.001f);
float keyValue = 0.2;
float linearExposure = (keyValue / avgLuminance);
float exposure = log2(max(linearExposure, 0.0001f));
// exposure -= threshold;
color = exp2(exposure) * color;
*/

return float4(linearRGB(color.rgb), color.a);
}
20 changes: 17 additions & 3 deletions Assets/BuiltIn/Shader/PostProcessing/HLSL/PostEffectFS.hlsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Texture2D uTexDiffuse : register(t0);
SamplerState uTexDiffuseSampler : register(s0);
Texture2D uTexColor : register(t0);
SamplerState uTexColorSampler : register(s0);
Texture2D uTexCurrentLum : register(t1);
SamplerState uTexCurrentLumSampler : register(s1);
Texture2D uTexLastLum : register(t2);
SamplerState uTexLastLumSampler : register(s2);
struct PS_INPUT
{
float4 pos : SV_POSITION;
Expand All @@ -16,8 +20,18 @@ float3 linearRGB(float3 color)
{
return pow(color, invGamma);
}
float CalcLuminance(float3 color)
{
return max(dot(color, float3(0.299, 0.587, 0.114)), 0.0001);
}
float4 main(PS_INPUT input) : SV_TARGET
{
float4 color = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
float4 color = uTexColor.Sample(uTexColorSampler, input.tex0);
float currentLum = uTexCurrentLum.SampleLevel(uTexCurrentLumSampler, input.tex0, 10.0).x;
if (input.tex0.x < 0.5)
{
float c = currentLum;
return float4(c, c, c, 1.0);
}
return float4(linearRGB(color.rgb), color.a);
}
4 changes: 3 additions & 1 deletion Assets/BuiltIn/Shader/PostProcessing/PostEffect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<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"/>
<uniform name="uTexColor" type="DEFAULT_VALUE" value="0" float="1" directX="false"/>
<uniform name="uTexCurrentLum" type="DEFAULT_VALUE" value="1" float="1" directX="false"/>
<uniform name="uTexLastLum" type="DEFAULT_VALUE" value="2" float="1" directX="false"/>
</fs>
</uniforms>
<customUI>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void main(void)
float indirectMul = uLightMultiplier.y;

// backface when render lightmap
if (dot(viewDir, normal) < 0)
if (dot(viewDir, normal) < 0.0)
{
normal = normal * -1.0;
directMul = 0.3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void main(void)
vec3 viewDir = normalize(v);
float directMul = uLightMultiplier.x;
float indirectMul = uLightMultiplier.y;
if (dot(viewDir, normal) < 0)
if (dot(viewDir, normal) < 0.0)
{
normal = normal * -1.0;
directMul = 0.3;
Expand Down
15 changes: 12 additions & 3 deletions Projects/Irrlicht/Source/COGLES3Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,11 @@ namespace irr
void COGLES3Texture::regenerateMipMapLevels(void* mipmapData)
{
// texture require mipmaps?
if (!HasMipMaps)
if (!HasMipMaps && !IsRenderTarget)
return;

// we don't use custom data for mipmaps.
if (!mipmapData)
if (!mipmapData && !IsRenderTarget)
{
// compressed textures require custom data for prepare mipmaps.
if (IsCompressed)
Expand All @@ -722,9 +722,18 @@ namespace irr
}

// hardware moethods for generate mipmaps.
if (!mipmapData && AutomaticMipmapUpdate && !MipmapLegacyMode)
if (IsRenderTarget || (!mipmapData && AutomaticMipmapUpdate && !MipmapLegacyMode))
{
if (IsRenderTarget)
{
Driver->setActiveTexture(0, this);
Driver->getBridgeCalls()->setTexture(0);
}

glEnable(GL_TEXTURE_2D);
glGenerateMipmap(GL_TEXTURE_2D);

HasMipMaps = true;
return;
}

Expand Down
13 changes: 10 additions & 3 deletions Projects/Irrlicht/Source/COpenGLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,11 @@ namespace irr
void COpenGLTexture::regenerateMipMapLevels(void* mipmapData)
{
// texture require mipmaps?
if (!HasMipMaps)
if (!HasMipMaps && !IsRenderTarget)
return;

// we don't use custom data for mipmaps.
if (!mipmapData)
if (!mipmapData && !IsRenderTarget)
{
// compressed textures require custom data for prepare mipmaps.
if (IsCompressed)
Expand All @@ -744,11 +744,18 @@ namespace irr
}

// hardware moethods for generate mipmaps.
if (!mipmapData && AutomaticMipmapUpdate && !MipmapLegacyMode)
if (IsRenderTarget || (!mipmapData && AutomaticMipmapUpdate && !MipmapLegacyMode))
{
if (IsRenderTarget)
{
Driver->setActiveTexture(0, this);
Driver->getBridgeCalls()->setTexture(0);
}

glEnable(GL_TEXTURE_2D);
Driver->extGlGenerateMipmap(GL_TEXTURE_2D);

HasMipMaps = true;
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace Skylicht
float h = (float)m_lumSize.Height;

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

void CPostProcessorRP::postProcessing(ITexture *finalTarget, ITexture *color, ITexture *normal, ITexture *position, const core::recti& viewport)
Expand All @@ -114,11 +114,9 @@ namespace Skylicht
renderH = (float)viewport.getHeight();
}

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);
m_finalPass.setTexture(0, color);
m_finalPass.setTexture(1, m_currentLum);
m_finalPass.setTexture(2, m_lastLum);

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

0 comments on commit 6d0fd1d

Please sign in to comment.