giprobe_write.glsl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* clang-format off */
  2. [compute]
  3. #version 450
  4. VERSION_DEFINES
  5. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  6. /* clang-format on */
  7. #define NO_CHILDREN 0xFFFFFFFF
  8. #define GREY_VEC vec3(0.33333, 0.33333, 0.33333)
  9. struct CellChildren {
  10. uint children[8];
  11. };
  12. layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
  13. CellChildren data[];
  14. }
  15. cell_children;
  16. struct CellData {
  17. uint position; // xyz 10 bits
  18. uint albedo; //rgb albedo
  19. uint emission; //rgb normalized with e as multiplier
  20. uint normal; //RGB normal encoded
  21. };
  22. layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
  23. CellData data[];
  24. }
  25. cell_data;
  26. #define LIGHT_TYPE_DIRECTIONAL 0
  27. #define LIGHT_TYPE_OMNI 1
  28. #define LIGHT_TYPE_SPOT 2
  29. #ifdef MODE_COMPUTE_LIGHT
  30. struct Light {
  31. uint type;
  32. float energy;
  33. float radius;
  34. float attenuation;
  35. vec3 color;
  36. float spot_angle_radians;
  37. vec3 position;
  38. float spot_attenuation;
  39. vec3 direction;
  40. bool has_shadow;
  41. };
  42. layout(set = 0, binding = 3, std140) uniform Lights {
  43. Light data[MAX_LIGHTS];
  44. }
  45. lights;
  46. #endif
  47. layout(push_constant, binding = 0, std430) uniform Params {
  48. ivec3 limits;
  49. uint stack_size;
  50. float emission_scale;
  51. float propagation;
  52. float dynamic_range;
  53. uint light_count;
  54. uint cell_offset;
  55. uint cell_count;
  56. uint pad[2];
  57. }
  58. params;
  59. layout(set = 0, binding = 4, std140) uniform Outputs {
  60. vec4 data[];
  61. }
  62. output;
  63. #ifdef MODE_COMPUTE_LIGHT
  64. uint raymarch(float distance, float distance_adv, vec3 from, vec3 direction) {
  65. uint result = NO_CHILDREN;
  66. ivec3 size = ivec3(max(max(params.limits.x, params.limits.y), params.limits.z));
  67. while (distance > -distance_adv) { //use this to avoid precision errors
  68. uint cell = 0;
  69. ivec3 pos = ivec3(from);
  70. if (all(greaterThanEqual(pos, ivec3(0))) && all(lessThan(pos, size))) {
  71. ivec3 ofs = ivec3(0);
  72. ivec3 half_size = size / 2;
  73. for (int i = 0; i < params.stack_size - 1; i++) {
  74. bvec3 greater = greaterThanEqual(pos, ofs + half_size);
  75. ofs += mix(ivec3(0), half_size, greater);
  76. uint child = 0; //wonder if this can be done faster
  77. if (greater.x) {
  78. child |= 1;
  79. }
  80. if (greater.y) {
  81. child |= 2;
  82. }
  83. if (greater.z) {
  84. child |= 4;
  85. }
  86. cell = cell_children.data[cell].children[child];
  87. if (cell == NO_CHILDREN)
  88. break;
  89. half_size >>= ivec3(1);
  90. }
  91. if (cell != NO_CHILDREN) {
  92. return cell; //found cell!
  93. }
  94. }
  95. from += direction * distance_adv;
  96. distance -= distance_adv;
  97. }
  98. return NO_CHILDREN;
  99. }
  100. bool compute_light_vector(uint light, uint cell, vec3 pos, out float attenuation, out vec3 light_pos) {
  101. if (lights.data[light].type == LIGHT_TYPE_DIRECTIONAL) {
  102. light_pos = pos - lights.data[light].direction * length(vec3(params.limits));
  103. attenuation = 1.0;
  104. } else {
  105. light_pos = lights.data[light].position;
  106. float distance = length(pos - light_pos);
  107. if (distance >= lights.data[light].radius) {
  108. return false;
  109. }
  110. attenuation = pow(clamp(1.0 - distance / lights.data[light].radius, 0.0001, 1.0), lights.data[light].attenuation);
  111. if (lights.data[light].type == LIGHT_TYPE_SPOT) {
  112. vec3 rel = normalize(pos - light_pos);
  113. float angle = acos(dot(rel, lights.data[light].direction));
  114. if (angle > lights.data[light].spot_angle_radians) {
  115. return false;
  116. }
  117. float d = clamp(angle / lights.data[light].spot_angle_radians, 0, 1);
  118. attenuation *= pow(1.0 - d, lights.data[light].spot_attenuation);
  119. }
  120. }
  121. return true;
  122. }
  123. float get_normal_advance(vec3 p_normal) {
  124. vec3 normal = p_normal;
  125. vec3 unorm = abs(normal);
  126. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  127. // x code
  128. unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
  129. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  130. // y code
  131. unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
  132. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  133. // z code
  134. unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
  135. } else {
  136. // oh-no we messed up code
  137. // has to be
  138. unorm = vec3(1.0, 0.0, 0.0);
  139. }
  140. return 1.0 / dot(normal, unorm);
  141. }
  142. #endif
  143. void main() {
  144. uint cell_index = gl_GlobalInvocationID.x;
  145. if (cell_index >= params.cell_count) {
  146. return;
  147. }
  148. cell_index += params.cell_offset;
  149. 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);
  150. vec4 albedo = unpackUnorm4x8(cell_data.data[cell_index].albedo);
  151. #ifdef MODE_COMPUTE_LIGHT
  152. vec3 pos = vec3(posu) + vec3(0.5);
  153. vec3 emission = vec3(ivec3(cell_data.data[cell_index].emission & 0x3FF, (cell_data.data[cell_index].emission >> 10) & 0x7FF, cell_data.data[cell_index].emission >> 21)) * params.emission_scale;
  154. vec4 normal = unpackSnorm4x8(cell_data.data[cell_index].normal);
  155. #ifdef MODE_ANISOTROPIC
  156. vec3 accum[6] = vec3[](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
  157. 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));
  158. #else
  159. vec3 accum = vec3(0.0);
  160. #endif
  161. for (uint i = 0; i < params.light_count; i++) {
  162. float attenuation;
  163. vec3 light_pos;
  164. if (!compute_light_vector(i, cell_index, pos, attenuation, light_pos)) {
  165. continue;
  166. }
  167. vec3 light_dir = pos - light_pos;
  168. float distance = length(light_dir);
  169. light_dir = normalize(light_dir);
  170. if (length(normal.xyz) > 0.2 && dot(normal.xyz, light_dir) >= 0) {
  171. continue; //not facing the light
  172. }
  173. if (lights.data[i].has_shadow) {
  174. float distance_adv = get_normal_advance(light_dir);
  175. distance += distance_adv - mod(distance, distance_adv); //make it reach the center of the box always
  176. vec3 from = pos - light_dir * distance; //approximate
  177. from -= sign(light_dir) * 0.45; //go near the edge towards the light direction to avoid self occlusion
  178. uint result = raymarch(distance, distance_adv, from, light_dir);
  179. if (result != cell_index) {
  180. continue; //was occluded
  181. }
  182. }
  183. vec3 light = lights.data[i].color * albedo.rgb * attenuation * lights.data[i].energy;
  184. #ifdef MODE_ANISOTROPIC
  185. for (uint j = 0; j < 6; j++) {
  186. accum[j] += max(0.0, dot(accum_dir, -light_dir)) * light + emission;
  187. }
  188. #else
  189. if (length(normal.xyz) > 0.2) {
  190. accum += max(0.0, dot(normal.xyz, -light_dir)) * light + emission;
  191. } else {
  192. //all directions
  193. accum += light + emission;
  194. }
  195. #endif
  196. }
  197. #ifdef MODE_ANISOTROPIC
  198. output.data[cell_index * 6 + 0] = vec4(accum[0], 0.0);
  199. output.data[cell_index * 6 + 1] = vec4(accum[1], 0.0);
  200. output.data[cell_index * 6 + 2] = vec4(accum[2], 0.0);
  201. output.data[cell_index * 6 + 3] = vec4(accum[3], 0.0);
  202. output.data[cell_index * 6 + 4] = vec4(accum[4], 0.0);
  203. output.data[cell_index * 6 + 5] = vec4(accum[5], 0.0);
  204. #else
  205. output.data[cell_index] = vec4(accum, 0.0);
  206. #endif
  207. #endif //MODE_COMPUTE_LIGHT
  208. #ifdef MODE_UPDATE_MIPMAPS
  209. {
  210. #ifdef MODE_ANISOTROPIC
  211. vec3 light_accum[6] = vec3[](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
  212. #else
  213. vec3 light_accum = vec3(0.0);
  214. #endif
  215. float count = 0.0;
  216. for (uint i = 0; i < 8; i++) {
  217. uint child_index = cell_children.data[cell_index].children[i];
  218. if (child_index == NO_CHILDREN) {
  219. continue;
  220. }
  221. #ifdef MODE_ANISOTROPIC
  222. light_accum[1] += output.data[child_index * 6 + 0].rgb;
  223. light_accum[2] += output.data[child_index * 6 + 1].rgb;
  224. light_accum[3] += output.data[child_index * 6 + 2].rgb;
  225. light_accum[4] += output.data[child_index * 6 + 3].rgb;
  226. light_accum[5] += output.data[child_index * 6 + 4].rgb;
  227. light_accum[6] += output.data[child_index * 6 + 5].rgb;
  228. #else
  229. light_accum += output.data[child_index].rgb;
  230. #endif
  231. count += 1.0;
  232. }
  233. float divisor = mix(8.0, count, params.propagation);
  234. #ifdef MODE_ANISOTROPIC
  235. output.data[cell_index * 6 + 0] = vec4(light_accum[0] / divisor, 0.0);
  236. output.data[cell_index * 6 + 1] = vec4(light_accum[1] / divisor, 0.0);
  237. output.data[cell_index * 6 + 2] = vec4(light_accum[2] / divisor, 0.0);
  238. output.data[cell_index * 6 + 3] = vec4(light_accum[3] / divisor, 0.0);
  239. output.data[cell_index * 6 + 4] = vec4(light_accum[4] / divisor, 0.0);
  240. output.data[cell_index * 6 + 5] = vec4(light_accum[5] / divisor, 0.0);
  241. #else
  242. output.data[cell_index] = vec4(light_accum / divisor, 0.0);
  243. #endif
  244. }
  245. #endif
  246. #ifdef MODE_WRITE_TEXTURE
  247. {
  248. }
  249. #endif
  250. }