copy.glsl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. #define FLAG_HORIZONTAL (1 << 0)
  6. #define FLAG_USE_BLUR_SECTION (1 << 1)
  7. #define FLAG_USE_ORTHOGONAL_PROJECTION (1 << 2)
  8. #define FLAG_DOF_NEAR_FIRST_TAP (1 << 3)
  9. #define FLAG_GLOW_FIRST_PASS (1 << 4)
  10. #define FLAG_FLIP_Y (1 << 5)
  11. #define FLAG_FORCE_LUMINANCE (1 << 6)
  12. #define FLAG_COPY_ALL_SOURCE (1 << 7)
  13. #define FLAG_ALPHA_TO_ONE (1 << 8)
  14. layout(push_constant, std430) uniform Params {
  15. ivec4 section;
  16. ivec2 target;
  17. uint flags;
  18. uint pad;
  19. // Glow.
  20. float glow_strength;
  21. float glow_bloom;
  22. float glow_hdr_threshold;
  23. float glow_hdr_scale;
  24. float glow_exposure;
  25. float glow_white;
  26. float glow_luminance_cap;
  27. float glow_auto_exposure_scale;
  28. // DOF.
  29. float camera_z_far;
  30. float camera_z_near;
  31. uint pad2[2];
  32. vec4 set_color;
  33. }
  34. params;
  35. #ifdef MODE_CUBEMAP_ARRAY_TO_PANORAMA
  36. layout(set = 0, binding = 0) uniform samplerCubeArray source_color;
  37. #elif defined(MODE_CUBEMAP_TO_PANORAMA)
  38. layout(set = 0, binding = 0) uniform samplerCube source_color;
  39. #elif !defined(MODE_SET_COLOR)
  40. layout(set = 0, binding = 0) uniform sampler2D source_color;
  41. #endif
  42. #ifdef GLOW_USE_AUTO_EXPOSURE
  43. layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
  44. #endif
  45. #if defined(MODE_LINEARIZE_DEPTH_COPY) || defined(MODE_SIMPLE_COPY_DEPTH)
  46. layout(r32f, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
  47. #elif defined(DST_IMAGE_8BIT)
  48. layout(rgba8, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
  49. #else
  50. layout(rgba16f, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
  51. #endif
  52. #ifdef MODE_GAUSSIAN_BLUR
  53. shared vec4 local_cache[256];
  54. shared vec4 temp_cache[128];
  55. #endif
  56. void main() {
  57. // Pixel being shaded
  58. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  59. #ifndef MODE_GAUSSIAN_BLUR // Gaussian blur needs the extra threads
  60. if (any(greaterThanEqual(pos, params.section.zw))) { //too large, do nothing
  61. return;
  62. }
  63. #endif
  64. #ifdef MODE_MIPMAP
  65. ivec2 base_pos = (pos + params.section.xy) << 1;
  66. vec4 color = texelFetch(source_color, base_pos, 0);
  67. color += texelFetch(source_color, base_pos + ivec2(0, 1), 0);
  68. color += texelFetch(source_color, base_pos + ivec2(1, 0), 0);
  69. color += texelFetch(source_color, base_pos + ivec2(1, 1), 0);
  70. color /= 4.0;
  71. color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isinf(color));
  72. color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isnan(color));
  73. imageStore(dest_buffer, pos + params.target, color);
  74. #endif
  75. #ifdef MODE_GAUSSIAN_BLUR
  76. // First pass copy texture into 16x16 local memory for every 8x8 thread block
  77. vec2 quad_center_uv = clamp(vec2(params.section.xy + gl_GlobalInvocationID.xy + gl_LocalInvocationID.xy - 3.5) / params.section.zw, vec2(0.5 / params.section.zw), vec2(1.0 - 1.5 / params.section.zw));
  78. uint dest_index = gl_LocalInvocationID.x * 2 + gl_LocalInvocationID.y * 2 * 16;
  79. local_cache[dest_index] = textureLod(source_color, quad_center_uv, 0);
  80. local_cache[dest_index + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.z, 0.0), 0);
  81. local_cache[dest_index + 16] = textureLod(source_color, quad_center_uv + vec2(0.0, 1.0 / params.section.w), 0);
  82. local_cache[dest_index + 16 + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.zw), 0);
  83. #ifdef MODE_GLOW
  84. if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
  85. // Tonemap initial samples to reduce weight of fireflies: https://graphicrants.blogspot.com/2013/12/tone-mapping.html
  86. vec3 tonemap_col = vec3(0.299, 0.587, 0.114) / max(params.glow_luminance_cap, 6.0);
  87. local_cache[dest_index] /= 1.0 + dot(local_cache[dest_index].rgb, tonemap_col);
  88. local_cache[dest_index + 1] /= 1.0 + dot(local_cache[dest_index + 1].rgb, tonemap_col);
  89. local_cache[dest_index + 16] /= 1.0 + dot(local_cache[dest_index + 16].rgb, tonemap_col);
  90. local_cache[dest_index + 16 + 1] /= 1.0 + dot(local_cache[dest_index + 16 + 1].rgb, tonemap_col);
  91. }
  92. const float kernel[5] = { 0.2024, 0.1790, 0.1240, 0.0672, 0.0285 };
  93. #else
  94. // Simpler blur uses SIGMA2 for the gaussian kernel for a stronger effect.
  95. const float kernel[4] = { 0.214607, 0.189879, 0.131514, 0.071303 };
  96. #endif
  97. memoryBarrierShared();
  98. barrier();
  99. // Horizontal pass. Needs to copy into 8x16 chunk of local memory so vertical pass has full resolution
  100. uint read_index = gl_LocalInvocationID.x + gl_LocalInvocationID.y * 32 + 4;
  101. vec4 color_top = vec4(0.0);
  102. color_top += local_cache[read_index] * kernel[0];
  103. color_top += local_cache[read_index + 1] * kernel[1];
  104. color_top += local_cache[read_index + 2] * kernel[2];
  105. color_top += local_cache[read_index + 3] * kernel[3];
  106. color_top += local_cache[read_index - 1] * kernel[1];
  107. color_top += local_cache[read_index - 2] * kernel[2];
  108. color_top += local_cache[read_index - 3] * kernel[3];
  109. #ifdef MODE_GLOW
  110. color_top += local_cache[read_index + 4] * kernel[4];
  111. color_top += local_cache[read_index - 4] * kernel[4];
  112. #endif // MODE_GLOW
  113. vec4 color_bottom = vec4(0.0);
  114. color_bottom += local_cache[read_index + 16] * kernel[0];
  115. color_bottom += local_cache[read_index + 1 + 16] * kernel[1];
  116. color_bottom += local_cache[read_index + 2 + 16] * kernel[2];
  117. color_bottom += local_cache[read_index + 3 + 16] * kernel[3];
  118. color_bottom += local_cache[read_index - 1 + 16] * kernel[1];
  119. color_bottom += local_cache[read_index - 2 + 16] * kernel[2];
  120. color_bottom += local_cache[read_index - 3 + 16] * kernel[3];
  121. #ifdef MODE_GLOW
  122. color_bottom += local_cache[read_index + 4 + 16] * kernel[4];
  123. color_bottom += local_cache[read_index - 4 + 16] * kernel[4];
  124. #endif // MODE_GLOW
  125. // rotate samples to take advantage of cache coherency
  126. uint write_index = gl_LocalInvocationID.y * 2 + gl_LocalInvocationID.x * 16;
  127. temp_cache[write_index] = color_top;
  128. temp_cache[write_index + 1] = color_bottom;
  129. memoryBarrierShared();
  130. barrier();
  131. // If destination outside of texture, can stop doing work now
  132. if (any(greaterThanEqual(pos, params.section.zw))) {
  133. return;
  134. }
  135. // Vertical pass
  136. uint index = gl_LocalInvocationID.y + gl_LocalInvocationID.x * 16 + 4;
  137. vec4 color = vec4(0.0);
  138. color += temp_cache[index] * kernel[0];
  139. color += temp_cache[index + 1] * kernel[1];
  140. color += temp_cache[index + 2] * kernel[2];
  141. color += temp_cache[index + 3] * kernel[3];
  142. color += temp_cache[index - 1] * kernel[1];
  143. color += temp_cache[index - 2] * kernel[2];
  144. color += temp_cache[index - 3] * kernel[3];
  145. #ifdef MODE_GLOW
  146. color += temp_cache[index + 4] * kernel[4];
  147. color += temp_cache[index - 4] * kernel[4];
  148. #endif // MODE_GLOW
  149. #ifdef MODE_GLOW
  150. if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
  151. // Undo tonemap to restore range: https://graphicrants.blogspot.com/2013/12/tone-mapping.html
  152. color /= 1.0 - dot(color.rgb, vec3(0.299, 0.587, 0.114) / max(params.glow_luminance_cap, 6.0));
  153. }
  154. color *= params.glow_strength;
  155. if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
  156. #ifdef GLOW_USE_AUTO_EXPOSURE
  157. color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / params.glow_auto_exposure_scale;
  158. #endif
  159. color *= params.glow_exposure;
  160. float luminance = max(color.r, max(color.g, color.b));
  161. float feedback = max(smoothstep(params.glow_hdr_threshold, params.glow_hdr_threshold + params.glow_hdr_scale, luminance), params.glow_bloom);
  162. color = min(color * feedback, vec4(params.glow_luminance_cap));
  163. }
  164. #endif // MODE_GLOW
  165. imageStore(dest_buffer, pos + params.target, color);
  166. #endif // MODE_GAUSSIAN_BLUR
  167. #ifdef MODE_SIMPLE_COPY
  168. vec4 color;
  169. if (bool(params.flags & FLAG_COPY_ALL_SOURCE)) {
  170. vec2 uv = vec2(pos) / vec2(params.section.zw);
  171. if (bool(params.flags & FLAG_FLIP_Y)) {
  172. uv.y = 1.0 - uv.y;
  173. }
  174. color = textureLod(source_color, uv, 0.0);
  175. } else {
  176. color = texelFetch(source_color, pos + params.section.xy, 0);
  177. if (bool(params.flags & FLAG_FLIP_Y)) {
  178. pos.y = params.section.w - pos.y - 1;
  179. }
  180. }
  181. if (bool(params.flags & FLAG_FORCE_LUMINANCE)) {
  182. color.rgb = vec3(max(max(color.r, color.g), color.b));
  183. }
  184. if (bool(params.flags & FLAG_ALPHA_TO_ONE)) {
  185. color.a = 1.0;
  186. }
  187. imageStore(dest_buffer, pos + params.target, color);
  188. #endif // MODE_SIMPLE_COPY
  189. #ifdef MODE_SIMPLE_COPY_DEPTH
  190. vec4 color = texelFetch(source_color, pos + params.section.xy, 0);
  191. if (bool(params.flags & FLAG_FLIP_Y)) {
  192. pos.y = params.section.w - pos.y - 1;
  193. }
  194. imageStore(dest_buffer, pos + params.target, vec4(color.r));
  195. #endif // MODE_SIMPLE_COPY_DEPTH
  196. #ifdef MODE_LINEARIZE_DEPTH_COPY
  197. float depth = texelFetch(source_color, pos + params.section.xy, 0).r;
  198. depth = depth * 2.0 - 1.0;
  199. depth = 2.0 * params.camera_z_near * params.camera_z_far / (params.camera_z_far + params.camera_z_near - depth * (params.camera_z_far - params.camera_z_near));
  200. vec4 color = vec4(depth / params.camera_z_far);
  201. if (bool(params.flags & FLAG_FLIP_Y)) {
  202. pos.y = params.section.w - pos.y - 1;
  203. }
  204. imageStore(dest_buffer, pos + params.target, color);
  205. #endif // MODE_LINEARIZE_DEPTH_COPY
  206. #if defined(MODE_CUBEMAP_TO_PANORAMA) || defined(MODE_CUBEMAP_ARRAY_TO_PANORAMA)
  207. const float PI = 3.14159265359;
  208. vec2 uv = vec2(pos) / vec2(params.section.zw);
  209. if (bool(params.flags & FLAG_FLIP_Y)) {
  210. uv.y = 1.0 - uv.y;
  211. }
  212. float phi = uv.x * 2.0 * PI;
  213. float theta = uv.y * PI;
  214. vec3 normal;
  215. normal.x = sin(phi) * sin(theta) * -1.0;
  216. normal.y = cos(theta);
  217. normal.z = cos(phi) * sin(theta) * -1.0;
  218. #ifdef MODE_CUBEMAP_TO_PANORAMA
  219. vec4 color = textureLod(source_color, normal, params.camera_z_far); //the biggest the lod the least the acne
  220. #else
  221. vec4 color = textureLod(source_color, vec4(normal, params.camera_z_far), 0.0); //the biggest the lod the least the acne
  222. #endif
  223. imageStore(dest_buffer, pos + params.target, color);
  224. #endif // defined(MODE_CUBEMAP_TO_PANORAMA) || defined(MODE_CUBEMAP_ARRAY_TO_PANORAMA)
  225. #ifdef MODE_SET_COLOR
  226. imageStore(dest_buffer, pos + params.target, params.set_color);
  227. #endif
  228. }