| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
#ifdef USE_VULKAN_MEMORY_MODEL |
| 8 |
#pragma use_vulkan_memory_model |
| 9 |
#endif |
| 10 |
|
| 11 |
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in; |
| 12 |
|
| 13 |
#define DENSITY_SCALE 1024.0 |
| 14 |
|
| 15 |
#include "../cluster_data_inc.glsl" |
| 16 |
#include "../light_data_inc.glsl" |
| 17 |
|
| 18 |
#define M_PI 3.14159265359 |
| 19 |
|
| 20 |
#include "../samplers_inc.glsl" |
| 21 |
|
| 22 |
layout(set = 0, binding = 2, std430) restrict readonly buffer GlobalShaderUniformData { |
| 23 |
vec4 data[]; |
| 24 |
} |
| 25 |
global_shader_uniforms; |
| 26 |
|
| 27 |
layout(push_constant, std430) uniform Params { |
| 28 |
vec3 position; |
| 29 |
float pad; |
| 30 |
|
| 31 |
vec3 size; |
| 32 |
float pad2; |
| 33 |
|
| 34 |
ivec3 corner; |
| 35 |
uint shape; |
| 36 |
|
| 37 |
mat4 transform; |
| 38 |
} |
| 39 |
params; |
| 40 |
|
| 41 |
#ifdef NO_IMAGE_ATOMICS |
| 42 |
layout(set = 1, binding = 1) volatile buffer emissive_only_map_buffer { |
| 43 |
uint emissive_only_map[]; |
| 44 |
}; |
| 45 |
#else |
| 46 |
layout(r32ui, set = 1, binding = 1) uniform volatile uimage3D emissive_only_map; |
| 47 |
#endif |
| 48 |
|
| 49 |
layout(set = 1, binding = 2, std140) uniform SceneParams { |
| 50 |
vec2 fog_frustum_size_begin; |
| 51 |
vec2 fog_frustum_size_end; |
| 52 |
|
| 53 |
float fog_frustum_end; |
| 54 |
float z_near; // |
| 55 |
float z_far; // |
| 56 |
float time; |
| 57 |
|
| 58 |
ivec3 fog_volume_size; |
| 59 |
uint directional_light_count; // |
| 60 |
|
| 61 |
bool use_temporal_reprojection; |
| 62 |
uint temporal_frame; |
| 63 |
float detail_spread; |
| 64 |
float temporal_blend; |
| 65 |
|
| 66 |
mat4 to_prev_view; |
| 67 |
mat4 transform; |
| 68 |
} |
| 69 |
scene_params; |
| 70 |
|
| 71 |
#ifdef NO_IMAGE_ATOMICS |
| 72 |
layout(set = 1, binding = 3) volatile buffer density_only_map_buffer { |
| 73 |
uint density_only_map[]; |
| 74 |
}; |
| 75 |
layout(set = 1, binding = 4) volatile buffer light_only_map_buffer { |
| 76 |
uint light_only_map[]; |
| 77 |
}; |
| 78 |
#else |
| 79 |
layout(r32ui, set = 1, binding = 3) uniform volatile uimage3D density_only_map; |
| 80 |
layout(r32ui, set = 1, binding = 4) uniform volatile uimage3D light_only_map; |
| 81 |
#endif |
| 82 |
|
| 83 |
#ifdef MATERIAL_UNIFORMS_USED |
| 84 |
/* clang-format off */ |
| 85 |
layout(set = 2, binding = 0, std140) uniform MaterialUniforms { |
| 86 |
#MATERIAL_UNIFORMS |
| 87 |
} material; |
| 88 |
/* clang-format on */ |
| 89 |
#endif |
| 90 |
|
| 91 |
#GLOBALS |
| 92 |
|
| 93 |
float get_depth_at_pos(float cell_depth_size, int z) { |
| 94 |
float d = float(z) * cell_depth_size + cell_depth_size * 0.5; //center of voxels |
| 95 |
d = pow(d, scene_params.detail_spread); |
| 96 |
return scene_params.fog_frustum_end * d; |
| 97 |
} |
| 98 |
|
| 99 |
#define TEMPORAL_FRAMES 16 |
| 100 |
|
| 101 |
const vec3 halton_map[TEMPORAL_FRAMES] = vec3[]( |
| 102 |
vec3(0.5, 0.33333333, 0.2), |
| 103 |
vec3(0.25, 0.66666667, 0.4), |
| 104 |
vec3(0.75, 0.11111111, 0.6), |
| 105 |
vec3(0.125, 0.44444444, 0.8), |
| 106 |
vec3(0.625, 0.77777778, 0.04), |
| 107 |
vec3(0.375, 0.22222222, 0.24), |
| 108 |
vec3(0.875, 0.55555556, 0.44), |
| 109 |
vec3(0.0625, 0.88888889, 0.64), |
| 110 |
vec3(0.5625, 0.03703704, 0.84), |
| 111 |
vec3(0.3125, 0.37037037, 0.08), |
| 112 |
vec3(0.8125, 0.7037037, 0.28), |
| 113 |
vec3(0.1875, 0.14814815, 0.48), |
| 114 |
vec3(0.6875, 0.48148148, 0.68), |
| 115 |
vec3(0.4375, 0.81481481, 0.88), |
| 116 |
vec3(0.9375, 0.25925926, 0.12), |
| 117 |
vec3(0.03125, 0.59259259, 0.32)); |
| 118 |
|
| 119 |
void main() { |
| 120 |
vec3 fog_cell_size = 1.0 / vec3(scene_params.fog_volume_size); |
| 121 |
|
| 122 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz) + params.corner; |
| 123 |
if (any(greaterThanEqual(pos, scene_params.fog_volume_size))) { |
| 124 |
return; //do not compute |
| 125 |
} |
| 126 |
#ifdef NO_IMAGE_ATOMICS |
| 127 |
uint lpos = pos.z * scene_params.fog_volume_size.x * scene_params.fog_volume_size.y + pos.y * scene_params.fog_volume_size.x + pos.x; |
| 128 |
#endif |
| 129 |
|
| 130 |
vec3 posf = vec3(pos); |
| 131 |
|
| 132 |
vec3 fog_unit_pos = posf * fog_cell_size + fog_cell_size * 0.5; //center of voxels |
| 133 |
fog_unit_pos.z = pow(fog_unit_pos.z, scene_params.detail_spread); |
| 134 |
|
| 135 |
vec3 view_pos; |
| 136 |
view_pos.xy = (fog_unit_pos.xy * 2.0 - 1.0) * mix(scene_params.fog_frustum_size_begin, scene_params.fog_frustum_size_end, vec2(fog_unit_pos.z)); |
| 137 |
view_pos.z = -scene_params.fog_frustum_end * fog_unit_pos.z; |
| 138 |
view_pos.y = -view_pos.y; |
| 139 |
|
| 140 |
if (scene_params.use_temporal_reprojection) { |
| 141 |
vec3 prev_view = (scene_params.to_prev_view * vec4(view_pos, 1.0)).xyz; |
| 142 |
//undo transform into prev view |
| 143 |
prev_view.y = -prev_view.y; |
| 144 |
//z back to unit size |
| 145 |
prev_view.z /= -scene_params.fog_frustum_end; |
| 146 |
//xy back to unit size |
| 147 |
prev_view.xy /= mix(scene_params.fog_frustum_size_begin, scene_params.fog_frustum_size_end, vec2(prev_view.z)); |
| 148 |
prev_view.xy = prev_view.xy * 0.5 + 0.5; |
| 149 |
//z back to unspread value |
| 150 |
prev_view.z = pow(prev_view.z, 1.0 / scene_params.detail_spread); |
| 151 |
|
| 152 |
if (all(greaterThan(prev_view, vec3(0.0))) && all(lessThan(prev_view, vec3(1.0)))) { |
| 153 |
//reprojectinon fits |
| 154 |
// Since we can reproject, now we must jitter the current view pos. |
| 155 |
// This is done here because cells that can't reproject should not jitter. |
| 156 |
|
| 157 |
fog_unit_pos = posf * fog_cell_size + fog_cell_size * halton_map[scene_params.temporal_frame]; //center of voxels, offset by halton table |
| 158 |
fog_unit_pos.z = pow(fog_unit_pos.z, scene_params.detail_spread); |
| 159 |
|
| 160 |
view_pos.xy = (fog_unit_pos.xy * 2.0 - 1.0) * mix(scene_params.fog_frustum_size_begin, scene_params.fog_frustum_size_end, vec2(fog_unit_pos.z)); |
| 161 |
view_pos.z = -scene_params.fog_frustum_end * fog_unit_pos.z; |
| 162 |
view_pos.y = -view_pos.y; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
float density = 0.0; |
| 167 |
vec3 emission = vec3(0.0); |
| 168 |
vec3 albedo = vec3(0.0); |
| 169 |
|
| 170 |
float cell_depth_size = abs(view_pos.z - get_depth_at_pos(fog_cell_size.z, pos.z + 1)); |
| 171 |
|
| 172 |
vec4 world = scene_params.transform * vec4(view_pos, 1.0); |
| 173 |
world.xyz /= world.w; |
| 174 |
|
| 175 |
vec3 uvw = fog_unit_pos; |
| 176 |
|
| 177 |
vec4 local_pos = params.transform * world; |
| 178 |
local_pos.xyz /= local_pos.w; |
| 179 |
|
| 180 |
vec3 half_size = params.size / 2.0; |
| 181 |
float sdf = -1.0; |
| 182 |
if (params.shape == 0) { |
| 183 |
// Ellipsoid |
| 184 |
// https://www.shadertoy.com/view/tdS3DG |
| 185 |
float k0 = length(local_pos.xyz / half_size); |
| 186 |
float k1 = length(local_pos.xyz / (half_size * half_size)); |
| 187 |
sdf = k0 * (k0 - 1.0) / k1; |
| 188 |
} else if (params.shape == 1) { |
| 189 |
// Cone |
| 190 |
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm |
| 191 |
|
| 192 |
// Compute the cone angle automatically to fit within the volume's size. |
| 193 |
float inv_height = 1.0 / max(0.001, half_size.y); |
| 194 |
float radius = 1.0 / max(0.001, (min(half_size.x, half_size.z) * 0.5)); |
| 195 |
float hypotenuse = sqrt(radius * radius + inv_height * inv_height); |
| 196 |
float rsin = radius / hypotenuse; |
| 197 |
float rcos = inv_height / hypotenuse; |
| 198 |
vec2 c = vec2(rsin, rcos); |
| 199 |
|
| 200 |
float q = length(local_pos.xz); |
| 201 |
sdf = max(dot(c, vec2(q, local_pos.y - half_size.y)), -half_size.y - local_pos.y); |
| 202 |
} else if (params.shape == 2) { |
| 203 |
// Cylinder |
| 204 |
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm |
| 205 |
vec2 d = abs(vec2(length(local_pos.xz), local_pos.y)) - vec2(min(half_size.x, half_size.z), half_size.y); |
| 206 |
sdf = min(max(d.x, d.y), 0.0) + length(max(d, 0.0)); |
| 207 |
} else if (params.shape == 3) { |
| 208 |
// Box |
| 209 |
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm |
| 210 |
vec3 q = abs(local_pos.xyz) - half_size; |
| 211 |
sdf = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); |
| 212 |
} |
| 213 |
|
| 214 |
float cull_mask = 1.0; //used to cull cells that do not contribute |
| 215 |
if (params.shape <= 3) { |
| 216 |
#ifndef SDF_USED |
| 217 |
cull_mask = 1.0 - smoothstep(-0.1, 0.0, sdf); |
| 218 |
#endif |
| 219 |
uvw = clamp((local_pos.xyz + half_size) / params.size, 0.0, 1.0); |
| 220 |
} |
| 221 |
|
| 222 |
if (cull_mask > 0.0) { |
| 223 |
{ |
| 224 |
#CODE : FOG |
| 225 |
} |
| 226 |
|
| 227 |
#ifdef DENSITY_USED |
| 228 |
density *= cull_mask; |
| 229 |
if (abs(density) > 0.001) { |
| 230 |
int final_density = int(density * DENSITY_SCALE); |
| 231 |
#ifdef NO_IMAGE_ATOMICS |
| 232 |
atomicAdd(density_only_map[lpos], uint(final_density)); |
| 233 |
#else |
| 234 |
imageAtomicAdd(density_only_map, pos, uint(final_density)); |
| 235 |
#endif |
| 236 |
|
| 237 |
#ifdef EMISSION_USED |
| 238 |
{ |
| 239 |
emission *= clamp(density, 0.0, 1.0); |
| 240 |
emission = clamp(emission, vec3(0.0), vec3(4.0)); |
| 241 |
// Scale to fit into R11G11B10 with a range of 0-4 |
| 242 |
uvec3 emission_u = uvec3(emission.r * 511.0, emission.g * 511.0, emission.b * 255.0); |
| 243 |
// R and G have 11 bits each and B has 10. Then pack them into a 32 bit uint |
| 244 |
uint final_emission = emission_u.r << 21 | emission_u.g << 10 | emission_u.b; |
| 245 |
#ifdef NO_IMAGE_ATOMICS |
| 246 |
uint prev_emission = atomicAdd(emissive_only_map[lpos], final_emission); |
| 247 |
#else |
| 248 |
uint prev_emission = imageAtomicAdd(emissive_only_map, pos, final_emission); |
| 249 |
#endif |
| 250 |
|
| 251 |
// Adding can lead to colors overflowing, so validate |
| 252 |
uvec3 prev_emission_u = uvec3(prev_emission >> 21, (prev_emission << 11) >> 21, prev_emission % 1024); |
| 253 |
uint add_emission = final_emission + prev_emission; |
| 254 |
uvec3 add_emission_u = uvec3(add_emission >> 21, (add_emission << 11) >> 21, add_emission % 1024); |
| 255 |
|
| 256 |
bvec3 overflowing = lessThan(add_emission_u, prev_emission_u + emission_u); |
| 257 |
|
| 258 |
if (any(overflowing)) { |
| 259 |
uvec3 overflow_factor = mix(uvec3(0), uvec3(2047 << 21, 2047 << 10, 1023), overflowing); |
| 260 |
uint force_max = overflow_factor.r | overflow_factor.g | overflow_factor.b; |
| 261 |
#ifdef NO_IMAGE_ATOMICS |
| 262 |
atomicOr(emissive_only_map[lpos], force_max); |
| 263 |
#else |
| 264 |
imageAtomicOr(emissive_only_map, pos, force_max); |
| 265 |
#endif |
| 266 |
} |
| 267 |
} |
| 268 |
#endif |
| 269 |
#ifdef ALBEDO_USED |
| 270 |
{ |
| 271 |
vec3 scattering = albedo * clamp(density, 0.0, 1.0); |
| 272 |
scattering = clamp(scattering, vec3(0.0), vec3(1.0)); |
| 273 |
uvec3 scattering_u = uvec3(scattering.r * 2047.0, scattering.g * 2047.0, scattering.b * 1023.0); |
| 274 |
// R and G have 11 bits each and B has 10. Then pack them into a 32 bit uint |
| 275 |
uint final_scattering = scattering_u.r << 21 | scattering_u.g << 10 | scattering_u.b; |
| 276 |
#ifdef NO_IMAGE_ATOMICS |
| 277 |
uint prev_scattering = atomicAdd(light_only_map[lpos], final_scattering); |
| 278 |
#else |
| 279 |
uint prev_scattering = imageAtomicAdd(light_only_map, pos, final_scattering); |
| 280 |
#endif |
| 281 |
|
| 282 |
// Adding can lead to colors overflowing, so validate |
| 283 |
uvec3 prev_scattering_u = uvec3(prev_scattering >> 21, (prev_scattering << 11) >> 21, prev_scattering % 1024); |
| 284 |
uint add_scattering = final_scattering + prev_scattering; |
| 285 |
uvec3 add_scattering_u = uvec3(add_scattering >> 21, (add_scattering << 11) >> 21, add_scattering % 1024); |
| 286 |
|
| 287 |
bvec3 overflowing = lessThan(add_scattering_u, prev_scattering_u + scattering_u); |
| 288 |
|
| 289 |
if (any(overflowing)) { |
| 290 |
uvec3 overflow_factor = mix(uvec3(0), uvec3(2047 << 21, 2047 << 10, 1023), overflowing); |
| 291 |
uint force_max = overflow_factor.r | overflow_factor.g | overflow_factor.b; |
| 292 |
#ifdef NO_IMAGE_ATOMICS |
| 293 |
atomicOr(light_only_map[lpos], force_max); |
| 294 |
#else |
| 295 |
imageAtomicOr(light_only_map, pos, force_max); |
| 296 |
#endif |
| 297 |
} |
| 298 |
} |
| 299 |
#endif // ALBEDO_USED |
| 300 |
} |
| 301 |
#endif // DENSITY_USED |
| 302 |
} |
| 303 |
} |