Skip to content
Closed
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Document the new resolve capability and reproduction command

This adds an externally usable startup operation and a new CTest command, but a repository-wide search for ResolveSubresource and resolve_subresource finds no corresponding README or experiment-documentation entry. AGENTS.md requires updating the relevant README row or docs/experiments.md when a public capability or reproduction command changes. Add the supported resource constraints, the fixture's reproduction command and result, and the distinction between the tested argument predicates and the still-unverified Windows forwarding path.

AGENTS.md reference: AGENTS.md:L211-L212

Useful? React with 👍 / 👎.

# 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)
Expand Down
36 changes: 36 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ID3D12Resource*>(to->own().real);
auto* origin = static_cast<ID3D12Resource*>(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<DXGI_FORMAT>(format));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve resolved data when uploading sampled textures

For a resolve destination with Flags == D3D12_RESOURCE_FLAG_NONE and readable title-memory backing, such as a texture created through slot 133, this updates only the desktop texture. The next translated SRV bind unconditionally calls upload_texture (src/startup_stubs/d3d12_draw.inl:999), which excludes only render-target, depth-stencil and UAV resources. On its first bind, it therefore uploads the untouched CPU backing over the resolved pixels. A valid resolve-then-sample sequence reads stale CPU data instead of the resolve result. Record the destination's GPU-written contents and make the upload path preserve them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Normalize console formats before submitting the resolve

When the caller supplies console colour format 116 or 117, sanitized() creates both desktop resources as DXGI_FORMAT_R10G10B10A2_UNORM through desktop_format(), but this call forwards the original console value. Both values pass x2w_resolve::check because its format check rejects only zero. Consequently, successfully created stand-ins receive a resolve with an unsupported desktop format that also differs from their actual format, potentially invalidating the command list or removing the device. Apply the existing desktop-format conversion before validating and forwarding the call.

Useful? React with 👍 / 👎.

Comment on lines +2331 to +2333

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore attachment states before subsequent draws

If the single-sample destination is already bound as a render target, resolving into it and then drawing again without rebinding leaves it in RESOLVE_DEST. list_targets() establishes render-target state only at bind time, while list_draw() and draw_translated() do not re-establish it; the title's barrier calls are still no-ops. Since targets_bound remains true, the next draw is recorded against an attachment in the wrong state, producing validation errors and possible device removal. Restore the previous tracked states after resolving, or ensure subsequent draws transition their bound attachments back to the required states.

Useful? React with 👍 / 👎.

}
// List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address).
void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address)
{
Expand Down Expand Up @@ -3755,6 +3790,7 @@ void build_tables()
list_typed[52] = reinterpret_cast<ListTyped>(&list_clear_target);
list_typed[139] = reinterpret_cast<ListTyped>(&list_reset);
list_typed[12] = reinterpret_cast<ListTyped>(&list_draw);
list_typed[19] = reinterpret_cast<ListTyped>(&list_resolve_subresource);
list_typed[20] = reinterpret_cast<ListTyped>(&list_topology);
list_typed[21] = reinterpret_cast<ListTyped>(&list_viewports);
list_typed[22] = reinterpret_cast<ListTyped>(&list_scissors);
Expand Down
60 changes: 60 additions & 0 deletions src/startup_stubs/resolve_subresource.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

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
51 changes: 51 additions & 0 deletions tests/startup_resolve_subresource.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

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;
}
Loading