Put Unleashed Recompiled specific implementations behind a macro.

This commit is contained in:
Skyth 2025-02-17 20:08:52 +03:00
parent 855a5a8c51
commit 08842eae92
3 changed files with 65 additions and 7 deletions

View file

@ -2,10 +2,13 @@
#define SHADER_COMMON_H_INCLUDED #define SHADER_COMMON_H_INCLUDED
#define SPEC_CONSTANT_R11G11B10_NORMAL (1 << 0) #define SPEC_CONSTANT_R11G11B10_NORMAL (1 << 0)
#define SPEC_CONSTANT_BICUBIC_GI_FILTER (1 << 1) #define SPEC_CONSTANT_ALPHA_TEST (1 << 1)
#define SPEC_CONSTANT_ALPHA_TEST (1 << 2)
#define SPEC_CONSTANT_ALPHA_TO_COVERAGE (1 << 3) #ifdef UNLEASHED_RECOMP
#define SPEC_CONSTANT_REVERSE_Z (1 << 4) #define SPEC_CONSTANT_BICUBIC_GI_FILTER (1 << 2)
#define SPEC_CONSTANT_ALPHA_TO_COVERAGE (1 << 3)
#define SPEC_CONSTANT_REVERSE_Z (1 << 4)
#endif
#if !defined(__cplusplus) || defined(__INTELLISENSE__) #if !defined(__cplusplus) || defined(__INTELLISENSE__)

View file

