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