bokeh_dof_raster.glsl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* clang-format off */
  2. #[vertex]
  3. #version 450
  4. #VERSION_DEFINES
  5. #include "bokeh_dof_inc.glsl"
  6. layout(location = 0) out vec2 uv_interp;
  7. /* clang-format on */
  8. void main() {
  9. // old code, ARM driver bug on Mali-GXXx GPUs and Vulkan API 1.3.xxx
  10. // https://github.com/godotengine/godot/pull/92817#issuecomment-2168625982
  11. //vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  12. //gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  13. //uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  14. vec2 vertex_base;
  15. if (gl_VertexIndex == 0) {
  16. vertex_base = vec2(-1.0, -1.0);
  17. } else if (gl_VertexIndex == 1) {
  18. vertex_base = vec2(-1.0, 3.0);
  19. } else {
  20. vertex_base = vec2(3.0, -1.0);
  21. }
  22. gl_Position = vec4(vertex_base, 0.0, 1.0);
  23. uv_interp = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  24. }
  25. /* clang-format off */
  26. #[fragment]
  27. #version 450
  28. #VERSION_DEFINES
  29. #include "bokeh_dof_inc.glsl"
  30. layout(location = 0) in vec2 uv_interp;
  31. /* clang-format on */
  32. #ifdef MODE_GEN_BLUR_SIZE
  33. layout(location = 0) out float weight;
  34. layout(set = 0, binding = 0) uniform sampler2D source_depth;
  35. #else
  36. layout(location = 0) out vec4 frag_color;
  37. #ifdef OUTPUT_WEIGHT
  38. layout(location = 1) out float weight;
  39. #endif
  40. layout(set = 0, binding = 0) uniform sampler2D source_color;
  41. layout(set = 1, binding = 0) uniform sampler2D source_weight;
  42. #ifdef MODE_COMPOSITE_BOKEH
  43. layout(set = 2, binding = 0) uniform sampler2D original_weight;
  44. #endif
  45. #endif
  46. //DOF
  47. // Bokeh single pass implementation based on https://tuxedolabs.blogspot.com/2018/05/bokeh-depth-of-field-in-single-pass.html
  48. #ifdef MODE_GEN_BLUR_SIZE
  49. float get_depth_at_pos(vec2 uv) {
  50. float depth = textureLod(source_depth, uv, 0.0).x * 2.0 - 1.0;
  51. if (params.orthogonal) {
  52. depth = -(depth * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0;
  53. } else {
  54. depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near + depth * (params.z_far - params.z_near));
  55. }
  56. return depth;
  57. }
  58. float get_blur_size(float depth) {
  59. if (params.blur_near_active && depth < params.blur_near_begin) {
  60. if (params.use_physical_near) {
  61. // Physically-based.
  62. float d = abs(params.blur_near_begin - depth);
  63. return -(d / (params.blur_near_begin - d)) * params.blur_size_near - DEPTH_GAP; // Near blur is negative.
  64. } else {
  65. // Non-physically-based.
  66. return -(1.0 - smoothstep(params.blur_near_end, params.blur_near_begin, depth)) * params.blur_size - DEPTH_GAP; // Near blur is negative.
  67. }
  68. }
  69. if (params.blur_far_active && depth > params.blur_far_begin) {
  70. if (params.use_physical_far) {
  71. // Physically-based.
  72. float d = abs(params.blur_far_begin - depth);
  73. return (d / (params.blur_far_begin + d)) * params.blur_size_far + DEPTH_GAP;
  74. } else {
  75. // Non-physically-based.
  76. return smoothstep(params.blur_far_begin, params.blur_far_end, depth) * params.blur_size + DEPTH_GAP;
  77. }
  78. }
  79. return 0.0;
  80. }
  81. #endif
  82. #if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL)
  83. vec4 weighted_filter_dir(vec2 dir, vec2 uv, vec2 pixel_size) {
  84. dir *= pixel_size;
  85. vec4 color = texture(source_color, uv);
  86. color.a = texture(source_weight, uv).r;
  87. vec4 accum = color;
  88. float total = 1.0;
  89. float blur_scale = params.blur_size / float(params.blur_steps);
  90. if (params.use_jitter) {
  91. uv += dir * (hash12n(uv + params.jitter_seed) - 0.5);
  92. }
  93. for (int i = -params.blur_steps; i <= params.blur_steps; i++) {
  94. if (i == 0) {
  95. continue;
  96. }
  97. float radius = float(i) * blur_scale;
  98. vec2 suv = uv + dir * radius;
  99. radius = abs(radius);
  100. vec4 sample_color = texture(source_color, suv);
  101. sample_color.a = texture(source_weight, suv).r;
  102. float limit;
  103. if (sample_color.a < color.a) {
  104. limit = abs(sample_color.a);
  105. } else {
  106. limit = abs(color.a);
  107. }
  108. limit -= DEPTH_GAP;
  109. float m = smoothstep(radius - 0.5, radius + 0.5, limit);
  110. accum += mix(color, sample_color, m);
  111. total += 1.0;
  112. }
  113. return accum / total;
  114. }
  115. #endif
  116. void main() {
  117. vec2 pixel_size = 1.0 / vec2(params.size);
  118. vec2 uv = uv_interp;
  119. #ifdef MODE_GEN_BLUR_SIZE
  120. uv += pixel_size * 0.5;
  121. float center_depth = get_depth_at_pos(uv);
  122. weight = get_blur_size(center_depth);
  123. #endif
  124. #ifdef MODE_BOKEH_BOX
  125. //pixel_size*=0.5; //resolution is doubled
  126. if (params.second_pass || !params.half_size) {
  127. uv += pixel_size * 0.5; //half pixel to read centers
  128. } else {
  129. uv += pixel_size * 0.25; //half pixel to read centers from full res
  130. }
  131. float alpha = texture(source_color, uv).a; // retain this
  132. vec2 dir = (params.second_pass ? vec2(0.0, 1.0) : vec2(1.0, 0.0));
  133. vec4 color = weighted_filter_dir(dir, uv, pixel_size);
  134. frag_color = color;
  135. frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size
  136. #ifdef OUTPUT_WEIGHT
  137. weight = color.a;
  138. #endif
  139. #endif
  140. #ifdef MODE_BOKEH_HEXAGONAL
  141. //pixel_size*=0.5; //resolution is doubled
  142. if (params.second_pass || !params.half_size) {
  143. uv += pixel_size * 0.5; //half pixel to read centers
  144. } else {
  145. uv += pixel_size * 0.25; //half pixel to read centers from full res
  146. }
  147. float alpha = texture(source_color, uv).a; // retain this
  148. vec2 dir = (params.second_pass ? normalize(vec2(1.0, 0.577350269189626)) : vec2(0.0, 1.0));
  149. vec4 color = weighted_filter_dir(dir, uv, pixel_size);
  150. if (params.second_pass) {
  151. dir = normalize(vec2(-1.0, 0.577350269189626));
  152. vec4 color2 = weighted_filter_dir(dir, uv, pixel_size);
  153. color.rgb = min(color.rgb, color2.rgb);
  154. color.a = (color.a + color2.a) * 0.5;
  155. }
  156. frag_color = color;
  157. frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size
  158. #ifdef OUTPUT_WEIGHT
  159. weight = color.a;
  160. #endif
  161. #endif
  162. #ifdef MODE_BOKEH_CIRCULAR
  163. if (params.half_size) {
  164. pixel_size *= 0.5; //resolution is doubled
  165. }
  166. uv += pixel_size * 0.5; //half pixel to read centers
  167. vec4 color = texture(source_color, uv);
  168. float alpha = color.a; // retain this
  169. color.a = texture(source_weight, uv).r;
  170. vec4 color_accum = color;
  171. float accum = 1.0;
  172. float radius = params.blur_scale;
  173. for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) {
  174. vec2 uv_adj = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius;
  175. vec4 sample_color = texture(source_color, uv_adj);
  176. sample_color.a = texture(source_weight, uv_adj).r;
  177. float limit = abs(sample_color.a);
  178. if (sample_color.a > color.a) {
  179. limit = clamp(limit, 0.0, abs(color.a) * 2.0);
  180. }
  181. limit -= DEPTH_GAP;
  182. float m = smoothstep(radius - 0.5, radius + 0.5, limit);
  183. color_accum += mix(color_accum / accum, sample_color, m);
  184. accum += 1.0;
  185. radius += params.blur_scale / radius;
  186. }
  187. color_accum = color_accum / accum;
  188. frag_color.rgb = color_accum.rgb;
  189. frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size
  190. #ifdef OUTPUT_WEIGHT
  191. weight = color_accum.a;
  192. #endif
  193. #endif
  194. #ifdef MODE_COMPOSITE_BOKEH
  195. frag_color.rgb = texture(source_color, uv).rgb;
  196. float center_weight = texture(source_weight, uv).r;
  197. float sample_weight = texture(original_weight, uv).r;
  198. float mix_amount;
  199. if (sample_weight < center_weight) {
  200. mix_amount = min(1.0, max(0.0, max(abs(center_weight), abs(sample_weight)) - DEPTH_GAP));
  201. } else {
  202. mix_amount = min(1.0, max(0.0, abs(center_weight) - DEPTH_GAP));
  203. }
  204. // let alpha blending take care of mixing
  205. frag_color.a = mix_amount;
  206. #endif
  207. }