Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2965,6 +2965,7 @@ ShaderRecompiler::SpirvTarget VulkanDevice::buildTarget() const {
target.fixedPushSlots = state->graphicsPipelineLibrary;
target.registrationModules = state->graphicsPipelineLibrary;
target.srgbDecodeFormats = state->srgbDecodeFormats;
target.vertexSubgroups = (state->subgroup.supportedStages & VK_SHADER_STAGE_VERTEX_BIT) != 0;
if (state->meshShader) {
const auto& mesh = state->meshLimits;
target.mesh = ShaderRecompiler::MeshTargetLimits{{mesh.maxMeshWorkGroupSize[0], mesh.maxMeshWorkGroupSize[1], mesh.maxMeshWorkGroupSize[2]}, mesh.maxMeshWorkGroupInvocations, std::min(mesh.maxMeshSharedMemorySize, mesh.maxMeshPayloadAndSharedMemorySize), mesh.maxMeshOutputVertices, mesh.maxMeshOutputPrimitives, mesh.maxMeshOutputComponents, std::min(mesh.maxMeshOutputMemorySize, mesh.maxMeshPayloadAndOutputMemorySize), mesh.meshOutputPerVertexGranularity, mesh.meshOutputPerPrimitiveGranularity};
Expand Down
93 changes: 91 additions & 2 deletions core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,94 @@ void verifyNarrowVertexWave() {
require(GetPreparedArtifact(*PrepareShader(vertex(wave32, 32u, 32u, 8u))).spirv == GetPreparedArtifact(*PrepareShader(vertex(wave32, 32u, 64u, 8u))).spirv, "narrow vertex wave: s3 of a wave32 vertex program depended on the subgroup width");
}

void verifySingleLaneVertexWave() {
using namespace ShaderRecompiler;
static constexpr std::array<std::uint32_t, 5> capabilities{spv::CapabilityGroupNonUniform, spv::CapabilityGroupNonUniformBallot, spv::CapabilityGroupNonUniformShuffle, spv::CapabilityGroupNonUniformArithmetic, spv::CapabilityClipDistance};
const auto vertex = [](std::span<const std::uint32_t> code, std::uint32_t subgroupSize, bool vertexSubgroups) {
RecompileRequest request{};
request.shader = {ShaderStage::Vertex, 0x58000u, code, 0, {}};
request.context.waveSize = 64;
request.context.userDataBaseRegister = 8;
request.context.vertex = ShaderVertexStageInfo{};
request.target.vulkanVersion = 0x00401000u;
request.target.spirvVersion = 0x00010300u;
request.target.subgroupSize = subgroupSize;
request.target.supportedCapabilities = capabilities;
request.target.vertexSubgroups = vertexSubgroups;
request.layout.pushConstantSizeBytes = 128;
request.useCache = false;
return request;
};
const auto compiled = [&vertex](std::span<const std::uint32_t> code, std::uint32_t subgroupSize, bool vertexSubgroups) {
return Recompile(vertex(code, subgroupSize, vertexSubgroups)).spirv;
};
const auto usesSubgroups = [](const std::vector<std::uint32_t>& spirv) {
for (std::size_t cursor = 5; cursor < spirv.size() && (spirv[cursor] >> 16u) != 0u; cursor += spirv[cursor] >> 16u) {
const auto op = spirv[cursor] & 0xffffu;
if (op == spv::OpCapability && spirv[cursor + 1u] >= spv::CapabilityGroupNonUniform && spirv[cursor + 1u] <= spv::CapabilityGroupNonUniformQuad) return true;
if (op >= spv::OpGroupNonUniformElect && op <= spv::OpGroupNonUniformQuadSwap) return true;
if (op == spv::OpDecorate && spirv[cursor + 2u] == spv::DecorationBuiltIn && (spirv[cursor + 3u] == spv::BuiltInSubgroupLocalInvocationId || spirv[cursor + 3u] == spv::BuiltInSubgroupSize)) return true;
}
return false;
};
const auto hasConstant = [](const std::vector<std::uint32_t>& spirv, std::uint32_t value) {
for (std::size_t cursor = 5; cursor < spirv.size() && (spirv[cursor] >> 16u) != 0u; cursor += spirv[cursor] >> 16u) {
if ((spirv[cursor] & 0xffffu) == spv::OpConstant && (spirv[cursor] >> 16u) == 4u && spirv[cursor + 3u] == value) return true;
}
return false;
};
static constexpr std::array<std::uint32_t, 4> waveInfo{0x7e020203u, 0xf80008cfu, 0x01010101u, 0xbf810000u};
static constexpr std::array<std::uint32_t, 12> laneOps{
0xd7650015u, 0x000100c1u, 0xd7660015u, 0x00022ac1u, 0x7e0c0500u, 0xd7600007u, 0x00010100u, 0x4a022a06u,
0x4a020207u, 0xf80008cfu, 0x01010101u, 0xbf810000u,
};
static constexpr std::array<std::uint32_t, 11> waterfall{0xbe80047eu, 0xbe821400u, 0xd7600003u, 0x00000500u, 0xbe801c02u, 0xbf138000u, 0xbf85fffau, 0x7e020203u, 0xf80008cfu, 0x01010101u, 0xbf810000u};
static constexpr std::array<std::uint32_t, 6> lane31{0xd7600006u, 0x00013f00u, 0x7e020206u, 0xf80008cfu, 0x01010101u, 0xbf810000u};

const auto single = compiled(waveInfo, 32u, false);
const auto wide = compiled(waveInfo, 64u, true);
require(hasConstant(single, 0x0101u) && !hasConstant(single, 0x4040u) && !hasConstant(single, 0x2020u), "single-lane vertex wave: s3 of a vertex program without vertex subgroups was not one vertex and one primitive");
require(hasConstant(wide, 0x4040u) && !hasConstant(wide, 0x0101u), "single-lane vertex wave: s3 of a vertex program with vertex subgroups on 64 lanes changed");
require(single == compiled(waveInfo, 4u, false), "single-lane vertex wave: the program without vertex subgroups depended on the device subgroup size");
for (const auto code : {std::span<const std::uint32_t>(laneOps), std::span<const std::uint32_t>(waterfall)}) {
const auto withSubgroups = compiled(code, 32u, true);
const auto withoutSubgroups = compiled(code, 32u, false);
require(usesSubgroups(withSubgroups), "single-lane vertex wave: the program did not use subgroup operations with vertex subgroups");
require(!withoutSubgroups.empty() && !usesSubgroups(withoutSubgroups), "single-lane vertex wave: a vertex program without vertex subgroups declared a subgroup capability, built-in or instruction");
}
expectFailure([&] { static_cast<void>(compiled(lane31, 32u, false)); }, "v_readlane_b32 of lane 31 is outside the 1-lane host subgroup", "single-lane vertex wave: a vertex program without vertex subgroups read lane 31");
require(!compiled(lane31, 32u, true).empty(), "single-lane vertex wave: lane 31 of a 32-lane vertex subgroup did not build");

ShaderPixelStageInfo info{};
info.inputAddr = PixelInputBit(PixelInput::PerspectiveCenter);
info.hasPerspectiveCenterVgpr = true;
info.targetOutputMode[0] = 9u;
info.targetExportMapping.fill(0xe4u);
static constexpr std::array<std::uint32_t, 6> pixelLane31{0xd7600006u, 0x00013f00u, 0x7e020206u, 0xf800180fu, 0x01010101u, 0xbf810000u};
RecompileRequest pixel{};
pixel.shader = {ShaderStage::Fragment, 0x30000u, pixelLane31, 0, {}};
pixel.context.waveSize = 64;
pixel.context.pixel = info;
pixel.target.vulkanVersion = 0x00401000u;
pixel.target.spirvVersion = 0x00010300u;
pixel.target.subgroupSize = 32u;
pixel.target.vertexSubgroups = false;
pixel.layout.pushConstantSizeBytes = 128;
pixel.useCache = false;
require(usesSubgroups(Recompile(pixel).spirv), "single-lane vertex wave: a pixel program stopped using subgroups when only the vertex stage lacks them");

auto request = vertex(waveInfo, 32u, false);
const auto replay = RequestSerializer{}.Deserialize(RequestSerializer{}.Serialize(request));
require(!replay.request.target.vertexSubgroups && replay.request.target.subgroupSize == 32u, "single-lane vertex wave: missing vertex subgroups were lost in serialization");
require(Recompile(replay.request).spirv == single, "single-lane vertex wave: the replayed request built another program");
std::vector<std::uint64_t> withoutKey;
std::vector<std::uint64_t> withKey;
RecompileCacheKey::Build(request, withoutKey);
request.target.vertexSubgroups = true;
RecompileCacheKey::Build(request, withKey);
require(withoutKey != withKey, "single-lane vertex wave: vertex subgroup support did not change the cache key");
}

void verifyMeshConfiguration() {
using namespace ShaderRecompiler;
ShaderMeshInputInfo list;
Expand Down Expand Up @@ -1637,12 +1725,12 @@ void verifyPixelRequestSerialization() {
minimal.context.waveSize = 64;
minimal.context.pixel = ShaderPixelStageInfo{};
const auto encoded = serializer.Serialize(minimal);
require(requestPrefix(encoded, 8u) == "NVNQQREAAAA=", "new requests did not use serialization version 17");
require(requestPrefix(encoded, 8u) == "NVNQQRIAAAA=", "new requests did not use serialization version 18");
constexpr std::size_t mappingOffset = 8u + 37u + 18u + 163u;
for (std::size_t bytes = 0; bytes < 19u; ++bytes) {
expectFailure([&] { static_cast<void>(serializer.Deserialize(requestPrefix(encoded, mappingOffset + bytes))); }, "truncated data", "a truncated pixel mapping, packing, dual-source, alpha-to-coverage or stencil reference export flag was accepted");
}
for (const auto unsupported : {"NVNQQQAAAAA=", "NVNQQRIAAAA="}) {
for (const auto unsupported : {"NVNQQQAAAAA=", "NVNQQRMAAAA="}) {
expectFailure([&] { static_cast<void>(serializer.Deserialize(unsupported)); }, "serialization version", "an unsupported request version was accepted");
}
}
Expand Down Expand Up @@ -3107,6 +3195,7 @@ int main(int argc, char** argv) {
verifyLanesOutsideHostSubgroup();
verifyHalfWaveReduction();
verifyNarrowVertexWave();
verifySingleLaneVertexWave();
verifyMeshConfiguration();
verifyPixelInputs();
verifyPixelRequestSerialization();
Expand Down
1 change: 1 addition & 0 deletions core/shader/recompiler/CacheKey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class RecompileCacheKey {
append(key, value.float64DenormPreserve);
append(key, value.registrationModules);
append(key, value.registrationWordLimit);
append(key, value.vertexSubgroups);
}
};

Expand Down
6 changes: 4 additions & 2 deletions core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ std::string RequestSerializer::Serialize(const RecompileRequest& request) const
std::string buffer;
Writer writer(buffer);
writer.WriteU32(0x41505335u);
writer.WriteU32(17u);
writer.WriteU32(18u);
writeShaderBinary(writer, request.shader);
writeGuestContext(writer, request.context);
writeSpirvTarget(writer, request.target);
Expand All @@ -758,6 +758,7 @@ std::string RequestSerializer::Serialize(const RecompileRequest& request) const
writer.WriteBool(request.target.float64DenormPreserve);
writer.WriteBool(request.target.registrationModules);
writer.WriteU32(request.target.registrationWordLimit);
writer.WriteBool(request.target.vertexSubgroups);
return base64Encode(buffer);
}

Expand All @@ -766,7 +767,7 @@ DeserializedRequest RequestSerializer::Deserialize(std::string_view text) const
Reader reader(decoded);
if (reader.ReadU32() != 0x41505335u) throw std::runtime_error("invalid recompile request signature");
const auto version = reader.ReadU32();
if (version < 1u || version > 17u) throw std::runtime_error("unsupported recompile request serialization version");
if (version < 1u || version > 18u) throw std::runtime_error("unsupported recompile request serialization version");
DeserializedRequest result{};
result.request.shader = readShaderBinary(reader, result.shaderCode, result.shaderHeader);
result.request.context = readGuestContext(reader, result, version);
Expand All @@ -789,6 +790,7 @@ DeserializedRequest RequestSerializer::Deserialize(std::string_view text) const
if (version >= 16u) result.request.target.float64DenormPreserve = reader.ReadBool();
if (version >= 17u) result.request.target.registrationModules = reader.ReadBool();
if (version >= 17u) result.request.target.registrationWordLimit = reader.ReadU32();
if (version >= 18u) result.request.target.vertexSubgroups = reader.ReadBool();
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion core/shader/recompiler/Recompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace {
// The host subgroup width wave64 programs are laid out for. Debug aid: APS5_SINGLE_LANE=<hex code
// addresses, comma separated, or "all"> keeps the listed programs at one guest lane per invocation.
std::uint32_t HostSubgroupSize(const RecompileRequest& request) {
const bool vertexStage = request.shader.stage == ShaderStage::Vertex || request.shader.stage == ShaderStage::Local;
if (vertexStage && !request.target.vertexSubgroups) return 1u;
static const std::string list = [] { const char* text = std::getenv("APS5_SINGLE_LANE"); return text ? std::string(text) : std::string(); }();
if (!list.empty()) {
if (list == "all") return 64u;
Expand Down Expand Up @@ -414,7 +416,7 @@ CompiledVariant compileVariant(const RecompileRequest& request, IrProgram progra
SpirvTargetOptions targetOptions {};
targetOptions.vulkanVersion = request.target.vulkanVersion;
targetOptions.spirvVersion = request.target.spirvVersion;
targetOptions.subgroupSize = request.target.subgroupSize;
targetOptions.subgroupSize = HostSubgroupSize(request) == 1u ? 1u : request.target.subgroupSize;
targetOptions.bdaAbiVersion = request.target.bdaAbiVersion;
targetOptions.supportedCapabilities = request.target.supportedCapabilities;
targetOptions.supportedExtensions = request.target.supportedExtensions;
Expand Down
1 change: 1 addition & 0 deletions core/shader/recompiler/Recompiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ struct SpirvTarget {
bool float64DenormPreserve = false;
bool registrationModules = false;
std::uint32_t registrationWordLimit = 200000u;
bool vertexSubgroups = true;
};

struct BindingLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace ShaderRecompiler {

bool HostSubgroupSingleLane(const SpirvEmitterState& state);
void EmitSubgroupBallot(SpirvEmitterState& state, std::uint32_t result, std::uint32_t predicate);
void EmitSubgroupBallotFindLsb(SpirvEmitterState& state, std::uint32_t result, std::uint32_t ballot);
void EmitSubgroupShuffle(SpirvEmitterState& state, std::uint32_t type, std::uint32_t result, std::uint32_t value, std::uint32_t lane);
void EmitSubgroupElect(SpirvEmitterState& state, std::uint32_t result);
std::uint32_t EmitSubgroupLocalInvocationId(SpirvEmitterState& state);
std::uint32_t EmitHostSubgroupLane(SpirvEmitterState& state, std::uint32_t lane);
std::uint32_t EmitWaveBallot(SpirvEmitterState& state, std::uint32_t ballot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ std::uint32_t EmitDsMaskedLaneRead(SpirvEmitterState& state, std::uint32_t sourc
}
const auto physicalLane = EmitHostSubgroupLane(state, lane);
const auto shuffled = state.module.AllocateId();
state.module.AddFunction(spv::OpGroupNonUniformShuffle, TypeU32(state), shuffled, ConstantU32(state, spv::ScopeSubgroup), source, physicalLane);
EmitSubgroupShuffle(state, TypeU32(state), shuffled, source, physicalLane);
const auto sourceExec = state.module.AllocateId();
state.module.AddFunction(spv::OpGroupNonUniformShuffle, TypeBool(state), sourceExec, ConstantU32(state, spv::ScopeSubgroup), exec, physicalLane);
EmitSubgroupShuffle(state, TypeBool(state), sourceExec, exec, physicalLane);
const auto sourceActive = Binary(state, spv::OpLogicalAnd, TypeBool(state), sourceExec, EmitSubgroupLaneActiveBool(state, lane));
return Select(state, TypeU32(state), sourceActive, shuffled, ConstantU32(state, 0u));
}
Expand Down Expand Up @@ -563,6 +563,7 @@ std::optional<std::uint32_t> EmitHalfWaveReduction(SpirvValueEmitContext& ctx, c
if (lane < 32u || state.hostSubgroupSize > 32u) return std::nullopt;
return ConstantU32(state, scan->reduction->identity);
}
if (HostSubgroupSingleLane(state)) return lane >= 32u ? ConstantU32(state, scan->reduction->identity) : ctx.Def(scan->source);
const auto read = "v_readlane_b32 of lane " + std::to_string(lane) + " of a wave64 half-wave reduction scan";
const auto& capabilities = state.supportedCapabilities;
if (std::find(capabilities.begin(), capabilities.end(), static_cast<std::uint32_t>(spv::CapabilityGroupNonUniformArithmetic)) == capabilities.end()) {
Expand Down
11 changes: 8 additions & 3 deletions core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ std::uint32_t SpirvValueEmitContext::Ballot(const IrValue* predicate) {
const auto ballotType = TypeU32Vector(state, 4u);
const auto scope = ConstantU32(state, spv::ScopeSubgroup);
const auto low = state.module.AllocateId();
state.module.AddFunction(spv::OpGroupNonUniformBallot, ballotType, low, scope, otherHalf == nullptr || half == 0u ? Def(predicate) : otherHalf->Def(predicate));
EmitSubgroupBallot(state, low, otherHalf == nullptr || half == 0u ? Def(predicate) : otherHalf->Def(predicate));
if (otherHalf == nullptr) {
return EmitWaveBallot(state, low);
}
Expand All @@ -136,7 +136,7 @@ std::uint32_t SpirvValueEmitContext::Ballot(const IrValue* predicate) {
std::uint32_t SpirvValueEmitContext::FirstLane(std::uint32_t ballot) {
if (otherHalf == nullptr) {
const auto result = state.module.AllocateId();
state.module.AddFunction(spv::OpGroupNonUniformBallotFindLSB, TypeU32(state), result, ConstantU32(state, spv::ScopeSubgroup), ballot);
EmitSubgroupBallotFindLsb(state, result, ballot);
return result;
}
const auto low = state.module.AllocateId();
Expand All @@ -159,7 +159,7 @@ std::uint32_t SpirvValueEmitContext::Shuffle(const IrValue& inst, std::size_t in
const auto scope = ConstantU32(state, spv::ScopeSubgroup);
const auto low = state.module.AllocateId();
if (otherHalf == nullptr) {
state.module.AddFunction(spv::OpGroupNonUniformShuffle, type, low, scope, Arg(inst, index), EmitHostSubgroupLane(state, lane));
EmitSubgroupShuffle(state, type, low, Arg(inst, index), EmitHostSubgroupLane(state, lane));
return low;
}
const auto physicalLane = EmitBinaryU32(state, spv::OpBitwiseAnd, lane, ConstantU32(state, 31u));
Expand Down Expand Up @@ -253,6 +253,11 @@ std::vector<std::uint32_t> SpirvEmitter::Emit(const IrProgram& program, const Sh
state.registrationModules = target.registrationModules;
state.fragmentDescriptorSet = target.fragmentDescriptorSet;
state.hostSubgroupSize = target.subgroupSize;
if (HostSubgroupSingleLane(state)) {
state.requirements.subgroupBallot = false;
state.requirements.subgroupShuffle = false;
state.requirements.subgroupLocalInvocationId = false;
}
state.splitSubgroup = program.WaveSize() == 32u && target.subgroupSize > 32u;
if (state.splitSubgroup && (state.requirements.subgroupBallot || state.requirements.subgroupShuffle)) state.requirements.subgroupLocalInvocationId = true;
const auto* workgroup = ShaderWorkgroupInputFor(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ bool IsContinueTarget(const IrProgram& program, std::uint32_t block) {

std::uint32_t EmitWaveAny(SpirvEmitterState& state, std::uint32_t predicate) {
const auto ballot = state.module.AllocateId();
state.module.AddFunction(spv::OpGroupNonUniformBallot, TypeU32Vector(state, 4u), ballot, ConstantU32(state, spv::ScopeSubgroup), predicate);
EmitSubgroupBallot(state, ballot, predicate);
const auto wave = EmitWaveBallot(state, ballot);
const auto low = state.module.AllocateId();
const auto high = state.module.AllocateId();
Expand Down
Loading
Loading