2021-02-16 07:10:22 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "shader_recompiler/backend/spirv/emit_context.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV {
|
2021-03-08 21:31:53 +00:00
|
|
|
namespace {
|
|
|
|
Id ImageType(EmitContext& ctx, const TextureDescriptor& desc) {
|
|
|
|
const spv::ImageFormat format{spv::ImageFormat::Unknown};
|
|
|
|
const Id type{ctx.F32[1]};
|
|
|
|
switch (desc.type) {
|
|
|
|
case TextureType::Color1D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim1D, false, false, false, 1, format);
|
|
|
|
case TextureType::ColorArray1D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim1D, false, true, false, 1, format);
|
|
|
|
case TextureType::Color2D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim2D, false, false, false, 1, format);
|
|
|
|
case TextureType::ColorArray2D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim2D, false, true, false, 1, format);
|
|
|
|
case TextureType::Color3D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim3D, false, false, false, 1, format);
|
|
|
|
case TextureType::ColorCube:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Cube, false, false, false, 1, format);
|
|
|
|
case TextureType::ColorArrayCube:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Cube, false, true, false, 1, format);
|
|
|
|
case TextureType::Shadow1D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim1D, true, false, false, 1, format);
|
|
|
|
case TextureType::ShadowArray1D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim1D, true, true, false, 1, format);
|
|
|
|
case TextureType::Shadow2D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim2D, true, false, false, 1, format);
|
|
|
|
case TextureType::ShadowArray2D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim2D, true, true, false, 1, format);
|
|
|
|
case TextureType::Shadow3D:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Dim3D, true, false, false, 1, format);
|
|
|
|
case TextureType::ShadowCube:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Cube, true, false, false, 1, format);
|
|
|
|
case TextureType::ShadowArrayCube:
|
|
|
|
return ctx.TypeImage(type, spv::Dim::Cube, false, true, false, 1, format);
|
|
|
|
}
|
|
|
|
throw InvalidArgument("Invalid texture type {}", desc.type);
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
2021-02-16 07:10:22 +00:00
|
|
|
|
|
|
|
void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
|
|
|
|
defs[0] = sirit_ctx.Name(base_type, name);
|
|
|
|
|
|
|
|
std::array<char, 6> def_name;
|
|
|
|
for (int i = 1; i < 4; ++i) {
|
|
|
|
const std::string_view def_name_view(
|
|
|
|
def_name.data(),
|
|
|
|
fmt::format_to_n(def_name.data(), def_name.size(), "{}x{}", name, i + 1).size);
|
|
|
|
defs[i] = sirit_ctx.Name(sirit_ctx.TypeVector(base_type, i + 1), def_name_view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:42:38 +00:00
|
|
|
EmitContext::EmitContext(const Profile& profile_, IR::Program& program)
|
|
|
|
: Sirit::Module(0x00010000), profile{profile_} {
|
2021-02-16 07:10:22 +00:00
|
|
|
AddCapability(spv::Capability::Shader);
|
|
|
|
DefineCommonTypes(program.info);
|
|
|
|
DefineCommonConstants();
|
|
|
|
DefineSpecialVariables(program.info);
|
2021-02-19 21:10:18 +00:00
|
|
|
|
|
|
|
u32 binding{};
|
|
|
|
DefineConstantBuffers(program.info, binding);
|
|
|
|
DefineStorageBuffers(program.info, binding);
|
2021-03-08 21:31:53 +00:00
|
|
|
DefineTextures(program.info, binding);
|
2021-02-19 21:10:18 +00:00
|
|
|
|
2021-02-16 07:10:22 +00:00
|
|
|
DefineLabels(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitContext::~EmitContext() = default;
|
|
|
|
|
|
|
|
Id EmitContext::Def(const IR::Value& value) {
|
|
|
|
if (!value.IsImmediate()) {
|
|
|
|
return value.Inst()->Definition<Id>();
|
|
|
|
}
|
|
|
|
switch (value.Type()) {
|
2021-03-08 21:31:53 +00:00
|
|
|
case IR::Type::Void:
|
|
|
|
// Void instructions are used for optional arguments (e.g. texture offsets)
|
|
|
|
// They are not meant to be used in the SPIR-V module
|
|
|
|
return Id{};
|
2021-02-16 07:10:22 +00:00
|
|
|
case IR::Type::U1:
|
|
|
|
return value.U1() ? true_value : false_value;
|
|
|
|
case IR::Type::U32:
|
|
|
|
return Constant(U32[1], value.U32());
|
|
|
|
case IR::Type::F32:
|
|
|
|
return Constant(F32[1], value.F32());
|
|
|
|
default:
|
|
|
|
throw NotImplementedException("Immediate type {}", value.Type());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitContext::DefineCommonTypes(const Info& info) {
|
|
|
|
void_id = TypeVoid();
|
|
|
|
|
|
|
|
U1 = Name(TypeBool(), "u1");
|
|
|
|
|
2021-02-19 21:10:18 +00:00
|
|
|
// TODO: Conditionally define these
|
|
|
|
AddCapability(spv::Capability::Int16);
|
|
|
|
AddCapability(spv::Capability::Int64);
|
|
|
|
U16 = Name(TypeInt(16, false), "u16");
|
|
|
|
U64 = Name(TypeInt(64, false), "u64");
|
|
|
|
|
2021-02-16 07:10:22 +00:00
|
|
|
F32.Define(*this, TypeFloat(32), "f32");
|
|
|
|
U32.Define(*this, TypeInt(32, false), "u32");
|
|
|
|
|
|
|
|
if (info.uses_fp16) {
|
|
|
|
AddCapability(spv::Capability::Float16);
|
|
|
|
F16.Define(*this, TypeFloat(16), "f16");
|
|
|
|
}
|
|
|
|
if (info.uses_fp64) {
|
|
|
|
AddCapability(spv::Capability::Float64);
|
|
|
|
F64.Define(*this, TypeFloat(64), "f64");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitContext::DefineCommonConstants() {
|
|
|
|
true_value = ConstantTrue(U1);
|
|
|
|
false_value = ConstantFalse(U1);
|
|
|
|
u32_zero_value = Constant(U32[1], 0U);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitContext::DefineSpecialVariables(const Info& info) {
|
|
|
|
const auto define{[this](Id type, spv::BuiltIn builtin, spv::StorageClass storage_class) {
|
|
|
|
const Id pointer_type{TypePointer(storage_class, type)};
|
|
|
|
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::Input)};
|
|
|
|
Decorate(id, spv::Decoration::BuiltIn, builtin);
|
|
|
|
return id;
|
|
|
|
}};
|
|
|
|
using namespace std::placeholders;
|
|
|
|
const auto define_input{std::bind(define, _1, _2, spv::StorageClass::Input)};
|
|
|
|
|
|
|
|
if (info.uses_workgroup_id) {
|
|
|
|
workgroup_id = define_input(U32[3], spv::BuiltIn::WorkgroupId);
|
|
|
|
}
|
|
|
|
if (info.uses_local_invocation_id) {
|
|
|
|
local_invocation_id = define_input(U32[3], spv::BuiltIn::LocalInvocationId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:10:18 +00:00
|
|
|
void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
|
2021-02-16 07:10:22 +00:00
|
|
|
if (info.constant_buffer_descriptors.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const Id array_type{TypeArray(U32[1], Constant(U32[1], 4096))};
|
2021-02-19 21:10:18 +00:00
|
|
|
Decorate(array_type, spv::Decoration::ArrayStride, 4U);
|
2021-02-16 07:10:22 +00:00
|
|
|
|
|
|
|
const Id struct_type{TypeStruct(array_type)};
|
|
|
|
Name(struct_type, "cbuf_block");
|
|
|
|
Decorate(struct_type, spv::Decoration::Block);
|
|
|
|
MemberName(struct_type, 0, "data");
|
|
|
|
MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
|
|
|
|
|
|
|
|
const Id uniform_type{TypePointer(spv::StorageClass::Uniform, struct_type)};
|
|
|
|
uniform_u32 = TypePointer(spv::StorageClass::Uniform, U32[1]);
|
|
|
|
|
2021-02-19 21:10:18 +00:00
|
|
|
u32 index{};
|
2021-03-08 21:31:53 +00:00
|
|
|
for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
|
2021-02-16 07:10:22 +00:00
|
|
|
const Id id{AddGlobalVariable(uniform_type, spv::StorageClass::Uniform)};
|
|
|
|
Decorate(id, spv::Decoration::Binding, binding);
|
2021-02-17 03:59:28 +00:00
|
|
|
Decorate(id, spv::Decoration::DescriptorSet, 0U);
|
2021-02-16 07:10:22 +00:00
|
|
|
Name(id, fmt::format("c{}", desc.index));
|
|
|
|
std::fill_n(cbufs.data() + desc.index, desc.count, id);
|
2021-02-19 21:10:18 +00:00
|
|
|
index += desc.count;
|
2021-02-16 07:10:22 +00:00
|
|
|
binding += desc.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:10:18 +00:00
|
|
|
void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
|
2021-02-16 07:10:22 +00:00
|
|
|
if (info.storage_buffers_descriptors.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AddExtension("SPV_KHR_storage_buffer_storage_class");
|
|
|
|
|
|
|
|
const Id array_type{TypeRuntimeArray(U32[1])};
|
|
|
|
Decorate(array_type, spv::Decoration::ArrayStride, 4U);
|
|
|
|
|
|
|
|
const Id struct_type{TypeStruct(array_type)};
|
|
|
|
Name(struct_type, "ssbo_block");
|
|
|
|
Decorate(struct_type, spv::Decoration::Block);
|
|
|
|
MemberName(struct_type, 0, "data");
|
|
|
|
MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
|
|
|
|
|
|
|
|
const Id storage_type{TypePointer(spv::StorageClass::StorageBuffer, struct_type)};
|
|
|
|
storage_u32 = TypePointer(spv::StorageClass::StorageBuffer, U32[1]);
|
|
|
|
|
2021-02-19 21:10:18 +00:00
|
|
|
u32 index{};
|
2021-03-08 21:31:53 +00:00
|
|
|
for (const StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
|
2021-02-16 07:10:22 +00:00
|
|
|
const Id id{AddGlobalVariable(storage_type, spv::StorageClass::StorageBuffer)};
|
|
|
|
Decorate(id, spv::Decoration::Binding, binding);
|
2021-02-17 03:59:28 +00:00
|
|
|
Decorate(id, spv::Decoration::DescriptorSet, 0U);
|
2021-02-19 21:10:18 +00:00
|
|
|
Name(id, fmt::format("ssbo{}", index));
|
|
|
|
std::fill_n(ssbos.data() + index, desc.count, id);
|
|
|
|
index += desc.count;
|
2021-02-16 07:10:22 +00:00
|
|
|
binding += desc.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
void EmitContext::DefineTextures(const Info& info, u32& binding) {
|
|
|
|
textures.reserve(info.texture_descriptors.size());
|
|
|
|
for (const TextureDescriptor& desc : info.texture_descriptors) {
|
|
|
|
if (desc.count != 1) {
|
|
|
|
throw NotImplementedException("Array of textures");
|
|
|
|
}
|
|
|
|
const Id type{TypeSampledImage(ImageType(*this, desc))};
|
|
|
|
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, type)};
|
|
|
|
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
|
|
|
|
Decorate(id, spv::Decoration::Binding, binding);
|
|
|
|
Decorate(id, spv::Decoration::DescriptorSet, 0U);
|
|
|
|
Name(id, fmt::format("tex{}_{:02x}", desc.cbuf_index, desc.cbuf_offset));
|
|
|
|
for (u32 index = 0; index < desc.count; ++index) {
|
|
|
|
// TODO: Pass count info
|
|
|
|
textures.push_back(TextureDefinition{
|
|
|
|
.id{id},
|
|
|
|
.type{type},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
binding += desc.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 07:10:22 +00:00
|
|
|
void EmitContext::DefineLabels(IR::Program& program) {
|
|
|
|
for (const IR::Function& function : program.functions) {
|
|
|
|
for (IR::Block* const block : function.blocks) {
|
|
|
|
block->SetDefinition(OpLabel());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Backend::SPIRV
|