volumetric_fog_process.glsl 25 KB

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