giprobe_write.glsl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. struct CellChildren {
  7. uint children[8];
  8. };
  9. layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
  10. CellChildren data[];
  11. }
  12. cell_children;
  13. struct CellData {
  14. uint position; // xyz 10 bits
  15. uint albedo; //rgb albedo
  16. uint emission; //rgb normalized with e as multiplier
  17. uint normal; //RGB normal encoded
  18. };
  19. layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
  20. CellData data[];
  21. }
  22. cell_data;
  23. #define LIGHT_TYPE_DIRECTIONAL 0
  24. #define LIGHT_TYPE_OMNI 1
  25. #define LIGHT_TYPE_SPOT 2
  26. #ifdef MODE_COMPUTE_LIGHT
  27. struct Light {
  28. uint type;
  29. float energy;
  30. float radius;
  31. float attenuation;
  32. vec3 color;
  33. float cos_spot_angle;
  34. vec3 position;
  35. float inv_spot_attenuation;
  36. vec3 direction;
  37. bool has_shadow;
  38. };
  39. layout(set = 0, binding = 3, std140) uniform Lights {
  40. Light data[MAX_LIGHTS];
  41. }
  42. lights;
  43. #endif
  44. layout(push_constant, std430) uniform Params {
  45. ivec3 limits;
  46. uint stack_size;
  47. float emission_scale;
  48. float propagation;
  49. float dynamic_range;
  50. uint light_count;
  51. uint cell_offset;
  52. uint cell_count;
  53. uint pad[2];
  54. }
  55. params;
  56. layout(set = 0, binding = 4, std140) uniform Outputs {
  57. vec4 data[];
  58. }
  59. output;
  60. #ifdef MODE_COMPUTE_LIGHT
  61. uint raymarch(float distance, float distance_adv, vec3 from, vec3 direction) {
  62. uint result = NO_CHILDREN;
  63. ivec3 size = ivec3(max(max(params.limits.x, params.limits.y), params.limits.z));
  64. while (distance > -distance_adv) { //use this to avoid precision errors
  65. uint cell = 0;
  66. ivec3 pos = ivec3(from);
  67. if (all(greaterThanEqual(pos, ivec3(0))) && all(lessThan(pos, size))) {
  68. ivec3 ofs = ivec3(0);
  69. ivec3 half_size = size / 2;
  70. for (int i = 0; i < params.stack_size - 1; i++) {
  71. bvec3 greater = greaterThanEqual(pos, ofs + half_size);
  72. ofs += mix(ivec3(0), half_size, greater);
  73. uint child = 0; //wonder if this can be done faster
  74. if (greater.x) {
  75. child |= 1;
  76. }
  77. if (greater.y) {
  78. child |= 2;
  79. }
  80. if (greater.z) {
  81. child |= 4;
  82. }
  83. cell = cell_children.data[cell].children[child];
  84. if (cell == NO_CHILDREN) {
  85. break;
  86. }
  87. half_size >>= ivec3(1);
  88. }
  89. if (cell != NO_CHILDREN) {
  90. return cell; //found cell!
  91. }
  92. }
  93. from += direction * distance_adv;
  94. distance -= distance_adv;
  95. }
  96. return NO_CHILDREN;
  97. }
  98. bool compute_light_vector(uint light, uint cell, vec3 pos, out float attenuation, out vec3 light_pos) {
  99. if (lights.data[light].type == LIGHT_TYPE_DIRECTIONAL) {
  100. light_pos = pos - lights.data[light].direction * length(vec3(params.limits));
  101. attenuation = 1.0;
  102. } else {
  103. light_pos = lights.data[light].position;
  104. float distance = length(pos - light_pos);
  105. if (distance >= lights.data[light].radius) {
  106. return false;
  107. }
  108. attenuation = pow(clamp(1.0 - distance / lights.data[light].radius, 0.0001, 1.0), lights.data[light].attenuation);
  109. if (lights.data[light].type == LIGHT_TYPE_SPOT) {
  110. vec3 rel = normalize(pos - light_pos);
  111. float cos_spot_angle = lights.data[light].cos_spot_angle;
  112. float cos_angle = dot(rel, lights.data[light].direction);
  113. if (cos_angle < cos_spot_angle) {
  114. return false;
  115. }
  116. float scos = max(cos_angle, cos_spot_angle);
  117. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
  118. attenuation *= 1.0 - pow(spot_rim, lights.data[light].inv_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. vec3 accum = vec3(0.0);
  156. for (uint i = 0; i < params.light_count; i++) {
  157. float attenuation;
  158. vec3 light_pos;
  159. if (!compute_light_vector(i, cell_index, pos, attenuation, light_pos)) {
  160. continue;
  161. }
  162. vec3 light_dir = pos - light_pos;
  163. float distance = length(light_dir);
  164. light_dir = normalize(light_dir);
  165. if (length(normal.xyz) > 0.2 && dot(normal.xyz, light_dir) >= 0) {
  166. continue; //not facing the light
  167. }
  168. if (lights.data[i].has_shadow) {
  169. float distance_adv = get_normal_advance(light_dir);
  170. distance += distance_adv - mod(distance, distance_adv); //make it reach the center of the box always
  171. vec3 from = pos - light_dir * distance; //approximate
  172. from -= sign(light_dir) * 0.45; //go near the edge towards the light direction to avoid self occlusion
  173. uint result = raymarch(distance, distance_adv, from, light_dir);
  174. if (result != cell_index) {
  175. continue; //was occluded
  176. }
  177. }
  178. vec3 light = lights.data[i].color * albedo.rgb * attenuation * lights.data[i].energy;
  179. if (length(normal.xyz) > 0.2) {
  180. accum += max(0.0, dot(normal.xyz, -light_dir)) * light + emission;
  181. } else {
  182. //all directions
  183. accum += light + emission;
  184. }
  185. }
  186. output.data[cell_index] = vec4(accum, 0.0);
  187. #endif //MODE_COMPUTE_LIGHT
  188. #ifdef MODE_UPDATE_MIPMAPS
  189. {
  190. vec3 light_accum = vec3(0.0);
  191. float count = 0.0;
  192. for (uint i = 0; i < 8; i++) {
  193. uint child_index = cell_children.data[cell_index].children[i];
  194. if (child_index == NO_CHILDREN) {
  195. continue;
  196. }
  197. light_accum += output.data[child_index].rgb;
  198. count += 1.0;
  199. }
  200. float divisor = mix(8.0, count, params.propagation);
  201. output.data[cell_index] = vec4(light_accum / divisor, 0.0);
  202. }
  203. #endif
  204. #ifdef MODE_WRITE_TEXTURE
  205. {
  206. }
  207. #endif
  208. }