2024-05-21 22:35:12 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <sirit/sirit.h>
|
|
|
|
|
|
|
|
#include "shader_recompiler/ir/program.h"
|
|
|
|
#include "shader_recompiler/profile.h"
|
|
|
|
#include "shader_recompiler/runtime_info.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV {
|
|
|
|
|
|
|
|
using Sirit::Id;
|
|
|
|
|
|
|
|
struct VectorIds {
|
|
|
|
[[nodiscard]] Id& operator[](u32 index) {
|
|
|
|
return ids[index - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const Id& operator[](u32 index) const {
|
|
|
|
return ids[index - 1];
|
|
|
|
}
|
|
|
|
|
2024-05-28 22:28:34 +00:00
|
|
|
[[nodiscard]] Id& Get(u32 index) {
|
|
|
|
return ids[index - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const Id& Get(u32 index) const {
|
|
|
|
return ids[index - 1];
|
|
|
|
}
|
|
|
|
|
2024-05-21 22:35:12 +00:00
|
|
|
std::array<Id, 4> ids;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EmitContext final : public Sirit::Module {
|
|
|
|
public:
|
2024-05-26 22:07:46 +00:00
|
|
|
explicit EmitContext(const Profile& profile, IR::Program& program, u32& binding);
|
2024-05-21 22:35:12 +00:00
|
|
|
~EmitContext();
|
|
|
|
|
|
|
|
Id Def(const IR::Value& value);
|
|
|
|
|
|
|
|
[[nodiscard]] Id DefineInput(Id type, u32 location) {
|
|
|
|
const Id input_id{DefineVar(type, spv::StorageClass::Input)};
|
|
|
|
Decorate(input_id, spv::Decoration::Location, location);
|
|
|
|
return input_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Id DefineOutput(Id type, std::optional<u32> location = std::nullopt) {
|
|
|
|
const Id output_id{DefineVar(type, spv::StorageClass::Output)};
|
|
|
|
if (location) {
|
|
|
|
Decorate(output_id, spv::Decoration::Location, *location);
|
|
|
|
}
|
|
|
|
return output_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Id DefineUniformConst(Id type, u32 set, u32 binding, bool readonly = false) {
|
|
|
|
const Id uniform_id{DefineVar(type, spv::StorageClass::UniformConstant)};
|
|
|
|
Decorate(uniform_id, spv::Decoration::DescriptorSet, set);
|
|
|
|
Decorate(uniform_id, spv::Decoration::Binding, binding);
|
|
|
|
if (readonly) {
|
|
|
|
Decorate(uniform_id, spv::Decoration::NonWritable);
|
|
|
|
}
|
|
|
|
return uniform_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = true>
|
|
|
|
[[nodiscard]] Id DefineVar(Id type, spv::StorageClass storage_class) {
|
|
|
|
const Id pointer_type_id{TypePointer(storage_class, type)};
|
|
|
|
return global ? AddGlobalVariable(pointer_type_id, storage_class)
|
|
|
|
: AddLocalVariable(pointer_type_id, storage_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Id DefineVariable(Id type, std::optional<spv::BuiltIn> builtin,
|
|
|
|
spv::StorageClass storage_class) {
|
|
|
|
const Id id{DefineVar(type, storage_class)};
|
|
|
|
if (builtin) {
|
|
|
|
Decorate(id, spv::Decoration::BuiltIn, *builtin);
|
|
|
|
}
|
|
|
|
interfaces.push_back(id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Id ConstU32(u32 value) {
|
|
|
|
return Constant(U32[1], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
[[nodiscard]] Id ConstU32(Args&&... values) {
|
|
|
|
constexpr u32 size = static_cast<u32>(sizeof...(values));
|
|
|
|
static_assert(size >= 2);
|
|
|
|
const std::array constituents{Constant(U32[1], values)...};
|
|
|
|
const Id type = size <= 4 ? U32[size] : TypeArray(U32[1], ConstU32(size));
|
|
|
|
return ConstantComposite(type, constituents);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Id ConstS32(s32 value) {
|
|
|
|
return Constant(S32[1], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
[[nodiscard]] Id ConstS32(Args&&... values) {
|
|
|
|
constexpr u32 size = static_cast<u32>(sizeof...(values));
|
|
|
|
static_assert(size >= 2);
|
|
|
|
const std::array constituents{Constant(S32[1], values)...};
|
|
|
|
const Id type = size <= 4 ? S32[size] : TypeArray(S32[1], ConstU32(size));
|
|
|
|
return ConstantComposite(type, constituents);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Id ConstF32(f32 value) {
|
|
|
|
return Constant(F32[1], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
[[nodiscard]] Id ConstF32(Args... values) {
|
|
|
|
constexpr u32 size = static_cast<u32>(sizeof...(values));
|
|
|
|
static_assert(size >= 2);
|
|
|
|
const std::array constituents{Constant(F32[1], values)...};
|
|
|
|
const Id type = size <= 4 ? F32[size] : TypeArray(F32[1], ConstU32(size));
|
|
|
|
return ConstantComposite(type, constituents);
|
|
|
|
}
|
|
|
|
|
2024-05-26 12:51:35 +00:00
|
|
|
Info& info;
|
2024-05-21 22:35:12 +00:00
|
|
|
const Profile& profile;
|
|
|
|
Stage stage{};
|
|
|
|
|
|
|
|
Id void_id{};
|
|
|
|
Id U8{};
|
|
|
|
Id S8{};
|
|
|
|
Id U16{};
|
|
|
|
Id S16{};
|
|
|
|
Id U64{};
|
|
|
|
VectorIds F16{};
|
|
|
|
VectorIds F32{};
|
|
|
|
VectorIds F64{};
|
|
|
|
VectorIds S32{};
|
|
|
|
VectorIds U32{};
|
|
|
|
VectorIds U1{};
|
|
|
|
|
2024-06-16 18:39:53 +00:00
|
|
|
Id full_result_i32x2;
|
|
|
|
Id full_result_u32x2;
|
|
|
|
|
2024-05-21 22:35:12 +00:00
|
|
|
Id true_value{};
|
|
|
|
Id false_value{};
|
2024-06-01 17:25:31 +00:00
|
|
|
Id u32_one_value{};
|
2024-05-21 22:35:12 +00:00
|
|
|
Id u32_zero_value{};
|
|
|
|
Id f32_zero_value{};
|
|
|
|
|
2024-05-25 12:33:15 +00:00
|
|
|
Id input_u32{};
|
|
|
|
Id input_f32{};
|
|
|
|
Id input_s32{};
|
2024-05-21 22:35:12 +00:00
|
|
|
Id output_u32{};
|
|
|
|
Id output_f32{};
|
|
|
|
|
|
|
|
boost::container::small_vector<Id, 16> interfaces;
|
|
|
|
|
|
|
|
Id output_position{};
|
|
|
|
Id vertex_index{};
|
|
|
|
Id base_vertex{};
|
2024-06-01 17:25:31 +00:00
|
|
|
Id frag_coord{};
|
|
|
|
Id front_facing{};
|
2024-05-21 22:35:12 +00:00
|
|
|
std::array<Id, 8> frag_color{};
|
2024-06-10 19:35:14 +00:00
|
|
|
std::array<u32, 8> frag_num_comp{};
|
2024-05-21 22:35:12 +00:00
|
|
|
|
2024-05-28 22:28:34 +00:00
|
|
|
Id workgroup_id{};
|
|
|
|
Id local_invocation_id{};
|
2024-06-10 19:35:14 +00:00
|
|
|
Id subgroup_local_invocation_id{};
|
2024-05-28 22:28:34 +00:00
|
|
|
|
2024-05-26 22:07:46 +00:00
|
|
|
struct TextureDefinition {
|
|
|
|
Id id;
|
|
|
|
Id sampled_type;
|
|
|
|
Id pointer_type;
|
|
|
|
Id image_type;
|
|
|
|
};
|
|
|
|
|
2024-05-28 22:28:34 +00:00
|
|
|
struct BufferDefinition {
|
|
|
|
Id id;
|
|
|
|
const VectorIds* data_types;
|
|
|
|
Id pointer_type;
|
|
|
|
};
|
|
|
|
|
2024-05-26 22:07:46 +00:00
|
|
|
u32& binding;
|
2024-05-28 22:28:34 +00:00
|
|
|
boost::container::small_vector<BufferDefinition, 4> buffers;
|
2024-05-26 22:07:46 +00:00
|
|
|
boost::container::small_vector<TextureDefinition, 4> images;
|
|
|
|
boost::container::small_vector<Id, 4> samplers;
|
|
|
|
|
|
|
|
Id sampler_type{};
|
|
|
|
Id sampler_pointer_type{};
|
2024-05-26 12:51:35 +00:00
|
|
|
|
2024-05-25 12:33:15 +00:00
|
|
|
struct SpirvAttribute {
|
2024-05-21 22:35:12 +00:00
|
|
|
Id id;
|
|
|
|
Id pointer_type;
|
|
|
|
Id component_type;
|
2024-05-25 12:33:15 +00:00
|
|
|
u32 num_components;
|
2024-05-21 22:35:12 +00:00
|
|
|
};
|
2024-05-25 12:33:15 +00:00
|
|
|
std::array<SpirvAttribute, 32> input_params{};
|
|
|
|
std::array<SpirvAttribute, 32> output_params{};
|
2024-05-21 22:35:12 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void DefineArithmeticTypes();
|
|
|
|
void DefineInterfaces(const IR::Program& program);
|
2024-05-26 12:51:35 +00:00
|
|
|
void DefineInputs(const Info& info);
|
|
|
|
void DefineOutputs(const Info& info);
|
|
|
|
void DefineBuffers(const Info& info);
|
2024-05-26 22:07:46 +00:00
|
|
|
void DefineImagesAndSamplers(const Info& info);
|
2024-05-25 12:33:15 +00:00
|
|
|
|
|
|
|
SpirvAttribute GetAttributeInfo(AmdGpu::NumberFormat fmt, Id id);
|
2024-05-21 22:35:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Shader::Backend::SPIRV
|