Files
armorpaint/base/shaders/std/deferred_light.glsl
T

101 lines
2.6 KiB
GLSL
Raw Normal View History

2023-02-15 19:56:40 +01:00
#ifndef _DEFERRED_LIGHT_GLSL_
#define _DEFERRED_LIGHT_GLSL_
2023-02-21 14:06:58 +01:00
#include "std/gbuffer.glsl"
#include "std/light.glsl"
#include "std/shirr.glsl"
2023-02-15 19:56:40 +01:00
#ifdef _Voxel
2023-02-21 14:06:58 +01:00
#include "std/conetrace.glsl"
2023-02-15 19:56:40 +01:00
#endif
uniform sampler2D gbufferD;
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
#ifdef _Voxel
uniform sampler3D voxels;
#endif
2024-08-15 20:29:14 +02:00
uniform vec4 envmap_data; // angle, sin(angle), cos(angle), strength
2023-02-15 19:56:40 +01:00
uniform vec4 shirr[7];
2024-08-15 20:29:14 +02:00
uniform sampler2D senvmap_brdf;
uniform sampler2D senvmap_radiance;
2023-02-15 19:56:40 +01:00
#ifdef SPIRV
2024-08-15 20:29:14 +02:00
uniform float envmap_num_mipmaps;
2023-02-15 19:56:40 +01:00
#else
2024-08-15 20:29:14 +02:00
uniform int envmap_num_mipmaps;
2023-02-15 19:56:40 +01:00
#endif
uniform sampler2D ssaotex;
2024-08-15 20:29:14 +02:00
uniform vec2 camera_proj;
2023-02-15 19:56:40 +01:00
uniform vec3 eye;
2024-08-15 20:29:14 +02:00
uniform vec3 eye_look;
uniform vec3 point_pos;
uniform vec3 point_col;
2023-02-15 19:56:40 +01:00
2024-08-15 20:29:14 +02:00
in vec2 tex_coord;
in vec3 view_ray;
out vec4 frag_color;
2023-02-15 19:56:40 +01:00
void main() {
2024-08-15 20:29:14 +02:00
vec4 g0 = textureLod(gbuffer0, tex_coord, 0.0); // Normal.xy, roughness, metallic/matid
2023-02-15 19:56:40 +01:00
vec3 n;
n.z = 1.0 - abs(g0.x) - abs(g0.y);
2024-08-15 20:29:14 +02:00
n.xy = n.z >= 0.0 ? g0.xy : octahedron_wrap(g0.xy);
2023-02-15 19:56:40 +01:00
n = normalize(n);
float roughness = g0.b;
float metallic;
uint matid;
2024-08-15 20:29:14 +02:00
unpack_f32_i16(g0.a, metallic, matid);
vec4 g1 = textureLod(gbuffer1, tex_coord, 0.0); // Basecolor.rgb, occ
2023-02-15 19:56:40 +01:00
float occ = g1.a;
2024-08-15 20:29:14 +02:00
vec3 albedo = surface_albedo(g1.rgb, metallic); // g1.rgb - basecolor
vec3 f0 = surface_f0(g1.rgb, metallic);
2023-02-15 19:56:40 +01:00
2024-08-15 20:29:14 +02:00
float depth = textureLod(gbufferD, tex_coord, 0.0).r * 2.0 - 1.0;
vec3 p = get_pos(eye, eye_look, normalize(view_ray), depth, camera_proj);
2023-02-15 19:56:40 +01:00
vec3 v = normalize(eye - p);
2024-08-15 20:29:14 +02:00
float dotnv = max(0.0, dot(n, v));
2023-02-15 19:56:40 +01:00
2024-08-15 20:29:14 +02:00
occ = mix(1.0, occ, dotnv); // AO Fresnel
2023-02-15 19:56:40 +01:00
2024-08-15 20:29:14 +02:00
vec2 env_brdf = texelFetch(senvmap_brdf, ivec2(vec2(roughness, 1.0 - dotnv) * 256.0), 0).xy;
2023-02-15 19:56:40 +01:00
// Envmap
2024-08-15 20:29:14 +02:00
vec3 envl = sh_irradiance(vec3(n.x * envmap_data.z - n.y * envmap_data.y, n.x * envmap_data.y + n.y * envmap_data.z, n.z), shirr);
2023-02-15 19:56:40 +01:00
envl /= PI;
2024-08-15 20:29:14 +02:00
vec3 reflection_world = reflect(-v, n);
float lod = mip_from_roughness(roughness, envmap_num_mipmaps);
vec3 prefiltered_color = textureLod(senvmap_radiance, envmap_equirect(reflection_world, envmap_data.x), lod).rgb;
2023-02-15 19:56:40 +01:00
envl.rgb *= albedo;
// Indirect specular
2024-08-15 20:29:14 +02:00
envl.rgb += prefiltered_color * (f0 * env_brdf.x + env_brdf.y) * 1.5;
2023-02-15 19:56:40 +01:00
2024-08-15 20:29:14 +02:00
envl.rgb *= envmap_data.w * occ;
2023-02-15 19:56:40 +01:00
#ifdef _Voxel
2024-08-15 20:29:14 +02:00
vec3 voxpos = p / voxelgi_half_extents;
envl.rgb *= 1.0 - trace_ao(voxpos, n, voxels);
2023-02-15 19:56:40 +01:00
#endif
2024-08-15 20:29:14 +02:00
frag_color.rgb = envl;
frag_color.rgb *= textureLod(ssaotex, tex_coord, 0.0).r;
2023-02-15 19:56:40 +01:00
if (matid == 1) { // Emission
2024-08-15 20:29:14 +02:00
frag_color.rgb += g1.rgb; // materialid
2023-02-15 19:56:40 +01:00
albedo = vec3(0.0);
}
2024-08-15 20:29:14 +02:00
frag_color.rgb += sample_light(p, n, v, dotnv, point_pos, point_col, albedo, roughness, f0, occ
2023-02-15 19:56:40 +01:00
#ifdef _Voxel
, voxels, voxpos
#endif
);
2024-08-15 20:29:14 +02:00
frag_color.a = 1.0; // Mark as opaque
2023-02-15 19:56:40 +01:00
}
#endif