Skip to content

Commit

Permalink
feat: #86 Add Texture Linear RGB & SRGB shader
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 23, 2020
1 parent 0752a52 commit af0a8cb
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 15 deletions.
15 changes: 15 additions & 0 deletions Assets/BuiltIn/Shader/Basic/GLSL/TextureLinearRGBFS.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 "../../PostProcessing/GLSL/LibToneMapping.glsl"

void main(void)
{
vec4 result = texture(uTexDiffuse, varTexCoord0.xy);
FragColor = vec4(linearRGB(result.rgb) * varColor.rgb, result.a * varColor.a);
}
20 changes: 20 additions & 0 deletions Assets/BuiltIn/Shader/Basic/GLSL/TextureLinearRGBFS.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)
{
vec4 result = texture(uTexDiffuse, varTexCoord0.xy);
FragColor = vec4(linearRGB(result.rgb) * varColor.rgb, result.a * varColor.a);
}
17 changes: 17 additions & 0 deletions Assets/BuiltIn/Shader/Basic/HLSL/TextureLinearRGBFS.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 "../../PostProcessing/HLSL/LibToneMapping.hlsl"

float4 main(PS_INPUT input) : SV_TARGET
{
float4 result = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
return float4(input.color.rgb * linearRGB(result.rgb), result.a * input.color.a);
}
23 changes: 23 additions & 0 deletions Assets/BuiltIn/Shader/Basic/HLSL/TextureLinearRGBFS.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 result = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0);
return float4(input.color.rgb * linearRGB(result.rgb), result.a * input.color.a);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<shaderConfig name="TextureTone" baseShader="SOLID">
<shaderConfig name="TextureLinearRGB" baseShader="SOLID">
<uniforms>
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
Expand All @@ -7,11 +7,6 @@
<uniform name="uTexDiffuse" type="DEFAULT_VALUE" value="0" float="1" directX="false"/>
</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/TextureToneFS.glsl"/>
<shader type="HLSL" vs="HLSL/TextureColorVS.hlsl" fs="HLSL/TextureToneFS.hlsl"/>
<shader type="GLSL" vs="GLSL/TextureColorVS.glsl" fs="GLSL/TextureLinearRGBFS.glsl"/>
<shader type="HLSL" vs="HLSL/TextureColorVS.hlsl" fs="HLSL/TextureLinearRGBFS.hlsl"/>
</shaderConfig>
12 changes: 12 additions & 0 deletions Assets/BuiltIn/Shader/Basic/TextureSRGB.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<shaderConfig name="TextureSRGB" 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>
<shader type="GLSL" vs="GLSL/TextureColorVS.glsl" fs="GLSL/TextureSRGBFS.glsl"/>
<shader type="HLSL" vs="HLSL/TextureColorVS.hlsl" fs="HLSL/TextureSRGBFS.hlsl"/>
</shaderConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main(void)

float avgLuminance = textureLod(uTexLuminance, varTexCoord0.xy, 10.0).x;

float target = 0.25;
float target = 0.2;
float threshold = 2.5;
float linearExposure = max((target / avgLuminance), 0.0001);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main(void)
{
vec4 color = texture(uTexColor, varTexCoord0.xy);
float avgLuminance = textureLod(uTexLuminance, varTexCoord0.xy, 10.0).x;
float target = 0.25;
float target = 0.2;
float threshold = 2.5;
float linearExposure = max((target / avgLuminance), 0.0001);
float exposure = min(exp2(log2(linearExposure)), threshold);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ float4 main(PS_INPUT input) : SV_TARGET

float avgLuminance = uTexLuminance.SampleLevel(uTexLuminanceSampler, input.tex0, 10.0).x;

float target = 0.25;
float target = 0.2;
float threshold = 2.5;
float linearExposure = max((target / avgLuminance), 0.0001);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ float4 main(PS_INPUT input) : SV_TARGET
{
float4 color = uTexColor.Sample(uTexColorSampler, input.tex0);
float avgLuminance = uTexLuminance.SampleLevel(uTexLuminanceSampler, input.tex0, 10.0).x;
float target = 0.25;
float target = 0.2;
float threshold = 2.5;
float linearExposure = max((target / avgLuminance), 0.0001);
float exposure = min(exp2(log2(linearExposure)), threshold);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Skylicht
SMaterial& mat = Buffer->getMaterial();
mat.setTexture(0, SkyDomeTexture);
mat.ZWriteEnable = false;
mat.MaterialType = CShaderManager::getInstance()->getShaderIDByName("TextureTone");
mat.MaterialType = CShaderManager::getInstance()->getShaderIDByName("TextureSRGB");

generateMesh();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ namespace Skylicht
loadShader("BuiltIn/Shader/ShadowMap/ShadowDepthWrite.xml");
loadShader("BuiltIn/Shader/ShadowMap/ShadowCubeDepthWrite.xml");

loadShader("BuiltIn/Shader/Basic/TextureTone.xml");
loadShader("BuiltIn/Shader/Basic/TextureSRGB.xml");
loadShader("BuiltIn/Shader/Basic/TextureLinearRGB.xml");
loadShader("BuiltIn/Shader/Basic/Luminance.xml");

loadShader("BuiltIn/Shader/Lightmap/Lightmap.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace Skylicht
m_textureColorShader(0),
m_pointLightShader(0),
m_pointLightShadowShader(0),
m_textureLinearRGBShader(0),
m_indirectMultipler(1.0f),
m_directMultipler(1.0f),
m_lightMultipler(1.0f),
Expand Down Expand Up @@ -112,6 +113,7 @@ namespace Skylicht
// get basic shader
CShaderManager *shaderMgr = CShaderManager::getInstance();
m_textureColorShader = shaderMgr->getShaderIDByName("TextureColor");
m_textureLinearRGBShader = shaderMgr->getShaderIDByName("TextureLinearRGB");
m_vertexColorShader = shaderMgr->getShaderIDByName("VertexColor");
m_lightmapArrayShader = shaderMgr->getShaderIDByName("Lightmap");
m_lightmapVertexShader = shaderMgr->getShaderIDByName("LightmapVertex");
Expand All @@ -126,7 +128,7 @@ namespace Skylicht

// final pass
m_finalPass.setTexture(0, m_target);
m_finalPass.MaterialType = m_textureColorShader;
m_finalPass.MaterialType = m_textureLinearRGBShader;
}

void CDeferredRP::initDefferredMaterial()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace Skylicht
bool m_isIndirectPass;
int m_vertexColorShader;
int m_textureColorShader;
int m_textureLinearRGBShader;
int m_lightmapArrayShader;
int m_lightmapVertexShader;
int m_lightmapIndirectTestShader;
Expand Down

0 comments on commit af0a8cb

Please sign in to comment.