spirv_emit_context.h 9.4 KB

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