Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
70acc43
perf(shader): skip redundant 32-bit sint clamping in StoreTexel
hamb3r Oct 20, 2026
70ec4c8
perf(shader): skip redundant unnormalized base-level branch when alre…
hamb3r Oct 20, 2026
d8ea386
perf(shader): deduplicate equivalent runtime image switch mode blocks…
hamb3r Oct 20, 2026
dd90db5
test(shader): cover packed and mip-mode runtime image switch deduplic…
hamb3r Oct 21, 2026
8a747c6
Merge remote-tracking branch 'origin/main' into perf/shader-image-emi…
hamb3r Oct 22, 2026
beeda2f
Merge remote-tracking branch 'origin/main' into perf/shader-image-emi…
hamb3r Oct 23, 2026
e650437
Merge remote-tracking branch 'origin/main' into perf/shader-image-emi…
hamb3r Oct 27, 2026
85a7415
perf(shader): balance dense runtime image dispatch
Filip722 Oct 28, 2026
0f90dc4
chore(shader): update balanced dispatch to current main
Filip722 Oct 28, 2026
c661de7
chore(shader): refresh PR 12299 onto current main
Filip722 Oct 29, 2026
e192e0c
chore(shader): refresh PR 12299 after upstream shader merges
Filip722 Oct 29, 2026
85eba6b
chore(shader): refresh PR 12299 after upstream AGC merges
Filip722 Oct 29, 2026
01514a6
Merge remote-tracking branch 'origin/main' into perf/shader-image-emi…
hamb3r Oct 29, 2026
b461c31
fix(shader): preserve equivalent image mode deduplication
Filip722 Oct 29, 2026
3de4522
chore(shader): integrate current main for PR 12299
Filip722 Oct 29, 2026
ca770b7
chore(shader): refresh PR 12299 for repeated performance validation
Filip722 Oct 29, 2026
53601c1
Merge commit 'ca770b73f881ed05979a87d4f133f5d572fad1fa' into review/i…
Filip722 Oct 31, 2026
5e4b068
test(shader): give runtime image fixture a separate heap source
Filip722 Oct 31, 2026
37d4bd0
fix(ci): register macOS write-watch fault tests once
Filip722 Oct 31, 2026
7330dd6
Merge remote-tracking branch 'origin/main' into perf/shader-image-emi…
hamb3r Oct 31, 2026
45998c3
test(shader): give runtime image fixture a separate heap source
hamb3r Oct 31, 2026
c2d483e
chore(shader): integrate refreshed image mode prerequisite
Filip722 Nov 1, 2026
12367bd
chore(shader): refresh balanced image dispatch against main
Filip722 Nov 1, 2026
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
145 changes: 145 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "IntermediateRepresentation/IrBuilder.hpp"
#include "IntermediateRepresentation/IrProgram.hpp"
#include "SpirvBackend/SpirvAnalysis.hpp"
#include "SpirvBackend/SpirvEmitter.hpp"
#include "SavedExecMaskLoop.hpp"
#if ANYPS5_ENABLE_SPIRV_TOOLS
#include "SpirvBackend/SpirvOptimizer.hpp"
Expand Down Expand Up @@ -10151,6 +10152,149 @@ void verifySrgbDepthCompareModes() {
}
}

