emit_glsl_floating_point.cpp 16 KB

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