scene_forward_gi_inc.glsl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Functions related to gi/sdfgi for our forward renderer
  2. //standard voxel cone trace
  3. vec4 voxel_cone_trace(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
  4. float dist = p_bias;
  5. vec4 color = vec4(0.0);
  6. while (dist < max_distance && color.a < 0.95) {
  7. float diameter = max(1.0, 2.0 * tan_half_angle * dist);
  8. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  9. float half_diameter = diameter * 0.5;
  10. //check if outside, then break
  11. if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + half_diameter * cell_size)))) {
  12. break;
  13. }
  14. vec4 scolor = textureLod(sampler3D(probe, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), uvw_pos, log2(diameter));
  15. float a = (1.0 - color.a);
  16. color += a * scolor;
  17. dist += half_diameter;
  18. }
  19. return color;
  20. }
  21. vec4 voxel_cone_trace_45_degrees(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
  22. float dist = p_bias;
  23. vec4 color = vec4(0.0);
  24. float radius = max(0.5, tan_half_angle * dist);
  25. float lod_level = log2(radius * 2.0);
  26. while (dist < max_distance && color.a < 0.95) {
  27. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  28. //check if outside, then break
  29. if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + radius * cell_size)))) {
  30. break;
  31. }
  32. vec4 scolor = textureLod(sampler3D(probe, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), uvw_pos, lod_level);
  33. lod_level += 1.0;
  34. float a = (1.0 - color.a);
  35. scolor *= a;
  36. color += scolor;
  37. dist += radius;
  38. radius = max(0.5, tan_half_angle * dist);
  39. }
  40. return color;
  41. }
  42. void voxel_gi_compute(uint index, vec3 position, vec3 normal, vec3 ref_vec, mat3 normal_xform, float roughness, vec3 ambient, vec3 environment, inout vec4 out_spec, inout vec4 out_diff) {
  43. position = (voxel_gi_instances.data[index].xform * vec4(position, 1.0)).xyz;
  44. ref_vec = normalize((voxel_gi_instances.data[index].xform * vec4(ref_vec, 0.0)).xyz);
  45. normal = normalize((voxel_gi_instances.data[index].xform * vec4(normal, 0.0)).xyz);
  46. position += normal * voxel_gi_instances.data[index].normal_bias;
  47. //this causes corrupted pixels, i have no idea why..
  48. if (any(bvec2(any(lessThan(position, vec3(0.0))), any(greaterThan(position, voxel_gi_instances.data[index].bounds))))) {
  49. return;
  50. }
  51. vec3 blendv = abs(position / voxel_gi_instances.data[index].bounds * 2.0 - 1.0);
  52. float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0);
  53. //float blend=1.0;
  54. float max_distance = length(voxel_gi_instances.data[index].bounds);
  55. vec3 cell_size = 1.0 / voxel_gi_instances.data[index].bounds;
  56. //radiance
  57. #define MAX_CONE_DIRS 4
  58. vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
  59. vec3(0.707107, 0.0, 0.707107),
  60. vec3(0.0, 0.707107, 0.707107),
  61. vec3(-0.707107, 0.0, 0.707107),
  62. vec3(0.0, -0.707107, 0.707107));
  63. float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.25, 0.25, 0.25);
  64. float cone_angle_tan = 0.98269;
  65. vec3 light = vec3(0.0);
  66. for (int i = 0; i < MAX_CONE_DIRS; i++) {
  67. vec3 dir = normalize((voxel_gi_instances.data[index].xform * vec4(normal_xform * cone_dirs[i], 0.0)).xyz);
  68. vec4 cone_light = voxel_cone_trace_45_degrees(voxel_gi_textures[index], cell_size, position, dir, cone_angle_tan, max_distance, voxel_gi_instances.data[index].bias);
  69. if (voxel_gi_instances.data[index].blend_ambient) {
  70. cone_light.rgb = mix(ambient, cone_light.rgb, min(1.0, cone_light.a / 0.95));
  71. }
  72. light += cone_weights[i] * cone_light.rgb;
  73. }
  74. light *= voxel_gi_instances.data[index].dynamic_range * voxel_gi_instances.data[index].exposure_normalization;
  75. out_diff += vec4(light * blend, blend);
  76. //irradiance
  77. 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);
  78. if (voxel_gi_instances.data[index].blend_ambient) {
  79. irr_light.rgb = mix(environment, irr_light.rgb, min(1.0, irr_light.a / 0.95));
  80. }
  81. irr_light.rgb *= voxel_gi_instances.data[index].dynamic_range * voxel_gi_instances.data[index].exposure_normalization;
  82. //irr_light=vec3(0.0);
  83. out_spec += vec4(irr_light.rgb * blend, blend);
  84. }
  85. vec2 octahedron_wrap(vec2 v) {
  86. vec2 signVal;
  87. signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
  88. signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
  89. return (1.0 - abs(v.yx)) * signVal;
  90. }
  91. vec2 octahedron_encode(vec3 n) {
  92. // https://twitter.com/Stubbesaurus/status/937994790553227264
  93. n /= (abs(n.x) + abs(n.y) + abs(n.z));
  94. n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
  95. n.xy = n.xy * 0.5 + 0.5;
  96. return n.xy;
  97. }
  98. void sdfgi_process(uint cascade, vec3 cascade_pos, vec3 cam_pos, vec3 cam_normal, vec3 cam_specular_normal, bool use_specular, float roughness, out vec3 diffuse_light, out vec3 specular_light, out float blend) {
  99. cascade_pos += cam_normal * sdfgi.normal_bias;
  100. vec3 base_pos = floor(cascade_pos);
  101. //cascade_pos += mix(vec3(0.0),vec3(0.01),lessThan(abs(cascade_pos-base_pos),vec3(0.01))) * cam_normal;
  102. ivec3 probe_base_pos = ivec3(base_pos);
  103. vec4 diffuse_accum = vec4(0.0);
  104. vec3 specular_accum;
  105. ivec3 tex_pos = ivec3(probe_base_pos.xy, int(cascade));
  106. tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
  107. tex_pos.xy = tex_pos.xy * (SDFGI_OCT_SIZE + 2) + ivec2(1);
  108. vec3 diffuse_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
  109. vec3 specular_posf;
  110. if (use_specular) {
  111. specular_accum = vec3(0.0);
  112. specular_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_specular_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
  113. }
  114. vec4 light_accum = vec4(0.0);
  115. float weight_accum = 0.0;
  116. for (uint j = 0; j < 8; j++) {
  117. ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
  118. ivec3 probe_posi = probe_base_pos;
  119. probe_posi += offset;
  120. // Compute weight
  121. vec3 probe_pos = vec3(probe_posi);
  122. vec3 probe_to_pos = cascade_pos - probe_pos;
  123. vec3 probe_dir = normalize(-probe_to_pos);
  124. vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
  125. float weight = trilinear.x * trilinear.y * trilinear.z * max(0.005, dot(cam_normal, probe_dir));
  126. // Compute lightprobe occlusion
  127. if (sdfgi.use_occlusion) {
  128. ivec3 occ_indexv = abs((sdfgi.cascades[cascade].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
  129. vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
  130. vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
  131. occ_pos.z += float(cascade);
  132. if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
  133. occ_pos.x += 1.0;
  134. }
  135. occ_pos *= sdfgi.occlusion_renormalize;
  136. float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_cascades, SAMPLER_LINEAR_CLAMP), occ_pos, 0.0), occ_mask);
  137. weight *= max(occlusion, 0.01);
  138. }
  139. // Compute lightprobe texture position
  140. vec3 diffuse;
  141. vec3 pos_uvw = diffuse_posf;
  142. pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
  143. pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
  144. diffuse = textureLod(sampler2DArray(sdfgi_lightprobe_texture, SAMPLER_LINEAR_CLAMP), pos_uvw, 0.0).rgb;
  145. diffuse_accum += vec4(diffuse * weight * sdfgi.cascades[cascade].exposure_normalization, weight);
  146. if (use_specular) {
  147. vec3 specular = vec3(0.0);
  148. vec3 pos_uvw = specular_posf;
  149. pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
  150. pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
  151. if (roughness < 0.99) {
  152. specular = textureLod(sampler2DArray(sdfgi_lightprobe_texture, SAMPLER_LINEAR_CLAMP), pos_uvw + vec3(0, 0, float(sdfgi.max_cascades)), 0.0).rgb;
  153. }
  154. if (roughness > 0.5) {
  155. specular = mix(specular, textureLod(sampler2DArray(sdfgi_lightprobe_texture, SAMPLER_LINEAR_CLAMP), pos_uvw, 0.0).rgb, (roughness - 0.5) * 2.0);
  156. }
  157. specular_accum += specular * weight * sdfgi.cascades[cascade].exposure_normalization;
  158. }
  159. }
  160. if (diffuse_accum.a > 0.0) {
  161. diffuse_accum.rgb /= diffuse_accum.a;
  162. }
  163. diffuse_light = diffuse_accum.rgb;
  164. if (use_specular) {
  165. if (diffuse_accum.a > 0.0) {
  166. specular_accum /= diffuse_accum.a;
  167. }
  168. specular_light = specular_accum;
  169. }
  170. {
  171. //process blend
  172. float blend_from = (float(sdfgi.probe_axis_size - 1) / 2.0) - 2.5;
  173. float blend_to = blend_from + 2.0;
  174. vec3 inner_pos = cam_pos * sdfgi.cascades[cascade].to_probe;
  175. float len = length(inner_pos);
  176. inner_pos = abs(normalize(inner_pos));
  177. len *= max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  178. if (len >= blend_from) {
  179. blend = smoothstep(blend_from, blend_to, len);
  180. } else {
  181. blend = 0.0;
  182. }
  183. }
  184. }