diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp index 82fdd645b..927c8ca8c 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp @@ -363,6 +363,7 @@ SpirvRequirements AnalyzeProgramRequirements(const IrProgram& program) { requirements.functionLds = true; } else if (sharedAccess == SharedAccess::Atomic && inst->Type() == IrType::U64 && kind == ResourceKind::Lds) { requirements.ldsLock = true; + requirements.subgroupBallot = true; } if (sharedAccess == SharedAccess::Append || sharedAccess == SharedAccess::Consume) { requirements.subgroupBallot = true; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp index 3657db5d6..a636d6699 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp @@ -1214,35 +1214,73 @@ std::uint32_t AppendConsume(SpirvValueEmitContext& ctx, const IrValue& inst, boo template std::uint32_t LockedLdsUpdate(SpirvEmitterState& state, TUpdate&& update) { const auto lock = EmitLdsLockPointer(state); + const auto entry = state.currentLabel; const auto header = state.module.AllocateId(); const auto body = state.module.AllocateId(); + const auto pending = state.module.AllocateId(); + const auto spinHeader = state.module.AllocateId(); + const auto spinBody = state.module.AllocateId(); + const auto spinCont = state.module.AllocateId(); const auto critical = state.module.AllocateId(); - const auto after = state.module.AllocateId(); + const auto servedMerge = state.module.AllocateId(); + const auto pendingMerge = state.module.AllocateId(); const auto cont = state.module.AllocateId(); const auto merge = state.module.AllocateId(); + const auto done = state.module.AllocateId(); + const auto result = state.module.AllocateId(); + const auto doneNext = state.module.AllocateId(); + const auto resultNext = state.module.AllocateId(); state.module.AddFunction(spv::OpBranch, header); EmitLabel(state, header); + state.module.AddFunction(spv::OpPhi, TypeBool(state), done, ConstantBool(state, false), entry, doneNext, cont); + state.module.AddFunction(spv::OpPhi, TypeU64(state), result, ConstantU64(state, 0u), entry, resultNext, cont); state.module.AddFunction(spv::OpLoopMerge, merge, cont, spv::LoopControlMaskNone); state.module.AddFunction(spv::OpBranch, body); EmitLabel(state, body); + state.module.AddFunction(spv::OpSelectionMerge, pendingMerge, spv::SelectionControlMaskNone); + state.module.AddFunction(spv::OpBranchConditional, done, pendingMerge, pending); + EmitLabel(state, pending); + const auto elected = state.module.AllocateId(); + state.module.AddFunction(spv::OpGroupNonUniformElect, TypeBool(state), elected, ConstantU32(state, spv::ScopeSubgroup)); + state.module.AddFunction(spv::OpSelectionMerge, servedMerge, spv::SelectionControlMaskNone); + state.module.AddFunction(spv::OpBranchConditional, elected, spinHeader, servedMerge); + EmitLabel(state, spinHeader); + state.module.AddFunction(spv::OpLoopMerge, critical, spinCont, spv::LoopControlMaskNone); + state.module.AddFunction(spv::OpBranch, spinBody); + EmitLabel(state, spinBody); const auto previous = state.module.AllocateId(); state.module.AddFunction(spv::OpAtomicCompareExchange, TypeU32(state), previous, lock, ConstantU32(state, spv::ScopeWorkgroup), ConstantU32(state, spv::MemorySemanticsAcquireMask | spv::MemorySemanticsWorkgroupMemoryMask), ConstantU32(state, spv::MemorySemanticsMaskNone), ConstantU32(state, 1u), ConstantU32(state, 0u)); const auto acquired = Binary(state, spv::OpIEqual, TypeBool(state), previous, ConstantU32(state, 0u)); - state.module.AddFunction(spv::OpSelectionMerge, after, spv::SelectionControlMaskNone); - state.module.AddFunction(spv::OpBranchConditional, acquired, critical, after); + state.module.AddFunction(spv::OpBranchConditional, acquired, critical, spinCont); + EmitLabel(state, spinCont); + state.module.AddFunction(spv::OpBranch, spinHeader); EmitLabel(state, critical); const auto old = update(); state.module.AddFunction(spv::OpAtomicStore, lock, ConstantU32(state, spv::ScopeWorkgroup), ConstantU32(state, spv::MemorySemanticsReleaseMask | spv::MemorySemanticsWorkgroupMemoryMask), ConstantU32(state, 0u)); const auto criticalExit = state.currentLabel; - state.module.AddFunction(spv::OpBranch, after); - EmitLabel(state, after); - const auto result = state.module.AllocateId(); - state.module.AddFunction(spv::OpPhi, TypeU64(state), result, old, criticalExit, ConstantU64(state, 0u), body); - state.module.AddFunction(spv::OpBranchConditional, acquired, merge, cont); + state.module.AddFunction(spv::OpBranch, servedMerge); + EmitLabel(state, servedMerge); + const auto served = state.module.AllocateId(); + const auto servedResult = state.module.AllocateId(); + state.module.AddFunction(spv::OpPhi, TypeBool(state), served, ConstantBool(state, true), criticalExit, ConstantBool(state, false), pending); + state.module.AddFunction(spv::OpPhi, TypeU64(state), servedResult, old, criticalExit, result, pending); + state.module.AddFunction(spv::OpBranch, pendingMerge); + EmitLabel(state, pendingMerge); + state.module.AddFunction(spv::OpPhi, TypeBool(state), doneNext, served, servedMerge, done, body); + state.module.AddFunction(spv::OpPhi, TypeU64(state), resultNext, servedResult, servedMerge, result, body); + const auto ballot = state.module.AllocateId(); + state.module.AddFunction(spv::OpGroupNonUniformBallot, TypeU32Vector(state, 4u), ballot, ConstantU32(state, spv::ScopeSubgroup), Unary(state, spv::OpLogicalNot, TypeBool(state), doneNext)); + std::uint32_t remaining = ConstantU32(state, 0u); + for (std::uint32_t component = 0; component < 4u; ++component) { + const auto word = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), word, ballot, component); + remaining = Binary(state, spv::OpBitwiseOr, TypeU32(state), remaining, word); + } + state.module.AddFunction(spv::OpBranchConditional, Binary(state, spv::OpINotEqual, TypeBool(state), remaining, ConstantU32(state, 0u)), cont, merge); EmitLabel(state, cont); state.module.AddFunction(spv::OpBranch, header); EmitLabel(state, merge); - return result; + return resultNext; } template