gi.glsl 24 KB

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