gi.glsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. #define M_PI 3.141592
  6. #define SDFGI_MAX_CASCADES 8
  7. //set 0 for SDFGI and render buffers
  8. layout(set = 0, binding = 1) uniform texture3D sdf_cascades[SDFGI_MAX_CASCADES];
  9. layout(set = 0, binding = 2) uniform texture3D light_cascades[SDFGI_MAX_CASCADES];
  10. layout(set = 0, binding = 3) uniform texture3D aniso0_cascades[SDFGI_MAX_CASCADES];
  11. layout(set = 0, binding = 4) uniform texture3D aniso1_cascades[SDFGI_MAX_CASCADES];
  12. layout(set = 0, binding = 5) uniform texture3D occlusion_texture;
  13. layout(set = 0, binding = 6) uniform sampler linear_sampler;
  14. layout(set = 0, binding = 7) uniform sampler linear_sampler_with_mipmaps;
  15. struct ProbeCascadeData {
  16. vec3 position;
  17. float to_probe;
  18. ivec3 probe_world_offset;
  19. float to_cell; // 1/bounds * grid_size
  20. };
  21. layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image2D ambient_buffer;
  22. layout(rgba16f, set = 0, binding = 10) uniform restrict writeonly image2D reflection_buffer;
  23. layout(set = 0, binding = 11) uniform texture2DArray lightprobe_texture;
  24. layout(set = 0, binding = 12) uniform texture2D depth_buffer;
  25. layout(set = 0, binding = 13) uniform texture2D normal_roughness_buffer;
  26. layout(set = 0, binding = 14) uniform utexture2D voxel_gi_buffer;
  27. layout(set = 0, binding = 15, std140) uniform SDFGI {
  28. vec3 grid_size;
  29. uint max_cascades;
  30. bool use_occlusion;
  31. int probe_axis_size;
  32. float probe_to_uvw;
  33. float normal_bias;
  34. vec3 lightprobe_tex_pixel_size;
  35. float energy;
  36. vec3 lightprobe_uv_offset;
  37. float y_mult;
  38. vec3 occlusion_clamp;
  39. uint pad3;
  40. vec3 occlusion_renormalize;
  41. uint pad4;
  42. vec3 cascade_probe_size;
  43. uint pad5;
  44. ProbeCascadeData cascades[SDFGI_MAX_CASCADES];
  45. }
  46. sdfgi;
  47. #define MAX_VOXEL_GI_INSTANCES 8
  48. struct VoxelGIData {
  49. mat4 xform;
  50. vec3 bounds;
  51. float dynamic_range;
  52. float bias;
  53. float normal_bias;
  54. bool blend_ambient;
  55. uint texture_slot;
  56. uint pad0;
  57. uint pad1;
  58. uint pad2;
  59. uint mipmaps;
  60. };
  61. layout(set = 0, binding = 16, std140) uniform VoxelGIs {
  62. VoxelGIData data[MAX_VOXEL_GI_INSTANCES];
  63. }
  64. voxel_gi_instances;
  65. layout(set = 0, binding = 17) uniform texture3D voxel_gi_textures[MAX_VOXEL_GI_INSTANCES];
  66. layout(push_constant, binding = 0, std430) uniform Params {
  67. ivec2 screen_size;
  68. float z_near;
  69. float z_far;
  70. vec4 proj_info;
  71. uint max_voxel_gi_instances;
  72. bool high_quality_vct;
  73. bool orthogonal;
  74. uint pad;
  75. mat3x4 cam_rotation;
  76. }
  77. params;
  78. vec2 octahedron_wrap(vec2 v) {
  79. vec2 signVal;
  80. signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
  81. signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
  82. return (1.0 - abs(v.yx)) * signVal;
  83. }
  84. vec2 octahedron_encode(vec3 n) {
  85. // https://twitter.com/Stubbesaurus/status/937994790553227264
  86. n /= (abs(n.x) + abs(n.y) + abs(n.z));
  87. n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
  88. n.xy = n.xy * 0.5 + 0.5;
  89. return n.xy;
  90. }
  91. vec4 blend_color(vec4 src, vec4 dst) {
  92. vec4 res;
  93. float sa = 1.0 - src.a;
  94. res.a = dst.a * sa + src.a;
  95. if (res.a == 0.0) {
  96. res.rgb = vec3(0);
  97. } else {
  98. res.rgb = (dst.rgb * dst.a * sa + src.rgb * src.a) / res.a;
  99. }
  100. return res;
  101. }
  102. vec3 reconstruct_position(ivec2 screen_pos) {
  103. vec3 pos;
  104. pos.z = texelFetch(sampler2D(depth_buffer, linear_sampler), screen_pos, 0).r;
  105. pos.z = pos.z * 2.0 - 1.0;
  106. if (params.orthogonal) {
  107. pos.z = ((pos.z + (params.z_far + params.z_near) / (params.z_far - params.z_near)) * (params.z_far - params.z_near)) / 2.0;
  108. } else {
  109. pos.z = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near - pos.z * (params.z_far - params.z_near));
  110. }
  111. pos.z = -pos.z;
  112. pos.xy = vec2(screen_pos) * params.proj_info.xy + params.proj_info.zw;
  113. if (!params.orthogonal) {
  114. pos.xy *= pos.z;
  115. }
  116. return pos;
  117. }
  118. void sdfvoxel_gi_process(uint cascade, vec3 cascade_pos, vec3 cam_pos, vec3 cam_normal, vec3 cam_specular_normal, float roughness, out vec3 diffuse_light, out vec3 specular_light) {
  119. cascade_pos += cam_normal * sdfgi.normal_bias;
  120. vec3 base_pos = floor(cascade_pos);
  121. //cascade_pos += mix(vec3(0.0),vec3(0.01),lessThan(abs(cascade_pos-base_pos),vec3(0.01))) * cam_normal;
  122. ivec3 probe_base_pos = ivec3(base_pos);
  123. vec4 diffuse_accum = vec4(0.0);
  124. vec3 specular_accum;
  125. ivec3 tex_pos = ivec3(probe_base_pos.xy, int(cascade));
  126. tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
  127. tex_pos.xy = tex_pos.xy * (SDFGI_OCT_SIZE + 2) + ivec2(1);
  128. vec3 diffuse_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
  129. vec3 specular_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_specular_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
  130. specular_accum = vec3(0.0);
  131. vec4 light_accum = vec4(0.0);
  132. float weight_accum = 0.0;
  133. for (uint j = 0; j < 8; j++) {
  134. ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
  135. ivec3 probe_posi = probe_base_pos;
  136. probe_posi += offset;
  137. // Compute weight
  138. vec3 probe_pos = vec3(probe_posi);
  139. vec3 probe_to_pos = cascade_pos - probe_pos;
  140. vec3 probe_dir = normalize(-probe_to_pos);
  141. vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
  142. float weight = trilinear.x * trilinear.y * trilinear.z * max(0.005, dot(cam_normal, probe_dir));
  143. // Compute lightprobe occlusion
  144. if (sdfgi.use_occlusion) {
  145. ivec3 occ_indexv = abs((sdfgi.cascades[cascade].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
  146. vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
  147. vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
  148. occ_pos.z += float(cascade);
  149. if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
  150. occ_pos.x += 1.0;
  151. }
  152. occ_pos *= sdfgi.occlusion_renormalize;
  153. float occlusion = dot(textureLod(sampler3D(occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask);
  154. weight *= max(occlusion, 0.01);
  155. }
  156. // Compute lightprobe texture position
  157. vec3 diffuse;
  158. vec3 pos_uvw = diffuse_posf;
  159. pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
  160. pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
  161. diffuse = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw, 0.0).rgb;
  162. diffuse_accum += vec4(diffuse * weight, weight);
  163. {
  164. vec3 specular = vec3(0.0);
  165. vec3 pos_uvw = specular_posf;
  166. pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
  167. pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
  168. if (roughness < 0.99) {
  169. specular = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw + vec3(0, 0, float(sdfgi.max_cascades)), 0.0).rgb;
  170. }
  171. if (roughness > 0.2) {
  172. specular = mix(specular, textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw, 0.0).rgb, (roughness - 0.2) * 1.25);
  173. }
  174. specular_accum += specular * weight;
  175. }
  176. }
  177. if (diffuse_accum.a > 0.0) {
  178. diffuse_accum.rgb /= diffuse_accum.a;
  179. }
  180. diffuse_light = diffuse_accum.rgb;
  181. if (diffuse_accum.a > 0.0) {
  182. specular_accum /= diffuse_accum.a;
  183. }
  184. specular_light = specular_accum;
  185. }
  186. void sdfgi_process(vec3 vertex, vec3 normal, vec3 reflection, float roughness, out vec4 ambient_light, out vec4 reflection_light) {
  187. //make vertex orientation the world one, but still align to camera
  188. vertex.y *= sdfgi.y_mult;
  189. normal.y *= sdfgi.y_mult;
  190. reflection.y *= sdfgi.y_mult;
  191. //renormalize
  192. normal = normalize(normal);
  193. reflection = normalize(reflection);
  194. vec3 cam_pos = vertex;
  195. vec3 cam_normal = normal;
  196. vec4 light_accum = vec4(0.0);
  197. float weight_accum = 0.0;
  198. vec4 light_blend_accum = vec4(0.0);
  199. float weight_blend_accum = 0.0;
  200. float blend = -1.0;
  201. // helper constants, compute once
  202. uint cascade = 0xFFFFFFFF;
  203. vec3 cascade_pos;
  204. vec3 cascade_normal;
  205. for (uint i = 0; i < sdfgi.max_cascades; i++) {
  206. cascade_pos = (cam_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
  207. if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
  208. continue; //skip cascade
  209. }
  210. cascade = i;
  211. break;
  212. }
  213. if (cascade < SDFGI_MAX_CASCADES) {
  214. ambient_light = vec4(0, 0, 0, 1);
  215. reflection_light = vec4(0, 0, 0, 1);
  216. float blend;
  217. vec3 diffuse, specular;
  218. sdfvoxel_gi_process(cascade, cascade_pos, cam_pos, cam_normal, reflection, roughness, diffuse, specular);
  219. {
  220. //process blend
  221. float blend_from = (float(sdfgi.probe_axis_size - 1) / 2.0) - 2.5;
  222. float blend_to = blend_from + 2.0;
  223. vec3 inner_pos = cam_pos * sdfgi.cascades[cascade].to_probe;
  224. float len = length(inner_pos);
  225. inner_pos = abs(normalize(inner_pos));
  226. len *= max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  227. if (len >= blend_from) {
  228. blend = smoothstep(blend_from, blend_to, len);
  229. } else {
  230. blend = 0.0;
  231. }
  232. }
  233. if (blend > 0.0) {
  234. //blend
  235. if (cascade == sdfgi.max_cascades - 1) {
  236. ambient_light.a = 1.0 - blend;
  237. reflection_light.a = 1.0 - blend;
  238. } else {
  239. vec3 diffuse2, specular2;
  240. cascade_pos = (cam_pos - sdfgi.cascades[cascade + 1].position) * sdfgi.cascades[cascade + 1].to_probe;
  241. sdfvoxel_gi_process(cascade + 1, cascade_pos, cam_pos, cam_normal, reflection, roughness, diffuse2, specular2);
  242. diffuse = mix(diffuse, diffuse2, blend);
  243. specular = mix(specular, specular2, blend);
  244. }
  245. }
  246. ambient_light.rgb = diffuse;
  247. if (roughness < 0.2) {
  248. vec3 pos_to_uvw = 1.0 / sdfgi.grid_size;
  249. vec4 light_accum = vec4(0.0);
  250. float blend_size = (sdfgi.grid_size.x / float(sdfgi.probe_axis_size - 1)) * 0.5;
  251. float radius_sizes[SDFGI_MAX_CASCADES];
  252. cascade = 0xFFFF;
  253. float base_distance = length(cam_pos);
  254. for (uint i = 0; i < sdfgi.max_cascades; i++) {
  255. radius_sizes[i] = (1.0 / sdfgi.cascades[i].to_cell) * (sdfgi.grid_size.x * 0.5 - blend_size);
  256. if (cascade == 0xFFFF && base_distance < radius_sizes[i]) {
  257. cascade = i;
  258. }
  259. }
  260. cascade = min(cascade, sdfgi.max_cascades - 1);
  261. float max_distance = radius_sizes[sdfgi.max_cascades - 1];
  262. vec3 ray_pos = cam_pos;
  263. vec3 ray_dir = reflection;
  264. {
  265. float prev_radius = cascade > 0 ? radius_sizes[cascade - 1] : 0.0;
  266. float base_blend = (base_distance - prev_radius) / (radius_sizes[cascade] - prev_radius);
  267. float bias = (1.0 + base_blend) * 1.1;
  268. vec3 abs_ray_dir = abs(ray_dir);
  269. //ray_pos += ray_dir * (bias / sdfgi.cascades[cascade].to_cell); //bias to avoid self occlusion
  270. ray_pos += (ray_dir * 1.0 / max(abs_ray_dir.x, max(abs_ray_dir.y, abs_ray_dir.z)) + cam_normal * 1.4) * bias / sdfgi.cascades[cascade].to_cell;
  271. }
  272. float softness = 0.2 + min(1.0, roughness * 5.0) * 4.0; //approximation to roughness so it does not seem like a hard fade
  273. uint i = 0;
  274. bool found = false;
  275. while (true) {
  276. if (length(ray_pos) >= max_distance || light_accum.a > 0.99) {
  277. break;
  278. }
  279. if (!found && i >= cascade && length(ray_pos) < radius_sizes[i]) {
  280. uint next_i = min(i + 1, sdfgi.max_cascades - 1);
  281. cascade = max(i, cascade); //never go down
  282. vec3 pos = ray_pos - sdfgi.cascades[i].position;
  283. pos *= sdfgi.cascades[i].to_cell * pos_to_uvw;
  284. float fdistance = textureLod(sampler3D(sdf_cascades[i], linear_sampler), pos, 0.0).r * 255.0 - 1.1;
  285. vec4 hit_light = vec4(0.0);
  286. if (fdistance < softness) {
  287. hit_light.rgb = textureLod(sampler3D(light_cascades[i], linear_sampler), pos, 0.0).rgb;
  288. hit_light.rgb *= 0.5; //approximation given value read is actually meant for anisotropy
  289. hit_light.a = clamp(1.0 - (fdistance / softness), 0.0, 1.0);
  290. hit_light.rgb *= hit_light.a;
  291. }
  292. fdistance /= sdfgi.cascades[i].to_cell;
  293. if (i < (sdfgi.max_cascades - 1)) {
  294. pos = ray_pos - sdfgi.cascades[next_i].position;
  295. pos *= sdfgi.cascades[next_i].to_cell * pos_to_uvw;
  296. float fdistance2 = textureLod(sampler3D(sdf_cascades[next_i], linear_sampler), pos, 0.0).r * 255.0 - 1.1;
  297. vec4 hit_light2 = vec4(0.0);
  298. if (fdistance2 < softness) {
  299. hit_light2.rgb = textureLod(sampler3D(light_cascades[next_i], linear_sampler), pos, 0.0).rgb;
  300. hit_light2.rgb *= 0.5; //approximation given value read is actually meant for anisotropy
  301. hit_light2.a = clamp(1.0 - (fdistance2 / softness), 0.0, 1.0);
  302. hit_light2.rgb *= hit_light2.a;
  303. }
  304. float prev_radius = i == 0 ? 0.0 : radius_sizes[max(0, i - 1)];
  305. float blend = clamp((length(ray_pos) - prev_radius) / (radius_sizes[i] - prev_radius), 0.0, 1.0);
  306. fdistance2 /= sdfgi.cascades[next_i].to_cell;
  307. hit_light = mix(hit_light, hit_light2, blend);
  308. fdistance = mix(fdistance, fdistance2, blend);
  309. }
  310. light_accum += hit_light;
  311. ray_pos += ray_dir * fdistance;
  312. found = true;
  313. }
  314. i++;
  315. if (i == sdfgi.max_cascades) {
  316. i = 0;
  317. found = false;
  318. }
  319. }
  320. vec3 light = light_accum.rgb / max(light_accum.a, 0.00001);
  321. float alpha = min(1.0, light_accum.a);
  322. float b = min(1.0, roughness * 5.0);
  323. float sa = 1.0 - b;
  324. reflection_light.a = alpha * sa + b;
  325. if (reflection_light.a == 0) {
  326. specular = vec3(0.0);
  327. } else {
  328. specular = (light * alpha * sa + specular * b) / reflection_light.a;
  329. }
  330. }
  331. reflection_light.rgb = specular;
  332. ambient_light.rgb *= sdfgi.energy;
  333. reflection_light.rgb *= sdfgi.energy;
  334. } else {
  335. ambient_light = vec4(0);
  336. reflection_light = vec4(0);
  337. }
  338. }
  339. //standard voxel cone trace
  340. vec4 voxel_cone_trace(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
  341. float dist = p_bias;
  342. vec4 color = vec4(0.0);
  343. while (dist < max_distance && color.a < 0.95) {
  344. float diameter = max(1.0, 2.0 * tan_half_angle * dist);
  345. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  346. float half_diameter = diameter * 0.5;
  347. //check if outside, then break
  348. if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + half_diameter * cell_size)))) {
  349. break;
  350. }
  351. vec4 scolor = textureLod(sampler3D(probe, linear_sampler_with_mipmaps), uvw_pos, log2(diameter));
  352. float a = (1.0 - color.a);
  353. color += a * scolor;
  354. dist += half_diameter;
  355. }
  356. return color;
  357. }
  358. vec4 voxel_cone_trace_45_degrees(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float max_distance, float p_bias) {
  359. float dist = p_bias;
  360. vec4 color = vec4(0.0);
  361. float radius = max(0.5, dist);
  362. float lod_level = log2(radius * 2.0);
  363. while (dist < max_distance && color.a < 0.95) {
  364. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  365. //check if outside, then break
  366. if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + radius * cell_size)))) {
  367. break;
  368. }
  369. vec4 scolor = textureLod(sampler3D(probe, linear_sampler_with_mipmaps), uvw_pos, lod_level);
  370. lod_level += 1.0;
  371. float a = (1.0 - color.a);
  372. scolor *= a;
  373. color += scolor;
  374. dist += radius;
  375. radius = max(0.5, dist);
  376. }
  377. return color;
  378. }
  379. void voxel_gi_compute(uint index, vec3 position, vec3 normal, vec3 ref_vec, mat3 normal_xform, float roughness, inout vec4 out_spec, inout vec4 out_diff, inout float out_blend) {
  380. position = (voxel_gi_instances.data[index].xform * vec4(position, 1.0)).xyz;
  381. ref_vec = normalize((voxel_gi_instances.data[index].xform * vec4(ref_vec, 0.0)).xyz);
  382. normal = normalize((voxel_gi_instances.data[index].xform * vec4(normal, 0.0)).xyz);
  383. position += normal * voxel_gi_instances.data[index].normal_bias;
  384. //this causes corrupted pixels, i have no idea why..
  385. if (any(bvec2(any(lessThan(position, vec3(0.0))), any(greaterThan(position, voxel_gi_instances.data[index].bounds))))) {
  386. return;
  387. }
  388. mat3 dir_xform = mat3(voxel_gi_instances.data[index].xform) * normal_xform;
  389. vec3 blendv = abs(position / voxel_gi_instances.data[index].bounds * 2.0 - 1.0);
  390. float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0);
  391. //float blend=1.0;
  392. float max_distance = length(voxel_gi_instances.data[index].bounds);
  393. vec3 cell_size = 1.0 / voxel_gi_instances.data[index].bounds;
  394. //irradiance
  395. vec4 light = vec4(0.0);
  396. if (params.high_quality_vct) {
  397. const uint cone_dir_count = 6;
  398. vec3 cone_dirs[cone_dir_count] = vec3[](
  399. vec3(0.0, 0.0, 1.0),
  400. vec3(0.866025, 0.0, 0.5),
  401. vec3(0.267617, 0.823639, 0.5),
  402. vec3(-0.700629, 0.509037, 0.5),
  403. vec3(-0.700629, -0.509037, 0.5),
  404. vec3(0.267617, -0.823639, 0.5));
  405. float cone_weights[cone_dir_count] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15);
  406. float cone_angle_tan = 0.577;
  407. for (uint i = 0; i < cone_dir_count; i++) {
  408. vec3 dir = normalize(dir_xform * cone_dirs[i]);
  409. light += cone_weights[i] * voxel_cone_trace(voxel_gi_textures[index], cell_size, position, dir, cone_angle_tan, max_distance, voxel_gi_instances.data[index].bias);
  410. }
  411. } else {
  412. const uint cone_dir_count = 4;
  413. vec3 cone_dirs[cone_dir_count] = vec3[](
  414. vec3(0.707107, 0.0, 0.707107),
  415. vec3(0.0, 0.707107, 0.707107),
  416. vec3(-0.707107, 0.0, 0.707107),
  417. vec3(0.0, -0.707107, 0.707107));
  418. float cone_weights[cone_dir_count] = float[](0.25, 0.25, 0.25, 0.25);
  419. for (int i = 0; i < cone_dir_count; i++) {
  420. vec3 dir = normalize(dir_xform * cone_dirs[i]);
  421. light += cone_weights[i] * voxel_cone_trace_45_degrees(voxel_gi_textures[index], cell_size, position, dir, max_distance, voxel_gi_instances.data[index].bias);
  422. }
  423. }
  424. light.rgb *= voxel_gi_instances.data[index].dynamic_range;
  425. if (!voxel_gi_instances.data[index].blend_ambient) {
  426. light.a = 1.0;
  427. }
  428. out_diff += light * blend;
  429. //radiance
  430. vec4 irr_light = voxel_cone_trace(voxel_gi_textures[index], cell_size, position, ref_vec, tan(roughness * 0.5 * M_PI * 0.99), max_distance, voxel_gi_instances.data[index].bias);
  431. irr_light.rgb *= voxel_gi_instances.data[index].dynamic_range;
  432. if (!voxel_gi_instances.data[index].blend_ambient) {
  433. irr_light.a = 1.0;
  434. }
  435. out_spec += irr_light * blend;
  436. out_blend += blend;
  437. }
  438. vec4 fetch_normal_and_roughness(ivec2 pos) {
  439. vec4 normal_roughness = texelFetch(sampler2D(normal_roughness_buffer, linear_sampler), pos, 0);
  440. normal_roughness.xyz = normalize(normal_roughness.xyz * 2.0 - 1.0);
  441. return normal_roughness;
  442. }
  443. void process_gi(ivec2 pos, vec3 vertex, inout vec4 ambient_light, inout vec4 reflection_light) {
  444. vec4 normal_roughness = fetch_normal_and_roughness(pos);
  445. vec3 normal = normal_roughness.xyz;
  446. if (normal.length() > 0.5) {
  447. //valid normal, can do GI
  448. float roughness = normal_roughness.w;
  449. vertex = mat3(params.cam_rotation) * vertex;
  450. normal = normalize(mat3(params.cam_rotation) * normal);
  451. vec3 reflection = normalize(reflect(normalize(vertex), normal));
  452. #ifdef USE_SDFGI
  453. sdfgi_process(vertex, normal, reflection, roughness, ambient_light, reflection_light);
  454. #endif
  455. #ifdef USE_VOXEL_GI_INSTANCES
  456. {
  457. uvec2 voxel_gi_tex = texelFetch(usampler2D(voxel_gi_buffer, linear_sampler), pos, 0).rg;
  458. roughness *= roughness;
  459. //find arbitrary tangent and bitangent, then build a matrix
  460. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  461. vec3 tangent = normalize(cross(v0, normal));
  462. vec3 bitangent = normalize(cross(tangent, normal));
  463. mat3 normal_mat = mat3(tangent, bitangent, normal);
  464. vec4 amb_accum = vec4(0.0);
  465. vec4 spec_accum = vec4(0.0);
  466. float blend_accum = 0.0;
  467. for (uint i = 0; i < params.max_voxel_gi_instances; i++) {
  468. if (any(equal(uvec2(i), voxel_gi_tex))) {
  469. voxel_gi_compute(i, vertex, normal, reflection, normal_mat, roughness, spec_accum, amb_accum, blend_accum);
  470. }
  471. }
  472. if (blend_accum > 0.0) {
  473. amb_accum /= blend_accum;
  474. spec_accum /= blend_accum;
  475. }
  476. #ifdef USE_SDFGI
  477. reflection_light = blend_color(spec_accum, reflection_light);
  478. ambient_light = blend_color(amb_accum, ambient_light);
  479. #else
  480. reflection_light = spec_accum;
  481. ambient_light = amb_accum;
  482. #endif
  483. }
  484. #endif
  485. }
  486. }
  487. void main() {
  488. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  489. #ifdef MODE_HALF_RES
  490. pos <<= 1;
  491. #endif
  492. if (any(greaterThanEqual(pos, params.screen_size))) { //too large, do nothing
  493. return;
  494. }
  495. vec4 ambient_light = vec4(0.0);
  496. vec4 reflection_light = vec4(0.0);
  497. vec3 vertex = reconstruct_position(pos);
  498. vertex.y = -vertex.y;
  499. process_gi(pos, vertex, ambient_light, reflection_light);
  500. #ifdef MODE_HALF_RES
  501. pos >>= 1;
  502. #endif
  503. imageStore(ambient_buffer, pos, ambient_light);
  504. imageStore(reflection_buffer, pos, reflection_light);
  505. }