add tonemapping shaders

This commit is contained in:
hyzboy 2020-05-22 19:59:34 +08:00
parent eb0b29275e
commit 23685cd868
18 changed files with 606 additions and 59 deletions

View File

@ -0,0 +1,21 @@
const float GAMMA = 2.2;
const float INV_GAMMA = 1.0 / GAMMA;
// linear to sRGB approximation
// see http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
vec3 linearTosRGB(vec3 color)
{
return pow(color, vec3(INV_GAMMA));
}
// sRGB to linear approximation
// see http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
vec3 sRGBToLinear(vec3 srgbIn)
{
return vec3(pow(srgbIn.xyz, vec3(GAMMA)));
}
vec4 sRGBToLinear(vec4 srgbIn)
{
return vec4(sRGBToLinear(srgbIn.xyz), srgbIn.w);
}

11
shader/ToneMap/ACES.glsl Normal file
View File

@ -0,0 +1,11 @@
// ACES tone map
// see: https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
vec3 ToneMapping(vec3 color)
{
const float A = 2.51;
const float B = 0.03;
const float C = 2.43;
const float D = 0.59;
const float E = 0.14;
return linearTosRGB(clamp((color * (A * color + B)) / (color * (C * color + D) + E), 0.0, 1.0));
}

View File

@ -0,0 +1,7 @@
// Hejl Richard tone map
// see: http://filmicworlds.com/blog/filmic-tonemapping-operators/
vec3 ToneMapping(vec3 color)
{
color = max(vec3(0.0), color - vec3(0.004));
return (color*(6.2*color+.5))/(color*(6.2*color+1.7)+0.06);
}

View File

@ -0,0 +1,6 @@
vec3 ToneMapping(vec3 color)
{
color = clamp(u_Exposure * color, 0., 1.);
return linearTosRGB(color);
}

View File

@ -0,0 +1,8 @@
vec3 ToneMapping(vec3 color)
{
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
float toneMappedLuma = luma / (1. + luma);
color *= toneMappedLuma / luma;
return linearTosRGB(color);
}

View File

@ -0,0 +1,6 @@
vec3 ToneMapping(vec3 color)
{
color = exp( -1.0 / ( 2.72*color + 0.15 ) );
return linearTosRGB(color);
}

View File

@ -0,0 +1,6 @@
vec3 ToneMapping(vec3 color)
{
color *= u_Exposure/(1. + color / u_Exposure);
return linearTosRGB(color);
}

View File

@ -0,0 +1,20 @@
// Uncharted 2 tone map
// see: http://filmicworlds.com/blog/filmic-tonemapping-operators/
vec3 toneMapUncharted2Impl(vec3 color)
{
const float A = 0.15;
const float B = 0.50;
const float C = 0.10;
const float D = 0.20;
const float E = 0.02;
const float F = 0.30;
return ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F;
}
vec3 ToneMapping(vec3 color)
{
const float W = 11.2;
color = toneMapUncharted2Impl(color * 2.0);
vec3 whiteScale = 1.0 / toneMapUncharted2Impl(vec3(W));
return linearTosRGB(color * whiteScale);
}

View File

@ -0,0 +1,9 @@
vec3 ToneMapping(vec3 color)
{
float white = 2.;
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma);
color *= toneMappedLuma / luma;
return linearTosRGB(color);
}

View File

