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