bokeh_dof_raster.glsl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  10. gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  11. uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.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 * 2.0 - 1.0;
  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. if (params.use_physical_near) {
  49. // Physically-based.
  50. float d = abs(params.blur_near_begin - depth);
  51. return -(d / (params.blur_near_begin - d)) * params.blur_size_near - DEPTH_GAP; // Near blur is negative.
  52. } else {
  53. // Non-physically-based.
  54. return -(1.0 - smoothstep(params.blur_near_end, params.blur_near_begin, depth)) * params.blur_size - DEPTH_GAP; // Near blur is negative.
  55. }
  56. }
  57. if (params.blur_far_active && depth > params.blur_far_begin) {
  58. if (params.use_physical_far) {
  59. // Physically-based.
  60. float d = abs(params.blur_far_begin - depth);
  61. return (d / (params.blur_far_begin + d)) * params.blur_size_far + DEPTH_GAP;
  62. } else {
  63. // Non-physically-based.
  64. return smoothstep(params.blur_far_begin, params.blur_far_end, depth) * params.blur_size + DEPTH_GAP;
  65. }
  66. }
  67. return 0.0;
  68. }
  69. #endif
  70. #if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL)
  71. vec4 weighted_filter_dir(vec2 dir, vec2 uv, vec2 pixel_size) {
  72. dir *= pixel_size;
  73. vec4 color = texture(source_color, uv);
  74. color.a = texture(source_weight, uv).r;
  75. vec4 accum = color;
  76. float total = 1.0;
  77. float blur_scale = params.blur_size / float(params.blur_steps);
  78. if (params.use_jitter) {
  79. uv += dir * (hash12n(uv + params.jitter_seed) - 0.5);
  80. }
  81. for (int i = -params.blur_steps; i <= params.blur_steps; i++) {
  82. if (i == 0) {
  83. continue;
  84. }
  85. float radius = float(i) * blur_scale;
  86. vec2 suv = uv + dir * radius;
  87. radius = abs(radius);
  88. vec4 sample_color = texture(source_color, suv);
  89. sample_color.a = texture(source_weight, suv).r;
  90. float limit;
  91. if (sample_color.a < color.a) {
  92. limit = abs(sample_color.a);
  93. } else {
  94. limit = abs(color.a);
  95. }
  96. limit -= DEPTH_GAP;
  97. float m = smoothstep(radius - 0.5, radius + 0.5, limit);
  98. accum += mix(color, sample_color, m);
  99. total += 1.0;
  100. }
  101. return accum / total;
  102. }
  103. #endif
  104. void main() {
  105. vec2 pixel_size = 1.0 / vec2(params.size);
  106. vec2 uv = uv_interp;
  107. #ifdef MODE_GEN_BLUR_SIZE
  108. uv += pixel_size * 0.5;
  109. float center_depth = get_depth_at_pos(uv);
  110. weight = get_blur_size(center_depth);
  111. #endif
  112. #ifdef MODE_BOKEH_BOX
  113. //pixel_size*=0.5; //resolution is doubled
  114. if (params.second_pass || !params.half_size) {
  115. uv += pixel_size * 0.5; //half pixel to read centers
  116. } else {
  117. uv += pixel_size * 0.25; //half pixel to read centers from full res
  118. }
  119. float alpha = texture(source_color, uv).a; // retain this
  120. vec2 dir = (params.second_pass ? vec2(0.0, 1.0) : vec2(1.0, 0.0));
  121. vec4 color = weighted_filter_dir(dir, uv, pixel_size);
  122. frag_color = color;
  123. frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size
  124. #ifdef OUTPUT_WEIGHT
  125. weight = color.a;
  126. #endif
  127. #endif
  128. #ifdef MODE_BOKEH_HEXAGONAL
  129. //pixel_size*=0.5; //resolution is doubled
  130. if (params.second_pass || !params.half_size) {
  131. uv += pixel_size * 0.5; //half pixel to read centers
  132. } else {
  133. uv += pixel_size * 0.25; //half pixel to read centers from full res
  134. }
  135. float alpha = texture(source_color, uv).a; // retain this
  136. vec2 dir = (params.second_pass ? normalize(vec2(1.0, 0.577350269189626)) : vec2(0.0, 1.0));
  137. vec4 color = weighted_filter_dir(dir, uv, pixel_size);
  138. if (params.second_pass) {
  139. dir = normalize(vec2(-1.0, 0.577350269189626));
  140. vec4 color2 = weighted_filter_dir(dir, uv, pixel_size);
  141. color.rgb = min(color.rgb, color2.rgb);
  142. color.a = (color.a + color2.a) * 0.5;
  143. }
  144. frag_color = color;
  145. frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size
  146. #ifdef OUTPUT_WEIGHT
  147. weight = color.a;
  148. #endif
  149. #endif
  150. #ifdef MODE_BOKEH_CIRCULAR
  151. if (params.half_size) {
  152. pixel_size *= 0.5; //resolution is doubled
  153. }
  154. uv += pixel_size * 0.5; //half pixel to read centers
  155. vec4 color = texture(source_color, uv);
  156. float alpha = color.a; // retain this
  157. color.a = texture(source_weight, uv).r;
  158. vec4 color_accum = color;
  159. float accum = 1.0;
  160. float radius = params.blur_scale;
  161. for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) {
  162. vec2 uv_adj = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius;
  163. vec4 sample_color = texture(source_color, uv_adj);
  164. sample_color.a = texture(source_weight, uv_adj).r;
  165. float limit = abs(sample_color.a);
  166. if (sample_color.a > color.a) {
  167. limit = clamp(limit, 0.0, abs(color.a) * 2.0);
  168. }
  169. limit -= DEPTH_GAP;
  170. float m = smoothstep(radius - 0.5, radius + 0.5, limit);
  171. color_accum += mix(color_accum / accum, sample_color, m);
  172. accum += 1.0;
  173. radius += params.blur_scale / radius;
  174. }
  175. color_accum = color_accum / accum;
  176. frag_color.rgb = color_accum.rgb;
  177. frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size
  178. #ifdef OUTPUT_WEIGHT
  179. weight = color_accum.a;
  180. #endif
  181. #endif
  182. #ifdef MODE_COMPOSITE_BOKEH
  183. frag_color.rgb = texture(source_color, uv).rgb;
  184. float center_weigth = texture(source_weight, uv).r;
  185. float sample_weight = texture(original_weight, uv).r;
  186. float mix_amount;
  187. if (sample_weight < center_weigth) {
  188. mix_amount = min(1.0, max(0.0, max(abs(center_weigth), abs(sample_weight)) - DEPTH_GAP));
  189. } else {
  190. mix_amount = min(1.0, max(0.0, abs(center_weigth) - DEPTH_GAP));
  191. }
  192. // let alpha blending take care of mixing
  193. frag_color.a = mix_amount;
  194. #endif
  195. }