@ -1,4 +1,4 @@
/* /*
This shader experiments the effect of different tone mapping operators. This shader experiments the effect of different tone mapping operators.
This is still a work in progress. This is still a work in progress.
@ -17,69 +17,44 @@ Zavie
*/ */
vec3 linearToneMapping(vec3 color) uniform float u_Exposure;
{
float exposure = 1.;
color = clamp(exposure * color, 0., 1.);
color = pow(color, vec3(1. / gamma));
return color;
}
vec3 simpleReinhardToneMapping(vec3 color)
{
float exposure = 1.5;
color *= exposure/(1. + color / exposure);
color = pow(color, vec3(1. / gamma));
return color;
}
vec3 lumaBasedReinhardToneMapping(vec3 color) vec3 toneMap(vec3 color)
{ {
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722)); color *= u_Exposure;
float toneMappedLuma = luma / (1. + luma);
color *= toneMappedLuma / luma;
color = pow(color, vec3(1. / gamma));
return color;
}
vec3 whitePreservingLumaBasedReinhardToneMapping(vec3 color) #ifdef TONEMAP_UNCHARTED
{ return toneMapUncharted(color);
float white = 2.; #endif
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma);
color *= toneMappedLuma / luma;
color = pow(color, vec3(1. / gamma));
return color;
}
vec3 RomBinDaHouseToneMapping(vec3 color) #ifdef TONEMAP_HEJLRICHARD
{ return toneMapHejlRichard(color);
color = exp( -1.0 / ( 2.72*color + 0.15 ) ); #endif
color = pow(color, vec3(1. / gamma));
return color;
}
vec3 filmicToneMapping(vec3 color) #ifdef TONEMAP_ACES
{ return toneMapACES(color);
color = max(vec3(0.), color - vec3(0.004)); #endif
color = (color * (6.2 * color + .5)) / (color * (6.2 * color + 1.7) + 0.06);
return color;
}
vec3 Uncharted2ToneMapping(vec3 color) #ifdef TONEMAP_LINEAR
{ return linearToneMapping(color);
float A = 0.15; #endif//TONEMAP_LINEAR
float B = 0.50;
float C = 0.10; #ifdef TONEMAP_SIMPLE_REINHARD
float D = 0.20; return simpleReinhardToneMapping(color)
float E = 0.02; #endif//TONEMAP_SIMPLE_REINHARD
float F = 0.30;
float W = 11.2; #ifdef TONEMAP_LUMA_BASED_REINHARD
float exposure = 2.; return lumaBasedReinhardToneMapping(color)
color *= exposure; #endif//TONE_MAP_LUMA_BASED_REINHARD
color = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F; #ifdef TONEMAP_WHITE_PRESERVING_LUMA_BASED_REINHARD
color /= white; return whitePreservingLumaBasedReinhardToneMapping(color)
color = pow(color, vec3(1. / gamma)); #endif//TONEMAP_WHITE_PRESERVING_LUMA_BASED_REINHARD
return color;
#ifdef TONEMAP_ROM_BIN_DA_HOUSE
return RomBinDaHouseToneMapping(color)
#endif//TONEMAP_ROM_BIN_DA_HOUSE
return linearTosRGB(color);
} }

159
shader/brdf.glsl Normal file
View File