void verifyRuntimeImageSwitchDeduplication() {
using namespace ShaderRecompiler;
const auto emitImageSwitch = [](IrOpcode opcode, bool packed) {
const bool read = opcode == IrOpcode::ImageRead;
IrProgram program;
program.SetWaveSize(32u);
program.Resources().stage = IrShaderStage::Compute;
program.Resources().srtPlanComplete = true;
program.Resources().resourceTrackingComplete = true;
program.Metadata().shaderInfoComplete = true;
program.Metadata().bindingLayoutComplete = true;
auto& block = program.CreateBlock();
program.SetEntryBlock(block);
program.BlockOrder().push_back(&block);
program.Metadata().blockInfo.emplace_back();
auto& zero = program.CreateValue(IrOpcode::Void, IrType::U32);
zero.SetImmediateU32(0u);
auto& enabled = program.CreateValue(IrOpcode::Void, IrType::Bool);
enabled.SetImmediateBool(true);
auto& handle = program.CreateValue(IrOpcode::GetImageResource, IrType::ImageResource, 0u);
for (std::uint32_t i = 0u; i < 8u; ++i) handle.AddArgument(&zero);
block.AppendInstruction(&handle);
auto& address = program.CreateValue(IrOpcode::MakeImageAddress, IrType::ImageAddress);
for (std::uint32_t i = 0u; i < 13u; ++i) address.AddArgument(&zero);
block.AppendInstruction(&address);
MemoryInfo memory{};
memory.kind = ResourceKind::Image;
memory.resource = 0u;
memory.imageDimension = packed ? RdnaImageDimension::Dim2DArray : RdnaImageDimension::Dim2D;
memory.imageAddressComponents = packed ? 3u : 2u;
memory.imagePacked = packed;
memory.dmask = 0xfu;
memory.dataBits = 32u;
memory.dataDwords = 4u;
memory.componentCount = 4u;
program.Resources().memoryInfo.push_back(memory);
ImageResource image{};
image.resourceClass = read ? ImageResourceClass::Sampled : ImageResourceClass::Storage;
image.numericClass = IrTextureNumericClass::Uint;
image.dimension = memory.imageDimension;
image.packed = packed;
image.read = read;
image.written = !read;
std::vector<ImageResource> modes;
if (packed) {
modes = ResourceMaterializer::RuntimeImageModes(image);
for (const auto& mode : modes) {
const auto kind = DescriptorBindingForImage(mode);
auto& descriptors = program.Metadata().bindings.descriptors;
if (std::ranges::none_of(descriptors, [&](const IrDescriptorBinding& entry) { return entry.kind == kind; })) {
descriptors.push_back({kind, {0u}});
}
}
} else {
image.table = 0u;
DescriptorSource source{};
source.dwordCount = 8u;
TableColumn column{};
column.stride = 32u;
column.heapSource = 1u;
source.tableColumn = column;
program.Resources().descriptorSources.push_back(source);
DescriptorSource heap{};
heap.dwordCount = 4u;
program.Resources().descriptorSources.push_back(heap);
modes = {image, image};
modes[1].mipMode = static_cast<ImageMipMode>(2);
program.Metadata().bindings.descriptors = {
{DescriptorBindingKind::FaultBuffer, {}},
{DescriptorBindingKind::ImageTableMap, {}},
{DescriptorBindingKind::ImageStorageTable, {}},
};
}
program.Info().images.push_back(image);
program.Info().runtimeImageModes.push_back(modes);
if (read) {
auto& op = program.CreateValue(IrOpcode::ImageRead, IrType::U32x4);
op.AddArgument(&handle);
op.AddArgument(&address);
op.AddArgument(&enabled);
block.AppendInstruction(&op);
} else {
auto& texel = program.CreateValue(IrOpcode::CompositeConstructU32x4, IrType::U32x4);
for (std::uint32_t i = 0u; i < 4u; ++i) texel.AddArgument(&zero);
block.AppendInstruction(&texel);
auto& op = program.CreateValue(IrOpcode::ImageWrite, IrType::Void);
op.AddArgument(&handle);
op.AddArgument(&address);
op.AddArgument(&texel);
op.AddArgument(&enabled);
block.AppendInstruction(&op);
}
ShaderComputeInputInfo compute{};
compute.threadsNum[0] = 32u;
compute.threadsNum[1] = 1u;
compute.threadsNum[2] = 1u;
compute.hostSubgroupSize = 32u;
compute.waveSize = 32u;
ShaderStageInputInfo inputs{};
inputs.compute = &compute;
CompiledBindingLayout bindings{};
bindings.layout = program.Metadata().bindings;
SpirvTargetOptions target{};
target.vulkanVersion = 0x00401000u;
target.spirvVersion = 0x00010300u;
target.subgroupSize = 32u;
const auto words = SpirvEmitter().Emit(program, inputs, bindings, target);
std::map<std::uint32_t, std::uint32_t> targets;
for (std::size_t cursor = 5u; cursor < words.size(); cursor += words[cursor] >> 16u) {
const auto count = words[cursor] >> 16u;
require(count != 0u && count <= words.size() - cursor, "runtime image switch deduplication: truncated SPIR-V instruction");
if ((words[cursor] & 0xffffu) == spv::OpSwitch) {
for (std::size_t at = cursor + 3u; at + 1u < cursor + count; at += 2u) {
targets.emplace(words[at], words[at + 1u]);
}
}
}
require(targets.size() == modes.size(), "runtime image switch deduplication: OpSwitch did not cover every runtime image mode");
return std::pair{modes, targets};
};
for (const auto opcode : {IrOpcode::ImageRead, IrOpcode::ImageWrite}) {
const auto [modes, targets] = emitImageSwitch(opcode, true);
bool deduplicatedCube = false;
bool separatedFormats = false;
for (std::uint32_t i = 0u; i < modes.size(); ++i) {
for (std::uint32_t j = i + 1u; j < modes.size(); ++j) {
if (modes[i].dimension == modes[j].dimension && modes[i].numericClass == modes[j].numericClass) {
if (modes[i].packedFormat == modes[j].packedFormat) {
require(targets.at(i) == targets.at(j), "runtime image switch deduplication: packed cube and array modes with identical format were not merged");
deduplicatedCube = true;
} else {
require(targets.at(i) != targets.at(j), "runtime image switch deduplication: distinct packed formats shared an OpSwitch target");
separatedFormats = true;
}
}
}
}
require(deduplicatedCube && separatedFormats, "runtime image switch deduplication: packed modes did not exercise both merged and distinct OpSwitch targets");
}
const auto [mipModes, mipTargets] = emitImageSwitch(IrOpcode::ImageWrite, false);
require(mipTargets.at(0u) != mipTargets.at(1u), "runtime image switch deduplication: storage image modes with different mip modes shared an OpSwitch target");
}

