volumetric_fog.glsl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
  5. #define SAMPLER_NEAREST_CLAMP 0
  6. #define SAMPLER_LINEAR_CLAMP 1
  7. #define SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP 2
  8. #define SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP 3
  9. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_CLAMP 4
  10. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_CLAMP 5
  11. #define SAMPLER_NEAREST_REPEAT 6
  12. #define SAMPLER_LINEAR_REPEAT 7
  13. #define SAMPLER_NEAREST_WITH_MIPMAPS_REPEAT 8
  14. #define SAMPLER_LINEAR_WITH_MIPMAPS_REPEAT 9
  15. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_REPEAT 10
  16. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_REPEAT 11
  17. #define DENSITY_SCALE 1024.0
  18. #include "cluster_data_inc.glsl"
  19. #include "light_data_inc.glsl"
  20. #define M_PI 3.14159265359
  21. layout(set = 0, binding = 1) uniform sampler material_samplers[12];
  22. layout(set = 0, binding = 2, std430) restrict readonly buffer GlobalVariableData {
  23. vec4 data[];
  24. }
  25. global_variables;
  26. layout(push_constant, binding = 0, std430) uniform Params {
  27. vec3 position;
  28. float pad;
  29. vec3 extents;
  30. float pad2;
  31. ivec3 corner;
  32. uint shape;
  33. mat4 transform;
  34. }
  35. params;
  36. layout(r32ui, set = 1, binding = 1) uniform volatile uimage3D emissive_only_map;
  37. layout(set = 1, binding = 2, std140) uniform SceneParams {
  38. vec2 fog_frustum_size_begin;
  39. vec2 fog_frustum_size_end;
  40. float fog_frustum_end;
  41. float z_near; //
  42. float z_far; //
  43. float time;
  44. ivec3 fog_volume_size;
  45. uint directional_light_count; //
  46. bool use_temporal_reprojection;
  47. uint temporal_frame;
  48. float detail_spread;
  49. float temporal_blend;
  50. mat4 to_prev_view;
  51. mat4 transform;
  52. }
  53. scene_params;
  54. layout(r32ui, set = 1, binding = 3) uniform volatile uimage3D density_only_map;
  55. layout(r32ui, set = 1, binding = 4) uniform volatile uimage3D light_only_map;
  56. #ifdef MATERIAL_UNIFORMS_USED
  57. layout(set = 2, binding = 0, std140) uniform MaterialUniforms{
  58. #MATERIAL_UNIFORMS
  59. } material;
  60. #endif
  61. #GLOBALS
  62. float get_depth_at_pos(float cell_depth_size, int z) {
  63. float d = float(z) * cell_depth_size + cell_depth_size * 0.5; //center of voxels
  64. d = pow(d, scene_params.detail_spread);
  65. return scene_params.fog_frustum_end * d;
  66. }
  67. #define TEMPORAL_FRAMES 16
  68. const vec3 halton_map[TEMPORAL_FRAMES] = vec3[](
  69. vec3(0.5, 0.33333333, 0.2),
  70. vec3(0.25, 0.66666667, 0.4),
  71. vec3(0.75, 0.11111111, 0.6),
  72. vec3(0.125, 0.44444444, 0.8),
  73. vec3(0.625, 0.77777778, 0.04),
  74. vec3(0.375, 0.22222222, 0.24),
  75. vec3(0.875, 0.55555556, 0.44),
  76. vec3(0.0625, 0.88888889, 0.64),
  77. vec3(0.5625, 0.03703704, 0.84),
  78. vec3(0.3125, 0.37037037, 0.08),
  79. vec3(0.8125, 0.7037037, 0.28),
  80. vec3(0.1875, 0.14814815, 0.48),
  81. vec3(0.6875, 0.48148148, 0.68),
  82. vec3(0.4375, 0.81481481, 0.88),
  83. vec3(0.9375, 0.25925926, 0.12),
  84. vec3(0.03125, 0.59259259, 0.32));
  85. void main() {
  86. vec3 fog_cell_size = 1.0 / vec3(scene_params.fog_volume_size);
  87. ivec3 pos = ivec3(gl_GlobalInvocationID.xyz) + params.corner;
  88. if (any(greaterThanEqual(pos, scene_params.fog_volume_size))) {
  89. return; //do not compute
  90. }
  91. vec3 posf = vec3(pos);
  92. vec3 fog_unit_pos = posf * fog_cell_size + fog_cell_size * 0.5; //center of voxels
  93. fog_unit_pos.z = pow(fog_unit_pos.z, scene_params.detail_spread);
  94. vec3 view_pos;
  95. 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));
  96. view_pos.z = -scene_params.fog_frustum_end * fog_unit_pos.z;
  97. view_pos.y = -view_pos.y;
  98. if (scene_params.use_temporal_reprojection) {
  99. vec3 prev_view = (scene_params.to_prev_view * vec4(view_pos, 1.0)).xyz;
  100. //undo transform into prev view
  101. prev_view.y = -prev_view.y;
  102. //z back to unit size
  103. prev_view.z /= -scene_params.fog_frustum_end;
  104. //xy back to unit size
  105. prev_view.xy /= mix(scene_params.fog_frustum_size_begin, scene_params.fog_frustum_size_end, vec2(prev_view.z));
  106. prev_view.xy = prev_view.xy * 0.5 + 0.5;
  107. //z back to unspread value
  108. prev_view.z = pow(prev_view.z, 1.0 / scene_params.detail_spread);
  109. if (all(greaterThan(prev_view, vec3(0.0))) && all(lessThan(prev_view, vec3(1.0)))) {
  110. //reprojectinon fits
  111. // Since we can reproject, now we must jitter the current view pos.
  112. // This is done here because cells that can't reproject should not jitter.
  113. fog_unit_pos = posf * fog_cell_size + fog_cell_size * halton_map[scene_params.temporal_frame]; //center of voxels, offset by halton table
  114. fog_unit_pos.z = pow(fog_unit_pos.z, scene_params.detail_spread);
  115. 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));
  116. view_pos.z = -scene_params.fog_frustum_end * fog_unit_pos.z;
  117. view_pos.y = -view_pos.y;
  118. }
  119. }
  120. float density = 0.0;
  121. vec3 emission = vec3(0.0);
  122. vec3 albedo = vec3(0.0);
  123. float cell_depth_size = abs(view_pos.z - get_depth_at_pos(fog_cell_size.z, pos.z + 1));
  124. vec4 world = scene_params.transform * vec4(view_pos, 1.0);
  125. world.xyz /= world.w;
  126. vec3 uvw = fog_unit_pos;
  127. vec4 local_pos = params.transform * world;
  128. local_pos.xyz /= local_pos.w;
  129. float sdf = -1.0;
  130. if (params.shape == 0) {
  131. //Ellipsoid
  132. // https://www.shadertoy.com/view/tdS3DG
  133. float k0 = length(local_pos.xyz / params.extents);
  134. float k1 = length(local_pos.xyz / (params.extents * params.extents));
  135. sdf = k0 * (k0 - 1.0) / k1;
  136. } else if (params.shape == 1) {
  137. // Box
  138. // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
  139. vec3 q = abs(local_pos.xyz) - params.extents;
  140. sdf = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
  141. }
  142. float cull_mask = 1.0; //used to cull cells that do not contribute
  143. if (params.shape <= 1) {
  144. #ifndef SDF_USED
  145. cull_mask = 1.0 - smoothstep(-0.1, 0.0, sdf);
  146. #endif
  147. uvw = clamp((local_pos.xyz + params.extents) / (2.0 * params.extents), 0.0, 1.0);
  148. }
  149. if (cull_mask > 0.0) {
  150. {
  151. #CODE : FOG
  152. }
  153. #ifdef DENSITY_USED
  154. density *= cull_mask;
  155. if (abs(density) > 0.001) {
  156. int final_density = int(density * DENSITY_SCALE);
  157. imageAtomicAdd(density_only_map, pos, uint(final_density));
  158. #ifdef EMISSION_USED
  159. {
  160. emission *= clamp(density, 0.0, 1.0);
  161. emission = clamp(emission, vec3(0.0), vec3(4.0));
  162. // Scale to fit into R11G11B10 with a range of 0-4
  163. uvec3 emission_u = uvec3(emission.r * 511.0, emission.g * 511.0, emission.b * 255.0);
  164. // R and G have 11 bits each and B has 10. Then pack them into a 32 bit uint
  165. uint final_emission = emission_u.r << 21 | emission_u.g << 10 | emission_u.b;
  166. uint prev_emission = imageAtomicAdd(emissive_only_map, pos, final_emission);
  167. // Adding can lead to colors overflowing, so validate
  168. uvec3 prev_emission_u = uvec3(prev_emission >> 21, (prev_emission << 11) >> 21, prev_emission % 1024);
  169. uint add_emission = final_emission + prev_emission;
  170. uvec3 add_emission_u = uvec3(add_emission >> 21, (add_emission << 11) >> 21, add_emission % 1024);
  171. bvec3 overflowing = lessThan(add_emission_u, prev_emission_u + emission_u);
  172. if (any(overflowing)) {
  173. uvec3 overflow_factor = mix(uvec3(0), uvec3(2047 << 21, 2047 << 10, 1023), overflowing);
  174. uint force_max = overflow_factor.r | overflow_factor.g | overflow_factor.b;
  175. imageAtomicOr(emissive_only_map, pos, force_max);
  176. }
  177. }
  178. #endif
  179. #ifdef ALBEDO_USED
  180. {
  181. vec3 scattering = albedo * clamp(density, 0.0, 1.0);
  182. scattering = clamp(scattering, vec3(0.0), vec3(1.0));
  183. uvec3 scattering_u = uvec3(scattering.r * 2047.0, scattering.g * 2047.0, scattering.b * 1023.0);
  184. // R and G have 11 bits each and B has 10. Then pack them into a 32 bit uint
  185. uint final_scattering = scattering_u.r << 21 | scattering_u.g << 10 | scattering_u.b;
  186. uint prev_scattering = imageAtomicAdd(light_only_map, pos, final_scattering);
  187. // Adding can lead to colors overflowing, so validate
  188. uvec3 prev_scattering_u = uvec3(prev_scattering >> 21, (prev_scattering << 11) >> 21, prev_scattering % 1024);
  189. uint add_scattering = final_scattering + prev_scattering;
  190. uvec3 add_scattering_u = uvec3(add_scattering >> 21, (add_scattering << 11) >> 21, add_scattering % 1024);
  191. bvec3 overflowing = lessThan(add_scattering_u, prev_scattering_u + scattering_u);
  192. if (any(overflowing)) {
  193. uvec3 overflow_factor = mix(uvec3(0), uvec3(2047 << 21, 2047 << 10, 1023), overflowing);
  194. uint force_max = overflow_factor.r | overflow_factor.g | overflow_factor.b;
  195. imageAtomicOr(light_only_map, pos, force_max);
  196. }
  197. }
  198. #endif // ALBEDO_USED
  199. }
  200. #endif // DENSITY_USED
  201. }
  202. }