voxel_gi_sdf.glsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
  5. #define MAX_DISTANCE 100000
  6. #define NO_CHILDREN 0xFFFFFFFF
  7. #define GREY_VEC vec3(0.33333, 0.33333, 0.33333)
  8. struct CellChildren {
  9. uint children[8];
  10. };
  11. layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
  12. CellChildren data[];
  13. }
  14. cell_children;
  15. struct CellData {
  16. uint position; // xyz 10 bits
  17. uint albedo; //rgb albedo
  18. uint emission; //rgb normalized with e as multiplier
  19. uint normal; //RGB normal encoded
  20. };
  21. layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
  22. CellData data[];
  23. }
  24. cell_data;
  25. layout(r8ui, set = 0, binding = 3) uniform restrict writeonly uimage3D sdf_tex;
  26. layout(push_constant, binding = 0, std430) uniform Params {
  27. uint offset;
  28. uint end;
  29. uint pad0;
  30. uint pad1;
  31. }
  32. params;
  33. void main() {
  34. vec3 pos = vec3(gl_GlobalInvocationID);
  35. float closest_dist = 100000.0;
  36. for (uint i = params.offset; i < params.end; i++) {
  37. vec3 posu = vec3(uvec3(cell_data.data[i].position & 0x7FF, (cell_data.data[i].position >> 11) & 0x3FF, cell_data.data[i].position >> 21));
  38. float dist = length(pos - posu);
  39. if (dist < closest_dist) {
  40. closest_dist = dist;
  41. }
  42. }
  43. uint dist_8;
  44. if (closest_dist < 0.0001) { // same cell
  45. dist_8 = 0; //equals to -1
  46. } else {
  47. dist_8 = clamp(uint(closest_dist), 0, 254) + 1; //conservative, 0 is 1, so <1 is considered solid
  48. }
  49. imageStore(sdf_tex, ivec3(gl_GlobalInvocationID), uvec4(dist_8));
  50. //imageStore(sdf_tex,pos,uvec4(pos*2,0));
  51. }
  52. #if 0
  53. layout(push_constant, binding = 0, std430) uniform Params {
  54. ivec3 limits;
  55. uint stack_size;
  56. }
  57. params;
  58. float distance_to_aabb(ivec3 pos, ivec3 aabb_pos, ivec3 aabb_size) {
  59. vec3 delta = vec3(max(ivec3(0), max(aabb_pos - pos, pos - (aabb_pos + aabb_size - ivec3(1)))));
  60. return length(delta);
  61. }
  62. void main() {
  63. ivec3 pos = ivec3(gl_GlobalInvocationID);
  64. uint stack[10] = uint[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  65. uint stack_indices[10] = uint[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  66. ivec3 stack_positions[10] = ivec3[](ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0));
  67. const uint cell_orders[8] = uint[](
  68. 0x11f58d1,
  69. 0xe2e70a,
  70. 0xd47463,
  71. 0xbb829c,
  72. 0x8d11f5,
  73. 0x70ae2e,
  74. 0x463d47,
  75. 0x29cbb8);
  76. bool cell_found = false;
  77. bool cell_found_exact = false;
  78. ivec3 closest_cell_pos;
  79. float closest_distance = MAX_DISTANCE;
  80. int stack_pos = 0;
  81. while (true) {
  82. uint index = stack_indices[stack_pos] >> 24;
  83. if (index == 8) {
  84. //go up
  85. if (stack_pos == 0) {
  86. break; //done going through octree
  87. }
  88. stack_pos--;
  89. continue;
  90. }
  91. stack_indices[stack_pos] = (stack_indices[stack_pos] & ((1 << 24) - 1)) | ((index + 1) << 24);
  92. uint cell_index = (stack_indices[stack_pos] >> (index * 3)) & 0x7;
  93. uint child_cell = cell_children.data[stack[stack_pos]].children[cell_index];
  94. if (child_cell == NO_CHILDREN) {
  95. continue;
  96. }
  97. ivec3 child_cell_size = params.limits >> (stack_pos + 1);
  98. ivec3 child_cell_pos = stack_positions[stack_pos];
  99. child_cell_pos += mix(ivec3(0), child_cell_size, bvec3(uvec3(index & 1, index & 2, index & 4) != uvec3(0)));
  100. bool is_leaf = stack_pos == (params.stack_size - 2);
  101. if (child_cell_pos == pos && is_leaf) {
  102. //we may actually end up in the exact cell.
  103. //if this happens, just abort
  104. cell_found_exact = true;
  105. break;
  106. }
  107. if (cell_found) {
  108. //discard by distance
  109. float distance = distance_to_aabb(pos, child_cell_pos, child_cell_size);
  110. if (distance >= closest_distance) {
  111. continue; //pointless, just test next child
  112. } else if (is_leaf) {
  113. //closer than what we have AND end of stack, save and continue
  114. closest_cell_pos = child_cell_pos;
  115. closest_distance = distance;
  116. continue;
  117. }
  118. } else if (is_leaf) {
  119. //first solid cell we find, save and continue
  120. closest_distance = distance_to_aabb(pos, child_cell_pos, child_cell_size);
  121. closest_cell_pos = child_cell_pos;
  122. cell_found = true;
  123. continue;
  124. }
  125. bvec3 direction = greaterThan((pos - (child_cell_pos + (child_cell_size >> 1))), ivec3(0));
  126. uint cell_order = 0;
  127. cell_order |= mix(0, 1, direction.x);
  128. cell_order |= mix(0, 2, direction.y);
  129. cell_order |= mix(0, 4, direction.z);
  130. stack[stack_pos + 1] = child_cell;
  131. stack_indices[stack_pos + 1] = cell_orders[cell_order]; //start counting
  132. stack_positions[stack_pos + 1] = child_cell_pos;
  133. stack_pos++; //go up stack
  134. }
  135. uint dist_8;
  136. if (cell_found_exact) {
  137. dist_8 = 0; //equals to -1
  138. } else {
  139. float closest_distance = length(vec3(pos - closest_cell_pos));
  140. dist_8 = clamp(uint(closest_distance), 0, 254) + 1; //conservative, 0 is 1, so <1 is considered solid
  141. }
  142. imageStore(sdf_tex, pos, uvec4(dist_8));
  143. }
  144. #endif