@ -74,6 +74,7 @@ struct DeclUsageLocation
uint32_t location; uint32_t location;
}; };
// NOTE: These are specialized Vulkan locations for Unleashed Recompiled. Change as necessary. Likely not going to work with other games.
static constexpr DeclUsageLocation USAGE_LOCATIONS[] = static constexpr DeclUsageLocation USAGE_LOCATIONS[] =
{ {
{ DeclUsage::Position, 0, 0 }, { DeclUsage::Position, 0, 0 },
@ -247,13 +248,18 @@ void ShaderRecompiler::recompile(const TextureFetchInstruction& instr, bool bicu
std::string constName; std::string constName;
const char* constNamePtr = nullptr; const char* constNamePtr = nullptr;
#ifdef UNLEASHED_RECOMP
bool subtractFromOne = false; bool subtractFromOne = false;
#endif
auto findResult = samplers.find(instr.constIndex); auto findResult = samplers.find(instr.constIndex);
if (findResult != samplers.end()) if (findResult != samplers.end())
{ {
constNamePtr = findResult->second; constNamePtr = findResult->second;
#ifdef UNLEASHED_RECOMP
subtractFromOne = hasMtxPrevInvViewProjection && strcmp(constNamePtr, "sampZBuffer") == 0; subtractFromOne = hasMtxPrevInvViewProjection && strcmp(constNamePtr, "sampZBuffer") == 0;
#endif
} }
else else
{ {
@ -261,6 +267,7 @@ void ShaderRecompiler::recompile(const TextureFetchInstruction& instr, bool bicu
constNamePtr = constName.c_str(); constNamePtr = constName.c_str();
} }
#ifdef UNLEASHED_RECOMP
if (instr.constIndex == 0 && instr.dimension == TextureDimension::Texture2D) if (instr.constIndex == 0 && instr.dimension == TextureDimension::Texture2D)
{ {
indent(); indent();
@ -268,6 +275,7 @@ void ShaderRecompiler::recompile(const TextureFetchInstruction& instr, bool bicu
printSrcRegister(2); printSrcRegister(2);
out += ");\n"; out += ");\n";
} }
#endif
indent(); indent();
print("r{}.", instr.dstRegister); print("r{}.", instr.dstRegister);
@ -278,8 +286,11 @@ void ShaderRecompiler::recompile(const TextureFetchInstruction& instr, bool bicu
{ {
case FetchOpcode::TextureFetch: case FetchOpcode::TextureFetch:
{ {
#ifdef UNLEASHED_RECOMP
if (subtractFromOne) if (subtractFromOne)
out += "1.0 - "; out += "1.0 - ";
#endif
out += "tfetch"; out += "tfetch";
break; break;
} }
@ -314,8 +325,11 @@ void ShaderRecompiler::recompile(const TextureFetchInstruction& instr, bool bicu
} }
out += dimension; out += dimension;
#ifdef UNLEASHED_RECOMP
if (bicubic) if (bicubic)
out += "Bicubic"; out += "Bicubic";
#endif
print("({0}_Texture{1}DescriptorIndex, {0}_SamplerDescriptorIndex, ", constNamePtr, dimension); print("({0}_Texture{1}DescriptorIndex, {0}_SamplerDescriptorIndex, ", constNamePtr, dimension);
printSrcRegister(componentCount); printSrcRegister(componentCount);
@ -447,12 +461,14 @@ void ShaderRecompiler::recompile(const AluInstruction& instr)
const char* constantName = reinterpret_cast<const char*>(constantTableData + findResult->second->name); const char* constantName = reinterpret_cast<const char*>(constantTableData + findResult->second->name);
if (findResult->second->registerCount > 1) if (findResult->second->registerCount > 1)
{ {
#ifdef UNLEASHED_RECOMP
if (hasMtxProjection && strcmp(constantName, "g_MtxProjection") == 0) if (hasMtxProjection && strcmp(constantName, "g_MtxProjection") == 0)
{ {
regFormatted = fmt::format("(iterationIndex == 0 ? mtxProjectionReverseZ[{0}] : mtxProjection[{0}])", regFormatted = fmt::format("(iterationIndex == 0 ? mtxProjectionReverseZ[{0}] : mtxProjection[{0}])",
reg - findResult->second->registerIndex); reg - findResult->second->registerIndex);
} }
else else
#endif
{ {
regFormatted = fmt::format("{}({}{})", constantName, regFormatted = fmt::format("{}({}{})", constantName,
reg - findResult->second->registerIndex, instr.const0Relative ? (instr.constAddressRegisterRelative ? " + a0" : " + aL") : ""); reg - findResult->second->registerIndex, instr.const0Relative ? (instr.constAddressRegisterRelative ? " + a0" : " + aL") : "");
@ -592,6 +608,7 @@ void ShaderRecompiler::recompile(const AluInstruction& instr)
case ExportRegister::VSPosition: case ExportRegister::VSPosition:
exportRegister = "oPos"; exportRegister = "oPos";
#ifdef UNLEASHED_RECOMP
if (hasMtxProjection) if (hasMtxProjection)
{ {
indent(); indent();
@ -602,6 +619,7 @@ void ShaderRecompiler::recompile(const AluInstruction& instr)
closeIfBracket = true; closeIfBracket = true;
} }
#endif
break; break;
@ -1097,8 +1115,10 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
out += "#ifdef __spirv__\n\n"; out += "#ifdef __spirv__\n\n";
#ifdef UNLEASHED_RECOMP
bool isMetaInstancer = false; bool isMetaInstancer = false;
bool hasIndexCount = false; bool hasIndexCount = false;
#endif
for (uint32_t i = 0; i < constantTableContainer->constantTable.constants; i++) for (uint32_t i = 0; i < constantTableContainer->constantTable.constants; i++)
{ {
@ -1107,6 +1127,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
const char* constantName = reinterpret_cast<const char*>(constantTableData + constantInfo->name); const char* constantName = reinterpret_cast<const char*>(constantTableData + constantInfo->name);
#ifdef UNLEASHED_RECOMP
if (!isPixelShader) if (!isPixelShader)
{ {
if (strcmp(constantName, "g_MtxProjection") == 0) if (strcmp(constantName, "g_MtxProjection") == 0)
@ -1121,6 +1142,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
if (strcmp(constantName, "g_MtxPrevInvViewProjection") == 0) if (strcmp(constantName, "g_MtxPrevInvViewProjection") == 0)
hasMtxPrevInvViewProjection = true; hasMtxPrevInvViewProjection = true;
} }
#endif
switch (constantInfo->registerSet) switch (constantInfo->registerSet)
{ {
@ -1292,11 +1314,13 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
const char* usageType = USAGE_TYPES[uint32_t(vertexElement.usage)]; const char* usageType = USAGE_TYPES[uint32_t(vertexElement.usage)];
#ifdef UNLEASHED_RECOMP
if ((vertexElement.usage == DeclUsage::TexCoord && vertexElement.usageIndex == 2 && isMetaInstancer) || if ((vertexElement.usage == DeclUsage::TexCoord && vertexElement.usageIndex == 2 && isMetaInstancer) ||
(vertexElement.usage == DeclUsage::Position && vertexElement.usageIndex == 1)) (vertexElement.usage == DeclUsage::Position && vertexElement.usageIndex == 1))
{ {
usageType = "uint4"; usageType = "uint4";
} }
#endif
out += '\t'; out += '\t';
@ -1315,11 +1339,14 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
vertexElements.emplace(uint32_t(vertexElement.address), vertexElement); vertexElements.emplace(uint32_t(vertexElement.address), vertexElement);
} }
#ifdef UNLEASHED_RECOMP
if (hasIndexCount) if (hasIndexCount)
{ {
out += "\tin uint iVertexId : SV_VertexID,\n"; out += "\tin uint iVertexId : SV_VertexID,\n";
out += "\tin uint iInstanceId : SV_InstanceID,\n"; out += "\tin uint iInstanceId : SV_InstanceID,\n";
} }
#endif
out += "\tout float4 oPos : SV_Position"; out += "\tout float4 oPos : SV_Position";
for (auto& [usage, usageIndex] : INTERPOLATORS) for (auto& [usage, usageIndex] : INTERPOLATORS)
@ -1329,7 +1356,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
out += ")\n"; out += ")\n";
out += "{\n"; out += "{\n";
#ifdef UNLEASHED_RECOMP
if (hasMtxProjection) if (hasMtxProjection)
{ {
specConstantsMask |= SPEC_CONSTANT_REVERSE_Z; specConstantsMask |= SPEC_CONSTANT_REVERSE_Z;
@ -1342,6 +1369,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
out += "\t[unroll] for (int iterationIndex = 0; iterationIndex < 2; iterationIndex++)\n"; out += "\t[unroll] for (int iterationIndex = 0; iterationIndex < 2; iterationIndex++)\n";
out += "\t{\n"; out += "\t{\n";
} }
#endif
if (shaderContainer->definitionTableOffset != NULL) if (shaderContainer->definitionTableOffset != NULL)
{ {
@ -1418,8 +1446,10 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
if (!isPixelShader) if (!isPixelShader)
{ {
#ifdef UNLEASHED_RECOMP
if (!hasMtxProjection) if (!hasMtxProjection)
out += "\toPos = 0.0;\n"; out += "\toPos = 0.0;\n";
#endif
for (auto& [usage, usageIndex] : INTERPOLATORS) for (auto& [usage, usageIndex] : INTERPOLATORS)
println("\to{}{} = 0.0;", USAGE_VARIABLES[uint32_t(usage)], usageIndex); println("\to{}{} = 0.0;", USAGE_VARIABLES[uint32_t(usage)], usageIndex);
@ -1436,10 +1466,12 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
{ {
out += "float4((iPos.xy - 0.5) * float2(iFace ? 1.0 : -1.0, 1.0), 0.0, 0.0);\n"; out += "float4((iPos.xy - 0.5) * float2(iFace ? 1.0 : -1.0, 1.0), 0.0, 0.0);\n";
} }
#ifdef UNLEASHED_RECOMP
else if (!isPixelShader && hasIndexCount && i == 0) else if (!isPixelShader && hasIndexCount && i == 0)
{ {
out += "float4(iVertexId + g_IndexCount.x * iInstanceId, 0.0, 0.0, 0.0);\n"; out += "float4(iVertexId + g_IndexCount.x * iInstanceId, 0.0, 0.0, 0.0);\n";
} }
#endif
else else
{ {
out += "0.0;\n"; out += "0.0;\n";
@ -1453,7 +1485,9 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
out += "\tfloat ps = 0.0;\n"; out += "\tfloat ps = 0.0;\n";
if (isPixelShader) if (isPixelShader)
{ {
#ifdef UNLEASHED_RECOMP
out += "\tfloat2 pixelCoord = 0.0;\n"; out += "\tfloat2 pixelCoord = 0.0;\n";
#endif
out += "\tCubeMapData cubeMapData = (CubeMapData)0;\n"; out += "\tCubeMapData cubeMapData = (CubeMapData)0;\n";
} }
@ -1611,7 +1645,10 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
if (simpleControlFlow) if (simpleControlFlow)
{ {
indent(); indent();
println("[unroll] for (aL = 0; aL < i{}.x; aL++)", uint32_t(cfInstr.loopStart.loopId)); #ifdef UNLEASHED_RECOMP
print("[unroll] ")
#endif
println("for (aL = 0; aL < i{}.x; aL++)", uint32_t(cfInstr.loopStart.loopId));
indent(); indent();
out += "{\n"; out += "{\n";
++indentation; ++indentation;
@ -1711,6 +1748,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
} }
else else
{ {
#ifdef UNLEASHED_RECOMP
if (textureFetch.constIndex == 10) // g_GISampler if (textureFetch.constIndex == 10) // g_GISampler
{ {
specConstantsMask |= SPEC_CONSTANT_BICUBIC_GI_FILTER; specConstantsMask |= SPEC_CONSTANT_BICUBIC_GI_FILTER;
@ -1739,6 +1777,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
out += '}'; out += '}';
} }
else else
#endif
{ {
recompile(textureFetch, false); recompile(textureFetch, false);
} }
@ -1757,7 +1796,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
{ {
if (isPixelShader) if (isPixelShader)
{ {
specConstantsMask |= (SPEC_CONSTANT_ALPHA_TEST | SPEC_CONSTANT_ALPHA_TO_COVERAGE); specConstantsMask |= SPEC_CONSTANT_ALPHA_TEST;
indent(); indent();
out += "[branch] if (g_SpecConstants() & SPEC_CONSTANT_ALPHA_TEST)"; out += "[branch] if (g_SpecConstants() & SPEC_CONSTANT_ALPHA_TEST)";
@ -1769,6 +1808,10 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
indent(); indent();
out += "}"; out += "}";
#ifdef UNLEASHED_RECOMP
specConstantsMask |= SPEC_CONSTANT_ALPHA_TO_COVERAGE;
indent(); indent();
out += "else if (g_SpecConstants() & SPEC_CONSTANT_ALPHA_TO_COVERAGE)"; out += "else if (g_SpecConstants() & SPEC_CONSTANT_ALPHA_TO_COVERAGE)";
indent(); indent();
@ -1781,15 +1824,22 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
indent(); indent();
out += '}'; out += '}';
#endif
} }
if (simpleControlFlow) if (simpleControlFlow)
{ {
indent(); indent();
#ifdef UNLEASHED_RECOMP
if (hasMtxProjection) if (hasMtxProjection)
{
out += "continue;\n"; out += "continue;\n";
}
else else
#endif
{
out += "return;\n"; out += "return;\n";
}
} }
else else
{ {
@ -1817,8 +1867,10 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi
out += "\t}\n"; out += "\t}\n";
} }
#ifdef UNLEASHED_RECOMP
if (hasMtxProjection) if (hasMtxProjection)
out += "\t}\n"; out += "\t}\n";
#endif
out += "}"; out += "}";
} }

View file

@ -33,8 +33,11 @@ struct ShaderRecompiler : StringBuffer
std::unordered_map<uint32_t, const char*> samplers; std::unordered_map<uint32_t, const char*> samplers;
std::unordered_map<uint32_t, uint32_t> ifEndLabels; std::unordered_map<uint32_t, uint32_t> ifEndLabels;
uint32_t specConstantsMask = 0; uint32_t specConstantsMask = 0;
#ifdef UNLEASHED_RECOMP
bool hasMtxProjection = false; bool hasMtxProjection = false;
bool hasMtxPrevInvViewProjection = false; bool hasMtxPrevInvViewProjection = false;
#endif
void indent() void indent()
{ {