volumetric_fog_process.glsl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. #ifdef USE_VULKAN_MEMORY_MODEL
  5. #pragma use_vulkan_memory_model
  6. #endif
  7. #ifdef MODE_DENSITY
  8. layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
  9. #else
  10. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  11. #endif
  12. #include "../cluster_data_inc.glsl"
  13. #include "../light_data_inc.glsl"
  14. #define M_PI 3.14159265359
  15. #define DENSITY_SCALE 1024.0
  16. layout(set = 0, binding = 1) uniform texture2D shadow_atlas;
  17. layout(set = 0, binding = 2) uniform texture2D directional_shadow_atlas;
  18. layout(set = 0, binding = 3, std430) restrict readonly buffer OmniLights {
  19. LightData data[];
  20. }
  21. omni_lights;
  22. layout(set = 0, binding = 4, std430) restrict readonly buffer SpotLights {
  23. LightData data[];
  24. }
  25. spot_lights;
  26. layout(set = 0, binding = 5, std140) uniform DirectionalLights {
  27. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  28. }
  29. directional_lights;
  30. layout(set = 0, binding = 6, std430) buffer restrict readonly ClusterBuffer {
  31. uint data[];
  32. }
  33. cluster_buffer;
  34. layout(set = 0, binding = 7) uniform sampler linear_sampler;
  35. #ifdef MODE_DENSITY
  36. layout(rgba16f, set = 0, binding = 8) uniform restrict writeonly image3D density_map;
  37. #endif
  38. #ifdef MODE_FOG
  39. layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D density_map;
  40. layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D fog_map;
  41. #endif
  42. #ifdef MODE_COPY
  43. layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D source_map;
  44. layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D dest_map;
  45. #endif
  46. #ifdef MODE_FILTER
  47. layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D source_map;
  48. layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D dest_map;
  49. #endif
  50. layout(set = 0, binding = 10) uniform sampler shadow_sampler;
  51. #define MAX_VOXEL_GI_INSTANCES 8
  52. struct VoxelGIData {
  53. mat4 xform; // 64 - 64
  54. vec3 bounds; // 12 - 76
  55. float dynamic_range; // 4 - 80
  56. float bias; // 4 - 84
  57. float normal_bias; // 4 - 88
  58. bool blend_ambient; // 4 - 92
  59. uint mipmaps; // 4 - 96
  60. vec3 pad; // 12 - 108
  61. float exposure_normalization; // 4 - 112
  62. };
  63. layout(set = 0, binding = 11, std140) uniform VoxelGIs {
  64. VoxelGIData data[MAX_VOXEL_GI_INSTANCES];
  65. }
  66. voxel_gi_instances;
  67. layout(set = 0, binding = 12) uniform texture3D voxel_gi_textures[MAX_VOXEL_GI_INSTANCES];
  68. layout(set = 0, binding = 13) uniform sampler linear_sampler_with_mipmaps;
  69. #ifdef ENABLE_SDFGI
  70. // SDFGI Integration on set 1
  71. #define SDFGI_MAX_CASCADES 8
  72. struct SDFVoxelGICascadeData {
  73. vec3 position;
  74. float to_probe;
  75. ivec3 probe_world_offset;
  76. float to_cell; // 1/bounds * grid_size
  77. vec3 pad;
  78. float exposure_normalization;
  79. };
  80. layout(set = 1, binding = 0, std140) uniform SDFGI {
  81. vec3 grid_size;
  82. uint max_cascades;
  83. bool use_occlusion;
  84. int probe_axis_size;
  85. float probe_to_uvw;
  86. float normal_bias;
  87. vec3 lightprobe_tex_pixel_size;
  88. float energy;
  89. vec3 lightprobe_uv_offset;
  90. float y_mult;
  91. vec3 occlusion_clamp;
  92. uint pad3;
  93. vec3 occlusion_renormalize;
  94. uint pad4;
  95. vec3 cascade_probe_size;
  96. uint pad5;
  97. SDFVoxelGICascadeData cascades[SDFGI_MAX_CASCADES];
  98. }
  99. sdfgi;
  100. layout(set = 1, binding = 1) uniform texture2DArray sdfgi_ambient_texture;
  101. layout(set = 1, binding = 2) uniform texture3D sdfgi_occlusion_texture;
  102. #endif //SDFGI
  103. layout(set = 0, binding = 14, std140) uniform Params {
  104. vec2 fog_frustum_size_begin;
  105. vec2 fog_frustum_size_end;
  106. float fog_frustum_end;
  107. float ambient_inject;
  108. float z_far;
  109. int filter_axis;
  110. vec3 ambient_color;
  111. float sky_contribution;
  112. ivec3 fog_volume_size;
  113. uint directional_light_count;
  114. vec3 base_emission;
  115. float base_density;
  116. vec3 base_scattering;
  117. float phase_g;
  118. float detail_spread;
  119. float gi_inject;
  120. uint max_voxel_gi_instances;
  121. uint cluster_type_size;
  122. vec2 screen_size;
  123. uint cluster_shift;
  124. uint cluster_width;
  125. uint max_cluster_element_count_div_32;
  126. bool use_temporal_reprojection;
  127. uint temporal_frame;
  128. float temporal_blend;
  129. mat3x4 cam_rotation;
  130. mat4 to_prev_view;
  131. mat3 radiance_inverse_xform;
  132. }
  133. params;
  134. #ifndef MODE_COPY
  135. layout(set = 0, binding = 15) uniform texture3D prev_density_texture;
  136. #ifdef NO_IMAGE_ATOMICS
  137. layout(set = 0, binding = 16) buffer density_only_map_buffer {
  138. uint density_only_map[];
  139. };
  140. layout(set = 0, binding = 17) buffer light_only_map_buffer {
  141. uint light_only_map[];
  142. };
  143. layout(set = 0, binding = 18) buffer emissive_only_map_buffer {
  144. uint emissive_only_map[];
  145. };
  146. #else
  147. layout(r32ui, set = 0, binding = 16) uniform uimage3D density_only_map;
  148. layout(r32ui, set = 0, binding = 17) uniform uimage3D light_only_map;
  149. layout(r32ui, set = 0, binding = 18) uniform uimage3D emissive_only_map;
  150. #endif
  151. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  152. layout(set = 0, binding = 19) uniform textureCubeArray sky_texture;
  153. #else
  154. layout(set = 0, binding = 19) uniform textureCube sky_texture;
  155. #endif
  156. #endif // MODE_COPY
  157. float get_depth_at_pos(float cell_depth_size, int z) {
  158. float d = float(z) * cell_depth_size + cell_depth_size * 0.5; //center of voxels
  159. d = pow(d, params.detail_spread);
  160. return params.fog_frustum_end * d;
  161. }
  162. vec3 hash3f(uvec3 x) {
  163. x = ((x >> 16) ^ x) * 0x45d9f3b;
  164. x = ((x >> 16) ^ x) * 0x45d9f3b;
  165. x = (x >> 16) ^ x;
  166. return vec3(x & 0xFFFFF) / vec3(float(0xFFFFF));
  167. }
  168. float get_omni_attenuation(float dist, float inv_range, float decay) {
  169. float nd = dist * inv_range;
  170. nd *= nd;
  171. nd *= nd; // nd^4
  172. nd = max(1.0 - nd, 0.0);
  173. nd *= nd; // nd^2
  174. return nd * pow(max(dist, 0.0001), -decay);
  175. }
  176. void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
  177. uint item_min_max = cluster_buffer.data[p_offset];
  178. item_min = item_min_max & 0xFFFF;
  179. item_max = item_min_max >> 16;
  180. item_from = item_min >> 5;
  181. 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
  182. }
  183. uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
  184. int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
  185. int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
  186. return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
  187. }
  188. float henyey_greenstein(float cos_theta, float g) {
  189. const float k = 0.0795774715459; // 1 / (4 * PI)
  190. return k * (1.0 - g * g) / (pow(1.0 + g * g - 2.0 * g * cos_theta, 1.5));
  191. }
  192. #define TEMPORAL_FRAMES 16
  193. const vec3 halton_map[TEMPORAL_FRAMES] = vec3[](
  194. vec3(0.5, 0.33333333, 0.2),
  195. vec3(0.25, 0.66666667, 0.4),
  196. vec3(0.75, 0.11111111, 0.6),
  197. vec3(0.125, 0.44444444, 0.8),
  198. vec3(0.625, 0.77777778, 0.04),
  199. vec3(0.375, 0.22222222, 0.24),
  200. vec3(0.875, 0.55555556, 0.44),
  201. vec3(0.0625, 0.88888889, 0.64),
  202. vec3(0.5625, 0.03703704, 0.84),
  203. vec3(0.3125, 0.37037037, 0.08),
  204. vec3(0.8125, 0.7037037, 0.28),
  205. vec3(0.1875, 0.14814815, 0.48),
  206. vec3(0.6875, 0.48148148, 0.68),
  207. vec3(0.4375, 0.81481481, 0.88),
  208. vec3(0.9375, 0.25925926, 0.12),
  209. vec3(0.03125, 0.59259259, 0.32));
  210. // Higher values will make light in volumetric fog fade out sooner when it's occluded by shadow.
  211. const float INV_FOG_FADE = 10.0;
  212. void main() {
  213. vec3 fog_cell_size = 1.0 / vec3(params.fog_volume_size);
  214. #ifdef MODE_DENSITY
  215. ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
  216. if (any(greaterThanEqual(pos, params.fog_volume_size))) {
  217. return; //do not compute
  218. }
  219. #ifdef NO_IMAGE_ATOMICS
  220. uint lpos = pos.z * params.fog_volume_size.x * params.fog_volume_size.y + pos.y * params.fog_volume_size.x + pos.x;
  221. #endif
  222. vec3 posf = vec3(pos);
  223. //posf += mix(vec3(0.0),vec3(1.0),0.3) * hash3f(uvec3(pos)) * 2.0 - 1.0;
  224. vec3 fog_unit_pos = posf * fog_cell_size + fog_cell_size * 0.5; //center of voxels
  225. uvec2 screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
  226. uvec2 cluster_pos = screen_pos >> params.cluster_shift;
  227. uint cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
  228. //positions in screen are too spread apart, no hopes for optimizing with subgroups
  229. fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
  230. vec3 view_pos;
  231. 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));
  232. view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
  233. view_pos.y = -view_pos.y;
  234. vec4 reprojected_density = vec4(0.0);
  235. float reproject_amount = 0.0;
  236. if (params.use_temporal_reprojection) {
  237. vec3 prev_view = (params.to_prev_view * vec4(view_pos, 1.0)).xyz;
  238. //undo transform into prev view
  239. prev_view.y = -prev_view.y;
  240. //z back to unit size
  241. prev_view.z /= -params.fog_frustum_end;
  242. //xy back to unit size
  243. prev_view.xy /= mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(prev_view.z));
  244. prev_view.xy = prev_view.xy * 0.5 + 0.5;
  245. //z back to unspread value
  246. prev_view.z = pow(prev_view.z, 1.0 / params.detail_spread);
  247. if (all(greaterThan(prev_view, vec3(0.0))) && all(lessThan(prev_view, vec3(1.0)))) {
  248. //reprojectinon fits
  249. reprojected_density = textureLod(sampler3D(prev_density_texture, linear_sampler), prev_view, 0.0);
  250. reproject_amount = params.temporal_blend;
  251. // Since we can reproject, now we must jitter the current view pos.
  252. // This is done here because cells that can't reproject should not jitter.
  253. fog_unit_pos = posf * fog_cell_size + fog_cell_size * halton_map[params.temporal_frame]; //center of voxels, offset by halton table
  254. screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
  255. cluster_pos = screen_pos >> params.cluster_shift;
  256. cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
  257. //positions in screen are too spread apart, no hopes for optimizing with subgroups
  258. fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
  259. 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));
  260. view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
  261. view_pos.y = -view_pos.y;
  262. }
  263. }
  264. uint cluster_z = uint(clamp((abs(view_pos.z) / params.z_far) * 32.0, 0.0, 31.0));
  265. vec3 total_light = vec3(0.0);
  266. float total_density = params.base_density;
  267. #ifdef NO_IMAGE_ATOMICS
  268. uint local_density = density_only_map[lpos];
  269. #else
  270. uint local_density = imageLoad(density_only_map, pos).x;
  271. #endif
  272. total_density += float(int(local_density)) / DENSITY_SCALE;
  273. total_density = max(0.0, total_density);
  274. #ifdef NO_IMAGE_ATOMICS
  275. uint scattering_u = light_only_map[lpos];
  276. #else
  277. uint scattering_u = imageLoad(light_only_map, pos).x;
  278. #endif
  279. vec3 scattering = vec3(scattering_u >> 21, (scattering_u << 11) >> 21, scattering_u % 1024) / vec3(2047.0, 2047.0, 1023.0);
  280. scattering += params.base_scattering * params.base_density;
  281. #ifdef NO_IMAGE_ATOMICS
  282. uint emission_u = emissive_only_map[lpos];
  283. #else
  284. uint emission_u = imageLoad(emissive_only_map, pos).x;
  285. #endif
  286. vec3 emission = vec3(emission_u >> 21, (emission_u << 11) >> 21, emission_u % 1024) / vec3(511.0, 511.0, 255.0);
  287. emission += params.base_emission * params.base_density;
  288. float cell_depth_size = abs(view_pos.z - get_depth_at_pos(fog_cell_size.z, pos.z + 1));
  289. //compute directional lights
  290. if (total_density > 0.00005) {
  291. for (uint i = 0; i < params.directional_light_count; i++) {
  292. if (directional_lights.data[i].volumetric_fog_energy > 0.001) {
  293. vec3 shadow_attenuation = vec3(1.0);
  294. if (directional_lights.data[i].shadow_opacity > 0.001) {
  295. float depth_z = -view_pos.z;
  296. vec4 pssm_coord;
  297. vec3 light_dir = directional_lights.data[i].direction;
  298. vec4 v = vec4(view_pos, 1.0);
  299. float z_range;
  300. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  301. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  302. pssm_coord /= pssm_coord.w;
  303. z_range = directional_lights.data[i].shadow_z_range.x;
  304. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  305. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  306. pssm_coord /= pssm_coord.w;
  307. z_range = directional_lights.data[i].shadow_z_range.y;
  308. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  309. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  310. pssm_coord /= pssm_coord.w;
  311. z_range = directional_lights.data[i].shadow_z_range.z;
  312. } else {
  313. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  314. pssm_coord /= pssm_coord.w;
  315. z_range = directional_lights.data[i].shadow_z_range.w;
  316. }
  317. float depth = texture(sampler2D(directional_shadow_atlas, linear_sampler), pssm_coord.xy).r;
  318. float shadow = exp(min(0.0, (pssm_coord.z - depth)) * z_range * INV_FOG_FADE);
  319. 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
  320. shadow_attenuation = mix(vec3(1.0 - directional_lights.data[i].shadow_opacity), vec3(1.0), shadow);
  321. }
  322. total_light += shadow_attenuation * directional_lights.data[i].color * directional_lights.data[i].energy * henyey_greenstein(dot(normalize(view_pos), normalize(directional_lights.data[i].direction)), params.phase_g) * directional_lights.data[i].volumetric_fog_energy;
  323. }
  324. }
  325. // Compute light from sky
  326. if (params.ambient_inject > 0.0) {
  327. vec3 isotropic = vec3(0.0);
  328. vec3 anisotropic = vec3(0.0);
  329. if (params.sky_contribution > 0.0) {
  330. float mip_bias = 2.0 + total_density * (MAX_SKY_LOD - 2.0); // Not physically based, but looks nice
  331. vec3 scatter_direction = (params.radiance_inverse_xform * normalize(view_pos)) * sign(params.phase_g);
  332. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  333. isotropic = texture(samplerCubeArray(sky_texture, linear_sampler_with_mipmaps), vec4(0.0, 1.0, 0.0, mip_bias)).rgb;
  334. anisotropic = texture(samplerCubeArray(sky_texture, linear_sampler_with_mipmaps), vec4(scatter_direction, mip_bias)).rgb;
  335. #else
  336. isotropic = textureLod(samplerCube(sky_texture, linear_sampler_with_mipmaps), vec3(0.0, 1.0, 0.0), mip_bias).rgb;
  337. anisotropic = textureLod(samplerCube(sky_texture, linear_sampler_with_mipmaps), vec3(scatter_direction), mip_bias).rgb;
  338. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  339. }
  340. total_light += mix(params.ambient_color, mix(isotropic, anisotropic, abs(params.phase_g)), params.sky_contribution) * params.ambient_inject;
  341. }
  342. //compute lights from cluster
  343. { //omni lights
  344. uint cluster_omni_offset = cluster_offset;
  345. uint item_min;
  346. uint item_max;
  347. uint item_from;
  348. uint item_to;
  349. cluster_get_item_range(cluster_omni_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  350. for (uint i = item_from; i < item_to; i++) {
  351. uint mask = cluster_buffer.data[cluster_omni_offset + i];
  352. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  353. uint merged_mask = mask;
  354. while (merged_mask != 0) {
  355. uint bit = findMSB(merged_mask);
  356. merged_mask &= ~(1 << bit);
  357. uint light_index = 32 * i + bit;
  358. //if (!bool(omni_omni_lights.data[light_index].mask & draw_call.layer_mask)) {
  359. // continue; //not masked
  360. //}
  361. vec3 light_pos = omni_lights.data[light_index].position;
  362. float d = distance(omni_lights.data[light_index].position, view_pos);
  363. float shadow_attenuation = 1.0;
  364. if (omni_lights.data[light_index].volumetric_fog_energy > 0.001 && d * omni_lights.data[light_index].inv_radius < 1.0) {
  365. float attenuation = get_omni_attenuation(d, omni_lights.data[light_index].inv_radius, omni_lights.data[light_index].attenuation);
  366. vec3 light = omni_lights.data[light_index].color;
  367. if (omni_lights.data[light_index].shadow_opacity > 0.001) {
  368. //has shadow
  369. vec4 uv_rect = omni_lights.data[light_index].atlas_rect;
  370. vec2 flip_offset = omni_lights.data[light_index].direction.xy;
  371. vec3 local_vert = (omni_lights.data[light_index].shadow_matrix * vec4(view_pos, 1.0)).xyz;
  372. float shadow_len = length(local_vert); //need to remember shadow len from here
  373. vec3 shadow_sample = normalize(local_vert);
  374. if (shadow_sample.z >= 0.0) {
  375. uv_rect.xy += flip_offset;
  376. }
  377. shadow_sample.z = 1.0 + abs(shadow_sample.z);
  378. vec3 pos = vec3(shadow_sample.xy / shadow_sample.z, shadow_len - omni_lights.data[light_index].shadow_bias);
  379. pos.z *= omni_lights.data[light_index].inv_radius;
  380. pos.z = 1.0 - pos.z;
  381. pos.xy = pos.xy * 0.5 + 0.5;
  382. pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
  383. float depth = texture(sampler2D(shadow_atlas, linear_sampler), pos.xy).r;
  384. shadow_attenuation = mix(1.0 - omni_lights.data[light_index].shadow_opacity, 1.0, exp(min(0.0, (pos.z - depth)) / omni_lights.data[light_index].inv_radius * INV_FOG_FADE));
  385. }
  386. total_light += light * attenuation * shadow_attenuation * henyey_greenstein(dot(normalize(light_pos - view_pos), normalize(view_pos)), params.phase_g) * omni_lights.data[light_index].volumetric_fog_energy;
  387. }
  388. }
  389. }
  390. }
  391. { //spot lights
  392. uint cluster_spot_offset = cluster_offset + params.cluster_type_size;
  393. uint item_min;
  394. uint item_max;
  395. uint item_from;
  396. uint item_to;
  397. cluster_get_item_range(cluster_spot_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  398. for (uint i = item_from; i < item_to; i++) {
  399. uint mask = cluster_buffer.data[cluster_spot_offset + i];
  400. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  401. uint merged_mask = mask;
  402. while (merged_mask != 0) {
  403. uint bit = findMSB(merged_mask);
  404. merged_mask &= ~(1 << bit);
  405. //if (!bool(omni_lights.data[light_index].mask & draw_call.layer_mask)) {
  406. // continue; //not masked
  407. //}
  408. uint light_index = 32 * i + bit;
  409. vec3 light_pos = spot_lights.data[light_index].position;
  410. vec3 light_rel_vec = spot_lights.data[light_index].position - view_pos;
  411. float d = length(light_rel_vec);
  412. float shadow_attenuation = 1.0;
  413. if (spot_lights.data[light_index].volumetric_fog_energy > 0.001 && d * spot_lights.data[light_index].inv_radius < 1.0) {
  414. float attenuation = get_omni_attenuation(d, spot_lights.data[light_index].inv_radius, spot_lights.data[light_index].attenuation);
  415. vec3 spot_dir = spot_lights.data[light_index].direction;
  416. float cone_angle = spot_lights.data[light_index].cone_angle;
  417. float scos = max(dot(-normalize(light_rel_vec), spot_dir), cone_angle);
  418. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cone_angle));
  419. attenuation *= 1.0 - pow(spot_rim, spot_lights.data[light_index].cone_attenuation);
  420. vec3 light = spot_lights.data[light_index].color;
  421. if (spot_lights.data[light_index].shadow_opacity > 0.001) {
  422. //has shadow
  423. vec4 uv_rect = spot_lights.data[light_index].atlas_rect;
  424. vec4 v = vec4(view_pos, 1.0);
  425. vec4 splane = (spot_lights.data[light_index].shadow_matrix * v);
  426. splane.z -= spot_lights.data[light_index].shadow_bias / (d * spot_lights.data[light_index].inv_radius);
  427. splane /= splane.w;
  428. vec3 pos = vec3(splane.xy * spot_lights.data[light_index].atlas_rect.zw + spot_lights.data[light_index].atlas_rect.xy, splane.z);
  429. float depth = texture(sampler2D(shadow_atlas, linear_sampler), pos.xy).r;
  430. shadow_attenuation = mix(1.0 - spot_lights.data[light_index].shadow_opacity, 1.0, exp(min(0.0, (pos.z - depth)) / spot_lights.data[light_index].inv_radius * INV_FOG_FADE));
  431. }
  432. total_light += light * attenuation * shadow_attenuation * henyey_greenstein(dot(normalize(light_rel_vec), normalize(view_pos)), params.phase_g) * spot_lights.data[light_index].volumetric_fog_energy;
  433. }
  434. }
  435. }
  436. }
  437. vec3 world_pos = mat3(params.cam_rotation) * view_pos;
  438. for (uint i = 0; i < params.max_voxel_gi_instances; i++) {
  439. vec3 position = (voxel_gi_instances.data[i].xform * vec4(world_pos, 1.0)).xyz;
  440. //this causes corrupted pixels, i have no idea why..
  441. if (all(bvec2(all(greaterThanEqual(position, vec3(0.0))), all(lessThan(position, voxel_gi_instances.data[i].bounds))))) {
  442. position /= voxel_gi_instances.data[i].bounds;
  443. vec4 light = vec4(0.0);
  444. for (uint j = 0; j < voxel_gi_instances.data[i].mipmaps; j++) {
  445. vec4 slight = textureLod(sampler3D(voxel_gi_textures[i], linear_sampler_with_mipmaps), position, float(j));
  446. float a = (1.0 - light.a);
  447. light += a * slight;
  448. }
  449. light.rgb *= voxel_gi_instances.data[i].dynamic_range * params.gi_inject * voxel_gi_instances.data[i].exposure_normalization;
  450. total_light += light.rgb;
  451. }
  452. }
  453. //sdfgi
  454. #ifdef ENABLE_SDFGI
  455. {
  456. float blend = -1.0;
  457. vec3 ambient_total = vec3(0.0);
  458. for (uint i = 0; i < sdfgi.max_cascades; i++) {
  459. vec3 cascade_pos = (world_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
  460. if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
  461. continue; //skip cascade
  462. }
  463. vec3 base_pos = floor(cascade_pos);
  464. ivec3 probe_base_pos = ivec3(base_pos);
  465. vec4 ambient_accum = vec4(0.0);
  466. ivec3 tex_pos = ivec3(probe_base_pos.xy, int(i));
  467. tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
  468. for (uint j = 0; j < 8; j++) {
  469. ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
  470. ivec3 probe_posi = probe_base_pos;
  471. probe_posi += offset;
  472. // Compute weight
  473. vec3 probe_pos = vec3(probe_posi);
  474. vec3 probe_to_pos = cascade_pos - probe_pos;
  475. vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
  476. float weight = trilinear.x * trilinear.y * trilinear.z;
  477. // Compute lightprobe occlusion
  478. if (sdfgi.use_occlusion) {
  479. ivec3 occ_indexv = abs((sdfgi.cascades[i].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
  480. vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
  481. vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
  482. occ_pos.z += float(i);
  483. if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
  484. occ_pos.x += 1.0;
  485. }
  486. occ_pos *= sdfgi.occlusion_renormalize;
  487. float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask);
  488. weight *= max(occlusion, 0.01);
  489. }
  490. // Compute ambient texture position
  491. ivec3 uvw = tex_pos;
  492. uvw.xy += offset.xy;
  493. uvw.x += offset.z * sdfgi.probe_axis_size;
  494. vec3 ambient = texelFetch(sampler2DArray(sdfgi_ambient_texture, linear_sampler), uvw, 0).rgb;
  495. ambient_accum.rgb += ambient * weight * sdfgi.cascades[i].exposure_normalization;
  496. ambient_accum.a += weight;
  497. }
  498. if (ambient_accum.a > 0) {
  499. ambient_accum.rgb /= ambient_accum.a;
  500. }
  501. ambient_total = ambient_accum.rgb;
  502. break;
  503. }
  504. total_light += ambient_total * params.gi_inject;
  505. }
  506. #endif
  507. }
  508. vec4 final_density = vec4(total_light * scattering + emission, total_density);
  509. final_density = mix(final_density, reprojected_density, reproject_amount);
  510. imageStore(density_map, pos, final_density);
  511. #ifdef NO_IMAGE_ATOMICS
  512. density_only_map[lpos] = 0;
  513. light_only_map[lpos] = 0;
  514. emissive_only_map[lpos] = 0;
  515. #else
  516. imageStore(density_only_map, pos, uvec4(0));
  517. imageStore(light_only_map, pos, uvec4(0));
  518. imageStore(emissive_only_map, pos, uvec4(0));
  519. #endif
  520. #endif
  521. #ifdef MODE_FOG
  522. ivec3 pos = ivec3(gl_GlobalInvocationID.xy, 0);
  523. if (any(greaterThanEqual(pos, params.fog_volume_size))) {
  524. return; //do not compute
  525. }
  526. vec4 fog_accum = vec4(0.0, 0.0, 0.0, 1.0);
  527. float prev_z = 0.0;
  528. for (int i = 0; i < params.fog_volume_size.z; i++) {
  529. //compute fog position
  530. ivec3 fog_pos = pos + ivec3(0, 0, i);
  531. //get fog value
  532. vec4 fog = imageLoad(density_map, fog_pos);
  533. //get depth at cell pos
  534. float z = get_depth_at_pos(fog_cell_size.z, i);
  535. //get distance from previous pos
  536. float d = abs(prev_z - z);
  537. //compute transmittance using beer's law
  538. float transmittance = exp(-d * fog.a);
  539. fog_accum.rgb += ((fog.rgb - fog.rgb * transmittance) / max(fog.a, 0.00001)) * fog_accum.a;
  540. fog_accum.a *= transmittance;
  541. prev_z = z;
  542. imageStore(fog_map, fog_pos, vec4(fog_accum.rgb, 1.0 - fog_accum.a));
  543. }
  544. #endif
  545. #ifdef MODE_FILTER
  546. ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
  547. const float gauss[7] = float[](0.071303, 0.131514, 0.189879, 0.214607, 0.189879, 0.131514, 0.071303);
  548. const ivec3 filter_dir[3] = ivec3[](ivec3(1, 0, 0), ivec3(0, 1, 0), ivec3(0, 0, 1));
  549. ivec3 offset = filter_dir[params.filter_axis];
  550. vec4 accum = vec4(0.0);
  551. for (int i = -3; i <= 3; i++) {
  552. accum += imageLoad(source_map, clamp(pos + offset * i, ivec3(0), params.fog_volume_size - ivec3(1))) * gauss[i + 3];
  553. }
  554. imageStore(dest_map, pos, accum);
  555. #endif
  556. #ifdef MODE_COPY
  557. ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
  558. if (any(greaterThanEqual(pos, params.fog_volume_size))) {
  559. return; //do not compute
  560. }
  561. imageStore(dest_map, pos, imageLoad(source_map, pos));
  562. #endif
  563. }