gi.glsl 24 KB

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