2021-05-20 01:58:32 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-05-24 23:33:11 +00:00
|
|
|
#include <ranges>
|
2021-05-20 01:58:32 +00:00
|
|
|
#include <string>
|
|
|
|
|
2021-06-11 04:33:33 +00:00
|
|
|
#include "common/alignment.h"
|
2021-05-20 01:58:32 +00:00
|
|
|
#include "shader_recompiler/backend/glsl/emit_context.h"
|
|
|
|
#include "shader_recompiler/backend/glsl/emit_glsl.h"
|
|
|
|
#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
|
2021-05-24 23:33:11 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
2021-05-20 01:58:32 +00:00
|
|
|
|
|
|
|
namespace Shader::Backend::GLSL {
|
|
|
|
namespace {
|
|
|
|
template <class Func>
|
|
|
|
struct FuncTraits {};
|
|
|
|
|
|
|
|
template <class ReturnType_, class... Args>
|
|
|
|
struct FuncTraits<ReturnType_ (*)(Args...)> {
|
|
|
|
using ReturnType = ReturnType_;
|
|
|
|
|
|
|
|
static constexpr size_t NUM_ARGS = sizeof...(Args);
|
|
|
|
|
|
|
|
template <size_t I>
|
|
|
|
using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <auto func, typename... Args>
|
|
|
|
void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) {
|
|
|
|
inst->SetDefinition<Id>(func(ctx, std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ArgType>
|
2021-05-21 23:55:58 +00:00
|
|
|
auto Arg(EmitContext& ctx, const IR::Value& arg) {
|
|
|
|
if constexpr (std::is_same_v<ArgType, std::string_view>) {
|
2021-05-30 21:27:00 +00:00
|
|
|
return ctx.var_alloc.Consume(arg);
|
2021-05-20 01:58:32 +00:00
|
|
|
} 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();
|
|
|
|
} else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
|
|
|
|
return arg.Attribute();
|
|
|
|
} else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
|
|
|
|
return arg.Patch();
|
|
|
|
} else if constexpr (std::is_same_v<ArgType, IR::Reg>) {
|
|
|
|
return arg.Reg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)>;
|
|
|
|
if constexpr (std::is_same_v<typename Traits::ReturnType, Id>) {
|
|
|
|
if constexpr (is_first_arg_inst) {
|
|
|
|
SetDefinition<func>(
|
2021-05-21 23:28:03 +00:00
|
|
|
ctx, inst, *inst,
|
2021-05-20 01:58:32 +00:00
|
|
|
Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
|
|
|
|
} else {
|
|
|
|
SetDefinition<func>(
|
|
|
|
ctx, inst, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if constexpr (is_first_arg_inst) {
|
2021-05-21 23:28:03 +00:00
|
|
|
func(ctx, *inst, Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
|
2021-05-20 01:58:32 +00:00
|
|
|
} else {
|
|
|
|
func(ctx, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <auto func>
|
|
|
|
void Invoke(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
using Traits = FuncTraits<decltype(func)>;
|
|
|
|
static_assert(Traits::NUM_ARGS >= 1, "Insufficient arguments");
|
|
|
|
if constexpr (Traits::NUM_ARGS == 1) {
|
|
|
|
Invoke<func, false>(ctx, inst, std::make_index_sequence<0>{});
|
|
|
|
} else {
|
|
|
|
using FirstArgType = typename Traits::template ArgType<1>;
|
2021-05-21 23:28:03 +00:00
|
|
|
static constexpr bool is_first_arg_inst = std::is_same_v<FirstArgType, IR::Inst&>;
|
2021-05-20 01:58:32 +00:00
|
|
|
using Indices = std::make_index_sequence<Traits::NUM_ARGS - (is_first_arg_inst ? 2 : 1)>;
|
|
|
|
Invoke<func, is_first_arg_inst>(ctx, inst, Indices{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitInst(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
switch (inst->GetOpcode()) {
|
|
|
|
#define OPCODE(name, result_type, ...) \
|
|
|
|
case IR::Opcode::name: \
|
|
|
|
return Invoke<&Emit##name>(ctx, inst);
|
|
|
|
#include "shader_recompiler/frontend/ir/opcodes.inc"
|
|
|
|
#undef OPCODE
|
|
|
|
}
|
|
|
|
throw LogicError("Invalid opcode {}", inst->GetOpcode());
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:55:06 +00:00
|
|
|
bool IsReference(IR::Inst& inst) {
|
|
|
|
return inst.GetOpcode() == IR::Opcode::Reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrecolorInst(IR::Inst& phi) {
|
|
|
|
// Insert phi moves before references to avoid overwritting other phis
|
|
|
|
const size_t num_args{phi.NumArgs()};
|
|
|
|
for (size_t i = 0; i < num_args; ++i) {
|
|
|
|
IR::Block& phi_block{*phi.PhiBlock(i)};
|
|
|
|
auto it{std::find_if_not(phi_block.rbegin(), phi_block.rend(), IsReference).base()};
|
|
|
|
IR::IREmitter ir{phi_block, it};
|
|
|
|
const IR::Value arg{phi.Arg(i)};
|
|
|
|
if (arg.IsImmediate()) {
|
|
|
|
ir.PhiMove(phi, arg);
|
|
|
|
} else {
|
2021-05-28 17:54:09 +00:00
|
|
|
ir.PhiMove(phi, IR::Value{arg.InstRecursive()});
|
2021-05-26 00:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-30 23:21:30 +00:00
|
|
|
for (size_t i = 0; i < num_args; ++i) {
|
|
|
|
IR::IREmitter{*phi.PhiBlock(i)}.Reference(IR::Value{&phi});
|
|
|
|
}
|
2021-05-26 00:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Precolor(const IR::Program& program) {
|
2021-05-24 23:33:11 +00:00
|
|
|
for (IR::Block* const block : program.blocks) {
|
|
|
|
for (IR::Inst& phi : block->Instructions() | std::views::take_while(IR::IsPhi)) {
|
2021-05-26 00:55:06 +00:00
|
|
|
PrecolorInst(phi);
|
2021-05-24 23:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 01:58:32 +00:00
|
|
|
void EmitCode(EmitContext& ctx, const IR::Program& program) {
|
|
|
|
for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
|
|
|
|
switch (node.type) {
|
|
|
|
case IR::AbstractSyntaxNode::Type::Block:
|
|
|
|
for (IR::Inst& inst : node.data.block->Instructions()) {
|
|
|
|
EmitInst(ctx, &inst);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::AbstractSyntaxNode::Type::If:
|
2021-06-04 00:57:52 +00:00
|
|
|
ctx.Add("if({}){{", ctx.var_alloc.Consume(node.data.if_node.cond));
|
2021-05-20 01:58:32 +00:00
|
|
|
break;
|
|
|
|
case IR::AbstractSyntaxNode::Type::EndIf:
|
2021-05-24 23:33:11 +00:00
|
|
|
ctx.Add("}}");
|
2021-05-20 01:58:32 +00:00
|
|
|
break;
|
|
|
|
case IR::AbstractSyntaxNode::Type::Break:
|
|
|
|
if (node.data.break_node.cond.IsImmediate()) {
|
|
|
|
if (node.data.break_node.cond.U1()) {
|
|
|
|
ctx.Add("break;");
|
|
|
|
}
|
2021-05-24 23:33:11 +00:00
|
|
|
} else {
|
2021-05-30 21:27:00 +00:00
|
|
|
ctx.Add("if({}){{break;}}", ctx.var_alloc.Consume(node.data.break_node.cond));
|
2021-05-20 01:58:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR::AbstractSyntaxNode::Type::Return:
|
|
|
|
case IR::AbstractSyntaxNode::Type::Unreachable:
|
2021-05-27 01:18:17 +00:00
|
|
|
ctx.Add("return;");
|
2021-05-20 01:58:32 +00:00
|
|
|
break;
|
2021-05-24 23:33:11 +00:00
|
|
|
case IR::AbstractSyntaxNode::Type::Loop:
|
2021-05-28 17:54:09 +00:00
|
|
|
ctx.Add("for(;;){{");
|
2021-05-27 04:26:16 +00:00
|
|
|
break;
|
2021-05-24 23:33:11 +00:00
|
|
|
case IR::AbstractSyntaxNode::Type::Repeat:
|
2021-06-04 00:57:52 +00:00
|
|
|
ctx.Add("if({}){{continue;}}else{{break;}}}}",
|
|
|
|
ctx.var_alloc.Consume(node.data.repeat.cond));
|
2021-05-27 04:26:16 +00:00
|
|
|
break;
|
2021-05-20 01:58:32 +00:00
|
|
|
default:
|
2021-06-11 04:33:33 +00:00
|
|
|
throw NotImplementedException("AbstractSyntaxNode Type {}", node.type);
|
2021-05-20 01:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:37:56 +00:00
|
|
|
std::string GlslVersionSpecifier(const EmitContext& ctx) {
|
|
|
|
if (ctx.uses_y_direction) {
|
|
|
|
return " compatibility";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2021-05-30 21:27:00 +00:00
|
|
|
|
2021-06-07 22:04:01 +00:00
|
|
|
bool IsPreciseType(GlslVarType type) {
|
|
|
|
switch (type) {
|
|
|
|
case GlslVarType::PrecF32:
|
|
|
|
case GlslVarType::PrecF64:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:27:00 +00:00
|
|
|
void DefineVariables(const EmitContext& ctx, std::string& header) {
|
|
|
|
for (u32 i = 0; i < static_cast<u32>(GlslVarType::Void); ++i) {
|
|
|
|
const auto type{static_cast<GlslVarType>(i)};
|
|
|
|
const auto& tracker{ctx.var_alloc.GetUseTracker(type)};
|
|
|
|
const auto type_name{ctx.var_alloc.GetGlslType(type)};
|
2021-06-07 22:04:01 +00:00
|
|
|
const auto precise{IsPreciseType(type) ? "precise " : ""};
|
2021-05-30 21:27:00 +00:00
|
|
|
// Temps/return types that are never used are stored at index 0
|
|
|
|
if (tracker.uses_temp) {
|
2021-06-07 22:04:01 +00:00
|
|
|
header += fmt::format("{}{} t{}={}(0);", precise, type_name,
|
2021-06-01 04:07:14 +00:00
|
|
|
ctx.var_alloc.Representation(0, type), type_name);
|
2021-05-30 21:27:00 +00:00
|
|
|
}
|
2021-06-07 22:04:01 +00:00
|
|
|
for (u32 index = 0; index < tracker.num_used; ++index) {
|
2021-06-01 04:07:14 +00:00
|
|
|
header += fmt::format("{}{} {}={}(0);", precise, type_name,
|
2021-05-30 21:27:00 +00:00
|
|
|
ctx.var_alloc.Representation(index, type), type_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 01:58:32 +00:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-05-28 00:37:56 +00:00
|
|
|
std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR::Program& program,
|
2021-05-21 23:55:58 +00:00
|
|
|
Bindings& bindings) {
|
2021-05-28 00:37:56 +00:00
|
|
|
EmitContext ctx{program, bindings, profile, runtime_info};
|
2021-05-26 00:55:06 +00:00
|
|
|
Precolor(program);
|
2021-05-20 01:58:32 +00:00
|
|
|
EmitCode(ctx, program);
|
2021-06-04 00:57:52 +00:00
|
|
|
const std::string version{fmt::format("#version 450{}\n", GlslVersionSpecifier(ctx))};
|
2021-05-28 17:54:09 +00:00
|
|
|
ctx.header.insert(0, version);
|
2021-05-30 03:31:58 +00:00
|
|
|
if (program.local_memory_size > 0) {
|
2021-06-11 04:33:33 +00:00
|
|
|
ctx.header += fmt::format("uint lmem[{}];", Common::AlignUp(program.local_memory_size, 4));
|
2021-05-30 03:31:58 +00:00
|
|
|
}
|
|
|
|
if (program.shared_memory_size > 0) {
|
2021-06-11 04:33:33 +00:00
|
|
|
ctx.header +=
|
|
|
|
fmt::format("shared uint smem[{}];", Common::AlignUp(program.shared_memory_size, 4));
|
2021-05-30 03:31:58 +00:00
|
|
|
}
|
2021-06-01 04:07:14 +00:00
|
|
|
ctx.header += "\nvoid main(){\n";
|
2021-05-30 03:31:58 +00:00
|
|
|
if (program.stage == Stage::VertexA || program.stage == Stage::VertexB) {
|
|
|
|
ctx.header += "gl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);";
|
2021-05-31 02:42:52 +00:00
|
|
|
// TODO: Properly resolve attribute issues
|
|
|
|
for (size_t index = 0; index < program.info.stores_generics.size() / 2; ++index) {
|
|
|
|
if (!program.info.stores_generics[index]) {
|
|
|
|
ctx.header += fmt::format("out_attr{}=vec4(0,0,0,1);", index);
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 03:31:58 +00:00
|
|
|
}
|
2021-05-30 21:27:00 +00:00
|
|
|
DefineVariables(ctx, ctx.header);
|
2021-05-30 00:19:24 +00:00
|
|
|
if (ctx.uses_cc_carry) {
|
|
|
|
ctx.header += "uint carry;";
|
|
|
|
}
|
2021-05-29 01:24:52 +00:00
|
|
|
if (program.info.uses_subgroup_shuffles) {
|
2021-05-30 00:19:24 +00:00
|
|
|
ctx.header += "bool shfl_in_bounds;";
|
2021-05-29 01:24:52 +00:00
|
|
|
}
|
2021-05-28 17:54:09 +00:00
|
|
|
ctx.code.insert(0, ctx.header);
|
2021-06-04 00:57:52 +00:00
|
|
|
ctx.code += '}';
|
2021-05-20 01:58:32 +00:00
|
|
|
return ctx.code;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Backend::GLSL
|