From b3ac4aec3a69e14473701ee51fa117e6b7d59325 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 00:35:32 +0100 Subject: [PATCH] feat: forward ResolveSubresource from the title command list Slot 19 validates both textures (single-sample destination, multisampled source, matching size, subresource range) and moves them to the resolve states before the desktop call. Part of #870. --- CMakeLists.txt | 5 +++ src/startup_stubs/d3d12_xs_device.cpp | 36 +++++++++++++++ src/startup_stubs/resolve_subresource.h | 60 +++++++++++++++++++++++++ tests/startup_resolve_subresource.cpp | 51 +++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 src/startup_stubs/resolve_subresource.h create mode 100644 tests/startup_resolve_subresource.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c79bb5045..bf51ad61b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -659,6 +659,11 @@ endforeach() add_executable(startup_command_list_slot_names tests/startup_command_list_slot_names.cpp) target_compile_features(startup_command_list_slot_names PRIVATE cxx_std_17) add_test(NAME startup_command_list_slot_names COMMAND startup_command_list_slot_names) + +# Rules that gate a title's ResolveSubresource (src/startup_stubs/resolve_subresource.h, #870). +add_executable(startup_resolve_subresource tests/startup_resolve_subresource.cpp) +target_compile_features(startup_resolve_subresource PRIVATE cxx_std_17) +add_test(NAME startup_resolve_subresource COMMAND startup_resolve_subresource) # Game-save stand-in storage (src/startup_stubs/save_store.h): a blob written by one process is read by a second. add_executable(startup_gamesave_persist tests/startup_gamesave_persist.cpp) target_compile_features(startup_gamesave_persist PRIVATE cxx_std_17) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 5ce28b6f8..9cb141f6d 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -38,6 +38,7 @@ #include "console_plane_size.h" #include "descriptor_heap_type.h" #include "null_binding_cause.h" +#include "resolve_subresource.h" #include "null_texture_view.h" #include "packed_target_format.h" #include "present_view_format.h" @@ -2297,6 +2298,40 @@ void __stdcall list_stencil_ref(Wrapper* self, UINT reference) { if (auto* list = recording(self)) list->OMSetStencilRef(reference); } +// List slot 19, ResolveSubresource(dest, dest subresource, source, source subresource, format). Both +// textures use COMMON state tracking; the list moves them to RESOLVE_DEST / RESOLVE_SOURCE for the call. +void __stdcall list_resolve_subresource(Wrapper* self, void* destination, UINT destination_subresource, void* source, + UINT source_subresource, UINT format) +{ + const auto refuse = [](const char* reason) { + static LONG refusals; + if (first_calls(refusals, 16)) log("x2w-render ResolveSubresource refused: %s", reason); + }; + auto* list = recording(self); + if (!list) return refuse("list is closed"); + Wrapper* to = as_wrapper(destination); + Wrapper* from = as_wrapper(source); + if (!to || !from || to->own().kind != resource_kind || from->own().kind != resource_kind || !to->own().real || + !from->own().real || to == from) + return refuse("missing desktop resource"); + const auto* to_info = resource_info(to); + const auto* from_info = resource_info(from); + if (!to_info || !from_info || to_info->initial_state != D3D12_RESOURCE_STATE_COMMON || + from_info->initial_state != D3D12_RESOURCE_STATE_COMMON) + return refuse("resource does not use COMMON state tracking"); + auto* target = static_cast(to->own().real); + auto* origin = static_cast(from->own().real); + const auto side = [](ID3D12Resource* resource) { + const D3D12_RESOURCE_DESC desc = resource->GetDesc(); + return x2w_resolve::Side{desc.Width, desc.Height, desc.MipLevels, desc.DepthOrArraySize, desc.SampleDesc.Count, + desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D}; + }; + const auto verdict = x2w_resolve::check(side(target), destination_subresource, side(origin), source_subresource, format); + if (verdict != x2w_resolve::Verdict::ok) return refuse("arguments do not describe a valid resolve"); + require_state(self, target, D3D12_RESOURCE_STATE_RESOLVE_DEST); + require_state(self, origin, D3D12_RESOURCE_STATE_RESOLVE_SOURCE); + list->ResolveSubresource(target, destination_subresource, origin, source_subresource, static_cast(format)); +} // List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address). void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address) { @@ -3755,6 +3790,7 @@ void build_tables() list_typed[52] = reinterpret_cast(&list_clear_target); list_typed[139] = reinterpret_cast(&list_reset); list_typed[12] = reinterpret_cast(&list_draw); + list_typed[19] = reinterpret_cast(&list_resolve_subresource); list_typed[20] = reinterpret_cast(&list_topology); list_typed[21] = reinterpret_cast(&list_viewports); list_typed[22] = reinterpret_cast(&list_scissors); diff --git a/src/startup_stubs/resolve_subresource.h b/src/startup_stubs/resolve_subresource.h new file mode 100644 index 000000000..8a83490cd --- /dev/null +++ b/src/startup_stubs/resolve_subresource.h @@ -0,0 +1,60 @@ +// Experimental (docs/startup-runtime.md, #870): the checks before a title's ResolveSubresource (command +// list slot 19, desktop order) is forwarded to the desktop list. Plain standard C++ with no Windows types, +// so the rules can be compiled and tested on their own. +// +// A resolve copies one subresource of a multisampled texture into one subresource of a single-sample +// texture of the same size. The desktop runtime treats any other call as a device-removing error, so a +// call that does not fit is refused here and logged instead of reaching the driver. +#pragma once + +#include + +namespace x2w_resolve { + +struct Side { + std::uint64_t width; + std::uint32_t height; + std::uint32_t mip_levels; + std::uint32_t array_size; + std::uint32_t sample_count; + bool is_texture2d; +}; + +enum class Verdict { + ok, + not_texture, + destination_multisampled, + source_not_multisampled, + size_mismatch, + subresource_out_of_range, + format_unknown, +}; + +inline std::uint64_t subresource_count(const Side& s) { return std::uint64_t{s.mip_levels} * s.array_size; } + +// The size of a subresource at `mip`: each mip halves the base size and stops at 1. +inline std::uint64_t mip_extent(std::uint64_t base, std::uint32_t mip) +{ + const std::uint64_t value = mip >= 63 ? 0 : base >> mip; + return value ? value : 1; +} + +inline Verdict check(const Side& destination, std::uint32_t destination_subresource, const Side& source, + std::uint32_t source_subresource, std::uint32_t format) +{ + if (!destination.is_texture2d || !source.is_texture2d) return Verdict::not_texture; + if (destination.sample_count != 1) return Verdict::destination_multisampled; + if (source.sample_count <= 1) return Verdict::source_not_multisampled; + if (!format) return Verdict::format_unknown; + if (!destination.mip_levels || !destination.array_size || !source.mip_levels || !source.array_size || + destination_subresource >= subresource_count(destination) || source_subresource >= subresource_count(source)) + return Verdict::subresource_out_of_range; + const std::uint32_t destination_mip = destination_subresource % destination.mip_levels; + const std::uint32_t source_mip = source_subresource % source.mip_levels; + if (mip_extent(destination.width, destination_mip) != mip_extent(source.width, source_mip) || + mip_extent(destination.height, destination_mip) != mip_extent(source.height, source_mip)) + return Verdict::size_mismatch; + return Verdict::ok; +} + +} // namespace x2w_resolve diff --git a/tests/startup_resolve_subresource.cpp b/tests/startup_resolve_subresource.cpp new file mode 100644 index 000000000..9981463b9 --- /dev/null +++ b/tests/startup_resolve_subresource.cpp @@ -0,0 +1,51 @@ +// Checks the rules that gate a title's ResolveSubresource (src/startup_stubs/resolve_subresource.h, #870). +#include "../src/startup_stubs/resolve_subresource.h" + +#include + +using namespace x2w_resolve; + +namespace { +int failures; +void expect(bool ok, const char* what) +{ + if (!ok) { + std::printf("FAIL: %s\n", what); + ++failures; + } +} +constexpr std::uint32_t rgba8 = 28; +} // namespace + +int main() +{ + const Side single{1280, 720, 1, 1, 1, true}; + const Side msaa{1280, 720, 1, 1, 4, true}; + + expect(check(single, 0, msaa, 0, rgba8) == Verdict::ok, "a 4x target resolves into a single-sample one"); + expect(check(msaa, 0, msaa, 0, rgba8) == Verdict::destination_multisampled, "destination must be single-sample"); + expect(check(single, 0, single, 0, rgba8) == Verdict::source_not_multisampled, "source must be multisampled"); + expect(check(single, 0, msaa, 0, 0) == Verdict::format_unknown, "format must be named"); + + Side buffer = single; + buffer.is_texture2d = false; + expect(check(buffer, 0, msaa, 0, rgba8) == Verdict::not_texture, "buffers are refused"); + + const Side smaller{640, 360, 1, 1, 1, true}; + expect(check(smaller, 0, msaa, 0, rgba8) == Verdict::size_mismatch, "sizes must match"); + + const Side mips{1280, 720, 3, 2, 1, true}; + expect(check(mips, 5, msaa, 0, rgba8) == Verdict::size_mismatch, "mip 2 of the destination is 320x180"); + expect(check(mips, 6, msaa, 0, rgba8) == Verdict::subresource_out_of_range, "subresource 6 of 6 is out of range"); + expect(check(mips, 3, msaa, 0, rgba8) == Verdict::ok, "array slice 1, mip 0 matches the source"); + expect(check(single, 0, msaa, 1, rgba8) == Verdict::subresource_out_of_range, "source subresource 1 of 1"); + + const Side tiny{1, 1, 1, 1, 1, true}; + const Side tiny_msaa{1, 1, 1, 1, 2, true}; + expect(check(tiny, 0, tiny_msaa, 0, rgba8) == Verdict::ok, "1x1 resolves"); + const Side deep{1, 1, 70, 1, 1, true}; + expect(check(deep, 69, tiny_msaa, 0, rgba8) == Verdict::ok, "a mip past the size floor stays 1x1"); + + if (!failures) std::printf("startup_resolve_subresource: ok\n"); + return failures ? 1 : 0; +}