giprobe_write.glsl 8.2 KB

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