Files
HydraV3/HydraEngine/source/ShaderFiles/deferred_output_shader.frag
T
2026-08-17 21:44:00 +01:00

635 lines
17 KiB
GLSL

//*PIXEL*
#version 460 core
#extension GL_ARB_bindless_texture : require
#extension GL_ARB_gpu_shader5 : enable
uint INVALID_HYDRA_ID = 4294967295;
uint DEPTH_TEXTURE_ID = 0;
uint DIFFUSE_TEXTURE_ID = 1;
uint METAL_ROUGH_TEXTURE_ID = 2;
uint NORMAL_TEXTURE_ID = 3;
uint POSITION_TEXTIURE_ID = 4;
uint HYDRA_LIGHT_DIRECTIONAL = 1;
uint HYDRA_LIGHT_POINT = 2;
uint HYDRA_LIGHT_SPOT = 3;
struct LightStructure
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float intensity;
float constant;
float linear;
float quadratic;
float cutoff;
float cutoff_outer;
uint entity_id;
uint light_type;
uint shadow_map_id;
uint shadow_caster;
uint padding0;
uint padding1;
};
struct DirectionalShadowMapSet
{
uint entity_id;
uint shadow_id;
uint shadow_texture_id;
uint frame_buffer_id;
float near_plane[8];
float far_plane[8];
mat4 light_space_proj[6];
mat4 light_space_view[6];
};
struct SpotShadowMapSet
{
uint entity_id;
uint shadow_id;
uint shadow_texture_id;
uint frame_buffer_id;
float far_plane;
uint padding[3];
mat4 light_space_proj;
mat4 light_space_view;
};
struct PointShadowMapSet
{
uint entity_id;
uint shadow_id;
uint shadow_texture_id;
uint frame_buffer_id;
float near_plane;
float far_plane;
uint padding[2];
mat4 light_space_proj;
mat4 light_space_view[6];
};
uniform samplerCube depth_map;
layout(binding = 0) uniform uniform_per_frame
{
mat4 view;
mat4 proj;
vec3 viewer_pos;
uint light_count;
} ubo_per_frame;
layout(binding = 1) uniform uniform_output_textures
{
uint texture_ids[16];
};
layout(std430, binding = 1) readonly buffer positions_buffer
{
mat4 model_matrix[];
};
layout( std430, binding = 3) readonly buffer texture_buffer {
uvec2 textures[1000];
};
layout (std430, binding = 4) readonly buffer light_buffer
{
LightStructure lights[500];
};
layout (std430, binding = 5) readonly buffer directional_shadow_map_sets
{
DirectionalShadowMapSet directional_shadows[];
};
layout (std430, binding = 6) readonly buffer spot_shadow_map_sets
{
SpotShadowMapSet spot_shadows[];
};
layout (std430, binding = 7) readonly buffer point_shadow_map_sets
{
PointShadowMapSet point_shadows[];
};
layout (location=0) in VS_OUT
{
vec2 vsUV;
}vs_out;
layout (location=0) out vec4 uFragColor;
const float PI = 3.14159265358979323846;
const float M_INV_PI = 0.31830988618379067153776752674503;
float sqr(float x) { return x*x; }
// ----------------------------------------------------------------------------
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
}
// ----------------------------------------------------------------------------
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
// ----------------------------------------------------------------------------
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
// ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
vec3 CalculatePBR(vec3 normal, vec3 view_pos, vec3 world_pos,vec3 light_dir, vec3 diffuse, vec3 radiance, float roughness, float metallic )
{
vec3 N = normalize(normal);
vec3 V = normalize(view_pos - world_pos);
// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
vec3 F0 = vec3(0.04);
F0 = mix(F0, diffuse, metallic);
// calculate per-light radiance
vec3 L = normalize(light_dir);
vec3 H = normalize(V + L);
// Cook-Torrance BRDF
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = numerator / denominator;
// kS is equal to Fresnel
vec3 kS = F;
// for energy conservation, the diffuse and specular light can't
// be above 1.0 (unless the surface emits light); to preserve this
// relationship the diffuse component (kD) should equal 1.0 - kS.
vec3 kD = vec3(1.0) - kS;
// multiply kD by the inverse metalness such that only non-metals
// have diffuse lighting, or a linear blend if partly metal (pure metals
// have no diffuse light).
kD *= 1.0 - metallic;
// scale light by NdotL
float NdotL = max(dot(N, L), 0.0);
// add to outgoing radiance Lo
vec3 Lo = (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
return Lo;
}
vec3 DirectionalShading(vec3 normal,
vec3 diffuse,
float roughness,
float metallic,
float light_intensity,
vec3 light_dir,
vec3 light_pos,
vec3 world_pos,
vec3 view_pos)
{
float distance = length(light_pos - world_pos);
float attenuation = 1.0;
vec3 radiance = diffuse * attenuation;
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
return Lo;
}
vec3 PointShading(vec3 normal,
vec3 diffuse,
float roughness,
float metallic,
float light_intensity,
vec3 light_pos,
float light_constant,
float light_linear,
float light_quadratic,
vec3 world_pos,
vec3 view_pos,
float shadow_val)
{
float distance = length(light_pos - world_pos);
float attenuation = 1.0;
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
vec3 radiance = diffuse * attenuation * (1.0-shadow_val);
vec3 light_dir = normalize(light_pos - world_pos);
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
return Lo;
}
vec3 SpotShading(vec3 normal,
vec3 diffuse,
float roughness,
float metallic,
float light_intensity,
vec3 light_dir,
vec3 light_pos,
float cutoff,
float cutoff_outer,
float light_constant,
float light_linear,
float light_quadratic,
vec3 world_pos,
vec3 view_pos,
float shadow_val)
{
vec3 direction_to_light = normalize(light_pos - world_pos);
float theta = dot(direction_to_light, light_dir);
float falloff = 0.0f;
float epsilon = cutoff - cutoff_outer;
falloff = clamp((theta - cutoff_outer) / epsilon, 0.0, 1.0);
float distance = length(light_pos - world_pos);
float attenuation = 1.0;
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
vec3 radiance = diffuse * attenuation * falloff * (1.0-shadow_val);
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, direction_to_light, diffuse, radiance, roughness, metallic);
return Lo;// * shadow;
}
mat3 GetRotationOnlyMatrix(mat4 model_mat)
{
mat3 rot_mat;
rot_mat[0] = model_mat[0].xyz;
rot_mat[1] = model_mat[1].xyz;
rot_mat[2] = model_mat[2].xyz;
return rot_mat;
}
float SimpleSampleShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint shadow_trans_idx)
{
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
frag_light_space = (frag_light_space * 0.5) + 0.5;
float shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy, shadow_trans_idx)).r;
// return frag_light_space.z - shadow_map_depth;
//return shadow_map_depth ;
//return frag_light_space.z;
if(frag_light_space.z > shadow_map_depth)
{
return 0;
}
return 1;
}
float SampleCascadeShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint shadow_trans_idx, vec3 normal, vec3 light_dir)
{
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
frag_light_space = (frag_light_space * 0.5) + 0.5;
float shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy, shadow_trans_idx)).r;
// calculate bias (based on depth map resolution and slope)
float mapped_distance = (shadow_trans_idx*2);
float bias = 0.0f;//max(0.0001 * (1.0 - dot(normal, light_dir)), 0.00001);
// check whether current frag pos is in shadow
// float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// PCF
if(shadow_trans_idx < 3)
{
bias = 0.00001;
}
else if(shadow_trans_idx == 3)
{
bias = 0.0001;// + (0.000001 * mapped_distance);
}
else
{
bias = 0.0005;
}
float shadow = 0.0;
vec2 texel_size = 1.0 / textureSize(sampler_shadow_map, 0).xy;
if((frag_light_space.x >= 0.0)&&(frag_light_space.y >= 0.0)
&&(frag_light_space.x <= 1.0)&&(frag_light_space.y <= 1.0))
{
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy + vec2(x, y) * texel_size, shadow_trans_idx)).r;
shadow += (frag_light_space.z - bias) > shadow_map_depth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
}
// keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
if(frag_light_space.z > 1.0)
shadow = 0.0;
return shadow;
}
float SampleSingleShadow(vec4 frag_clip_space, sampler2D sampler_shadow_map, vec3 normal, vec3 light_dir)
{
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
frag_light_space = (frag_light_space * 0.5) + 0.5;
float shadow_map_depth = texture(sampler_shadow_map, frag_light_space.xy).r;
// calculate bias (based on depth map resolution and slope)
float bias = max(0.00005 * (1.0 - dot(normal, light_dir)), 0.000005);
// check whether current frag pos is in shadow
// float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// PCF
//bias = 0;
float shadow = 0.0;
vec2 texel_size = 1.0 / textureSize(sampler_shadow_map, 0).xy;
if((frag_light_space.x >= 0.0)&&(frag_light_space.y >= 0.0)
&&(frag_light_space.x <= 1.0)&&(frag_light_space.y <= 1.0))
{
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
shadow_map_depth = texture(sampler_shadow_map, frag_light_space.xy + vec2(x, y) * texel_size).r;
shadow += frag_light_space.z - bias > shadow_map_depth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
}
// keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
if(frag_light_space.z > 1.0)
shadow = 0.0;
return shadow;
}
vec3 CalculateDirectionalShadow(vec4 world_pos, uint shadow_idx, vec3 normal, vec3 light_dir, vec3 viewer_pos)
{
float shadow = 1;
//measure the distance of the fragment from the light
float frag_distance = abs(length(viewer_pos - world_pos.xyz));
uint shadow_trans_idx = 5;
uint ignore_shadow = 0;
//Work out which shadowmap is appropriate for this distance
for(uint i = 0; i < 6; i++)
{
if(frag_distance <= directional_shadows[shadow_idx].far_plane[i])
{
//record the shadow map level
shadow_trans_idx = i;
//and indicate that this has a level
ignore_shadow = 1;
break;
}
}
if(ignore_shadow == 1)
{
vec4 translated_pixel = directional_shadows[shadow_idx].light_space_proj[shadow_trans_idx] *
directional_shadows[shadow_idx].light_space_view[shadow_trans_idx] *
world_pos;
uvec2 shadow_handle = textures[directional_shadows[shadow_idx].shadow_texture_id];
shadow = 1.0 - SampleCascadeShadow(translated_pixel, sampler2DArray(shadow_handle), shadow_trans_idx, normal, light_dir);
}
return vec3(shadow);
if(shadow_trans_idx == 0)
{
return vec3(1,0,0);
}
if(shadow_trans_idx == 1)
{
return vec3(0,1,0);
}
if(shadow_trans_idx == 2)
{
return vec3(0,0,1);
}
if(shadow_trans_idx == 3)
{
return vec3(1,1,0);
}
if(shadow_trans_idx == 4)
{
return vec3(1,0,1);
}
if(shadow_trans_idx == 5)
{
return vec3(0,1,1);
}
}
float CalculateSpotShadow(vec4 world_pos, uint shadow_idx, vec3 normal, vec3 light_dir, vec3 viewer_pos)
{
vec4 translated_pixel = spot_shadows[shadow_idx].light_space_proj *
spot_shadows[shadow_idx].light_space_view *
world_pos;
uvec2 shadow_handle = textures[spot_shadows[shadow_idx].shadow_texture_id];
float shadow =(SampleSingleShadow(translated_pixel, sampler2D(shadow_handle), normal, light_dir));
return shadow;
}
float SampleCubemapShadow(vec3 frag_to_light, float far_plane, float view_distance, samplerCube sampler_shadow_map)
{
vec3 sample_offset_directions[20] = vec3[]
(
vec3( 1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
vec3( 1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
vec3( 1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3( 1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1),
vec3( 0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1)
);
float shadow = 0.0;
float bias = 0.05;
int samples = 20;
float disk_radius = (1.0 + (view_distance / far_plane)) / 50.0;
float frag_depth = length(frag_to_light);
for(int i = 0; i < samples; ++i)
{
float closest_depth = texture(sampler_shadow_map, frag_to_light + sample_offset_directions[i] * disk_radius).r;
closest_depth *= far_plane; // undo mapping [0;1]
if(frag_depth - bias > closest_depth)
shadow += 1.0;
}
shadow /= float(samples);
return shadow;
}
float CalculatePointShadow(vec3 world_pos, uint shadow_idx, float view_distance, vec3 light_position)
{
vec3 frag_to_light = light_position - world_pos;
uvec2 shadow_handle = textures[point_shadows[shadow_idx].shadow_texture_id];
float far_plane = point_shadows[shadow_idx].far_plane;
return SampleCubemapShadow(frag_to_light, far_plane, view_distance, samplerCube(shadow_handle));
}
void main()
{
uvec2 diffuse_handle = textures[texture_ids[DIFFUSE_TEXTURE_ID]];
uvec2 normal_handle = textures[texture_ids[NORMAL_TEXTURE_ID]];
uvec2 metal_rough_handle = textures[texture_ids[METAL_ROUGH_TEXTURE_ID]];
uvec2 world_pos_handle = textures[texture_ids[POSITION_TEXTIURE_ID]];
vec4 diffuse = texture(sampler2D(diffuse_handle), vs_out.vsUV.xy);
if(diffuse[3] <= 0.1)
{
discard;
}
vec3 normal = texture(sampler2D(normal_handle), vs_out.vsUV.xy).xyz;
vec3 metal_rough = texture(sampler2D(metal_rough_handle), vs_out.vsUV.xy).xyz;
vec3 world_pos = texture(sampler2D(world_pos_handle), vs_out.vsUV.xy).xyz;
float rough = metal_rough[1];
float metal = metal_rough[2];
float ao = 1;
vec3 ambient = vec3(0.05) * diffuse.xyz * ao;
vec3 Lo = ambient;
for(int i = 0; i < ubo_per_frame.light_count; i ++)
{
mat4 model = model_matrix[lights[i].entity_id];
mat3 rot_mat = GetRotationOnlyMatrix(model);
vec3 light_direction = normalize(rot_mat * vec3(0,0,1));
vec3 light_pos = model[3].xyz;
if(lights[i].light_type == HYDRA_LIGHT_DIRECTIONAL)
{
Lo += DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].intensity, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
Lo *= lights[i].diffuse.xyz;
if(lights[i].shadow_caster == 1)
{
vec3 shadow = CalculateDirectionalShadow(vec4(world_pos, 1.0), lights[i].shadow_map_id, normal, light_direction, ubo_per_frame.viewer_pos);
Lo = Lo * shadow;
}
}
else if(lights[i].light_type == HYDRA_LIGHT_POINT)
{
float shadow = 0;
if(lights[i].shadow_caster == 1)
{
float view_to_frag = length(light_pos - ubo_per_frame.viewer_pos );
shadow = CalculatePointShadow(world_pos, i, view_to_frag, light_pos);
}
Lo += PointShading(normal,
diffuse.xyz,
rough,
metal,
lights[i].intensity,
light_pos,
lights[i].constant,
lights[i].linear,
lights[i].quadratic,
world_pos,
ubo_per_frame.viewer_pos,
shadow);
}
else if(lights[i].light_type == HYDRA_LIGHT_SPOT)
{
float shadow = 0;
if(lights[i].shadow_caster == 1)
{
// float distance = length(light_pos - world_pos);
shadow = CalculateSpotShadow(vec4(world_pos, 1.0),
lights[i].shadow_map_id,
normal,
light_direction,
ubo_per_frame.viewer_pos);
}
Lo += SpotShading(normal,
diffuse.xyz,
rough,
metal,
lights[i].intensity,
light_direction,
light_pos,
lights[i].cutoff,
lights[i].cutoff_outer,
lights[i].constant,
lights[i].linear,
lights[i].quadratic,
world_pos,
ubo_per_frame.viewer_pos,
shadow) * lights[i].diffuse.xyz;
}
}
float lighting = 0;
vec3 output_color = Lo;
// HDR tonemapping
output_color = output_color / (output_color + vec3(1.0));
// gamma correct
output_color = pow(output_color, vec3(1.0/2.1));
uFragColor = vec4(output_color, 1.0);
// float light = dot(lights[0].light_direction, normal);
// uFragColor = vec4(diffuse.xyz * light,1);
// uFragColor = vec4(normal, 1);
}