diff options
| author | 2021-02-16 04:10:22 -0300 | |
|---|---|---|
| committer | 2021-07-22 21:51:22 -0400 | |
| commit | b5d7279d878211654b4abb165d94af763a365f47 (patch) | |
| tree | 9b3a7b6e9d7d2b8945fe87d27ff75f1712ef06aa /src/shader_recompiler/backend/spirv/emit_context.cpp | |
| parent | shader: Improve object pool (diff) | |
| download | yuzu-b5d7279d878211654b4abb165d94af763a365f47.tar.gz yuzu-b5d7279d878211654b4abb165d94af763a365f47.tar.xz yuzu-b5d7279d878211654b4abb165d94af763a365f47.zip | |
spirv: Initial bindings support
Diffstat (limited to 'src/shader_recompiler/backend/spirv/emit_context.cpp')
| -rw-r--r-- | src/shader_recompiler/backend/spirv/emit_context.cpp | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/spirv/emit_context.cpp b/src/shader_recompiler/backend/spirv/emit_context.cpp new file mode 100644 index 000000000..1c985aff8 --- /dev/null +++ b/src/shader_recompiler/backend/spirv/emit_context.cpp | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <string_view> | ||
| 8 | |||
| 9 | #include <fmt/format.h> | ||
| 10 | |||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "shader_recompiler/backend/spirv/emit_context.h" | ||
| 13 | |||
| 14 | namespace Shader::Backend::SPIRV { | ||
| 15 | |||
| 16 | void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) { | ||
| 17 | defs[0] = sirit_ctx.Name(base_type, name); | ||
| 18 | |||
| 19 | std::array<char, 6> def_name; | ||
| 20 | for (int i = 1; i < 4; ++i) { | ||
| 21 | const std::string_view def_name_view( | ||
| 22 | def_name.data(), | ||
| 23 | fmt::format_to_n(def_name.data(), def_name.size(), "{}x{}", name, i + 1).size); | ||
| 24 | defs[i] = sirit_ctx.Name(sirit_ctx.TypeVector(base_type, i + 1), def_name_view); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | EmitContext::EmitContext(IR::Program& program) : Sirit::Module(0x00010000) { | ||
| 29 | AddCapability(spv::Capability::Shader); | ||
| 30 | DefineCommonTypes(program.info); | ||
| 31 | DefineCommonConstants(); | ||
| 32 | DefineSpecialVariables(program.info); | ||
| 33 | DefineConstantBuffers(program.info); | ||
| 34 | DefineStorageBuffers(program.info); | ||
| 35 | DefineLabels(program); | ||
| 36 | } | ||
| 37 | |||
| 38 | EmitContext::~EmitContext() = default; | ||
| 39 | |||
| 40 | Id EmitContext::Def(const IR::Value& value) { | ||
| 41 | if (!value.IsImmediate()) { | ||
| 42 | return value.Inst()->Definition<Id>(); | ||
| 43 | } | ||
| 44 | switch (value.Type()) { | ||
| 45 | case IR::Type::U1: | ||
| 46 | return value.U1() ? true_value : false_value; | ||
| 47 | case IR::Type::U32: | ||
| 48 | return Constant(U32[1], value.U32()); | ||
| 49 | case IR::Type::F32: | ||
| 50 | return Constant(F32[1], value.F32()); | ||
| 51 | default: | ||
| 52 | throw NotImplementedException("Immediate type {}", value.Type()); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | void EmitContext::DefineCommonTypes(const Info& info) { | ||
| 57 | void_id = TypeVoid(); | ||
| 58 | |||
| 59 | U1 = Name(TypeBool(), "u1"); | ||
| 60 | |||
| 61 | F32.Define(*this, TypeFloat(32), "f32"); | ||
| 62 | U32.Define(*this, TypeInt(32, false), "u32"); | ||
| 63 | |||
| 64 | if (info.uses_fp16) { | ||
| 65 | AddCapability(spv::Capability::Float16); | ||
| 66 | F16.Define(*this, TypeFloat(16), "f16"); | ||
| 67 | } | ||
| 68 | if (info.uses_fp64) { | ||
| 69 | AddCapability(spv::Capability::Float64); | ||
| 70 | F64.Define(*this, TypeFloat(64), "f64"); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | void EmitContext::DefineCommonConstants() { | ||
| 75 | true_value = ConstantTrue(U1); | ||
| 76 | false_value = ConstantFalse(U1); | ||
| 77 | u32_zero_value = Constant(U32[1], 0U); | ||
| 78 | } | ||
| 79 | |||
| 80 | void EmitContext::DefineSpecialVariables(const Info& info) { | ||
| 81 | const auto define{[this](Id type, spv::BuiltIn builtin, spv::StorageClass storage_class) { | ||
| 82 | const Id pointer_type{TypePointer(storage_class, type)}; | ||
| 83 | const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::Input)}; | ||
| 84 | Decorate(id, spv::Decoration::BuiltIn, builtin); | ||
| 85 | return id; | ||
| 86 | }}; | ||
| 87 | using namespace std::placeholders; | ||
| 88 | const auto define_input{std::bind(define, _1, _2, spv::StorageClass::Input)}; | ||
| 89 | |||
| 90 | if (info.uses_workgroup_id) { | ||
| 91 | workgroup_id = define_input(U32[3], spv::BuiltIn::WorkgroupId); | ||
| 92 | } | ||
| 93 | if (info.uses_local_invocation_id) { | ||
| 94 | local_invocation_id = define_input(U32[3], spv::BuiltIn::LocalInvocationId); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | void EmitContext::DefineConstantBuffers(const Info& info) { | ||
| 99 | if (info.constant_buffer_descriptors.empty()) { | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | const Id array_type{TypeArray(U32[1], Constant(U32[1], 4096))}; | ||
| 103 | Decorate(array_type, spv::Decoration::ArrayStride, 16U); | ||
| 104 | |||
| 105 | const Id struct_type{TypeStruct(array_type)}; | ||
| 106 | Name(struct_type, "cbuf_block"); | ||
| 107 | Decorate(struct_type, spv::Decoration::Block); | ||
| 108 | MemberName(struct_type, 0, "data"); | ||
| 109 | MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U); | ||
| 110 | |||
| 111 | const Id uniform_type{TypePointer(spv::StorageClass::Uniform, struct_type)}; | ||
| 112 | uniform_u32 = TypePointer(spv::StorageClass::Uniform, U32[1]); | ||
| 113 | |||
| 114 | u32 binding{}; | ||
| 115 | for (const Info::ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) { | ||
| 116 | const Id id{AddGlobalVariable(uniform_type, spv::StorageClass::Uniform)}; | ||
| 117 | Decorate(id, spv::Decoration::Binding, binding); | ||
| 118 | Name(id, fmt::format("c{}", desc.index)); | ||
| 119 | std::fill_n(cbufs.data() + desc.index, desc.count, id); | ||
| 120 | binding += desc.count; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | void EmitContext::DefineStorageBuffers(const Info& info) { | ||
| 125 | if (info.storage_buffers_descriptors.empty()) { | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | AddExtension("SPV_KHR_storage_buffer_storage_class"); | ||
| 129 | |||
| 130 | const Id array_type{TypeRuntimeArray(U32[1])}; | ||
| 131 | Decorate(array_type, spv::Decoration::ArrayStride, 4U); | ||
| 132 | |||
| 133 | const Id struct_type{TypeStruct(array_type)}; | ||
| 134 | Name(struct_type, "ssbo_block"); | ||
| 135 | Decorate(struct_type, spv::Decoration::Block); | ||
| 136 | MemberName(struct_type, 0, "data"); | ||
| 137 | MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U); | ||
| 138 | |||
| 139 | const Id storage_type{TypePointer(spv::StorageClass::StorageBuffer, struct_type)}; | ||
| 140 | storage_u32 = TypePointer(spv::StorageClass::StorageBuffer, U32[1]); | ||
| 141 | |||
| 142 | u32 binding{}; | ||
| 143 | for (const Info::StorageBufferDescriptor& desc : info.storage_buffers_descriptors) { | ||
| 144 | const Id id{AddGlobalVariable(storage_type, spv::StorageClass::StorageBuffer)}; | ||
| 145 | Decorate(id, spv::Decoration::Binding, binding); | ||
| 146 | Name(id, fmt::format("ssbo{}", binding)); | ||
| 147 | std::fill_n(ssbos.data() + binding, desc.count, id); | ||
| 148 | binding += desc.count; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | void EmitContext::DefineLabels(IR::Program& program) { | ||
| 153 | for (const IR::Function& function : program.functions) { | ||
| 154 | for (IR::Block* const block : function.blocks) { | ||
| 155 | block->SetDefinition(OpLabel()); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | } // namespace Shader::Backend::SPIRV | ||