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
5 changes: 5 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/include/Recipe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ enum class RecipeOutcome { Recorded, Rebuild };
// pipeline stages. Built at the end of a recorded, cacheable, reusable, non-indirect draw and
// attached to the draw entry's matched stage variants; immutable.
struct DrawRecipe {
struct MultisampleAttachment {
std::weak_ptr<void> owner;
VkImage image;
};
VkDevice device = VK_NULL_HANDLE;
std::weak_ptr<Graphics::ShaderResources> templateRef;
Graphics::ResourceCache::Key key;
std::weak_ptr<Graphics::Pipeline> pipeline;
std::weak_ptr<Graphics::Framebuffer> framebuffer;
std::vector<std::weak_ptr<Graphics::StorageTexture>> targets;
std::vector<MultisampleAttachment> multisampleAttachments;
std::vector<VkImageView> targetViews;
std::uint64_t passKey = 0;
Graphics::VertexInputLayout vertexInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ std::vector<std::byte> ReadMultisampleTargetSamples_nid_no_patch(const Context&
MultisampleColorSnapshot ReadMultisampleTargetColorSnapshot_nid_no_patch(const Context& context, const ColorTarget& target);
VkImageView MultisampleTargetView(const Context& context, const ColorTarget& target, VkImage* image = nullptr);
VkImageView MultisampleTargetView(const Context& context, const ColorTarget& target, std::shared_ptr<void>& owner, VkImage* image = nullptr);
bool RepeatMultisampleTargetView(const Context& context, const ColorTarget& target, const std::shared_ptr<void>& owner, VkImage image, VkImageView view);
void PrepareMultisampleTargetForDraw_nid_no_patch(const Context& context, const ColorTarget& target);
void KeepMultisampleTarget_nid_no_patch(const std::shared_ptr<void>& owner);
void MaterializeMultisampleTargetClear(const Context& context, const ColorTarget& target);
Expand Down
47 changes: 40 additions & 7 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4806,14 +4806,19 @@ void recordPreparedDraw(const Context& context, const State& state, const Pm4::D
// only a template the resource cache serves under this content key (reusable: no lease,
// no copied writes, every direct region import- or mirror-served) of a direct draw, so a
// hit's proof is the template's ProveCurrent and nothing needs completion work.
if (recipeOut != nullptr && DrawRecipes() && cacheable && contentKey != nullptr && !outcome.waited && args == nullptr && resources->Reusable() && !resources->SpaceReuse() && !multisampled && !pipeline->ReordersSamples()) {
if (recipeOut != nullptr && DrawRecipes() && cacheable && contentKey != nullptr && !outcome.waited && args == nullptr && resources->Reusable() && !resources->SpaceReuse() && !pipeline->ReordersSamples() &&
(!multisampled || std::ranges::all_of(targets, &TargetBinding::multisampled))) {
auto recipe = std::make_shared<DrawRecipe>();
recipe->device = context.device;
recipe->templateRef = resources;
recipe->key = *contentKey;
recipe->pipeline = pipeline;
recipe->framebuffer = framebuffer;
for (const auto& owner : owners) recipe->targets.emplace_back(owner);
if (multisampled) {
for (const auto& binding : targets) recipe->multisampleAttachments.push_back({binding.multisampleOwner, binding.multisampleImage});
} else {
for (const auto& owner : owners) recipe->targets.emplace_back(owner);
}
recipe->targetViews = targetViews;
recipe->passKey = DrawRenderPassKey(context, state, targetViews);
recipe->vertexInput = inputs.VertexLayout();
Expand Down Expand Up @@ -5345,7 +5350,9 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con
if (recipe.hasDepth != state.depth.has_value()) return miss(DrawRecipeMiss::TargetGone);
if (!ZeroVertexLayoutMatches(recipe.vertexInput, shaders.front().program->vertexAttributes)) return miss(DrawRecipeMiss::Proof);
const std::size_t expectedViews = state.colors.size() + (recipe.hasDepth ? 1u : 0u);
Require(recipe.targets.size() == state.colors.size() && recipe.targetViews.size() == expectedViews, "draw recipe targets do not match the state");
const bool multisampled = !recipe.multisampleAttachments.empty();
const auto colorCount = multisampled ? recipe.multisampleAttachments.size() : recipe.targets.size();
Require(colorCount == state.colors.size() && recipe.targetViews.size() == expectedViews, "draw recipe targets do not match the state");
const auto plan = planDraw(context, state, draw, shaders, &recipe, outcome, timer);
DrawInputs inputs;
if (plan.nothing) inputs.nothing = true;
Expand All @@ -5364,20 +5371,37 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con
if (ownsTargetMemo(memo) && (memo.colors.valid || memo.depth.valid)) noteTargetMemo(TargetMemoEvent::RecipeInvalidated);
memo.Invalidate();
const bool passAttachments = PassAttachmentMemoEnabled();
const bool verifyAttachments = passAttachments && VerifyPassAttachments() && [&] {
std::vector<std::shared_ptr<void>> multisampleOwners;
std::vector<VkImage> multisampleImages;
if (multisampled) {
multisampleOwners.reserve(state.colors.size());
multisampleImages.reserve(state.colors.size());
}
const bool verifyAttachments = passAttachments && !multisampled && VerifyPassAttachments() && [&] {
InlineList<std::shared_ptr<StorageTexture>, 8> ignored;
return matchAttachmentMemo(context, state, ignored, recipe.targetViews);
}();
InlineList<std::shared_ptr<StorageTexture>, 8> targets;
const bool attachmentsFromMemo = passAttachments && !VerifyPassAttachments() && matchAttachmentMemo(context, state, targets, recipe.targetViews);
const bool attachmentsFromMemo = passAttachments && !multisampled && !VerifyPassAttachments() && matchAttachmentMemo(context, state, targets, recipe.targetViews);
if (attachmentsFromMemo) {
noteAttachments(AttachmentEvent::Hit);
for (std::size_t index = 0; index < state.colors.size(); ++index) {
if (state.colors[index].fmaskAddress != 0) GuardOneFragmentFmask(context, state.colors[index]);
}
} else {
for (std::size_t index = 0; index < recipe.targets.size(); ++index) {
for (std::size_t index = 0; index < colorCount; ++index) {
timer.phase(PhaseSetup);
if (multisampled) {
const auto& color = state.colors[index];
const auto& attachment = recipe.multisampleAttachments[index];
auto owner = attachment.owner.lock();
if (!RepeatMultisampleTargetView(context, color, owner, attachment.image, recipe.targetViews[index])) return miss(DrawRecipeMiss::TargetGone);
multisampleOwners.push_back(std::move(owner));
multisampleImages.push_back(attachment.image);
targets.EmplaceBack(nullptr);
timer.phase(PhaseReadTarget);
continue;
}
if (state.colors[index].fmaskAddress != 0) GuardOneFragmentFmask(context, state.colors[index]);
auto stored = recipe.targets[index].lock();
if (stored == nullptr || !StorageImageCached(context, stored.get()) || !StorageImageServesKeys(*stored, SurfaceForTarget(state.colors[index]).dccAddress)) return miss(DrawRecipeMiss::TargetGone);
Expand Down Expand Up @@ -5421,6 +5445,13 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con
}
const auto moved = resources->MovedReadOnlyBuffers(shaders, *recorder, &state.color);
if (!moved.has_value()) return miss(DrawRecipeMiss::Buffers);
if (multisampled) {
const auto& sources = resources->MultisampleSources();
for (const auto& color : state.colors) {
if (std::ranges::find(sources, color.surfaceAddress) != sources.end()) return miss(DrawRecipeMiss::Proof);
}
if (state.depth) RequireNoNativeDepthFeedback_nid_no_patch(context, *state.depth, resources->SampledTextures());
}
CheckBufferAliases(shaders, state.color, draw.indexAddress, inputs.indexBytes);
if (recipe.hasDepth && writesDepthTarget(state)) NoteDepthSurfaceWrite(context, *state.depth);
SharedResourceCache().Touch(recipe.key);
Expand All @@ -5437,7 +5468,7 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con
auto framebuffer = recipe.framebuffer.lock();
if (framebuffer == nullptr) {
const auto stored = targets.Span();
const std::vector<std::shared_ptr<void>> framebufferOwners(stored.begin(), stored.end());
const auto framebufferOwners = multisampled ? multisampleOwners : std::vector<std::shared_ptr<void>>(stored.begin(), stored.end());
framebuffer = pipeline->AcquireFramebuffer(recipe.targetViews, framebufferOwners, state.renderExtent, state.renderLayers);
}
timer.phase(PhasePipeline);
Expand All @@ -5449,6 +5480,8 @@ DrawRecipeOutcome DrawWithRecipe(const Context& context, const State& state, con
record.moved = *moved;
record.targetViews = recipe.targetViews;
record.targets = targets.Span();
record.multisampleTargets = std::move(multisampleOwners);
record.multisampleImages = std::move(multisampleImages);
InlineList<std::pair<std::uint32_t, std::uint32_t>, 8> slices;
for (const auto& color : state.colors) slices.EmplaceBack(color.arraySlice, color.arrayLastSlice != 0 ? color.layers : 1u);
record.targetSlices = slices.Span();
Expand Down
29 changes: 24 additions & 5 deletions core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1552,16 +1552,18 @@ void requireNoTextureHost(const Context& context, const ColorTarget& target) {
}
}

MultisampleTarget& acquire(const Context& context, const ColorTarget& requested, std::optional<VkClearColorValue> initialClear = std::nullopt, std::shared_ptr<Buffer> initialUpload = {}, std::vector<std::byte> initialBaseline = {}) {
MultisampleTarget* acquireExisting(const Context& context, const ColorTarget& requested, const MultisampleTarget* expected = nullptr, VkImage expectedImage = VK_NULL_HANDLE) {
discardRetiredTargets();
for (const auto& target : targets()) {
if (!target->active->load(std::memory_order_acquire)) continue;
if (target->context.device != context.device || !target->Serves(requested)) continue;
auto use = requested;
use.format = target->target.format;
const bool takesRegisterState = target->TakesRegisterState(use);
if (takesRegisterState) target->CheckTextureImport();
if (takesRegisterState || recreated(target->target, use)) {
const bool recreate = takesRegisterState || recreated(target->target, use);
if (expected != nullptr && (target.get() != expected || target->image != expectedImage || recreate)) return nullptr;
if (recreate) {
if (takesRegisterState) target->CheckTextureImport();
{
std::unique_lock lock(guardedMutex());
if (!target->active->load(std::memory_order_acquire)) continue;
Expand All @@ -1572,7 +1574,7 @@ MultisampleTarget& acquire(const Context& context, const ColorTarget& requested,
target->use->hostOnly.store(true, std::memory_order_release);
guard(context, use, target->active, target->metadataValid, target->use, target);
target->ApplyFastClear(use);
return *target;
return target.get();
}
{
std::unique_lock lock(guardedMutex());
Expand All @@ -1598,8 +1600,13 @@ MultisampleTarget& acquire(const Context& context, const ColorTarget& requested,
target->ApplyFastClear(use);
collectFmaskWrites(target->target);
stampUse(*target->use, target->target.address, guardedColorBytes(target->target));
return *target;
return target.get();
}
return nullptr;
}

MultisampleTarget& acquire(const Context& context, const ColorTarget& requested, std::optional<VkClearColorValue> initialClear = std::nullopt, std::shared_ptr<Buffer> initialUpload = {}, std::vector<std::byte> initialBaseline = {}) {
if (auto* existing = acquireExisting(context, requested)) return *existing;
const auto& use = requested;
requireNoTextureHost(context, use);
auto target = std::make_shared<MultisampleTarget>(context, use, false, initialClear, std::move(initialUpload), std::move(initialBaseline));
Expand Down Expand Up @@ -2054,6 +2061,18 @@ VkImageView MultisampleTargetView(const Context& context, const ColorTarget& tar
return acquired.View(target.format);
}

bool RepeatMultisampleTargetView(const Context& context, const ColorTarget& target, const std::shared_ptr<void>& owner, VkImage image, VkImageView view) {
if (owner == nullptr || target.samples <= 1 || target.staged) return false;
storeEarlierStorageResults(target);
const DccPublication publication(target);
TargetsLock lock;
auto* existing = acquireExisting(context, target, static_cast<const MultisampleTarget*>(owner.get()), image);
if (existing == nullptr || existing->View(target.format) != view) return false;
existing->drawn = true;
++existing->contents;
return true;
}

void KeepMultisampleTarget_nid_no_patch(const std::shared_ptr<void>& owner) {
std::static_pointer_cast<MultisampleTarget>(owner)->Keep();
}
Expand Down
Loading
Loading