From a3d5bfcce5feb0ec4c084c436cb7d06df7c94e9f Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 25 Jun 2017 16:01:45 +0200 Subject: [PATCH] Restructured code layout of shaders --- shaders/{src => debug}/fs_debug.sc | 0 shaders/debug/varying.def.sc | 13 + shaders/{src => debug}/vs_debug.sc | 0 .../fs_shadowmaps_unpackdepth.sc | 19 - shaders/draw_shadowmap/varying.def.sc | 5 - .../vs_shadowmaps_unpackdepth.sc | 16 - .../fs_sms_mesh.sc} | 9 +- .../{src => scene}/fs_sms_mesh_textured.sc | 0 shaders/scene/fs_sms_shadow.sh | 115 ++++++ shaders/scene/varying.def.sc | 13 + shaders/{src => scene}/vs_sms_mesh.sc | 0 .../{src => scene}/vs_sms_mesh_textured.sc | 0 shaders/{src => shadowmap}/fs_sms_shadow.sc | 0 shaders/shadowmap/varying.def.sc | 13 + shaders/shadowmap/vs_sms_mesh.sc | 24 ++ shaders/{src => skybox}/fs_ibl_skybox.sc | 0 shaders/skybox/shaderlib.sh | 386 ++++++++++++++++++ shaders/skybox/uniforms.sh | 28 ++ shaders/skybox/varying.def.sc | 13 + shaders/{src => skybox}/vs_ibl_skybox.sc | 0 shaders/src/vs_sms_shadow_pd.sc | 15 - src/modules/RenderModule.cc | 25 +- 22 files changed, 614 insertions(+), 80 deletions(-) rename shaders/{src => debug}/fs_debug.sc (100%) create mode 100644 shaders/debug/varying.def.sc rename shaders/{src => debug}/vs_debug.sc (100%) delete mode 100644 shaders/draw_shadowmap/fs_shadowmaps_unpackdepth.sc delete mode 100644 shaders/draw_shadowmap/varying.def.sc delete mode 100644 shaders/draw_shadowmap/vs_shadowmaps_unpackdepth.sc rename shaders/{src/fs_sms_shadow_pd.sc => scene/fs_sms_mesh.sc} (55%) rename shaders/{src => scene}/fs_sms_mesh_textured.sc (100%) create mode 100644 shaders/scene/fs_sms_shadow.sh create mode 100644 shaders/scene/varying.def.sc rename shaders/{src => scene}/vs_sms_mesh.sc (100%) rename shaders/{src => scene}/vs_sms_mesh_textured.sc (100%) rename shaders/{src => shadowmap}/fs_sms_shadow.sc (100%) create mode 100644 shaders/shadowmap/varying.def.sc create mode 100644 shaders/shadowmap/vs_sms_mesh.sc rename shaders/{src => skybox}/fs_ibl_skybox.sc (100%) create mode 100644 shaders/skybox/shaderlib.sh create mode 100644 shaders/skybox/uniforms.sh create mode 100644 shaders/skybox/varying.def.sc rename shaders/{src => skybox}/vs_ibl_skybox.sc (100%) delete mode 100644 shaders/src/vs_sms_shadow_pd.sc diff --git a/shaders/src/fs_debug.sc b/shaders/debug/fs_debug.sc similarity index 100% rename from shaders/src/fs_debug.sc rename to shaders/debug/fs_debug.sc diff --git a/shaders/debug/varying.def.sc b/shaders/debug/varying.def.sc new file mode 100644 index 0000000..2bd6633 --- /dev/null +++ b/shaders/debug/varying.def.sc @@ -0,0 +1,13 @@ +vec3 v_view : TEXCOORD0 = vec3(0.0, 0.0, 0.0); +vec4 v_shadowcoord : TEXCOORD1 = vec4(0.0, 0.0, 0.0, 0.0); +vec4 v_position : TEXCOORD2 = vec4(0.0, 0.0, 0.0, 0.0); +vec3 v_dir : TEXCOORD3 = vec3(0.0, 0.0, 0.0); +vec2 v_texcoord0 : TEXCOORD4 = vec2(0.0, 0.0); +vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_normal : NORMAL; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; + diff --git a/shaders/src/vs_debug.sc b/shaders/debug/vs_debug.sc similarity index 100% rename from shaders/src/vs_debug.sc rename to shaders/debug/vs_debug.sc diff --git a/shaders/draw_shadowmap/fs_shadowmaps_unpackdepth.sc b/shaders/draw_shadowmap/fs_shadowmaps_unpackdepth.sc deleted file mode 100644 index de62bdf..0000000 --- a/shaders/draw_shadowmap/fs_shadowmaps_unpackdepth.sc +++ /dev/null @@ -1,19 +0,0 @@ -$input v_texcoord0 - -/* - * Copyright 2013-2014 Dario Manesku. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause - */ - -#include "../common/common.sh" -SAMPLER2D(s_shadowMap0, 4); - -uniform vec4 u_params2; -#define u_depthValuePow u_params2.x - -void main() -{ - float depth = unpackRgbaToFloat(texture2D(s_shadowMap0, v_texcoord0) ); - vec3 rgba = pow(vec3_splat(depth), vec3_splat(u_depthValuePow) ); - gl_FragColor = vec4(rgba, 1.0); -} diff --git a/shaders/draw_shadowmap/varying.def.sc b/shaders/draw_shadowmap/varying.def.sc deleted file mode 100644 index 0ab43ca..0000000 --- a/shaders/draw_shadowmap/varying.def.sc +++ /dev/null @@ -1,5 +0,0 @@ -vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); - -vec3 a_position : POSITION; -vec4 a_normal : NORMAL; -vec2 a_texcoord0 : TEXCOORD0; diff --git a/shaders/draw_shadowmap/vs_shadowmaps_unpackdepth.sc b/shaders/draw_shadowmap/vs_shadowmaps_unpackdepth.sc deleted file mode 100644 index 239f418..0000000 --- a/shaders/draw_shadowmap/vs_shadowmaps_unpackdepth.sc +++ /dev/null @@ -1,16 +0,0 @@ -$input a_position, a_texcoord0 -$output v_texcoord0 - -/* - * Copyright 2013-2014 Dario Manesku. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - - v_texcoord0 = a_texcoord0; -} diff --git a/shaders/src/fs_sms_shadow_pd.sc b/shaders/scene/fs_sms_mesh.sc similarity index 55% rename from shaders/src/fs_sms_shadow_pd.sc rename to shaders/scene/fs_sms_mesh.sc index 86dc039..949af40 100644 --- a/shaders/src/fs_sms_shadow_pd.sc +++ b/shaders/scene/fs_sms_mesh.sc @@ -1,4 +1,4 @@ -$input v_position +$input v_view, v_normal, v_shadowcoord /* * Copyright 2013-2014 Dario Manesku. All rights reserved. @@ -7,8 +7,5 @@ $input v_position #include "../common/common.sh" -void main() -{ - float depth = v_position.z/v_position.w * 0.5 + 0.5; - gl_FragColor = packFloatToRgba(depth); -} +#define SHADOW_PACKED_DEPTH 0 +#include "fs_sms_shadow.sh" diff --git a/shaders/src/fs_sms_mesh_textured.sc b/shaders/scene/fs_sms_mesh_textured.sc similarity index 100% rename from shaders/src/fs_sms_mesh_textured.sc rename to shaders/scene/fs_sms_mesh_textured.sc diff --git a/shaders/scene/fs_sms_shadow.sh b/shaders/scene/fs_sms_shadow.sh new file mode 100644 index 0000000..77e7a23 --- /dev/null +++ b/shaders/scene/fs_sms_shadow.sh @@ -0,0 +1,115 @@ +/* + * Copyright 2013-2014 Dario Manesku. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +uniform vec4 u_lightPos; +uniform vec4 u_color; + +uniform vec4 u_shadowMapParams; +#define u_shadowMapSize u_shadowMapParams.x +#define u_shadowMapBias u_shadowMapParams.y + +#if SHADOW_PACKED_DEPTH +SAMPLER2D(u_shadowMap, 0); +# define Sampler sampler2D +#else +SAMPLER2DSHADOW(u_shadowMap, 0); +# define Sampler sampler2DShadow +#endif // SHADOW_PACKED_DEPTH + +#if TEXTURED +SAMPLER2D(sceneDefaultTexture, 1); +#endif + +vec2 lit(vec3 _ld, vec3 _n, vec3 _vd, float _exp) +{ + //diff + float ndotl = dot(_n, _ld); + + //spec + vec3 r = 2.0*ndotl*_n - _ld; //reflect(_ld, _n); + float rdotv = dot(r, _n); + float spec = step(0.0, ndotl) * pow(max(0.0, rdotv), _exp) * (2.0 + _exp)/8.0; + + return max(vec2(ndotl, spec), 0.0); +} + +float hardShadow(Sampler _sampler, vec4 _shadowCoord, float _bias) +{ + vec3 texCoord = _shadowCoord.xyz/_shadowCoord.w; +#if SHADOW_PACKED_DEPTH + return step(texCoord.z-_bias, unpackRgbaToFloat(texture2D(_sampler, texCoord.xy) ) ); +#else + return shadow2D(_sampler, vec3(texCoord.xy, texCoord.z-_bias) ); +#endif // SHADOW_PACKED_DEPTH +} + +float PCF(Sampler _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize) +{ + vec2 texCoord = _shadowCoord.xy/_shadowCoord.w; + + bool outside = any(greaterThan(texCoord, vec2_splat(1.0))) + || any(lessThan (texCoord, vec2_splat(0.0))) + ; + + if (outside) + { + return 1.0; + } + + float result = 0.0; + vec2 offset = _texelSize * _shadowCoord.w; + + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, -1.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, -0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, 0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-1.5, 1.5) * offset, 0.0, 0.0), _bias); + + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, -1.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, -0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, 0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(-0.5, 1.5) * offset, 0.0, 0.0), _bias); + + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, -1.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, -0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, 0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(0.5, 1.5) * offset, 0.0, 0.0), _bias); + + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, -1.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, -0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, 0.5) * offset, 0.0, 0.0), _bias); + result += hardShadow(_sampler, _shadowCoord + vec4(vec2(1.5, 1.5) * offset, 0.0, 0.0), _bias); + + return result / 16.0; +} + +void main() +{ + vec3 color = u_color.xyz; + + vec3 v = v_view; + vec3 vd = -normalize(v); + vec3 n = v_normal; + vec3 l = u_lightPos.xyz; + vec3 ld = normalize(l); + + vec2 lc = lit(ld, n, vd, 1.0); + + vec2 texelSize = vec2_splat(1.0/u_shadowMapSize); + float visibility = PCF(u_shadowMap, v_shadowcoord, u_shadowMapBias, texelSize); + + vec3 ambient = 0.05 * color; + vec3 brdf = (lc.x * color + lc.y * color) * visibility; + +#if TEXTURED + vec4 texcolor = toLinear (texture2D(sceneDefaultTexture, v_texcoord0) ); + brdf = brdf * texcolor.xyz; + ambient = ambient * texcolor.xyz; +#endif + vec3 final = toGamma(abs(ambient + brdf) ); + + gl_FragColor = vec4(final, 1.0); +} diff --git a/shaders/scene/varying.def.sc b/shaders/scene/varying.def.sc new file mode 100644 index 0000000..2bd6633 --- /dev/null +++ b/shaders/scene/varying.def.sc @@ -0,0 +1,13 @@ +vec3 v_view : TEXCOORD0 = vec3(0.0, 0.0, 0.0); +vec4 v_shadowcoord : TEXCOORD1 = vec4(0.0, 0.0, 0.0, 0.0); +vec4 v_position : TEXCOORD2 = vec4(0.0, 0.0, 0.0, 0.0); +vec3 v_dir : TEXCOORD3 = vec3(0.0, 0.0, 0.0); +vec2 v_texcoord0 : TEXCOORD4 = vec2(0.0, 0.0); +vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_normal : NORMAL; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; + diff --git a/shaders/src/vs_sms_mesh.sc b/shaders/scene/vs_sms_mesh.sc similarity index 100% rename from shaders/src/vs_sms_mesh.sc rename to shaders/scene/vs_sms_mesh.sc diff --git a/shaders/src/vs_sms_mesh_textured.sc b/shaders/scene/vs_sms_mesh_textured.sc similarity index 100% rename from shaders/src/vs_sms_mesh_textured.sc rename to shaders/scene/vs_sms_mesh_textured.sc diff --git a/shaders/src/fs_sms_shadow.sc b/shaders/shadowmap/fs_sms_shadow.sc similarity index 100% rename from shaders/src/fs_sms_shadow.sc rename to shaders/shadowmap/fs_sms_shadow.sc diff --git a/shaders/shadowmap/varying.def.sc b/shaders/shadowmap/varying.def.sc new file mode 100644 index 0000000..2bd6633 --- /dev/null +++ b/shaders/shadowmap/varying.def.sc @@ -0,0 +1,13 @@ +vec3 v_view : TEXCOORD0 = vec3(0.0, 0.0, 0.0); +vec4 v_shadowcoord : TEXCOORD1 = vec4(0.0, 0.0, 0.0, 0.0); +vec4 v_position : TEXCOORD2 = vec4(0.0, 0.0, 0.0, 0.0); +vec3 v_dir : TEXCOORD3 = vec3(0.0, 0.0, 0.0); +vec2 v_texcoord0 : TEXCOORD4 = vec2(0.0, 0.0); +vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_normal : NORMAL; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; + diff --git a/shaders/shadowmap/vs_sms_mesh.sc b/shaders/shadowmap/vs_sms_mesh.sc new file mode 100644 index 0000000..585ed14 --- /dev/null +++ b/shaders/shadowmap/vs_sms_mesh.sc @@ -0,0 +1,24 @@ +$input a_position, a_normal +$output v_view, v_normal, v_shadowcoord + +/* + * Copyright 2013-2014 Dario Manesku. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "../common/common.sh" + +uniform mat4 u_lightMtx; + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + + vec4 normal = a_normal * 2.0 - 1.0; + v_normal = normalize(normal.xyz); + v_view = mul(u_modelView, vec4(a_position, 1.0)).xyz; + + const float shadowMapOffset = 0.001; + vec3 posOffset = a_position + normal.xyz * shadowMapOffset; + v_shadowcoord = mul(u_lightMtx, vec4(posOffset, 1.0) ); +} diff --git a/shaders/src/fs_ibl_skybox.sc b/shaders/skybox/fs_ibl_skybox.sc similarity index 100% rename from shaders/src/fs_ibl_skybox.sc rename to shaders/skybox/fs_ibl_skybox.sc diff --git a/shaders/skybox/shaderlib.sh b/shaders/skybox/shaderlib.sh new file mode 100644 index 0000000..51f9b6a --- /dev/null +++ b/shaders/skybox/shaderlib.sh @@ -0,0 +1,386 @@ +/* + * Copyright 2011-2016 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#ifndef __SHADERLIB_SH__ +#define __SHADERLIB_SH__ + +vec4 encodeRE8(float _r) +{ + float exponent = ceil(log2(_r) ); + return vec4(_r / exp2(exponent) + , 0.0 + , 0.0 + , (exponent + 128.0) / 255.0 + ); +} + +float decodeRE8(vec4 _re8) +{ + float exponent = _re8.w * 255.0 - 128.0; + return _re8.x * exp2(exponent); +} + +vec4 encodeRGBE8(vec3 _rgb) +{ + vec4 rgbe8; + float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z); + float exponent = ceil(log2(maxComponent) ); + rgbe8.xyz = _rgb / exp2(exponent); + rgbe8.w = (exponent + 128.0) / 255.0; + return rgbe8; +} + +vec3 decodeRGBE8(vec4 _rgbe8) +{ + float exponent = _rgbe8.w * 255.0 - 128.0; + vec3 rgb = _rgbe8.xyz * exp2(exponent); + return rgb; +} + +vec3 encodeNormalUint(vec3 _normal) +{ + return _normal * 0.5 + 0.5; +} + +vec3 decodeNormalUint(vec3 _encodedNormal) +{ + return _encodedNormal * 2.0 - 1.0; +} + +vec2 encodeNormalSphereMap(vec3 _normal) +{ + return normalize(_normal.xy) * sqrt(_normal.z * 0.5 + 0.5); +} + +vec3 decodeNormalSphereMap(vec2 _encodedNormal) +{ + float zz = dot(_encodedNormal, _encodedNormal) * 2.0 - 1.0; + return vec3(normalize(_encodedNormal.xy) * sqrt(1.0 - zz*zz), zz); +} + +vec2 octahedronWrap(vec2 _val) +{ + // Reference: + // Octahedron normal vector encoding + // http://kriscg.blogspot.com/2014/04/octahedron-normal-vector-encoding.html + return (1.0 - abs(_val.yx) ) + * mix(vec2_splat(-1.0), vec2_splat(1.0), vec2(greaterThanEqual(_val.xy, vec2_splat(0.0) ) ) ); +} + +vec2 encodeNormalOctahedron(vec3 _normal) +{ + _normal /= abs(_normal.x) + abs(_normal.y) + abs(_normal.z); + _normal.xy = _normal.z >= 0.0 ? _normal.xy : octahedronWrap(_normal.xy); + _normal.xy = _normal.xy * 0.5 + 0.5; + return _normal.xy; +} + +vec3 decodeNormalOctahedron(vec2 _encodedNormal) +{ + _encodedNormal = _encodedNormal * 2.0 - 1.0; + + vec3 normal; + normal.z = 1.0 - abs(_encodedNormal.x) - abs(_encodedNormal.y); + normal.xy = normal.z >= 0.0 ? _encodedNormal.xy : octahedronWrap(_encodedNormal.xy); + return normalize(normal); +} + +vec3 convertRGB2XYZ(vec3 _rgb) +{ + // Reference: + // RGB/XYZ Matrices + // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html + vec3 xyz; + xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb); + xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); + xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb); + return xyz; +} + +vec3 convertXYZ2RGB(vec3 _xyz) +{ + vec3 rgb; + rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz); + rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz); + rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz); + return rgb; +} + +vec3 convertXYZ2Yxy(vec3 _xyz) +{ + // Reference: + // http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html + float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) ); + return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv); +} + +vec3 convertYxy2XYZ(vec3 _Yxy) +{ + // Reference: + // http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html + vec3 xyz; + xyz.x = _Yxy.x*_Yxy.y/_Yxy.z; + xyz.y = _Yxy.x; + xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z; + return xyz; +} + +vec3 convertRGB2Yxy(vec3 _rgb) +{ + return convertXYZ2Yxy(convertRGB2XYZ(_rgb) ); +} + +vec3 convertYxy2RGB(vec3 _Yxy) +{ + return convertXYZ2RGB(convertYxy2XYZ(_Yxy) ); +} + +vec3 convertRGB2Yuv(vec3 _rgb) +{ + vec3 yuv; + yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) ); + yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5; + yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5; + return yuv; +} + +vec3 convertYuv2RGB(vec3 _yuv) +{ + vec3 rgb; + rgb.x = _yuv.x + 1.403*(_yuv.y-0.5); + rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5); + rgb.z = _yuv.x + 1.773*(_yuv.z-0.5); + return rgb; +} + +vec3 convertRGB2YIQ(vec3 _rgb) +{ + vec3 yiq; + yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb); + yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb); + yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb); + return yiq; +} + +vec3 convertYIQ2RGB(vec3 _yiq) +{ + vec3 rgb; + rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq); + rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq); + rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq); + return rgb; +} + +vec3 toLinear(vec3 _rgb) +{ + return pow(abs(_rgb), vec3_splat(2.2) ); +} + +vec4 toLinear(vec4 _rgba) +{ + return vec4(toLinear(_rgba.xyz), _rgba.w); +} + +vec3 toLinearAccurate(vec3 _rgb) +{ + vec3 lo = _rgb / 12.92; + vec3 hi = pow( (_rgb + 0.055) / 1.055, vec3_splat(2.4) ); + vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.04045) ) ) ); + return rgb; +} + +vec4 toLinearAccurate(vec4 _rgba) +{ + return vec4(toLinearAccurate(_rgba.xyz), _rgba.w); +} + +float toGamma(float _r) +{ + return pow(abs(_r), 1.0/2.2); +} + +vec3 toGamma(vec3 _rgb) +{ + return pow(abs(_rgb), vec3_splat(1.0/2.2) ); +} + +vec4 toGamma(vec4 _rgba) +{ + return vec4(toGamma(_rgba.xyz), _rgba.w); +} + +vec3 toGammaAccurate(vec3 _rgb) +{ + vec3 lo = _rgb * 12.92; + vec3 hi = pow(abs(_rgb), vec3_splat(1.0/2.4) ) * 1.055 - 0.055; + vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.0031308) ) ) ); + return rgb; +} + +vec4 toGammaAccurate(vec4 _rgba) +{ + return vec4(toGammaAccurate(_rgba.xyz), _rgba.w); +} + +vec3 toReinhard(vec3 _rgb) +{ + return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) ); +} + +vec4 toReinhard(vec4 _rgba) +{ + return vec4(toReinhard(_rgba.xyz), _rgba.w); +} + +vec3 toFilmic(vec3 _rgb) +{ + _rgb = max(vec3_splat(0.0), _rgb - 0.004); + _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06); + return _rgb; +} + +vec4 toFilmic(vec4 _rgba) +{ + return vec4(toFilmic(_rgba.xyz), _rgba.w); +} + +vec3 toAcesFilmic(vec3 _rgb) +{ + // Reference: + // ACES Filmic Tone Mapping Curve + // https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ + float aa = 2.51f; + float bb = 0.03f; + float cc = 2.43f; + float dd = 0.59f; + float ee = 0.14f; + return saturate( (_rgb*(aa*_rgb + bb) )/(_rgb*(cc*_rgb + dd) + ee) ); +} + +vec4 toAcesFilmic(vec4 _rgba) +{ + return vec4(toAcesFilmic(_rgba.xyz), _rgba.w); +} + +vec3 luma(vec3 _rgb) +{ + float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); + return vec3_splat(yy); +} + +vec4 luma(vec4 _rgba) +{ + return vec4(luma(_rgba.xyz), _rgba.w); +} + +vec3 conSatBri(vec3 _rgb, vec3 _csb) +{ + vec3 rgb = _rgb * _csb.z; + rgb = mix(luma(rgb), rgb, _csb.y); + rgb = mix(vec3_splat(0.5), rgb, _csb.x); + return rgb; +} + +vec4 conSatBri(vec4 _rgba, vec3 _csb) +{ + return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w); +} + +vec3 posterize(vec3 _rgb, float _numColors) +{ + return floor(_rgb*_numColors) / _numColors; +} + +vec4 posterize(vec4 _rgba, float _numColors) +{ + return vec4(posterize(_rgba.xyz, _numColors), _rgba.w); +} + +vec3 sepia(vec3 _rgb) +{ + vec3 color; + color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) ); + color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) ); + color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) ); + return color; +} + +vec4 sepia(vec4 _rgba) +{ + return vec4(sepia(_rgba.xyz), _rgba.w); +} + +vec3 blendOverlay(vec3 _base, vec3 _blend) +{ + vec3 lt = 2.0 * _base * _blend; + vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend); + return mix(lt, gte, step(vec3_splat(0.5), _base) ); +} + +vec4 blendOverlay(vec4 _base, vec4 _blend) +{ + return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w); +} + +vec3 adjustHue(vec3 _rgb, float _hue) +{ + vec3 yiq = convertRGB2YIQ(_rgb); + float angle = _hue + atan2(yiq.z, yiq.y); + float len = length(yiq.yz); + return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) ); +} + +vec4 packFloatToRgba(float _value) +{ + const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0); + const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + vec4 comp = fract(_value * shift); + comp -= comp.xxyz * mask; + return comp; +} + +float unpackRgbaToFloat(vec4 _rgba) +{ + const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0); + return dot(_rgba, shift); +} + +vec2 packHalfFloat(float _value) +{ + const vec2 shift = vec2(256, 1.0); + const vec2 mask = vec2(0, 1.0 / 256.0); + vec2 comp = fract(_value * shift); + comp -= comp.xx * mask; + return comp; +} + +float unpackHalfFloat(vec2 _rg) +{ + const vec2 shift = vec2(1.0 / 256.0, 1.0); + return dot(_rg, shift); +} + +float random(vec2 _uv) +{ + return fract(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453); +} + +vec3 fixCubeLookup(vec3 _v, float _lod, float _topLevelCubeSize) +{ + // Reference: + // Seamless cube-map filtering + // http://the-witness.net/news/2012/02/seamless-cube-map-filtering/ + float ax = abs(_v.x); + float ay = abs(_v.y); + float az = abs(_v.z); + float vmax = max(max(ax, ay), az); + float scale = 1.0 - exp2(_lod) / _topLevelCubeSize; + if (ax != vmax) { _v.x *= scale; } + if (ay != vmax) { _v.y *= scale; } + if (az != vmax) { _v.z *= scale; } + return _v; +} + +#endif // __SHADERLIB_SH__ diff --git a/shaders/skybox/uniforms.sh b/shaders/skybox/uniforms.sh new file mode 100644 index 0000000..61354fc --- /dev/null +++ b/shaders/skybox/uniforms.sh @@ -0,0 +1,28 @@ +/* + * Copyright 2016 Dario Manesku. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +uniform vec4 u_params[12]; +#define u_mtx0 u_params[0] +#define u_mtx1 u_params[1] +#define u_mtx2 u_params[2] +#define u_mtx3 u_params[3] +#define u_glossiness u_params[4].x +#define u_reflectivity u_params[4].y +#define u_exposure u_params[4].z +#define u_bgType u_params[4].w +#define u_metalOrSpec u_params[5].x +#define u_unused u_params[5].yzw +#define u_doDiffuse u_params[6].x +#define u_doSpecular u_params[6].y +#define u_doDiffuseIbl u_params[6].z +#define u_doSpecularIbl u_params[6].w +#define u_camPos u_params[7].xyz +#define u_unused7 u_params[7].w +#define u_rgbDiff u_params[8] +#define u_rgbSpec u_params[9] +#define u_lightDir u_params[10].xyz +#define u_unused10 u_params[10].w +#define u_lightCol u_params[11].xyz +#define u_unused11 u_params[11].w diff --git a/shaders/skybox/varying.def.sc b/shaders/skybox/varying.def.sc new file mode 100644 index 0000000..2bd6633 --- /dev/null +++ b/shaders/skybox/varying.def.sc @@ -0,0 +1,13 @@ +vec3 v_view : TEXCOORD0 = vec3(0.0, 0.0, 0.0); +vec4 v_shadowcoord : TEXCOORD1 = vec4(0.0, 0.0, 0.0, 0.0); +vec4 v_position : TEXCOORD2 = vec4(0.0, 0.0, 0.0, 0.0); +vec3 v_dir : TEXCOORD3 = vec3(0.0, 0.0, 0.0); +vec2 v_texcoord0 : TEXCOORD4 = vec2(0.0, 0.0); +vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_normal : NORMAL; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; + diff --git a/shaders/src/vs_ibl_skybox.sc b/shaders/skybox/vs_ibl_skybox.sc similarity index 100% rename from shaders/src/vs_ibl_skybox.sc rename to shaders/skybox/vs_ibl_skybox.sc diff --git a/shaders/src/vs_sms_shadow_pd.sc b/shaders/src/vs_sms_shadow_pd.sc deleted file mode 100644 index ca364dd..0000000 --- a/shaders/src/vs_sms_shadow_pd.sc +++ /dev/null @@ -1,15 +0,0 @@ -$input a_position -$output v_position - -/* - * Copyright 2013-2014 Dario Manesku. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include "../common/common.sh" - -void main() -{ - gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); - v_position = gl_Position; -} diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 6d5b88e..e991f40 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -1050,7 +1050,7 @@ void Renderer::setupShaders() { memcpy(IBL::uniforms.m_lightDir, IBL::settings.m_lightDir, 3*sizeof(float) ); memcpy(IBL::uniforms.m_lightCol, IBL::settings.m_lightCol, 3*sizeof(float) ); - s_renderStates[RenderState::Skybox].m_program = RenderProgram("shaders/src/vs_ibl_skybox.sc", "shaders/src/fs_ibl_skybox.sc"); + s_renderStates[RenderState::Skybox].m_program = RenderProgram("shaders/skybox/vs_ibl_skybox.sc", "shaders/skybox/fs_ibl_skybox.sc"); // Get renderer capabilities info. const bgfx::Caps* caps = bgfx::getCaps(); @@ -1061,9 +1061,9 @@ void Renderer::setupShaders() { if (shadowSamplerSupported) { // Depth textures and shadow samplers are supported. - s_renderStates[RenderState::ShadowMap].m_program = RenderProgram("shaders/src/vs_sms_mesh.sc", "shaders/src/fs_sms_shadow.sc"); - s_renderStates[RenderState::Scene].m_program = RenderProgram("shaders/src/vs_sms_mesh.sc", "shaders/src/fs_sms_mesh.sc"); - s_renderStates[RenderState::SceneTextured].m_program = RenderProgram("shaders/src/vs_sms_mesh_textured.sc", "shaders/src/fs_sms_mesh_textured.sc"); + s_renderStates[RenderState::ShadowMap].m_program = RenderProgram("shaders/shadowmap/vs_sms_mesh.sc", "shaders/shadowmap/fs_sms_shadow.sc"); + s_renderStates[RenderState::Scene].m_program = RenderProgram("shaders/scene/vs_sms_mesh.sc", "shaders/scene/fs_sms_mesh.sc"); + s_renderStates[RenderState::SceneTextured].m_program = RenderProgram("shaders/scene/vs_sms_mesh_textured.sc", "shaders/scene/fs_sms_mesh_textured.sc"); lights[0].shadowMapTexture= bgfx::createTexture2D(lights[0].shadowMapSize, lights[0].shadowMapSize, false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_COMPARE_LEQUAL); bgfx::TextureHandle fbtextures[] = { lights[0].shadowMapTexture }; @@ -1073,24 +1073,14 @@ void Renderer::setupShaders() { { // Depth textures and shadow samplers are not supported. Use float // depth packing into color buffer instead. - s_renderStates[RenderState::ShadowMap].m_program.program = bgfxutils::loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd"); - s_renderStates[RenderState::Scene].m_program.program = bgfxutils::loadProgram("vs_sms_mesh", "fs_sms_mesh_pd"); - s_renderStates[RenderState::SceneTextured].m_program.program = bgfxutils::loadProgram("vs_sms_mesh_textured", "fs_sms_mesh_pd_textured"); - - lights[0].shadowMapTexture = bgfx::createTexture2D(lights[0].shadowMapSize, lights[0].shadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT); - bgfx::TextureHandle fbtextures[] = - { - lights[0].shadowMapTexture, - bgfx::createTexture2D(lights[0].shadowMapSize, lights[0].shadowMapSize, false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY), - }; - lights[0].shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); + assert(false); } s_renderStates[RenderState::Lines].m_program = RenderProgram("shaders/lines/vs_lines.sc", "shaders/lines/fs_lines.sc"); s_renderStates[RenderState::LinesOccluded].m_program = RenderProgram("shaders/lines/vs_lines.sc", "shaders/lines/fs_lines_occluded.sc"); - s_renderStates[RenderState::Debug].m_program = RenderProgram("shaders/src/vs_debug.sc", "shaders/src/fs_debug.sc"); + s_renderStates[RenderState::Debug].m_program = RenderProgram("shaders/debug/vs_debug.sc", "shaders/debug/fs_debug.sc"); } void Renderer::setupRenderPasses() { @@ -1689,9 +1679,6 @@ void Renderer::paintGL() { // process submitted rendering primitives. bgfx::frame(); -// ImGui::SetNextWindowSize (ImVec2(400.f, 300.0f), ImGuiSetCond_Once); -// ImGui::SetNextWindowPos (ImVec2(10.f, 300.0f), ImGuiSetCond_Once); -// if (ImGui::BeginDock("Render Settings")) { if(ImGui::DragFloat3 ("Light0 Pos", lights[0].pos.data(), 1.0f, -10.0f, 10.0f)) { }