volumetric_fog_process.glsl 24 KB

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