volumetric_fog.glsl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. #[compute]
  2. #version 450
  3. VERSION_DEFINES
  4. /* Do not use subgroups here, seems there is not much advantage and causes glitches
  5. #extension GL_KHR_shader_subgroup_ballot: enable
  6. #extension GL_KHR_shader_subgroup_arithmetic: enable
  7. #if defined(GL_KHR_shader_subgroup_ballot) && defined(GL_KHR_shader_subgroup_arithmetic)
  8. #define USE_SUBGROUPS
  9. #endif
  10. */
  11. #if defined(MODE_FOG) || defined(MODE_FILTER)
  12. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  13. #endif
  14. #if defined(MODE_DENSITY)
  15. layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
  16. #endif
  17. #include "cluster_data_inc.glsl"
  18. #define M_PI 3.14159265359
  19. layout(set = 0, binding = 1) uniform texture2D shadow_atlas;
  20. layout(set = 0, binding = 2) uniform texture2D directional_shadow_atlas;
  21. layout(set = 0, binding = 3, std430) restrict readonly buffer OmniLights {
  22. LightData data[];
  23. }
  24. omni_lights;
  25. layout(set = 0, binding = 4, std430) restrict readonly buffer SpotLights {
  26. LightData data[];
  27. }
  28. spot_lights;
  29. layout(set = 0, binding = 5, std140) uniform DirectionalLights {
  30. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  31. }
  32. directional_lights;
  33. layout(set = 0, binding = 6, std430) buffer restrict readonly ClusterBuffer {
  34. uint data[];
  35. }
  36. cluster_buffer;
  37. layout(set = 0, binding = 7) uniform sampler linear_sampler;
  38. #ifdef MODE_DENSITY
  39. layout(rgba16f, set = 0, binding = 8) uniform restrict writeonly image3D density_map;
  40. layout(rgba16f, set = 0, binding = 9) uniform restrict readonly image3D fog_map; //unused
  41. #endif
  42. #ifdef MODE_FOG
  43. layout(rgba16f, set = 0, binding = 8) uniform restrict readonly image3D density_map;
  44. layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image3D fog_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_GI_PROBES 8
  52. struct GIProbeData {
  53. mat4 xform;
  54. vec3 bounds;
  55. float dynamic_range;
  56. float bias;
  57. float normal_bias;
  58. bool blend_ambient;
  59. uint texture_slot;
  60. float anisotropy_strength;
  61. float ambient_occlusion;
  62. float ambient_occlusion_size;
  63. uint mipmaps;
  64. };
  65. layout(set = 0, binding = 11, std140) uniform GIProbes {
  66. GIProbeData data[MAX_GI_PROBES];
  67. }
  68. gi_probes;
  69. layout(set = 0, binding = 12) uniform texture3D gi_probe_textures[MAX_GI_PROBES];
  70. layout(set = 0, binding = 13) uniform sampler linear_sampler_with_mipmaps;
  71. #ifdef ENABLE_SDFGI
  72. // SDFGI Integration on set 1
  73. #define SDFGI_MAX_CASCADES 8
  74. struct SDFGIProbeCascadeData {
  75. vec3 position;
  76. float to_probe;
  77. ivec3 probe_world_offset;
  78. float to_cell; // 1/bounds * grid_size
  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. SDFGIProbeCascadeData 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 z_near;
  108. float z_far;
  109. int filter_axis;
  110. ivec3 fog_volume_size;
  111. uint directional_light_count;
  112. vec3 light_color;
  113. float base_density;
  114. float detail_spread;
  115. float gi_inject;
  116. uint max_gi_probes;
  117. uint cluster_type_size;
  118. vec2 screen_size;
  119. uint cluster_shift;
  120. uint cluster_width;
  121. uint max_cluster_element_count_div_32;
  122. bool use_temporal_reprojection;
  123. uint temporal_frame;
  124. float temporal_blend;
  125. mat3x4 cam_rotation;
  126. mat4 to_prev_view;
  127. }
  128. params;
  129. layout(set = 0, binding = 15) uniform texture3D prev_density_texture;
  130. float get_depth_at_pos(float cell_depth_size, int z) {
  131. float d = float(z) * cell_depth_size + cell_depth_size * 0.5; //center of voxels
  132. d = pow(d, params.detail_spread);
  133. return params.fog_frustum_end * d;
  134. }
  135. vec3 hash3f(uvec3 x) {
  136. x = ((x >> 16) ^ x) * 0x45d9f3b;
  137. x = ((x >> 16) ^ x) * 0x45d9f3b;
  138. x = (x >> 16) ^ x;
  139. return vec3(x & 0xFFFFF) / vec3(float(0xFFFFF));
  140. }
  141. float get_omni_attenuation(float distance, float inv_range, float decay) {
  142. float nd = distance * inv_range;
  143. nd *= nd;
  144. nd *= nd; // nd^4
  145. nd = max(1.0 - nd, 0.0);
  146. nd *= nd; // nd^2
  147. return nd * pow(max(distance, 0.0001), -decay);
  148. }
  149. void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
  150. uint item_min_max = cluster_buffer.data[p_offset];
  151. item_min = item_min_max & 0xFFFF;
  152. item_max = item_min_max >> 16;
  153. ;
  154. item_from = item_min >> 5;
  155. 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
  156. }
  157. uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
  158. int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
  159. int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
  160. return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
  161. }
  162. #define TEMPORAL_FRAMES 16
  163. const vec3 halton_map[TEMPORAL_FRAMES] = vec3[](
  164. vec3(0.5, 0.33333333, 0.2),
  165. vec3(0.25, 0.66666667, 0.4),
  166. vec3(0.75, 0.11111111, 0.6),
  167. vec3(0.125, 0.44444444, 0.8),
  168. vec3(0.625, 0.77777778, 0.04),
  169. vec3(0.375, 0.22222222, 0.24),
  170. vec3(0.875, 0.55555556, 0.44),
  171. vec3(0.0625, 0.88888889, 0.64),
  172. vec3(0.5625, 0.03703704, 0.84),
  173. vec3(0.3125, 0.37037037, 0.08),
  174. vec3(0.8125, 0.7037037, 0.28),
  175. vec3(0.1875, 0.14814815, 0.48),
  176. vec3(0.6875, 0.48148148, 0.68),
  177. vec3(0.4375, 0.81481481, 0.88),
  178. vec3(0.9375, 0.25925926, 0.12),
  179. vec3(0.03125, 0.59259259, 0.32));
  180. void main() {
  181. vec3 fog_cell_size = 1.0 / vec3(params.fog_volume_size);
  182. #ifdef MODE_DENSITY
  183. ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
  184. if (any(greaterThanEqual(pos, params.fog_volume_size))) {
  185. return; //do not compute
  186. }
  187. vec3 posf = vec3(pos);
  188. //posf += mix(vec3(0.0),vec3(1.0),0.3) * hash3f(uvec3(pos)) * 2.0 - 1.0;
  189. vec3 fog_unit_pos = posf * fog_cell_size + fog_cell_size * 0.5; //center of voxels
  190. uvec2 screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
  191. uvec2 cluster_pos = screen_pos >> params.cluster_shift;
  192. uint cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
  193. //positions in screen are too spread apart, no hopes for optimizing with subgroups
  194. fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
  195. vec3 view_pos;
  196. 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));
  197. view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
  198. view_pos.y = -view_pos.y;
  199. vec4 reprojected_density = vec4(0.0);
  200. float reproject_amount = 0.0;
  201. if (params.use_temporal_reprojection) {
  202. vec3 prev_view = (params.to_prev_view * vec4(view_pos, 1.0)).xyz;
  203. //undo transform into prev view
  204. prev_view.y = -prev_view.y;
  205. //z back to unit size
  206. prev_view.z /= -params.fog_frustum_end;
  207. //xy back to unit size
  208. prev_view.xy /= mix(params.fog_frustum_size_begin, params.fog_frustum_size_end, vec2(prev_view.z));
  209. prev_view.xy = prev_view.xy * 0.5 + 0.5;
  210. //z back to unspread value
  211. prev_view.z = pow(prev_view.z, 1.0 / params.detail_spread);
  212. if (all(greaterThan(prev_view, vec3(0.0))) && all(lessThan(prev_view, vec3(1.0)))) {
  213. //reprojectinon fits
  214. reprojected_density = textureLod(sampler3D(prev_density_texture, linear_sampler), prev_view, 0.0);
  215. reproject_amount = params.temporal_blend;
  216. // Since we can reproject, now we must jitter the current view pos.
  217. // This is done here because cells that can't reproject should not jitter.
  218. fog_unit_pos = posf * fog_cell_size + fog_cell_size * halton_map[params.temporal_frame]; //center of voxels, offset by halton table
  219. screen_pos = uvec2(fog_unit_pos.xy * params.screen_size);
  220. cluster_pos = screen_pos >> params.cluster_shift;
  221. cluster_offset = (params.cluster_width * cluster_pos.y + cluster_pos.x) * (params.max_cluster_element_count_div_32 + 32);
  222. //positions in screen are too spread apart, no hopes for optimizing with subgroups
  223. fog_unit_pos.z = pow(fog_unit_pos.z, params.detail_spread);
  224. 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));
  225. view_pos.z = -params.fog_frustum_end * fog_unit_pos.z;
  226. view_pos.y = -view_pos.y;
  227. }
  228. }
  229. uint cluster_z = uint(clamp((abs(view_pos.z) / params.z_far) * 32.0, 0.0, 31.0));
  230. vec3 total_light = params.light_color;
  231. float total_density = params.base_density;
  232. float cell_depth_size = abs(view_pos.z - get_depth_at_pos(fog_cell_size.z, pos.z + 1));
  233. //compute directional lights
  234. for (uint i = 0; i < params.directional_light_count; i++) {
  235. vec3 shadow_attenuation = vec3(1.0);
  236. if (directional_lights.data[i].shadow_enabled) {
  237. float depth_z = -view_pos.z;
  238. vec4 pssm_coord;
  239. vec3 shadow_color = directional_lights.data[i].shadow_color1.rgb;
  240. vec3 light_dir = directional_lights.data[i].direction;
  241. vec4 v = vec4(view_pos, 1.0);
  242. float z_range;
  243. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  244. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  245. pssm_coord /= pssm_coord.w;
  246. z_range = directional_lights.data[i].shadow_z_range.x;
  247. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  248. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  249. pssm_coord /= pssm_coord.w;
  250. z_range = directional_lights.data[i].shadow_z_range.y;
  251. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  252. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  253. pssm_coord /= pssm_coord.w;
  254. z_range = directional_lights.data[i].shadow_z_range.z;
  255. } else {
  256. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  257. pssm_coord /= pssm_coord.w;
  258. z_range = directional_lights.data[i].shadow_z_range.w;
  259. }
  260. float depth = texture(sampler2D(directional_shadow_atlas, linear_sampler), pssm_coord.xy).r;
  261. float shadow = exp(min(0.0, (depth - pssm_coord.z)) * z_range * directional_lights.data[i].shadow_volumetric_fog_fade);
  262. /*
  263. //float shadow = textureProj(sampler2DShadow(directional_shadow_atlas,shadow_sampler),pssm_coord);
  264. float shadow = 0.0;
  265. for(float xi=-1;xi<=1;xi++) {
  266. for(float yi=-1;yi<=1;yi++) {
  267. vec2 ofs = vec2(xi,yi) * 1.5 * params.directional_shadow_pixel_size;
  268. shadow += textureProj(sampler2DShadow(directional_shadow_atlas,shadow_sampler),pssm_coord + vec4(ofs,0.0,0.0));
  269. }
  270. }
  271. shadow /= 3.0 * 3.0;
  272. */
  273. 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
  274. shadow_attenuation = mix(shadow_color, vec3(1.0), shadow);
  275. }
  276. total_light += shadow_attenuation * directional_lights.data[i].color * directional_lights.data[i].energy / M_PI;
  277. }
  278. //compute lights from cluster
  279. { //omni lights
  280. uint cluster_omni_offset = cluster_offset;
  281. uint item_min;
  282. uint item_max;
  283. uint item_from;
  284. uint item_to;
  285. cluster_get_item_range(cluster_omni_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  286. #ifdef USE_SUBGROUPS
  287. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  288. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  289. #endif
  290. for (uint i = item_from; i < item_to; i++) {
  291. uint mask = cluster_buffer.data[cluster_omni_offset + i];
  292. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  293. #ifdef USE_SUBGROUPS
  294. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  295. #else
  296. uint merged_mask = mask;
  297. #endif
  298. while (merged_mask != 0) {
  299. uint bit = findMSB(merged_mask);
  300. merged_mask &= ~(1 << bit);
  301. #ifdef USE_SUBGROUPS
  302. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  303. continue;
  304. }
  305. #endif
  306. uint light_index = 32 * i + bit;
  307. //if (!bool(omni_omni_lights.data[light_index].mask & draw_call.layer_mask)) {
  308. // continue; //not masked
  309. //}
  310. vec3 light_pos = omni_lights.data[light_index].position;
  311. float d = distance(omni_lights.data[light_index].position, view_pos);
  312. float shadow_attenuation = 1.0;
  313. if (d * omni_lights.data[light_index].inv_radius < 1.0) {
  314. float attenuation = get_omni_attenuation(d, omni_lights.data[light_index].inv_radius, omni_lights.data[light_index].attenuation);
  315. vec3 light = omni_lights.data[light_index].color / M_PI;
  316. if (omni_lights.data[light_index].shadow_enabled) {
  317. //has shadow
  318. vec4 v = vec4(view_pos, 1.0);
  319. vec4 splane = (omni_lights.data[light_index].shadow_matrix * v);
  320. float shadow_len = length(splane.xyz); //need to remember shadow len from here
  321. splane.xyz = normalize(splane.xyz);
  322. vec4 clamp_rect = omni_lights.data[light_index].atlas_rect;
  323. if (splane.z >= 0.0) {
  324. splane.z += 1.0;
  325. clamp_rect.y += clamp_rect.w;
  326. } else {
  327. splane.z = 1.0 - splane.z;
  328. }
  329. splane.xy /= splane.z;
  330. splane.xy = splane.xy * 0.5 + 0.5;
  331. splane.z = shadow_len * omni_lights.data[light_index].inv_radius;
  332. splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
  333. splane.w = 1.0; //needed? i think it should be 1 already
  334. float depth = texture(sampler2D(shadow_atlas, linear_sampler), splane.xy).r;
  335. 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);
  336. }
  337. total_light += light * attenuation * shadow_attenuation;
  338. }
  339. }
  340. }
  341. }
  342. { //spot lights
  343. uint cluster_spot_offset = cluster_offset + params.cluster_type_size;
  344. uint item_min;
  345. uint item_max;
  346. uint item_from;
  347. uint item_to;
  348. cluster_get_item_range(cluster_spot_offset + params.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  349. #ifdef USE_SUBGROUPS
  350. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  351. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  352. #endif
  353. for (uint i = item_from; i < item_to; i++) {
  354. uint mask = cluster_buffer.data[cluster_spot_offset + i];
  355. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  356. #ifdef USE_SUBGROUPS
  357. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  358. #else
  359. uint merged_mask = mask;
  360. #endif
  361. while (merged_mask != 0) {
  362. uint bit = findMSB(merged_mask);
  363. merged_mask &= ~(1 << bit);
  364. #ifdef USE_SUBGROUPS
  365. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  366. continue;
  367. }
  368. #endif
  369. //if (!bool(omni_lights.data[light_index].mask & draw_call.layer_mask)) {
  370. // continue; //not masked
  371. //}
  372. uint light_index = 32 * i + bit;
  373. vec3 light_pos = spot_lights.data[light_index].position;
  374. vec3 light_rel_vec = spot_lights.data[light_index].position - view_pos;
  375. float d = length(light_rel_vec);
  376. float shadow_attenuation = 1.0;
  377. if (d * spot_lights.data[light_index].inv_radius < 1.0) {
  378. float attenuation = get_omni_attenuation(d, spot_lights.data[light_index].inv_radius, spot_lights.data[light_index].attenuation);
  379. vec3 spot_dir = spot_lights.data[light_index].direction;
  380. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights.data[light_index].cone_angle);
  381. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights.data[light_index].cone_angle));
  382. attenuation *= 1.0 - pow(spot_rim, spot_lights.data[light_index].cone_attenuation);
  383. vec3 light = spot_lights.data[light_index].color / M_PI;
  384. if (spot_lights.data[light_index].shadow_enabled) {
  385. //has shadow
  386. vec4 v = vec4(view_pos, 1.0);
  387. vec4 splane = (spot_lights.data[light_index].shadow_matrix * v);
  388. splane /= splane.w;
  389. float depth = texture(sampler2D(shadow_atlas, linear_sampler), splane.xy).r;
  390. 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);
  391. }
  392. total_light += light * attenuation * shadow_attenuation;
  393. }
  394. }
  395. }
  396. }
  397. vec3 world_pos = mat3(params.cam_rotation) * view_pos;
  398. for (uint i = 0; i < params.max_gi_probes; i++) {
  399. vec3 position = (gi_probes.data[i].xform * vec4(world_pos, 1.0)).xyz;
  400. //this causes corrupted pixels, i have no idea why..
  401. if (all(bvec2(all(greaterThanEqual(position, vec3(0.0))), all(lessThan(position, gi_probes.data[i].bounds))))) {
  402. position /= gi_probes.data[i].bounds;
  403. vec4 light = vec4(0.0);
  404. for (uint j = 0; j < gi_probes.data[i].mipmaps; j++) {
  405. vec4 slight = textureLod(sampler3D(gi_probe_textures[i], linear_sampler_with_mipmaps), position, float(j));
  406. float a = (1.0 - light.a);
  407. light += a * slight;
  408. }
  409. light.rgb *= gi_probes.data[i].dynamic_range * params.gi_inject;
  410. total_light += light.rgb;
  411. }
  412. }
  413. //sdfgi
  414. #ifdef ENABLE_SDFGI
  415. {
  416. float blend = -1.0;
  417. vec3 ambient_total = vec3(0.0);
  418. for (uint i = 0; i < sdfgi.max_cascades; i++) {
  419. vec3 cascade_pos = (world_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
  420. if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
  421. continue; //skip cascade
  422. }
  423. vec3 base_pos = floor(cascade_pos);
  424. ivec3 probe_base_pos = ivec3(base_pos);
  425. vec4 ambient_accum = vec4(0.0);
  426. ivec3 tex_pos = ivec3(probe_base_pos.xy, int(i));
  427. tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
  428. for (uint j = 0; j < 8; j++) {
  429. ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
  430. ivec3 probe_posi = probe_base_pos;
  431. probe_posi += offset;
  432. // Compute weight
  433. vec3 probe_pos = vec3(probe_posi);
  434. vec3 probe_to_pos = cascade_pos - probe_pos;
  435. vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
  436. float weight = trilinear.x * trilinear.y * trilinear.z;
  437. // Compute lightprobe occlusion
  438. if (sdfgi.use_occlusion) {
  439. ivec3 occ_indexv = abs((sdfgi.cascades[i].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
  440. vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
  441. vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
  442. occ_pos.z += float(i);
  443. if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
  444. occ_pos.x += 1.0;
  445. }
  446. occ_pos *= sdfgi.occlusion_renormalize;
  447. float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask);
  448. weight *= max(occlusion, 0.01);
  449. }
  450. // Compute ambient texture position
  451. ivec3 uvw = tex_pos;
  452. uvw.xy += offset.xy;
  453. uvw.x += offset.z * sdfgi.probe_axis_size;
  454. vec3 ambient = texelFetch(sampler2DArray(sdfgi_ambient_texture, linear_sampler), uvw, 0).rgb;
  455. ambient_accum.rgb += ambient * weight;
  456. ambient_accum.a += weight;
  457. }
  458. if (ambient_accum.a > 0) {
  459. ambient_accum.rgb /= ambient_accum.a;
  460. }
  461. ambient_total = ambient_accum.rgb;
  462. break;
  463. }
  464. total_light += ambient_total * params.gi_inject;
  465. }
  466. #endif
  467. vec4 final_density = vec4(total_light, total_density);
  468. final_density = mix(final_density, reprojected_density, reproject_amount);
  469. imageStore(density_map, pos, final_density);
  470. #endif
  471. #ifdef MODE_FOG
  472. ivec3 pos = ivec3(gl_GlobalInvocationID.xy, 0);
  473. if (any(greaterThanEqual(pos, params.fog_volume_size))) {
  474. return; //do not compute
  475. }
  476. vec4 fog_accum = vec4(0.0);
  477. float prev_z = 0.0;
  478. float t = 1.0;
  479. for (int i = 0; i < params.fog_volume_size.z; i++) {
  480. //compute fog position
  481. ivec3 fog_pos = pos + ivec3(0, 0, i);
  482. //get fog value
  483. vec4 fog = imageLoad(density_map, fog_pos);
  484. //get depth at cell pos
  485. float z = get_depth_at_pos(fog_cell_size.z, i);
  486. //get distance from previous pos
  487. float d = abs(prev_z - z);
  488. //compute exinction based on beer's
  489. float extinction = t * exp(-d * fog.a);
  490. //compute alpha based on different of extinctions
  491. float alpha = t - extinction;
  492. //update extinction
  493. t = extinction;
  494. fog_accum += vec4(fog.rgb * alpha, alpha);
  495. prev_z = z;
  496. vec4 fog_value;
  497. if (fog_accum.a > 0.0) {
  498. fog_value = vec4(fog_accum.rgb / fog_accum.a, 1.0 - t);
  499. } else {
  500. fog_value = vec4(0.0);
  501. }
  502. imageStore(fog_map, fog_pos, fog_value);
  503. }
  504. #endif
  505. #ifdef MODE_FILTER
  506. ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
  507. const float gauss[7] = float[](0.071303, 0.131514, 0.189879, 0.214607, 0.189879, 0.131514, 0.071303);
  508. const ivec3 filter_dir[3] = ivec3[](ivec3(1, 0, 0), ivec3(0, 1, 0), ivec3(0, 0, 1));
  509. ivec3 offset = filter_dir[params.filter_axis];
  510. vec4 accum = vec4(0.0);
  511. for (int i = -3; i <= 3; i++) {
  512. accum += imageLoad(source_map, clamp(pos + offset * i, ivec3(0), params.fog_volume_size - ivec3(1))) * gauss[i + 3];
  513. }
  514. imageStore(dest_map, pos, accum);
  515. #endif
  516. }