fsr_upscale.glsl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**************************************************************************/
  2. /* fsr_upscale.glsl */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #[compute]
  31. #version 450
  32. #VERSION_DEFINES
  33. #define A_GPU
  34. #define A_GLSL
  35. #ifdef MODE_FSR_UPSCALE_NORMAL
  36. #define A_HALF
  37. #endif
  38. #include "thirdparty/amd-fsr/ffx_a.h"
  39. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  40. layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly image2D fsr_image;
  41. layout(set = 0, binding = 0) uniform sampler2D source_image;
  42. #define FSR_UPSCALE_PASS_TYPE_EASU 0
  43. #define FSR_UPSCALE_PASS_TYPE_RCAS 1
  44. layout(push_constant, std430) uniform Params {
  45. float resolution_width;
  46. float resolution_height;
  47. float upscaled_width;
  48. float upscaled_height;
  49. float sharpness;
  50. int pass;
  51. }
  52. params;
  53. AU4 Const0, Const1, Const2, Const3;
  54. #ifdef MODE_FSR_UPSCALE_FALLBACK
  55. #define FSR_EASU_F
  56. AF4 FsrEasuRF(AF2 p) {
  57. AF4 res = textureGather(source_image, p, 0);
  58. return res;
  59. }
  60. AF4 FsrEasuGF(AF2 p) {
  61. AF4 res = textureGather(source_image, p, 1);
  62. return res;
  63. }
  64. AF4 FsrEasuBF(AF2 p) {
  65. AF4 res = textureGather(source_image, p, 2);
  66. return res;
  67. }
  68. #define FSR_RCAS_F
  69. AF4 FsrRcasLoadF(ASU2 p) {
  70. return AF4(texelFetch(source_image, ASU2(p), 0));
  71. }
  72. void FsrRcasInputF(inout AF1 r, inout AF1 g, inout AF1 b) {}
  73. #else
  74. #define FSR_EASU_H
  75. AH4 FsrEasuRH(AF2 p) {
  76. AH4 res = AH4(textureGather(source_image, p, 0));
  77. return res;
  78. }
  79. AH4 FsrEasuGH(AF2 p) {
  80. AH4 res = AH4(textureGather(source_image, p, 1));
  81. return res;
  82. }
  83. AH4 FsrEasuBH(AF2 p) {
  84. AH4 res = AH4(textureGather(source_image, p, 2));
  85. return res;
  86. }
  87. #define FSR_RCAS_H
  88. AH4 FsrRcasLoadH(ASW2 p) {
  89. return AH4(texelFetch(source_image, ASU2(p), 0));
  90. }
  91. void FsrRcasInputH(inout AH1 r, inout AH1 g, inout AH1 b) {}
  92. #endif
  93. #include "thirdparty/amd-fsr/ffx_fsr1.h"
  94. void fsr_easu_pass(AU2 pos) {
  95. #ifdef MODE_FSR_UPSCALE_NORMAL
  96. AH3 Gamma2Color = AH3(0, 0, 0);
  97. FsrEasuH(Gamma2Color, pos, Const0, Const1, Const2, Const3);
  98. imageStore(fsr_image, ASU2(pos), AH4(Gamma2Color, 1));
  99. #else
  100. AF3 Gamma2Color = AF3(0, 0, 0);
  101. FsrEasuF(Gamma2Color, pos, Const0, Const1, Const2, Const3);
  102. imageStore(fsr_image, ASU2(pos), AF4(Gamma2Color, 1));
  103. #endif
  104. }
  105. void fsr_rcas_pass(AU2 pos) {
  106. #ifdef MODE_FSR_UPSCALE_NORMAL
  107. AH3 Gamma2Color = AH3(0, 0, 0);
  108. FsrRcasH(Gamma2Color.r, Gamma2Color.g, Gamma2Color.b, pos, Const0);
  109. imageStore(fsr_image, ASU2(pos), AH4(Gamma2Color, 1));
  110. #else
  111. AF3 Gamma2Color = AF3(0, 0, 0);
  112. FsrRcasF(Gamma2Color.r, Gamma2Color.g, Gamma2Color.b, pos, Const0);
  113. imageStore(fsr_image, ASU2(pos), AF4(Gamma2Color, 1));
  114. #endif
  115. }
  116. void fsr_pass(AU2 pos) {
  117. if (params.pass == FSR_UPSCALE_PASS_TYPE_EASU) {
  118. fsr_easu_pass(pos);
  119. } else if (params.pass == FSR_UPSCALE_PASS_TYPE_RCAS) {
  120. fsr_rcas_pass(pos);
  121. }
  122. }
  123. void main() {
  124. // Clang does not like unused functions. If ffx_a.h is included in the binary, clang will throw a fit and not compile so we must configure FSR in this shader
  125. if (params.pass == FSR_UPSCALE_PASS_TYPE_EASU) {
  126. FsrEasuCon(Const0, Const1, Const2, Const3, params.resolution_width, params.resolution_height, params.resolution_width, params.resolution_height, params.upscaled_width, params.upscaled_height);
  127. } else if (params.pass == FSR_UPSCALE_PASS_TYPE_RCAS) {
  128. FsrRcasCon(Const0, params.sharpness);
  129. }
  130. AU2 gxy = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
  131. fsr_pass(gxy);
  132. gxy.x += 8u;
  133. fsr_pass(gxy);
  134. gxy.y += 8u;
  135. fsr_pass(gxy);
  136. gxy.x -= 8u;
  137. fsr_pass(gxy);
  138. }