From 43afb198556f762440c09c56ff9853364e272b9c Mon Sep 17 00:00:00 2001 From: DH Date: Wed, 23 Oct 2024 02:48:27 +0300 Subject: [PATCH] gpu: implement mrtz exp target --- .../include/shader/SpvConverter.hpp | 3 ++ rpcsx/gpu/lib/gcn-shader/src/GcnConverter.cpp | 33 ++++++++++++++++--- rpcsx/gpu/lib/gcn-shader/src/SpvConverter.cpp | 31 +++++++++++++++-- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/rpcsx/gpu/lib/gcn-shader/include/shader/SpvConverter.hpp b/rpcsx/gpu/lib/gcn-shader/include/shader/SpvConverter.hpp index 05ef9070c..0010f2344 100644 --- a/rpcsx/gpu/lib/gcn-shader/include/shader/SpvConverter.hpp +++ b/rpcsx/gpu/lib/gcn-shader/include/shader/SpvConverter.hpp @@ -17,6 +17,7 @@ struct Context : ir::Context { ir::Value perVertex; std::map outputs; std::map inputs; + ir::Value fragDepth; ir::RegionLike localVariables; ir::RegionLike epilogue; @@ -151,5 +152,7 @@ struct Context : ir::Context { ir::Value createOutput(ir::Location loc, int index); ir::Value createInput(ir::Location loc, int index); ir::Value createAttr(ir::Location loc, int attrId, bool perVertex, bool flat); + + ir::Value createFragDepth(ir::Location loc); }; } // namespace shader::spv diff --git a/rpcsx/gpu/lib/gcn-shader/src/GcnConverter.cpp b/rpcsx/gpu/lib/gcn-shader/src/GcnConverter.cpp index 231db5fc1..288ac2b0e 100644 --- a/rpcsx/gpu/lib/gcn-shader/src/GcnConverter.cpp +++ b/rpcsx/gpu/lib/gcn-shader/src/GcnConverter.cpp @@ -9,6 +9,7 @@ #include "rx/die.hpp" #include #include +#include using namespace shader; @@ -651,9 +652,9 @@ static void expToSpv(GcnConverter &converter, gcn::Stage stage, continue; } - auto src = - builder.createSpvBitcast(loc, context.getTypeFloat32(), - inst.getOperand(operandIndex++).getAsValue()); + auto src = builder.createSpvBitcast( + loc, context.getTypeFloat32(), + inst.getOperand(operandIndex++).getAsValue()); auto srcType = src.getOperand(0).getAsValue(); ir::Value elementType; @@ -786,6 +787,26 @@ static void expToSpv(GcnConverter &converter, gcn::Stage stage, return; } + if (target == ET_MRTZ) { + auto output = context.createFragDepth(loc); + auto channelType = context.getTypeFloat32(); + + for (int channel = 0; channel < 4; ++channel) { + if (~swizzle & (1 << channel)) { + continue; + } + + auto channelValue = + builder.createSpvCompositeExtract(loc, elemType, value, {{channel}}); + channelValue = + context.createCast(loc, builder, channelType, channelValue); + builder.createSpvStore(loc, output, channelValue); + break; + } + + return; + } + if (target >= ET_PARAM0 && target <= ET_PARAM31) { auto output = context.createOutput(loc, target - ET_PARAM0); auto floatT = context.getTypeFloat32(); @@ -849,8 +870,8 @@ static void expToSpv(GcnConverter &converter, gcn::Stage stage, return result; }; - std::printf("exp target %s.%s\n", targetToString(target).c_str(), - swizzleToString(swizzle).c_str()); + std::println(stderr, "exp target {}.{}", targetToString(target), + swizzleToString(swizzle)); std::abort(); } @@ -1454,6 +1475,8 @@ static void createEntryPoint(gcn::Context &context, const gcn::Environment &env, executionModes.createSpvExecutionMode( mainFn.getLocation(), mainFn, ir::spv::ExecutionMode::OriginUpperLeft()); + executionModes.createSpvExecutionMode( + mainFn.getLocation(), mainFn, ir::spv::ExecutionMode::DepthReplacing()); } if (executionModel == ir::spv::ExecutionModel::GLCompute) { diff --git a/rpcsx/gpu/lib/gcn-shader/src/SpvConverter.cpp b/rpcsx/gpu/lib/gcn-shader/src/SpvConverter.cpp index 4fa3d53db..f812a2741 100644 --- a/rpcsx/gpu/lib/gcn-shader/src/SpvConverter.cpp +++ b/rpcsx/gpu/lib/gcn-shader/src/SpvConverter.cpp @@ -571,8 +571,8 @@ ir::Value spv::Context::createOutput(ir::Location loc, int index) { auto annotations = Builder::createAppend(*this, layout.getOrCreateAnnotations(*this)); - auto variable = globals.createSpvVariable(loc, variableType, - ir::spv::StorageClass::Output, nullValue); + auto variable = globals.createSpvVariable( + loc, variableType, ir::spv::StorageClass::Output, nullValue); annotations.createSpvDecorate(loc, variable, ir::spv::Decoration::Location(index)); @@ -647,3 +647,30 @@ ir::Value spv::Context::createAttr(ir::Location loc, int attrId, bool perVertex, return result; } + +ir::Value spv::Context::createFragDepth(ir::Location loc) { + if (fragDepth == nullptr) { + auto floatType = getTypeFloat32(); + auto nullValue = getNull(floatType); + + auto variableType = + getTypePointer(ir::spv::StorageClass::Output, floatType); + + auto globals = + Builder::createAppend(*this, layout.getOrCreateGlobals(*this)); + auto annotations = + Builder::createAppend(*this, layout.getOrCreateAnnotations(*this)); + + auto variable = globals.createSpvVariable( + loc, variableType, ir::spv::StorageClass::Output, nullValue); + + annotations.createSpvDecorate( + loc, variable, + ir::spv::Decoration::BuiltIn(ir::spv::BuiltIn::FragDepth)); + + setName(variable, "fragDepth"); + fragDepth = variable; + } + + return fragDepth; +}