diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 040ae3e4b2..1706d095a0 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -2393,6 +2393,12 @@ formats are read; for others it logs at least 16 cells differ by more than 12 levels, so a blinking cursor does not count as a new screen. Without a presenter every wait times out and says so. +The presenter logs `x2w-present sampled colour: present= rgba=,,, target=x format=` +for the first plane-0 colour sample and whenever its RGBA changes. The colour comes from the middle +signature row. While signature sampling is idle, the existing plane-0 readback supplies a colour +at most every 60 presents for the 8-bit RGBA and BGRA formats it already reads. This adds no GPU +copies. `X2W_PRESENT_SIGNATURE_EVERY=0` disables the colour log. + Log lines, each once per item: ```text diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 2a92ec33b0..ee26fb4abe 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2147,12 +2147,23 @@ LONG shown_frames; SRWLOCK shown_lock = SRWLOCK_INIT; char shown_note[160] = "no frame read back yet"; +void log_presented_colour(LONG64 present, UINT width, UINT height, DXGI_FORMAT format, const x2w_present_signature::Rgba& rgba) +{ + static std::mutex mutex; + static std::optional previous; + const std::lock_guard lock(mutex); + if (previous && *previous == rgba) return; + previous = rgba; + log("x2w-present sampled colour: present=%lld rgba=%u,%u,%u,%u target=%ux%u format=%d", present, + unsigned{rgba[0]}, unsigned{rgba[1]}, unsigned{rgba[2]}, unsigned{rgba[3]}, width, height, format); +} + void write_file(const wchar_t* path, const void* data, std::size_t bytes); // raw_plane < 0: the baseline capture of the presented surface (plane 0), reduced for the window and // logged as `x2w-render presented`. raw_plane >= 0 (X2W_PRESENT_COMPOSITE=1 only): a sample of that // plane's own surface, before compositing, logged as `x2w-render raw plane `; it leaves the window // copy, the note and the dump alone. Neither is the composited window picture. -void read_back_presented(ID3D12CommandQueue* queue, ID3D12Resource* surface, int raw_plane = -1) +void read_back_presented(ID3D12CommandQueue* queue, ID3D12Resource* surface, LONG colour_present, int raw_plane = -1) { const bool raw = raw_plane >= 0; char failure[160] = ""; @@ -2315,6 +2326,14 @@ void read_back_presented(ID3D12CommandQueue* queue, ID3D12Resource* surface, int report_failure(); return; } + if (colour_present) { + using namespace x2w_present_signature; + const UINT y = sampled_row(cells_y / 2, desc.Height); + const auto* texel = static_cast(mapped) + footprint.Offset + + UINT64{y} * footprint.Footprint.RowPitch + (desc.Width / 2) * 4; + log_presented_colour(colour_present, static_cast(desc.Width), desc.Height, desc.Format, + sampled_rgba(texel, rgba ? Layout::rgba8 : Layout::bgra8, desc.Format)); + } if (!raw) { AcquireSRWLockExclusive(&shown_lock); shown_pixels.resize(shown_width * shown_height); @@ -7537,13 +7556,14 @@ void draw_overlay(LONG frame, double seconds) // only while a script with a picture wait has asked for them (request_sampling). X2W_PRESENT_SIGNATURE_EVERY= // samples every n presents from the start without a request, and 0 turns the signature off. // 18 one-texel rows are copied on the presenting queue and read back on a later present; nothing waits. -void present_signature(Wrapper* queue, Wrapper* surface, LONG frame) +// Returns true when the periodic plane-0 readback can supply colour because no signature is requested. +bool present_signature(Wrapper* queue, Wrapper* surface, LONG frame) { static const int explicit_every = [] { char value[16]{}; return GetEnvironmentVariableA("X2W_PRESENT_SIGNATURE_EVERY", value, sizeof(value)) ? std::atoi(value) : -1; }(); - if (explicit_every == 0) return; + if (explicit_every == 0) return false; static const int every = explicit_every > 0 ? explicit_every : 4; static x2w_present_signature::Block* const block = [] { auto* created = x2w_present_signature::create_block(GetCurrentProcessId(), static_cast(every)); @@ -7553,14 +7573,18 @@ void present_signature(Wrapper* queue, Wrapper* surface, LONG frame) return created; }(); // Kept until the process ends, so a copy in flight never loses the objects it uses. - static auto* const sampler = new x2w_present_signature::Sampler(every, explicit_every > 0, red_producers().enabled()); + static auto* const sampler = new x2w_present_signature::Sampler(every, explicit_every > 0, true); + static std::mutex mutex; + const std::lock_guard lock(mutex); ID3D12Resource* real = surface && surface->own().kind == resource_kind ? static_cast(surface->own().real) : nullptr; const bool published = sampler->present(device_of(the_device), static_cast(queue->own().real), real, frame, block); static LONG first, refused, failed; + if (published) + log_presented_colour(sampler->last_present(), sampler->last_width(), sampler->last_height(), sampler->last_format(), sampler->last_rgba()); if (published && red_producers().enabled() && sampler->last_format() == DXGI_FORMAT_R8G8B8A8_UNORM) { - const auto* rgba = sampler->last_rgba(); + const auto& rgba = sampler->last_rgba(); const bool red = rgba[0] == 255 && rgba[1] == 0 && rgba[2] == 0; - const bool matched = red_producers().matches(rgba); + const bool matched = red_producers().matches(rgba.data()); red_producers().observe(sampler->last_present(), frame, sampler->last_surface(), matched); static bool was_matched; if (matched != was_matched || red_producers().interested()) { @@ -7577,6 +7601,7 @@ void present_signature(Wrapper* queue, Wrapper* surface, LONG frame) if (sampler->failed() && first_calls(failed, 1)) log(sampler->stranded() ? "x2w-present picture signature: Signal failed after a copy was submitted; sampling stopped, its surface stays referenced" : "x2w-present picture signature: setup failed, no samples"); + return explicit_every < 0 && !(block && block->sampling_wanted); } void report_budget(ID3D12CommandQueue* queue) @@ -7738,6 +7763,7 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa } } bool shown_in_window = false; + LONG readback_colour_present = 0; log_present_planes(frame, planes, plane_parameters); // X2W_PRESENT_COMPOSITE=1: the highest plane above plane 0, sampled raw below. ID3D12Resource* raw_surface = nullptr; @@ -7773,7 +7799,10 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa } } InterlockedExchange(&last_frame_shown, shown_in_window ? 1 : 0); - if (render_mode()) present_signature(self, surface, frame); + // Reuse an already scheduled readback at most every 60 presents, even if the overlay + // needs one every six. The colour diagnostic records no GPU work of its own. + if (render_mode() && present_signature(self, surface, frame) && frame % 60 == 0) + readback_colour_present = frame; } const ULONGLONG now = GetTickCount64(); if (frame == 1) started = last_report = now; @@ -7789,13 +7818,14 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa if (x2w_local::readable(words, 0x18) && words[1] >= 1 && x2w_local::readable(reinterpret_cast(words[2]), 8)) { Wrapper* surface = as_wrapper(*reinterpret_cast(words[2])); if (surface && surface->own().kind == resource_kind && surface->own().real) - read_back_presented(static_cast(self->own().real), static_cast(surface->own().real)); + read_back_presented(static_cast(self->own().real), static_cast(surface->own().real), + readback_colour_present); else std::snprintf(shown_note, sizeof(shown_note), "presented object %p is not a known resource", *reinterpret_cast(words[2])); } // The raw plane sample is that plane's own surface, not the composited window picture. - if (raw_surface) read_back_presented(static_cast(self->own().real), raw_surface, static_cast(raw_plane)); + if (raw_surface) read_back_presented(static_cast(self->own().real), raw_surface, 0, static_cast(raw_plane)); } if (shown_in_window) { if (frame % 30 == 0) { diff --git a/src/startup_stubs/present_signature_sampler.h b/src/startup_stubs/present_signature_sampler.h index c3c3dbce2b..98c08236c2 100644 --- a/src/startup_stubs/present_signature_sampler.h +++ b/src/startup_stubs/present_signature_sampler.h @@ -27,6 +27,7 @@ #include +#include #include namespace x2w_present_signature { @@ -46,6 +47,22 @@ inline bool layout_of(DXGI_FORMAT format, Layout& layout) } } +using Rgba = std::array; + +inline Rgba sampled_rgba(const unsigned char* texel, Layout layout, DXGI_FORMAT format) +{ + if (layout == Layout::rgb10a2) { + std::uint32_t packed; + std::memcpy(&packed, texel, sizeof(packed)); + return {static_cast((packed >> 2) & 255), static_cast((packed >> 12) & 255), + static_cast((packed >> 22) & 255), static_cast((packed >> 30) * 85)}; + } + if (layout == Layout::rgba8) return {texel[0], texel[1], texel[2], texel[3]}; + const auto view = x2w_startup::present_view_format(format); + const bool opaque = view == DXGI_FORMAT_B8G8R8X8_UNORM || view == DXGI_FORMAT_B8G8R8X8_UNORM_SRGB; + return {texel[2], texel[1], texel[0], opaque ? static_cast(255) : texel[3]}; +} + class Sampler { public: explicit Sampler(int every, bool always = true, bool capture_center = false) @@ -61,33 +78,34 @@ class Sampler { // One observation per present decision: device removal (UINT64_MAX) is never a // completion, exactly like a failed Signal. switch (x2w_startup::fence_completion(fence_->GetCompletedValue(), fence_value_)) { - case x2w_startup::FenceCompletion::completed: + case x2w_startup::FenceCompletion::completed: { in_flight_ = false; - if (capture_center_) { - last_present_ = copied_present_; - last_format_ = format_; - last_surface_ = source_; - } + ID3D12Resource* const copied_surface = source_; retire_source(); - { - void* mapped = nullptr; - const D3D12_RANGE range{0, static_cast(row_stride_ * rows_)}; - if (SUCCEEDED(readback_->Map(0, &range, &mapped))) { - const auto* base = static_cast(mapped); - if (capture_center_) - std::memcpy(last_rgba_, base + row_stride_ * (cells_y / 2) + (width_ / 2) * 4, 4); - std::uint8_t luma[cells]; - for (int band = 0; band < cells_y; ++band) reduce_row(base + row_stride_ * band, width_, layout_, luma + band * cells_x); - if (rows_ > cells_y) - for (int band = 0; band < fine_y; ++band) - reduce_row(base + row_stride_ * (cells_y + band), width_, layout_, fine_ + band * fine_x, fine_x); - const D3D12_RANGE none{0, 0}; - readback_->Unmap(0, &none); - if (block) publish(block, luma, copied_present_, rows_ > cells_y ? fine_ : nullptr); - published = true; + void* mapped = nullptr; + const D3D12_RANGE range{0, static_cast(row_stride_ * rows_)}; + if (SUCCEEDED(readback_->Map(0, &range, &mapped)) && mapped) { + const auto* base = static_cast(mapped); + if (capture_center_) { + last_present_ = copied_present_; + last_format_ = format_; + last_width_ = width_; + last_height_ = height_; + last_surface_ = copied_surface; + last_rgba_ = sampled_rgba(base + row_stride_ * (cells_y / 2) + (width_ / 2) * 4, layout_, format_); } + std::uint8_t luma[cells]; + for (int band = 0; band < cells_y; ++band) reduce_row(base + row_stride_ * band, width_, layout_, luma + band * cells_x); + if (rows_ > cells_y) + for (int band = 0; band < fine_y; ++band) + reduce_row(base + row_stride_ * (cells_y + band), width_, layout_, fine_ + band * fine_x, fine_x); + const D3D12_RANGE none{0, 0}; + readback_->Unmap(0, &none); + if (block) publish(block, luma, copied_present_, rows_ > cells_y ? fine_ : nullptr); + published = true; } break; + } case x2w_startup::FenceCompletion::removed: // Device removal: the copy's end cannot be seen. Publish nothing (the readback // is stale) and keep the prior historical sample state as it is, as a failed @@ -149,7 +167,10 @@ class Sampler { } in_flight_ = true; layout_ = layout; - if (capture_center_) format_ = desc.Format; + if (capture_center_) { + format_ = desc.Format; + height_ = desc.Height; + } width_ = width; row_stride_ = stride; rows_ = rows; @@ -159,7 +180,9 @@ class Sampler { DXGI_FORMAT last_format() const { return last_format_; } LONG64 last_present() const { return last_present_; } - const unsigned char* last_rgba() const { return last_rgba_; } + UINT last_width() const { return last_width_; } + UINT last_height() const { return last_height_; } + const Rgba& last_rgba() const { return last_rgba_; } ID3D12Resource* last_surface() const { return last_surface_; } bool failed() const { return failed_; } @@ -247,7 +270,7 @@ class Sampler { const bool capture_center_; DXGI_FORMAT format_ = DXGI_FORMAT_UNKNOWN, last_format_ = DXGI_FORMAT_UNKNOWN; LONG64 last_present_ = 0; - unsigned char last_rgba_[4]{}; + Rgba last_rgba_{}; ID3D12Resource* last_surface_ = nullptr; int every_; bool always_; @@ -258,7 +281,7 @@ class Sampler { ID3D12Resource* readback_ = nullptr; ID3D12Resource* source_ = nullptr; // the surface of the copy in flight UINT64 readback_bytes_ = 0, fence_value_ = 0, row_stride_ = 0; - UINT width_ = 0; + UINT width_ = 0, height_ = 0, last_width_ = 0, last_height_ = 0; int rows_ = cells_y; std::uint8_t fine_[fine_cells] = {}; Layout layout_ = Layout::rgba8; diff --git a/tests/cmake/startup_present_colour.cmake b/tests/cmake/startup_present_colour.cmake new file mode 100644 index 0000000000..c093f693cf --- /dev/null +++ b/tests/cmake/startup_present_colour.cmake @@ -0,0 +1,5 @@ +if(TARGET startup_present_producer) + add_test(NAME startup_present_colour_warp COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_present_producer_check.py" warp "$" colour) + set_tests_properties(startup_present_colour_warp PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 105) +endif() diff --git a/tests/startup_present_producer.cpp b/tests/startup_present_producer.cpp index 32f6fe0b6e..ca43084481 100644 --- a/tests/startup_present_producer.cpp +++ b/tests/startup_present_producer.cpp @@ -30,6 +30,8 @@ int main(int argc, char** argv) const bool use_magenta = argc >= 2 && std::strcmp(argv[1], "auto-magenta") == 0; const bool automatic = use_magenta || (argc >= 2 && std::strcmp(argv[1], "auto") == 0); const bool barriers = argc >= 2 && std::strcmp(argv[1], "barriers") == 0; + const bool colour_readback = argc >= 2 && std::strcmp(argv[1], "colour-readback") == 0; + const bool colour_log = colour_readback || (argc >= 2 && std::strcmp(argv[1], "colour") == 0); const bool hardware = argc == 3 && std::strcmp(argv[2], "hardware") == 0; ComPtr debug; if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)))) debug->EnableDebugLayer(); @@ -66,8 +68,8 @@ int main(int argc, char** argv) !ok(call(device, 73, arg(list_desc), arg(allocator), 0, arg(&__uuidof(ID3D12GraphicsCommandList)), arg(&list))) || !ok(call(device, 36, 0, 0, arg(&__uuidof(ID3D12Fence)), arg(&fence)))) return 5; const D3D12_HEAP_PROPERTIES properties{D3D12_HEAP_TYPE_DEFAULT}; - constexpr auto format = DXGI_FORMAT_R8G8B8A8_UNORM; - const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 16, 16, 1, 1, format, {1, 0}, + const auto format = colour_log && !colour_readback ? DXGI_FORMAT_B8G8R8A8_UNORM : DXGI_FORMAT_R8G8B8A8_UNORM; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 16, colour_log ? 12u : 16u, 1, 1, format, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}; using Create = HRESULT (WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS, const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_STATES, const D3D12_CLEAR_VALUE*, REFIID, void**); @@ -129,6 +131,16 @@ int main(int argc, char** argv) void* lists[]{list}; call(queue, 10, 1, arg(lists)); if (!finish()) return 17; + } else if (colour_log) { + // Clear to uniform colours so this checks the sampled RGBA and the sample present number. + constexpr float transparent[]{0, 0, 0, 0}, black[]{0, 0, 0, 1}; + const unsigned interval = colour_readback ? 60 : 1; + for (const float* colour : {transparent, transparent, black, black, red, red, transparent}) { + if (!emit(colour)) return 18; + for (unsigned i = 0; i < interval; ++i) + if (!present()) return 18; + } + if (!present()) return 18; // publish the last asynchronous signature } else if (!emit(automatic ? chosen : blue) || !present()) { return 8; // discover the title's present surface } else if (automatic) { diff --git a/tests/startup_present_producer_check.py b/tests/startup_present_producer_check.py index 9307ffcddc..ec80cb8b03 100644 --- a/tests/startup_present_producer_check.py +++ b/tests/startup_present_producer_check.py @@ -8,13 +8,16 @@ BACKEND, EXECUTABLE, *OPTIONS = sys.argv[1:] -def run(mode, rgb=""): +def run(mode, rgb="", every="1"): env = dict(os.environ, X2W_D3D12_PERMISSIVE="1", X2W_D3D12_LIST_NOOP="1", X2W_D3D12_RENDER="1", - X2W_PRESENT_SIGNATURE_EVERY="1", X2W_RED_PRODUCER_FRAMES="", X2W_RED_PRODUCER_AUTO="", - X2W_RED_PRODUCER_RGB=rgb) + X2W_RED_PRODUCER_FRAMES="", X2W_RED_PRODUCER_AUTO="", X2W_RED_PRODUCER_RGB=rgb) + if every is None: + env.pop("X2W_PRESENT_SIGNATURE_EVERY", None) + else: + env["X2W_PRESENT_SIGNATURE_EVERY"] = every if mode.startswith("auto"): env["X2W_RED_PRODUCER_AUTO"] = "4,2" - else: + elif not mode.startswith("colour"): env["X2W_RED_PRODUCER_FRAMES"] = "1,12" args = [EXECUTABLE, mode or "manual", BACKEND] done = subprocess.run(args, env=env, capture_output=True, text=True, timeout=30) @@ -59,8 +62,27 @@ def check_barriers(): print(f"slot 26: forwarded global and resource UAV barriers, refused five unsupported records, an unreadable array and a closed list ({adapter})") +def check_colour(): + prefix = "x2w-present sampled colour: " + colours = ("0,0,0,0", "0,0,0,255", RED, "0,0,0,0") + for mode, every, presents in (("colour", "1", (1, 3, 5, 7)), + ("colour-readback", None, (60, 180, 300, 420)), + ("colour-readback", "0", ())): + logs, _, adapter = run(mode, every=every) + actual = [line for line in logs.splitlines() if line.startswith(prefix)] + target_format = 87 if mode == "colour" else 28 # BGRA8 and RGBA8 UNORM + expected = [f"{prefix}present={present} rgba={colour} target=16x12 format={target_format}" + for present, colour in zip(presents, colours)] + assert actual == expected, (expected, actual, logs) + assert ("first sample published" in logs) == (every == "1"), logs + assert "x2w-red sample" not in logs, logs + print(f"{mode} every={every}: {len(actual)} sampled colour lines ({adapter})") + + if __name__ == "__main__" and OPTIONS == ["barriers"]: check_barriers() +elif __name__ == "__main__" and OPTIONS == ["colour"]: + check_colour() elif __name__ == "__main__": check(False) check(True)