glasm_emit_context.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/bindings.h"
  6. #include "shader_recompiler/backend/glasm/emit_glasm.h"
  7. #include "shader_recompiler/backend/glasm/glasm_emit_context.h"
  8. #include "shader_recompiler/frontend/ir/program.h"
  9. #include "shader_recompiler/profile.h"
  10. #include "shader_recompiler/runtime_info.h"
  11. namespace Shader::Backend::GLASM {
  12. namespace {
  13. std::string_view InterpDecorator(Interpolation interp) {
  14. switch (interp) {
  15. case Interpolation::Smooth:
  16. return "";
  17. case Interpolation::Flat:
  18. return "FLAT ";
  19. case Interpolation::NoPerspective:
  20. return "NOPERSPECTIVE ";
  21. }
  22. throw InvalidArgument("Invalid interpolation {}", interp);
  23. }
  24. bool IsInputArray(Stage stage) {
  25. return stage == Stage::Geometry || stage == Stage::TessellationControl ||
  26. stage == Stage::TessellationEval;
  27. }
  28. } // Anonymous namespace
  29. EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
  30. const RuntimeInfo& runtime_info_)
  31. : info{program.info}, profile{profile_}, runtime_info{runtime_info_} {
  32. // FIXME: Temporary partial implementation
  33. u32 cbuf_index{};
  34. for (const auto& desc : info.constant_buffer_descriptors) {
  35. if (desc.count != 1) {
  36. throw NotImplementedException("Constant buffer descriptor array");
  37. }
  38. Add("CBUFFER c{}[]={{program.buffer[{}]}};", desc.index, cbuf_index);
  39. ++cbuf_index;
  40. }
  41. u32 ssbo_index{};
  42. for (const auto& desc : info.storage_buffers_descriptors) {
  43. if (desc.count != 1) {
  44. throw NotImplementedException("Storage buffer descriptor array");
  45. }
  46. if (runtime_info.glasm_use_storage_buffers) {
  47. Add("STORAGE ssbo{}[]={{program.storage[{}]}};", ssbo_index, bindings.storage_buffer);
  48. ++bindings.storage_buffer;
  49. ++ssbo_index;
  50. }
  51. }
  52. if (!runtime_info.glasm_use_storage_buffers) {
  53. if (const size_t num = info.storage_buffers_descriptors.size(); num > 0) {
  54. const size_t index{num + PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE};
  55. Add("PARAM c[{}]={{program.local[0..{}]}};", index, index - 1);
  56. }
  57. }
  58. stage = program.stage;
  59. switch (program.stage) {
  60. case Stage::VertexA:
  61. case Stage::VertexB:
  62. stage_name = "vertex";
  63. attrib_name = "vertex";
  64. break;
  65. case Stage::TessellationControl:
  66. case Stage::TessellationEval:
  67. stage_name = "primitive";
  68. attrib_name = "primitive";
  69. break;
  70. case Stage::Geometry:
  71. stage_name = "primitive";
  72. attrib_name = "vertex";
  73. break;
  74. case Stage::Fragment:
  75. stage_name = "fragment";
  76. attrib_name = "fragment";
  77. break;
  78. case Stage::Compute:
  79. stage_name = "invocation";
  80. break;
  81. }
  82. const std::string_view attr_stage{stage == Stage::Fragment ? "fragment" : "vertex"};
  83. const VaryingState loads{info.loads.mask | info.passthrough.mask};
  84. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  85. if (loads.Generic(index)) {
  86. Add("{}ATTRIB in_attr{}[]={{{}.attrib[{}..{}]}};",
  87. InterpDecorator(info.interpolation[index]), index, attr_stage, index, index);
  88. }
  89. }
  90. if (IsInputArray(stage) && loads.AnyComponent(IR::Attribute::PositionX)) {
  91. Add("ATTRIB vertex_position=vertex.position;");
  92. }
  93. if (info.uses_invocation_id) {
  94. Add("ATTRIB primitive_invocation=primitive.invocation;");
  95. }
  96. if (info.stores_tess_level_outer) {
  97. Add("OUTPUT result_patch_tessouter[]={{result.patch.tessouter[0..3]}};");
  98. }
  99. if (info.stores_tess_level_inner) {
  100. Add("OUTPUT result_patch_tessinner[]={{result.patch.tessinner[0..1]}};");
  101. }
  102. if (info.stores.ClipDistances()) {
  103. Add("OUTPUT result_clip[]={{result.clip[0..7]}};");
  104. }
  105. for (size_t index = 0; index < info.uses_patches.size(); ++index) {
  106. if (!info.uses_patches[index]) {
  107. continue;
  108. }
  109. if (stage == Stage::TessellationControl) {
  110. Add("OUTPUT result_patch_attrib{}[]={{result.patch.attrib[{}..{}]}};"
  111. "ATTRIB primitive_out_patch_attrib{}[]={{primitive.out.patch.attrib[{}..{}]}};",
  112. index, index, index, index, index, index);
  113. } else {
  114. Add("ATTRIB primitive_patch_attrib{}[]={{primitive.patch.attrib[{}..{}]}};", index,
  115. index, index);
  116. }
  117. }
  118. if (stage == Stage::Fragment) {
  119. Add("OUTPUT frag_color0=result.color;");
  120. for (size_t index = 1; index < info.stores_frag_color.size(); ++index) {
  121. Add("OUTPUT frag_color{}=result.color[{}];", index, index);
  122. }
  123. }
  124. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  125. if (info.stores.Generic(index)) {
  126. Add("OUTPUT out_attr{}[]={{result.attrib[{}..{}]}};", index, index, index);
  127. }
  128. }
  129. image_buffer_bindings.reserve(info.image_buffer_descriptors.size());
  130. for (const auto& desc : info.image_buffer_descriptors) {
  131. image_buffer_bindings.push_back(bindings.image);
  132. bindings.image += desc.count;
  133. }
  134. image_bindings.reserve(info.image_descriptors.size());
  135. for (const auto& desc : info.image_descriptors) {
  136. image_bindings.push_back(bindings.image);
  137. bindings.image += desc.count;
  138. }
  139. texture_buffer_bindings.reserve(info.texture_buffer_descriptors.size());
  140. for (const auto& desc : info.texture_buffer_descriptors) {
  141. texture_buffer_bindings.push_back(bindings.texture);
  142. bindings.texture += desc.count;
  143. }
  144. texture_bindings.reserve(info.texture_descriptors.size());
  145. for (const auto& desc : info.texture_descriptors) {
  146. texture_bindings.push_back(bindings.texture);
  147. bindings.texture += desc.count;
  148. }
  149. }
  150. } // namespace Shader::Backend::GLASM