2
0

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