@ -0,0 +1,159 @@
//
// Fresnel
//
// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
// https://github.com/wdas/brdf/tree/master/src/brdfs
// https://google.github.io/filament/Filament.md.html
//
vec3 F_None(vec3 f0, vec3 f90, float VdotH)
{
return f0;
}
// The following equation models the Fresnel reflectance term of the spec equation (aka F())
// Implementation of fresnel from [4], Equation 15
vec3 F_Schlick(vec3 f0, vec3 f90, float VdotH)
{
return f0 + (f90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0);
}
vec3 F_CookTorrance(vec3 f0, vec3 f90, float VdotH)
{
vec3 f0_sqrt = sqrt(f0);
vec3 ior = (1.0 + f0_sqrt) / (1.0 - f0_sqrt);
vec3 c = vec3(VdotH);
vec3 g = sqrt(sq(ior) + c*c - 1.0);
return 0.5 * pow(g-c, vec3(2.0)) / pow(g+c, vec3(2.0)) * (1.0 + pow(c*(g+c) - 1.0, vec3(2.0)) / pow(c*(g-c) + 1.0, vec3(2.0)));
}
// Smith Joint GGX
// Note: Vis = G / (4 * NdotL * NdotV)
// see Eric Heitz. 2014. Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs. Journal of Computer Graphics Techniques, 3
// see Real-Time Rendering. Page 331 to 336.
// see https://google.github.io/filament/Filament.md.html#materialsystem/specularbrdf/geometricshadowing(specularg)
float V_GGX(float NdotL, float NdotV, float alphaRoughness)
{
float alphaRoughnessSq = alphaRoughness * alphaRoughness;
float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);
float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);
float GGX = GGXV + GGXL;
if (GGX > 0.0)
{
return 0.5 / GGX;
}
return 0.0;
}
// Anisotropic GGX visibility function, with height correlation.
// T: Tanget, B: Bi-tanget
float V_GGX_anisotropic(float NdotL, float NdotV, float BdotV, float TdotV, float TdotL, float BdotL, float anisotropy, float at, float ab)
{
float GGXV = NdotL * length(vec3(at * TdotV, ab * BdotV, NdotV));
float GGXL = NdotV * length(vec3(at * TdotL, ab * BdotL, NdotL));
float v = 0.5 / (GGXV + GGXL);
return clamp(v, 0.0, 1.0);
}
// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L136
// https://github.com/google/filament/blob/master/libs/ibl/src/CubemapIBL.cpp#L179
// Note: Google call it V_Ashikhmin and V_Neubelt
float V_Ashikhmin(float NdotL, float NdotV)
{
return clamp(1.0 / (4.0 * (NdotL + NdotV - NdotL * NdotV)),0.0,1.0);
}
// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L131
float V_Kelemen(float LdotH)
{
// Kelemen 2001, "A Microfacet Based Coupled Specular-Matte BRDF Model with Importance Sampling"
return 0.25 / (LdotH * LdotH);
}
// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
float D_GGX(float NdotH, float alphaRoughness)
{
float alphaRoughnessSq = alphaRoughness * alphaRoughness;
float f = (NdotH * NdotH) * (alphaRoughnessSq - 1.0) + 1.0;
return alphaRoughnessSq / (M_PI * f * f);
}
// Anisotropic GGX NDF with a single anisotropy parameter controlling the normal orientation.
// See https://google.github.io/filament/Filament.html#materialsystem/anisotropicmodel
// T: Tanget, B: Bi-tanget
float D_GGX_anisotropic(float NdotH, float TdotH, float BdotH, float anisotropy, float at, float ab)
{
float a2 = at * ab;
vec3 f = vec3(ab * TdotH, at * BdotH, a2 * NdotH);
float w2 = a2 / dot(f, f);
return a2 * w2 * w2 / M_PI;
}
float D_Ashikhmin(float NdotH, float alphaRoughness)
{
// Ashikhmin 2007, "Distribution-based BRDFs"
float a2 = alphaRoughness * alphaRoughness;
float cos2h = NdotH * NdotH;
float sin2h = 1.0 - cos2h;
float sin4h = sin2h * sin2h;
float cot2 = -cos2h / (a2 * sin2h);
return 1.0 / (M_PI * (4.0 * a2 + 1.0) * sin4h) * (4.0 * exp(cot2) + sin4h);
}
//Sheen implementation-------------------------------------------------------------------------------------
// See https://github.com/sebavan/glTF/tree/KHR_materials_sheen/extensions/2.0/Khronos/KHR_materials_sheen
// Estevez and Kulla http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
float D_Charlie(float sheenRoughness, float NdotH)
{
sheenRoughness = max(sheenRoughness, 0.000001); //clamp (0,1]
float alphaG = sheenRoughness * sheenRoughness;
float invR = 1.0 / alphaG;
float cos2h = NdotH * NdotH;
float sin2h = 1.0 - cos2h;
return (2.0 + invR) * pow(sin2h, invR * 0.5) / (2.0 * M_PI);
}
//https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#acknowledgments AppendixB
vec3 BRDF_lambertian(vec3 f0, vec3 f90, vec3 diffuseColor, float VdotH)
{
// see https://seblagarde.wordpress.com/2012/01/08/pi-or-not-to-pi-in-game-lighting-equation/
return (1.0 - F_Schlick(f0, f90, VdotH)) * (diffuseColor / M_PI);
}
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#acknowledgments AppendixB
vec3 BRDF_specularGGX(vec3 f0, vec3 f90, float alphaRoughness, float VdotH, float NdotL, float NdotV, float NdotH)
{
vec3 F = F_Schlick(f0, f90, VdotH);
float Vis = V_GGX(NdotL, NdotV, alphaRoughness);
float D = D_GGX(NdotH, alphaRoughness);
return F * Vis * D;
}
vec3 BRDF_specularAnisotropicGGX(vec3 f0, vec3 f90, float alphaRoughness, float VdotH, float NdotL, float NdotV, float NdotH,
float BdotV, float TdotV, float TdotL, float BdotL, float TdotH, float BdotH, float anisotropy)
{
// Roughness along tangent and bitangent.
// Christopher Kulla and Alejandro Conty. 2017. Revisiting Physically Based Shading at Imageworks
float at = max(alphaRoughness * (1.0 + anisotropy), 0.00001);
float ab = max(alphaRoughness * (1.0 - anisotropy), 0.00001);
vec3 F = F_Schlick(f0, f90, VdotH);
float V = V_GGX_anisotropic(NdotL, NdotV, BdotV, TdotV, TdotL, BdotL, anisotropy, at, ab);
float D = D_GGX_anisotropic(NdotH, TdotH, BdotH, anisotropy, at, ab);
return F * V * D;
}
// f_sheen
vec3 BRDF_specularSheen(vec3 sheenColor, float sheenIntensity, float sheenRoughness, float NdotL, float NdotV, float NdotH)
{
float sheenDistribution = D_Charlie(sheenRoughness, NdotH);
float sheenVisibility = V_Ashikhmin(NdotL, NdotV);
return sheenColor * sheenIntensity * sheenDistribution * sheenVisibility;
}

