2021-05-08 21:28:52 +02:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include "shader_recompiler/backend/glasm/emit_context.h"
|
|
|
|
#include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/value.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::GLASM {
|
|
|
|
namespace {
|
2021-05-09 08:11:34 +02:00
|
|
|
void GetCbuf(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset,
|
2021-05-08 21:28:52 +02:00
|
|
|
std::string_view size) {
|
|
|
|
if (!binding.IsImmediate()) {
|
|
|
|
throw NotImplementedException("Indirect constant buffer loading");
|
|
|
|
}
|
2021-05-09 08:11:34 +02:00
|
|
|
const Register ret{ctx.reg_alloc.Define(inst)};
|
|
|
|
ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
|
2021-05-08 21:28:52 +02:00
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "U8");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "S8");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "U16");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "S16");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "U32");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "F32");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
|
2021-05-09 08:11:34 +02:00
|
|
|
ScalarU32 offset) {
|
2021-05-08 21:28:52 +02:00
|
|
|
GetCbuf(ctx, inst, binding, offset, "U32X2");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
|
2021-05-09 08:11:34 +02:00
|
|
|
[[maybe_unused]] ScalarU32 vertex) {
|
2021-05-16 01:33:57 +02:00
|
|
|
const u32 element{static_cast<u32>(attr) % 4};
|
|
|
|
const char swizzle{"xyzw"[element]};
|
2021-05-08 21:28:52 +02:00
|
|
|
if (IR::IsGeneric(attr)) {
|
|
|
|
const u32 index{IR::GenericAttributeIndex(attr)};
|
2021-05-16 01:33:57 +02:00
|
|
|
ctx.Add("MOV.F {}.x,in_attr{}[0].{};", inst, index, swizzle);
|
2021-05-08 21:28:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-16 01:33:57 +02:00
|
|
|
switch (attr) {
|
|
|
|
case IR::Attribute::PositionX:
|
|
|
|
case IR::Attribute::PositionY:
|
|
|
|
case IR::Attribute::PositionZ:
|
|
|
|
case IR::Attribute::PositionW:
|
|
|
|
ctx.Add("MOV.F {}.x,{}.position.{};", inst, ctx.stage_name, swizzle);
|
|
|
|
break;
|
2021-05-18 00:25:19 +02:00
|
|
|
case IR::Attribute::InstanceId:
|
|
|
|
ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.stage_name);
|
|
|
|
break;
|
|
|
|
case IR::Attribute::VertexId:
|
|
|
|
ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.stage_name);
|
|
|
|
break;
|
2021-05-16 01:33:57 +02:00
|
|
|
default:
|
|
|
|
throw NotImplementedException("Get attribute {}", attr);
|
|
|
|
}
|
2021-05-08 21:28:52 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
|
|
|
|
[[maybe_unused]] ScalarU32 vertex) {
|
2021-05-08 21:28:52 +02:00
|
|
|
const u32 element{static_cast<u32>(attr) % 4};
|
|
|
|
const char swizzle{"xyzw"[element]};
|
|
|
|
if (IR::IsGeneric(attr)) {
|
|
|
|
const u32 index{IR::GenericAttributeIndex(attr)};
|
2021-05-15 23:15:50 +02:00
|
|
|
ctx.Add("MOV.F out_attr{}[0].{},{};", index, swizzle, value);
|
2021-05-08 21:28:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (attr) {
|
|
|
|
case IR::Attribute::PositionX:
|
|
|
|
case IR::Attribute::PositionY:
|
|
|
|
case IR::Attribute::PositionZ:
|
|
|
|
case IR::Attribute::PositionW:
|
|
|
|
ctx.Add("MOV.F result.position.{},{};", swizzle, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw NotImplementedException("Set attribute {}", attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitGetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
|
|
|
|
[[maybe_unused]] ScalarU32 vertex) {
|
2021-05-08 21:28:52 +02:00
|
|
|
throw NotImplementedException("GLASM instruction");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
|
|
|
|
[[maybe_unused]] ScalarF32 value, [[maybe_unused]] ScalarU32 vertex) {
|
2021-05-08 21:28:52 +02:00
|
|
|
throw NotImplementedException("GLASM instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitGetPatch([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Patch patch) {
|
|
|
|
throw NotImplementedException("GLASM instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitSetPatch([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Patch patch,
|
2021-05-09 08:11:34 +02:00
|
|
|
[[maybe_unused]] ScalarF32 value) {
|
2021-05-08 21:28:52 +02:00
|
|
|
throw NotImplementedException("GLASM instruction");
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:15:50 +02:00
|
|
|
void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, ScalarF32 value) {
|
|
|
|
ctx.Add("MOV.F frag_color{}.{},{};", index, "xyzw"[component], value);
|
2021-05-08 21:28:52 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitSetSampleMask([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarF32 value) {
|
2021-05-08 21:28:52 +02:00
|
|
|
throw NotImplementedException("GLASM instruction");
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:11:34 +02:00
|
|
|
void EmitSetFragDepth([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarF32 value) {
|
2021-05-08 21:28:52 +02:00
|
|
|
throw NotImplementedException("GLASM instruction");
|
|
|
|
}
|
|
|
|
|
2021-05-15 03:18:53 +02:00
|
|
|
void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) {
|
|
|
|
ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value) {
|
|
|
|
ctx.Add("MOV.U lmem[{}].x,{};", word_offset, value);
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:28:52 +02:00
|
|
|
} // namespace Shader::Backend::GLASM
|