emit_glsl_floating_point.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string_view>
  5. #include "shader_recompiler/backend/glsl/emit_context.h"
  6. #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
  7. #include "shader_recompiler/frontend/ir/value.h"
  8. #include "shader_recompiler/profile.h"
  9. namespace Shader::Backend::GLSL {
  10. namespace {
  11. void Compare(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::string_view rhs,
  12. std::string_view op, bool ordered) {
  13. ctx.AddU1("{}={}{}{}", inst, lhs, op, rhs, lhs, rhs);
  14. if (ordered) {
  15. ctx.code += fmt::format("&&!isnan({})&&!isnan({})", lhs, rhs);
  16. } else {
  17. ctx.code += fmt::format("||isnan({})||isnan({})", lhs, rhs);
  18. }
  19. ctx.code += ";";
  20. }
  21. } // namespace
  22. void EmitFPAbs16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  23. [[maybe_unused]] std::string_view value) {
  24. throw NotImplementedException("GLSL Instruction");
  25. }
  26. void EmitFPAbs32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  27. ctx.AddF32("{}=abs({});", inst, value);
  28. }
  29. void EmitFPAbs64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  30. ctx.AddF64("{}=abs({});", inst, value);
  31. }
  32. void EmitFPAdd16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  33. [[maybe_unused]] std::string_view a, [[maybe_unused]] std::string_view b) {
  34. throw NotImplementedException("GLSL Instruction");
  35. }
  36. void EmitFPAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  37. ctx.AddF32("{}=float({})+float({});", inst, a, b);
  38. }
  39. void EmitFPAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  40. ctx.AddF64("{}=double({})+double({});", inst, a, b);
  41. }
  42. void EmitFPFma16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  43. [[maybe_unused]] std::string_view a, [[maybe_unused]] std::string_view b,
  44. [[maybe_unused]] std::string_view c) {
  45. throw NotImplementedException("GLSL Instruction");
  46. }
  47. void EmitFPFma32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b,
  48. std::string_view c) {
  49. ctx.AddF32("{}=fma({},{},{});", inst, a, b, c);
  50. }
  51. void EmitFPFma64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b,
  52. std::string_view c) {
  53. ctx.AddF64("{}=fma({},{},{});", inst, a, b, c);
  54. }
  55. void EmitFPMax32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  56. ctx.AddF32("{}=max({},{});", inst, a, b);
  57. }
  58. void EmitFPMax64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  59. ctx.AddF64("{}=max({},{});", inst, a, b);
  60. }
  61. void EmitFPMin32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  62. ctx.AddF32("{}=min({},{});", inst, a, b);
  63. }
  64. void EmitFPMin64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  65. ctx.AddF64("{}=min({},{});", inst, a, b);
  66. }
  67. void EmitFPMul16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  68. [[maybe_unused]] std::string_view a, [[maybe_unused]] std::string_view b) {
  69. throw NotImplementedException("GLSL Instruction");
  70. }
  71. void EmitFPMul32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  72. ctx.AddF32("{}={}*{};", inst, a, b);
  73. }
  74. void EmitFPMul64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
  75. ctx.AddF64("{}={}*{};", inst, a, b);
  76. }
  77. void EmitFPNeg16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  78. [[maybe_unused]] std::string_view value) {
  79. throw NotImplementedException("GLSL Instruction");
  80. }
  81. void EmitFPNeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  82. ctx.AddF32("{}=-({});", inst, value);
  83. }
  84. void EmitFPNeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  85. ctx.AddF64("{}=-({});", inst, value);
  86. }
  87. void EmitFPSin(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  88. ctx.AddF32("{}=sin({});", inst, value);
  89. }
  90. void EmitFPCos(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  91. ctx.AddF32("{}=cos({});", inst, value);
  92. }
  93. void EmitFPExp2(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  94. ctx.AddF32("{}=exp2({});", inst, value);
  95. }
  96. void EmitFPLog2(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  97. ctx.AddF32("{}=log2({});", inst, value);
  98. }
  99. void EmitFPRecip32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  100. ctx.AddF32("{}=1/{};", inst, value);
  101. }
  102. void EmitFPRecip64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  103. ctx.AddF64("{}=1/{};", inst, value);
  104. }
  105. void EmitFPRecipSqrt32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  106. [[maybe_unused]] std::string_view value) {
  107. ctx.AddF32("{}=1/sqrt({});", inst, value);
  108. }
  109. void EmitFPRecipSqrt64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  110. [[maybe_unused]] std::string_view value) {
  111. throw NotImplementedException("GLSL Instruction");
  112. }
  113. void EmitFPSqrt(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  114. ctx.AddF32("{}=sqrt({});", inst, value);
  115. }
  116. void EmitFPSaturate16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  117. [[maybe_unused]] std::string_view value) {
  118. throw NotImplementedException("GLSL Instruction");
  119. }
  120. void EmitFPSaturate32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  121. ctx.AddF32("{}=min(max({},0.0),1.0);", inst, value);
  122. }
  123. void EmitFPSaturate64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  124. ctx.AddF64("{}=min(max({},0.0),1.0);", inst, value);
  125. }
  126. void EmitFPClamp16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  127. [[maybe_unused]] std::string_view value,
  128. [[maybe_unused]] std::string_view min_value,
  129. [[maybe_unused]] std::string_view max_value) {
  130. throw NotImplementedException("GLSL Instruction");
  131. }
  132. void EmitFPClamp32(EmitContext& ctx, IR::Inst& inst, std::string_view value,
  133. std::string_view min_value, std::string_view max_value) {
  134. // GLSL's clamp does not produce desirable results
  135. ctx.AddF32("{}=min(max({},float({})),float({}));", inst, value, min_value, max_value);
  136. }
  137. void EmitFPClamp64(EmitContext& ctx, IR::Inst& inst, std::string_view value,
  138. std::string_view min_value, std::string_view max_value) {
  139. // GLSL's clamp does not produce desirable results
  140. ctx.AddF64("{}=min(max({},double({})),double({}));", inst, value, min_value, max_value);
  141. }
  142. void EmitFPRoundEven16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  143. [[maybe_unused]] std::string_view value) {
  144. throw NotImplementedException("GLSL Instruction");
  145. }
  146. void EmitFPRoundEven32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  147. ctx.AddF32("{}=roundEven({});", inst, value);
  148. }
  149. void EmitFPRoundEven64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  150. ctx.AddF64("{}=roundEven({});", inst, value);
  151. }
  152. void EmitFPFloor16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  153. [[maybe_unused]] std::string_view value) {
  154. throw NotImplementedException("GLSL Instruction");
  155. }
  156. void EmitFPFloor32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  157. ctx.AddF32("{}=floor({});", inst, value);
  158. }
  159. void EmitFPFloor64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  160. ctx.AddF64("{}=floor({});", inst, value);
  161. }
  162. void EmitFPCeil16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  163. [[maybe_unused]] std::string_view value) {
  164. throw NotImplementedException("GLSL Instruction");
  165. }
  166. void EmitFPCeil32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  167. ctx.AddF32("{}=ceil({});", inst, value);
  168. }
  169. void EmitFPCeil64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  170. ctx.AddF64("{}=ceil({});", inst, value);
  171. }
  172. void EmitFPTrunc16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  173. [[maybe_unused]] std::string_view value) {
  174. throw NotImplementedException("GLSL Instruction");
  175. }
  176. void EmitFPTrunc32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  177. ctx.AddF32("{}=trunc({});", inst, value);
  178. }
  179. void EmitFPTrunc64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  180. ctx.AddF64("{}=trunc({});", inst, value);
  181. }
  182. void EmitFPOrdEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
  183. [[maybe_unused]] std::string_view rhs) {
  184. throw NotImplementedException("GLSL instruction");
  185. }
  186. void EmitFPOrdEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  187. std::string_view rhs) {
  188. Compare(ctx, inst, lhs, rhs, "==", true);
  189. }
  190. void EmitFPOrdEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  191. std::string_view rhs) {
  192. Compare(ctx, inst, lhs, rhs, "==", true);
  193. }
  194. void EmitFPUnordEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
  195. [[maybe_unused]] std::string_view rhs) {
  196. throw NotImplementedException("GLSL instruction");
  197. }
  198. void EmitFPUnordEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  199. std::string_view rhs) {
  200. Compare(ctx, inst, lhs, rhs, "==", false);
  201. }
  202. void EmitFPUnordEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  203. std::string_view rhs) {
  204. Compare(ctx, inst, lhs, rhs, "==", false);
  205. }
  206. void EmitFPOrdNotEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
  207. [[maybe_unused]] std::string_view rhs) {
  208. throw NotImplementedException("GLSL instruction");
  209. }
  210. void EmitFPOrdNotEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  211. std::string_view rhs) {
  212. Compare(ctx, inst, lhs, rhs, "!=", true);
  213. }
  214. void EmitFPOrdNotEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  215. std::string_view rhs) {
  216. Compare(ctx, inst, lhs, rhs, "!=", true);
  217. }
  218. void EmitFPUnordNotEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
  219. [[maybe_unused]] std::string_view rhs) {
  220. throw NotImplementedException("GLSL instruction");
  221. }
  222. void EmitFPUnordNotEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  223. std::string_view rhs) {
  224. Compare(ctx, inst, lhs, rhs, "!=", false);
  225. }
  226. void EmitFPUnordNotEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  227. std::string_view rhs) {
  228. Compare(ctx, inst, lhs, rhs, "!=", false);
  229. }
  230. void EmitFPOrdLessThan16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
  231. [[maybe_unused]] std::string_view rhs) {
  232. throw NotImplementedException("GLSL instruction");
  233. }
  234. void EmitFPOrdLessThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  235. std::string_view rhs) {
  236. Compare(ctx, inst, lhs, rhs, "<", true);
  237. }
  238. void EmitFPOrdLessThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  239. std::string_view rhs) {
  240. Compare(ctx, inst, lhs, rhs, "<", true);
  241. }
  242. void EmitFPUnordLessThan16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
  243. [[maybe_unused]] std::string_view rhs) {
  244. throw NotImplementedException("GLSL instruction");
  245. }
  246. void EmitFPUnordLessThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  247. std::string_view rhs) {
  248. Compare(ctx, inst, lhs, rhs, "<", false);
  249. }
  250. void EmitFPUnordLessThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  251. std::string_view rhs) {
  252. Compare(ctx, inst, lhs, rhs, "<", false);
  253. }
  254. void EmitFPOrdGreaterThan16([[maybe_unused]] EmitContext& ctx,
  255. [[maybe_unused]] std::string_view lhs,
  256. [[maybe_unused]] std::string_view rhs) {
  257. throw NotImplementedException("GLSL instruction");
  258. }
  259. void EmitFPOrdGreaterThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  260. std::string_view rhs) {
  261. Compare(ctx, inst, lhs, rhs, ">", true);
  262. }
  263. void EmitFPOrdGreaterThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  264. std::string_view rhs) {
  265. Compare(ctx, inst, lhs, rhs, ">", true);
  266. }
  267. void EmitFPUnordGreaterThan16([[maybe_unused]] EmitContext& ctx,
  268. [[maybe_unused]] std::string_view lhs,
  269. [[maybe_unused]] std::string_view rhs) {
  270. throw NotImplementedException("GLSL instruction");
  271. }
  272. void EmitFPUnordGreaterThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  273. std::string_view rhs) {
  274. Compare(ctx, inst, lhs, rhs, ">", false);
  275. }
  276. void EmitFPUnordGreaterThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  277. std::string_view rhs) {
  278. Compare(ctx, inst, lhs, rhs, ">", false);
  279. }
  280. void EmitFPOrdLessThanEqual16([[maybe_unused]] EmitContext& ctx,
  281. [[maybe_unused]] std::string_view lhs,
  282. [[maybe_unused]] std::string_view rhs) {
  283. throw NotImplementedException("GLSL instruction");
  284. }
  285. void EmitFPOrdLessThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  286. std::string_view rhs) {
  287. Compare(ctx, inst, lhs, rhs, "<=", true);
  288. }
  289. void EmitFPOrdLessThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  290. std::string_view rhs) {
  291. Compare(ctx, inst, lhs, rhs, "<=", true);
  292. }
  293. void EmitFPUnordLessThanEqual16([[maybe_unused]] EmitContext& ctx,
  294. [[maybe_unused]] std::string_view lhs,
  295. [[maybe_unused]] std::string_view rhs) {
  296. throw NotImplementedException("GLSL instruction");
  297. }
  298. void EmitFPUnordLessThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  299. std::string_view rhs) {
  300. Compare(ctx, inst, lhs, rhs, "<=", false);
  301. }
  302. void EmitFPUnordLessThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  303. std::string_view rhs) {
  304. Compare(ctx, inst, lhs, rhs, "<=", false);
  305. }
  306. void EmitFPOrdGreaterThanEqual16([[maybe_unused]] EmitContext& ctx,
  307. [[maybe_unused]] std::string_view lhs,
  308. [[maybe_unused]] std::string_view rhs) {
  309. throw NotImplementedException("GLSL instruction");
  310. }
  311. void EmitFPOrdGreaterThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  312. std::string_view rhs) {
  313. Compare(ctx, inst, lhs, rhs, ">=", true);
  314. }
  315. void EmitFPOrdGreaterThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  316. std::string_view rhs) {
  317. Compare(ctx, inst, lhs, rhs, ">=", true);
  318. }
  319. void EmitFPUnordGreaterThanEqual16([[maybe_unused]] EmitContext& ctx,
  320. [[maybe_unused]] std::string_view lhs,
  321. [[maybe_unused]] std::string_view rhs) {
  322. throw NotImplementedException("GLSL instruction");
  323. }
  324. void EmitFPUnordGreaterThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  325. std::string_view rhs) {
  326. Compare(ctx, inst, lhs, rhs, ">=", false);
  327. }
  328. void EmitFPUnordGreaterThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
  329. std::string_view rhs) {
  330. Compare(ctx, inst, lhs, rhs, ">=", false);
  331. }
  332. void EmitFPIsNan16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  333. [[maybe_unused]] std::string_view value) {
  334. throw NotImplementedException("GLSL instruction");
  335. }
  336. void EmitFPIsNan32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  337. ctx.AddU1("{}=isnan({});", inst, value);
  338. }
  339. void EmitFPIsNan64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  340. ctx.AddU1("{}=isnan({});", inst, value);
  341. }
  342. } // namespace Shader::Backend::GLSL