emit_context.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. struct TextureImageDefinition {
  30. u32 binding;
  31. u32 count;
  32. };
  33. class EmitContext {
  34. public:
  35. explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
  36. const RuntimeInfo& runtime_info_);
  37. template <GlslVarType type, typename... Args>
  38. void Add(const char* format_str, IR::Inst& inst, Args&&... args) {
  39. const auto var_def{var_alloc.AddDefine(inst, type)};
  40. if (var_def.empty()) {
  41. // skip assigment.
  42. code += fmt::format(format_str + 3, std::forward<Args>(args)...);
  43. } else {
  44. code += fmt::format(format_str, var_def, std::forward<Args>(args)...);
  45. }
  46. // TODO: Remove this
  47. code += '\n';
  48. }
  49. template <typename... Args>
  50. void AddU1(const char* format_str, IR::Inst& inst, Args&&... args) {
  51. Add<GlslVarType::U1>(format_str, inst, args...);
  52. }
  53. template <typename... Args>
  54. void AddF16x2(const char* format_str, IR::Inst& inst, Args&&... args) {
  55. Add<GlslVarType::F16x2>(format_str, inst, args...);
  56. }
  57. template <typename... Args>
  58. void AddU32(const char* format_str, IR::Inst& inst, Args&&... args) {
  59. Add<GlslVarType::U32>(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 AddU64(const char* format_str, IR::Inst& inst, Args&&... args) {
  67. Add<GlslVarType::U64>(format_str, inst, args...);
  68. }
  69. template <typename... Args>
  70. void AddF64(const char* format_str, IR::Inst& inst, Args&&... args) {
  71. Add<GlslVarType::F64>(format_str, inst, args...);
  72. }
  73. template <typename... Args>
  74. void AddU32x2(const char* format_str, IR::Inst& inst, Args&&... args) {
  75. Add<GlslVarType::U32x2>(format_str, inst, args...);
  76. }
  77. template <typename... Args>
  78. void AddF32x2(const char* format_str, IR::Inst& inst, Args&&... args) {
  79. Add<GlslVarType::F32x2>(format_str, inst, args...);
  80. }
  81. template <typename... Args>
  82. void AddU32x3(const char* format_str, IR::Inst& inst, Args&&... args) {
  83. Add<GlslVarType::U32x3>(format_str, inst, args...);
  84. }
  85. template <typename... Args>
  86. void AddF32x3(const char* format_str, IR::Inst& inst, Args&&... args) {
  87. Add<GlslVarType::F32x3>(format_str, inst, args...);
  88. }
  89. template <typename... Args>
  90. void AddU32x4(const char* format_str, IR::Inst& inst, Args&&... args) {
  91. Add<GlslVarType::U32x4>(format_str, inst, args...);
  92. }
  93. template <typename... Args>
  94. void AddF32x4(const char* format_str, IR::Inst& inst, Args&&... args) {
  95. Add<GlslVarType::F32x4>(format_str, inst, args...);
  96. }
  97. template <typename... Args>
  98. void AddPrecF32(const char* format_str, IR::Inst& inst, Args&&... args) {
  99. Add<GlslVarType::PrecF32>(format_str, inst, args...);
  100. }
  101. template <typename... Args>
  102. void AddPrecF64(const char* format_str, IR::Inst& inst, Args&&... args) {
  103. Add<GlslVarType::PrecF64>(format_str, inst, args...);
  104. }
  105. template <typename... Args>
  106. void Add(const char* format_str, Args&&... args) {
  107. code += fmt::format(format_str, std::forward<Args>(args)...);
  108. // TODO: Remove this
  109. code += '\n';
  110. }
  111. std::string header;
  112. std::string code;
  113. VarAlloc var_alloc;
  114. const Info& info;
  115. const Profile& profile;
  116. const RuntimeInfo& runtime_info;
  117. Stage stage{};
  118. std::string_view stage_name = "invalid";
  119. std::string_view position_name = "gl_Position";
  120. std::vector<TextureImageDefinition> texture_buffers;
  121. std::vector<TextureImageDefinition> image_buffers;
  122. std::vector<TextureImageDefinition> textures;
  123. std::vector<TextureImageDefinition> images;
  124. std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
  125. u32 num_safety_loop_vars{};
  126. bool uses_y_direction{};
  127. bool uses_cc_carry{};
  128. private:
  129. void SetupExtensions();
  130. void DefineConstantBuffers(Bindings& bindings);
  131. void DefineStorageBuffers(Bindings& bindings);
  132. void DefineGenericOutput(size_t index, u32 invocations);
  133. void DefineHelperFunctions();
  134. void DefineConstants();
  135. std::string DefineGlobalMemoryFunctions();
  136. void SetupImages(Bindings& bindings);
  137. void SetupTextures(Bindings& bindings);
  138. };
  139. } // namespace Shader::Backend::GLSL