spirv: Initial bindings support
parent
d5d468cf2c
commit
b5d7279d87
@ -1 +1 @@
|
||||
Subproject commit f819ade0efe925a782090dea9e1bf300fedffb39
|
||||
Subproject commit 200310e8faa756b9869dd6dfc902c255246ac74a
|
@ -0,0 +1,160 @@
|
||||
// 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 {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
EmitContext::EmitContext(IR::Program& program) : Sirit::Module(0x00010000) {
|
||||
AddCapability(spv::Capability::Shader);
|
||||
DefineCommonTypes(program.info);
|
||||
DefineCommonConstants();
|
||||
DefineSpecialVariables(program.info);
|
||||
DefineConstantBuffers(program.info);
|
||||
DefineStorageBuffers(program.info);
|
||||
DefineLabels(program);
|
||||
}
|
||||
|
||||
EmitContext::~EmitContext() = default;
|
||||
|
||||
Id EmitContext::Def(const IR::Value& value) {
|
||||
if (!value.IsImmediate()) {
|
||||
return value.Inst()->Definition<Id>();
|
||||
}
|
||||
switch (value.Type()) {
|
||||
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");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void EmitContext::DefineConstantBuffers(const Info& info) {
|
||||
if (info.constant_buffer_descriptors.empty()) {
|
||||
return;
|
||||
}
|
||||
const Id array_type{TypeArray(U32[1], Constant(U32[1], 4096))};
|
||||
Decorate(array_type, spv::Decoration::ArrayStride, 16U);
|
||||
|
||||
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]);
|
||||
|
||||
u32 binding{};
|
||||
for (const Info::ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
|
||||
const Id id{AddGlobalVariable(uniform_type, spv::StorageClass::Uniform)};
|
||||
Decorate(id, spv::Decoration::Binding, binding);
|
||||
Name(id, fmt::format("c{}", desc.index));
|
||||
std::fill_n(cbufs.data() + desc.index, desc.count, id);
|
||||
binding += desc.count;
|
||||
}
|
||||
}
|
||||
|
||||
void EmitContext::DefineStorageBuffers(const Info& info) {
|
||||
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]);
|
||||
|
||||
u32 binding{};
|
||||
for (const Info::StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
|
||||
const Id id{AddGlobalVariable(storage_type, spv::StorageClass::StorageBuffer)};
|
||||
Decorate(id, spv::Decoration::Binding, binding);
|
||||
Name(id, fmt::format("ssbo{}", binding));
|
||||
std::fill_n(ssbos.data() + binding, desc.count, id);
|
||||
binding += desc.count;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
@ -0,0 +1,67 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
|
||||
#include <sirit/sirit.h>
|
||||
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
namespace Shader::Backend::SPIRV {
|
||||
|
||||
using Sirit::Id;
|
||||
|
||||
class VectorTypes {
|
||||
public:
|
||||
void Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name);
|
||||
|
||||
[[nodiscard]] Id operator[](size_t size) const noexcept {
|
||||
return defs[size - 1];
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<Id, 4> defs{};
|
||||
};
|
||||
|
||||
class EmitContext final : public Sirit::Module {
|
||||
public:
|
||||
explicit EmitContext(IR::Program& program);
|
||||
~EmitContext();
|
||||
|
||||
[[nodiscard]] Id Def(const IR::Value& value);
|
||||
|
||||
Id void_id{};
|
||||
Id U1{};
|
||||
VectorTypes F32;
|
||||
VectorTypes U32;
|
||||
VectorTypes F16;
|
||||
VectorTypes F64;
|
||||
|
||||
Id true_value{};
|
||||
Id false_value{};
|
||||
Id u32_zero_value{};
|
||||
|
||||
Id uniform_u32{};
|
||||
Id storage_u32{};
|
||||
|
||||
std::array<Id, Info::MAX_CBUFS> cbufs{};
|
||||
std::array<Id, Info::MAX_SSBOS> ssbos{};
|
||||
|
||||
Id workgroup_id{};
|
||||
Id local_invocation_id{};
|
||||
|
||||
private:
|
||||
void DefineCommonTypes(const Info& info);
|
||||
void DefineCommonConstants();
|
||||
void DefineSpecialVariables(const Info& info);
|
||||
void DefineConstantBuffers(const Info& info);
|
||||
void DefineStorageBuffers(const Info& info);
|
||||
void DefineLabels(IR::Program& program);
|
||||
};
|
||||
|
||||
} // namespace Shader::Backend::SPIRV
|
@ -0,0 +1,81 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
void AddConstantBufferDescriptor(Info& info, u32 index) {
|
||||
auto& descriptor{info.constant_buffers.at(index)};
|
||||
if (descriptor) {
|
||||
return;
|
||||
}
|
||||
descriptor = &info.constant_buffer_descriptors.emplace_back(Info::ConstantBufferDescriptor{
|
||||
.index{index},
|
||||
.count{1},
|
||||
});
|
||||
}
|
||||
|
||||
void Visit(Info& info, IR::Inst& inst) {
|
||||
switch (inst.Opcode()) {
|
||||
case IR::Opcode::WorkgroupId:
|
||||
info.uses_workgroup_id = true;
|
||||
break;
|
||||
case IR::Opcode::LocalInvocationId:
|
||||
info.uses_local_invocation_id = true;
|
||||
break;
|
||||
case IR::Opcode::FPAbs16:
|
||||
case IR::Opcode::FPAdd16:
|
||||
case IR::Opcode::FPCeil16:
|
||||
case IR::Opcode::FPFloor16:
|
||||
case IR::Opcode::FPFma16:
|
||||
case IR::Opcode::FPMul16:
|
||||
case IR::Opcode::FPNeg16:
|
||||
case IR::Opcode::FPRoundEven16:
|
||||
case IR::Opcode::FPSaturate16:
|
||||
case IR::Opcode::FPTrunc16:
|
||||
info.uses_fp16;
|
||||
break;
|
||||
case IR::Opcode::FPAbs64:
|
||||
case IR::Opcode::FPAdd64:
|
||||
case IR::Opcode::FPCeil64:
|
||||
case IR::Opcode::FPFloor64:
|
||||
case IR::Opcode::FPFma64:
|
||||
case IR::Opcode::FPMax64:
|
||||
case IR::Opcode::FPMin64:
|
||||
case IR::Opcode::FPMul64:
|
||||
case IR::Opcode::FPNeg64:
|
||||
case IR::Opcode::FPRecip64:
|
||||
case IR::Opcode::FPRecipSqrt64:
|
||||
case IR::Opcode::FPRoundEven64:
|
||||
case IR::Opcode::FPSaturate64:
|
||||
case IR::Opcode::FPTrunc64:
|
||||
info.uses_fp64 = true;
|
||||
break;
|
||||
case IR::Opcode::GetCbuf:
|
||||
if (const IR::Value index{inst.Arg(0)}; index.IsImmediate()) {
|
||||
AddConstantBufferDescriptor(info, index.U32());
|
||||
} else {
|
||||
throw NotImplementedException("Constant buffer with non-immediate index");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void CollectShaderInfoPass(IR::Program& program) {
|
||||
Info& info{program.info};
|
||||
for (IR::Function& function : program.functions) {
|
||||
for (IR::Block* const block : function.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
Visit(info, inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
Loading…
Reference in New Issue