2021-02-08 05:54:35 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
#include <span>
|
|
|
|
#include <tuple>
|
2021-02-08 05:54:35 +00:00
|
|
|
#include <type_traits>
|
2021-02-17 03:59:28 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2021-02-08 05:54:35 +00:00
|
|
|
|
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
2021-05-03 23:53:00 +00:00
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
|
2021-02-08 05:54:35 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/program.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV {
|
2021-02-16 07:10:22 +00:00
|
|
|
namespace {
|
|
|
|
template <class Func>
|
2021-04-06 02:25:22 +00:00
|
|
|
struct FuncTraits {};
|
2021-02-08 05:54:35 +00:00
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
template <class ReturnType_, class... Args>
|
|
|
|
struct FuncTraits<ReturnType_ (*)(Args...)> {
|
2021-02-16 07:10:22 +00:00
|
|
|
using ReturnType = ReturnType_;
|
2021-02-08 05:54:35 +00:00
|
|
|
|
2021-02-16 07:10:22 +00:00
|
|
|
static constexpr size_t NUM_ARGS = sizeof...(Args);
|
2021-02-08 05:54:35 +00:00
|
|
|
|
2021-02-16 07:10:22 +00:00
|
|
|
template <size_t I>
|
|
|
|
using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
|
|
|
|
};
|
2021-02-14 04:24:32 +00:00
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
template <auto func, typename... Args>
|
|
|
|
void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) {
|
2021-04-11 05:08:15 +00:00
|
|
|
inst->SetDefinition<Id>(func(ctx, std::forward<Args>(args)...));
|
2021-02-16 07:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ArgType>
|
|
|
|
ArgType Arg(EmitContext& ctx, const IR::Value& arg) {
|
|
|
|
if constexpr (std::is_same_v<ArgType, Id>) {
|
|
|
|
return ctx.Def(arg);
|
|
|
|
} else if constexpr (std::is_same_v<ArgType, const IR::Value&>) {
|
|
|
|
return arg;
|
|
|
|
} else if constexpr (std::is_same_v<ArgType, u32>) {
|
|
|
|
return arg.U32();
|
2021-03-19 22:28:31 +00:00
|
|
|
} else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
|
|
|
|
return arg.Attribute();
|
2021-04-16 01:46:11 +00:00
|
|
|
} else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
|
|
|
|
return arg.Patch();
|
2021-04-02 04:17:47 +00:00
|
|
|
} else if constexpr (std::is_same_v<ArgType, IR::Reg>) {
|
|
|
|
return arg.Reg();
|
2021-02-16 07:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
template <auto func, bool is_first_arg_inst, size_t... I>
|
|
|
|
void Invoke(EmitContext& ctx, IR::Inst* inst, std::index_sequence<I...>) {
|
|
|
|
using Traits = FuncTraits<decltype(func)>;
|
2021-04-06 02:25:22 +00:00
|
|
|
if constexpr (std::is_same_v<typename Traits::ReturnType, Id>) {
|
2021-02-16 07:10:22 +00:00
|
|
|
if constexpr (is_first_arg_inst) {
|
2021-04-06 02:25:22 +00:00
|
|
|
SetDefinition<func>(
|
|
|
|
ctx, inst, inst,
|
|
|
|
Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
|
2021-02-16 07:10:22 +00:00
|
|
|
} else {
|
2021-04-06 02:25:22 +00:00
|
|
|
SetDefinition<func>(
|
|
|
|
ctx, inst, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
|
2021-02-16 07:10:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if constexpr (is_first_arg_inst) {
|
2021-04-06 02:25:22 +00:00
|
|
|
func(ctx, inst, Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
|
2021-02-16 07:10:22 +00:00
|
|
|
} else {
|
2021-04-06 02:25:22 +00:00
|
|
|
func(ctx, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
|
2021-02-08 05:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
template <auto func>
|
|
|
|
void Invoke(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
using Traits = FuncTraits<decltype(func)>;
|
2021-02-16 07:10:22 +00:00
|
|
|
static_assert(Traits::NUM_ARGS >= 1, "Insufficient arguments");
|
|
|
|
if constexpr (Traits::NUM_ARGS == 1) {
|
2021-02-17 03:59:28 +00:00
|
|
|
Invoke<func, false>(ctx, inst, std::make_index_sequence<0>{});
|
2021-02-16 07:10:22 +00:00
|
|
|
} else {
|
|
|
|
using FirstArgType = typename Traits::template ArgType<1>;
|
|
|
|
static constexpr bool is_first_arg_inst = std::is_same_v<FirstArgType, IR::Inst*>;
|
|
|
|
using Indices = std::make_index_sequence<Traits::NUM_ARGS - (is_first_arg_inst ? 2 : 1)>;
|
2021-02-17 03:59:28 +00:00
|
|
|
Invoke<func, is_first_arg_inst>(ctx, inst, Indices{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitInst(EmitContext& ctx, IR::Inst* inst) {
|
2021-04-06 02:25:22 +00:00
|
|
|
switch (inst->GetOpcode()) {
|
2021-02-17 03:59:28 +00:00
|
|
|
#define OPCODE(name, result_type, ...) \
|
|
|
|
case IR::Opcode::name: \
|
|
|
|
return Invoke<&Emit##name>(ctx, inst);
|
|
|
|
#include "shader_recompiler/frontend/ir/opcodes.inc"
|
|
|
|
#undef OPCODE
|
|
|
|
}
|
2021-04-06 02:25:22 +00:00
|
|
|
throw LogicError("Invalid opcode {}", inst->GetOpcode());
|
2021-02-17 03:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Id TypeId(const EmitContext& ctx, IR::Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case IR::Type::U1:
|
|
|
|
return ctx.U1;
|
|
|
|
case IR::Type::U32:
|
|
|
|
return ctx.U32[1];
|
|
|
|
default:
|
|
|
|
throw NotImplementedException("Phi node type {}", type);
|
2021-02-16 07:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 06:30:13 +00:00
|
|
|
|
2021-05-14 03:40:54 +00:00
|
|
|
void Traverse(EmitContext& ctx, IR::Program& program) {
|
|
|
|
IR::Block* current_block{};
|
|
|
|
for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
|
|
|
|
switch (node.type) {
|
2021-05-15 01:01:01 +00:00
|
|
|
case IR::AbstractSyntaxNode::Type::Block: {
|
2021-05-16 21:06:13 +00:00
|
|
|
const Id label{node.data.block->Definition<Id>()};
|
2021-05-14 03:40:54 +00:00
|
|
|
if (current_block) {
|
|
|
|
ctx.OpBranch(label);
|
|
|
|
}
|
2021-05-16 21:06:13 +00:00
|
|
|
current_block = node.data.block;
|
2021-05-14 03:40:54 +00:00
|
|
|
ctx.AddLabel(label);
|
2021-05-16 21:06:13 +00:00
|
|
|
for (IR::Inst& inst : node.data.block->Instructions()) {
|
2021-05-14 03:40:54 +00:00
|
|
|
EmitInst(ctx, &inst);
|
|
|
|
}
|
|
|
|
break;
|
2021-05-15 01:01:01 +00:00
|
|
|
}
|
2021-05-14 03:40:54 +00:00
|
|
|
case IR::AbstractSyntaxNode::Type::If: {
|
2021-05-16 21:06:13 +00:00
|
|
|
const Id if_label{node.data.if_node.body->Definition<Id>()};
|
|
|
|
const Id endif_label{node.data.if_node.merge->Definition<Id>()};
|
2021-05-14 03:40:54 +00:00
|
|
|
ctx.OpSelectionMerge(endif_label, spv::SelectionControlMask::MaskNone);
|
2021-05-16 21:06:13 +00:00
|
|
|
ctx.OpBranchConditional(ctx.Def(node.data.if_node.cond), if_label, endif_label);
|
2021-05-14 03:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IR::AbstractSyntaxNode::Type::Loop: {
|
2021-05-16 21:06:13 +00:00
|
|
|
const Id body_label{node.data.loop.body->Definition<Id>()};
|
|
|
|
const Id continue_label{node.data.loop.continue_block->Definition<Id>()};
|
|
|
|
const Id endloop_label{node.data.loop.merge->Definition<Id>()};
|
2021-05-14 03:40:54 +00:00
|
|
|
|
|
|
|
ctx.OpLoopMerge(endloop_label, continue_label, spv::LoopControlMask::MaskNone);
|
2021-05-15 21:17:40 +00:00
|
|
|
ctx.OpBranch(body_label);
|
2021-05-14 03:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IR::AbstractSyntaxNode::Type::Break: {
|
2021-05-16 21:06:13 +00:00
|
|
|
const Id break_label{node.data.break_node.merge->Definition<Id>()};
|
|
|
|
const Id skip_label{node.data.break_node.skip->Definition<Id>()};
|
|
|
|
ctx.OpBranchConditional(ctx.Def(node.data.break_node.cond), break_label, skip_label);
|
2021-05-14 03:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IR::AbstractSyntaxNode::Type::EndIf:
|
|
|
|
if (current_block) {
|
2021-05-16 21:06:13 +00:00
|
|
|
ctx.OpBranch(node.data.end_if.merge->Definition<Id>());
|
2021-05-14 03:40:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::AbstractSyntaxNode::Type::Repeat: {
|
2021-05-16 21:06:13 +00:00
|
|
|
const Id loop_header_label{node.data.repeat.loop_header->Definition<Id>()};
|
|
|
|
const Id merge_label{node.data.repeat.merge->Definition<Id>()};
|
|
|
|
ctx.OpBranchConditional(ctx.Def(node.data.repeat.cond), loop_header_label, merge_label);
|
2021-05-14 03:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IR::AbstractSyntaxNode::Type::Return:
|
|
|
|
ctx.OpReturn();
|
|
|
|
break;
|
|
|
|
case IR::AbstractSyntaxNode::Type::Unreachable:
|
|
|
|
ctx.OpUnreachable();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (node.type != IR::AbstractSyntaxNode::Type::Block) {
|
|
|
|
current_block = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:11:56 +00:00
|
|
|
Id DefineMain(EmitContext& ctx, IR::Program& program) {
|
|
|
|
const Id void_function{ctx.TypeFunction(ctx.void_id)};
|
|
|
|
const Id main{ctx.OpFunction(ctx.void_id, spv::FunctionControlMask::MaskNone, void_function)};
|
|
|
|
for (IR::Block* const block : program.blocks) {
|
2021-05-14 03:40:54 +00:00
|
|
|
block->SetDefinition(ctx.OpLabel());
|
2021-03-20 22:11:56 +00:00
|
|
|
}
|
2021-05-14 03:40:54 +00:00
|
|
|
Traverse(ctx, program);
|
2021-03-20 22:11:56 +00:00
|
|
|
ctx.OpFunctionEnd();
|
|
|
|
return main;
|
|
|
|
}
|
|
|
|
|
2021-04-16 01:46:11 +00:00
|
|
|
spv::ExecutionMode ExecutionMode(TessPrimitive primitive) {
|
|
|
|
switch (primitive) {
|
|
|
|
case TessPrimitive::Isolines:
|
|
|
|
return spv::ExecutionMode::Isolines;
|
|
|
|
case TessPrimitive::Triangles:
|
|
|
|
return spv::ExecutionMode::Triangles;
|
|
|
|
case TessPrimitive::Quads:
|
|
|
|
return spv::ExecutionMode::Quads;
|
|
|
|
}
|
|
|
|
throw InvalidArgument("Tessellation primitive {}", primitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
spv::ExecutionMode ExecutionMode(TessSpacing spacing) {
|
|
|
|
switch (spacing) {
|
|
|
|
case TessSpacing::Equal:
|
|
|
|
return spv::ExecutionMode::SpacingEqual;
|
|
|
|
case TessSpacing::FractionalOdd:
|
|
|
|
return spv::ExecutionMode::SpacingFractionalOdd;
|
|
|
|
case TessSpacing::FractionalEven:
|
|
|
|
return spv::ExecutionMode::SpacingFractionalEven;
|
|
|
|
}
|
|
|
|
throw InvalidArgument("Tessellation spacing {}", spacing);
|
|
|
|
}
|
|
|
|
|
2021-03-27 06:08:31 +00:00
|
|
|
void DefineEntryPoint(const IR::Program& program, EmitContext& ctx, Id main) {
|
2021-03-20 22:11:56 +00:00
|
|
|
const std::span interfaces(ctx.interfaces.data(), ctx.interfaces.size());
|
|
|
|
spv::ExecutionModel execution_model{};
|
2021-03-26 21:45:38 +00:00
|
|
|
switch (program.stage) {
|
2021-04-14 21:09:18 +00:00
|
|
|
case Stage::Compute: {
|
2021-03-27 06:08:31 +00:00
|
|
|
const std::array<u32, 3> workgroup_size{program.workgroup_size};
|
2021-03-20 22:11:56 +00:00
|
|
|
execution_model = spv::ExecutionModel::GLCompute;
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::LocalSize, workgroup_size[0],
|
|
|
|
workgroup_size[1], workgroup_size[2]);
|
|
|
|
break;
|
|
|
|
}
|
2021-04-14 21:09:18 +00:00
|
|
|
case Stage::VertexB:
|
2021-03-20 22:11:56 +00:00
|
|
|
execution_model = spv::ExecutionModel::Vertex;
|
|
|
|
break;
|
2021-04-16 01:46:11 +00:00
|
|
|
case Stage::TessellationControl:
|
|
|
|
execution_model = spv::ExecutionModel::TessellationControl;
|
|
|
|
ctx.AddCapability(spv::Capability::Tessellation);
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OutputVertices, program.invocations);
|
|
|
|
break;
|
|
|
|
case Stage::TessellationEval:
|
|
|
|
execution_model = spv::ExecutionModel::TessellationEvaluation;
|
|
|
|
ctx.AddCapability(spv::Capability::Tessellation);
|
2021-05-21 05:12:32 +00:00
|
|
|
ctx.AddExecutionMode(main, ExecutionMode(ctx.runtime_info.tess_primitive));
|
|
|
|
ctx.AddExecutionMode(main, ExecutionMode(ctx.runtime_info.tess_spacing));
|
|
|
|
ctx.AddExecutionMode(main, ctx.runtime_info.tess_clockwise
|
|
|
|
? spv::ExecutionMode::VertexOrderCw
|
|
|
|
: spv::ExecutionMode::VertexOrderCcw);
|
2021-04-16 01:46:11 +00:00
|
|
|
break;
|
2021-04-14 21:09:18 +00:00
|
|
|
case Stage::Geometry:
|
2021-04-12 22:41:22 +00:00
|
|
|
execution_model = spv::ExecutionModel::Geometry;
|
|
|
|
ctx.AddCapability(spv::Capability::Geometry);
|
|
|
|
ctx.AddCapability(spv::Capability::GeometryStreams);
|
2021-05-21 05:12:32 +00:00
|
|
|
switch (ctx.runtime_info.input_topology) {
|
2021-04-12 22:41:22 +00:00
|
|
|
case InputTopology::Points:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::InputPoints);
|
|
|
|
break;
|
|
|
|
case InputTopology::Lines:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::InputLines);
|
|
|
|
break;
|
|
|
|
case InputTopology::LinesAdjacency:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::InputLinesAdjacency);
|
|
|
|
break;
|
|
|
|
case InputTopology::Triangles:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::Triangles);
|
|
|
|
break;
|
|
|
|
case InputTopology::TrianglesAdjacency:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::InputTrianglesAdjacency);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (program.output_topology) {
|
|
|
|
case OutputTopology::PointList:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OutputPoints);
|
|
|
|
break;
|
|
|
|
case OutputTopology::LineStrip:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OutputLineStrip);
|
|
|
|
break;
|
|
|
|
case OutputTopology::TriangleStrip:
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OutputTriangleStrip);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (program.info.stores_point_size) {
|
|
|
|
ctx.AddCapability(spv::Capability::GeometryPointSize);
|
|
|
|
}
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OutputVertices, program.output_vertices);
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::Invocations, program.invocations);
|
|
|
|
break;
|
2021-04-14 21:09:18 +00:00
|
|
|
case Stage::Fragment:
|
2021-03-20 22:11:56 +00:00
|
|
|
execution_model = spv::ExecutionModel::Fragment;
|
2021-05-23 07:18:22 +00:00
|
|
|
if (ctx.profile.lower_left_origin_mode) {
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OriginLowerLeft);
|
|
|
|
} else {
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::OriginUpperLeft);
|
|
|
|
}
|
2021-03-26 21:45:38 +00:00
|
|
|
if (program.info.stores_frag_depth) {
|
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::DepthReplacing);
|
|
|
|
}
|
2021-05-21 05:12:32 +00:00
|
|
|
if (ctx.runtime_info.force_early_z) {
|
2021-04-13 19:56:22 +00:00
|
|
|
ctx.AddExecutionMode(main, spv::ExecutionMode::EarlyFragmentTests);
|
|
|
|
}
|
2021-03-20 22:11:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-03-27 06:08:31 +00:00
|
|
|
throw NotImplementedException("Stage {}", program.stage);
|
2021-03-20 22:11:56 +00:00
|
|
|
}
|
|
|
|
ctx.AddEntryPoint(execution_model, main, "main", interfaces);
|
|
|
|
}
|
|
|
|
|
2021-02-20 06:30:13 +00:00
|
|
|
void SetupDenormControl(const Profile& profile, const IR::Program& program, EmitContext& ctx,
|
|
|
|
Id main_func) {
|
|
|
|
const Info& info{program.info};
|
|
|
|
if (info.uses_fp32_denorms_flush && info.uses_fp32_denorms_preserve) {
|
|
|
|
// LOG_ERROR(HW_GPU, "Fp32 denorm flush and preserve on the same shader");
|
|
|
|
} else if (info.uses_fp32_denorms_flush) {
|
|
|
|
if (profile.support_fp32_denorm_flush) {
|
|
|
|
ctx.AddCapability(spv::Capability::DenormFlushToZero);
|
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::DenormFlushToZero, 32U);
|
|
|
|
} else {
|
|
|
|
// Drivers will most likely flush denorms by default, no need to warn
|
|
|
|
}
|
|
|
|
} else if (info.uses_fp32_denorms_preserve) {
|
|
|
|
if (profile.support_fp32_denorm_preserve) {
|
|
|
|
ctx.AddCapability(spv::Capability::DenormPreserve);
|
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::DenormPreserve, 32U);
|
|
|
|
} else {
|
|
|
|
// LOG_WARNING(HW_GPU, "Fp32 denorm preserve used in shader without host support");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!profile.support_separate_denorm_behavior) {
|
|
|
|
// No separate denorm behavior
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (info.uses_fp16_denorms_flush && info.uses_fp16_denorms_preserve) {
|
|
|
|
// LOG_ERROR(HW_GPU, "Fp16 denorm flush and preserve on the same shader");
|
|
|
|
} else if (info.uses_fp16_denorms_flush) {
|
|
|
|
if (profile.support_fp16_denorm_flush) {
|
|
|
|
ctx.AddCapability(spv::Capability::DenormFlushToZero);
|
2021-02-22 02:42:38 +00:00
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::DenormFlushToZero, 16U);
|
2021-02-20 06:30:13 +00:00
|
|
|
} else {
|
|
|
|
// Same as fp32, no need to warn as most drivers will flush by default
|
|
|
|
}
|
2021-02-22 02:42:38 +00:00
|
|
|
} else if (info.uses_fp16_denorms_preserve) {
|
2021-02-20 06:30:13 +00:00
|
|
|
if (profile.support_fp16_denorm_preserve) {
|
|
|
|
ctx.AddCapability(spv::Capability::DenormPreserve);
|
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::DenormPreserve, 16U);
|
|
|
|
} else {
|
|
|
|
// LOG_WARNING(HW_GPU, "Fp16 denorm preserve used in shader without host support");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 21:37:47 +00:00
|
|
|
|
2021-03-21 23:28:37 +00:00
|
|
|
void SetupSignedNanCapabilities(const Profile& profile, const IR::Program& program,
|
|
|
|
EmitContext& ctx, Id main_func) {
|
|
|
|
if (program.info.uses_fp16 && profile.support_fp16_signed_zero_nan_preserve) {
|
|
|
|
ctx.AddCapability(spv::Capability::SignedZeroInfNanPreserve);
|
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::SignedZeroInfNanPreserve, 16U);
|
|
|
|
}
|
|
|
|
if (profile.support_fp32_signed_zero_nan_preserve) {
|
|
|
|
ctx.AddCapability(spv::Capability::SignedZeroInfNanPreserve);
|
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::SignedZeroInfNanPreserve, 32U);
|
|
|
|
}
|
|
|
|
if (program.info.uses_fp64 && profile.support_fp64_signed_zero_nan_preserve) {
|
|
|
|
ctx.AddCapability(spv::Capability::SignedZeroInfNanPreserve);
|
|
|
|
ctx.AddExecutionMode(main_func, spv::ExecutionMode::SignedZeroInfNanPreserve, 64U);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:11:56 +00:00
|
|
|
void SetupCapabilities(const Profile& profile, const Info& info, EmitContext& ctx) {
|
|
|
|
if (info.uses_sampled_1d) {
|
|
|
|
ctx.AddCapability(spv::Capability::Sampled1D);
|
|
|
|
}
|
|
|
|
if (info.uses_sparse_residency) {
|
|
|
|
ctx.AddCapability(spv::Capability::SparseResidency);
|
|
|
|
}
|
2021-05-23 07:18:55 +00:00
|
|
|
if (info.uses_demote_to_helper_invocation && profile.support_demote_to_helper_invocation) {
|
2021-03-20 22:11:56 +00:00
|
|
|
ctx.AddExtension("SPV_EXT_demote_to_helper_invocation");
|
|
|
|
ctx.AddCapability(spv::Capability::DemoteToHelperInvocationEXT);
|
|
|
|
}
|
2021-04-01 06:34:45 +00:00
|
|
|
if (info.stores_viewport_index) {
|
|
|
|
ctx.AddCapability(spv::Capability::MultiViewport);
|
2021-04-14 21:09:18 +00:00
|
|
|
}
|
2021-04-16 19:31:15 +00:00
|
|
|
if (info.stores_viewport_mask && profile.support_viewport_mask) {
|
|
|
|
ctx.AddExtension("SPV_NV_viewport_array2");
|
|
|
|
ctx.AddCapability(spv::Capability::ShaderViewportMaskNV);
|
|
|
|
}
|
2021-04-14 21:09:18 +00:00
|
|
|
if (info.stores_layer || info.stores_viewport_index) {
|
|
|
|
if (profile.support_viewport_index_layer_non_geometry && ctx.stage != Stage::Geometry) {
|
2021-04-01 06:34:45 +00:00
|
|
|
ctx.AddExtension("SPV_EXT_shader_viewport_index_layer");
|
|
|
|
ctx.AddCapability(spv::Capability::ShaderViewportIndexLayerEXT);
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 22:11:56 +00:00
|
|
|
if (!profile.support_vertex_instance_id && (info.loads_instance_id || info.loads_vertex_id)) {
|
|
|
|
ctx.AddExtension("SPV_KHR_shader_draw_parameters");
|
|
|
|
ctx.AddCapability(spv::Capability::DrawParameters);
|
|
|
|
}
|
2021-05-10 21:21:28 +00:00
|
|
|
if ((info.uses_subgroup_vote || info.uses_subgroup_invocation_id ||
|
|
|
|
info.uses_subgroup_shuffles) &&
|
|
|
|
profile.support_vote) {
|
2021-03-24 00:27:17 +00:00
|
|
|
ctx.AddExtension("SPV_KHR_shader_ballot");
|
|
|
|
ctx.AddCapability(spv::Capability::SubgroupBallotKHR);
|
|
|
|
if (!profile.warp_size_potentially_larger_than_guest) {
|
|
|
|
// vote ops are only used when not taking the long path
|
|
|
|
ctx.AddExtension("SPV_KHR_subgroup_vote");
|
|
|
|
ctx.AddCapability(spv::Capability::SubgroupVoteKHR);
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 08:32:21 +00:00
|
|
|
if (info.uses_int64_bit_atomics && profile.support_int64_atomics) {
|
2021-04-11 06:07:02 +00:00
|
|
|
ctx.AddCapability(spv::Capability::Int64Atomics);
|
|
|
|
}
|
2021-04-11 05:37:03 +00:00
|
|
|
if (info.uses_typeless_image_reads && profile.support_typeless_image_loads) {
|
|
|
|
ctx.AddCapability(spv::Capability::StorageImageReadWithoutFormat);
|
|
|
|
}
|
2021-04-12 00:02:44 +00:00
|
|
|
if (info.uses_typeless_image_writes) {
|
|
|
|
ctx.AddCapability(spv::Capability::StorageImageWriteWithoutFormat);
|
|
|
|
}
|
2021-04-23 21:47:54 +00:00
|
|
|
if (info.uses_image_buffers) {
|
|
|
|
ctx.AddCapability(spv::Capability::ImageBuffer);
|
|
|
|
}
|
2021-04-16 20:22:59 +00:00
|
|
|
if (info.uses_sample_id) {
|
|
|
|
ctx.AddCapability(spv::Capability::SampleRateShading);
|
|
|
|
}
|
2021-05-21 05:12:32 +00:00
|
|
|
if (!ctx.runtime_info.xfb_varyings.empty()) {
|
2021-04-14 04:04:59 +00:00
|
|
|
ctx.AddCapability(spv::Capability::TransformFeedback);
|
|
|
|
}
|
2021-04-18 07:07:48 +00:00
|
|
|
if (info.uses_derivatives) {
|
2021-04-17 09:56:45 +00:00
|
|
|
ctx.AddCapability(spv::Capability::DerivativeControl);
|
|
|
|
}
|
2021-03-20 22:11:56 +00:00
|
|
|
// TODO: Track this usage
|
|
|
|
ctx.AddCapability(spv::Capability::ImageGatherExtended);
|
2021-03-26 21:45:38 +00:00
|
|
|
ctx.AddCapability(spv::Capability::ImageQuery);
|
2021-04-06 05:56:15 +00:00
|
|
|
ctx.AddCapability(spv::Capability::SampledBuffer);
|
2021-03-20 22:11:56 +00:00
|
|
|
}
|
2021-04-11 05:08:15 +00:00
|
|
|
|
2021-04-11 05:46:51 +00:00
|
|
|
void PatchPhiNodes(IR::Program& program, EmitContext& ctx) {
|
2021-04-11 05:08:15 +00:00
|
|
|
auto inst{program.blocks.front()->begin()};
|
2021-04-11 05:46:51 +00:00
|
|
|
size_t block_index{0};
|
2021-04-11 05:08:15 +00:00
|
|
|
ctx.PatchDeferredPhi([&](size_t phi_arg) {
|
|
|
|
if (phi_arg == 0) {
|
|
|
|
++inst;
|
|
|
|
if (inst == program.blocks[block_index]->end() ||
|
|
|
|
inst->GetOpcode() != IR::Opcode::Phi) {
|
|
|
|
do {
|
|
|
|
++block_index;
|
|
|
|
inst = program.blocks[block_index]->begin();
|
|
|
|
} while (inst->GetOpcode() != IR::Opcode::Phi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.Def(inst->Arg(phi_arg));
|
|
|
|
});
|
2021-04-11 05:46:51 +00:00
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-05-21 05:12:32 +00:00
|
|
|
std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info,
|
|
|
|
IR::Program& program, Bindings& bindings) {
|
|
|
|
EmitContext ctx{profile, runtime_info, program, bindings};
|
2021-04-11 05:46:51 +00:00
|
|
|
const Id main{DefineMain(ctx, program)};
|
|
|
|
DefineEntryPoint(program, ctx, main);
|
|
|
|
if (profile.support_float_controls) {
|
|
|
|
ctx.AddExtension("SPV_KHR_float_controls");
|
|
|
|
SetupDenormControl(profile, program, ctx, main);
|
|
|
|
SetupSignedNanCapabilities(profile, program, ctx, main);
|
|
|
|
}
|
|
|
|
SetupCapabilities(profile, program.info, ctx);
|
|
|
|
PatchPhiNodes(program, ctx);
|
2021-02-17 03:59:28 +00:00
|
|
|
return ctx.Assemble();
|
2021-02-11 19:39:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
Id EmitPhi(EmitContext& ctx, IR::Inst* inst) {
|
2021-02-11 19:39:06 +00:00
|
|
|
const size_t num_args{inst->NumArgs()};
|
2021-04-11 05:08:15 +00:00
|
|
|
boost::container::small_vector<Id, 32> blocks;
|
|
|
|
blocks.reserve(num_args);
|
2021-02-11 19:39:06 +00:00
|
|
|
for (size_t index = 0; index < num_args; ++index) {
|
2021-04-11 05:08:15 +00:00
|
|
|
blocks.push_back(inst->PhiBlock(index)->Definition<Id>());
|
2021-02-11 19:39:06 +00:00
|
|
|
}
|
2021-03-30 06:19:50 +00:00
|
|
|
// The type of a phi instruction is stored in its flags
|
|
|
|
const Id result_type{TypeId(ctx, inst->Flags<IR::Type>())};
|
2021-04-11 05:08:15 +00:00
|
|
|
return ctx.DeferredOpPhi(result_type, std::span(blocks.data(), blocks.size()));
|
2021-02-08 05:54:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
void EmitVoid(EmitContext&) {}
|
2021-02-08 05:54:35 +00:00
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
Id EmitIdentity(EmitContext& ctx, const IR::Value& value) {
|
2021-04-11 05:08:15 +00:00
|
|
|
const Id id{ctx.Def(value)};
|
|
|
|
if (!Sirit::ValidId(id)) {
|
|
|
|
throw NotImplementedException("Forward identity declaration");
|
2021-04-01 04:07:51 +00:00
|
|
|
}
|
2021-04-11 05:08:15 +00:00
|
|
|
return id;
|
2021-02-08 05:54:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 05:04:22 +00:00
|
|
|
Id EmitConditionRef(EmitContext& ctx, const IR::Value& value) {
|
|
|
|
const Id id{ctx.Def(value)};
|
|
|
|
if (!Sirit::ValidId(id)) {
|
|
|
|
throw NotImplementedException("Forward identity declaration");
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitReference(EmitContext&) {}
|
2021-05-14 07:48:46 +00:00
|
|
|
|
|
|
|
void EmitPhiMove(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
2021-05-14 03:40:54 +00:00
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
void EmitGetZeroFromOp(EmitContext&) {
|
2021-02-08 05:54:35 +00:00
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
void EmitGetSignFromOp(EmitContext&) {
|
2021-02-08 05:54:35 +00:00
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
void EmitGetCarryFromOp(EmitContext&) {
|
2021-02-08 05:54:35 +00:00
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 03:59:28 +00:00
|
|
|
void EmitGetOverflowFromOp(EmitContext&) {
|
2021-02-08 05:54:35 +00:00
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:53 +00:00
|
|
|
void EmitGetSparseFromOp(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:31:37 +00:00
|
|
|
void EmitGetInBoundsFromOp(EmitContext&) {
|
|
|
|
throw LogicError("Unreachable instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-08 05:54:35 +00:00
|
|
|
} // namespace Shader::Backend::SPIRV
|