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
76 changes: 73 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ std::atomic<bool>& anyDepthGuard() {

struct DepthSamplingState {
std::vector<GuestMemory::UnchangedQuery> initial;
std::optional<float> initialDepth;
std::optional<std::uint8_t> initialStencil;
std::string refusal;
std::optional<std::uint8_t> aliasStencil;
std::optional<MultisampleColorAlias> unprovenStencil;
Expand Down Expand Up @@ -153,13 +155,76 @@ std::uint64_t fillNoteAfter(const Recorder& recorder, const GuestMemory::Unchang
return found;
}

bool uniformPlaneTarget(const DepthTarget& target) {
return target.format == VK_FORMAT_D32_SFLOAT_S8_UINT && target.stencilAddress != 0u && target.htileAddress == 0u;
}

template <typename Element>
std::optional<Element> uniformPlaneValue(std::uint64_t address, std::size_t bytes) {
std::array<Element, 16384> chunk{};
std::optional<Element> value;
for (std::size_t offset = 0u; offset < bytes;) {
const auto count = std::min(chunk.size(), (bytes - offset) / sizeof(Element));
const auto elements = std::span(chunk).first(count);
GuestMemory::Read(address + offset, std::as_writable_bytes(elements));
if (!value) value = elements.front();
if (std::ranges::any_of(elements, [&](Element element) { return element != *value; })) return std::nullopt;
offset += count * sizeof(Element);
}
return value;
}

DepthSamplingState prepareUniformPlaneSampling(const Context& context, const DepthTarget& target, DepthSamplingState state) {
const auto refuse = [&](const char* reason) {
state.refusal = reason;
return state;
};
if (target.depthSwizzle != 24u || target.firstSlice != 0u || target.lastSlice != 0u) return refuse("native multisample depth arrays or non-Z_X layout are not implemented");
if ((target.pipeBankXor & ~0xcf00u) != 0u) return refuse("unsupported native multisample depth XOR");
if (target.address == 0u || (target.address & 65535u) != 0u || (target.stencilAddress & 65535u) != 0u) return refuse("native multisample depth base is not aligned");
if (target.htileStencil) return refuse("native multisample stencil import is not implemented");
state.initial = {{target.address, static_cast<std::size_t>(MultisampledDepthBytes_nid_no_patch(target.extent, 4u, target.samples)), 0u}, {target.stencilAddress, static_cast<std::size_t>(MultisampledDepthBytes_nid_no_patch(target.extent, 1u, target.samples)), 0u}};
for (const auto& range : state.initial) {
if (range.address > std::numeric_limits<std::uint64_t>::max() - range.bytes) return refuse("native depth import range overflows");
if (!GuestMemory::Accessible(reinterpret_cast<const void*>(range.address), range.bytes) || !GuestMemory::Watched(range.address, range.bytes)) return refuse("native multisample depth import requires watched accessible guest memory");
}
const auto& depth = state.initial[0];
const auto& stencil = state.initial[1];
if (depth.address + depth.bytes > stencil.address && stencil.address + stencil.bytes > depth.address) return refuse("native depth and stencil initial ranges overlap");
for (std::size_t index = 0u; index < state.initial.size(); ++index) {
const auto alias = MultisampleColorTargetOver(state.initial[index].address, state.initial[index].bytes);
if (!alias) continue;
reportAlias(*alias, state.initial[index], index == 1u ? "stencil plane" : "depth");
return refuse(index == 1u ? stencilAliasRefusal : depthAliasRefusal);
}
for (auto& range : state.initial) {
if (auto* recorder = RecorderFor(context)) recorder->SyncThrough(range.address, range.bytes);
StorageTexture::FlushPending(range.address, range.bytes);
GuestMemory::FlushGpuWrites(range.address, range.bytes);
if (!GuestMemory::Accessible(reinterpret_cast<const void*>(range.address), range.bytes) || !GuestMemory::Watched(range.address, range.bytes)) return refuse("native multisample depth import requires watched accessible guest memory");
range.generation = GuestMemory::CollectWritesUncached(range.address, range.bytes);
if (range.generation == 0u) return refuse("native multisample depth import requires tracked generations");
}
const auto depthWord = uniformPlaneValue<std::uint32_t>(depth.address, depth.bytes);
const auto depthValue = depthWord ? std::bit_cast<float>(*depthWord) : 0.0f;
if (!depthWord || !(depthValue >= 0.0f && depthValue <= 1.0f)) return refuse("native multisample depth plane without HTILE does not hold one depth value in [0, 1]");
const auto stencilByte = uniformPlaneValue<std::uint8_t>(stencil.address, stencil.bytes);
if (!stencilByte) return refuse("native multisample stencil plane without HTILE does not hold one stencil value");
for (const auto& range : state.initial) GuestMemory::CollectWritesUncached(range.address, range.bytes);
if (!GuestMemory::UnchangedSinceAll(state.initial)) return refuse("guest writes raced native multisample depth import");
state.initialDepth = depthValue;
state.initialStencil = *stencilByte;
return state;
}

DepthSamplingState prepareDepthSampling(const Context& context, const DepthTarget& target) {
DepthSamplingState state;
const auto refuse = [&](const char* reason) {
state.refusal = reason;
return state;
};
if (target.samples != 2u && target.samples != 4u && target.samples != 8u) return refuse("unsupported native depth sample count");
if (uniformPlaneTarget(target)) return prepareUniformPlaneSampling(context, target, std::move(state));
const bool stencil = target.format == VK_FORMAT_D32_SFLOAT_S8_UINT;
const bool stencilPlane = stencil && !target.htileStencil && target.htileDepthOnly;
if (target.format != VK_FORMAT_D16_UNORM && target.format != VK_FORMAT_D32_SFLOAT && !stencil) return refuse("native multisample stencil or depth format import is not implemented");
Expand Down Expand Up @@ -411,7 +476,7 @@ class DepthSurface {
toGeneral.subresourceRange = viewInfo.subresourceRange;
const auto barrier = context.Resolved(&DeviceFunctions::cmdPipelineBarrier, "vkCmdPipelineBarrier");
barrier(commands, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &toGeneral);
const VkClearDepthStencilValue clear{target.clearDepth, this->sampling.aliasStencil.value_or(target.clearStencil)};
const VkClearDepthStencilValue clear{this->sampling.initialDepth.value_or(target.clearDepth), this->sampling.initialStencil.value_or(this->sampling.aliasStencil.value_or(target.clearStencil))};
context.Function<PFN_vkCmdClearDepthStencilImage>("vkCmdClearDepthStencilImage")(commands, image, VK_IMAGE_LAYOUT_GENERAL, &clear, 1, &toGeneral.subresourceRange);
RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
if (batch) batch->SubmitAndWait();
Expand Down Expand Up @@ -450,8 +515,9 @@ class DepthSurface {
if (!alias || (index == 2u && sampling.unprovenStencil && stencilClearGeneration == 0u)) continue;
const auto value = index == 2u && separatePlane ? aliasedStencil(*alias, range, target.samples) : std::nullopt;
const bool kept = value.has_value() && (stencilClearGeneration != 0u ? *value == *stencilClearValue : value == sampling.aliasStencil);
if (!kept) reportAlias(*alias, range, index == 2u ? "stencil plane" : index == 1u ? "HTILE" : "depth");
Require(kept, index == 2u ? stencilAliasRefusal : depthAliasRefusal);
const bool stencilRange = index == 2u || (index == 1u && uniformPlaneTarget(target));
if (!kept) reportAlias(*alias, range, stencilRange ? "stencil plane" : index == 1u ? "HTILE" : "depth");
Require(kept, stencilRange ? stencilAliasRefusal : depthAliasRefusal);
planeAliased = true;
}
if (stencilClearGeneration != 0u) {
Expand Down Expand Up @@ -508,6 +574,10 @@ class DepthSurface {
const bool identity = stencil ? resource.format == 5u : resource.baseAddress == target.address && resource.format == (target.format == VK_FORMAT_D16_UNORM ? 7u : 22u);
Require(identity && resource.width == target.extent.width && resource.height == target.extent.height && resource.samples == target.samples, "native multisample depth sampled identity or format mismatch");
Require(resource.dimension == TextureDimension::k2DMsaa && resource.tileMode == TextureTileMode::kZ64KBX && resource.depthOrLastArray == 0u && resource.baseArray == 0u && resource.baseLevel == 0u && resource.lastLevel == 0u && resource.mipCount == 1u && resource.allocatedMipCount == 1u && resource.minLod == 0u, "native multisample depth sampled array, mip or layout is not implemented");
if (uniformPlaneTarget(target)) {
Require(resource.pipeBankXor == target.pipeBankXor && resource.dccAddress == 0u && !resource.dccAlphaOnMsb, "native multisample depth sampled metadata or XOR mismatch");
return;
}
const bool htile = resource.dccAddress == target.htileAddress && resource.dccPipeAligned == target.htilePipeAligned;
const bool bare = resource.dccAddress == 0u && !resource.dccPipeAligned;
if (stencil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ if(BUILD_TESTING)
add_test(NAME agc_driver_native_depth_expand_gate COMMAND agc_driver_native_multisample_depth_tests --expand-gate)
add_test(NAME agc_driver_native_depth_stencil_view COMMAND agc_driver_native_multisample_depth_tests --stencil-view)
add_test(NAME agc_driver_native_depth_buffer_clear COMMAND agc_driver_native_multisample_depth_tests --buffer-clear)
set_tests_properties(agc_driver_native_depth_clears agc_driver_native_depth_clear_refusals agc_driver_native_depth_stencil agc_driver_native_depth_separate_stencil agc_driver_native_depth_aliased_stencil agc_driver_native_depth_expand_gate agc_driver_native_depth_stencil_view agc_driver_native_depth_buffer_clear PROPERTIES TIMEOUT 30 ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
add_test(NAME agc_driver_native_depth_uniform_planes COMMAND agc_driver_native_multisample_depth_tests --uniform-planes)
set_tests_properties(agc_driver_native_depth_clears agc_driver_native_depth_clear_refusals agc_driver_native_depth_stencil agc_driver_native_depth_separate_stencil agc_driver_native_depth_aliased_stencil agc_driver_native_depth_expand_gate agc_driver_native_depth_stencil_view agc_driver_native_depth_buffer_clear agc_driver_native_depth_uniform_planes PROPERTIES TIMEOUT 30 ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
if(WIN32)
set_tests_properties(agc_driver_native_depth_retirement PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
set_tests_properties(agc_driver_native_depth_clears agc_driver_native_depth_clear_refusals agc_driver_native_depth_stencil agc_driver_native_depth_separate_stencil agc_driver_native_depth_aliased_stencil agc_driver_native_depth_expand_gate agc_driver_native_depth_stencil_view agc_driver_native_depth_buffer_clear PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
set_tests_properties(agc_driver_native_depth_clears agc_driver_native_depth_clear_refusals agc_driver_native_depth_stencil agc_driver_native_depth_separate_stencil agc_driver_native_depth_aliased_stencil agc_driver_native_depth_expand_gate agc_driver_native_depth_stencil_view agc_driver_native_depth_buffer_clear agc_driver_native_depth_uniform_planes PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test(NAME agc_driver_native_depth_driver_path COMMAND agc_driver_native_multisample_depth_tests --driver-path)
add_test(NAME agc_driver_native_depth_driver_queued_fill COMMAND agc_driver_native_multisample_depth_tests --driver-queued-fill)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,97 @@ int CheckDriverPath(bool queued) {

}

void CheckUniformPlanesWithoutHtile(Context context, std::uint32_t samples) {
constexpr VkExtent2D extent{129u, 67u};
const auto depthBytes = static_cast<std::size_t>(MultisampledDepthBytes_nid_no_patch(extent, 4u, samples));
const auto stencilBytes = static_cast<std::size_t>(MultisampledDepthBytes_nid_no_patch(extent, 1u, samples));
TargetMemory memory(depthBytes), stencil(stencilBytes);
const auto fill = [&](float depth, std::uint8_t value) {
const auto word = std::bit_cast<std::uint32_t>(depth);
for (std::size_t offset = 0u; offset < depthBytes; offset += 4u) std::memcpy(memory.Data() + offset, &word, 4u);
std::memset(stencil.Data(), value, stencilBytes);
};
DepthTarget target{memory.Address(), stencil.Address(), extent, VK_FORMAT_D32_SFLOAT_S8_UINT, 1.0f, 0u, 0u, false, samples};
target.depthSwizzle = 24u;
Recorder recorder(context);
recorder.Activate();
auto words = DepthSampleWords(target);
words[6] &= ~((1u << 19u) | (1u << 21u));
auto stencilTarget = target;
stencilTarget.address = target.stencilAddress;
auto stencilWords = DepthSampleWords(stencilTarget);
stencilWords[1] = (stencilWords[1] & ~(0x1ffu << 20u)) | (5u << 20u);
stencilWords[6] &= ~((1u << 19u) | (1u << 21u));
const auto lookup = [&] { return DepthSurfaceTexture(context, words, DecodeTextureResource(words), VkComponentMapping{}); };
const auto clear = [&] {
recorder.Sync();
ClearCachedTextures(context.device);
ClearDepthSurfaces(context.device);
};
struct Cleanup {
decltype(clear)& action;
~Cleanup() { action(); }
} cleanup{clear};
for (std::uint32_t field = 0u; field < 5u; ++field) {
fill(0.25f, 0x5au);
auto invalid = target;
std::string_view reason;
switch (field) {
case 0u: memory.Data()[depthBytes - 1u] = std::byte{0x3f}; reason = "does not hold one depth value"; break;
case 1u: fill(2.0f, 0x5au); reason = "does not hold one depth value"; break;
case 2u: stencil.Data()[stencilBytes - 1u] = std::byte{0x5b}; reason = "does not hold one stencil value"; break;
case 3u: invalid.htileStencil = true; reason = "stencil import"; break;
case 4u: invalid.stencilAddress = memory.Address() + 65536u; reason = "overlap"; break;
}
Require(DepthSurfaceView(context, invalid) != VK_NULL_HANDLE, "a refused uniform-plane import must preserve attachment rendering");
TargetRefuses([&] { DepthSurfaceTexture(context, words, DecodeTextureResource(words), VkComponentMapping{}); }, reason);
clear();
}
for (const auto& [depth, value] : std::array{std::pair{0.0f, std::uint8_t{0xc3u}}, std::pair{0.25f, std::uint8_t{0x5au}}}) {
fill(depth, value);
Require(DepthSurfaceView(context, target) != VK_NULL_HANDLE, "a uniform-plane D32S8 target without HTILE has no attachment view");
auto texture = lookup();
Require(texture != nullptr && texture->View() != VK_NULL_HANDLE, "a uniform-plane D32S8 target without HTILE was not sampled natively");
Buffer output(context, (extent.width * extent.height * samples + 2u) * 16u, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
std::memset(output.Bytes().data(), 0xa5, output.Bytes().size());
{
const Fetch fetch(context, *texture, output, FetchShader(extent.width, extent.height, samples, false, true));
CommandBatch batch(context);
fetch.Run(batch.Handle(), extent.width * extent.height * samples);
RecordMemoryBarrier(context, batch.Handle(), VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT);
batch.SubmitAndWait();
output.Invalidate();
for (std::uint32_t i = 0u; i < extent.width * extent.height * samples; ++i) {
std::array<float, 4> sampled{};
std::memcpy(sampled.data(), output.Bytes().data() + (i + 1u) * 16u, 16u);
Require(sampled == std::array<float, 4>{depth, 0.0f, 0.0f, 1.0f}, "a uniform depth plane without HTILE was not imported into every sample");
}
}
auto stencilTexture = DepthSurfaceTexture(context, stencilWords, DecodeTextureResource(stencilWords), VkComponentMapping{});
Require(stencilTexture != nullptr && stencilTexture->View() != VK_NULL_HANDLE, "the stencil plane of a uniform-plane D32S8 target without HTILE was not sampled natively");
{
std::memset(output.Bytes().data(), 0xa5, output.Bytes().size());
const Fetch fetch(context, *stencilTexture, output, FetchShader(extent.width, extent.height, samples, false, false));
CommandBatch batch(context);
fetch.Run(batch.Handle(), extent.width * extent.height * samples);
RecordMemoryBarrier(context, batch.Handle(), VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT);
batch.SubmitAndWait();
output.Invalidate();
for (std::uint32_t i = 0u; i < extent.width * extent.height * samples; ++i) {
std::array<std::uint32_t, 4> sampled{};
std::memcpy(sampled.data(), output.Bytes().data() + (i + 1u) * 16u, 16u);
Require(sampled[0] == value, "a uniform stencil plane without HTILE was not imported into every sample: sample " + std::to_string(i) + " holds " + std::to_string(sampled[0]) + ", the plane " + std::to_string(value));
}
}
stencilTexture.reset();
texture.reset();
stencil.Data()[0] = std::byte{static_cast<std::uint8_t>(value + 1u)};
TargetRefuses([&] { lookup(); }, "guest writes");
clear();
}
std::printf("Native D32S8 without HTILE, samples=%u: uniform planes imported and sampled, other planes refused\n", samples);
}

int main(int argc, char** argv) {
try {
#ifdef __linux__
Expand Down Expand Up @@ -2616,6 +2707,16 @@ int main(int argc, char** argv) {
}
return 0;
}
if (argc == 2 && std::string_view(argv[1]) == "--uniform-planes") {
std::uint32_t checked = 0u, unavailable = 0u;
for (const auto samples : {2u, 4u, 8u}) {
if (!DepthSupported(context, HostDepthFormat(context, VK_FORMAT_D32_SFLOAT_S8_UINT), samples)) { ++unavailable; continue; }
CheckUniformPlanesWithoutHtile(context, samples);
++checked;
}
std::printf("Native depth uniform-plane cases checked=%u unavailable=%u\n", checked, unavailable);
return checked != 0u ? 0 : VulkanTestSkipped;
}
if (argc == 2 && std::string_view(argv[1]) == "--clears") {
CheckClearDispatch();
std::uint32_t checked = 0u, unavailable = 0u;
Expand Down
Loading
Loading