Skip to content

Commit

Permalink
gpu: implement mrtz exp target
Browse files Browse the repository at this point in the history
  • Loading branch information
DHrpcs3 committed Oct 22, 2024
1 parent f79e16e commit 43afb19
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
3 changes: 3 additions & 0 deletions rpcsx/gpu/lib/gcn-shader/include/shader/SpvConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Context : ir::Context {
ir::Value perVertex;
std::map<int, ir::Value> outputs;
std::map<int, ir::Value> inputs;
ir::Value fragDepth;

ir::RegionLike localVariables;
ir::RegionLike epilogue;
Expand Down Expand Up @@ -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
33 changes: 28 additions & 5 deletions rpcsx/gpu/lib/gcn-shader/src/GcnConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "rx/die.hpp"
#include <iostream>
#include <limits>
#include <print>

using namespace shader;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 29 additions & 2 deletions rpcsx/gpu/lib/gcn-shader/src/SpvConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

0 comments on commit 43afb19

Please sign in to comment.