spirv_emit_context.h 7.9 KB

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