bloom_upsample_pass.kong 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #[set(everything)]
  2. const constants: {
  3. screen_size_inv: float2;
  4. current_mip_level: int;
  5. sample_scale: float;
  6. };
  7. #[set(everything)]
  8. const sampler_linear: sampler;
  9. #[set(everything)]
  10. const tex: tex2d;
  11. struct vert_in {
  12. pos: float2;
  13. }
  14. struct vert_out {
  15. pos: float4;
  16. tex: float2;
  17. }
  18. const bloom_strength: float = 0.02;
  19. fun bloom_upsample_pass_vert(input: vert_in): vert_out {
  20. var output: vert_out;
  21. output.tex = input.pos.xy * 0.5 + 0.5;
  22. output.tex.y = 1.0 - output.tex.y;
  23. output.pos = float4(input.pos.xy, 0.0, 1.0);
  24. return output;
  25. }
  26. fun upsample_dual_filter(tex_coord: float2, texel_size: float2): float3 {
  27. var delta: float2 = texel_size * constants.sample_scale;
  28. var result: float3;
  29. result = sample_lod(tex, sampler_linear, tex_coord + float2(-delta.x * 2.0, 0.0), 0.0).rgb;
  30. result += sample_lod(tex, sampler_linear, tex_coord + float2(-delta.x, delta.y), 0.0).rgb * 2.0;
  31. result += sample_lod(tex, sampler_linear, tex_coord + float2(0.0, delta.y * 2.0), 0.0).rgb;
  32. result += sample_lod(tex, sampler_linear, tex_coord + delta, 0.0).rgb * 2.0;
  33. result += sample_lod(tex, sampler_linear, tex_coord + float2(delta.x * 2.0, 0.0), 0.0).rgb;
  34. result += sample_lod(tex, sampler_linear, tex_coord + float2(delta.x, -delta.y), 0.0).rgb * 2.0;
  35. result += sample_lod(tex, sampler_linear, tex_coord + float2(0.0, -delta.y * 2.0), 0.0).rgb;
  36. result += sample_lod(tex, sampler_linear, tex_coord - delta, 0.0).rgb * 2.0;
  37. return result * (1.0 / 12.0);
  38. }
  39. fun bloom_upsample_pass_frag(input: vert_out): float4 {
  40. var color: float4;
  41. color.rgb = upsample_dual_filter(input.tex, constants.screen_size_inv);
  42. if (constants.current_mip_level == 0) {
  43. color.rgb = color.rgb * float3(bloom_strength, bloom_strength, bloom_strength);
  44. }
  45. color.a = 1.0;
  46. return color;
  47. }
  48. #[pipe]
  49. struct pipe {
  50. vertex = bloom_upsample_pass_vert;
  51. fragment = bloom_upsample_pass_frag;
  52. }