giprobe.glsl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #[compute]
  2. #version 450
  3. VERSION_DEFINES
  4. #ifdef MODE_DYNAMIC
  5. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  6. #else
  7. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  8. #endif
  9. #ifndef MODE_DYNAMIC
  10. #define NO_CHILDREN 0xFFFFFFFF
  11. #define GREY_VEC vec3(0.33333, 0.33333, 0.33333)
  12. struct CellChildren {
  13. uint children[8];
  14. };
  15. layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
  16. CellChildren data[];
  17. }
  18. cell_children;
  19. struct CellData {
  20. uint position; // xyz 10 bits
  21. uint albedo; //rgb albedo
  22. uint emission; //rgb normalized with e as multiplier
  23. uint normal; //RGB normal encoded
  24. };
  25. layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
  26. CellData data[];
  27. }
  28. cell_data;
  29. #endif // MODE DYNAMIC
  30. #define LIGHT_TYPE_DIRECTIONAL 0
  31. #define LIGHT_TYPE_OMNI 1
  32. #define LIGHT_TYPE_SPOT 2
  33. #if defined(MODE_COMPUTE_LIGHT) || defined(MODE_DYNAMIC_LIGHTING)
  34. struct Light {
  35. uint type;
  36. float energy;
  37. float radius;
  38. float attenuation;
  39. vec3 color;
  40. float cos_spot_angle;
  41. vec3 position;
  42. float inv_spot_attenuation;
  43. vec3 direction;
  44. bool has_shadow;
  45. };
  46. layout(set = 0, binding = 3, std140) uniform Lights {
  47. Light data[MAX_LIGHTS];
  48. }
  49. lights;
  50. #endif // MODE COMPUTE LIGHT
  51. #ifdef MODE_SECOND_BOUNCE
  52. layout(set = 0, binding = 5) uniform texture3D color_texture;
  53. #ifdef MODE_ANISOTROPIC
  54. layout(set = 0, binding = 7) uniform texture3D aniso_pos_texture;
  55. layout(set = 0, binding = 8) uniform texture3D aniso_neg_texture;
  56. #endif // MODE ANISOTROPIC
  57. #endif // MODE_SECOND_BOUNCE
  58. #ifndef MODE_DYNAMIC
  59. layout(push_constant, binding = 0, std430) uniform Params {
  60. ivec3 limits;
  61. uint stack_size;
  62. float emission_scale;
  63. float propagation;
  64. float dynamic_range;
  65. uint light_count;
  66. uint cell_offset;
  67. uint cell_count;
  68. float aniso_strength;
  69. uint pad;
  70. }
  71. params;
  72. layout(set = 0, binding = 4, std430) buffer Outputs {
  73. vec4 data[];
  74. }
  75. outputs;
  76. #endif // MODE DYNAMIC
  77. layout(set = 0, binding = 9) uniform texture3D texture_sdf;
  78. layout(set = 0, binding = 10) uniform sampler texture_sampler;
  79. #ifdef MODE_WRITE_TEXTURE
  80. layout(rgba8, set = 0, binding = 5) uniform restrict writeonly image3D color_tex;
  81. #ifdef MODE_ANISOTROPIC
  82. layout(r16ui, set = 0, binding = 6) uniform restrict writeonly uimage3D aniso_pos_tex;
  83. layout(r16ui, set = 0, binding = 7) uniform restrict writeonly uimage3D aniso_neg_tex;
  84. #endif
  85. #endif
  86. #ifdef MODE_DYNAMIC
  87. layout(push_constant, binding = 0, std430) uniform Params {
  88. ivec3 limits;
  89. uint light_count; //when not lighting
  90. ivec3 x_dir;
  91. float z_base;
  92. ivec3 y_dir;
  93. float z_sign;
  94. ivec3 z_dir;
  95. float pos_multiplier;
  96. ivec2 rect_pos;
  97. ivec2 rect_size;
  98. ivec2 prev_rect_ofs;
  99. ivec2 prev_rect_size;
  100. bool flip_x;
  101. bool flip_y;
  102. float dynamic_range;
  103. bool on_mipmap;
  104. float propagation;
  105. float pad[3];
  106. }
  107. params;
  108. #ifdef MODE_DYNAMIC_LIGHTING
  109. layout(rgba8, set = 0, binding = 5) uniform restrict readonly image2D source_albedo;
  110. layout(rgba8, set = 0, binding = 6) uniform restrict readonly image2D source_normal;
  111. layout(rgba8, set = 0, binding = 7) uniform restrict readonly image2D source_orm;
  112. //layout (set=0,binding=8) uniform texture2D source_depth;
  113. layout(rgba16f, set = 0, binding = 11) uniform restrict image2D emission;
  114. layout(r32f, set = 0, binding = 12) uniform restrict image2D depth;
  115. #endif
  116. #ifdef MODE_DYNAMIC_SHRINK
  117. layout(rgba16f, set = 0, binding = 5) uniform restrict readonly image2D source_light;
  118. layout(r32f, set = 0, binding = 6) uniform restrict readonly image2D source_depth;
  119. #ifdef MODE_DYNAMIC_SHRINK_WRITE
  120. layout(rgba16f, set = 0, binding = 7) uniform restrict writeonly image2D light;
  121. layout(r32f, set = 0, binding = 8) uniform restrict writeonly image2D depth;
  122. #endif // MODE_DYNAMIC_SHRINK_WRITE
  123. #ifdef MODE_DYNAMIC_SHRINK_PLOT
  124. layout(rgba8, set = 0, binding = 11) uniform restrict image3D color_texture;
  125. #ifdef MODE_ANISOTROPIC
  126. layout(r16ui, set = 0, binding = 12) uniform restrict writeonly uimage3D aniso_pos_texture;
  127. layout(r16ui, set = 0, binding = 13) uniform restrict writeonly uimage3D aniso_neg_texture;
  128. #endif // MODE ANISOTROPIC
  129. #endif //MODE_DYNAMIC_SHRINK_PLOT
  130. #endif // MODE_DYNAMIC_SHRINK
  131. //layout (rgba8,set=0,binding=5) uniform restrict writeonly image3D color_tex;
  132. #endif // MODE DYNAMIC
  133. #if defined(MODE_COMPUTE_LIGHT) || defined(MODE_DYNAMIC_LIGHTING)
  134. float raymarch(float distance, float distance_adv, vec3 from, vec3 direction) {
  135. vec3 cell_size = 1.0 / vec3(params.limits);
  136. float occlusion = 1.0;
  137. while (distance > 0.5) { //use this to avoid precision errors
  138. float advance = texture(sampler3D(texture_sdf, texture_sampler), from * cell_size).r * 255.0 - 1.0;
  139. if (advance < 0.0) {
  140. occlusion = 0.0;
  141. break;
  142. }
  143. occlusion = min(advance, occlusion);
  144. advance = max(distance_adv, advance - mod(advance, distance_adv)); //should always advance in multiples of distance_adv
  145. from += direction * advance;
  146. distance -= advance;
  147. }
  148. return occlusion; //max(0.0,distance);
  149. }
  150. float get_omni_attenuation(float distance, float inv_range, float decay) {
  151. float nd = distance * inv_range;
  152. nd *= nd;
  153. nd *= nd; // nd^4
  154. nd = max(1.0 - nd, 0.0);
  155. nd *= nd; // nd^2
  156. return nd * pow(max(distance, 0.0001), -decay);
  157. }
  158. bool compute_light_vector(uint light, vec3 pos, out float attenuation, out vec3 light_pos) {
  159. if (lights.data[light].type == LIGHT_TYPE_DIRECTIONAL) {
  160. light_pos = pos - lights.data[light].direction * length(vec3(params.limits));
  161. attenuation = 1.0;
  162. } else {
  163. light_pos = lights.data[light].position;
  164. float distance = length(pos - light_pos);
  165. if (distance >= lights.data[light].radius) {
  166. return false;
  167. }
  168. attenuation = get_omni_attenuation(distance, 1.0 / lights.data[light].radius, lights.data[light].attenuation);
  169. if (lights.data[light].type == LIGHT_TYPE_SPOT) {
  170. vec3 rel = normalize(pos - light_pos);
  171. float cos_spot_angle = lights.data[light].cos_spot_angle;
  172. float cos_angle = dot(rel, lights.data[light].direction);
  173. if (cos_angle < cos_spot_angle) {
  174. return false;
  175. }
  176. float scos = max(cos_angle, cos_spot_angle);
  177. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
  178. attenuation *= 1.0 - pow(spot_rim, lights.data[light].inv_spot_attenuation);
  179. }
  180. }
  181. return true;
  182. }
  183. float get_normal_advance(vec3 p_normal) {
  184. vec3 normal = p_normal;
  185. vec3 unorm = abs(normal);
  186. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  187. // x code
  188. unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
  189. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  190. // y code
  191. unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
  192. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  193. // z code
  194. unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
  195. } else {
  196. // oh-no we messed up code
  197. // has to be
  198. unorm = vec3(1.0, 0.0, 0.0);
  199. }
  200. return 1.0 / dot(normal, unorm);
  201. }
  202. void clip_segment(vec4 plane, vec3 begin, inout vec3 end) {
  203. vec3 segment = begin - end;
  204. float den = dot(plane.xyz, segment);
  205. //printf("den is %i\n",den);
  206. if (den < 0.0001) {
  207. return;
  208. }
  209. float dist = (dot(plane.xyz, begin) - plane.w) / den;
  210. if (dist < 0.0001 || dist > 1.0001) {
  211. return;
  212. }
  213. end = begin + segment * -dist;
  214. }
  215. bool compute_light_at_pos(uint index, vec3 pos, vec3 normal, inout vec3 light, inout vec3 light_dir) {
  216. float attenuation;
  217. vec3 light_pos;
  218. if (!compute_light_vector(index, pos, attenuation, light_pos)) {
  219. return false;
  220. }
  221. light_dir = normalize(pos - light_pos);
  222. if (attenuation < 0.01 || (length(normal) > 0.2 && dot(normal, light_dir) >= 0)) {
  223. return false; //not facing the light, or attenuation is near zero
  224. }
  225. if (lights.data[index].has_shadow) {
  226. float distance_adv = get_normal_advance(light_dir);
  227. vec3 to = pos;
  228. if (length(normal) > 0.2) {
  229. to += normal * distance_adv * 0.51;
  230. } else {
  231. to -= sign(light_dir) * 0.45; //go near the edge towards the light direction to avoid self occlusion
  232. }
  233. //clip
  234. clip_segment(mix(vec4(-1.0, 0.0, 0.0, 0.0), vec4(1.0, 0.0, 0.0, float(params.limits.x - 1)), bvec4(light_dir.x < 0.0)), to, light_pos);
  235. clip_segment(mix(vec4(0.0, -1.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, float(params.limits.y - 1)), bvec4(light_dir.y < 0.0)), to, light_pos);
  236. clip_segment(mix(vec4(0.0, 0.0, -1.0, 0.0), vec4(0.0, 0.0, 1.0, float(params.limits.z - 1)), bvec4(light_dir.z < 0.0)), to, light_pos);
  237. float distance = length(to - light_pos);
  238. if (distance < 0.1) {
  239. return false; // hit
  240. }
  241. distance += distance_adv - mod(distance, distance_adv); //make it reach the center of the box always
  242. light_pos = to - light_dir * distance;
  243. //from -= sign(light_dir)*0.45; //go near the edge towards the light direction to avoid self occlusion
  244. /*float dist = raymarch(distance,distance_adv,light_pos,light_dir);
  245. if (dist > distance_adv) {
  246. return false;
  247. }
  248. attenuation *= 1.0 - smoothstep(0.1*distance_adv,distance_adv,dist);
  249. */
  250. float occlusion = raymarch(distance, distance_adv, light_pos, light_dir);
  251. if (occlusion == 0.0) {
  252. return false;
  253. }
  254. attenuation *= occlusion; //1.0 - smoothstep(0.1*distance_adv,distance_adv,dist);
  255. }
  256. light = lights.data[index].color * attenuation * lights.data[index].energy;
  257. return true;
  258. }
  259. #endif // MODE COMPUTE LIGHT
  260. void main() {
  261. #ifndef MODE_DYNAMIC
  262. uint cell_index = gl_GlobalInvocationID.x;
  263. if (cell_index >= params.cell_count) {
  264. return;
  265. }
  266. cell_index += params.cell_offset;
  267. uvec3 posu = uvec3(cell_data.data[cell_index].position & 0x7FF, (cell_data.data[cell_index].position >> 11) & 0x3FF, cell_data.data[cell_index].position >> 21);
  268. vec4 albedo = unpackUnorm4x8(cell_data.data[cell_index].albedo);
  269. #endif
  270. /////////////////COMPUTE LIGHT///////////////////////////////
  271. #ifdef MODE_COMPUTE_LIGHT
  272. vec3 pos = vec3(posu) + vec3(0.5);
  273. vec3 emission = vec3(uvec3(cell_data.data[cell_index].emission & 0x1ff, (cell_data.data[cell_index].emission >> 9) & 0x1ff, (cell_data.data[cell_index].emission >> 18) & 0x1ff)) * pow(2.0, float(cell_data.data[cell_index].emission >> 27) - 15.0 - 9.0);
  274. vec3 normal = unpackSnorm4x8(cell_data.data[cell_index].normal).xyz;
  275. #ifdef MODE_ANISOTROPIC
  276. vec3 accum[6] = vec3[](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
  277. const vec3 accum_dirs[6] = vec3[](vec3(1.0, 0.0, 0.0), vec3(-1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, -1.0, 0.0), vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0));
  278. #else
  279. vec3 accum = vec3(0.0);
  280. #endif
  281. for (uint i = 0; i < params.light_count; i++) {
  282. vec3 light;
  283. vec3 light_dir;
  284. if (!compute_light_at_pos(i, pos, normal.xyz, light, light_dir)) {
  285. continue;
  286. }
  287. light *= albedo.rgb;
  288. #ifdef MODE_ANISOTROPIC
  289. for (uint j = 0; j < 6; j++) {
  290. accum[j] += max(0.0, dot(accum_dirs[j], -light_dir)) * light;
  291. }
  292. #else
  293. if (length(normal) > 0.2) {
  294. accum += max(0.0, dot(normal, -light_dir)) * light;
  295. } else {
  296. //all directions
  297. accum += light;
  298. }
  299. #endif
  300. }
  301. #ifdef MODE_ANISOTROPIC
  302. for (uint i = 0; i < 6; i++) {
  303. vec3 light = accum[i];
  304. if (length(normal) > 0.2) {
  305. light += max(0.0, dot(accum_dirs[i], -normal)) * emission;
  306. } else {
  307. light += emission;
  308. }
  309. outputs.data[cell_index * 6 + i] = vec4(light, 0.0);
  310. }
  311. #else
  312. outputs.data[cell_index] = vec4(accum + emission, 0.0);
  313. #endif
  314. #endif //MODE_COMPUTE_LIGHT
  315. /////////////////SECOND BOUNCE///////////////////////////////
  316. #ifdef MODE_SECOND_BOUNCE
  317. vec3 pos = vec3(posu) + vec3(0.5);
  318. ivec3 ipos = ivec3(posu);
  319. vec4 normal = unpackSnorm4x8(cell_data.data[cell_index].normal);
  320. #ifdef MODE_ANISOTROPIC
  321. vec3 accum[6];
  322. const vec3 accum_dirs[6] = vec3[](vec3(1.0, 0.0, 0.0), vec3(-1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, -1.0, 0.0), vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0));
  323. /*vec3 src_color = texelFetch(sampler3D(color_texture,texture_sampler),ipos,0).rgb * params.dynamic_range;
  324. vec3 src_aniso_pos = texelFetch(sampler3D(aniso_pos_texture,texture_sampler),ipos,0).rgb;
  325. vec3 src_anisp_neg = texelFetch(sampler3D(anisp_neg_texture,texture_sampler),ipos,0).rgb;
  326. accum[0]=src_col * src_aniso_pos.x;
  327. accum[1]=src_col * src_aniso_neg.x;
  328. accum[2]=src_col * src_aniso_pos.y;
  329. accum[3]=src_col * src_aniso_neg.y;
  330. accum[4]=src_col * src_aniso_pos.z;
  331. accum[5]=src_col * src_aniso_neg.z;*/
  332. accum[0] = outputs.data[cell_index * 6 + 0].rgb;
  333. accum[1] = outputs.data[cell_index * 6 + 1].rgb;
  334. accum[2] = outputs.data[cell_index * 6 + 2].rgb;
  335. accum[3] = outputs.data[cell_index * 6 + 3].rgb;
  336. accum[4] = outputs.data[cell_index * 6 + 4].rgb;
  337. accum[5] = outputs.data[cell_index * 6 + 5].rgb;
  338. #else
  339. vec3 accum = outputs.data[cell_index].rgb;
  340. #endif
  341. if (length(normal.xyz) > 0.2) {
  342. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  343. vec3 tangent = normalize(cross(v0, normal.xyz));
  344. vec3 bitangent = normalize(cross(tangent, normal.xyz));
  345. mat3 normal_mat = mat3(tangent, bitangent, normal.xyz);
  346. #define MAX_CONE_DIRS 6
  347. vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
  348. vec3(0.0, 0.0, 1.0),
  349. vec3(0.866025, 0.0, 0.5),
  350. vec3(0.267617, 0.823639, 0.5),
  351. vec3(-0.700629, 0.509037, 0.5),
  352. vec3(-0.700629, -0.509037, 0.5),
  353. vec3(0.267617, -0.823639, 0.5));
  354. float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15);
  355. float tan_half_angle = 0.577;
  356. for (int i = 0; i < MAX_CONE_DIRS; i++) {
  357. vec3 direction = normal_mat * cone_dirs[i];
  358. vec4 color = vec4(0.0);
  359. {
  360. float dist = 1.5;
  361. float max_distance = length(vec3(params.limits));
  362. vec3 cell_size = 1.0 / vec3(params.limits);
  363. #ifdef MODE_ANISOTROPIC
  364. vec3 aniso_normal = mix(direction, normal.xyz, params.aniso_strength);
  365. #endif
  366. while (dist < max_distance && color.a < 0.95) {
  367. float diameter = max(1.0, 2.0 * tan_half_angle * dist);
  368. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  369. float half_diameter = diameter * 0.5;
  370. //check if outside, then break
  371. //if ( any(greaterThan(abs(uvw_pos - 0.5),vec3(0.5f + half_diameter * cell_size)) ) ) {
  372. // break;
  373. //}
  374. float log2_diameter = log2(diameter);
  375. vec4 scolor = textureLod(sampler3D(color_texture, texture_sampler), uvw_pos, log2_diameter);
  376. #ifdef MODE_ANISOTROPIC
  377. vec3 aniso_neg = textureLod(sampler3D(aniso_neg_texture, texture_sampler), uvw_pos, log2_diameter).rgb;
  378. vec3 aniso_pos = textureLod(sampler3D(aniso_pos_texture, texture_sampler), uvw_pos, log2_diameter).rgb;
  379. scolor.rgb *= dot(max(vec3(0.0), (aniso_normal * aniso_pos)), vec3(1.0)) + dot(max(vec3(0.0), (-aniso_normal * aniso_neg)), vec3(1.0));
  380. #endif
  381. float a = (1.0 - color.a);
  382. color += a * scolor;
  383. dist += half_diameter;
  384. }
  385. }
  386. color *= cone_weights[i] * vec4(albedo.rgb, 1.0) * params.dynamic_range; //restore range
  387. #ifdef MODE_ANISOTROPIC
  388. for (uint j = 0; j < 6; j++) {
  389. accum[j] += max(0.0, dot(accum_dirs[j], direction)) * color.rgb;
  390. }
  391. #else
  392. accum += color.rgb;
  393. #endif
  394. }
  395. }
  396. #ifdef MODE_ANISOTROPIC
  397. outputs.data[cell_index * 6 + 0] = vec4(accum[0], 0.0);
  398. outputs.data[cell_index * 6 + 1] = vec4(accum[1], 0.0);
  399. outputs.data[cell_index * 6 + 2] = vec4(accum[2], 0.0);
  400. outputs.data[cell_index * 6 + 3] = vec4(accum[3], 0.0);
  401. outputs.data[cell_index * 6 + 4] = vec4(accum[4], 0.0);
  402. outputs.data[cell_index * 6 + 5] = vec4(accum[5], 0.0);
  403. #else
  404. outputs.data[cell_index] = vec4(accum, 0.0);
  405. #endif
  406. #endif // MODE_SECOND_BOUNCE
  407. /////////////////UPDATE MIPMAPS///////////////////////////////
  408. #ifdef MODE_UPDATE_MIPMAPS
  409. {
  410. #ifdef MODE_ANISOTROPIC
  411. vec3 light_accum[6] = vec3[](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
  412. #else
  413. vec3 light_accum = vec3(0.0);
  414. #endif
  415. float count = 0.0;
  416. for (uint i = 0; i < 8; i++) {
  417. uint child_index = cell_children.data[cell_index].children[i];
  418. if (child_index == NO_CHILDREN) {
  419. continue;
  420. }
  421. #ifdef MODE_ANISOTROPIC
  422. light_accum[0] += outputs.data[child_index * 6 + 0].rgb;
  423. light_accum[1] += outputs.data[child_index * 6 + 1].rgb;
  424. light_accum[2] += outputs.data[child_index * 6 + 2].rgb;
  425. light_accum[3] += outputs.data[child_index * 6 + 3].rgb;
  426. light_accum[4] += outputs.data[child_index * 6 + 4].rgb;
  427. light_accum[5] += outputs.data[child_index * 6 + 5].rgb;
  428. #else
  429. light_accum += outputs.data[child_index].rgb;
  430. #endif
  431. count += 1.0;
  432. }
  433. float divisor = mix(8.0, count, params.propagation);
  434. #ifdef MODE_ANISOTROPIC
  435. outputs.data[cell_index * 6 + 0] = vec4(light_accum[0] / divisor, 0.0);
  436. outputs.data[cell_index * 6 + 1] = vec4(light_accum[1] / divisor, 0.0);
  437. outputs.data[cell_index * 6 + 2] = vec4(light_accum[2] / divisor, 0.0);
  438. outputs.data[cell_index * 6 + 3] = vec4(light_accum[3] / divisor, 0.0);
  439. outputs.data[cell_index * 6 + 4] = vec4(light_accum[4] / divisor, 0.0);
  440. outputs.data[cell_index * 6 + 5] = vec4(light_accum[5] / divisor, 0.0);
  441. #else
  442. outputs.data[cell_index] = vec4(light_accum / divisor, 0.0);
  443. #endif
  444. }
  445. #endif
  446. ///////////////////WRITE TEXTURE/////////////////////////////
  447. #ifdef MODE_WRITE_TEXTURE
  448. {
  449. #ifdef MODE_ANISOTROPIC
  450. vec3 accum_total = vec3(0.0);
  451. accum_total += outputs.data[cell_index * 6 + 0].rgb;
  452. accum_total += outputs.data[cell_index * 6 + 1].rgb;
  453. accum_total += outputs.data[cell_index * 6 + 2].rgb;
  454. accum_total += outputs.data[cell_index * 6 + 3].rgb;
  455. accum_total += outputs.data[cell_index * 6 + 4].rgb;
  456. accum_total += outputs.data[cell_index * 6 + 5].rgb;
  457. float accum_total_energy = max(dot(accum_total, GREY_VEC), 0.00001);
  458. vec3 iso_positive = vec3(dot(outputs.data[cell_index * 6 + 0].rgb, GREY_VEC), dot(outputs.data[cell_index * 6 + 2].rgb, GREY_VEC), dot(outputs.data[cell_index * 6 + 4].rgb, GREY_VEC)) / vec3(accum_total_energy);
  459. vec3 iso_negative = vec3(dot(outputs.data[cell_index * 6 + 1].rgb, GREY_VEC), dot(outputs.data[cell_index * 6 + 3].rgb, GREY_VEC), dot(outputs.data[cell_index * 6 + 5].rgb, GREY_VEC)) / vec3(accum_total_energy);
  460. {
  461. uint aniso_pos = uint(clamp(iso_positive.b * 31.0, 0.0, 31.0));
  462. aniso_pos |= uint(clamp(iso_positive.g * 63.0, 0.0, 63.0)) << 5;
  463. aniso_pos |= uint(clamp(iso_positive.r * 31.0, 0.0, 31.0)) << 11;
  464. imageStore(aniso_pos_tex, ivec3(posu), uvec4(aniso_pos));
  465. }
  466. {
  467. uint aniso_neg = uint(clamp(iso_negative.b * 31.0, 0.0, 31.0));
  468. aniso_neg |= uint(clamp(iso_negative.g * 63.0, 0.0, 63.0)) << 5;
  469. aniso_neg |= uint(clamp(iso_negative.r * 31.0, 0.0, 31.0)) << 11;
  470. imageStore(aniso_neg_tex, ivec3(posu), uvec4(aniso_neg));
  471. }
  472. imageStore(color_tex, ivec3(posu), vec4(accum_total / params.dynamic_range, albedo.a));
  473. #else
  474. imageStore(color_tex, ivec3(posu), vec4(outputs.data[cell_index].rgb / params.dynamic_range, albedo.a));
  475. #endif
  476. }
  477. #endif
  478. ///////////////////DYNAMIC LIGHTING/////////////////////////////
  479. #ifdef MODE_DYNAMIC
  480. ivec2 pos_xy = ivec2(gl_GlobalInvocationID.xy);
  481. if (any(greaterThanEqual(pos_xy, params.rect_size))) {
  482. return; //out of bounds
  483. }
  484. ivec2 uv_xy = pos_xy;
  485. if (params.flip_x) {
  486. uv_xy.x = params.rect_size.x - pos_xy.x - 1;
  487. }
  488. if (params.flip_y) {
  489. uv_xy.y = params.rect_size.y - pos_xy.y - 1;
  490. }
  491. #ifdef MODE_DYNAMIC_LIGHTING
  492. {
  493. float z = params.z_base + imageLoad(depth, uv_xy).x * params.z_sign;
  494. ivec3 pos = params.x_dir * (params.rect_pos.x + pos_xy.x) + params.y_dir * (params.rect_pos.y + pos_xy.y) + abs(params.z_dir) * int(z);
  495. vec3 normal = imageLoad(source_normal, uv_xy).xyz * 2.0 - 1.0;
  496. normal = vec3(params.x_dir) * normal.x * mix(1.0, -1.0, params.flip_x) + vec3(params.y_dir) * normal.y * mix(1.0, -1.0, params.flip_y) - vec3(params.z_dir) * normal.z;
  497. vec4 albedo = imageLoad(source_albedo, uv_xy);
  498. //determine the position in space
  499. vec3 accum = vec3(0.0);
  500. for (uint i = 0; i < params.light_count; i++) {
  501. vec3 light;
  502. vec3 light_dir;
  503. if (!compute_light_at_pos(i, vec3(pos) * params.pos_multiplier, normal, light, light_dir)) {
  504. continue;
  505. }
  506. light *= albedo.rgb;
  507. accum += max(0.0, dot(normal, -light_dir)) * light;
  508. }
  509. accum += imageLoad(emission, uv_xy).xyz;
  510. imageStore(emission, uv_xy, vec4(accum, albedo.a));
  511. imageStore(depth, uv_xy, vec4(z));
  512. }
  513. #endif // MODE DYNAMIC LIGHTING
  514. #ifdef MODE_DYNAMIC_SHRINK
  515. {
  516. vec4 accum = vec4(0.0);
  517. float accum_z = 0.0;
  518. float count = 0.0;
  519. for (int i = 0; i < 4; i++) {
  520. ivec2 ofs = pos_xy * 2 + ivec2(i & 1, i >> 1) - params.prev_rect_ofs;
  521. if (any(lessThan(ofs, ivec2(0))) || any(greaterThanEqual(ofs, params.prev_rect_size))) {
  522. continue;
  523. }
  524. if (params.flip_x) {
  525. ofs.x = params.prev_rect_size.x - ofs.x - 1;
  526. }
  527. if (params.flip_y) {
  528. ofs.y = params.prev_rect_size.y - ofs.y - 1;
  529. }
  530. vec4 light = imageLoad(source_light, ofs);
  531. if (light.a == 0.0) { //ignore empty
  532. continue;
  533. }
  534. accum += light;
  535. float z = imageLoad(source_depth, ofs).x;
  536. accum_z += z * 0.5; //shrink half too
  537. count += 1.0;
  538. }
  539. if (params.on_mipmap) {
  540. accum.rgb /= mix(8.0, count, params.propagation);
  541. accum.a /= 8.0;
  542. } else {
  543. accum /= 4.0;
  544. }
  545. if (count == 0.0) {
  546. accum_z = 0.0; //avoid nan
  547. } else {
  548. accum_z /= count;
  549. }
  550. #ifdef MODE_DYNAMIC_SHRINK_WRITE
  551. imageStore(light, uv_xy, accum);
  552. imageStore(depth, uv_xy, vec4(accum_z));
  553. #endif
  554. #ifdef MODE_DYNAMIC_SHRINK_PLOT
  555. if (accum.a < 0.001) {
  556. return; //do not blit if alpha is too low
  557. }
  558. ivec3 pos = params.x_dir * (params.rect_pos.x + pos_xy.x) + params.y_dir * (params.rect_pos.y + pos_xy.y) + abs(params.z_dir) * int(accum_z);
  559. float z_frac = fract(accum_z);
  560. for (int i = 0; i < 2; i++) {
  561. ivec3 pos3d = pos + abs(params.z_dir) * i;
  562. if (any(lessThan(pos3d, ivec3(0))) || any(greaterThanEqual(pos3d, params.limits))) {
  563. //skip if offlimits
  564. continue;
  565. }
  566. vec4 color_blit = accum * (i == 0 ? 1.0 - z_frac : z_frac);
  567. vec4 color = imageLoad(color_texture, pos3d);
  568. color.rgb *= params.dynamic_range;
  569. #if 0
  570. color.rgb = mix(color.rgb,color_blit.rgb,color_blit.a);
  571. color.a+=color_blit.a;
  572. #else
  573. float sa = 1.0 - color_blit.a;
  574. vec4 result;
  575. result.a = color.a * sa + color_blit.a;
  576. if (result.a == 0.0) {
  577. result = vec4(0.0);
  578. } else {
  579. result.rgb = (color.rgb * color.a * sa + color_blit.rgb * color_blit.a) / result.a;
  580. color = result;
  581. }
  582. #endif
  583. color.rgb /= params.dynamic_range;
  584. imageStore(color_texture, pos3d, color);
  585. //imageStore(color_texture,pos3d,vec4(1,1,1,1));
  586. #ifdef MODE_ANISOTROPIC
  587. //do not care about anisotropy for dynamic objects, just store full lit in all directions
  588. imageStore(aniso_pos_texture, pos3d, uvec4(0xFFFF));
  589. imageStore(aniso_neg_texture, pos3d, uvec4(0xFFFF));
  590. #endif // ANISOTROPIC
  591. }
  592. #endif // MODE_DYNAMIC_SHRINK_PLOT
  593. }
  594. #endif
  595. #endif // MODE DYNAMIC
  596. }