emit_context.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <fmt/format.h>
  9. #include "shader_recompiler/backend/glsl/var_alloc.h"
  10. #include "shader_recompiler/stage.h"
  11. namespace Shader {
  12. struct Info;
  13. struct Profile;
  14. struct RuntimeInfo;
  15. } // namespace Shader
  16. namespace Shader::Backend {
  17. struct Bindings;
  18. }
  19. namespace Shader::IR {
  20. class Inst;
  21. struct Program;
  22. } // namespace Shader::IR
  23. namespace Shader::Backend::GLSL {
  24. struct GenericElementInfo {
  25. std::string name{};
  26. u32 first_element{};
  27. u32 num_components{};
  28. };
  29. class EmitContext {
  30. public:
  31. explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
  32. const RuntimeInfo& runtime_info_);
  33. template <GlslVarType type, typename... Args>
  34. void Add(const char* format_str, IR::Inst& inst, Args&&... args) {
  35. const auto var_def{var_alloc.AddDefine(inst, type)};
  36. if (var_def.empty()) {
  37. // skip assigment.
  38. code += fmt::format(&format_str[3], std::forward<Args>(args)...);
  39. } else {
  40. code += fmt::format(format_str, var_def, std::forward<Args>(args)...);
  41. }
  42. // TODO: Remove this
  43. code += '\n';
  44. }
  45. template <typename... Args>
  46. void AddU1(const char* format_str, IR::Inst& inst, Args&&... args) {
  47. Add<GlslVarType::U1>(format_str, inst, args...);
  48. }
  49. template <typename... Args>
  50. void AddF16x2(const char* format_str, IR::Inst& inst, Args&&... args) {
  51. Add<GlslVarType::F16x2>(format_str, inst, args...);
  52. }
  53. template <typename... Args>
  54. void AddU32(const char* format_str, IR::Inst& inst, Args&&... args) {
  55. Add<GlslVarType::U32>(format_str, inst, args...);
  56. }
  57. template <typename... Args>
  58. void AddS32(const char* format_str, IR::Inst& inst, Args&&... args) {
  59. Add<GlslVarType::S32>(format_str, inst, args...);
  60. }
  61. template <typename... Args>
  62. void AddF32(const char* format_str, IR::Inst& inst, Args&&... args) {
  63. Add<GlslVarType::F32>(format_str, inst, args...);
  64. }
  65. template <typename... Args>
  66. void AddS64(const char* format_str, IR::Inst& inst, Args&&... args) {
  67. Add<GlslVarType::S64>(format_str, inst, args...);
  68. }
  69. template <typename... Args>
  70. void AddU64(const char* format_str, IR::Inst& inst, Args&&... args) {
  71. Add<GlslVarType::U64>(format_str, inst, args...);
  72. }
  73. template <typename... Args>
  74. void AddF64(const char* format_str, IR::Inst& inst, Args&&... args) {
  75. Add<GlslVarType::F64>(format_str, inst, args...);
  76. }
  77. template <typename... Args>
  78. void AddU32x2(const char* format_str, IR::Inst& inst, Args&&... args) {
  79. Add<GlslVarType::U32x2>(format_str, inst, args...);
  80. }
  81. template <typename... Args>
  82. void AddF32x2(const char* format_str, IR::Inst& inst, Args&&... args) {
  83. Add<GlslVarType::F32x2>(format_str, inst, args...);
  84. }
  85. template <typename... Args>
  86. void AddU32x3(const char* format_str, IR::Inst& inst, Args&&... args) {
  87. Add<GlslVarType::U32x3>(format_str, inst, args...);
  88. }
  89. template <typename... Args>
  90. void AddF32x3(const char* format_str, IR::Inst& inst, Args&&... args) {
  91. Add<GlslVarType::F32x3>(format_str, inst, args...);
  92. }
  93. template <typename... Args>
  94. void AddU32x4(const char* format_str, IR::Inst& inst, Args&&... args) {
  95. Add<GlslVarType::U32x4>(format_str, inst, args...);
  96. }
  97. template <typename... Args>
  98. void AddF32x4(const char* format_str, IR::Inst& inst, Args&&... args) {
  99. Add<GlslVarType::F32x4>(format_str, inst, args...);
  100. }
  101. template <typename... Args>
  102. void AddPrecF32(const char* format_str, IR::Inst& inst, Args&&... args) {
  103. Add<GlslVarType::PrecF32>(format_str, inst, args...);
  104. }
  105. template <typename... Args>
  106. void AddPrecF64(const char* format_str, IR::Inst& inst, Args&&... args) {
  107. Add<GlslVarType::PrecF64>(format_str, inst, args...);
  108. }
  109. template <typename... Args>
  110. void Add(const char* format_str, Args&&... args) {
  111. code += fmt::format(format_str, std::forward<Args>(args)...);
  112. // TODO: Remove this
  113. code += '\n';
  114. }
  115. std::string header;
  116. std::string code;
  117. VarAlloc var_alloc;
  118. const Info& info;
  119. const Profile& profile;
  120. const RuntimeInfo& runtime_info;
  121. Stage stage{};
  122. std::string_view stage_name = "invalid";
  123. std::vector<u32> texture_buffer_bindings;
  124. std::vector<u32> image_buffer_bindings;
  125. std::vector<u32> texture_bindings;
  126. std::vector<u32> image_bindings;
  127. std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
  128. bool uses_y_direction{};
  129. bool uses_cc_carry{};
  130. private:
  131. void SetupExtensions(std::string& header);
  132. void DefineConstantBuffers(Bindings& bindings);
  133. void DefineStorageBuffers(Bindings& bindings);
  134. void DefineGenericOutput(size_t index, u32 invocations);
  135. void DefineHelperFunctions();
  136. void SetupImages(Bindings& bindings);
  137. };
  138. } // namespace Shader::Backend::GLSL