int main(int argc, char** argv) {
try {
#ifdef _WIN32
Expand Down Expand Up @@ -10355,6 +10499,7 @@ int main(int argc, char** argv) {
verifySamplerTableDeduplication();
verifyDynamicStorageMipChain();
verifySrgbDepthCompareModes();
verifyRuntimeImageSwitchDeduplication();
verifyQuadMaskNeighbourReads();
verifyCoherentGds();
#if ANYPS5_ENABLE_SPIRV_TOOLS
Expand Down
103 changes: 99 additions & 4 deletions core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ std::uint32_t StoreTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess& acce
if (float16 && component < storageFormat.componentCount && mem.dataBits != 16u) {
values[component] = EmitFormatStoreComponent(state, storageFormat, component, values[component]);
}
if (component < sint.componentCount) {
if (component < sint.componentCount && (mem.dataBits == 16u || sint.componentBits[component] < 32u)) {
values[component] = mem.dataBits == 16u ? EmitD16StoreComponent(state, sint, component, values[component]) : EmitFormatStoreComponent(state, sint, component, values[component]);
}
}
Expand Down Expand Up @@ -1662,7 +1662,8 @@ void EmitSampleOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, con
return sample;
};
std::uint32_t sample = 0;
const bool baseLevelIfUnnormalized = !setup.dref && SamplesBaseLevelIfUnnormalized(state.program.Info().samplers.at(mem.sampler));
const bool alreadyBaseLevel = !hasOffset && opcode == spv::OpImageSampleExplicitLod && operandMask == spv::ImageOperandsLodMask && !HasFlag(mem, RdnaImageSampleFlagLod) && setup.layout.clamp == NoImageComponent;
const bool baseLevelIfUnnormalized = !setup.dref && !alreadyBaseLevel && SamplesBaseLevelIfUnnormalized(state.program.Info().samplers.at(mem.sampler));
if (baseLevelIfUnnormalized) {
const auto selector = state.module.SpecializationConstant(TypeU32(state), PipelineSpecialization::UnnormalizedBaseLevelBase + mem.sampler, 0u);
const auto unnormalized = Binary(state, spv::OpINotEqual, TypeBool(state), selector, ConstantU32(state, 0u));
Expand Down Expand Up @@ -1981,6 +1982,47 @@ void EmitImageMode(SpirvValueEmitContext& ctx, const IrValue& inst, const ImageR
}
}

bool SameImageModeForInstruction(const IrValue& inst, const MemoryInfo& memory, const ImageResource& a, const ImageResource& b) {
if (a.resourceClass != b.resourceClass || a.dimension != b.dimension || a.numericClass != b.numericClass ||
a.depthCompare != b.depthCompare || a.atomic != b.atomic || a.atomic64 != b.atomic64 || a.table != b.table || a.depthBits != b.depthBits) {
return false;
}
const auto opcode = inst.Opcode();
if (ImageOpcodeInfoOf(opcode).access == ImageAccess::Atomic || opcode == IrOpcode::ImageQueryDimensions) {
return true;
}
if (opcode == IrOpcode::ImageQueryLod) {
const bool pointA = a.table != NoTable ? TablePointSampled(a) : (a.numericClass == IrTextureNumericClass::Sint || a.conversionFormat != IrBufferFormat::Invalid || a.depthBits);
const bool pointB = b.table != NoTable ? TablePointSampled(b) : (b.numericClass == IrTextureNumericClass::Sint || b.conversionFormat != IrBufferFormat::Invalid || b.depthBits);
return a.cube == b.cube && pointA == pointB && ImageConversionFormat(a).type == ImageConversionFormat(b).type;
}
if (opcode == IrOpcode::ImageRead) {
if (a.depthUnorm16 != b.depthUnorm16 || a.srgbDecode != b.srgbDecode) {
return false;
}
if (memory.imagePacked) {
return a.packedFormat == b.packedFormat && (a.conversionFormat != IrBufferFormat::Invalid) == (b.conversionFormat != IrBufferFormat::Invalid);
}
return a.conversionFormat == b.conversionFormat && (a.conversionFormat == IrBufferFormat::Invalid || a.shaderSwizzle == b.shaderSwizzle);
}
if (opcode == IrOpcode::ImageWrite) {
if (a.mipMode != b.mipMode) {
return false;
}
if (memory.imagePacked) {
return a.packedFormat == b.packedFormat && (a.conversionFormat != IrBufferFormat::Invalid) == (b.conversionFormat != IrBufferFormat::Invalid);
}
const auto formatForWrite = [&](const ImageResource& image) {
if (memory.dataBits != 16u && (image.conversionFormat == IrBufferFormat::Format32SInt || image.conversionFormat == IrBufferFormat::Format32_32SInt || image.conversionFormat == IrBufferFormat::Format32_32_32_32SInt)) {
return IrBufferFormat::Invalid;
}
return image.conversionFormat;
};
return formatForWrite(a) == formatForWrite(b);
}
return false;
}

void EmitImage(SpirvValueEmitContext& ctx, const IrValue& inst) {
auto& state = ctx.state;
const auto& memory = ctx.Memory(inst);
Expand Down Expand Up @@ -2024,6 +2066,46 @@ void EmitImage(SpirvValueEmitContext& ctx, const IrValue& inst) {
const auto guardedId = returnsValue ? ctx.Result(inst) : 0u;
ctx.definitions.erase(&inst);
const bool exits = !state.continueTarget;
bool equivalentModes = false;
for (std::size_t i = 0u; i < modes.size() && !equivalentModes; ++i) {
for (std::size_t j = i + 1u; j < modes.size(); ++j) {
if (SameImageModeForInstruction(inst, memory, modes[i], modes[j])) {
equivalentModes = true;
break;
}
}
}
if (returnsValue && !tables && exits && modes.size() > 1u && !equivalentModes) {
const auto emitRange = [&](const auto& self, std::uint32_t begin, std::uint32_t end) -> std::uint32_t {
if (end - begin == 1u) {
emitMode(modes[begin]);
const auto value = ctx.Def(&inst);
ctx.definitions.erase(&inst);
return value;
}
const auto middle = begin + (end - begin) / 2u;
const auto condition = Binary(state, spv::OpULessThan, TypeBool(state), selector, ConstantU32(state, middle));
return EmitValueIfElse(state, condition, TypeId(state, inst.Type()), [&] { return self(self, begin, middle); }, [&] { return self(self, middle, end); });
};
const auto body = state.module.AllocateId();
const auto invalid = state.module.AllocateId();
const auto merge = state.module.AllocateId();
const auto count = static_cast<std::uint32_t>(modes.size());
const auto valid = Binary(state, spv::OpULessThan, TypeBool(state), selector, ConstantU32(state, count));
state.module.AddFunction(spv::OpSelectionMerge, merge, spv::SelectionControlMaskNone);
state.module.AddFunction(spv::OpBranchConditional, valid, body, invalid);
EmitLabel(state, body);
const auto value = emitRange(emitRange, 0u, count);
state.module.AddFunction(spv::OpBranch, merge);
EmitLabel(state, invalid);
state.module.AddFunction(spv::OpUnreachable);
EmitLabel(state, merge);
state.module.AddFunction(spv::OpCopyObject, TypeId(state, inst.Type()), guardedId, value);
state.runtimeImage = nullptr;
state.tableAccess = {};
ctx.definitions.emplace(&inst, guardedId);
return;
}
const auto guardMerge = samplerGuard != 0u ? state.module.AllocateId() : 0u;
const auto guardSkip = samplerGuard != 0u ? state.module.AllocateId() : 0u;
if (samplerGuard != 0u) {
Expand All @@ -2037,10 +2119,23 @@ void EmitImage(SpirvValueEmitContext& ctx, const IrValue& inst) {
const auto invalid = tables || exits ? state.module.AllocateId() : merge;
const auto undefined = returnsValue && !tables && !exits ? state.module.AllocateId() : 0u;
std::vector<std::uint32_t> labels(modes.size());
std::vector<bool> emitLabel(modes.size(), false);
std::vector<std::uint32_t> words{spv::OpSwitch, selector, invalid};
for (std::uint32_t index = 0u; index < modes.size(); ++index) {
if (base.table != NoTable && !TableModeEmitted(modes[index])) continue;
labels[index] = state.module.AllocateId();
std::uint32_t reused = 0u;
for (std::uint32_t prev = 0u; prev < index; ++prev) {
if (emitLabel[prev] && SameImageModeForInstruction(inst, memory, modes[prev], modes[index])) {
reused = labels[prev];
break;
}
}
if (reused != 0u) {
labels[index] = reused;
} else {
labels[index] = state.module.AllocateId();
emitLabel[index] = true;
}
words.insert(words.end(), {index, labels[index]});
}
std::vector<std::uint32_t> incoming;
Expand All @@ -2051,7 +2146,7 @@ void EmitImage(SpirvValueEmitContext& ctx, const IrValue& inst) {
state.module.AddFunction(spv::OpSelectionMerge, merge, spv::SelectionControlMaskNone);
state.module.AddFunction(words);
for (std::uint32_t index = 0u; index < modes.size(); ++index) {
if (labels[index] == 0u) continue;
if (!emitLabel[index]) continue;
EmitLabel(state, labels[index]);
emitMode(modes[index]);
if (returnsValue) incoming.insert(incoming.end(), {ctx.Def(&inst), state.currentLabel});
Expand Down
Loading