armory/Shaders/deferred_indirect/deferred_indirect.frag.glsl

206 lines
4.6 KiB
Plaintext
Raw Normal View History

2016-08-09 23:51:40 +02:00
#version 450
#include "compiled.inc"
2017-12-13 14:21:42 +01:00
#include "std/gbuffer.glsl"
#include "std/math.glsl"
#include "std/brdf.glsl"
2017-01-23 20:41:45 +01:00
#ifdef _Irr
2017-12-13 14:21:42 +01:00
#include "std/shirr.glsl"
2017-01-23 20:41:45 +01:00
#endif
2017-02-22 15:50:19 +01:00
#ifdef _VoxelGI
2017-12-13 14:21:42 +01:00
#include "std/conetrace.glsl"
2017-02-22 15:50:19 +01:00
#endif
2018-09-13 12:13:32 +02:00
#ifdef _VoxelAOvar
2017-12-13 14:21:42 +01:00
#include "std/conetrace.glsl"
2017-08-22 10:04:13 +02:00
#endif
2016-08-09 23:51:40 +02:00
2018-01-29 23:52:42 +01:00
// uniform sampler2D gbufferD;
2016-08-09 23:51:40 +02:00
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
2017-02-22 15:50:19 +01:00
#ifdef _VoxelGI
2018-01-29 23:52:42 +01:00
uniform sampler3D voxels;
2017-02-22 15:50:19 +01:00
#endif
2018-09-13 12:13:32 +02:00
#ifdef _VoxelAOvar
2018-01-29 23:52:42 +01:00
uniform sampler3D voxels;
2017-10-23 16:24:57 +02:00
#endif
2017-12-19 18:05:38 +01:00
#ifdef _VoxelGITemporal
2018-01-29 23:52:42 +01:00
uniform sampler3D voxelsLast;
2017-12-19 18:05:38 +01:00
uniform float voxelBlend;
#endif
2017-10-23 16:24:57 +02:00
#ifdef _VoxelGICam
2017-10-12 12:12:48 +02:00
uniform vec3 eyeSnap;
2017-08-22 10:04:13 +02:00
#endif
2017-02-22 15:50:19 +01:00
2016-08-09 23:51:40 +02:00
uniform float envmapStrength;
2017-01-23 20:41:45 +01:00
#ifdef _Irr
2017-02-07 11:50:21 +01:00
//!uniform vec4 shirr[7];
2017-01-23 20:41:45 +01:00
#endif
2018-01-29 19:06:57 +01:00
#ifdef _Brdf
uniform sampler2D senvmapBrdf;
#endif
2016-08-09 23:51:40 +02:00
#ifdef _Rad
uniform sampler2D senvmapRadiance;
uniform int envmapNumMipmaps;
#endif
2017-10-22 20:52:39 +02:00
#ifdef _EnvCol
uniform vec3 backgroundCol;
#endif
2016-08-09 23:51:40 +02:00
#ifdef _SSAO
uniform sampler2D ssaotex;
#endif
2018-01-29 19:06:57 +01:00
#ifdef _IndPos
uniform vec2 cameraProj;
2016-08-09 23:51:40 +02:00
uniform vec3 eye;
uniform vec3 eyeLook;
#endif
in vec2 texCoord;
2018-01-29 19:06:57 +01:00
#ifdef _IndPos
2016-08-09 23:51:40 +02:00
in vec3 viewRay;
#endif
2016-10-12 17:52:27 +02:00
out vec4 fragColor;
2016-08-09 23:51:40 +02:00
void main() {
2017-05-23 15:01:56 +02:00
vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, metallic/roughness, depth
2016-08-09 23:51:40 +02:00
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);
2016-08-29 09:56:34 +02:00
vec2 metrough = unpackFloat(g0.b);
2016-08-09 23:51:40 +02:00
2018-05-19 19:29:14 +02:00
vec4 g1 = texture(gbuffer1, texCoord); // Basecolor.rgb, spec/occ
2018-05-26 16:39:10 +02:00
vec2 occspec = unpackFloat2(g1.a);
2017-02-22 15:50:19 +01:00
vec3 albedo = surfaceAlbedo(g1.rgb, metrough.x); // g1.rgb - basecolor
2018-01-29 19:06:57 +01:00
#ifdef _IndPos
2018-01-29 23:52:42 +01:00
// #ifdef _InvY // D3D
// float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
// #else
2017-05-31 10:43:57 +02:00
float depth = (1.0 - g0.a) * 2.0 - 1.0;
2018-01-29 23:52:42 +01:00
// #endif
2017-11-04 18:35:34 +01:00
vec3 p = getPos(eye, eyeLook, viewRay, depth, cameraProj);
2018-01-29 19:06:57 +01:00
#endif
#ifdef _Brdf
2016-08-09 23:51:40 +02:00
vec3 v = normalize(eye - p.xyz);
2017-02-22 15:50:19 +01:00
float dotNV = max(dot(n, v), 0.0);
vec3 f0 = surfaceF0(g1.rgb, metrough.x);
vec2 envBRDF = texture(senvmapBrdf, vec2(metrough.y, 1.0 - dotNV)).xy;
2016-08-09 23:51:40 +02:00
#endif
2016-10-09 16:06:18 +02:00
2017-02-22 15:50:19 +01:00
#ifdef _VoxelGI
2017-08-03 14:01:04 +02:00
#ifdef _VoxelGICam
2017-10-12 12:12:48 +02:00
vec3 voxpos = (p - eyeSnap) / voxelgiHalfExtents;
2017-08-03 14:01:04 +02:00
#else
2017-10-12 12:12:48 +02:00
vec3 voxpos = p / voxelgiHalfExtents;
2017-08-03 14:01:04 +02:00
#endif
2017-10-12 12:12:48 +02:00
2017-12-19 18:05:38 +01:00
#ifdef _VoxelGITemporal
vec4 indirectDiffuse = traceDiffuse(voxpos, n, voxels) * voxelBlend + traceDiffuse(voxpos, n, voxelsLast) * (1.0 - voxelBlend);
#else
vec4 indirectDiffuse = traceDiffuse(voxpos, n, voxels);
#endif
2017-02-22 15:50:19 +01:00
2018-05-21 17:55:26 +02:00
fragColor.rgb = indirectDiffuse.rgb * voxelgiDiff * g1.rgb;
2018-05-26 16:39:10 +02:00
if (occspec.y > 0.0) {
2018-05-21 17:55:26 +02:00
vec3 indirectSpecular = traceSpecular(voxels, voxpos, n, v, metrough.y);
indirectSpecular *= f0 * envBRDF.x + envBRDF.y;
2018-05-26 16:39:10 +02:00
fragColor.rgb += indirectSpecular * voxelgiSpec * occspec.y;
2018-05-21 17:55:26 +02:00
}
2017-05-17 23:02:36 +02:00
2017-10-12 12:12:48 +02:00
// if (!isInsideCube(voxpos)) fragColor = vec4(1.0); // Show bounds
2018-05-19 19:29:14 +02:00
// float opacity = g2.b;
2017-10-12 12:12:48 +02:00
// if (opacity < 1.0) fragColor.rgb = mix(indirectRefractiveLight(-v, n, vec3(1.0), opacity, voxpos), fragColor.rgb, opacity);
2017-02-22 15:50:19 +01:00
#endif
2017-08-02 11:46:54 +02:00
2017-02-22 15:50:19 +01:00
// Envmap
2017-01-23 20:41:45 +01:00
#ifdef _Irr
2017-09-25 10:16:34 +02:00
vec3 envl = shIrradiance(n);
#ifdef _EnvTex
envl /= PI;
#endif
2017-01-23 20:41:45 +01:00
#else
2017-03-12 17:29:22 +01:00
vec3 envl = vec3(1.0);
2017-01-23 20:41:45 +01:00
#endif
2017-01-02 22:46:18 +01:00
#ifdef _Rad
vec3 reflectionWorld = reflect(-v, n);
float lod = getMipFromRoughness(metrough.y, envmapNumMipmaps);
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
2016-08-09 23:51:40 +02:00
#endif
#ifdef _EnvLDR
2017-03-12 17:29:22 +01:00
envl.rgb = pow(envl.rgb, vec3(2.2));
2016-08-09 23:51:40 +02:00
#ifdef _Rad
prefilteredColor = pow(prefilteredColor, vec3(2.2));
#endif
#endif
2017-03-12 17:29:22 +01:00
envl.rgb *= albedo;
2016-08-09 23:51:40 +02:00
2017-02-22 15:50:19 +01:00
#ifdef _Rad // Indirect specular
2018-05-26 16:39:10 +02:00
envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y) * 1.5 * occspec.y;
2017-10-22 20:52:39 +02:00
#else
#ifdef _EnvCol
2018-01-30 10:17:07 +01:00
envl.rgb += backgroundCol * surfaceF0(g1.rgb, metrough.x); // f0
2017-10-22 20:52:39 +02:00
#endif
2016-08-09 23:51:40 +02:00
#endif
2017-05-23 15:01:56 +02:00
#ifdef _SSS
2018-05-26 16:39:10 +02:00
envl.rgb *= envmapStrength * occspec.x;
2017-05-23 15:01:56 +02:00
#else
2017-08-13 20:28:06 +02:00
#ifndef _VoxelGIRefract
2018-05-26 16:39:10 +02:00
envl.rgb *= envmapStrength * occspec.x; // Occlusion
2017-08-13 20:28:06 +02:00
#endif
2017-05-23 15:01:56 +02:00
#endif
2016-08-09 23:51:40 +02:00
2018-09-13 12:13:32 +02:00
#ifdef _VoxelAOvar
2017-10-12 12:12:48 +02:00
#ifdef _VoxelGICam
vec3 voxpos = (p - eyeSnap) / voxelgiHalfExtents;
#else
vec3 voxpos = p / voxelgiHalfExtents;
#endif
2017-12-19 18:05:38 +01:00
#ifdef _VoxelGITemporal
envl.rgb *= 1.0 - (traceAO(voxpos, n, voxels) * voxelBlend + traceAO(voxpos, n, voxelsLast) * (1.0 - voxelBlend));
#else
envl.rgb *= 1.0 - traceAO(voxpos, n, voxels);
#endif
2017-08-22 10:04:13 +02:00
#endif
2017-03-12 17:29:22 +01:00
#ifdef _VoxelGI
2017-08-06 17:36:25 +02:00
fragColor.rgb += envl * voxelgiEnv;
2017-03-12 17:29:22 +01:00
#else
fragColor.rgb = envl;
2016-08-09 23:51:40 +02:00
#endif
2017-10-12 12:12:48 +02:00
2017-11-04 16:16:07 +01:00
#ifdef _SSAO
#ifdef _RTGI
fragColor.rgb *= texture(ssaotex, texCoord).rgb;
#else
fragColor.rgb *= texture(ssaotex, texCoord).r;
#endif
#endif
2017-10-12 12:12:48 +02:00
// Show voxels
// vec3 origin = vec3(texCoord * 2.0 - 1.0, 0.99);
// vec3 direction = vec3(0.0, 0.0, -1.0);
// vec4 color = vec4(0.0f);
// for(uint step = 0; step < 400 && color.a < 0.99f; ++step) {
// vec3 point = origin + 0.005 * step * direction;
// color += (1.0f - color.a) * textureLod(voxels, point * 0.5 + 0.5, 0);
// }
// fragColor.rgb += color.rgb;
2017-11-04 16:16:07 +01:00
// Show SSAO
// fragColor.rgb = texture(ssaotex, texCoord).rrr;
2016-08-09 23:51:40 +02:00
}