emit_glasm_memory.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/glasm/emit_context.h"
  6. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  7. #include "shader_recompiler/frontend/ir/program.h"
  8. #include "shader_recompiler/frontend/ir/value.h"
  9. namespace Shader::Backend::GLASM {
  10. namespace {
  11. void StorageOp(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  12. std::string_view then_expr, std::string_view else_expr = {}) {
  13. // Operate on bindless SSBO, call the expression with bounds checking
  14. // address = c[binding].xy
  15. // length = c[binding].z
  16. const u32 sb_binding{binding.U32()};
  17. ctx.Add("PK64.U LC,c[{}];" // pointer = address
  18. "CVT.U64.U32 LC.z,{};" // offset = uint64_t(offset)
  19. "ADD.U64 LC.x,LC.x,LC.z;" // pointer += offset
  20. "SLT.U.CC RC.x,{},c[{}].z;", // cc = offset < length
  21. sb_binding, offset, offset, sb_binding);
  22. if (else_expr.empty()) {
  23. ctx.Add("{}", then_expr);
  24. } else {
  25. ctx.Add("IF NE.x;{}ELSE;{}ENDIF;", then_expr, else_expr);
  26. }
  27. }
  28. void Store(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  29. std::string_view value, std::string_view size) {
  30. StorageOp(ctx, binding, offset, fmt::format("STORE.{} {},LC.x;", size, value));
  31. }
  32. void Load(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, std::string_view offset,
  33. std::string_view size) {
  34. const std::string ret{ctx.reg_alloc.Define(inst)};
  35. StorageOp(ctx, binding, offset, fmt::format("STORE.{} {},LC.x;", size, ret),
  36. fmt::format("MOV.U {},{{0,0,0,0}};", ret));
  37. }
  38. } // Anonymous namespace
  39. void EmitLoadGlobalU8([[maybe_unused]] EmitContext& ctx) {
  40. throw NotImplementedException("GLASM instruction");
  41. }
  42. void EmitLoadGlobalS8([[maybe_unused]] EmitContext& ctx) {
  43. throw NotImplementedException("GLASM instruction");
  44. }
  45. void EmitLoadGlobalU16([[maybe_unused]] EmitContext& ctx) {
  46. throw NotImplementedException("GLASM instruction");
  47. }
  48. void EmitLoadGlobalS16([[maybe_unused]] EmitContext& ctx) {
  49. throw NotImplementedException("GLASM instruction");
  50. }
  51. void EmitLoadGlobal32([[maybe_unused]] EmitContext& ctx,
  52. [[maybe_unused]] std::string_view address) {
  53. throw NotImplementedException("GLASM instruction");
  54. }
  55. void EmitLoadGlobal64([[maybe_unused]] EmitContext& ctx,
  56. [[maybe_unused]] std::string_view address) {
  57. throw NotImplementedException("GLASM instruction");
  58. }
  59. void EmitLoadGlobal128([[maybe_unused]] EmitContext& ctx,
  60. [[maybe_unused]] std::string_view address) {
  61. throw NotImplementedException("GLASM instruction");
  62. }
  63. void EmitWriteGlobalU8([[maybe_unused]] EmitContext& ctx) {
  64. throw NotImplementedException("GLASM instruction");
  65. }
  66. void EmitWriteGlobalS8([[maybe_unused]] EmitContext& ctx) {
  67. throw NotImplementedException("GLASM instruction");
  68. }
  69. void EmitWriteGlobalU16([[maybe_unused]] EmitContext& ctx) {
  70. throw NotImplementedException("GLASM instruction");
  71. }
  72. void EmitWriteGlobalS16([[maybe_unused]] EmitContext& ctx) {
  73. throw NotImplementedException("GLASM instruction");
  74. }
  75. void EmitWriteGlobal32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view address,
  76. [[maybe_unused]] std::string_view value) {
  77. throw NotImplementedException("GLASM instruction");
  78. }
  79. void EmitWriteGlobal64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view address,
  80. [[maybe_unused]] std::string_view value) {
  81. throw NotImplementedException("GLASM instruction");
  82. }
  83. void EmitWriteGlobal128([[maybe_unused]] EmitContext& ctx,
  84. [[maybe_unused]] std::string_view address,
  85. [[maybe_unused]] std::string_view value) {
  86. throw NotImplementedException("GLASM instruction");
  87. }
  88. void EmitLoadStorageU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  89. std::string_view offset) {
  90. Load(ctx, inst, binding, offset, "U8");
  91. }
  92. void EmitLoadStorageS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  93. std::string_view offset) {
  94. Load(ctx, inst, binding, offset, "S8");
  95. }
  96. void EmitLoadStorageU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  97. std::string_view offset) {
  98. Load(ctx, inst, binding, offset, "U16");
  99. }
  100. void EmitLoadStorageS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  101. std::string_view offset) {
  102. Load(ctx, inst, binding, offset, "S16");
  103. }
  104. void EmitLoadStorage32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  105. std::string_view offset) {
  106. Load(ctx, inst, binding, offset, "U32");
  107. }
  108. void EmitLoadStorage64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  109. std::string_view offset) {
  110. Load(ctx, inst, binding, offset, "U32X2");
  111. }
  112. void EmitLoadStorage128(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  113. std::string_view offset) {
  114. Load(ctx, inst, binding, offset, "U32X4");
  115. }
  116. void EmitWriteStorageU8(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  117. std::string_view value) {
  118. Store(ctx, binding, offset, value, "U8");
  119. }
  120. void EmitWriteStorageS8(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  121. std::string_view value) {
  122. Store(ctx, binding, offset, value, "S8");
  123. }
  124. void EmitWriteStorageU16(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  125. std::string_view value) {
  126. Store(ctx, binding, offset, value, "U16");
  127. }
  128. void EmitWriteStorageS16(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  129. std::string_view value) {
  130. Store(ctx, binding, offset, value, "S16");
  131. }
  132. void EmitWriteStorage32(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  133. std::string_view value) {
  134. Store(ctx, binding, offset, value, "U32");
  135. }
  136. void EmitWriteStorage64(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  137. std::string_view value) {
  138. Store(ctx, binding, offset, value, "U32X2");
  139. }
  140. void EmitWriteStorage128(EmitContext& ctx, const IR::Value& binding, std::string_view offset,
  141. std::string_view value) {
  142. Store(ctx, binding, offset, value, "U32X4");
  143. }
  144. } // namespace Shader::Backend::GLASM