cubemap_downsampler_raster.glsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2016 Activision Publishing, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the Software
  8. // is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. // SOFTWARE.
  20. /* clang-format off */
  21. #[vertex]
  22. #version 450
  23. #VERSION_DEFINES
  24. #include "cubemap_downsampler_inc.glsl"
  25. layout(location = 0) out vec2 uv_interp;
  26. /* clang-format on */
  27. void main() {
  28. vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  29. gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  30. uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0 * float(params.face_size); // saturate(x) * 2.0
  31. }
  32. /* clang-format off */
  33. #[fragment]
  34. #version 450
  35. #VERSION_DEFINES
  36. #include "cubemap_downsampler_inc.glsl"
  37. layout(set = 0, binding = 0) uniform samplerCube source_cubemap;
  38. layout(location = 0) in vec2 uv_interp;
  39. layout(location = 0) out vec4 frag_color;
  40. /* clang-format on */
  41. void main() {
  42. // Converted from compute shader which uses absolute coordinates.
  43. // Could possibly simplify this
  44. float face_size = float(params.face_size);
  45. float inv_face_size = 1.0 / face_size;
  46. vec2 id = floor(uv_interp);
  47. float u1 = (id.x * 2.0 + 1.0 + 0.75) * inv_face_size - 1.0;
  48. float u0 = (id.x * 2.0 + 1.0 - 0.75) * inv_face_size - 1.0;
  49. float v0 = (id.y * 2.0 + 1.0 - 0.75) * -inv_face_size + 1.0;
  50. float v1 = (id.y * 2.0 + 1.0 + 0.75) * -inv_face_size + 1.0;
  51. float weights[4];
  52. weights[0] = calcWeight(u0, v0);
  53. weights[1] = calcWeight(u1, v0);
  54. weights[2] = calcWeight(u0, v1);
  55. weights[3] = calcWeight(u1, v1);
  56. const float wsum = 0.5 / (weights[0] + weights[1] + weights[2] + weights[3]);
  57. for (int i = 0; i < 4; i++) {
  58. weights[i] = weights[i] * wsum + .125;
  59. }
  60. vec3 dir;
  61. vec4 color;
  62. switch (params.face_id) {
  63. case 0:
  64. get_dir_0(dir, u0, v0);
  65. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  66. get_dir_0(dir, u1, v0);
  67. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  68. get_dir_0(dir, u0, v1);
  69. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  70. get_dir_0(dir, u1, v1);
  71. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  72. break;
  73. case 1:
  74. get_dir_1(dir, u0, v0);
  75. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  76. get_dir_1(dir, u1, v0);
  77. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  78. get_dir_1(dir, u0, v1);
  79. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  80. get_dir_1(dir, u1, v1);
  81. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  82. break;
  83. case 2:
  84. get_dir_2(dir, u0, v0);
  85. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  86. get_dir_2(dir, u1, v0);
  87. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  88. get_dir_2(dir, u0, v1);
  89. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  90. get_dir_2(dir, u1, v1);
  91. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  92. break;
  93. case 3:
  94. get_dir_3(dir, u0, v0);
  95. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  96. get_dir_3(dir, u1, v0);
  97. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  98. get_dir_3(dir, u0, v1);
  99. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  100. get_dir_3(dir, u1, v1);
  101. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  102. break;
  103. case 4:
  104. get_dir_4(dir, u0, v0);
  105. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  106. get_dir_4(dir, u1, v0);
  107. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  108. get_dir_4(dir, u0, v1);
  109. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  110. get_dir_4(dir, u1, v1);
  111. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  112. break;
  113. default:
  114. get_dir_5(dir, u0, v0);
  115. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  116. get_dir_5(dir, u1, v0);
  117. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  118. get_dir_5(dir, u0, v1);
  119. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  120. get_dir_5(dir, u1, v1);
  121. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  122. break;
  123. }
  124. frag_color = color;
  125. }