2020-05-18 08:56:22 +00:00
|
|
|
#[compute]
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2021-04-13 20:01:43 +00:00
|
|
|
#VERSION_DEFINES
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifdef MODE_DYNAMIC
|
|
|
|
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
|
|
|
#else
|
2019-10-03 20:39:08 +00:00
|
|
|
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MODE_DYNAMIC
|
|
|
|
|
2019-10-03 20:39:08 +00:00
|
|
|
#define NO_CHILDREN 0xFFFFFFFF
|
|
|
|
|
|
|
|
struct CellChildren {
|
|
|
|
uint children[8];
|
|
|
|
};
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
|
|
|
|
CellChildren data[];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
cell_children;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
struct CellData {
|
|
|
|
uint position; // xyz 10 bits
|
|
|
|
uint albedo; //rgb albedo
|
|
|
|
uint emission; //rgb normalized with e as multiplier
|
|
|
|
uint normal; //RGB normal encoded
|
|
|
|
};
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
|
|
|
|
CellData data[];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
cell_data;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif // MODE DYNAMIC
|
|
|
|
|
2019-10-03 20:39:08 +00:00
|
|
|
#define LIGHT_TYPE_DIRECTIONAL 0
|
|
|
|
#define LIGHT_TYPE_OMNI 1
|
|
|
|
#define LIGHT_TYPE_SPOT 2
|
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#if defined(MODE_COMPUTE_LIGHT) || defined(MODE_DYNAMIC_LIGHTING)
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
struct Light {
|
|
|
|
uint type;
|
|
|
|
float energy;
|
|
|
|
float radius;
|
|
|
|
float attenuation;
|
|
|
|
|
|
|
|
vec3 color;
|
2021-02-06 20:39:08 +00:00
|
|
|
float cos_spot_angle;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
vec3 position;
|
2021-02-06 20:39:08 +00:00
|
|
|
float inv_spot_attenuation;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
vec3 direction;
|
|
|
|
bool has_shadow;
|
|
|
|
};
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 3, std140) uniform Lights {
|
|
|
|
Light data[MAX_LIGHTS];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
lights;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#endif // MODE COMPUTE LIGHT
|
|
|
|
|
|
|
|
#ifdef MODE_SECOND_BOUNCE
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 5) uniform texture3D color_texture;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#endif // MODE_SECOND_BOUNCE
|
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifndef MODE_DYNAMIC
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2022-02-11 17:40:24 +00:00
|
|
|
layout(push_constant, std430) uniform Params {
|
2019-10-03 20:39:08 +00:00
|
|
|
ivec3 limits;
|
|
|
|
uint stack_size;
|
|
|
|
|
|
|
|
float emission_scale;
|
|
|
|
float propagation;
|
|
|
|
float dynamic_range;
|
|
|
|
|
|
|
|
uint light_count;
|
|
|
|
uint cell_offset;
|
|
|
|
uint cell_count;
|
|
|
|
float aniso_strength;
|
|
|
|
uint pad;
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
params;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 4, std430) buffer Outputs {
|
|
|
|
vec4 data[];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
outputs;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif // MODE DYNAMIC
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 9) uniform texture3D texture_sdf;
|
|
|
|
layout(set = 0, binding = 10) uniform sampler texture_sampler;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-10-03 20:39:08 +00:00
|
|
|
#ifdef MODE_WRITE_TEXTURE
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(rgba8, set = 0, binding = 5) uniform restrict writeonly image3D color_tex;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifdef MODE_DYNAMIC
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2022-02-11 17:40:24 +00:00
|
|
|
layout(push_constant, std430) uniform Params {
|
2019-10-11 02:14:56 +00:00
|
|
|
ivec3 limits;
|
2019-11-02 01:08:56 +00:00
|
|
|
uint light_count; //when not lighting
|
2019-10-11 02:14:56 +00:00
|
|
|
ivec3 x_dir;
|
|
|
|
float z_base;
|
|
|
|
ivec3 y_dir;
|
|
|
|
float z_sign;
|
|
|
|
ivec3 z_dir;
|
|
|
|
float pos_multiplier;
|
|
|
|
ivec2 rect_pos;
|
|
|
|
ivec2 rect_size;
|
|
|
|
ivec2 prev_rect_ofs;
|
|
|
|
ivec2 prev_rect_size;
|
|
|
|
bool flip_x;
|
|
|
|
bool flip_y;
|
|
|
|
float dynamic_range;
|
2019-11-02 01:08:56 +00:00
|
|
|
bool on_mipmap;
|
2019-11-02 01:28:53 +00:00
|
|
|
float propagation;
|
|
|
|
float pad[3];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
params;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifdef MODE_DYNAMIC_LIGHTING
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(rgba8, set = 0, binding = 5) uniform restrict readonly image2D source_albedo;
|
|
|
|
layout(rgba8, set = 0, binding = 6) uniform restrict readonly image2D source_normal;
|
|
|
|
layout(rgba8, set = 0, binding = 7) uniform restrict readonly image2D source_orm;
|
2019-10-11 02:14:56 +00:00
|
|
|
//layout (set=0,binding=8) uniform texture2D source_depth;
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(rgba16f, set = 0, binding = 11) uniform restrict image2D emission;
|
|
|
|
layout(r32f, set = 0, binding = 12) uniform restrict image2D depth;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifdef MODE_DYNAMIC_SHRINK
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(rgba16f, set = 0, binding = 5) uniform restrict readonly image2D source_light;
|
|
|
|
layout(r32f, set = 0, binding = 6) uniform restrict readonly image2D source_depth;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifdef MODE_DYNAMIC_SHRINK_WRITE
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(rgba16f, set = 0, binding = 7) uniform restrict writeonly image2D light;
|
|
|
|
layout(r32f, set = 0, binding = 8) uniform restrict writeonly image2D depth;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif // MODE_DYNAMIC_SHRINK_WRITE
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifdef MODE_DYNAMIC_SHRINK_PLOT
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(rgba8, set = 0, binding = 11) uniform restrict image3D color_texture;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif //MODE_DYNAMIC_SHRINK_PLOT
|
|
|
|
|
|
|
|
#endif // MODE_DYNAMIC_SHRINK
|
|
|
|
|
|
|
|
//layout (rgba8,set=0,binding=5) uniform restrict writeonly image3D color_tex;
|
|
|
|
|
|
|
|
#endif // MODE DYNAMIC
|
|
|
|
|
|
|
|
#if defined(MODE_COMPUTE_LIGHT) || defined(MODE_DYNAMIC_LIGHTING)
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
float raymarch(float distance, float distance_adv, vec3 from, vec3 direction) {
|
2019-10-11 02:14:56 +00:00
|
|
|
vec3 cell_size = 1.0 / vec3(params.limits);
|
2019-11-01 00:27:42 +00:00
|
|
|
float occlusion = 1.0;
|
|
|
|
while (distance > 0.5) { //use this to avoid precision errors
|
2019-11-05 11:01:00 +00:00
|
|
|
float advance = texture(sampler3D(texture_sdf, texture_sampler), from * cell_size).r * 255.0 - 1.0;
|
|
|
|
if (advance < 0.0) {
|
2019-11-01 00:27:42 +00:00
|
|
|
occlusion = 0.0;
|
2019-10-11 02:14:56 +00:00
|
|
|
break;
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
occlusion = min(advance, occlusion);
|
2019-11-01 00:27:42 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
advance = max(distance_adv, advance - mod(advance, distance_adv)); //should always advance in multiples of distance_adv
|
|
|
|
|
|
|
|
from += direction * advance;
|
|
|
|
distance -= advance;
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
return occlusion; //max(0.0,distance);
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
bool compute_light_vector(uint light, vec3 pos, out float attenuation, out vec3 light_pos) {
|
|
|
|
if (lights.data[light].type == LIGHT_TYPE_DIRECTIONAL) {
|
2019-10-03 20:39:08 +00:00
|
|
|
light_pos = pos - lights.data[light].direction * length(vec3(params.limits));
|
|
|
|
attenuation = 1.0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
light_pos = lights.data[light].position;
|
|
|
|
float distance = length(pos - light_pos);
|
|
|
|
if (distance >= lights.data[light].radius) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-05 15:33:54 +00:00
|
|
|
attenuation = get_omni_attenuation(distance, 1.0 / lights.data[light].radius, lights.data[light].attenuation);
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (lights.data[light].type == LIGHT_TYPE_SPOT) {
|
2019-10-03 20:39:08 +00:00
|
|
|
vec3 rel = normalize(pos - light_pos);
|
2021-02-06 20:39:08 +00:00
|
|
|
float cos_spot_angle = lights.data[light].cos_spot_angle;
|
|
|
|
float cos_angle = dot(rel, lights.data[light].direction);
|
|
|
|
if (cos_angle < cos_spot_angle) {
|
2019-10-03 20:39:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-06 20:39:08 +00:00
|
|
|
float scos = max(cos_angle, cos_spot_angle);
|
|
|
|
float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
|
|
|
|
attenuation *= 1.0 - pow(spot_rim, lights.data[light].inv_spot_attenuation);
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float get_normal_advance(vec3 p_normal) {
|
|
|
|
vec3 normal = p_normal;
|
|
|
|
vec3 unorm = abs(normal);
|
|
|
|
|
|
|
|
if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
|
|
|
|
// x code
|
|
|
|
unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
|
|
|
|
} else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
|
|
|
|
// y code
|
|
|
|
unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
|
|
|
|
} else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
|
|
|
|
// z code
|
|
|
|
unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
|
|
|
|
} else {
|
|
|
|
// oh-no we messed up code
|
|
|
|
// has to be
|
|
|
|
unorm = vec3(1.0, 0.0, 0.0);
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
return 1.0 / dot(normal, unorm);
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
void clip_segment(vec4 plane, vec3 begin, inout vec3 end) {
|
|
|
|
vec3 segment = begin - end;
|
2019-11-05 11:01:00 +00:00
|
|
|
float den = dot(plane.xyz, segment);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
//printf("den is %i\n",den);
|
|
|
|
if (den < 0.0001) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
float dist = (dot(plane.xyz, begin) - plane.w) / den;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
if (dist < 0.0001 || dist > 1.0001) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = begin + segment * -dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool compute_light_at_pos(uint index, vec3 pos, vec3 normal, inout vec3 light, inout vec3 light_dir) {
|
|
|
|
float attenuation;
|
|
|
|
vec3 light_pos;
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (!compute_light_vector(index, pos, attenuation, light_pos)) {
|
2019-10-11 02:14:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
light_dir = normalize(pos - light_pos);
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (attenuation < 0.01 || (length(normal) > 0.2 && dot(normal, light_dir) >= 0)) {
|
2019-10-11 02:14:56 +00:00
|
|
|
return false; //not facing the light, or attenuation is near zero
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lights.data[index].has_shadow) {
|
|
|
|
float distance_adv = get_normal_advance(light_dir);
|
|
|
|
|
|
|
|
vec3 to = pos;
|
2019-11-01 00:27:42 +00:00
|
|
|
if (length(normal) > 0.2) {
|
|
|
|
to += normal * distance_adv * 0.51;
|
|
|
|
} else {
|
2019-11-05 11:01:00 +00:00
|
|
|
to -= sign(light_dir) * 0.45; //go near the edge towards the light direction to avoid self occlusion
|
2019-11-01 00:27:42 +00:00
|
|
|
}
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
//clip
|
2019-11-05 11:01:00 +00:00
|
|
|
clip_segment(mix(vec4(-1.0, 0.0, 0.0, 0.0), vec4(1.0, 0.0, 0.0, float(params.limits.x - 1)), bvec4(light_dir.x < 0.0)), to, light_pos);
|
|
|
|
clip_segment(mix(vec4(0.0, -1.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, float(params.limits.y - 1)), bvec4(light_dir.y < 0.0)), to, light_pos);
|
|
|
|
clip_segment(mix(vec4(0.0, 0.0, -1.0, 0.0), vec4(0.0, 0.0, 1.0, float(params.limits.z - 1)), bvec4(light_dir.z < 0.0)), to, light_pos);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
float distance = length(to - light_pos);
|
2019-10-11 02:14:56 +00:00
|
|
|
if (distance < 0.1) {
|
|
|
|
return false; // hit
|
|
|
|
}
|
|
|
|
|
|
|
|
distance += distance_adv - mod(distance, distance_adv); //make it reach the center of the box always
|
|
|
|
light_pos = to - light_dir * distance;
|
|
|
|
|
|
|
|
//from -= sign(light_dir)*0.45; //go near the edge towards the light direction to avoid self occlusion
|
2019-11-01 00:27:42 +00:00
|
|
|
|
|
|
|
/*float dist = raymarch(distance,distance_adv,light_pos,light_dir);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
if (dist > distance_adv) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-01 00:27:42 +00:00
|
|
|
attenuation *= 1.0 - smoothstep(0.1*distance_adv,distance_adv,dist);
|
|
|
|
*/
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
float occlusion = raymarch(distance, distance_adv, light_pos, light_dir);
|
2019-11-01 00:27:42 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (occlusion == 0.0) {
|
2019-11-01 00:27:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
attenuation *= occlusion; //1.0 - smoothstep(0.1*distance_adv,distance_adv,dist);
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
light = lights.data[index].color * attenuation * lights.data[index].energy;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MODE COMPUTE LIGHT
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
void main() {
|
2019-10-11 02:14:56 +00:00
|
|
|
#ifndef MODE_DYNAMIC
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
uint cell_index = gl_GlobalInvocationID.x;
|
2019-10-03 20:39:08 +00:00
|
|
|
if (cell_index >= params.cell_count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cell_index += params.cell_offset;
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
uvec3 posu = uvec3(cell_data.data[cell_index].position & 0x7FF, (cell_data.data[cell_index].position >> 11) & 0x3FF, cell_data.data[cell_index].position >> 21);
|
2019-10-03 20:39:08 +00:00
|
|
|
vec4 albedo = unpackUnorm4x8(cell_data.data[cell_index].albedo);
|
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
/////////////////COMPUTE LIGHT///////////////////////////////
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#ifdef MODE_COMPUTE_LIGHT
|
|
|
|
|
|
|
|
vec3 pos = vec3(posu) + vec3(0.5);
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
vec3 emission = vec3(uvec3(cell_data.data[cell_index].emission & 0x1ff, (cell_data.data[cell_index].emission >> 9) & 0x1ff, (cell_data.data[cell_index].emission >> 18) & 0x1ff)) * pow(2.0, float(cell_data.data[cell_index].emission >> 27) - 15.0 - 9.0);
|
2019-10-11 02:14:56 +00:00
|
|
|
vec3 normal = unpackSnorm4x8(cell_data.data[cell_index].normal).xyz;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
vec3 accum = vec3(0.0);
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
for (uint i = 0; i < params.light_count; i++) {
|
2019-10-11 02:14:56 +00:00
|
|
|
vec3 light;
|
|
|
|
vec3 light_dir;
|
2019-11-05 11:01:00 +00:00
|
|
|
if (!compute_light_at_pos(i, pos, normal.xyz, light, light_dir)) {
|
2019-10-03 20:39:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
light *= albedo.rgb;
|
2019-10-03 20:39:08 +00:00
|
|
|
|
2019-10-11 02:14:56 +00:00
|
|
|
if (length(normal) > 0.2) {
|
2019-11-05 11:01:00 +00:00
|
|
|
accum += max(0.0, dot(normal, -light_dir)) * light;
|
2019-10-03 20:39:08 +00:00
|
|
|
} else {
|
|
|
|
//all directions
|
2019-11-05 11:01:00 +00:00
|
|
|
accum += light;
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
outputs.data[cell_index] = vec4(accum + emission, 0.0);
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#endif //MODE_COMPUTE_LIGHT
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
/////////////////SECOND BOUNCE///////////////////////////////
|
|
|
|
|
2019-10-03 20:39:08 +00:00
|
|
|
#ifdef MODE_SECOND_BOUNCE
|
|
|
|
vec3 pos = vec3(posu) + vec3(0.5);
|
|
|
|
ivec3 ipos = ivec3(posu);
|
|
|
|
vec4 normal = unpackSnorm4x8(cell_data.data[cell_index].normal);
|
|
|
|
|
|
|
|
vec3 accum = outputs.data[cell_index].rgb;
|
|
|
|
|
|
|
|
if (length(normal.xyz) > 0.2) {
|
|
|
|
vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
|
|
|
|
vec3 tangent = normalize(cross(v0, normal.xyz));
|
|
|
|
vec3 bitangent = normalize(cross(tangent, normal.xyz));
|
|
|
|
mat3 normal_mat = mat3(tangent, bitangent, normal.xyz);
|
|
|
|
|
|
|
|
#define MAX_CONE_DIRS 6
|
|
|
|
|
|
|
|
vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
|
2019-11-05 11:01:00 +00:00
|
|
|
vec3(0.0, 0.0, 1.0),
|
|
|
|
vec3(0.866025, 0.0, 0.5),
|
|
|
|
vec3(0.267617, 0.823639, 0.5),
|
|
|
|
vec3(-0.700629, 0.509037, 0.5),
|
|
|
|
vec3(-0.700629, -0.509037, 0.5),
|
|
|
|
vec3(0.267617, -0.823639, 0.5));
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15);
|
|
|
|
float tan_half_angle = 0.577;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_CONE_DIRS; i++) {
|
|
|
|
vec3 direction = normal_mat * cone_dirs[i];
|
|
|
|
vec4 color = vec4(0.0);
|
|
|
|
{
|
|
|
|
float dist = 1.5;
|
|
|
|
float max_distance = length(vec3(params.limits));
|
|
|
|
vec3 cell_size = 1.0 / vec3(params.limits);
|
|
|
|
|
|
|
|
while (dist < max_distance && color.a < 0.95) {
|
|
|
|
float diameter = max(1.0, 2.0 * tan_half_angle * dist);
|
|
|
|
vec3 uvw_pos = (pos + dist * direction) * cell_size;
|
|
|
|
float half_diameter = diameter * 0.5;
|
|
|
|
//check if outside, then break
|
|
|
|
//if ( any(greaterThan(abs(uvw_pos - 0.5),vec3(0.5f + half_diameter * cell_size)) ) ) {
|
|
|
|
// break;
|
|
|
|
//}
|
|
|
|
|
|
|
|
float log2_diameter = log2(diameter);
|
2019-11-05 11:01:00 +00:00
|
|
|
vec4 scolor = textureLod(sampler3D(color_texture, texture_sampler), uvw_pos, log2_diameter);
|
2019-10-03 20:39:08 +00:00
|
|
|
float a = (1.0 - color.a);
|
|
|
|
color += a * scolor;
|
|
|
|
dist += half_diameter;
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 11:01:00 +00:00
|
|
|
color *= cone_weights[i] * vec4(albedo.rgb, 1.0) * params.dynamic_range; //restore range
|
|
|
|
accum += color.rgb;
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
outputs.data[cell_index] = vec4(accum, 0.0);
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#endif // MODE_SECOND_BOUNCE
|
2019-11-05 11:01:00 +00:00
|
|
|
|
|
|
|
/////////////////UPDATE MIPMAPS///////////////////////////////
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#ifdef MODE_UPDATE_MIPMAPS
|
|
|
|
|
|
|
|
{
|
|
|
|
vec3 light_accum = vec3(0.0);
|
|
|
|
float count = 0.0;
|
2019-11-05 11:01:00 +00:00
|
|
|
for (uint i = 0; i < 8; i++) {
|
2019-10-03 20:39:08 +00:00
|
|
|
uint child_index = cell_children.data[cell_index].children[i];
|
2019-11-05 11:01:00 +00:00
|
|
|
if (child_index == NO_CHILDREN) {
|
2019-10-03 20:39:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
light_accum += outputs.data[child_index].rgb;
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
count += 1.0;
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
float divisor = mix(8.0, count, params.propagation);
|
|
|
|
outputs.data[cell_index] = vec4(light_accum / divisor, 0.0);
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
///////////////////WRITE TEXTURE/////////////////////////////
|
2019-10-03 20:39:08 +00:00
|
|
|
|
|
|
|
#ifdef MODE_WRITE_TEXTURE
|
|
|
|
{
|
2019-11-05 11:01:00 +00:00
|
|
|
imageStore(color_tex, ivec3(posu), vec4(outputs.data[cell_index].rgb / params.dynamic_range, albedo.a));
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
///////////////////DYNAMIC LIGHTING/////////////////////////////
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
#ifdef MODE_DYNAMIC
|
|
|
|
|
|
|
|
ivec2 pos_xy = ivec2(gl_GlobalInvocationID.xy);
|
2019-11-05 11:01:00 +00:00
|
|
|
if (any(greaterThanEqual(pos_xy, params.rect_size))) {
|
2019-10-11 02:14:56 +00:00
|
|
|
return; //out of bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec2 uv_xy = pos_xy;
|
|
|
|
if (params.flip_x) {
|
|
|
|
uv_xy.x = params.rect_size.x - pos_xy.x - 1;
|
|
|
|
}
|
|
|
|
if (params.flip_y) {
|
|
|
|
uv_xy.y = params.rect_size.y - pos_xy.y - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MODE_DYNAMIC_LIGHTING
|
|
|
|
|
|
|
|
{
|
2019-11-05 11:01:00 +00:00
|
|
|
float z = params.z_base + imageLoad(depth, uv_xy).x * params.z_sign;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
ivec3 pos = params.x_dir * (params.rect_pos.x + pos_xy.x) + params.y_dir * (params.rect_pos.y + pos_xy.y) + abs(params.z_dir) * int(z);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2023-12-18 22:35:26 +00:00
|
|
|
vec3 normal = normalize(imageLoad(source_normal, uv_xy).xyz * 2.0 - 1.0);
|
2019-11-05 11:01:00 +00:00
|
|
|
normal = vec3(params.x_dir) * normal.x * mix(1.0, -1.0, params.flip_x) + vec3(params.y_dir) * normal.y * mix(1.0, -1.0, params.flip_y) - vec3(params.z_dir) * normal.z;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
vec4 albedo = imageLoad(source_albedo, uv_xy);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
//determine the position in space
|
|
|
|
|
|
|
|
vec3 accum = vec3(0.0);
|
2019-11-05 11:01:00 +00:00
|
|
|
for (uint i = 0; i < params.light_count; i++) {
|
2019-10-11 02:14:56 +00:00
|
|
|
vec3 light;
|
|
|
|
vec3 light_dir;
|
2019-11-05 11:01:00 +00:00
|
|
|
if (!compute_light_at_pos(i, vec3(pos) * params.pos_multiplier, normal, light, light_dir)) {
|
2019-10-11 02:14:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
light *= albedo.rgb;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
accum += max(0.0, dot(normal, -light_dir)) * light;
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
accum += imageLoad(emission, uv_xy).xyz;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
imageStore(emission, uv_xy, vec4(accum, albedo.a));
|
|
|
|
imageStore(depth, uv_xy, vec4(z));
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MODE DYNAMIC LIGHTING
|
|
|
|
|
|
|
|
#ifdef MODE_DYNAMIC_SHRINK
|
|
|
|
|
|
|
|
{
|
|
|
|
vec4 accum = vec4(0.0);
|
|
|
|
float accum_z = 0.0;
|
|
|
|
float count = 0.0;
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
ivec2 ofs = pos_xy * 2 + ivec2(i & 1, i >> 1) - params.prev_rect_ofs;
|
|
|
|
if (any(lessThan(ofs, ivec2(0))) || any(greaterThanEqual(ofs, params.prev_rect_size))) {
|
2019-10-11 02:14:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (params.flip_x) {
|
|
|
|
ofs.x = params.prev_rect_size.x - ofs.x - 1;
|
|
|
|
}
|
|
|
|
if (params.flip_y) {
|
|
|
|
ofs.y = params.prev_rect_size.y - ofs.y - 1;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
vec4 light = imageLoad(source_light, ofs);
|
|
|
|
if (light.a == 0.0) { //ignore empty
|
2019-10-11 02:14:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
accum += light;
|
2019-11-05 11:01:00 +00:00
|
|
|
float z = imageLoad(source_depth, ofs).x;
|
|
|
|
accum_z += z * 0.5; //shrink half too
|
|
|
|
count += 1.0;
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 01:28:53 +00:00
|
|
|
if (params.on_mipmap) {
|
2019-11-05 11:01:00 +00:00
|
|
|
accum.rgb /= mix(8.0, count, params.propagation);
|
2019-11-02 01:28:53 +00:00
|
|
|
accum.a /= 8.0;
|
|
|
|
} else {
|
2019-11-05 11:01:00 +00:00
|
|
|
accum /= 4.0;
|
2019-11-02 01:28:53 +00:00
|
|
|
}
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (count == 0.0) {
|
|
|
|
accum_z = 0.0; //avoid nan
|
2019-10-11 02:14:56 +00:00
|
|
|
} else {
|
2019-11-05 11:01:00 +00:00
|
|
|
accum_z /= count;
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MODE_DYNAMIC_SHRINK_WRITE
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
imageStore(light, uv_xy, accum);
|
|
|
|
imageStore(depth, uv_xy, vec4(accum_z));
|
2019-10-11 02:14:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_DYNAMIC_SHRINK_PLOT
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (accum.a < 0.001) {
|
2019-10-11 02:14:56 +00:00
|
|
|
return; //do not blit if alpha is too low
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
ivec3 pos = params.x_dir * (params.rect_pos.x + pos_xy.x) + params.y_dir * (params.rect_pos.y + pos_xy.y) + abs(params.z_dir) * int(accum_z);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
float z_frac = fract(accum_z);
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2019-10-11 02:14:56 +00:00
|
|
|
ivec3 pos3d = pos + abs(params.z_dir) * i;
|
2019-11-05 11:01:00 +00:00
|
|
|
if (any(lessThan(pos3d, ivec3(0))) || any(greaterThanEqual(pos3d, params.limits))) {
|
2019-10-11 02:14:56 +00:00
|
|
|
//skip if offlimits
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-05 11:01:00 +00:00
|
|
|
vec4 color_blit = accum * (i == 0 ? 1.0 - z_frac : z_frac);
|
|
|
|
vec4 color = imageLoad(color_texture, pos3d);
|
|
|
|
color.rgb *= params.dynamic_range;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
color.rgb = mix(color.rgb,color_blit.rgb,color_blit.a);
|
|
|
|
color.a+=color_blit.a;
|
|
|
|
#else
|
|
|
|
|
|
|
|
float sa = 1.0 - color_blit.a;
|
|
|
|
vec4 result;
|
|
|
|
result.a = color.a * sa + color_blit.a;
|
2019-11-05 11:01:00 +00:00
|
|
|
if (result.a == 0.0) {
|
2019-10-11 02:14:56 +00:00
|
|
|
result = vec4(0.0);
|
|
|
|
} else {
|
|
|
|
result.rgb = (color.rgb * color.a * sa + color_blit.rgb * color_blit.a) / result.a;
|
|
|
|
color = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
color.rgb /= params.dynamic_range;
|
2019-11-05 11:01:00 +00:00
|
|
|
imageStore(color_texture, pos3d, color);
|
2019-10-11 02:14:56 +00:00
|
|
|
//imageStore(color_texture,pos3d,vec4(1,1,1,1));
|
|
|
|
}
|
|
|
|
#endif // MODE_DYNAMIC_SHRINK_PLOT
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // MODE DYNAMIC
|
2019-10-03 20:39:08 +00:00
|
|
|
}
|