From ffb70f6ad28f30deb704eb219bdad040c4cb243f Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 17:31:05 +0100 Subject: [PATCH 1/6] fix: default viewport and scissor for translated draws --- CMakeLists.txt | 6 ++- docs/startup-runtime.md | 13 +++++ src/startup_stubs/d3d12_draw.inl | 14 +++++ src/startup_stubs/d3d12_xs_device.cpp | 22 +++++++- tests/startup_stream_draw.cpp | 34 +++++++++--- tests/startup_stream_setup.cpp | 14 ++++- tests/startup_stream_viewport_check.py | 74 ++++++++++++++++++++++++++ 7 files changed, 166 insertions(+), 11 deletions(-) create mode 100644 tests/startup_stream_viewport_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index a060c141dd..876bef6c61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1696,6 +1696,10 @@ if(EXISTS "${INTAKE_PYTHON}") "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_stream_draw_check.py" "$" "${backend}") set_tests_properties(startup_stream_draw_${backend} PROPERTIES TIMEOUT 90) + add_test(NAME startup_stream_viewport_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" + "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_stream_viewport_check.py" + "$" "${backend}") + set_tests_properties(startup_stream_viewport_${backend} PROPERTIES TIMEOUT 90) add_test(NAME startup_stream_depth_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}" "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_stream_depth_check.py" "$" "${backend}") @@ -1751,7 +1755,7 @@ if(EXISTS "${INTAKE_PYTHON}") "$" "$" "${backend}") set_tests_properties(startup_xg_bc_array_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) endforeach() - set_tests_properties(startup_stream_draw_hardware PROPERTIES SKIP_RETURN_CODE 77) + set_tests_properties(startup_stream_draw_hardware startup_stream_viewport_hardware PROPERTIES SKIP_RETURN_CODE 77) set_tests_properties(startup_stream_request_warp PROPERTIES TIMEOUT 60) # Startup shader translator: descriptor offsets inside wide scalar loads (no game data). diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 96c3b5eda8..5c5fed39d1 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -27,6 +27,19 @@ It covers what the draw cannot observe: the logged descriptor offset (4 bytes in a byte-for-byte copy of the descriptor, and refusal of a null list or one that is not whole records. The control expects the first blend patch to win and must fail. +## Default viewport and scissor + +A desktop command list that never sets a viewport or scissor rectangle draws no pixel. A translated +draw therefore covers the whole first render target (its width and height, depth 0 to 1) when the title +set no viewport, and the same rectangle as scissor when it set no scissor. Each default applies on its own, +and a viewport or scissor the title did set is never replaced. A list reset starts without either. A call +with no viewports or no rectangles changes nothing. A draw with no render target bound, for example +depth only, gets no default. + +Run `ctest --preset dev --no-tests=error -R "^startup_stream_viewport_(hardware|warp)$" -V` after building +`startup_stream_consumer`. On an 8×4 target the cases draw with neither call, inside a 4×2 rectangle (the +rest keeps the clear color), and on a reset list that follows the rectangle (all pixels drawn again). + ## Output window and fallback diagnostics The desktop swap chain has its own top-level window, class `X2WOutputWindow`. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index be28272f3c..98c69432c7 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1054,6 +1054,20 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI : ud.kind == Translation::root_constant ? ((ud.a >= 0 && ud.a < 16 && ud.b >= 0 && ud.b < 8) ? pass.constants[ud.a][ud.b] : 0) : ud.kind == Translation::stride && ud.a >= 0 && ud.a < 32 ? strides[ud.a] : 0; } + // A desktop list with no viewport or scissor draws nothing: cover the first render target unless the title set its own. + if (pass.target0 && (!pass.viewport_set || !pass.scissor_set)) { + const D3D12_RESOURCE_DESC target = pass.target0->GetDesc(); + if (!pass.viewport_set) { + const D3D12_VIEWPORT full{0.0f, 0.0f, static_cast(target.Width), static_cast(target.Height), 0.0f, 1.0f}; + list->RSSetViewports(1, &full); + pass.viewport_set = true; + } + if (!pass.scissor_set) { + const D3D12_RECT full{0, 0, static_cast(target.Width), static_cast(target.Height)}; + list->RSSetScissorRects(1, &full); + pass.scissor_set = true; + } + } list->SetPipelineState(pipeline); list->SetGraphicsRootSignature(tr->root); ID3D12DescriptorHeap* samplers = tr->samplers.heap ? pass.sampler_heap.Get() : nullptr; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index b08ed9089a..450d3f1151 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -978,6 +978,7 @@ HRESULT __stdcall device_residency(Wrapper* self, UINT count, void* const* objec // when the desktop GPU finishes, and the presented surface is read back and shown in the window. // Everything else on a list is still dropped. void release_targets(Wrapper* self); +void reset_viewport_state(Wrapper* self); void discard_draw_sampler_heaps(Wrapper* self); void use_as_target(Wrapper* self, ID3D12Resource* resource); bool is_container(const void* p); @@ -1198,6 +1199,7 @@ HRESULT __stdcall list_reset(Wrapper* self, void* allocator) } if (SUCCEEDED(hr)) { self->own().list_ready = false; + reset_viewport_state(self); discard_draw_sampler_heaps(self); if (draw_dump_frame()) draw_dumps().discard(static_cast(self->own().real)); } @@ -1648,6 +1650,9 @@ struct Pass { std::unique_ptr sampler_snapshot; x2w_startup::ComPtr sampler_heap; ID3D12DescriptorHeap* bound_sampler_heap = nullptr; + // Whether the title set a viewport / scissor on this list; without them a desktop list draws no pixel. + bool viewport_set = false; + bool scissor_set = false; }; LONG render_draws, render_draws_skipped, render_pipelines_real, render_pipelines_missing; @@ -1690,6 +1695,14 @@ void release_targets(Wrapper* self) pass = Pass{}; } +// A reset list starts without a viewport or scissor, whatever the previous recording set. +void reset_viewport_state(Wrapper* self) +{ + if (!self->own().pass) return; + pass_of(self).viewport_set = false; + pass_of(self).scissor_set = false; +} + // List slot 20, IASetPrimitiveTopology. 0x11 is a console-only rectangle list (no desktop draw yet). void __stdcall list_topology(Wrapper* self, UINT topology) { @@ -1833,13 +1846,18 @@ void __stdcall list_targets(Wrapper* self, UINT count, const D3D12_CPU_DESCRIPTO // List slots 21 / 22 / 23, RSSetViewports / RSSetScissorRects / OMSetBlendFactor. void __stdcall list_viewports(Wrapper* self, UINT count, const D3D12_VIEWPORT* viewports) { - if (auto* list = recording(self); list && count && count <= 16 && x2w_local::readable(viewports, count * sizeof(*viewports))) + // Only a viewport that is forwarded counts as set; a call with no viewports leaves the state as it was. + if (auto* list = recording(self); list && count && count <= 16 && x2w_local::readable(viewports, count * sizeof(*viewports))) { list->RSSetViewports(count, viewports); + pass_of(self).viewport_set = true; + } } void __stdcall list_scissors(Wrapper* self, UINT count, const D3D12_RECT* rects) { - if (auto* list = recording(self); list && count && count <= 16 && x2w_local::readable(rects, count * sizeof(*rects))) + if (auto* list = recording(self); list && count && count <= 16 && x2w_local::readable(rects, count * sizeof(*rects))) { list->RSSetScissorRects(count, rects); + pass_of(self).scissor_set = true; + } } void __stdcall list_blend_factor(Wrapper* self, const float* factor) { diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 366e9b8eba..1641fb6f96 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -287,12 +288,33 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* extern int draw_bc_address_srvs(void*, void*); if (const int result = draw_bc_address_srvs(guest, list)) return result; } - invoke(list, 9); - invoke(queue, 10, 1, arg(&list)); - if (FAILED(static_cast(invoke(queue, 14, arg(fence), 1)))) return 24; - const auto deadline = GetTickCount64() + 10000; - while (invoke(fence, 8) == 0 && GetTickCount64() < deadline) Sleep(1); - if (invoke(fence, 8) != 1) return 25; + const auto submit = [&](UINT64 value) { + invoke(list, 9); + invoke(queue, 10, 1, arg(&list)); + if (FAILED(static_cast(invoke(queue, 14, arg(fence), value)))) return 24; + const auto deadline = GetTickCount64() + 10000; + while (invoke(fence, 8) < value && GetTickCount64() < deadline) Sleep(1); + return invoke(fence, 8) == value ? 0 : 25; + }; + // X2W_TEST_VIEWPORT=reset: the list above drew inside a small rectangle. Record a second list on the same + // object after Reset that sets neither a viewport nor a scissor; it must not inherit the first rectangle. + wchar_t viewport_mode[8] = L""; + GetEnvironmentVariableW(L"X2W_TEST_VIEWPORT", viewport_mode, 8); + UINT64 last = 1; + if (std::wcscmp(viewport_mode, L"reset") == 0) { + if (const int rc = submit(1)) return rc; + const float clear[] = {0, 0, 1, 1}; + D3D12_CPU_DESCRIPTOR_HANDLE rtv{static_cast(invoke(frame.rtv_heap, 9))}; + if (FAILED(static_cast(invoke(frame.allocator, 8))) || + FAILED(static_cast(invoke(list, 139, arg(frame.allocator))))) return 35; + invoke(list, 52, rtv.ptr, arg(clear)); + invoke(list, 50, 1, arg(&rtv)); + invoke(list, 20, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + invoke(list, 25, arg(pso)); + invoke(list, 12, 3, 1); + last = 2; + } + if (const int rc = submit(last)) return rc; return read_stream_target(desktop.Get(), frame, pixels); } diff --git a/tests/startup_stream_setup.cpp b/tests/startup_stream_setup.cpp index 1beffcf29d..aaa09e57b8 100644 --- a/tests/startup_stream_setup.cpp +++ b/tests/startup_stream_setup.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -216,8 +217,17 @@ int open_stream_target(ID3D12Device* desktop, void* guest, void* pso, UINT width invoke(list, 139, arg(allocator)); for (UINT i = 0; i < count; ++i) invoke(list, 52, rtvs[i].ptr, arg(clear)); invoke(list, 50, count, arg(rtvs.data())); - invoke(list, 21, 1, arg(&viewport)); - invoke(list, 22, 1, arg(&rect)); + // X2W_TEST_VIEWPORT: "none" never sets a viewport or scissor; "rect" and "reset" set a rectangle smaller than the target. + wchar_t mode[8] = L""; + GetEnvironmentVariableW(L"X2W_TEST_VIEWPORT", mode, 8); + const bool none = std::wcscmp(mode, L"none") == 0; + const bool small = std::wcscmp(mode, L"rect") == 0 || std::wcscmp(mode, L"reset") == 0; + const D3D12_VIEWPORT inner{2, 1, 4, 2, 0, 1}; + const D3D12_RECT inner_rect{2, 1, 6, 3}; + if (!none) { + invoke(list, 21, 1, small ? arg(&inner) : arg(&viewport)); + invoke(list, 22, 1, small ? arg(&inner_rect) : arg(&rect)); + } invoke(list, 20, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); invoke(list, 25, arg(pso)); return 0; diff --git a/tests/startup_stream_viewport_check.py b/tests/startup_stream_viewport_check.py new file mode 100644 index 0000000000..80889c8988 --- /dev/null +++ b/tests/startup_stream_viewport_check.py @@ -0,0 +1,74 @@ +"""A translated draw covers the render target when the title set no viewport or scissor, and never replaces one it set. + +Three cases on an 8x4 RGBA8 target cleared to blue, drawn with a red pixel shader through a slot-86 wrapper: + none: no RSSetViewports and no RSSetScissorRects (list slots 21 and 22) -> every pixel is red. + rect: a viewport and scissor of 4x2 pixels at (2, 1) -> red inside the rectangle, blue outside. + reset: the rectangle of 'rect' on one list, then the same list object Reset with no viewport or scissor + -> every pixel red, the rectangle is not inherited. +""" +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +from startup_stream_check import gfx2hlsl +from startup_stream_draw_check import pixel, vertex + +WIDTH, HEIGHT = 8, 4 +RED, BLUE = bytes((255, 0, 0, 255)), bytes((0, 0, 255, 255)) + + +def expected_pixels(inside): + """RGBA bytes of the target; inside(x, y) says whether the pixel belongs to the draw.""" + return b"".join(RED if inside(x, y) else BLUE for y in range(HEIGHT) for x in range(WIDTH)) + + +CASES = { + "none": lambda x, y: True, + "rect": lambda x, y: 2 <= x < 6 and 1 <= y < 3, + "reset": lambda x, y: True, +} + + +def first_difference(actual, expected): + for index in range(0, len(expected), 4): + if actual[index:index + 4] != expected[index:index + 4]: + return index // 4 % WIDTH, index // 4 // WIDTH, actual[index:index + 4].hex(), expected[index:index + 4].hex() + return None + + +def check(directory, consumer, backend): + cache = directory / "cache" + cache.mkdir() + vs, ps, root, output = (directory / name for name in ("vs", "ps", "root", "pixels")) + vs.write_bytes(vertex()) + ps.write_bytes(pixel(False)) + args = [consumer, vs, ps, root] + env = dict(os.environ, X2W_D3D12_PERMISSIVE="1", X2W_D3D12_LIST_NOOP="1", X2W_D3D12_RENDER="1", + X2W_SHADER_CACHE=str(cache)) + env.setdefault("X2W_D3D12_DEBUG", "0") + subprocess.run(args, env=env, check=True) + subprocess.run([sys.executable, "-B", gfx2hlsl.__file__, "--pending", cache], env=env, check=True) + for name, inside in CASES.items(): + result = subprocess.run(args + [backend, output], env=dict(env, X2W_TEST_VIEWPORT=name), + capture_output=True, text=True) + print(result.stdout, end="") + if backend == "hardware" and result.returncode == 77: + print("No D3D12 feature-level 12 hardware adapter") + return 77 + if result.returncode or "-> drawn;" not in result.stderr: + raise RuntimeError(f"{name} draw failed on {backend}: {result.returncode}\n{result.stderr}") + actual = output.read_bytes() + expected = expected_pixels(inside) + difference = first_difference(actual, expected) + if difference: + x, y, got, want = difference + raise AssertionError(f"{name}: pixel ({x}, {y}) is {got}, expected {want}") + print(f"{backend} viewport {name}: all {WIDTH * HEIGHT} pixels exact") + return 0 + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory() as temp: + sys.exit(check(Path(temp), *sys.argv[1:])) From 50aaa702f3e1c88b12d9ce71c264e54871f09a0d Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 17:34:54 +0100 Subject: [PATCH 2/6] test: avoid the Windows small macro in the viewport case --- docs/startup-runtime.md | 2 +- tests/startup_stream_setup.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 5c5fed39d1..3d987668db 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -34,7 +34,7 @@ draw therefore covers the whole first render target (its width and height, depth set no viewport, and the same rectangle as scissor when it set no scissor. Each default applies on its own, and a viewport or scissor the title did set is never replaced. A list reset starts without either. A call with no viewports or no rectangles changes nothing. A draw with no render target bound, for example -depth only, gets no default. +depth only, gets no default. The default uses the size of mip 0 of the resource; the render target view's mip slice is not tracked. Run `ctest --preset dev --no-tests=error -R "^startup_stream_viewport_(hardware|warp)$" -V` after building `startup_stream_consumer`. On an 8×4 target the cases draw with neither call, inside a 4×2 rectangle (the diff --git a/tests/startup_stream_setup.cpp b/tests/startup_stream_setup.cpp index aaa09e57b8..454917c7d5 100644 --- a/tests/startup_stream_setup.cpp +++ b/tests/startup_stream_setup.cpp @@ -221,12 +221,12 @@ int open_stream_target(ID3D12Device* desktop, void* guest, void* pso, UINT width wchar_t mode[8] = L""; GetEnvironmentVariableW(L"X2W_TEST_VIEWPORT", mode, 8); const bool none = std::wcscmp(mode, L"none") == 0; - const bool small = std::wcscmp(mode, L"rect") == 0 || std::wcscmp(mode, L"reset") == 0; + const bool inset = std::wcscmp(mode, L"rect") == 0 || std::wcscmp(mode, L"reset") == 0; const D3D12_VIEWPORT inner{2, 1, 4, 2, 0, 1}; const D3D12_RECT inner_rect{2, 1, 6, 3}; if (!none) { - invoke(list, 21, 1, small ? arg(&inner) : arg(&viewport)); - invoke(list, 22, 1, small ? arg(&inner_rect) : arg(&rect)); + invoke(list, 21, 1, inset ? arg(&inner) : arg(&viewport)); + invoke(list, 22, 1, inset ? arg(&inner_rect) : arg(&rect)); } invoke(list, 20, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); invoke(list, 25, arg(pso)); From 1f24626bf511b26ec7cf8236093e2e8e2dce9d3d Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 17:38:50 +0100 Subject: [PATCH 3/6] MUTATION (temporary): disable default viewport --- src/startup_stubs/d3d12_draw.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 98c69432c7..9a649fe4f3 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1057,7 +1057,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI // A desktop list with no viewport or scissor draws nothing: cover the first render target unless the title set its own. if (pass.target0 && (!pass.viewport_set || !pass.scissor_set)) { const D3D12_RESOURCE_DESC target = pass.target0->GetDesc(); - if (!pass.viewport_set) { + if (false && !pass.viewport_set) { const D3D12_VIEWPORT full{0.0f, 0.0f, static_cast(target.Width), static_cast(target.Height), 0.0f, 1.0f}; list->RSSetViewports(1, &full); pass.viewport_set = true; From 22989d61c8691f46fc4347cc69c8f835d9856baa Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 17:40:18 +0100 Subject: [PATCH 4/6] Revert "MUTATION (temporary): disable default viewport" This reverts commit 1f24626bf511b26ec7cf8236093e2e8e2dce9d3d. --- src/startup_stubs/d3d12_draw.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 9a649fe4f3..98c69432c7 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1057,7 +1057,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI // A desktop list with no viewport or scissor draws nothing: cover the first render target unless the title set its own. if (pass.target0 && (!pass.viewport_set || !pass.scissor_set)) { const D3D12_RESOURCE_DESC target = pass.target0->GetDesc(); - if (false && !pass.viewport_set) { + if (!pass.viewport_set) { const D3D12_VIEWPORT full{0.0f, 0.0f, static_cast(target.Width), static_cast(target.Height), 0.0f, 1.0f}; list->RSSetViewports(1, &full); pass.viewport_set = true; From 07f2464fd1c8e1c7c7b5b87b12235700b11a6b6d Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 17:53:01 +0100 Subject: [PATCH 5/6] Regenerate the default viewport and scissor when render target 0 changes Keep the title's viewport/scissor flags apart from the automatic default and remember the size of the default already forwarded, so a list that never sets its own state follows render target 0 across OMSetRenderTargets. Adds a grow case to startup_stream_viewport. AI-assisted; commands listed were executed locally. --- src/startup_stubs/d3d12_draw.inl | 11 ++++++---- src/startup_stubs/d3d12_xs_device.cpp | 6 ++++++ tests/startup_stream_draw.cpp | 28 ++++++++++++++++++++++++++ tests/startup_stream_setup.cpp | 4 ++-- tests/startup_stream_viewport_check.py | 19 ++++++++++------- 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 98c69432c7..ca8e3cadc1 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1057,15 +1057,18 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI // A desktop list with no viewport or scissor draws nothing: cover the first render target unless the title set its own. if (pass.target0 && (!pass.viewport_set || !pass.scissor_set)) { const D3D12_RESOURCE_DESC target = pass.target0->GetDesc(); - if (!pass.viewport_set) { + // Only the title's own calls set the flags; an automatic default is regenerated when render target 0 changes size. + if (!pass.viewport_set && (pass.default_viewport_w != target.Width || pass.default_viewport_h != target.Height)) { const D3D12_VIEWPORT full{0.0f, 0.0f, static_cast(target.Width), static_cast(target.Height), 0.0f, 1.0f}; list->RSSetViewports(1, &full); - pass.viewport_set = true; + pass.default_viewport_w = target.Width; + pass.default_viewport_h = target.Height; } - if (!pass.scissor_set) { + if (!pass.scissor_set && (pass.default_scissor_w != target.Width || pass.default_scissor_h != target.Height)) { const D3D12_RECT full{0, 0, static_cast(target.Width), static_cast(target.Height)}; list->RSSetScissorRects(1, &full); - pass.scissor_set = true; + pass.default_scissor_w = target.Width; + pass.default_scissor_h = target.Height; } } list->SetPipelineState(pipeline); diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 450d3f1151..ec124fb66b 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1653,6 +1653,10 @@ struct Pass { // Whether the title set a viewport / scissor on this list; without them a desktop list draws no pixel. bool viewport_set = false; bool scissor_set = false; + // Size of the automatic full-target default already forwarded to the desktop list (0 = none yet). Kept apart from + // the title flags above so a later render target 0 of another size gets a fresh default. + UINT64 default_viewport_w = 0, default_scissor_w = 0; + UINT default_viewport_h = 0, default_scissor_h = 0; }; LONG render_draws, render_draws_skipped, render_pipelines_real, render_pipelines_missing; @@ -1701,6 +1705,8 @@ void reset_viewport_state(Wrapper* self) if (!self->own().pass) return; pass_of(self).viewport_set = false; pass_of(self).scissor_set = false; + pass_of(self).default_viewport_w = pass_of(self).default_scissor_w = 0; + pass_of(self).default_viewport_h = pass_of(self).default_scissor_h = 0; } // List slot 20, IASetPrimitiveTopology. 0x11 is a console-only rectangle list (no desktop draw yet). diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 1641fb6f96..006fc4d2dd 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -301,6 +301,34 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* wchar_t viewport_mode[8] = L""; GetEnvironmentVariableW(L"X2W_TEST_VIEWPORT", viewport_mode, 8); UINT64 last = 1; + // X2W_TEST_VIEWPORT=grow: no viewport or scissor anywhere. After the draw above on the small target, the same + // list switches to a target twice as large and draws again; the default must follow the new target. + if (std::wcscmp(viewport_mode, L"grow") == 0) { + const struct { + D3D12_DESCRIPTOR_HEAP_DESC desc; + Word backing, bytes; + } big_heap_desc{{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1}, 0, 0}; + void* big_heap = nullptr; + if (FAILED(static_cast(invoke(guest, 14, arg(&big_heap_desc), arg(&__uuidof(ID3D12DescriptorHeap)), arg(&big_heap))))) return 36; + D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT}; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, width * 2, height * 2, 1, 1, + format, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}; + const float clear[] = {0, 0, 1, 1}; + const D3D12_CLEAR_VALUE optimized_clear{format, {clear[0], clear[1], clear[2], clear[3]}}; + StreamAttachment big; + if (FAILED(desktop->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, + D3D12_RESOURCE_STATE_COMMON, &optimized_clear, IID_PPV_ARGS(&big.texture)))) return 37; + D3D12_RENDER_TARGET_VIEW_DESC view{format, D3D12_RTV_DIMENSION_TEXTURE2D, {}}; + D3D12_CPU_DESCRIPTOR_HANDLE rtv{static_cast(invoke(big_heap, 9))}; + invoke(guest, 20, arg(big.texture.Get()), arg(&view), rtv.ptr); + big.row_bytes = width * 2 * 4; + invoke(list, 52, rtv.ptr, arg(clear)); + invoke(list, 50, 1, arg(&rtv)); + invoke(list, 12, 3, 1); + // Read back the larger target only. + frame.attachments[0] = std::move(big); + frame.count = 1; + } if (std::wcscmp(viewport_mode, L"reset") == 0) { if (const int rc = submit(1)) return rc; const float clear[] = {0, 0, 1, 1}; diff --git a/tests/startup_stream_setup.cpp b/tests/startup_stream_setup.cpp index 454917c7d5..bfb5954db4 100644 --- a/tests/startup_stream_setup.cpp +++ b/tests/startup_stream_setup.cpp @@ -217,10 +217,10 @@ int open_stream_target(ID3D12Device* desktop, void* guest, void* pso, UINT width invoke(list, 139, arg(allocator)); for (UINT i = 0; i < count; ++i) invoke(list, 52, rtvs[i].ptr, arg(clear)); invoke(list, 50, count, arg(rtvs.data())); - // X2W_TEST_VIEWPORT: "none" never sets a viewport or scissor; "rect" and "reset" set a rectangle smaller than the target. + // X2W_TEST_VIEWPORT: "none" and "grow" never set a viewport or scissor; "rect" and "reset" set a rectangle smaller than the target. wchar_t mode[8] = L""; GetEnvironmentVariableW(L"X2W_TEST_VIEWPORT", mode, 8); - const bool none = std::wcscmp(mode, L"none") == 0; + const bool none = std::wcscmp(mode, L"none") == 0 || std::wcscmp(mode, L"grow") == 0; const bool inset = std::wcscmp(mode, L"rect") == 0 || std::wcscmp(mode, L"reset") == 0; const D3D12_VIEWPORT inner{2, 1, 4, 2, 0, 1}; const D3D12_RECT inner_rect{2, 1, 6, 3}; diff --git a/tests/startup_stream_viewport_check.py b/tests/startup_stream_viewport_check.py index 80889c8988..129811353b 100644 --- a/tests/startup_stream_viewport_check.py +++ b/tests/startup_stream_viewport_check.py @@ -3,6 +3,8 @@ Three cases on an 8x4 RGBA8 target cleared to blue, drawn with a red pixel shader through a slot-86 wrapper: none: no RSSetViewports and no RSSetScissorRects (list slots 21 and 22) -> every pixel is red. rect: a viewport and scissor of 4x2 pixels at (2, 1) -> red inside the rectangle, blue outside. + grow: no viewport or scissor; one draw on the 8x4 target, then OMSetRenderTargets to a 16x8 target on the same + list and a second draw -> the 16x8 target is entirely red (the default follows the new target). reset: the rectangle of 'rect' on one list, then the same list object Reset with no viewport or scissor -> every pixel red, the rectangle is not inherited. """ @@ -19,22 +21,24 @@ RED, BLUE = bytes((255, 0, 0, 255)), bytes((0, 0, 255, 255)) -def expected_pixels(inside): +def expected_pixels(inside, scale=1): """RGBA bytes of the target; inside(x, y) says whether the pixel belongs to the draw.""" - return b"".join(RED if inside(x, y) else BLUE for y in range(HEIGHT) for x in range(WIDTH)) + return b"".join(RED if inside(x, y) else BLUE for y in range(HEIGHT * scale) for x in range(WIDTH * scale)) CASES = { "none": lambda x, y: True, "rect": lambda x, y: 2 <= x < 6 and 1 <= y < 3, "reset": lambda x, y: True, + "grow": lambda x, y: True, } +SCALE = {"grow": 2} -def first_difference(actual, expected): +def first_difference(actual, expected, width=WIDTH): for index in range(0, len(expected), 4): if actual[index:index + 4] != expected[index:index + 4]: - return index // 4 % WIDTH, index // 4 // WIDTH, actual[index:index + 4].hex(), expected[index:index + 4].hex() + return index // 4 % width, index // 4 // width, actual[index:index + 4].hex(), expected[index:index + 4].hex() return None @@ -60,12 +64,13 @@ def check(directory, consumer, backend): if result.returncode or "-> drawn;" not in result.stderr: raise RuntimeError(f"{name} draw failed on {backend}: {result.returncode}\n{result.stderr}") actual = output.read_bytes() - expected = expected_pixels(inside) - difference = first_difference(actual, expected) + scale = SCALE.get(name, 1) + expected = expected_pixels(inside, scale) + difference = first_difference(actual, expected, WIDTH * scale) if difference: x, y, got, want = difference raise AssertionError(f"{name}: pixel ({x}, {y}) is {got}, expected {want}") - print(f"{backend} viewport {name}: all {WIDTH * HEIGHT} pixels exact") + print(f"{backend} viewport {name}: all {WIDTH * HEIGHT * scale * scale} pixels exact") return 0 From 3e895d2398095ceb73ab9f79c2f72cc7502151fd Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 17:59:04 +0100 Subject: [PATCH 6/6] test: keep the small target alive until the grow list has executed AI-assisted; commands listed were executed locally. --- tests/startup_stream_draw.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/startup_stream_draw.cpp b/tests/startup_stream_draw.cpp index 006fc4d2dd..a9fd3a5bdd 100644 --- a/tests/startup_stream_draw.cpp +++ b/tests/startup_stream_draw.cpp @@ -325,7 +325,8 @@ int check_stream_draw(IDXGIAdapter* adapter, void* guest, void* pso, const char* invoke(list, 52, rtv.ptr, arg(clear)); invoke(list, 50, 1, arg(&rtv)); invoke(list, 12, 3, 1); - // Read back the larger target only. + // Read back the larger target only; the small one stays alive in slot 1 until the list has executed. + frame.attachments[1] = std::move(frame.attachments[0]); frame.attachments[0] = std::move(big); frame.count = 1; }