17
shader/cnmr.lightmodel Normal file
View File

@ -0,0 +1,17 @@
//该文件用于描述光照模型
[intro]
author=
infomation=
[attr] //该段用于描述该着色最终会需要那些数据
//会根据最终需求,数据可能来自纹理,可能来自顶点,或是全局固定
vec3 BaseColor;
vec3 Normal;
float Metallic;
float Roughness;
[compute] //计算函数体
{
}

View File

@ -0,0 +1,12 @@
#version 450 core
layout(location = 0) in vec2 Vertex;
layout(location = 0) out vec2 FragmentPosition;
void main()
{
gl_Position=vec4(Vertex,0.0,1.0);
FragmentPosition=(Vertex+1.0)/2.0;
}

45
shader/gbuffer intro.txt Normal file
View File

@ -0,0 +1,45 @@
[attr] //该段用于描述该材质最终会需要那些数据
vec3 BaseColor;
vec3 Normal;
float Metallic;
float Roughness;
[framebuffer] //该段用于描述当前shader会输出到几个Framebuffer上
//如果是前向渲染一般只有一个Framebuffer
vec4 FinalColor;
//如果是延迟渲染则代表GBuffer可能存在多个framebuffer
vec4 gb_color_metallic;
vec4 gb_normal_roughness;
[attr_to_fb] //该段用于描述attr如何转换到framebuffer上
//如果是UI之类直接上色的那会比较颜色
FinalColor=vec4(BaseColor,1.0);
//如果是前向渲染
FinalColor=BRDF(BaseColor,Normal,Metallic,Roughness);
//如果是延迟渲染
gb_color_metallic =vec4(BaseColor,Metallic);
gb_normal_roughness =vec4(Normal, Roughness);
[gb_to_attr] //该段为延迟渲染独有用于表示如何从gbuffer分解成独立数据
vec4 gb_cm=texture(gb_color_metallic,FragmentPosition);
vec4 gb_nr=texture(gb_normal_roughness,FragmentPosition);
BaseColor =gb_cm.rgb;
Metallic =gb_cm.a;
Normal =gb_nr.rgb;
Roughness =gb_nr.a;
gl_FragColor=BRDF(BaseColor,Normal,Metallic,Roughness);

129
shader/primitive.vert Normal file
View File

@ -0,0 +1,129 @@
#include <animation.glsl>
in vec3 a_Position;
out vec3 v_Position;
#ifdef HAS_NORMALS
in vec3 a_Normal;
#endif
#ifdef HAS_TANGENTS
in vec4 a_Tangent;
#endif
#ifdef HAS_NORMALS
#ifdef HAS_TANGENTS
out mat3 v_TBN;
#else
out vec3 v_Normal;
#endif
#endif
#ifdef HAS_UV_SET1
in vec2 a_UV1;
#endif
#ifdef HAS_UV_SET2
in vec2 a_UV2;
#endif
out vec2 v_UVCoord1;
out vec2 v_UVCoord2;
#ifdef HAS_VERTEX_COLOR_VEC3
in vec3 a_Color;
out vec3 v_Color;
#endif
#ifdef HAS_VERTEX_COLOR_VEC4
in vec4 a_Color;
out vec4 v_Color;
#endif
uniform mat4 u_ViewProjectionMatrix;
uniform mat4 u_ModelMatrix;
uniform mat4 u_NormalMatrix;
vec4 getPosition()
{
vec4 pos = vec4(a_Position, 1.0);
#ifdef USE_MORPHING
pos += getTargetPosition();
#endif
#ifdef USE_SKINNING
pos = getSkinningMatrix() * pos;
#endif
return pos;
}
#ifdef HAS_NORMALS
vec3 getNormal()
{
vec3 normal = a_Normal;
#ifdef USE_MORPHING
normal += getTargetNormal();
#endif
#ifdef USE_SKINNING
normal = mat3(getSkinningNormalMatrix()) * normal;
#endif
return normalize(normal);
}
#endif
#ifdef HAS_TANGENTS
vec3 getTangent()
{
vec3 tangent = a_Tangent.xyz;
#ifdef USE_MORPHING
tangent += getTargetTangent();
#endif
#ifdef USE_SKINNING
tangent = mat3(getSkinningMatrix()) * tangent;
#endif
return normalize(tangent);
}
#endif
void main()
{
vec4 pos = u_ModelMatrix * getPosition();
v_Position = vec3(pos.xyz) / pos.w;
#ifdef HAS_NORMALS
#ifdef HAS_TANGENTS
vec3 tangent = getTangent();
vec3 normalW = normalize(vec3(u_NormalMatrix * vec4(getNormal(), 0.0)));
vec3 tangentW = normalize(vec3(u_ModelMatrix * vec4(tangent, 0.0)));
vec3 bitangentW = cross(normalW, tangentW) * a_Tangent.w;
v_TBN = mat3(tangentW, bitangentW, normalW);
#else // !HAS_TANGENTS
v_Normal = normalize(vec3(u_NormalMatrix * vec4(getNormal(), 0.0)));
#endif
#endif // !HAS_NORMALS
v_UVCoord1 = vec2(0.0, 0.0);
v_UVCoord2 = vec2(0.0, 0.0);
#ifdef HAS_UV_SET1
v_UVCoord1 = a_UV1;
#endif
#ifdef HAS_UV_SET2
v_UVCoord2 = a_UV2;
#endif
#if defined(HAS_VERTEX_COLOR_VEC3) || defined(HAS_VERTEX_COLOR_VEC4)
v_Color = a_Color;
#endif
gl_Position = u_ViewProjectionMatrix * pos;
}

