2
0

lm_compute.glsl 22 KB

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