2020-08-13 01:21:01 +00:00
|
|
|
#[compute]
|
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2021-04-13 20:01:43 +00:00
|
|
|
#VERSION_DEFINES
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
/* Do not use subgroups here, seems there is not much advantage and causes glitches
|
2021-03-31 10:31:27 +00:00
|
|
|
#if defined(has_GL_KHR_shader_subgroup_ballot) && defined(has_GL_KHR_shader_subgroup_arithmetic)
|
2021-01-17 16:25:38 +00:00
|
|
|
#extension GL_KHR_shader_subgroup_ballot: enable
|
|
|
|
#extension GL_KHR_shader_subgroup_arithmetic: enable
|
|
|
|
|
|
|
|
#define USE_SUBGROUPS
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
2020-08-13 01:21:01 +00:00
|
|
|
#if defined(MODE_FOG) || defined(MODE_FILTER)
|
|
|
|
|
|
|
|
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MODE_DENSITY)
|
|
|
|
|
|
|
|
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cluster_data_inc.glsl"
|
2021-03-10 11:23:55 +00:00
|
|
|
#include "light_data_inc.glsl"
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
#define M_PI 3.14159265359
|
|
|
|
|
|
|
|
layout(set = 0, binding = 1) uniform texture2D shadow_atlas;
|
|
|
|
layout(set = 0, binding = 2) uniform texture2D directional_shadow_atlas;
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
layout(set = 0, binding = 3, std430) restrict readonly buffer OmniLights {
|
2020-08-13 01:21:01 +00:00
|
|
|
LightData data[];
|
|
|
|
}
|
2021-01-17 16:25:38 +00:00
|
|
|
omni_lights;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
layout(set = 0, binding = 4, std430) restrict readonly buffer SpotLights {
|
|
|
|
LightData data[];
|
|
|
|
}
|
|
|
|
spot_lights;
|
|
|
|
|
|
|
|
layout(set = 0, binding = 5, std140) uniform DirectionalLights {
|
2020-08-13 01:21:01 +00:00
|
|
|
DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
|
|
|
|
}
|
|
|
|
directional_lights;
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
layout(set = 0, binding = 6, std430) buffer restrict readonly ClusterBuffer {
|
|
|
|
uint data[];
|
2020-08-13 01:21:01 +00:00
|
|
|
}
|
2021-01-17 16:25:38 +00:00
|
|
|
cluster_buffer;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
layout(set = 0, binding = 7) uniform sampler linear_sampler;
|
|
|
|
|
|
|
|
#ifdef MODE_DENSITY
|
|
|
|
layout(rgba16f, set = 0, binding = 8) uniform restrict writeonly image3D density_map;
|
|
|
|
layout(rgba16f, set = 0, binding = 9) uniform restrict readonly image3D fog_map; //unused
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_FOG
|
|
|
|
layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D density_map;
|
|
|
|
layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D fog_map;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_FILTER
|
|
|
|
layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D source_map;
|
|
|
|
layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D dest_map;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
layout(set = 0, binding = 10) uniform sampler shadow_sampler;
|
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
#define MAX_VOXEL_GI_INSTANCES 8
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
struct VoxelGIData {
|
2020-08-13 01:21:01 +00:00
|
|
|
mat4 xform;
|
|
|
|
vec3 bounds;
|
|
|
|
float dynamic_range;
|
|
|
|
|
|
|
|
float bias;
|
|
|
|
float normal_bias;
|
|
|
|
bool blend_ambient;
|
|
|
|
uint texture_slot;
|
|
|
|
|
|
|
|
float anisotropy_strength;
|
|
|
|
float ambient_occlusion;
|
|
|
|
float ambient_occlusion_size;
|
|
|
|
uint mipmaps;
|
|
|
|
};
|
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
layout(set = 0, binding = 11, std140) uniform VoxelGIs {
|
|
|
|
VoxelGIData data[MAX_VOXEL_GI_INSTANCES];
|
2020-08-13 01:21:01 +00:00
|
|
|
}
|
2021-06-04 22:47:26 +00:00
|
|
|
voxel_gi_instances;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
layout(set = 0, binding = 12) uniform texture3D voxel_gi_textures[MAX_VOXEL_GI_INSTANCES];
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
layout(set = 0, binding = 13) uniform sampler linear_sampler_with_mipmaps;
|
|
|
|
|
|
|
|
#ifdef ENABLE_SDFGI
|
|
|
|
|
|
|
|
// SDFGI Integration on set 1
|
|
|
|
#define SDFGI_MAX_CASCADES 8
|
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
struct SDFVoxelGICascadeData {
|
2020-08-13 01:21:01 +00:00
|
|
|
vec3 position;
|
|
|
|
float to_probe;
|
|
|
|
ivec3 probe_world_offset;
|
|
|
|
float to_cell; // 1/bounds * grid_size
|
|
|
|
};
|
|
|
|
|
|
|
|
layout(set = 1, binding = 0, std140) uniform SDFGI {
|
|
|
|
vec3 grid_size;
|
|
|
|
uint max_cascades;
|
|
|
|
|
|
|
|
bool use_occlusion;
|
|
|
|
int probe_axis_size;
|
|
|
|
float probe_to_uvw;
|
|
|
|
float normal_bias;
|
|
|
|
|
|
|
|
vec3 lightprobe_tex_pixel_size;
|
|
|
|
float energy;
|
|
|
|
|
|
|
|
vec3 lightprobe_uv_offset;
|
|
|
|
float y_mult;
|
|
|
|
|
|
|
|
vec3 occlusion_clamp;
|
|
|
|
uint pad3;
|
|
|
|
|
|
|
|
vec3 occlusion_renormalize;
|
|
|
|
uint pad4;
|
|
|
|
|
|
|
|
vec3 cascade_probe_size;
|
|
|
|
uint pad5;
|
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
SDFVoxelGICascadeData cascades[SDFGI_MAX_CASCADES];
|
2020-08-13 01:21:01 +00:00
|
|
|
}
|
|
|
|
sdfgi;
|
|
|
|
|
|
|
|
layout(set = 1, binding = 1) uniform texture2DArray sdfgi_ambient_texture;
|
|
|
|
|
|
|
|
layout(set = 1, binding = 2) uniform texture3D sdfgi_occlusion_texture;
|
|
|
|
|
|
|
|
#endif //SDFGI
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
layout(set = 0, binding = 14, std140) uniform Params {
|
2020-08-13 01:21:01 +00:00
|
|
|
vec2 fog_frustum_size_begin;
|
|
|
|
vec2 fog_frustum_size_end;
|
|
|
|
|
|
|
|
float fog_frustum_end;
|
|
|
|
float z_near;
|
|
|
|
float z_far;
|
|
|
|
int filter_axis;
|
|
|
|
|
|
|
|
ivec3 fog_volume_size;
|
|
|
|
uint directional_light_count;
|
|
|
|
|
|
|
|
vec3 light_color;
|
|
|
|
float base_density;
|
|
|
|
|
|
|
|
float detail_spread;
|
|
|
|
float gi_inject;
|
2021-06-04 22:47:26 +00:00
|
|
|
uint max_voxel_gi_instances;
|
2021-01-17 16:25:38 +00:00
|
|
|
uint cluster_type_size;
|
|
|
|
|
|
|
|
vec2 screen_size;
|
|
|
|
uint cluster_shift;
|
|
|
|
uint cluster_width;
|
|
|
|
|
|
|
|
uint max_cluster_element_count_div_32;
|
2021-02-05 13:51:49 +00:00
|
|
|
bool use_temporal_reprojection;
|
|
|
|
uint temporal_frame;
|
|
|
|
float temporal_blend;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
mat3x4 cam_rotation;
|
2021-02-05 13:51:49 +00:00
|
|
|
mat4 to_prev_view;
|
2020-08-13 01:21:01 +00:00
|
|
|
}
|
|
|
|
params;
|
|
|
|
|
2021-02-05 13:51:49 +00:00
|
|
|
layout(set = 0, binding = 15) uniform texture3D prev_density_texture;
|
|
|
|
|
2020-08-13 01:21:01 +00:00
|
|
|
float get_depth_at_pos(float cell_depth_size, int z) {
|
|
|
|
float d = float(z) * cell_depth_size + cell_depth_size * 0.5; //center of voxels
|
|
|
|
d = pow(d, params.detail_spread);
|
|
|
|
return params.fog_frustum_end * d;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 hash3f(uvec3 x) {
|
|
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
|
|
x = ((x >> 16) ^ x) * 0x45d9f3b;
|
|
|
|
x = (x >> 16) ^ x;
|
|
|
|
return vec3(x & 0xFFFFF) / vec3(float(0xFFFFF));
|
|
|
|
}
|
|
|
|
|
2021-01-05 15:33:54 +00:00
|
|
|
float get_omni_attenuation(float distance, float inv_range, float decay) {
|
|
|
|
float nd = distance * inv_range;
|
|
|
|
nd *= nd;
|
|
|
|
nd *= nd; // nd^4
|
|
|
|
nd = max(1.0 - nd, 0.0);
|
|
|
|
nd *= nd; // nd^2
|
|
|
|
return nd * pow(max(distance, 0.0001), -decay);
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
|
|
|
|
uint item_min_max = cluster_buffer.data[p_offset];
|
|
|
|
item_min = item_min_max & 0xFFFF;
|
|
|
|
item_max = item_min_max >> 16;
|
|
|
|
;
|
|
|
|
|
|
|
|
item_from = item_min >> 5;
|
|
|
|
item_to = (item_max == 0) ? 0 : ((item_max - 1) >> 5) + 1; //side effect of how it is stored, as item_max 0 means no elements
|
|
|
|
}
|
|
|
|
|
|
|
|
uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
|
|
|
|
int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
|
|
|
|
int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
|
|
|
|
return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:51:49 +00:00
|
|
|
#define TEMPORAL_FRAMES 16
|
|
|
|
|
|
|
|
const vec3 halton_map[TEMPORAL_FRAMES] = vec3[](
|
|
|
|
vec3(0.5, 0.33333333, 0.2),
|
|
|
|
vec3(0.25, 0.66666667, 0.4),
|
|
|
|
vec3(0.75, 0.11111111, 0.6),
|
|
|
|
vec3(0.125, 0.44444444, 0.8),
|
|
|
|
vec3(0.625, 0.77777778, 0.04),
|
|
|
|
vec3(0.375, 0.22222222, 0.24),
|
|
|
|
vec3(0.875, 0.55555556, 0.44),
|
|
|
|
vec3(0.0625, 0.88888889, 0.64),
|
|
|
|
vec3(0.5625, 0.03703704, 0.84),
|
|
|
|
vec3(0.3125, 0.37037037, 0.08),
|
|
|
|
vec3(0.8125, 0.7037037, 0.28),
|
|
|
|
vec3(0.1875, 0.14814815, 0.48),
|
|
|
|
vec3(0.6875, 0.48148148, 0.68),
|
|
|
|
vec3(0.4375, 0.81481481, 0.88),
|
|
|
|
vec3(0.9375, 0.25925926, 0.12),
|
|
|
|
vec3(0.03125, 0.59259259, 0.32));
|
|
|
|
|
2020-08-13 01:21:01 +00:00
|
|
|
void main() {
|
|
|
|
vec3 fog_cell_size = 1.0 / vec3(params.fog_volume_size);
|
|
|
|
|
|
|
|
#ifdef MODE_DENSITY
|
|
|
|
|
|
|
|
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
|
|
|
|
if (any(greaterThanEqual(pos, params.fog_volume_size))) {
|
|
|
|
return; //do not compute
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 posf = vec3(pos);
|
|
|
|
|
|
|
|
//posf += mix(vec3(0.0),vec3(1.0),0.3) * hash3f(uvec3(pos)) * 2.0 - 1.0;
|
|
|
|
|
|
|
|
vec3 fog_unit_pos = posf * fog_cell_size + fog_cell_size * 0.5; //center of voxels
|
2021-01-17 16:25:38 +00:00
|
|
|
|
|
|
|
uvec2 screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
|
|
|
|
uvec2 cluster_pos = screen_pos >> params.cluster_shift;
|
|
|
|
uint cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
|
|
|
|
//positions in screen are too spread apart, no hopes for optimizing with subgroups
|
|
|
|
|
2020-08-13 01:21:01 +00:00
|
|
|
fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
|
|
|
|
|
|
|
|
vec3 view_pos;
|
|
|
|
view_pos.xy = (fog_unit_pos.xy * 2.0 - 1.0) * mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(fog_unit_pos.z));
|
|
|
|
view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
|
|
|
|
view_pos.y = -view_pos.y;
|
|
|
|
|
2021-02-05 13:51:49 +00:00
|
|
|
vec4 reprojected_density = vec4(0.0);
|
|
|
|
float reproject_amount = 0.0;
|
|
|
|
|
|
|
|
if (params.use_temporal_reprojection) {
|
|
|
|
vec3 prev_view = (params.to_prev_view * vec4(view_pos, 1.0)).xyz;
|
|
|
|
//undo transform into prev view
|
|
|
|
prev_view.y = -prev_view.y;
|
|
|
|
//z back to unit size
|
|
|
|
prev_view.z /= -params.fog_frustum_end;
|
|
|
|
//xy back to unit size
|
|
|
|
prev_view.xy /= mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(prev_view.z));
|
|
|
|
prev_view.xy = prev_view.xy * 0.5 + 0.5;
|
|
|
|
//z back to unspread value
|
|
|
|
prev_view.z = pow(prev_view.z, 1.0 / params.detail_spread);
|
|
|
|
|
|
|
|
if (all(greaterThan(prev_view, vec3(0.0))) && all(lessThan(prev_view, vec3(1.0)))) {
|
|
|
|
//reprojectinon fits
|
|
|
|
|
|
|
|
reprojected_density = textureLod(sampler3D(prev_density_texture, linear_sampler), prev_view, 0.0);
|
|
|
|
reproject_amount = params.temporal_blend;
|
|
|
|
|
|
|
|
// Since we can reproject, now we must jitter the current view pos.
|
|
|
|
// This is done here because cells that can't reproject should not jitter.
|
|
|
|
|
|
|
|
fog_unit_pos = posf * fog_cell_size + fog_cell_size * halton_map[params.temporal_frame]; //center of voxels, offset by halton table
|
|
|
|
|
|
|
|
screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
|
|
|
|
cluster_pos = screen_pos >> params.cluster_shift;
|
|
|
|
cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
|
|
|
|
//positions in screen are too spread apart, no hopes for optimizing with subgroups
|
|
|
|
|
|
|
|
fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
|
|
|
|
|
|
|
|
view_pos.xy = (fog_unit_pos.xy * 2.0 - 1.0) * mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(fog_unit_pos.z));
|
|
|
|
view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
|
|
|
|
view_pos.y = -view_pos.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
uint cluster_z = uint(clamp((abs(view_pos.z) / params.z_far) * 32.0, 0.0, 31.0));
|
|
|
|
|
2020-08-13 01:21:01 +00:00
|
|
|
vec3 total_light = params.light_color;
|
|
|
|
|
|
|
|
float total_density = params.base_density;
|
|
|
|
float cell_depth_size = abs(view_pos.z - get_depth_at_pos(fog_cell_size.z, pos.z + 1));
|
|
|
|
//compute directional lights
|
|
|
|
|
|
|
|
for (uint i = 0; i < params.directional_light_count; i++) {
|
|
|
|
vec3 shadow_attenuation = vec3(1.0);
|
|
|
|
|
|
|
|
if (directional_lights.data[i].shadow_enabled) {
|
|
|
|
float depth_z = -view_pos.z;
|
|
|
|
|
|
|
|
vec4 pssm_coord;
|
|
|
|
vec3 shadow_color = directional_lights.data[i].shadow_color1.rgb;
|
|
|
|
vec3 light_dir = directional_lights.data[i].direction;
|
|
|
|
vec4 v = vec4(view_pos, 1.0);
|
|
|
|
float z_range;
|
|
|
|
|
|
|
|
if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
|
|
|
|
pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
|
|
|
|
pssm_coord /= pssm_coord.w;
|
|
|
|
z_range = directional_lights.data[i].shadow_z_range.x;
|
|
|
|
|
|
|
|
} else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
|
|
|
|
pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
|
|
|
|
pssm_coord /= pssm_coord.w;
|
|
|
|
z_range = directional_lights.data[i].shadow_z_range.y;
|
|
|
|
|
|
|
|
} else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
|
|
|
|
pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
|
|
|
|
pssm_coord /= pssm_coord.w;
|
|
|
|
z_range = directional_lights.data[i].shadow_z_range.z;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
|
|
|
|
pssm_coord /= pssm_coord.w;
|
|
|
|
z_range = directional_lights.data[i].shadow_z_range.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
float depth = texture(sampler2D(directional_shadow_atlas, linear_sampler), pssm_coord.xy).r;
|
|
|
|
float shadow = exp(min(0.0, (depth - pssm_coord.z)) * z_range * directional_lights.data[i].shadow_volumetric_fog_fade);
|
|
|
|
|
|
|
|
/*
|
|
|
|
//float shadow = textureProj(sampler2DShadow(directional_shadow_atlas,shadow_sampler),pssm_coord);
|
|
|
|
float shadow = 0.0;
|
|
|
|
for(float xi=-1;xi<=1;xi++) {
|
|
|
|
for(float yi=-1;yi<=1;yi++) {
|
|
|
|
vec2 ofs = vec2(xi,yi) * 1.5 * params.directional_shadow_pixel_size;
|
|
|
|
shadow += textureProj(sampler2DShadow(directional_shadow_atlas,shadow_sampler),pssm_coord + vec4(ofs,0.0,0.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
shadow /= 3.0 * 3.0;
|
|
|
|
|
|
|
|
*/
|
|
|
|
shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, view_pos.z)); //done with negative values for performance
|
|
|
|
|
|
|
|
shadow_attenuation = mix(shadow_color, vec3(1.0), shadow);
|
|
|
|
}
|
|
|
|
|
|
|
|
total_light += shadow_attenuation * directional_lights.data[i].color * directional_lights.data[i].energy / M_PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
//compute lights from cluster
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
{ //omni lights
|
|
|
|
|
|
|
|
uint cluster_omni_offset = cluster_offset;
|
|
|
|
|
|
|
|
uint item_min;
|
|
|
|
uint item_max;
|
|
|
|
uint item_from;
|
|
|
|
uint item_to;
|
|
|
|
|
|
|
|
cluster_get_item_range(cluster_omni_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
#ifdef USE_SUBGROUPS
|
|
|
|
item_from = subgroupBroadcastFirst(subgroupMin(item_from));
|
|
|
|
item_to = subgroupBroadcastFirst(subgroupMax(item_to));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (uint i = item_from; i < item_to; i++) {
|
|
|
|
uint mask = cluster_buffer.data[cluster_omni_offset + i];
|
|
|
|
mask &= cluster_get_range_clip_mask(i, item_min, item_max);
|
|
|
|
#ifdef USE_SUBGROUPS
|
|
|
|
uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
|
|
|
|
#else
|
|
|
|
uint merged_mask = mask;
|
|
|
|
#endif
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
while (merged_mask != 0) {
|
|
|
|
uint bit = findMSB(merged_mask);
|
|
|
|
merged_mask &= ~(1 << bit);
|
|
|
|
#ifdef USE_SUBGROUPS
|
|
|
|
if (((1 << bit) & mask) == 0) { //do not process if not originally here
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
uint light_index = 32 * i + bit;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
//if (!bool(omni_omni_lights.data[light_index].mask & draw_call.layer_mask)) {
|
|
|
|
// continue; //not masked
|
|
|
|
//}
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
vec3 light_pos = omni_lights.data[light_index].position;
|
|
|
|
float d = distance(omni_lights.data[light_index].position, view_pos);
|
|
|
|
float shadow_attenuation = 1.0;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
if (d * omni_lights.data[light_index].inv_radius < 1.0) {
|
|
|
|
float attenuation = get_omni_attenuation(d, omni_lights.data[light_index].inv_radius, omni_lights.data[light_index].attenuation);
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
vec3 light = omni_lights.data[light_index].color / M_PI;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
if (omni_lights.data[light_index].shadow_enabled) {
|
|
|
|
//has shadow
|
|
|
|
vec4 v = vec4(view_pos, 1.0);
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
vec4 splane = (omni_lights.data[light_index].shadow_matrix * v);
|
|
|
|
float shadow_len = length(splane.xyz); //need to remember shadow len from here
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
splane.xyz = normalize(splane.xyz);
|
|
|
|
vec4 clamp_rect = omni_lights.data[light_index].atlas_rect;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
if (splane.z >= 0.0) {
|
|
|
|
splane.z += 1.0;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
clamp_rect.y += clamp_rect.w;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
} else {
|
|
|
|
splane.z = 1.0 - splane.z;
|
|
|
|
}
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
splane.xy /= splane.z;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
splane.xy = splane.xy * 0.5 + 0.5;
|
|
|
|
splane.z = shadow_len * omni_lights.data[light_index].inv_radius;
|
|
|
|
splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
|
|
|
|
splane.w = 1.0; //needed? i think it should be 1 already
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
float depth = texture(sampler2D(shadow_atlas, linear_sampler), splane.xy).r;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
shadow_attenuation = exp(min(0.0, (depth - splane.z)) / omni_lights.data[light_index].inv_radius * omni_lights.data[light_index].shadow_volumetric_fog_fade);
|
|
|
|
}
|
|
|
|
total_light += light * attenuation * shadow_attenuation;
|
|
|
|
}
|
2020-08-13 01:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
{ //spot lights
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
uint cluster_spot_offset = cluster_offset + params.cluster_type_size;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
uint item_min;
|
|
|
|
uint item_max;
|
|
|
|
uint item_from;
|
|
|
|
uint item_to;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
cluster_get_item_range(cluster_spot_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
#ifdef USE_SUBGROUPS
|
|
|
|
item_from = subgroupBroadcastFirst(subgroupMin(item_from));
|
|
|
|
item_to = subgroupBroadcastFirst(subgroupMax(item_to));
|
|
|
|
#endif
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
for (uint i = item_from; i < item_to; i++) {
|
|
|
|
uint mask = cluster_buffer.data[cluster_spot_offset + i];
|
|
|
|
mask &= cluster_get_range_clip_mask(i, item_min, item_max);
|
|
|
|
#ifdef USE_SUBGROUPS
|
|
|
|
uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
|
|
|
|
#else
|
|
|
|
uint merged_mask = mask;
|
|
|
|
#endif
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
while (merged_mask != 0) {
|
|
|
|
uint bit = findMSB(merged_mask);
|
|
|
|
merged_mask &= ~(1 << bit);
|
|
|
|
#ifdef USE_SUBGROUPS
|
|
|
|
if (((1 << bit) & mask) == 0) { //do not process if not originally here
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
//if (!bool(omni_lights.data[light_index].mask & draw_call.layer_mask)) {
|
|
|
|
// continue; //not masked
|
|
|
|
//}
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-01-17 16:25:38 +00:00
|
|
|
uint light_index = 32 * i + bit;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
vec3 light_pos = spot_lights.data[light_index].position;
|
|
|
|
vec3 light_rel_vec = spot_lights.data[light_index].position - view_pos;
|
2021-01-17 16:25:38 +00:00
|
|
|
float d = length(light_rel_vec);
|
|
|
|
float shadow_attenuation = 1.0;
|
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
if (d * spot_lights.data[light_index].inv_radius < 1.0) {
|
|
|
|
float attenuation = get_omni_attenuation(d, spot_lights.data[light_index].inv_radius, spot_lights.data[light_index].attenuation);
|
2021-01-17 16:25:38 +00:00
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
vec3 spot_dir = spot_lights.data[light_index].direction;
|
|
|
|
float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights.data[light_index].cone_angle);
|
|
|
|
float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights.data[light_index].cone_angle));
|
|
|
|
attenuation *= 1.0 - pow(spot_rim, spot_lights.data[light_index].cone_attenuation);
|
2021-01-17 16:25:38 +00:00
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
vec3 light = spot_lights.data[light_index].color / M_PI;
|
2021-01-17 16:25:38 +00:00
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
if (spot_lights.data[light_index].shadow_enabled) {
|
2021-01-17 16:25:38 +00:00
|
|
|
//has shadow
|
|
|
|
vec4 v = vec4(view_pos, 1.0);
|
2020-08-13 01:21:01 +00:00
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
vec4 splane = (spot_lights.data[light_index].shadow_matrix * v);
|
2021-01-17 16:25:38 +00:00
|
|
|
splane /= splane.w;
|
|
|
|
|
|
|
|
float depth = texture(sampler2D(shadow_atlas, linear_sampler), splane.xy).r;
|
|
|
|
|
2021-02-07 02:34:10 +00:00
|
|
|
shadow_attenuation = exp(min(0.0, (depth - splane.z)) / spot_lights.data[light_index].inv_radius * spot_lights.data[light_index].shadow_volumetric_fog_fade);
|
2021-01-17 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
total_light += light * attenuation * shadow_attenuation;
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 01:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 world_pos = mat3(params.cam_rotation) * view_pos;
|
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
for (uint i = 0; i < params.max_voxel_gi_instances; i++) {
|
|
|
|
vec3 position = (voxel_gi_instances.data[i].xform * vec4(world_pos, 1.0)).xyz;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
//this causes corrupted pixels, i have no idea why..
|
2021-06-04 22:47:26 +00:00
|
|
|
if (all(bvec2(all(greaterThanEqual(position, vec3(0.0))), all(lessThan(position, voxel_gi_instances.data[i].bounds))))) {
|
|
|
|
position /= voxel_gi_instances.data[i].bounds;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
vec4 light = vec4(0.0);
|
2021-06-04 22:47:26 +00:00
|
|
|
for (uint j = 0; j < voxel_gi_instances.data[i].mipmaps; j++) {
|
|
|
|
vec4 slight = textureLod(sampler3D(voxel_gi_textures[i], linear_sampler_with_mipmaps), position, float(j));
|
2020-08-13 01:21:01 +00:00
|
|
|
float a = (1.0 - light.a);
|
|
|
|
light += a * slight;
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:47:26 +00:00
|
|
|
light.rgb *= voxel_gi_instances.data[i].dynamic_range * params.gi_inject;
|
2020-08-13 01:21:01 +00:00
|
|
|
|
|
|
|
total_light += light.rgb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//sdfgi
|
|
|
|
#ifdef ENABLE_SDFGI
|
|
|
|
|
|
|
|
{
|
|
|
|
float blend = -1.0;
|
|
|
|
vec3 ambient_total = vec3(0.0);
|
|
|
|
|
|
|
|
for (uint i = 0; i < sdfgi.max_cascades; i++) {
|
|
|
|
vec3 cascade_pos = (world_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
|
|
|
|
|
|
|
|
if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
|
|
|
|
continue; //skip cascade
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 base_pos = floor(cascade_pos);
|
|
|
|
ivec3 probe_base_pos = ivec3(base_pos);
|
|
|
|
|
|
|
|
vec4 ambient_accum = vec4(0.0);
|
|
|
|
|
|
|
|
ivec3 tex_pos = ivec3(probe_base_pos.xy, int(i));
|
|
|
|
tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
|
|
|
|
|
|
|
|
for (uint j = 0; j < 8; j++) {
|
|
|
|
ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
|
|
|
|
ivec3 probe_posi = probe_base_pos;
|
|
|
|
probe_posi += offset;
|
|
|
|
|
|
|
|
// Compute weight
|
|
|
|
|
|
|
|
vec3 probe_pos = vec3(probe_posi);
|
|
|
|
vec3 probe_to_pos = cascade_pos - probe_pos;
|
|
|
|
|
|
|
|
vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
|
|
|
|
float weight = trilinear.x * trilinear.y * trilinear.z;
|
|
|
|
|
|
|
|
// Compute lightprobe occlusion
|
|
|
|
|
|
|
|
if (sdfgi.use_occlusion) {
|
|
|
|
ivec3 occ_indexv = abs((sdfgi.cascades[i].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
|
|
|
|
vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
|
|
|
|
|
|
|
|
vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
|
|
|
|
occ_pos.z += float(i);
|
|
|
|
if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
|
|
|
|
occ_pos.x += 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
occ_pos *= sdfgi.occlusion_renormalize;
|
|
|
|
float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask);
|
|
|
|
|
|
|
|
weight *= max(occlusion, 0.01);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute ambient texture position
|
|
|
|
|
|
|
|
ivec3 uvw = tex_pos;
|
|
|
|
uvw.xy += offset.xy;
|
|
|
|
uvw.x += offset.z * sdfgi.probe_axis_size;
|
|
|
|
|
|
|
|
vec3 ambient = texelFetch(sampler2DArray(sdfgi_ambient_texture, linear_sampler), uvw, 0).rgb;
|
|
|
|
|
|
|
|
ambient_accum.rgb += ambient * weight;
|
|
|
|
ambient_accum.a += weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ambient_accum.a > 0) {
|
|
|
|
ambient_accum.rgb /= ambient_accum.a;
|
|
|
|
}
|
|
|
|
ambient_total = ambient_accum.rgb;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_light += ambient_total * params.gi_inject;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-02-05 13:51:49 +00:00
|
|
|
vec4 final_density = vec4(total_light, total_density);
|
|
|
|
|
|
|
|
final_density = mix(final_density, reprojected_density, reproject_amount);
|
|
|
|
|
|
|
|
imageStore(density_map, pos, final_density);
|
2020-08-13 01:21:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_FOG
|
|
|
|
|
|
|
|
ivec3 pos = ivec3(gl_GlobalInvocationID.xy, 0);
|
|
|
|
|
|
|
|
if (any(greaterThanEqual(pos, params.fog_volume_size))) {
|
|
|
|
return; //do not compute
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 fog_accum = vec4(0.0);
|
|
|
|
float prev_z = 0.0;
|
|
|
|
|
|
|
|
float t = 1.0;
|
|
|
|
|
|
|
|
for (int i = 0; i < params.fog_volume_size.z; i++) {
|
|
|
|
//compute fog position
|
|
|
|
ivec3 fog_pos = pos + ivec3(0, 0, i);
|
|
|
|
//get fog value
|
|
|
|
vec4 fog = imageLoad(density_map, fog_pos);
|
|
|
|
|
|
|
|
//get depth at cell pos
|
|
|
|
float z = get_depth_at_pos(fog_cell_size.z, i);
|
2020-09-18 11:35:51 +00:00
|
|
|
//get distance from previous pos
|
2020-08-13 01:21:01 +00:00
|
|
|
float d = abs(prev_z - z);
|
|
|
|
//compute exinction based on beer's
|
|
|
|
float extinction = t * exp(-d * fog.a);
|
|
|
|
//compute alpha based on different of extinctions
|
|
|
|
float alpha = t - extinction;
|
|
|
|
//update extinction
|
|
|
|
t = extinction;
|
|
|
|
|
|
|
|
fog_accum += vec4(fog.rgb * alpha, alpha);
|
|
|
|
prev_z = z;
|
|
|
|
|
|
|
|
vec4 fog_value;
|
|
|
|
|
|
|
|
if (fog_accum.a > 0.0) {
|
|
|
|
fog_value = vec4(fog_accum.rgb / fog_accum.a, 1.0 - t);
|
|
|
|
} else {
|
|
|
|
fog_value = vec4(0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore(fog_map, fog_pos, fog_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_FILTER
|
|
|
|
|
|
|
|
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
|
|
|
|
|
|
|
|
const float gauss[7] = float[](0.071303, 0.131514, 0.189879, 0.214607, 0.189879, 0.131514, 0.071303);
|
|
|
|
|
|
|
|
const ivec3 filter_dir[3] = ivec3[](ivec3(1, 0, 0), ivec3(0, 1, 0), ivec3(0, 0, 1));
|
|
|
|
ivec3 offset = filter_dir[params.filter_axis];
|
|
|
|
|
|
|
|
vec4 accum = vec4(0.0);
|
|
|
|
for (int i = -3; i <= 3; i++) {
|
|
|
|
accum += imageLoad(source_map, clamp(pos + offset * i, ivec3(0), params.fog_volume_size - ivec3(1))) * gauss[i + 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore(dest_map, pos, accum);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|