spirv_emit_context.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <sirit/sirit.h>
  6. #include "shader_recompiler/backend/bindings.h"
  7. #include "shader_recompiler/frontend/ir/program.h"
  8. #include "shader_recompiler/profile.h"
  9. #include "shader_recompiler/runtime_info.h"
  10. #include "shader_recompiler/shader_info.h"
  11. namespace Shader::Backend::SPIRV {
  12. using Sirit::Id;
  13. class VectorTypes {
  14. public:
  15. void Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name);
  16. [[nodiscard]] Id operator[](size_t size) const noexcept {
  17. return defs[size - 1];
  18. }
  19. private:
  20. std::array<Id, 4> defs{};
  21. };
  22. struct TextureDefinition {
  23. Id id;
  24. Id sampled_type;
  25. Id pointer_type;
  26. Id image_type;
  27. u32 count;
  28. bool is_multisample;
  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. bool is_integer;
  39. };
  40. struct ImageDefinition {
  41. Id id;
  42. Id image_type;
  43. u32 count;
  44. bool is_integer;
  45. };
  46. struct UniformDefinitions {
  47. Id U8{};
  48. Id S8{};
  49. Id U16{};
  50. Id S16{};
  51. Id U32{};
  52. Id F32{};
  53. Id U32x2{};
  54. Id U32x4{};
  55. constexpr static size_t NumElements(Id UniformDefinitions::*member_ptr) {
  56. if (member_ptr == &UniformDefinitions::U8) {
  57. return 1;
  58. }
  59. if (member_ptr == &UniformDefinitions::S8) {
  60. return 1;
  61. }
  62. if (member_ptr == &UniformDefinitions::U16) {
  63. return 1;
  64. }
  65. if (member_ptr == &UniformDefinitions::S16) {
  66. return 1;
  67. }
  68. if (member_ptr == &UniformDefinitions::U32) {
  69. return 1;
  70. }
  71. if (member_ptr == &UniformDefinitions::F32) {
  72. return 1;
  73. }
  74. if (member_ptr == &UniformDefinitions::U32x2) {
  75. return 2;
  76. }
  77. if (member_ptr == &UniformDefinitions::U32x4) {
  78. return 4;
  79. }
  80. ASSERT(false);
  81. return 1;
  82. }
  83. constexpr static bool IsFloat(Id UniformDefinitions::*member_ptr) {
  84. if (member_ptr == &UniformDefinitions::F32) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. };
  90. struct StorageTypeDefinition {
  91. Id array{};
  92. Id element{};
  93. };
  94. struct StorageTypeDefinitions {
  95. StorageTypeDefinition U8{};
  96. StorageTypeDefinition S8{};
  97. StorageTypeDefinition U16{};
  98. StorageTypeDefinition S16{};
  99. StorageTypeDefinition U32{};
  100. StorageTypeDefinition U64{};
  101. StorageTypeDefinition F32{};
  102. StorageTypeDefinition U32x2{};
  103. StorageTypeDefinition U32x4{};
  104. };
  105. struct StorageDefinitions {
  106. Id U8{};
  107. Id S8{};
  108. Id U16{};
  109. Id S16{};
  110. Id U32{};
  111. Id F32{};
  112. Id U64{};
  113. Id U32x2{};
  114. Id U32x4{};
  115. };
  116. enum class InputGenericLoadOp {
  117. None,
  118. Bitcast,
  119. SToF,
  120. UToF,
  121. };
  122. struct InputGenericInfo {
  123. Id id;
  124. Id pointer_type;
  125. Id component_type;
  126. InputGenericLoadOp load_op;
  127. };
  128. struct GenericElementInfo {
  129. Id id{};
  130. u32 first_element{};
  131. u32 num_components{};
  132. };
  133. class EmitContext final : public Sirit::Module {
  134. public:
  135. explicit EmitContext(const Profile& profile, const RuntimeInfo& runtime_info,
  136. IR::Program& program, Bindings& binding);
  137. ~EmitContext();
  138. [[nodiscard]] Id Def(const IR::Value& value);
  139. [[nodiscard]] Id BitOffset8(const IR::Value& offset);
  140. [[nodiscard]] Id BitOffset16(const IR::Value& offset);
  141. Id Const(u32 value) {
  142. return Constant(U32[1], value);
  143. }
  144. Id Const(u32 element_1, u32 element_2) {
  145. return ConstantComposite(U32[2], Const(element_1), Const(element_2));
  146. }
  147. Id Const(u32 element_1, u32 element_2, u32 element_3) {
  148. return ConstantComposite(U32[3], Const(element_1), Const(element_2), Const(element_3));
  149. }
  150. Id Const(u32 element_1, u32 element_2, u32 element_3, u32 element_4) {
  151. return ConstantComposite(U32[4], Const(element_1), Const(element_2), Const(element_3),
  152. Const(element_4));
  153. }
  154. Id SConst(s32 value) {
  155. return Constant(S32[1], value);
  156. }
  157. Id SConst(s32 element_1, s32 element_2) {
  158. return ConstantComposite(S32[2], SConst(element_1), SConst(element_2));
  159. }
  160. Id SConst(s32 element_1, s32 element_2, s32 element_3) {
  161. return ConstantComposite(S32[3], SConst(element_1), SConst(element_2), SConst(element_3));
  162. }
  163. Id SConst(s32 element_1, s32 element_2, s32 element_3, s32 element_4) {
  164. return ConstantComposite(S32[4], SConst(element_1), SConst(element_2), SConst(element_3),
  165. SConst(element_4));
  166. }
  167. Id Const(f32 value) {
  168. return Constant(F32[1], value);
  169. }
  170. const Profile& profile;
  171. const RuntimeInfo& runtime_info;
  172. Stage stage{};
  173. Id void_id{};
  174. Id U1{};
  175. Id U8{};
  176. Id S8{};
  177. Id U16{};
  178. Id S16{};
  179. Id U64{};
  180. VectorTypes F32;
  181. VectorTypes U32;
  182. VectorTypes S32;
  183. VectorTypes F16;
  184. VectorTypes F64;
  185. Id true_value{};
  186. Id false_value{};
  187. Id u32_zero_value{};
  188. Id f32_zero_value{};
  189. UniformDefinitions uniform_types;
  190. StorageTypeDefinitions storage_types;
  191. Id private_u32{};
  192. Id shared_u8{};
  193. Id shared_u16{};
  194. Id shared_u32{};
  195. Id shared_u64{};
  196. Id shared_u32x2{};
  197. Id shared_u32x4{};
  198. Id input_f32{};
  199. Id input_u32{};
  200. Id input_s32{};
  201. Id output_f32{};
  202. Id output_u32{};
  203. Id image_buffer_type{};
  204. Id image_u32{};
  205. std::array<UniformDefinitions, Info::MAX_CBUFS> cbufs{};
  206. std::array<StorageDefinitions, Info::MAX_SSBOS> ssbos{};
  207. std::vector<TextureBufferDefinition> texture_buffers;
  208. std::vector<ImageBufferDefinition> image_buffers;
  209. std::vector<TextureDefinition> textures;
  210. std::vector<ImageDefinition> images;
  211. Id workgroup_id{};
  212. Id local_invocation_id{};
  213. Id invocation_id{};
  214. Id patch_vertices_in{};
  215. Id sample_id{};
  216. Id is_helper_invocation{};
  217. Id subgroup_local_invocation_id{};
  218. Id subgroup_mask_eq{};
  219. Id subgroup_mask_lt{};
  220. Id subgroup_mask_le{};
  221. Id subgroup_mask_gt{};
  222. Id subgroup_mask_ge{};
  223. Id instance_id{};
  224. Id instance_index{};
  225. Id base_instance{};
  226. Id vertex_id{};
  227. Id vertex_index{};
  228. Id draw_index{};
  229. Id base_vertex{};
  230. Id front_face{};
  231. Id point_coord{};
  232. Id tess_coord{};
  233. Id clip_distances{};
  234. Id layer{};
  235. Id viewport_index{};
  236. Id viewport_mask{};
  237. Id primitive_id{};
  238. Id fswzadd_lut_a{};
  239. Id fswzadd_lut_b{};
  240. Id indexed_load_func{};
  241. Id indexed_store_func{};
  242. Id rescaling_uniform_constant{};
  243. Id rescaling_push_constants{};
  244. Id rescaling_textures_type{};
  245. Id rescaling_images_type{};
  246. u32 rescaling_textures_member_index{};
  247. u32 rescaling_images_member_index{};
  248. u32 rescaling_downfactor_member_index{};
  249. u32 texture_rescaling_index{};
  250. u32 image_rescaling_index{};
  251. Id render_area_push_constant{};
  252. u32 render_are_member_index{};
  253. Id local_memory{};
  254. Id shared_memory_u8{};
  255. Id shared_memory_u16{};
  256. Id shared_memory_u32{};
  257. Id shared_memory_u64{};
  258. Id shared_memory_u32x2{};
  259. Id shared_memory_u32x4{};
  260. Id shared_memory_u32_type{};
  261. Id shared_store_u8_func{};
  262. Id shared_store_u16_func{};
  263. Id increment_cas_shared{};
  264. Id increment_cas_ssbo{};
  265. Id decrement_cas_shared{};
  266. Id decrement_cas_ssbo{};
  267. Id f32_add_cas{};
  268. Id f16x2_add_cas{};
  269. Id f16x2_min_cas{};
  270. Id f16x2_max_cas{};
  271. Id f32x2_add_cas{};
  272. Id f32x2_min_cas{};
  273. Id f32x2_max_cas{};
  274. Id load_global_func_u32{};
  275. Id load_global_func_u32x2{};
  276. Id load_global_func_u32x4{};
  277. Id write_global_func_u32{};
  278. Id write_global_func_u32x2{};
  279. Id write_global_func_u32x4{};
  280. bool need_input_position_indirect{};
  281. Id input_position{};
  282. std::array<InputGenericInfo, 32> input_generics{};
  283. Id output_point_size{};
  284. Id output_position{};
  285. std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
  286. Id output_tess_level_outer{};
  287. Id output_tess_level_inner{};
  288. std::array<Id, 30> patches{};
  289. std::array<Id, 8> frag_color{};
  290. Id sample_mask{};
  291. Id frag_depth{};
  292. std::vector<Id> interfaces;
  293. Id load_const_func_u8{};
  294. Id load_const_func_u16{};
  295. Id load_const_func_u32{};
  296. Id load_const_func_f32{};
  297. Id load_const_func_u32x2{};
  298. Id load_const_func_u32x4{};
  299. private:
  300. void DefineCommonTypes(const Info& info);
  301. void DefineCommonConstants();
  302. void DefineInterfaces(const IR::Program& program);
  303. void DefineLocalMemory(const IR::Program& program);
  304. void DefineSharedMemory(const IR::Program& program);
  305. void DefineSharedMemoryFunctions(const IR::Program& program);
  306. void DefineConstantBuffers(const Info& info, u32& binding);
  307. void DefineConstantBufferIndirectFunctions(const Info& info);
  308. void DefineStorageBuffers(const Info& info, u32& binding);
  309. void DefineTextureBuffers(const Info& info, u32& binding);
  310. void DefineImageBuffers(const Info& info, u32& binding);
  311. void DefineTextures(const Info& info, u32& binding, u32& scaling_index);
  312. void DefineImages(const Info& info, u32& binding, u32& scaling_index);
  313. void DefineAttributeMemAccess(const Info& info);
  314. void DefineGlobalMemoryFunctions(const Info& info);
  315. void DefineRescalingInput(const Info& info);
  316. void DefineRescalingInputPushConstant();
  317. void DefineRescalingInputUniformConstant();
  318. void DefineRenderArea(const Info& info);
  319. void DefineInputs(const IR::Program& program);
  320. void DefineOutputs(const IR::Program& program);
  321. };
  322. } // namespace Shader::Backend::SPIRV