resolve.glsl 5.5 KB

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