resolve.glsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. #ifdef MODE_RESOLVE_DEPTH
  6. layout(set = 0, binding = 0) uniform sampler2DMS source_depth;
  7. layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_depth;
  8. #endif
  9. #ifdef MODE_RESOLVE_GI
  10. layout(set = 0, binding = 0) uniform sampler2DMS source_depth;
  11. layout(set = 0, binding = 1) uniform sampler2DMS source_normal_roughness;
  12. layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_depth;
  13. layout(rgba8, set = 1, binding = 1) uniform restrict writeonly image2D dest_normal_roughness;
  14. #ifdef VOXEL_GI_RESOLVE
  15. layout(set = 2, binding = 0) uniform usampler2DMS source_voxel_gi;
  16. layout(rg8ui, set = 3, binding = 0) uniform restrict writeonly uimage2D dest_voxel_gi;
  17. #endif
  18. #endif
  19. layout(push_constant, std430) uniform Params {
  20. ivec2 screen_size;
  21. int sample_count;
  22. uint pad;
  23. }
  24. params;
  25. void main() {
  26. // Pixel being shaded
  27. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  28. if (any(greaterThanEqual(pos, params.screen_size))) { //too large, do nothing
  29. return;
  30. }
  31. #ifdef MODE_RESOLVE_DEPTH
  32. float depth_avg = 0.0;
  33. for (int i = 0; i < params.sample_count; i++) {
  34. depth_avg += texelFetch(source_depth, pos, i).r;
  35. }
  36. depth_avg /= float(params.sample_count);
  37. imageStore(dest_depth, pos, vec4(depth_avg));
  38. #endif
  39. #ifdef MODE_RESOLVE_GI
  40. float best_depth = 1e20;
  41. vec4 best_normal_roughness = vec4(0.0);
  42. #ifdef VOXEL_GI_RESOLVE
  43. uvec2 best_voxel_gi;
  44. #endif
  45. #if 0
  46. for(int i=0;i<params.sample_count;i++) {
  47. float depth = texelFetch(source_depth,pos,i).r;
  48. if (depth < best_depth) { //use the depth closest to camera
  49. best_depth = depth;
  50. best_normal_roughness = texelFetch(source_normal_roughness,pos,i);
  51. #ifdef VOXEL_GI_RESOLVE
  52. best_voxel_gi = texelFetch(source_voxel_gi,pos,i).rg;
  53. #endif
  54. }
  55. }
  56. #else
  57. #if 1
  58. vec4 group1;
  59. vec4 group2;
  60. vec4 group3;
  61. vec4 group4;
  62. int best_index = 0;
  63. //2X
  64. group1.x = texelFetch(source_depth, pos, 0).r;
  65. group1.y = texelFetch(source_depth, pos, 1).r;
  66. //4X
  67. if (params.sample_count >= 4) {
  68. group1.z = texelFetch(source_depth, pos, 2).r;
  69. group1.w = texelFetch(source_depth, pos, 3).r;
  70. }
  71. //8X
  72. if (params.sample_count >= 8) {
  73. group2.x = texelFetch(source_depth, pos, 4).r;
  74. group2.y = texelFetch(source_depth, pos, 5).r;
  75. group2.z = texelFetch(source_depth, pos, 6).r;
  76. group2.w = texelFetch(source_depth, pos, 7).r;
  77. }
  78. //16X
  79. if (params.sample_count >= 16) {
  80. group3.x = texelFetch(source_depth, pos, 8).r;
  81. group3.y = texelFetch(source_depth, pos, 9).r;
  82. group3.z = texelFetch(source_depth, pos, 10).r;
  83. group3.w = texelFetch(source_depth, pos, 11).r;
  84. group4.x = texelFetch(source_depth, pos, 12).r;
  85. group4.y = texelFetch(source_depth, pos, 13).r;
  86. group4.z = texelFetch(source_depth, pos, 14).r;
  87. group4.w = texelFetch(source_depth, pos, 15).r;
  88. }
  89. if (params.sample_count == 2) {
  90. best_index = (pos.x & 1) ^ ((pos.y >> 1) & 1); //not much can be done here
  91. } else if (params.sample_count == 4) {
  92. vec4 freq = vec4(equal(group1, vec4(group1.x)));
  93. freq += vec4(equal(group1, vec4(group1.y)));
  94. freq += vec4(equal(group1, vec4(group1.z)));
  95. freq += vec4(equal(group1, vec4(group1.w)));
  96. float min_f = freq.x;
  97. best_index = 0;
  98. if (freq.y < min_f) {
  99. best_index = 1;
  100. min_f = freq.y;
  101. }
  102. if (freq.z < min_f) {
  103. best_index = 2;
  104. min_f = freq.z;
  105. }
  106. if (freq.w < min_f) {
  107. best_index = 3;
  108. }
  109. } else if (params.sample_count == 8) {
  110. vec4 freq0 = vec4(equal(group1, vec4(group1.x)));
  111. vec4 freq1 = vec4(equal(group2, vec4(group1.x)));
  112. freq0 += vec4(equal(group1, vec4(group1.y)));
  113. freq1 += vec4(equal(group2, vec4(group1.y)));
  114. freq0 += vec4(equal(group1, vec4(group1.z)));
  115. freq1 += vec4(equal(group2, vec4(group1.z)));
  116. freq0 += vec4(equal(group1, vec4(group1.w)));
  117. freq1 += vec4(equal(group2, vec4(group1.w)));
  118. freq0 += vec4(equal(group1, vec4(group2.x)));
  119. freq1 += vec4(equal(group2, vec4(group2.x)));
  120. freq0 += vec4(equal(group1, vec4(group2.y)));
  121. freq1 += vec4(equal(group2, vec4(group2.y)));
  122. freq0 += vec4(equal(group1, vec4(group2.z)));
  123. freq1 += vec4(equal(group2, vec4(group2.z)));
  124. freq0 += vec4(equal(group1, vec4(group2.w)));
  125. freq1 += vec4(equal(group2, vec4(group2.w)));
  126. float min_f0 = freq0.x;
  127. int best_index0 = 0;
  128. if (freq0.y < min_f0) {
  129. best_index0 = 1;
  130. min_f0 = freq0.y;
  131. }
  132. if (freq0.z < min_f0) {
  133. best_index0 = 2;
  134. min_f0 = freq0.z;
  135. }
  136. if (freq0.w < min_f0) {
  137. best_index0 = 3;
  138. min_f0 = freq0.w;
  139. }
  140. float min_f1 = freq1.x;
  141. int best_index1 = 4;
  142. if (freq1.y < min_f1) {
  143. best_index1 = 5;
  144. min_f1 = freq1.y;
  145. }
  146. if (freq1.z < min_f1) {
  147. best_index1 = 6;
  148. min_f1 = freq1.z;
  149. }
  150. if (freq1.w < min_f1) {
  151. best_index1 = 7;
  152. min_f1 = freq1.w;
  153. }
  154. best_index = mix(best_index0, best_index1, min_f0 < min_f1);
  155. }
  156. #else
  157. float depths[16];
  158. int depth_indices[16];
  159. int depth_amount[16];
  160. int depth_count = 0;
  161. for (int i = 0; i < params.sample_count; i++) {
  162. float depth = texelFetch(source_depth, pos, i).r;
  163. int depth_index = -1;
  164. for (int j = 0; j < depth_count; j++) {
  165. if (abs(depths[j] - depth) < 0.000001) {
  166. depth_index = j;
  167. break;
  168. }
  169. }
  170. if (depth_index == -1) {
  171. depths[depth_count] = depth;
  172. depth_indices[depth_count] = i;
  173. depth_amount[depth_count] = 1;
  174. depth_count += 1;
  175. } else {
  176. depth_amount[depth_index] += 1;
  177. }
  178. }
  179. int depth_least = 0xFFFF;
  180. int best_index = 0;
  181. for (int j = 0; j < depth_count; j++) {
  182. if (depth_amount[j] < depth_least) {
  183. best_index = depth_indices[j];
  184. depth_least = depth_amount[j];
  185. }
  186. }
  187. #endif
  188. best_depth = texelFetch(source_depth, pos, best_index).r;
  189. best_normal_roughness = texelFetch(source_normal_roughness, pos, best_index);
  190. #ifdef VOXEL_GI_RESOLVE
  191. best_voxel_gi = texelFetch(source_voxel_gi, pos, best_index).rg;
  192. #endif
  193. #endif
  194. imageStore(dest_depth, pos, vec4(best_depth));
  195. imageStore(dest_normal_roughness, pos, vec4(best_normal_roughness));
  196. #ifdef VOXEL_GI_RESOLVE
  197. imageStore(dest_voxel_gi, pos, uvec4(best_voxel_gi, 0, 0));
  198. #endif
  199. #endif
  200. }