From 7958fc20dab153b1cdfcaccf38de563d61169787 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Wed, 15 Feb 2023 19:56:40 +0100 Subject: [PATCH] Shader cleanup --- base/Shaders/common/compositor_pass.frag.glsl | 15 +-- base/Shaders/common/deferred_light.frag.glsl | 113 +---------------- .../common/deferred_light_voxel.frag.glsl | 118 +----------------- base/Shaders/std/deferred_light.glsl | 101 +++++++++++++++ base/Shaders/std/light.glsl | 19 ++- 5 files changed, 116 insertions(+), 250 deletions(-) create mode 100644 base/Shaders/std/deferred_light.glsl diff --git a/base/Shaders/common/compositor_pass.frag.glsl b/base/Shaders/common/compositor_pass.frag.glsl index c15353b4..42aff1fd 100644 --- a/base/Shaders/common/compositor_pass.frag.glsl +++ b/base/Shaders/common/compositor_pass.frag.glsl @@ -1,11 +1,7 @@ #version 450 -// #define _AutoExposure - uniform sampler2D tex; -#ifdef _AutoExposure -uniform sampler2D histogram; -#endif +// uniform sampler2D histogram; uniform float vignetteStrength; @@ -27,11 +23,10 @@ void main() { fragColor.rgb *= (1.0 - vignetteStrength) + vignetteStrength * pow(16.0 * texCoord.x * texCoord.y * (1.0 - texCoord.x) * (1.0 - texCoord.y), 0.2); -#ifdef _AutoExposure - const float autoExposureStrength = 1.0; - float expo = 2.0 - clamp(length(textureLod(histogram, vec2(0.5, 0.5), 0).rgb), 0.0, 1.0); - fragColor.rgb *= pow(expo, autoExposureStrength * 2.0); -#endif + // Auto exposure + // const float autoExposureStrength = 1.0; + // float expo = 2.0 - clamp(length(textureLod(histogram, vec2(0.5, 0.5), 0).rgb), 0.0, 1.0); + // fragColor.rgb *= pow(expo, autoExposureStrength * 2.0); fragColor.rgb = tonemapFilmic(fragColor.rgb); // With gamma } diff --git a/base/Shaders/common/deferred_light.frag.glsl b/base/Shaders/common/deferred_light.frag.glsl index 784f04e0..e114d93b 100644 --- a/base/Shaders/common/deferred_light.frag.glsl +++ b/base/Shaders/common/deferred_light.frag.glsl @@ -1,114 +1,3 @@ #version 450 -#define _LTC -#define _VoxelShadow -#define _MicroShadowing -#define _SinglePoint -#define _SSAO -#define _Emission -#define _Rad -#define _Irr -#define _Brdf -#define _EnvTex - -#include "../std/gbuffer.glsl" -#include "../std/light.glsl" -#include "../std/shirr.glsl" -#ifdef _VoxelAOvar -#include "../std/conetrace.glsl" -#endif - -uniform sampler2D gbufferD; -uniform sampler2D gbuffer0; -uniform sampler2D gbuffer1; - -#ifdef _VoxelAOvar -uniform sampler3D voxels; -#endif - -uniform vec4 envmapData; // angle, sin(angle), cos(angle), strength -uniform vec4 shirr[7]; -uniform sampler2D senvmapBrdf; -uniform sampler2D senvmapRadiance; -#ifdef SPIRV -uniform float envmapNumMipmaps; -#else -uniform int envmapNumMipmaps; -#endif -uniform sampler2D ssaotex; - -uniform vec2 cameraProj; -uniform vec3 eye; -uniform vec3 eyeLook; - -uniform vec3 pointPos; -uniform vec3 pointCol; - -in vec2 texCoord; -in vec3 viewRay; -out vec4 fragColor; - -void main() { - vec4 g0 = textureLod(gbuffer0, texCoord, 0.0); // Normal.xy, roughness, metallic/matid - - vec3 n; - n.z = 1.0 - abs(g0.x) - abs(g0.y); - n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy); - n = normalize(n); - - float roughness = g0.b; - float metallic; - uint matid; - unpackFloatInt16(g0.a, metallic, matid); - vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, occ - float occ = g1.a; - vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor - vec3 f0 = surfaceF0(g1.rgb, metallic); - - float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0; - vec3 p = getPos(eye, eyeLook, normalize(viewRay), depth, cameraProj); - vec3 v = normalize(eye - p); - float dotNV = max(0.0, dot(n, v)); - - occ = mix(1.0, occ, dotNV); // AO Fresnel - - vec2 envBRDF = texelFetch(senvmapBrdf, ivec2(vec2(roughness, 1.0 - dotNV) * 256.0), 0).xy; - - // Envmap - vec4 envmapDataLocal = envmapData; // TODO: SPIRV workaround - vec3 envl = shIrradiance(vec3(n.x * envmapDataLocal.z - n.y * envmapDataLocal.y, n.x * envmapDataLocal.y + n.y * envmapDataLocal.z, n.z), shirr); - envl /= PI; - - vec3 reflectionWorld = reflect(-v, n); - float lod = getMipFromRoughness(roughness, envmapNumMipmaps); - vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld, envmapDataLocal.x), lod).rgb; - - envl.rgb *= albedo; - - // Indirect specular - envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y) * 1.5; - - envl.rgb *= envmapDataLocal.w * occ; - -#ifdef _VoxelAOvar - vec3 voxpos = p / voxelgiHalfExtents; - envl.rgb *= 1.0 - traceAO(voxpos, n, voxels); -#endif - - fragColor.rgb = envl; - fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r; - - if (matid == 1) { // Emission - fragColor.rgb += g1.rgb; // materialid - albedo = vec3(0.0); - } - - fragColor.rgb += sampleLight(p, n, v, dotNV, pointPos, pointCol, albedo, roughness, f0 - #ifdef _VoxelAOvar - #ifdef _VoxelShadow - , voxels, voxpos - #endif - #endif - , occ); - fragColor.a = 1.0; // Mark as opaque -} +#include "../std/deferred_light.glsl" diff --git a/base/Shaders/common/deferred_light_voxel.frag.glsl b/base/Shaders/common/deferred_light_voxel.frag.glsl index a8a7a93f..a9b0abaa 100644 --- a/base/Shaders/common/deferred_light_voxel.frag.glsl +++ b/base/Shaders/common/deferred_light_voxel.frag.glsl @@ -1,118 +1,4 @@ #version 450 -#define _VoxelAOvar +#define _Voxel -#define _LTC -#define _VoxelShadow -#define _MicroShadowing -#define _SinglePoint -#define _SSAO -#define _Emission -#define _Rad -#define _Irr -#define _Brdf -#define _EnvTex - -#include "../std/gbuffer.glsl" -#include "../std/light.glsl" -#include "../std/shirr.glsl" -#ifdef _VoxelAOvar -#include "../std/conetrace.glsl" -#endif - -uniform sampler2D gbufferD; -uniform sampler2D gbuffer0; -uniform sampler2D gbuffer1; - -#ifdef _VoxelAOvar -uniform sampler3D voxels; -#endif - -uniform vec4 envmapData; // angle, sin(angle), cos(angle), strength -uniform vec4 shirr[7]; -uniform sampler2D senvmapBrdf; -uniform sampler2D senvmapRadiance; -uniform int envmapNumMipmaps; -uniform sampler2D ssaotex; - -//!uniform vec3 lightArea0; -//!uniform vec3 lightArea1; -//!uniform vec3 lightArea2; -//!uniform vec3 lightArea3; -//!uniform sampler2D sltcMat; -//!uniform sampler2D sltcMag; - -uniform vec2 cameraProj; -uniform vec3 eye; -uniform vec3 eyeLook; - -uniform vec3 pointPos; -uniform vec3 pointCol; - -in vec2 texCoord; -in vec3 viewRay; -out vec4 fragColor; - -void main() { - vec4 g0 = textureLod(gbuffer0, texCoord, 0.0); // Normal.xy, roughness, metallic/matid - - vec3 n; - n.z = 1.0 - abs(g0.x) - abs(g0.y); - n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy); - n = normalize(n); - - float roughness = g0.b; - float metallic; - uint matid; - unpackFloatInt16(g0.a, metallic, matid); - vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, occ - float occ = g1.a; - vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor - vec3 f0 = surfaceF0(g1.rgb, metallic); - - float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0; - vec3 p = getPos(eye, eyeLook, normalize(viewRay), depth, cameraProj); - vec3 v = normalize(eye - p); - float dotNV = max(0.0, dot(n, v)); - - occ = mix(1.0, occ, dotNV); // AO Fresnel - - vec2 envBRDF = texelFetch(senvmapBrdf, ivec2(vec2(roughness, 1.0 - dotNV) * 256.0), 0).xy; - - // Envmap - vec4 envmapDataLocal = envmapData; // TODO: SPIRV workaround - vec3 envl = shIrradiance(vec3(n.x * envmapDataLocal.z - n.y * envmapDataLocal.y, n.x * envmapDataLocal.y + n.y * envmapDataLocal.z, n.z), shirr); - envl /= PI; - - vec3 reflectionWorld = reflect(-v, n); - float lod = getMipFromRoughness(roughness, envmapNumMipmaps); - vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld, envmapDataLocal.x), lod).rgb; - - envl.rgb *= albedo; - - // Indirect specular - envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y) * 1.5; - - envl.rgb *= envmapDataLocal.w * occ; - -#ifdef _VoxelAOvar - vec3 voxpos = p / voxelgiHalfExtents; - envl.rgb *= 1.0 - traceAO(voxpos, n, voxels); -#endif - - fragColor.rgb = envl; - fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r; - - if (g0.a == 1.0) { // Emission - fragColor.rgb += g1.rgb; // materialid - albedo = vec3(0.0); - } - - fragColor.rgb += sampleLight(p, n, v, dotNV, pointPos, pointCol, albedo, roughness, f0 - #ifdef _VoxelAOvar - #ifdef _VoxelShadow - , voxels, voxpos - #endif - #endif - , occ); - fragColor.a = 1.0; // Mark as opaque -} +#include "../std/deferred_light.glsl" diff --git a/base/Shaders/std/deferred_light.glsl b/base/Shaders/std/deferred_light.glsl new file mode 100644 index 00000000..ba061a50 --- /dev/null +++ b/base/Shaders/std/deferred_light.glsl @@ -0,0 +1,101 @@ + +#ifndef _DEFERRED_LIGHT_GLSL_ +#define _DEFERRED_LIGHT_GLSL_ + +#include "../std/gbuffer.glsl" +#include "../std/light.glsl" +#include "../std/shirr.glsl" +#ifdef _Voxel +#include "../std/conetrace.glsl" +#endif + +uniform sampler2D gbufferD; +uniform sampler2D gbuffer0; +uniform sampler2D gbuffer1; +#ifdef _Voxel +uniform sampler3D voxels; +#endif +uniform vec4 envmapData; // angle, sin(angle), cos(angle), strength +uniform vec4 shirr[7]; +uniform sampler2D senvmapBrdf; +uniform sampler2D senvmapRadiance; +#ifdef SPIRV +uniform float envmapNumMipmaps; +#else +uniform int envmapNumMipmaps; +#endif +uniform sampler2D ssaotex; +uniform vec2 cameraProj; +uniform vec3 eye; +uniform vec3 eyeLook; +uniform vec3 pointPos; +uniform vec3 pointCol; + +in vec2 texCoord; +in vec3 viewRay; +out vec4 fragColor; + +void main() { + vec4 g0 = textureLod(gbuffer0, texCoord, 0.0); // Normal.xy, roughness, metallic/matid + + vec3 n; + n.z = 1.0 - abs(g0.x) - abs(g0.y); + n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy); + n = normalize(n); + + float roughness = g0.b; + float metallic; + uint matid; + unpackFloatInt16(g0.a, metallic, matid); + vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, occ + float occ = g1.a; + vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor + vec3 f0 = surfaceF0(g1.rgb, metallic); + + float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0; + vec3 p = getPos(eye, eyeLook, normalize(viewRay), depth, cameraProj); + vec3 v = normalize(eye - p); + float dotNV = max(0.0, dot(n, v)); + + occ = mix(1.0, occ, dotNV); // AO Fresnel + + vec2 envBRDF = texelFetch(senvmapBrdf, ivec2(vec2(roughness, 1.0 - dotNV) * 256.0), 0).xy; + + // Envmap + vec4 envmapDataLocal = envmapData; // TODO: SPIRV workaround + vec3 envl = shIrradiance(vec3(n.x * envmapDataLocal.z - n.y * envmapDataLocal.y, n.x * envmapDataLocal.y + n.y * envmapDataLocal.z, n.z), shirr); + envl /= PI; + + vec3 reflectionWorld = reflect(-v, n); + float lod = getMipFromRoughness(roughness, envmapNumMipmaps); + vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld, envmapDataLocal.x), lod).rgb; + + envl.rgb *= albedo; + + // Indirect specular + envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y) * 1.5; + + envl.rgb *= envmapDataLocal.w * occ; + +#ifdef _Voxel + vec3 voxpos = p / voxelgiHalfExtents; + envl.rgb *= 1.0 - traceAO(voxpos, n, voxels); +#endif + + fragColor.rgb = envl; + fragColor.rgb *= textureLod(ssaotex, texCoord, 0.0).r; + + if (matid == 1) { // Emission + fragColor.rgb += g1.rgb; // materialid + albedo = vec3(0.0); + } + + fragColor.rgb += sampleLight(p, n, v, dotNV, pointPos, pointCol, albedo, roughness, f0, occ +#ifdef _Voxel + , voxels, voxpos +#endif + ); + fragColor.a = 1.0; // Mark as opaque +} + +#endif diff --git a/base/Shaders/std/light.glsl b/base/Shaders/std/light.glsl index 2fd76bc9..c0ae77cd 100644 --- a/base/Shaders/std/light.glsl +++ b/base/Shaders/std/light.glsl @@ -4,10 +4,10 @@ #include "../std/brdf.glsl" #include "../std/math.glsl" -#ifdef _VoxelAOvar +#include "../std/ltc.glsl" +#ifdef _Voxel #include "../std/conetrace.glsl" #endif -#include "../std/ltc.glsl" uniform vec3 lightArea0; uniform vec3 lightArea1; @@ -17,13 +17,10 @@ uniform sampler2D sltcMat; uniform sampler2D sltcMag; vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol, - const vec3 albedo, const float rough, const vec3 f0 - #ifdef _VoxelAOvar - #ifdef _VoxelShadow - , sampler3D voxels, vec3 voxpos - #endif - #endif - , float occ + const vec3 albedo, const float rough, const vec3 f0, const float occ +#ifdef _Voxel + , sampler3D voxels, vec3 voxpos +#endif ) { vec3 ld = lp - p; vec3 l = normalize(ld); @@ -46,11 +43,9 @@ vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co direct *= lightCol; direct *= clamp(dotNL + 2.0 * occ * occ - 1.0, 0.0, 1.0); // Micro shadowing - #ifdef _VoxelAOvar - #ifdef _VoxelShadow + #ifdef _Voxel direct *= 1.0 - traceShadow(voxels, voxpos, l); #endif - #endif return direct; }