55
shader/r_composition.frag Normal file
View File

@ -0,0 +1,55 @@
#version 460 core
/**
* Copyright (c) 2018-2020 www.hyzgame.com
*
* Create by ShaderMaker
*/
layout(push_constant) uniform Consts
{
mat4 local_to_world;
mat3 normal;
vec3 object_position;
vec3 object_size;
} pc;
layout(binding = 0) uniform WorldMatrix // hgl/math/Math.h
{
mat4 ortho;
mat4 projection;
mat4 inverse_projection;
mat4 modelview;
mat4 inverse_modelview;
mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos;
} world;
layout(binding=0) uniform sampler2D rb_depth;
layout(binding=1) uniform sampler2D gb_color_metallic;
layout(binding=2) uniform sampler2D gb_normal_roughness;
layout(location=0) in vec2 vs_out_position;
layout(location=0) out vec4 FragColor;
void main()
{
vec3 BaseColor;
vec3 Normal;
float Metallic;
float Roughness;
vec4 gb_cm=texture(gb_color_metallic,FragmentPosition);
vec4 gb_cr=texture(gb_normal_roughness,FragmentPosition);
BaseColor =gb_cm.rgb;
Metallic =gb_cm.a;
Normal =gb_cr.rgb;
Roughness =gb_cr.a;
//[Begin] Your code------------------------------------
//[End] Your code--------------------------------------
FragColor=
}

17
shader/r_composition.vert Normal file
View File

@ -0,0 +1,17 @@
#version 460 core
/**
* Copyright (c) 2018-2020 www.hyzgame.com
*
* Create by ShaderMaker
*/
layout(location = 0) in vec2 Vertex;
layout(location = 0) out vec2 vs_out_position;
void main()
{
gl_Position=vec4(Vertex,0.0,1.0);
vs_out_position=(Vertex+1.0)/2.0;
}

44
shader/r_gbuffer.frag Normal file
View File

@ -0,0 +1,44 @@
#version 460 core
/**
* Copyright (c) 2018-2020 www.hyzgame.com
*
* Create by ShaderMaker
*/
layout(push_constant) uniform Consts
{
mat4 local_to_world;
mat3 normal;
vec3 object_position;
vec3 object_size;
} pc;
layout(binding = 0) uniform WorldMatrix // hgl/math/Math.h
{
mat4 ortho;
mat4 projection;
mat4 inverse_projection;
mat4 modelview;
mat4 inverse_modelview;
mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos;
} world;
layout(location=0) out vec4 gb_color_metallic;
layout(location=1) out vec4 gb_normal_roughness;
void main()
{
vec3 BaseColor;
vec3 Normal;
float Metallic;
float Roughness;
//[Begin] Your code------------------------------------
//[End] Your code--------------------------------------
gb_color_metallic =vec4(BaseColor,Metallic);
gb_normal_roughness =vec4(Normal, Roughness);
}