emit_glasm_context_get_set.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/value.h"
  8. namespace Shader::Backend::GLASM {
  9. namespace {
  10. void GetCbuf(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset,
  11. std::string_view size) {
  12. if (!binding.IsImmediate()) {
  13. throw NotImplementedException("Indirect constant buffer loading");
  14. }
  15. const Register ret{ctx.reg_alloc.Define(inst)};
  16. ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
  17. }
  18. } // Anonymous namespace
  19. void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  20. GetCbuf(ctx, inst, binding, offset, "U8");
  21. }
  22. void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  23. GetCbuf(ctx, inst, binding, offset, "S8");
  24. }
  25. void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  26. GetCbuf(ctx, inst, binding, offset, "U16");
  27. }
  28. void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  29. GetCbuf(ctx, inst, binding, offset, "S16");
  30. }
  31. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  32. GetCbuf(ctx, inst, binding, offset, "U32");
  33. }
  34. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  35. GetCbuf(ctx, inst, binding, offset, "F32");
  36. }
  37. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  38. ScalarU32 offset) {
  39. GetCbuf(ctx, inst, binding, offset, "U32X2");
  40. }
  41. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
  42. [[maybe_unused]] ScalarU32 vertex) {
  43. const u32 element{static_cast<u32>(attr) % 4};
  44. const char swizzle{"xyzw"[element]};
  45. if (IR::IsGeneric(attr)) {
  46. const u32 index{IR::GenericAttributeIndex(attr)};
  47. ctx.Add("MOV.F {}.x,in_attr{}[0].{};", inst, index, swizzle);
  48. return;
  49. }
  50. switch (attr) {
  51. case IR::Attribute::PositionX:
  52. case IR::Attribute::PositionY:
  53. case IR::Attribute::PositionZ:
  54. case IR::Attribute::PositionW:
  55. ctx.Add("MOV.F {}.x,{}.position.{};", inst, ctx.stage_name, swizzle);
  56. break;
  57. case IR::Attribute::InstanceId:
  58. ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.stage_name);
  59. break;
  60. case IR::Attribute::VertexId:
  61. ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.stage_name);
  62. break;
  63. default:
  64. throw NotImplementedException("Get attribute {}", attr);
  65. }
  66. }
  67. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
  68. [[maybe_unused]] ScalarU32 vertex) {
  69. const u32 element{static_cast<u32>(attr) % 4};
  70. const char swizzle{"xyzw"[element]};
  71. if (IR::IsGeneric(attr)) {
  72. const u32 index{IR::GenericAttributeIndex(attr)};
  73. ctx.Add("MOV.F out_attr{}[0].{},{};", index, swizzle, value);
  74. return;
  75. }
  76. switch (attr) {
  77. case IR::Attribute::PositionX:
  78. case IR::Attribute::PositionY:
  79. case IR::Attribute::PositionZ:
  80. case IR::Attribute::PositionW:
  81. ctx.Add("MOV.F result.position.{},{};", swizzle, value);
  82. break;
  83. default:
  84. throw NotImplementedException("Set attribute {}", attr);
  85. }
  86. }
  87. void EmitGetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  88. [[maybe_unused]] ScalarU32 vertex) {
  89. throw NotImplementedException("GLASM instruction");
  90. }
  91. void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  92. [[maybe_unused]] ScalarF32 value, [[maybe_unused]] ScalarU32 vertex) {
  93. throw NotImplementedException("GLASM instruction");
  94. }
  95. void EmitGetPatch([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Patch patch) {
  96. throw NotImplementedException("GLASM instruction");
  97. }
  98. void EmitSetPatch([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Patch patch,
  99. [[maybe_unused]] ScalarF32 value) {
  100. throw NotImplementedException("GLASM instruction");
  101. }
  102. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, ScalarF32 value) {
  103. ctx.Add("MOV.F frag_color{}.{},{};", index, "xyzw"[component], value);
  104. }
  105. void EmitSetSampleMask(EmitContext& ctx, ScalarS32 value) {
  106. ctx.Add("MOV.S result.samplemask.x,{};", value);
  107. }
  108. void EmitSetFragDepth(EmitContext& ctx, ScalarF32 value) {
  109. ctx.Add("MOV.F result.depth.z,{};", value);
  110. }
  111. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) {
  112. ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset);
  113. }
  114. void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value) {
  115. ctx.Add("MOV.U lmem[{}].x,{};", word_offset, value);
  116. }
  117. } // namespace Shader::Backend::GLASM