voxel_gi.glsl 16 KB

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