2
0

bokeh_dof_raster.glsl 6.3 KB

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