lm_compute.glsl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #[versions]
  2. primary = "#define MODE_DIRECT_LIGHT";
  3. secondary = "#define MODE_BOUNCE_LIGHT";
  4. dilate = "#define MODE_DILATE";
  5. unocclude = "#define MODE_UNOCCLUDE";
  6. light_probes = "#define MODE_LIGHT_PROBES";
  7. #[compute]
  8. #version 450
  9. VERSION_DEFINES
  10. // One 2D local group focusing in one layer at a time, though all
  11. // in parallel (no barriers) makes more sense than a 3D local group
  12. // as this can take more advantage of the cache for each group.
  13. #ifdef MODE_LIGHT_PROBES
  14. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  15. #else
  16. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  17. #endif
  18. #include "lm_common_inc.glsl"
  19. #ifdef MODE_LIGHT_PROBES
  20. layout(set = 1, binding = 0, std430) restrict buffer LightProbeData {
  21. vec4 data[];
  22. }
  23. light_probes;
  24. layout(set = 1, binding = 1) uniform texture2DArray source_light;
  25. layout(set = 1, binding = 2) uniform texture2DArray source_direct_light; //also need the direct light, which was omitted
  26. layout(set = 1, binding = 3) uniform texture2D environment;
  27. #endif
  28. #ifdef MODE_UNOCCLUDE
  29. layout(rgba32f, set = 1, binding = 0) uniform restrict image2DArray position;
  30. layout(rgba32f, set = 1, binding = 1) uniform restrict readonly image2DArray unocclude;
  31. #endif
  32. #if defined(MODE_DIRECT_LIGHT) || defined(MODE_BOUNCE_LIGHT)
  33. layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly image2DArray dest_light;
  34. layout(set = 1, binding = 1) uniform texture2DArray source_light;
  35. layout(set = 1, binding = 2) uniform texture2DArray source_position;
  36. layout(set = 1, binding = 3) uniform texture2DArray source_normal;
  37. layout(rgba16f, set = 1, binding = 4) uniform restrict image2DArray accum_light;
  38. #endif
  39. #ifdef MODE_BOUNCE_LIGHT
  40. layout(rgba32f, set = 1, binding = 5) uniform restrict image2DArray bounce_accum;
  41. layout(set = 1, binding = 6) uniform texture2D environment;
  42. #endif
  43. #ifdef MODE_DIRECT_LIGHT
  44. layout(rgba32f, set = 1, binding = 5) uniform restrict writeonly image2DArray primary_dynamic;
  45. #endif
  46. #ifdef MODE_DILATE
  47. layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly image2DArray dest_light;
  48. layout(set = 1, binding = 1) uniform texture2DArray source_light;
  49. #endif
  50. layout(push_constant, binding = 0, std430) uniform Params {
  51. ivec2 atlas_size; // x used for light probe mode total probes
  52. uint ray_count;
  53. uint ray_to;
  54. vec3 world_size;
  55. float bias;
  56. vec3 to_cell_offset;
  57. uint ray_from;
  58. vec3 to_cell_size;
  59. uint light_count;
  60. int grid_size;
  61. int atlas_slice;
  62. ivec2 region_ofs;
  63. mat3x4 env_transform;
  64. }
  65. params;
  66. //check it, but also return distance and barycentric coords (for uv lookup)
  67. bool ray_hits_triangle(vec3 from, vec3 dir, float max_dist, vec3 p0, vec3 p1, vec3 p2, out float r_distance, out vec3 r_barycentric) {
  68. const vec3 e0 = p1 - p0;
  69. const vec3 e1 = p0 - p2;
  70. vec3 triangleNormal = cross(e1, e0);
  71. const vec3 e2 = (1.0 / dot(triangleNormal, dir)) * (p0 - from);
  72. const vec3 i = cross(dir, e2);
  73. r_barycentric.y = dot(i, e1);
  74. r_barycentric.z = dot(i, e0);
  75. r_barycentric.x = 1.0 - (r_barycentric.z + r_barycentric.y);
  76. r_distance = dot(triangleNormal, e2);
  77. return (r_distance > params.bias) && (r_distance < max_dist) && all(greaterThanEqual(r_barycentric, vec3(0.0)));
  78. }
  79. bool trace_ray(vec3 p_from, vec3 p_to
  80. #if defined(MODE_BOUNCE_LIGHT) || defined(MODE_LIGHT_PROBES)
  81. ,
  82. out uint r_triangle, out vec3 r_barycentric
  83. #endif
  84. #if defined(MODE_UNOCCLUDE)
  85. ,
  86. out float r_distance, out vec3 r_normal
  87. #endif
  88. ) {
  89. /* world coords */
  90. vec3 rel = p_to - p_from;
  91. float rel_len = length(rel);
  92. vec3 dir = normalize(rel);
  93. vec3 inv_dir = 1.0 / dir;
  94. /* cell coords */
  95. vec3 from_cell = (p_from - params.to_cell_offset) * params.to_cell_size;
  96. vec3 to_cell = (p_to - params.to_cell_offset) * params.to_cell_size;
  97. //prepare DDA
  98. vec3 rel_cell = to_cell - from_cell;
  99. ivec3 icell = ivec3(from_cell);
  100. ivec3 iendcell = ivec3(to_cell);
  101. vec3 dir_cell = normalize(rel_cell);
  102. vec3 delta = abs(1.0 / dir_cell); //vec3(length(rel_cell)) / rel_cell);
  103. ivec3 step = ivec3(sign(rel_cell));
  104. vec3 side = (sign(rel_cell) * (vec3(icell) - from_cell) + (sign(rel_cell) * 0.5) + 0.5) * delta;
  105. uint iters = 0;
  106. while (all(greaterThanEqual(icell, ivec3(0))) && all(lessThan(icell, ivec3(params.grid_size))) && iters < 1000) {
  107. uvec2 cell_data = texelFetch(usampler3D(grid, linear_sampler), icell, 0).xy;
  108. if (cell_data.x > 0) { //triangles here
  109. bool hit = false;
  110. #if defined(MODE_UNOCCLUDE)
  111. bool hit_backface = false;
  112. #endif
  113. float best_distance = 1e20;
  114. for (uint i = 0; i < cell_data.x; i++) {
  115. uint tidx = grid_indices.data[cell_data.y + i];
  116. //Ray-Box test
  117. vec3 t0 = (boxes.data[tidx].min_bounds - p_from) * inv_dir;
  118. vec3 t1 = (boxes.data[tidx].max_bounds - p_from) * inv_dir;
  119. vec3 tmin = min(t0, t1), tmax = max(t0, t1);
  120. if (max(tmin.x, max(tmin.y, tmin.z)) <= min(tmax.x, min(tmax.y, tmax.z))) {
  121. continue; //ray box failed
  122. }
  123. //prepare triangle vertices
  124. vec3 vtx0 = vertices.data[triangles.data[tidx].indices.x].position;
  125. vec3 vtx1 = vertices.data[triangles.data[tidx].indices.y].position;
  126. vec3 vtx2 = vertices.data[triangles.data[tidx].indices.z].position;
  127. #if defined(MODE_UNOCCLUDE)
  128. vec3 normal = -normalize(cross((vtx0 - vtx1), (vtx0 - vtx2)));
  129. bool backface = dot(normal, dir) >= 0.0;
  130. #endif
  131. float distance;
  132. vec3 barycentric;
  133. if (ray_hits_triangle(p_from, dir, rel_len, vtx0, vtx1, vtx2, distance, barycentric)) {
  134. #ifdef MODE_DIRECT_LIGHT
  135. return true; //any hit good
  136. #endif
  137. #if defined(MODE_UNOCCLUDE)
  138. if (!backface) {
  139. // the case of meshes having both a front and back face in the same plane is more common than
  140. // expected, so if this is a front-face, bias it closer to the ray origin, so it always wins over the back-face
  141. distance = max(params.bias, distance - params.bias);
  142. }
  143. hit = true;
  144. if (distance < best_distance) {
  145. hit_backface = backface;
  146. best_distance = distance;
  147. r_distance = distance;
  148. r_normal = normal;
  149. }
  150. #endif
  151. #if defined(MODE_BOUNCE_LIGHT) || defined(MODE_LIGHT_PROBES)
  152. hit = true;
  153. if (distance < best_distance) {
  154. best_distance = distance;
  155. r_triangle = tidx;
  156. r_barycentric = barycentric;
  157. }
  158. #endif
  159. }
  160. }
  161. #if defined(MODE_UNOCCLUDE)
  162. if (hit) {
  163. return hit_backface;
  164. }
  165. #endif
  166. #if defined(MODE_BOUNCE_LIGHT) || defined(MODE_LIGHT_PROBES)
  167. if (hit) {
  168. return true;
  169. }
  170. #endif
  171. }
  172. if (icell == iendcell) {
  173. break;
  174. }
  175. bvec3 mask = lessThanEqual(side.xyz, min(side.yzx, side.zxy));
  176. side += vec3(mask) * delta;
  177. icell += ivec3(vec3(mask)) * step;
  178. iters++;
  179. }
  180. return false;
  181. }
  182. const float PI = 3.14159265f;
  183. const float GOLDEN_ANGLE = PI * (3.0 - sqrt(5.0));
  184. vec3 vogel_hemisphere(uint p_index, uint p_count, float p_offset) {
  185. float r = sqrt(float(p_index) + 0.5f) / sqrt(float(p_count));
  186. float theta = float(p_index) * GOLDEN_ANGLE + p_offset;
  187. float y = cos(r * PI * 0.5);
  188. float l = sin(r * PI * 0.5);
  189. return vec3(l * cos(theta), l * sin(theta), y);
  190. }
  191. float quick_hash(vec2 pos) {
  192. return fract(sin(dot(pos * 19.19, vec2(49.5791, 97.413))) * 49831.189237);
  193. }
  194. float get_omni_attenuation(float distance, float inv_range, float decay) {
  195. float nd = distance * inv_range;
  196. nd *= nd;
  197. nd *= nd; // nd^4
  198. nd = max(1.0 - nd, 0.0);
  199. nd *= nd; // nd^2
  200. return nd * pow(max(distance, 0.0001), -decay);
  201. }
  202. void main() {
  203. #ifdef MODE_LIGHT_PROBES
  204. int probe_index = int(gl_GlobalInvocationID.x);
  205. if (probe_index >= params.atlas_size.x) { //too large, do nothing
  206. return;
  207. }
  208. #else
  209. ivec2 atlas_pos = ivec2(gl_GlobalInvocationID.xy) + params.region_ofs;
  210. if (any(greaterThanEqual(atlas_pos, params.atlas_size))) { //too large, do nothing
  211. return;
  212. }
  213. #endif
  214. #ifdef MODE_DIRECT_LIGHT
  215. vec3 normal = texelFetch(sampler2DArray(source_normal, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0).xyz;
  216. if (length(normal) < 0.5) {
  217. return; //empty texel, no process
  218. }
  219. vec3 position = texelFetch(sampler2DArray(source_position, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0).xyz;
  220. //go through all lights
  221. //start by own light (emissive)
  222. vec3 static_light = vec3(0.0);
  223. vec3 dynamic_light = vec3(0.0);
  224. #ifdef USE_SH_LIGHTMAPS
  225. vec4 sh_accum[4] = vec4[](
  226. vec4(0.0, 0.0, 0.0, 1.0),
  227. vec4(0.0, 0.0, 0.0, 1.0),
  228. vec4(0.0, 0.0, 0.0, 1.0),
  229. vec4(0.0, 0.0, 0.0, 1.0));
  230. #endif
  231. for (uint i = 0; i < params.light_count; i++) {
  232. vec3 light_pos;
  233. float attenuation;
  234. if (lights.data[i].type == LIGHT_TYPE_DIRECTIONAL) {
  235. vec3 light_vec = lights.data[i].direction;
  236. light_pos = position - light_vec * length(params.world_size);
  237. attenuation = 1.0;
  238. } else {
  239. light_pos = lights.data[i].position;
  240. float d = distance(position, light_pos);
  241. if (d > lights.data[i].range) {
  242. continue;
  243. }
  244. d /= lights.data[i].range;
  245. attenuation = get_omni_attenuation(d, 1.0 / lights.data[i].range, lights.data[i].attenuation);
  246. if (lights.data[i].type == LIGHT_TYPE_SPOT) {
  247. vec3 rel = normalize(position - light_pos);
  248. float cos_spot_angle = lights.data[i].cos_spot_angle;
  249. float cos_angle = dot(rel, lights.data[i].direction);
  250. if (cos_angle < cos_spot_angle) {
  251. continue; //invisible, dont try
  252. }
  253. float scos = max(cos_angle, cos_spot_angle);
  254. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
  255. attenuation *= 1.0 - pow(spot_rim, lights.data[i].inv_spot_attenuation);
  256. }
  257. }
  258. vec3 light_dir = normalize(light_pos - position);
  259. attenuation *= max(0.0, dot(normal, light_dir));
  260. if (attenuation <= 0.0001) {
  261. continue; //no need to do anything
  262. }
  263. if (!trace_ray(position + light_dir * params.bias, light_pos)) {
  264. vec3 light = lights.data[i].color * lights.data[i].energy * attenuation;
  265. if (lights.data[i].static_bake) {
  266. static_light += light;
  267. #ifdef USE_SH_LIGHTMAPS
  268. float c[4] = float[](
  269. 0.282095, //l0
  270. 0.488603 * light_dir.y, //l1n1
  271. 0.488603 * light_dir.z, //l1n0
  272. 0.488603 * light_dir.x //l1p1
  273. );
  274. for (uint j = 0; j < 4; j++) {
  275. sh_accum[j].rgb += light * c[j] * (1.0 / 3.0);
  276. }
  277. #endif
  278. } else {
  279. dynamic_light += light;
  280. }
  281. }
  282. }
  283. vec3 albedo = texelFetch(sampler2DArray(albedo_tex, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0).rgb;
  284. vec3 emissive = texelFetch(sampler2DArray(emission_tex, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0).rgb;
  285. dynamic_light *= albedo; //if it will bounce, must multiply by albedo
  286. dynamic_light += emissive;
  287. //keep for lightprobes
  288. imageStore(primary_dynamic, ivec3(atlas_pos, params.atlas_slice), vec4(dynamic_light, 1.0));
  289. dynamic_light += static_light * albedo; //send for bounces
  290. imageStore(dest_light, ivec3(atlas_pos, params.atlas_slice), vec4(dynamic_light, 1.0));
  291. #ifdef USE_SH_LIGHTMAPS
  292. //keep for adding at the end
  293. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice * 4 + 0), sh_accum[0]);
  294. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice * 4 + 1), sh_accum[1]);
  295. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice * 4 + 2), sh_accum[2]);
  296. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice * 4 + 3), sh_accum[3]);
  297. #else
  298. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice), vec4(static_light, 1.0));
  299. #endif
  300. #endif
  301. #ifdef MODE_BOUNCE_LIGHT
  302. vec3 normal = texelFetch(sampler2DArray(source_normal, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0).xyz;
  303. if (length(normal) < 0.5) {
  304. return; //empty texel, no process
  305. }
  306. vec3 position = texelFetch(sampler2DArray(source_position, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0).xyz;
  307. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  308. vec3 tangent = normalize(cross(v0, normal));
  309. vec3 bitangent = normalize(cross(tangent, normal));
  310. mat3 normal_mat = mat3(tangent, bitangent, normal);
  311. #ifdef USE_SH_LIGHTMAPS
  312. vec4 sh_accum[4] = vec4[](
  313. vec4(0.0, 0.0, 0.0, 1.0),
  314. vec4(0.0, 0.0, 0.0, 1.0),
  315. vec4(0.0, 0.0, 0.0, 1.0),
  316. vec4(0.0, 0.0, 0.0, 1.0));
  317. #endif
  318. vec3 light_average = vec3(0.0);
  319. for (uint i = params.ray_from; i < params.ray_to; i++) {
  320. vec3 ray_dir = normal_mat * vogel_hemisphere(i, params.ray_count, quick_hash(vec2(atlas_pos)));
  321. uint tidx;
  322. vec3 barycentric;
  323. vec3 light;
  324. if (trace_ray(position + ray_dir * params.bias, position + ray_dir * length(params.world_size), tidx, barycentric)) {
  325. //hit a triangle
  326. vec2 uv0 = vertices.data[triangles.data[tidx].indices.x].uv;
  327. vec2 uv1 = vertices.data[triangles.data[tidx].indices.y].uv;
  328. vec2 uv2 = vertices.data[triangles.data[tidx].indices.z].uv;
  329. vec3 uvw = vec3(barycentric.x * uv0 + barycentric.y * uv1 + barycentric.z * uv2, float(triangles.data[tidx].slice));
  330. light = textureLod(sampler2DArray(source_light, linear_sampler), uvw, 0.0).rgb;
  331. } else {
  332. //did not hit a triangle, reach out for the sky
  333. vec3 sky_dir = normalize(mat3(params.env_transform) * ray_dir);
  334. vec2 st = vec2(
  335. atan(sky_dir.x, sky_dir.z),
  336. acos(sky_dir.y));
  337. if (st.x < 0.0)
  338. st.x += PI * 2.0;
  339. st /= vec2(PI * 2.0, PI);
  340. light = textureLod(sampler2D(environment, linear_sampler), st, 0.0).rgb;
  341. }
  342. light_average += light;
  343. #ifdef USE_SH_LIGHTMAPS
  344. float c[4] = float[](
  345. 0.282095, //l0
  346. 0.488603 * ray_dir.y, //l1n1
  347. 0.488603 * ray_dir.z, //l1n0
  348. 0.488603 * ray_dir.x //l1p1
  349. );
  350. for (uint j = 0; j < 4; j++) {
  351. sh_accum[j].rgb += light * c[j] * (8.0 / float(params.ray_count));
  352. }
  353. #endif
  354. }
  355. vec3 light_total;
  356. if (params.ray_from == 0) {
  357. light_total = vec3(0.0);
  358. } else {
  359. light_total = imageLoad(bounce_accum, ivec3(atlas_pos, params.atlas_slice)).rgb;
  360. }
  361. light_total += light_average;
  362. #ifdef USE_SH_LIGHTMAPS
  363. for (int i = 0; i < 4; i++) {
  364. vec4 accum = imageLoad(accum_light, ivec3(atlas_pos, params.atlas_slice * 4 + i));
  365. accum.rgb += sh_accum[i].rgb;
  366. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice * 4 + i), accum);
  367. }
  368. #endif
  369. if (params.ray_to == params.ray_count) {
  370. light_total /= float(params.ray_count);
  371. imageStore(dest_light, ivec3(atlas_pos, params.atlas_slice), vec4(light_total, 1.0));
  372. #ifndef USE_SH_LIGHTMAPS
  373. vec4 accum = imageLoad(accum_light, ivec3(atlas_pos, params.atlas_slice));
  374. accum.rgb += light_total;
  375. imageStore(accum_light, ivec3(atlas_pos, params.atlas_slice), accum);
  376. #endif
  377. } else {
  378. imageStore(bounce_accum, ivec3(atlas_pos, params.atlas_slice), vec4(light_total, 1.0));
  379. }
  380. #endif
  381. #ifdef MODE_UNOCCLUDE
  382. //texel_size = 0.5;
  383. //compute tangents
  384. vec4 position_alpha = imageLoad(position, ivec3(atlas_pos, params.atlas_slice));
  385. if (position_alpha.a < 0.5) {
  386. return;
  387. }
  388. vec3 vertex_pos = position_alpha.xyz;
  389. vec4 normal_tsize = imageLoad(unocclude, ivec3(atlas_pos, params.atlas_slice));
  390. vec3 face_normal = normal_tsize.xyz;
  391. float texel_size = normal_tsize.w;
  392. vec3 v0 = abs(face_normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  393. vec3 tangent = normalize(cross(v0, face_normal));
  394. vec3 bitangent = normalize(cross(tangent, face_normal));
  395. vec3 base_pos = vertex_pos + face_normal * params.bias; //raise a bit
  396. vec3 rays[4] = vec3[](tangent, bitangent, -tangent, -bitangent);
  397. float min_d = 1e20;
  398. for (int i = 0; i < 4; i++) {
  399. vec3 ray_to = base_pos + rays[i] * texel_size;
  400. float d;
  401. vec3 norm;
  402. if (trace_ray(base_pos, ray_to, d, norm)) {
  403. if (d < min_d) {
  404. vertex_pos = base_pos + rays[i] * d + norm * params.bias * 10.0; //this bias needs to be greater than the regular bias, because otherwise later, rays will go the other side when pointing back.
  405. min_d = d;
  406. }
  407. }
  408. }
  409. position_alpha.xyz = vertex_pos;
  410. imageStore(position, ivec3(atlas_pos, params.atlas_slice), position_alpha);
  411. #endif
  412. #ifdef MODE_LIGHT_PROBES
  413. vec3 position = probe_positions.data[probe_index].xyz;
  414. vec4 probe_sh_accum[9] = vec4[](
  415. vec4(0.0),
  416. vec4(0.0),
  417. vec4(0.0),
  418. vec4(0.0),
  419. vec4(0.0),
  420. vec4(0.0),
  421. vec4(0.0),
  422. vec4(0.0),
  423. vec4(0.0));
  424. for (uint i = params.ray_from; i < params.ray_to; i++) {
  425. vec3 ray_dir = vogel_hemisphere(i, params.ray_count, quick_hash(vec2(float(probe_index), 0.0)));
  426. if (bool(i & 1)) {
  427. //throw to both sides, so alternate them
  428. ray_dir.z *= -1.0;
  429. }
  430. uint tidx;
  431. vec3 barycentric;
  432. vec3 light;
  433. if (trace_ray(position + ray_dir * params.bias, position + ray_dir * length(params.world_size), tidx, barycentric)) {
  434. vec2 uv0 = vertices.data[triangles.data[tidx].indices.x].uv;
  435. vec2 uv1 = vertices.data[triangles.data[tidx].indices.y].uv;
  436. vec2 uv2 = vertices.data[triangles.data[tidx].indices.z].uv;
  437. vec3 uvw = vec3(barycentric.x * uv0 + barycentric.y * uv1 + barycentric.z * uv2, float(triangles.data[tidx].slice));
  438. light = textureLod(sampler2DArray(source_light, linear_sampler), uvw, 0.0).rgb;
  439. light += textureLod(sampler2DArray(source_direct_light, linear_sampler), uvw, 0.0).rgb;
  440. } else {
  441. //did not hit a triangle, reach out for the sky
  442. vec3 sky_dir = normalize(mat3(params.env_transform) * ray_dir);
  443. vec2 st = vec2(
  444. atan(sky_dir.x, sky_dir.z),
  445. acos(sky_dir.y));
  446. if (st.x < 0.0)
  447. st.x += PI * 2.0;
  448. st /= vec2(PI * 2.0, PI);
  449. light = textureLod(sampler2D(environment, linear_sampler), st, 0.0).rgb;
  450. }
  451. {
  452. float c[9] = float[](
  453. 0.282095, //l0
  454. 0.488603 * ray_dir.y, //l1n1
  455. 0.488603 * ray_dir.z, //l1n0
  456. 0.488603 * ray_dir.x, //l1p1
  457. 1.092548 * ray_dir.x * ray_dir.y, //l2n2
  458. 1.092548 * ray_dir.y * ray_dir.z, //l2n1
  459. //0.315392 * (ray_dir.x * ray_dir.x + ray_dir.y * ray_dir.y + 2.0 * ray_dir.z * ray_dir.z), //l20
  460. 0.315392 * (3.0 * ray_dir.z * ray_dir.z - 1.0), //l20
  461. 1.092548 * ray_dir.x * ray_dir.z, //l2p1
  462. 0.546274 * (ray_dir.x * ray_dir.x - ray_dir.y * ray_dir.y) //l2p2
  463. );
  464. for (uint j = 0; j < 9; j++) {
  465. probe_sh_accum[j].rgb += light * c[j];
  466. }
  467. }
  468. }
  469. if (params.ray_from > 0) {
  470. for (uint j = 0; j < 9; j++) { //accum from existing
  471. probe_sh_accum[j] += light_probes.data[probe_index * 9 + j];
  472. }
  473. }
  474. if (params.ray_to == params.ray_count) {
  475. for (uint j = 0; j < 9; j++) { //accum from existing
  476. probe_sh_accum[j] *= 4.0 / float(params.ray_count);
  477. }
  478. }
  479. for (uint j = 0; j < 9; j++) { //accum from existing
  480. light_probes.data[probe_index * 9 + j] = probe_sh_accum[j];
  481. }
  482. #endif
  483. #ifdef MODE_DILATE
  484. vec4 c = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos, params.atlas_slice), 0);
  485. //sides first, as they are closer
  486. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-1, 0), params.atlas_slice), 0);
  487. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(0, 1), params.atlas_slice), 0);
  488. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(1, 0), params.atlas_slice), 0);
  489. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(0, -1), params.atlas_slice), 0);
  490. //endpoints second
  491. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-1, -1), params.atlas_slice), 0);
  492. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-1, 1), params.atlas_slice), 0);
  493. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(1, -1), params.atlas_slice), 0);
  494. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(1, 1), params.atlas_slice), 0);
  495. //far sides third
  496. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-2, 0), params.atlas_slice), 0);
  497. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(0, 2), params.atlas_slice), 0);
  498. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(2, 0), params.atlas_slice), 0);
  499. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(0, -2), params.atlas_slice), 0);
  500. //far-mid endpoints
  501. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-2, -1), params.atlas_slice), 0);
  502. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-2, 1), params.atlas_slice), 0);
  503. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(2, -1), params.atlas_slice), 0);
  504. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(2, 1), params.atlas_slice), 0);
  505. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-1, -2), params.atlas_slice), 0);
  506. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-1, 2), params.atlas_slice), 0);
  507. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(1, -2), params.atlas_slice), 0);
  508. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(1, 2), params.atlas_slice), 0);
  509. //far endpoints
  510. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-2, -2), params.atlas_slice), 0);
  511. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(-2, 2), params.atlas_slice), 0);
  512. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(2, -2), params.atlas_slice), 0);
  513. c = c.a > 0.5 ? c : texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos + ivec2(2, 2), params.atlas_slice), 0);
  514. imageStore(dest_light, ivec3(atlas_pos, params.atlas_slice), c);
  515. #endif
  516. }