effect_blur.glsl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* clang-format off */
  2. [vertex]
  3. #ifdef USE_GLES_OVER_GL
  4. #define lowp
  5. #define mediump
  6. #define highp
  7. #else
  8. precision highp float;
  9. precision highp int;
  10. #endif
  11. layout(location = 0) in vec2 vertex_attrib;
  12. /* clang-format on */
  13. layout(location = 4) in vec2 uv_in;
  14. out vec2 uv_interp;
  15. #ifdef USE_BLUR_SECTION
  16. uniform vec4 blur_section;
  17. #endif
  18. void main() {
  19. uv_interp = uv_in;
  20. gl_Position = vec4(vertex_attrib, 0.0, 1.0);
  21. #ifdef USE_BLUR_SECTION
  22. uv_interp = blur_section.xy + uv_interp * blur_section.zw;
  23. gl_Position.xy = (blur_section.xy + (gl_Position.xy * 0.5 + 0.5) * blur_section.zw) * 2.0 - 1.0;
  24. #endif
  25. }
  26. /* clang-format off */
  27. [fragment]
  28. #ifdef USE_GLES_OVER_GL
  29. #define lowp
  30. #define mediump
  31. #define highp
  32. #else
  33. #if defined(USE_HIGHP_PRECISION)
  34. precision highp float;
  35. precision highp int;
  36. #else
  37. precision mediump float;
  38. precision mediump int;
  39. #endif
  40. #endif
  41. in vec2 uv_interp;
  42. /* clang-format on */
  43. uniform sampler2D source_color; //texunit:0
  44. uniform float lod;
  45. uniform vec2 pixel_size;
  46. #if defined(GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL)
  47. uniform float glow_strength;
  48. #endif
  49. #if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR)
  50. #ifdef USE_GLES_OVER_GL
  51. #ifdef DOF_QUALITY_LOW
  52. const int dof_kernel_size = 5;
  53. const int dof_kernel_from = 2;
  54. const float dof_kernel[5] = float[](0.153388, 0.221461, 0.250301, 0.221461, 0.153388);
  55. #endif
  56. #ifdef DOF_QUALITY_MEDIUM
  57. const int dof_kernel_size = 11;
  58. const int dof_kernel_from = 5;
  59. const float dof_kernel[11] = float[](0.055037, 0.072806, 0.090506, 0.105726, 0.116061, 0.119726, 0.116061, 0.105726, 0.090506, 0.072806, 0.055037);
  60. #endif
  61. #ifdef DOF_QUALITY_HIGH
  62. const int dof_kernel_size = 21;
  63. const int dof_kernel_from = 10;
  64. const float dof_kernel[21] = float[](0.028174, 0.032676, 0.037311, 0.041944, 0.046421, 0.050582, 0.054261, 0.057307, 0.059587, 0.060998, 0.061476, 0.060998, 0.059587, 0.057307, 0.054261, 0.050582, 0.046421, 0.041944, 0.037311, 0.032676, 0.028174);
  65. #endif
  66. #endif
  67. uniform sampler2D dof_source_depth; //texunit:1
  68. uniform float dof_begin;
  69. uniform float dof_end;
  70. uniform vec2 dof_dir;
  71. uniform float dof_radius;
  72. #endif
  73. #ifdef GLOW_FIRST_PASS
  74. uniform highp float luminance_cap;
  75. uniform float glow_bloom;
  76. uniform float glow_hdr_threshold;
  77. uniform float glow_hdr_scale;
  78. #endif
  79. uniform float camera_z_far;
  80. uniform float camera_z_near;
  81. layout(location = 0) out vec4 frag_color;
  82. void main() {
  83. #ifdef GLOW_GAUSSIAN_HORIZONTAL
  84. vec2 pix_size = pixel_size;
  85. pix_size *= 0.5; //reading from larger buffer, so use more samples
  86. vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938;
  87. color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569;
  88. color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367;
  89. color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595;
  90. color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569;
  91. color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367;
  92. color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595;
  93. color *= glow_strength;
  94. frag_color = color;
  95. #endif
  96. #ifdef GLOW_GAUSSIAN_VERTICAL
  97. vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713;
  98. color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062;
  99. color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581;
  100. color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062;
  101. color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581;
  102. color *= glow_strength;
  103. frag_color = color;
  104. #endif
  105. #ifndef USE_GLES_OVER_GL
  106. #if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR)
  107. #ifdef DOF_QUALITY_LOW
  108. const int dof_kernel_size = 5;
  109. const int dof_kernel_from = 2;
  110. float dof_kernel[5];
  111. dof_kernel[0] = 0.153388;
  112. dof_kernel[1] = 0.221461;
  113. dof_kernel[2] = 0.250301;
  114. dof_kernel[3] = 0.221461;
  115. dof_kernel[4] = 0.153388;
  116. #endif
  117. #ifdef DOF_QUALITY_MEDIUM
  118. const int dof_kernel_size = 11;
  119. const int dof_kernel_from = 5;
  120. float dof_kernel[11];
  121. dof_kernel[0] = 0.055037;
  122. dof_kernel[1] = 0.072806;
  123. dof_kernel[2] = 0.090506;
  124. dof_kernel[3] = 0.105726;
  125. dof_kernel[4] = 0.116061;
  126. dof_kernel[5] = 0.119726;
  127. dof_kernel[6] = 0.116061;
  128. dof_kernel[7] = 0.105726;
  129. dof_kernel[8] = 0.090506;
  130. dof_kernel[9] = 0.072806;
  131. dof_kernel[10] = 0.055037;
  132. #endif
  133. #ifdef DOF_QUALITY_HIGH
  134. const int dof_kernel_size = 21;
  135. const int dof_kernel_from = 10;
  136. float dof_kernel[21];
  137. dof_kernel[0] = 0.028174;
  138. dof_kernel[1] = 0.032676;
  139. dof_kernel[2] = 0.037311;
  140. dof_kernel[3] = 0.041944;
  141. dof_kernel[4] = 0.046421;
  142. dof_kernel[5] = 0.050582;
  143. dof_kernel[6] = 0.054261;
  144. dof_kernel[7] = 0.057307;
  145. dof_kernel[8] = 0.059587;
  146. dof_kernel[9] = 0.060998;
  147. dof_kernel[10] = 0.061476;
  148. dof_kernel[11] = 0.060998;
  149. dof_kernel[12] = 0.059587;
  150. dof_kernel[13] = 0.057307;
  151. dof_kernel[14] = 0.054261;
  152. dof_kernel[15] = 0.050582;
  153. dof_kernel[16] = 0.046421;
  154. dof_kernel[17] = 0.041944;
  155. dof_kernel[18] = 0.037311;
  156. dof_kernel[19] = 0.032676;
  157. dof_kernel[20] = 0.028174;
  158. #endif
  159. #endif
  160. #endif //!USE_GLES_OVER_GL
  161. #ifdef DOF_FAR_BLUR
  162. vec4 color_accum = vec4(0.0);
  163. float depth = textureLod(dof_source_depth, uv_interp, 0.0).r;
  164. depth = depth * 2.0 - 1.0;
  165. #ifdef USE_ORTHOGONAL_PROJECTION
  166. depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
  167. #else
  168. depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near));
  169. #endif
  170. float amount = smoothstep(dof_begin, dof_end, depth);
  171. float k_accum = 0.0;
  172. for (int i = 0; i < dof_kernel_size; i++) {
  173. int int_ofs = i - dof_kernel_from;
  174. vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * amount * dof_radius;
  175. float tap_k = dof_kernel[i];
  176. float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r;
  177. tap_depth = tap_depth * 2.0 - 1.0;
  178. #ifdef USE_ORTHOGONAL_PROJECTION
  179. tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
  180. #else
  181. tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near));
  182. #endif
  183. float tap_amount = int_ofs == 0 ? 1.0 : smoothstep(dof_begin, dof_end, tap_depth);
  184. tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect
  185. vec4 tap_color = textureLod(source_color, tap_uv, 0.0) * tap_k;
  186. k_accum += tap_k * tap_amount;
  187. color_accum += tap_color * tap_amount;
  188. }
  189. if (k_accum > 0.0) {
  190. color_accum /= k_accum;
  191. }
  192. frag_color = color_accum; ///k_accum;
  193. #endif
  194. #ifdef DOF_NEAR_BLUR
  195. vec4 color_accum = vec4(0.0);
  196. float max_accum = 0.0;
  197. for (int i = 0; i < dof_kernel_size; i++) {
  198. int int_ofs = i - dof_kernel_from;
  199. vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * dof_radius;
  200. float ofs_influence = max(0.0, 1.0 - abs(float(int_ofs)) / float(dof_kernel_from));
  201. float tap_k = dof_kernel[i];
  202. vec4 tap_color = textureLod(source_color, tap_uv, 0.0);
  203. float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r;
  204. tap_depth = tap_depth * 2.0 - 1.0;
  205. #ifdef USE_ORTHOGONAL_PROJECTION
  206. tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
  207. #else
  208. tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near));
  209. #endif
  210. float tap_amount = 1.0 - smoothstep(dof_end, dof_begin, tap_depth);
  211. tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect
  212. #ifdef DOF_NEAR_FIRST_TAP
  213. tap_color.a = 1.0 - smoothstep(dof_end, dof_begin, tap_depth);
  214. #endif
  215. max_accum = max(max_accum, tap_amount * ofs_influence);
  216. color_accum += tap_color * tap_k;
  217. }
  218. color_accum.a = max(color_accum.a, sqrt(max_accum));
  219. frag_color = color_accum;
  220. #endif
  221. #ifdef GLOW_FIRST_PASS
  222. float luminance = max(frag_color.r, max(frag_color.g, frag_color.b));
  223. float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, luminance), glow_bloom);
  224. frag_color = min(frag_color * feedback, vec4(luminance_cap));
  225. #endif
  226. }