sdfgi_integrate.glsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 MAX_CASCADES 8
  6. layout(set = 0, binding = 1) uniform texture3D sdf_cascades[MAX_CASCADES];
  7. layout(set = 0, binding = 2) uniform texture3D light_cascades[MAX_CASCADES];
  8. layout(set = 0, binding = 3) uniform texture3D aniso0_cascades[MAX_CASCADES];
  9. layout(set = 0, binding = 4) uniform texture3D aniso1_cascades[MAX_CASCADES];
  10. layout(set = 0, binding = 6) uniform sampler linear_sampler;
  11. struct CascadeData {
  12. vec3 offset; //offset of (0,0,0) in world coordinates
  13. float to_cell; // 1/bounds * grid_size
  14. ivec3 probe_world_offset;
  15. uint pad;
  16. };
  17. layout(set = 0, binding = 7, std140) uniform Cascades {
  18. CascadeData data[MAX_CASCADES];
  19. }
  20. cascades;
  21. layout(r32ui, set = 0, binding = 8) uniform restrict uimage2DArray lightprobe_texture_data;
  22. layout(rgba16i, set = 0, binding = 9) uniform restrict iimage2DArray lightprobe_history_texture;
  23. layout(rgba32i, set = 0, binding = 10) uniform restrict iimage2D lightprobe_average_texture;
  24. //used for scrolling
  25. layout(rgba16i, set = 0, binding = 11) uniform restrict iimage2DArray lightprobe_history_scroll_texture;
  26. layout(rgba32i, set = 0, binding = 12) uniform restrict iimage2D lightprobe_average_scroll_texture;
  27. layout(rgba32i, set = 0, binding = 13) uniform restrict iimage2D lightprobe_average_parent_texture;
  28. layout(rgba16f, set = 0, binding = 14) uniform restrict writeonly image2DArray lightprobe_ambient_texture;
  29. #ifdef USE_CUBEMAP_ARRAY
  30. layout(set = 1, binding = 0) uniform textureCubeArray sky_irradiance;
  31. #else
  32. layout(set = 1, binding = 0) uniform textureCube sky_irradiance;
  33. #endif
  34. layout(set = 1, binding = 1) uniform sampler linear_sampler_mipmaps;
  35. #define HISTORY_BITS 10
  36. #define SKY_MODE_DISABLED 0
  37. #define SKY_MODE_COLOR 1
  38. #define SKY_MODE_SKY 2
  39. layout(push_constant, binding = 0, std430) uniform Params {
  40. vec3 grid_size;
  41. uint max_cascades;
  42. uint probe_axis_size;
  43. uint cascade;
  44. uint history_index;
  45. uint history_size;
  46. uint ray_count;
  47. float ray_bias;
  48. ivec2 image_size;
  49. ivec3 world_offset;
  50. uint sky_mode;
  51. ivec3 scroll;
  52. float sky_energy;
  53. vec3 sky_color;
  54. float y_mult;
  55. bool store_ambient_texture;
  56. uint pad[3];
  57. }
  58. params;
  59. const float PI = 3.14159265f;
  60. const float GOLDEN_ANGLE = PI * (3.0 - sqrt(5.0));
  61. vec3 vogel_hemisphere(uint p_index, uint p_count, float p_offset) {
  62. float r = sqrt(float(p_index) + 0.5f) / sqrt(float(p_count));
  63. float theta = float(p_index) * GOLDEN_ANGLE + p_offset;
  64. float y = cos(r * PI * 0.5);
  65. float l = sin(r * PI * 0.5);
  66. return vec3(l * cos(theta), l * sin(theta), y * (float(p_index & 1) * 2.0 - 1.0));
  67. }
  68. uvec3 hash3(uvec3 x) {
  69. x = ((x >> 16) ^ x) * 0x45d9f3b;
  70. x = ((x >> 16) ^ x) * 0x45d9f3b;
  71. x = (x >> 16) ^ x;
  72. return x;
  73. }
  74. float hashf3(vec3 co) {
  75. return fract(sin(dot(co, vec3(12.9898, 78.233, 137.13451))) * 43758.5453);
  76. }
  77. vec3 octahedron_encode(vec2 f) {
  78. // https://twitter.com/Stubbesaurus/status/937994790553227264
  79. f = f * 2.0 - 1.0;
  80. vec3 n = vec3(f.x, f.y, 1.0f - abs(f.x) - abs(f.y));
  81. float t = clamp(-n.z, 0.0, 1.0);
  82. n.x += n.x >= 0 ? -t : t;
  83. n.y += n.y >= 0 ? -t : t;
  84. return normalize(n);
  85. }
  86. uint rgbe_encode(vec3 color) {
  87. const float pow2to9 = 512.0f;
  88. const float B = 15.0f;
  89. const float N = 9.0f;
  90. const float LN2 = 0.6931471805599453094172321215;
  91. float cRed = clamp(color.r, 0.0, 65408.0);
  92. float cGreen = clamp(color.g, 0.0, 65408.0);
  93. float cBlue = clamp(color.b, 0.0, 65408.0);
  94. float cMax = max(cRed, max(cGreen, cBlue));
  95. float expp = max(-B - 1.0f, floor(log(cMax) / LN2)) + 1.0f + B;
  96. float sMax = floor((cMax / pow(2.0f, expp - B - N)) + 0.5f);
  97. float exps = expp + 1.0f;
  98. if (0.0 <= sMax && sMax < pow2to9) {
  99. exps = expp;
  100. }
  101. float sRed = floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  102. float sGreen = floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  103. float sBlue = floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  104. return (uint(sRed) & 0x1FF) | ((uint(sGreen) & 0x1FF) << 9) | ((uint(sBlue) & 0x1FF) << 18) | ((uint(exps) & 0x1F) << 27);
  105. }
  106. struct SH {
  107. #if (SH_SIZE == 16)
  108. float c[48];
  109. #else
  110. float c[28];
  111. #endif
  112. };
  113. shared SH sh_accum[64]; //8x8
  114. void main() {
  115. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  116. if (any(greaterThanEqual(pos, params.image_size))) { //too large, do nothing
  117. return;
  118. }
  119. uint probe_index = gl_LocalInvocationID.x + gl_LocalInvocationID.y * 8;
  120. #ifdef MODE_PROCESS
  121. float probe_cell_size = float(params.grid_size.x / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell;
  122. ivec3 probe_cell;
  123. probe_cell.x = pos.x % int(params.probe_axis_size);
  124. probe_cell.y = pos.y;
  125. probe_cell.z = pos.x / int(params.probe_axis_size);
  126. vec3 probe_pos = cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size;
  127. vec3 pos_to_uvw = 1.0 / params.grid_size;
  128. for (uint i = 0; i < SH_SIZE * 3; i++) {
  129. sh_accum[probe_index].c[i] = 0.0;
  130. }
  131. // quickly ensure each probe has a different "offset" for the vogel function, based on integer world position
  132. uvec3 h3 = hash3(uvec3(params.world_offset + probe_cell));
  133. float offset = hashf3(vec3(h3 & uvec3(0xFFFFF)));
  134. //for a more homogeneous hemisphere, alternate based on history frames
  135. uint ray_offset = params.history_index;
  136. uint ray_mult = params.history_size;
  137. uint ray_total = ray_mult * params.ray_count;
  138. for (uint i = 0; i < params.ray_count; i++) {
  139. vec3 ray_dir = vogel_hemisphere(ray_offset + i * ray_mult, ray_total, offset);
  140. ray_dir.y *= params.y_mult;
  141. ray_dir = normalize(ray_dir);
  142. //needs to be visible
  143. vec3 ray_pos = probe_pos;
  144. vec3 inv_dir = 1.0 / ray_dir;
  145. bool hit = false;
  146. uint hit_cascade;
  147. float bias = params.ray_bias;
  148. vec3 abs_ray_dir = abs(ray_dir);
  149. ray_pos += ray_dir * 1.0 / max(abs_ray_dir.x, max(abs_ray_dir.y, abs_ray_dir.z)) * bias / cascades.data[params.cascade].to_cell;
  150. vec3 uvw;
  151. for (uint j = params.cascade; j < params.max_cascades; j++) {
  152. //convert to local bounds
  153. vec3 pos = ray_pos - cascades.data[j].offset;
  154. pos *= cascades.data[j].to_cell;
  155. if (any(lessThan(pos, vec3(0.0))) || any(greaterThanEqual(pos, params.grid_size))) {
  156. continue; //already past bounds for this cascade, goto next
  157. }
  158. //find maximum advance distance (until reaching bounds)
  159. vec3 t0 = -pos * inv_dir;
  160. vec3 t1 = (params.grid_size - pos) * inv_dir;
  161. vec3 tmax = max(t0, t1);
  162. float max_advance = min(tmax.x, min(tmax.y, tmax.z));
  163. float advance = 0.0;
  164. while (advance < max_advance) {
  165. //read how much to advance from SDF
  166. uvw = (pos + ray_dir * advance) * pos_to_uvw;
  167. float distance = texture(sampler3D(sdf_cascades[j], linear_sampler), uvw).r * 255.0 - 1.0;
  168. if (distance < 0.05) {
  169. //consider hit
  170. hit = true;
  171. break;
  172. }
  173. advance += distance;
  174. }
  175. if (hit) {
  176. hit_cascade = j;
  177. break;
  178. }
  179. //change ray origin to collision with bounds
  180. pos += ray_dir * max_advance;
  181. pos /= cascades.data[j].to_cell;
  182. pos += cascades.data[j].offset;
  183. ray_pos = pos;
  184. }
  185. vec4 light;
  186. if (hit) {
  187. //avoid reading different texture from different threads
  188. for (uint j = params.cascade; j < params.max_cascades; j++) {
  189. if (j == hit_cascade) {
  190. const float EPSILON = 0.001;
  191. vec3 hit_normal = normalize(vec3(
  192. texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw + vec3(EPSILON, 0.0, 0.0)).r - texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw - vec3(EPSILON, 0.0, 0.0)).r,
  193. texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw + vec3(0.0, EPSILON, 0.0)).r - texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw - vec3(0.0, EPSILON, 0.0)).r,
  194. texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw + vec3(0.0, 0.0, EPSILON)).r - texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw - vec3(0.0, 0.0, EPSILON)).r));
  195. vec3 hit_light = texture(sampler3D(light_cascades[hit_cascade], linear_sampler), uvw).rgb;
  196. vec4 aniso0 = texture(sampler3D(aniso0_cascades[hit_cascade], linear_sampler), uvw);
  197. vec3 hit_aniso0 = aniso0.rgb;
  198. vec3 hit_aniso1 = vec3(aniso0.a, texture(sampler3D(aniso1_cascades[hit_cascade], linear_sampler), uvw).rg);
  199. //one liner magic
  200. light.rgb = hit_light * (dot(max(vec3(0.0), (hit_normal * hit_aniso0)), vec3(1.0)) + dot(max(vec3(0.0), (-hit_normal * hit_aniso1)), vec3(1.0)));
  201. light.a = 1.0;
  202. }
  203. }
  204. } else if (params.sky_mode == SKY_MODE_SKY) {
  205. #ifdef USE_CUBEMAP_ARRAY
  206. light.rgb = textureLod(samplerCubeArray(sky_irradiance, linear_sampler_mipmaps), vec4(ray_dir, 0.0), 2.0).rgb; //use second mipmap because we dont usually throw a lot of rays, so this compensates
  207. #else
  208. light.rgb = textureLod(samplerCube(sky_irradiance, linear_sampler_mipmaps), ray_dir, 2.0).rgb; //use second mipmap because we dont usually throw a lot of rays, so this compensates
  209. #endif
  210. light.rgb *= params.sky_energy;
  211. light.a = 0.0;
  212. } else if (params.sky_mode == SKY_MODE_COLOR) {
  213. light.rgb = params.sky_color;
  214. light.rgb *= params.sky_energy;
  215. light.a = 0.0;
  216. } else {
  217. light = vec4(0, 0, 0, 0);
  218. }
  219. vec3 ray_dir2 = ray_dir * ray_dir;
  220. #define SH_ACCUM(m_idx, m_value) \
  221. { \
  222. vec3 l = light.rgb * (m_value); \
  223. sh_accum[probe_index].c[m_idx * 3 + 0] += l.r; \
  224. sh_accum[probe_index].c[m_idx * 3 + 1] += l.g; \
  225. sh_accum[probe_index].c[m_idx * 3 + 2] += l.b; \
  226. }
  227. SH_ACCUM(0, 0.282095); //l0
  228. SH_ACCUM(1, 0.488603 * ray_dir.y); //l1n1
  229. SH_ACCUM(2, 0.488603 * ray_dir.z); //l1n0
  230. SH_ACCUM(3, 0.488603 * ray_dir.x); //l1p1
  231. SH_ACCUM(4, 1.092548 * ray_dir.x * ray_dir.y); //l2n2
  232. SH_ACCUM(5, 1.092548 * ray_dir.y * ray_dir.z); //l2n1
  233. SH_ACCUM(6, 0.315392 * (3.0 * ray_dir2.z - 1.0)); //l20
  234. SH_ACCUM(7, 1.092548 * ray_dir.x * ray_dir.z); //l2p1
  235. SH_ACCUM(8, 0.546274 * (ray_dir2.x - ray_dir2.y)); //l2p2
  236. #if (SH_SIZE == 16)
  237. SH_ACCUM(9, 0.590043 * ray_dir.y * (3.0f * ray_dir2.x - ray_dir2.y));
  238. SH_ACCUM(10, 2.890611 * ray_dir.y * ray_dir.x * ray_dir.z);
  239. SH_ACCUM(11, 0.646360 * ray_dir.y * (-1.0f + 5.0f * ray_dir2.z));
  240. SH_ACCUM(12, 0.373176 * (5.0f * ray_dir2.z * ray_dir.z - 3.0f * ray_dir.z));
  241. SH_ACCUM(13, 0.457045 * ray_dir.x * (-1.0f + 5.0f * ray_dir2.z));
  242. SH_ACCUM(14, 1.445305 * (ray_dir2.x - ray_dir2.y) * ray_dir.z);
  243. SH_ACCUM(15, 0.590043 * ray_dir.x * (ray_dir2.x - 3.0f * ray_dir2.y));
  244. #endif
  245. }
  246. for (uint i = 0; i < SH_SIZE; i++) {
  247. // store in history texture
  248. ivec3 prev_pos = ivec3(pos.x, pos.y * SH_SIZE + i, int(params.history_index));
  249. ivec2 average_pos = prev_pos.xy;
  250. vec4 value = vec4(sh_accum[probe_index].c[i * 3 + 0], sh_accum[probe_index].c[i * 3 + 1], sh_accum[probe_index].c[i * 3 + 2], 1.0) * 4.0 / float(params.ray_count);
  251. ivec4 ivalue = clamp(ivec4(value * float(1 << HISTORY_BITS)), -32768, 32767); //clamp to 16 bits, so higher values don't break average
  252. ivec4 prev_value = imageLoad(lightprobe_history_texture, prev_pos);
  253. ivec4 average = imageLoad(lightprobe_average_texture, average_pos);
  254. average -= prev_value;
  255. average += ivalue;
  256. imageStore(lightprobe_history_texture, prev_pos, ivalue);
  257. imageStore(lightprobe_average_texture, average_pos, average);
  258. if (params.store_ambient_texture && i == 0) {
  259. ivec3 ambient_pos = ivec3(pos, int(params.cascade));
  260. vec4 ambient_light = (vec4(average) / float(params.history_size)) / float(1 << HISTORY_BITS);
  261. ambient_light *= 0.88622; // SHL0
  262. imageStore(lightprobe_ambient_texture, ambient_pos, ambient_light);
  263. }
  264. }
  265. #endif // MODE PROCESS
  266. #ifdef MODE_STORE
  267. // converting to octahedral in this step is required because
  268. // octahedral is much faster to read from the screen than spherical harmonics,
  269. // despite the very slight quality loss
  270. ivec2 sh_pos = (pos / OCT_SIZE) * ivec2(1, SH_SIZE);
  271. ivec2 oct_pos = (pos / OCT_SIZE) * (OCT_SIZE + 2) + ivec2(1);
  272. ivec2 local_pos = pos % OCT_SIZE;
  273. //compute the octahedral normal for this texel
  274. vec3 normal = octahedron_encode(vec2(local_pos) / float(OCT_SIZE));
  275. // read the spherical harmonic
  276. vec3 normal2 = normal * normal;
  277. float c[SH_SIZE] = float[](
  278. 0.282095, //l0
  279. 0.488603 * normal.y, //l1n1
  280. 0.488603 * normal.z, //l1n0
  281. 0.488603 * normal.x, //l1p1
  282. 1.092548 * normal.x * normal.y, //l2n2
  283. 1.092548 * normal.y * normal.z, //l2n1
  284. 0.315392 * (3.0 * normal2.z - 1.0), //l20
  285. 1.092548 * normal.x * normal.z, //l2p1
  286. 0.546274 * (normal2.x - normal2.y) //l2p2
  287. #if (SH_SIZE == 16)
  288. ,
  289. 0.590043 * normal.y * (3.0f * normal2.x - normal2.y),
  290. 2.890611 * normal.y * normal.x * normal.z,
  291. 0.646360 * normal.y * (-1.0f + 5.0f * normal2.z),
  292. 0.373176 * (5.0f * normal2.z * normal.z - 3.0f * normal.z),
  293. 0.457045 * normal.x * (-1.0f + 5.0f * normal2.z),
  294. 1.445305 * (normal2.x - normal2.y) * normal.z,
  295. 0.590043 * normal.x * (normal2.x - 3.0f * normal2.y)
  296. #endif
  297. );
  298. const float l_mult[SH_SIZE] = float[](
  299. 1.0,
  300. 2.0 / 3.0,
  301. 2.0 / 3.0,
  302. 2.0 / 3.0,
  303. 1.0 / 4.0,
  304. 1.0 / 4.0,
  305. 1.0 / 4.0,
  306. 1.0 / 4.0,
  307. 1.0 / 4.0
  308. #if (SH_SIZE == 16)
  309. , // l4 does not contribute to irradiance
  310. 0.0,
  311. 0.0,
  312. 0.0,
  313. 0.0,
  314. 0.0,
  315. 0.0,
  316. 0.0
  317. #endif
  318. );
  319. vec3 irradiance = vec3(0.0);
  320. vec3 radiance = vec3(0.0);
  321. for (uint i = 0; i < SH_SIZE; i++) {
  322. // store in history texture
  323. ivec2 average_pos = sh_pos + ivec2(0, i);
  324. ivec4 average = imageLoad(lightprobe_average_texture, average_pos);
  325. vec4 sh = (vec4(average) / float(params.history_size)) / float(1 << HISTORY_BITS);
  326. vec3 m = sh.rgb * c[i] * 4.0;
  327. irradiance += m * l_mult[i];
  328. radiance += m;
  329. }
  330. //encode RGBE9995 for the final texture
  331. uint irradiance_rgbe = rgbe_encode(irradiance);
  332. uint radiance_rgbe = rgbe_encode(radiance);
  333. //store in octahedral map
  334. ivec3 texture_pos = ivec3(oct_pos, int(params.cascade));
  335. ivec3 copy_to[4] = ivec3[](ivec3(-2, -2, -2), ivec3(-2, -2, -2), ivec3(-2, -2, -2), ivec3(-2, -2, -2));
  336. copy_to[0] = texture_pos + ivec3(local_pos, 0);
  337. if (local_pos == ivec2(0, 0)) {
  338. copy_to[1] = texture_pos + ivec3(OCT_SIZE - 1, -1, 0);
  339. copy_to[2] = texture_pos + ivec3(-1, OCT_SIZE - 1, 0);
  340. copy_to[3] = texture_pos + ivec3(OCT_SIZE, OCT_SIZE, 0);
  341. } else if (local_pos == ivec2(OCT_SIZE - 1, 0)) {
  342. copy_to[1] = texture_pos + ivec3(0, -1, 0);
  343. copy_to[2] = texture_pos + ivec3(OCT_SIZE, OCT_SIZE - 1, 0);
  344. copy_to[3] = texture_pos + ivec3(-1, OCT_SIZE, 0);
  345. } else if (local_pos == ivec2(0, OCT_SIZE - 1)) {
  346. copy_to[1] = texture_pos + ivec3(-1, 0, 0);
  347. copy_to[2] = texture_pos + ivec3(OCT_SIZE - 1, OCT_SIZE, 0);
  348. copy_to[3] = texture_pos + ivec3(OCT_SIZE, -1, 0);
  349. } else if (local_pos == ivec2(OCT_SIZE - 1, OCT_SIZE - 1)) {
  350. copy_to[1] = texture_pos + ivec3(0, OCT_SIZE, 0);
  351. copy_to[2] = texture_pos + ivec3(OCT_SIZE, 0, 0);
  352. copy_to[3] = texture_pos + ivec3(-1, -1, 0);
  353. } else if (local_pos.y == 0) {
  354. copy_to[1] = texture_pos + ivec3(OCT_SIZE - local_pos.x - 1, local_pos.y - 1, 0);
  355. } else if (local_pos.x == 0) {
  356. copy_to[1] = texture_pos + ivec3(local_pos.x - 1, OCT_SIZE - local_pos.y - 1, 0);
  357. } else if (local_pos.y == OCT_SIZE - 1) {
  358. copy_to[1] = texture_pos + ivec3(OCT_SIZE - local_pos.x - 1, local_pos.y + 1, 0);
  359. } else if (local_pos.x == OCT_SIZE - 1) {
  360. copy_to[1] = texture_pos + ivec3(local_pos.x + 1, OCT_SIZE - local_pos.y - 1, 0);
  361. }
  362. for (int i = 0; i < 4; i++) {
  363. if (copy_to[i] == ivec3(-2, -2, -2)) {
  364. continue;
  365. }
  366. imageStore(lightprobe_texture_data, copy_to[i], uvec4(irradiance_rgbe));
  367. imageStore(lightprobe_texture_data, copy_to[i] + ivec3(0, 0, int(params.max_cascades)), uvec4(radiance_rgbe));
  368. }
  369. #endif
  370. #ifdef MODE_SCROLL
  371. ivec3 probe_cell;
  372. probe_cell.x = pos.x % int(params.probe_axis_size);
  373. probe_cell.y = pos.y;
  374. probe_cell.z = pos.x / int(params.probe_axis_size);
  375. ivec3 read_probe = probe_cell - params.scroll;
  376. if (all(greaterThanEqual(read_probe, ivec3(0))) && all(lessThan(read_probe, ivec3(params.probe_axis_size)))) {
  377. // can scroll
  378. ivec2 tex_pos;
  379. tex_pos = read_probe.xy;
  380. tex_pos.x += read_probe.z * int(params.probe_axis_size);
  381. //scroll
  382. for (uint j = 0; j < params.history_size; j++) {
  383. for (int i = 0; i < SH_SIZE; i++) {
  384. // copy from history texture
  385. ivec3 src_pos = ivec3(tex_pos.x, tex_pos.y * SH_SIZE + i, int(j));
  386. ivec3 dst_pos = ivec3(pos.x, pos.y * SH_SIZE + i, int(j));
  387. ivec4 value = imageLoad(lightprobe_history_texture, src_pos);
  388. imageStore(lightprobe_history_scroll_texture, dst_pos, value);
  389. }
  390. }
  391. for (int i = 0; i < SH_SIZE; i++) {
  392. // copy from average texture
  393. ivec2 src_pos = ivec2(tex_pos.x, tex_pos.y * SH_SIZE + i);
  394. ivec2 dst_pos = ivec2(pos.x, pos.y * SH_SIZE + i);
  395. ivec4 value = imageLoad(lightprobe_average_texture, src_pos);
  396. imageStore(lightprobe_average_scroll_texture, dst_pos, value);
  397. }
  398. } else if (params.cascade < params.max_cascades - 1) {
  399. //can't scroll, must look for position in parent cascade
  400. //to global coords
  401. float cell_to_probe = float(params.grid_size.x / float(params.probe_axis_size - 1));
  402. float probe_cell_size = cell_to_probe / cascades.data[params.cascade].to_cell;
  403. vec3 probe_pos = cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size;
  404. //to parent local coords
  405. float probe_cell_size_next = cell_to_probe / cascades.data[params.cascade + 1].to_cell;
  406. probe_pos -= cascades.data[params.cascade + 1].offset;
  407. probe_pos /= probe_cell_size_next;
  408. ivec3 probe_posi = ivec3(probe_pos);
  409. //add up all light, no need to use occlusion here, since occlusion will do its work afterwards
  410. vec4 average_light[SH_SIZE] = vec4[](vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0)
  411. #if (SH_SIZE == 16)
  412. ,
  413. vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0)
  414. #endif
  415. );
  416. float total_weight = 0.0;
  417. for (int i = 0; i < 8; i++) {
  418. ivec3 offset = probe_posi + ((ivec3(i) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1));
  419. vec3 trilinear = vec3(1.0) - abs(probe_pos - vec3(offset));
  420. float weight = trilinear.x * trilinear.y * trilinear.z;
  421. ivec2 tex_pos;
  422. tex_pos = offset.xy;
  423. tex_pos.x += offset.z * int(params.probe_axis_size);
  424. for (int j = 0; j < SH_SIZE; j++) {
  425. // copy from history texture
  426. ivec2 src_pos = ivec2(tex_pos.x, tex_pos.y * SH_SIZE + j);
  427. ivec4 average = imageLoad(lightprobe_average_parent_texture, src_pos);
  428. vec4 value = (vec4(average) / float(params.history_size)) / float(1 << HISTORY_BITS);
  429. average_light[j] += value * weight;
  430. }
  431. total_weight += weight;
  432. }
  433. if (total_weight > 0.0) {
  434. total_weight = 1.0 / total_weight;
  435. }
  436. //store the averaged values everywhere
  437. for (int i = 0; i < SH_SIZE; i++) {
  438. ivec4 ivalue = clamp(ivec4(average_light[i] * total_weight * float(1 << HISTORY_BITS)), ivec4(-32768), ivec4(32767)); //clamp to 16 bits, so higher values don't break average
  439. // copy from history texture
  440. ivec3 dst_pos = ivec3(pos.x, pos.y * SH_SIZE + i, 0);
  441. for (uint j = 0; j < params.history_size; j++) {
  442. dst_pos.z = int(j);
  443. imageStore(lightprobe_history_scroll_texture, dst_pos, ivalue);
  444. }
  445. ivalue *= int(params.history_size); //average needs to have all history added up
  446. imageStore(lightprobe_average_scroll_texture, dst_pos.xy, ivalue);
  447. }
  448. } else {
  449. //scroll at the edge of the highest cascade, just copy what is there,
  450. //since its the closest we have anyway
  451. for (uint j = 0; j < params.history_size; j++) {
  452. ivec2 tex_pos;
  453. tex_pos = probe_cell.xy;
  454. tex_pos.x += probe_cell.z * int(params.probe_axis_size);
  455. for (int i = 0; i < SH_SIZE; i++) {
  456. // copy from history texture
  457. ivec3 src_pos = ivec3(tex_pos.x, tex_pos.y * SH_SIZE + i, int(j));
  458. ivec3 dst_pos = ivec3(pos.x, pos.y * SH_SIZE + i, int(j));
  459. ivec4 value = imageLoad(lightprobe_history_texture, dst_pos);
  460. imageStore(lightprobe_history_scroll_texture, dst_pos, value);
  461. }
  462. }
  463. for (int i = 0; i < SH_SIZE; i++) {
  464. // copy from average texture
  465. ivec2 spos = ivec2(pos.x, pos.y * SH_SIZE + i);
  466. ivec4 average = imageLoad(lightprobe_average_texture, spos);
  467. imageStore(lightprobe_average_scroll_texture, spos, average);
  468. }
  469. }
  470. #endif
  471. #ifdef MODE_SCROLL_STORE
  472. //do not update probe texture, as these will be updated later
  473. for (uint j = 0; j < params.history_size; j++) {
  474. for (int i = 0; i < SH_SIZE; i++) {
  475. // copy from history texture
  476. ivec3 spos = ivec3(pos.x, pos.y * SH_SIZE + i, int(j));
  477. ivec4 value = imageLoad(lightprobe_history_scroll_texture, spos);
  478. imageStore(lightprobe_history_texture, spos, value);
  479. }
  480. }
  481. for (int i = 0; i < SH_SIZE; i++) {
  482. // copy from average texture
  483. ivec2 spos = ivec2(pos.x, pos.y * SH_SIZE + i);
  484. ivec4 average = imageLoad(lightprobe_average_scroll_texture, spos);
  485. imageStore(lightprobe_average_texture, spos, average);
  486. }
  487. #endif
  488. }