emit_context.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <array>
  6. #include <string_view>
  7. #include <sirit/sirit.h>
  8. #include "shader_recompiler/backend/bindings.h"
  9. #include "shader_recompiler/frontend/ir/program.h"
  10. #include "shader_recompiler/profile.h"
  11. #include "shader_recompiler/shader_info.h"
  12. namespace Shader::Backend::SPIRV {
  13. using Sirit::Id;
  14. class VectorTypes {
  15. public:
  16. void Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name);
  17. [[nodiscard]] Id operator[](size_t size) const noexcept {
  18. return defs[size - 1];
  19. }
  20. private:
  21. std::array<Id, 4> defs{};
  22. };
  23. struct TextureDefinition {
  24. Id id;
  25. Id sampled_type;
  26. Id pointer_type;
  27. Id image_type;
  28. u32 count;
  29. };
  30. struct TextureBufferDefinition {
  31. Id id;
  32. u32 count;
  33. };
  34. struct ImageBufferDefinition {
  35. Id id;
  36. Id image_type;
  37. u32 count;
  38. };
  39. struct ImageDefinition {
  40. Id id;
  41. Id image_type;
  42. u32 count;
  43. };
  44. struct UniformDefinitions {
  45. Id U8{};
  46. Id S8{};
  47. Id U16{};
  48. Id S16{};
  49. Id U32{};
  50. Id F32{};
  51. Id U32x2{};
  52. Id U32x4{};
  53. };
  54. struct StorageTypeDefinition {
  55. Id array{};
  56. Id element{};
  57. };
  58. struct StorageTypeDefinitions {
  59. StorageTypeDefinition U8{};
  60. StorageTypeDefinition S8{};
  61. StorageTypeDefinition U16{};
  62. StorageTypeDefinition S16{};
  63. StorageTypeDefinition U32{};
  64. StorageTypeDefinition U64{};
  65. StorageTypeDefinition F32{};
  66. StorageTypeDefinition U32x2{};
  67. StorageTypeDefinition U32x4{};
  68. };
  69. struct StorageDefinitions {
  70. Id U8{};
  71. Id S8{};
  72. Id U16{};
  73. Id S16{};
  74. Id U32{};
  75. Id F32{};
  76. Id U64{};
  77. Id U32x2{};
  78. Id U32x4{};
  79. };
  80. struct GenericElementInfo {
  81. Id id{};
  82. u32 first_element{};
  83. u32 num_components{};
  84. };
  85. class EmitContext final : public Sirit::Module {
  86. public:
  87. explicit EmitContext(const Profile& profile, const RuntimeInfo& runtime_info,
  88. IR::Program& program, Bindings& binding);
  89. ~EmitContext();
  90. [[nodiscard]] Id Def(const IR::Value& value);
  91. [[nodiscard]] Id BitOffset8(const IR::Value& offset);
  92. [[nodiscard]] Id BitOffset16(const IR::Value& offset);
  93. Id Const(u32 value) {
  94. return Constant(U32[1], value);
  95. }
  96. Id Const(u32 element_1, u32 element_2) {
  97. return ConstantComposite(U32[2], Const(element_1), Const(element_2));
  98. }
  99. Id Const(u32 element_1, u32 element_2, u32 element_3) {
  100. return ConstantComposite(U32[3], Const(element_1), Const(element_2), Const(element_3));
  101. }
  102. Id Const(u32 element_1, u32 element_2, u32 element_3, u32 element_4) {
  103. return ConstantComposite(U32[4], Const(element_1), Const(element_2), Const(element_3),
  104. Const(element_4));
  105. }
  106. Id SConst(s32 value) {
  107. return Constant(S32[1], value);
  108. }
  109. Id SConst(s32 element_1, s32 element_2) {
  110. return ConstantComposite(S32[2], SConst(element_1), SConst(element_2));
  111. }
  112. Id SConst(s32 element_1, s32 element_2, s32 element_3) {
  113. return ConstantComposite(S32[3], SConst(element_1), SConst(element_2), SConst(element_3));
  114. }
  115. Id SConst(s32 element_1, s32 element_2, s32 element_3, s32 element_4) {
  116. return ConstantComposite(S32[4], SConst(element_1), SConst(element_2), SConst(element_3),
  117. SConst(element_4));
  118. }
  119. Id Const(f32 value) {
  120. return Constant(F32[1], value);
  121. }
  122. const Profile& profile;
  123. const RuntimeInfo& runtime_info;
  124. Stage stage{};
  125. Id void_id{};
  126. Id U1{};
  127. Id U8{};
  128. Id S8{};
  129. Id U16{};
  130. Id S16{};
  131. Id U64{};
  132. VectorTypes F32;
  133. VectorTypes U32;
  134. VectorTypes S32;
  135. VectorTypes F16;
  136. VectorTypes F64;
  137. Id true_value{};
  138. Id false_value{};
  139. Id u32_zero_value{};
  140. Id f32_zero_value{};
  141. UniformDefinitions uniform_types;
  142. StorageTypeDefinitions storage_types;
  143. Id private_u32{};
  144. Id shared_u8{};
  145. Id shared_u16{};
  146. Id shared_u32{};
  147. Id shared_u64{};
  148. Id shared_u32x2{};
  149. Id shared_u32x4{};
  150. Id input_f32{};
  151. Id input_u32{};
  152. Id input_s32{};
  153. Id output_f32{};
  154. Id output_u32{};
  155. Id image_buffer_type{};
  156. Id sampled_texture_buffer_type{};
  157. Id image_u32{};
  158. std::array<UniformDefinitions, Info::MAX_CBUFS> cbufs{};
  159. std::array<StorageDefinitions, Info::MAX_SSBOS> ssbos{};
  160. std::vector<TextureBufferDefinition> texture_buffers;
  161. std::vector<ImageBufferDefinition> image_buffers;
  162. std::vector<TextureDefinition> textures;
  163. std::vector<ImageDefinition> images;
  164. Id workgroup_id{};
  165. Id local_invocation_id{};
  166. Id invocation_id{};
  167. Id sample_id{};
  168. Id is_helper_invocation{};
  169. Id subgroup_local_invocation_id{};
  170. Id subgroup_mask_eq{};
  171. Id subgroup_mask_lt{};
  172. Id subgroup_mask_le{};
  173. Id subgroup_mask_gt{};
  174. Id subgroup_mask_ge{};
  175. Id instance_id{};
  176. Id instance_index{};
  177. Id base_instance{};
  178. Id vertex_id{};
  179. Id vertex_index{};
  180. Id base_vertex{};
  181. Id front_face{};
  182. Id point_coord{};
  183. Id tess_coord{};
  184. Id clip_distances{};
  185. Id layer{};
  186. Id viewport_index{};
  187. Id viewport_mask{};
  188. Id primitive_id{};
  189. Id fswzadd_lut_a{};
  190. Id fswzadd_lut_b{};
  191. Id indexed_load_func{};
  192. Id indexed_store_func{};
  193. Id local_memory{};
  194. Id shared_memory_u8{};
  195. Id shared_memory_u16{};
  196. Id shared_memory_u32{};
  197. Id shared_memory_u64{};
  198. Id shared_memory_u32x2{};
  199. Id shared_memory_u32x4{};
  200. Id shared_memory_u32_type{};
  201. Id shared_store_u8_func{};
  202. Id shared_store_u16_func{};
  203. Id increment_cas_shared{};
  204. Id increment_cas_ssbo{};
  205. Id decrement_cas_shared{};
  206. Id decrement_cas_ssbo{};
  207. Id f32_add_cas{};
  208. Id f16x2_add_cas{};
  209. Id f16x2_min_cas{};
  210. Id f16x2_max_cas{};
  211. Id f32x2_add_cas{};
  212. Id f32x2_min_cas{};
  213. Id f32x2_max_cas{};
  214. Id load_global_func_u32{};
  215. Id load_global_func_u32x2{};
  216. Id load_global_func_u32x4{};
  217. Id write_global_func_u32{};
  218. Id write_global_func_u32x2{};
  219. Id write_global_func_u32x4{};
  220. Id input_position{};
  221. std::array<Id, 32> input_generics{};
  222. Id output_point_size{};
  223. Id output_position{};
  224. std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
  225. Id output_tess_level_outer{};
  226. Id output_tess_level_inner{};
  227. std::array<Id, 30> patches{};
  228. std::array<Id, 8> frag_color{};
  229. Id sample_mask{};
  230. Id frag_depth{};
  231. std::vector<Id> interfaces;
  232. private:
  233. void DefineCommonTypes(const Info& info);
  234. void DefineCommonConstants();
  235. void DefineInterfaces(const IR::Program& program);
  236. void DefineLocalMemory(const IR::Program& program);
  237. void DefineSharedMemory(const IR::Program& program);
  238. void DefineSharedMemoryFunctions(const IR::Program& program);
  239. void DefineConstantBuffers(const Info& info, u32& binding);
  240. void DefineStorageBuffers(const Info& info, u32& binding);
  241. void DefineTextureBuffers(const Info& info, u32& binding);
  242. void DefineImageBuffers(const Info& info, u32& binding);
  243. void DefineTextures(const Info& info, u32& binding);
  244. void DefineImages(const Info& info, u32& binding);
  245. void DefineAttributeMemAccess(const Info& info);
  246. void DefineGlobalMemoryFunctions(const Info& info);
  247. void DefineInputs(const Info& info);
  248. void DefineOutputs(const IR::Program& program);
  249. };
  250. } // namespace Shader::Backend::SPIRV