effect_blur.glsl 8.7 KB

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