bloom_downsample_pass.kong 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #[set(everything)]
  2. const constants: {
  3. screen_size_inv: float2;
  4. current_mip_level: int;
  5. };
  6. #[set(everything)]
  7. const sampler_linear: sampler;
  8. #[set(everything)]
  9. const tex: tex2d;
  10. struct vert_in {
  11. pos: float2;
  12. }
  13. struct vert_out {
  14. pos: float4;
  15. tex: float2;
  16. }
  17. const bloom_knee: float = 0.5;
  18. const bloom_threshold: float = 0.8;
  19. const epsilon: float = 0.000062;
  20. fun bloom_downsample_pass_vert(input: vert_in): vert_out {
  21. var output: vert_out;
  22. output.tex = input.pos.xy * 0.5 + 0.5;
  23. output.tex.y = 1.0 - output.tex.y;
  24. output.pos = float4(input.pos.xy, 0.0, 1.0);
  25. return output;
  26. }
  27. fun downsample_dual_filter(tex_coord: float2, texel_size: float2): float3 {
  28. var delta: float3 = float3(texel_size.xy, texel_size.x) * float3(0.5, 0.5, -0.5);
  29. var result: float3;
  30. result = sample_lod(tex, sampler_linear, tex_coord, 0.0).rgb * 4.0;
  31. result += sample_lod(tex, sampler_linear, tex_coord - delta.xy, 0.0).rgb;
  32. result += sample_lod(tex, sampler_linear, tex_coord - delta.zy, 0.0).rgb;
  33. result += sample_lod(tex, sampler_linear, tex_coord + delta.zy, 0.0).rgb;
  34. result += sample_lod(tex, sampler_linear, tex_coord + delta.xy, 0.0).rgb;
  35. return result * (1.0 / 8.0);
  36. }
  37. fun bloom_downsample_pass_frag(input: vert_out): float4 {
  38. var color: float4;
  39. color.rgb = downsample_dual_filter(input.tex, constants.screen_size_inv);
  40. if (constants.current_mip_level == 0) {
  41. var brightness: float = max(color.r, max(color.g, color.b));
  42. var softening_curve: float = brightness - bloom_threshold + bloom_knee;
  43. softening_curve = clamp(softening_curve, 0.0, 2.0 * bloom_knee);
  44. softening_curve = softening_curve * softening_curve / (4.0 * bloom_knee + epsilon);
  45. var contribution_factor: float = max(softening_curve, brightness - bloom_threshold);
  46. contribution_factor /= max(epsilon, brightness);
  47. color.rgb = color.rgb * contribution_factor;
  48. }
  49. color.a = 1.0;
  50. return color;
  51. }
  52. #[pipe]
  53. struct pipe {
  54. vertex = bloom_downsample_pass_vert;
  55. fragment = bloom_downsample_pass_frag;
  56. }