copy_to_fb.glsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #[vertex]
  2. #version 450
  3. VERSION_DEFINES
  4. layout(location = 0) out vec2 uv_interp;
  5. layout(push_constant, binding = 1, std430) uniform Params {
  6. vec4 section;
  7. vec2 pixel_size;
  8. bool flip_y;
  9. bool use_section;
  10. bool force_luminance;
  11. uint pad[3];
  12. }
  13. params;
  14. void main() {
  15. 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));
  16. uv_interp = base_arr[gl_VertexIndex];
  17. vec2 vpos = uv_interp;
  18. if (params.use_section) {
  19. vpos = params.section.xy + vpos * params.section.zw;
  20. }
  21. gl_Position = vec4(vpos * 2.0 - 1.0, 0.0, 1.0);
  22. if (params.flip_y) {
  23. uv_interp.y = 1.0 - uv_interp.y;
  24. }
  25. }
  26. #[fragment]
  27. #version 450
  28. VERSION_DEFINES
  29. layout(push_constant, binding = 1, std430) uniform Params {
  30. vec4 section;
  31. vec2 pixel_size;
  32. bool flip_y;
  33. bool use_section;
  34. bool force_luminance;
  35. bool alpha_to_zero;
  36. bool srgb;
  37. uint pad;
  38. }
  39. params;
  40. layout(location = 0) in vec2 uv_interp;
  41. layout(set = 0, binding = 0) uniform sampler2D source_color;
  42. #ifdef MODE_TWO_SOURCES
  43. layout(set = 1, binding = 0) uniform sampler2D source_color2;
  44. #endif
  45. layout(location = 0) out vec4 frag_color;
  46. vec3 linear_to_srgb(vec3 color) {
  47. //if going to srgb, clamp from 0 to 1.
  48. color = clamp(color, vec3(0.0), vec3(1.0));
  49. const vec3 a = vec3(0.055f);
  50. return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
  51. }
  52. void main() {
  53. vec2 uv = uv_interp;
  54. #ifdef MODE_PANORAMA_TO_DP
  55. //obtain normal from dual paraboloid uv
  56. #define M_PI 3.14159265359
  57. float side;
  58. uv.y = modf(uv.y * 2.0, side);
  59. side = side * 2.0 - 1.0;
  60. vec3 normal = vec3(uv * 2.0 - 1.0, 0.0);
  61. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  62. normal *= -side;
  63. normal = normalize(normal);
  64. //now convert normal to panorama uv
  65. vec2 st = vec2(atan(normal.x, normal.z), acos(normal.y));
  66. if (st.x < 0.0) {
  67. st.x += M_PI * 2.0;
  68. }
  69. uv = st / vec2(M_PI * 2.0, M_PI);
  70. if (side < 0.0) {
  71. //uv.y = 1.0 - uv.y;
  72. uv = 1.0 - uv;
  73. }
  74. #endif
  75. vec4 color = textureLod(source_color, uv, 0.0);
  76. #ifdef MODE_TWO_SOURCES
  77. color += textureLod(source_color2, uv, 0.0);
  78. #endif
  79. if (params.force_luminance) {
  80. color.rgb = vec3(max(max(color.r, color.g), color.b));
  81. }
  82. if (params.alpha_to_zero) {
  83. color.rgb *= color.a;
  84. }
  85. if (params.srgb) {
  86. color.rgb = linear_to_srgb(color.rgb);
  87. }
  88. frag_color = color;
  89. }