cubemap_downsampler.glsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #[compute]
  21. #version 450
  22. #VERSION_DEFINES
  23. #define BLOCK_SIZE 8
  24. layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
  25. layout(set = 0, binding = 0) uniform samplerCube source_cubemap;
  26. layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly imageCube dest_cubemap;
  27. #include "cubemap_downsampler_inc.glsl"
  28. void main() {
  29. uvec3 id = gl_GlobalInvocationID;
  30. uint face_size = params.face_size;
  31. if (id.x < face_size && id.y < face_size) {
  32. float inv_face_size = 1.0 / float(face_size);
  33. float u0 = (float(id.x) * 2.0 + 1.0 - 0.75) * inv_face_size - 1.0;
  34. float u1 = (float(id.x) * 2.0 + 1.0 + 0.75) * inv_face_size - 1.0;
  35. float v0 = (float(id.y) * 2.0 + 1.0 - 0.75) * -inv_face_size + 1.0;
  36. float v1 = (float(id.y) * 2.0 + 1.0 + 0.75) * -inv_face_size + 1.0;
  37. float weights[4];
  38. weights[0] = calcWeight(u0, v0);
  39. weights[1] = calcWeight(u1, v0);
  40. weights[2] = calcWeight(u0, v1);
  41. weights[3] = calcWeight(u1, v1);
  42. const float wsum = 0.5 / (weights[0] + weights[1] + weights[2] + weights[3]);
  43. for (int i = 0; i < 4; i++) {
  44. weights[i] = weights[i] * wsum + .125;
  45. }
  46. vec3 dir;
  47. vec4 color;
  48. switch (id.z) {
  49. case 0:
  50. get_dir_0(dir, u0, v0);
  51. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  52. get_dir_0(dir, u1, v0);
  53. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  54. get_dir_0(dir, u0, v1);
  55. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  56. get_dir_0(dir, u1, v1);
  57. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  58. break;
  59. case 1:
  60. get_dir_1(dir, u0, v0);
  61. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  62. get_dir_1(dir, u1, v0);
  63. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  64. get_dir_1(dir, u0, v1);
  65. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  66. get_dir_1(dir, u1, v1);
  67. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  68. break;
  69. case 2:
  70. get_dir_2(dir, u0, v0);
  71. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  72. get_dir_2(dir, u1, v0);
  73. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  74. get_dir_2(dir, u0, v1);
  75. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  76. get_dir_2(dir, u1, v1);
  77. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  78. break;
  79. case 3:
  80. get_dir_3(dir, u0, v0);
  81. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  82. get_dir_3(dir, u1, v0);
  83. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  84. get_dir_3(dir, u0, v1);
  85. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  86. get_dir_3(dir, u1, v1);
  87. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  88. break;
  89. case 4:
  90. get_dir_4(dir, u0, v0);
  91. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  92. get_dir_4(dir, u1, v0);
  93. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  94. get_dir_4(dir, u0, v1);
  95. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  96. get_dir_4(dir, u1, v1);
  97. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  98. break;
  99. default:
  100. get_dir_5(dir, u0, v0);
  101. color = textureLod(source_cubemap, normalize(dir), 0.0) * weights[0];
  102. get_dir_5(dir, u1, v0);
  103. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[1];
  104. get_dir_5(dir, u0, v1);
  105. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[2];
  106. get_dir_5(dir, u1, v1);
  107. color += textureLod(source_cubemap, normalize(dir), 0.0) * weights[3];
  108. break;
  109. }
  110. imageStore(dest_cubemap, ivec3(id), color);
  111. }
  112. }