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
6 changes: 6 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<n> rgba=<r>,<g>,<b>,<a> target=<w>x<h> format=<dxgi>`
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
Expand Down
48 changes: 39 additions & 9 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<x2w_present_signature::Rgba> 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 <n>`; 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] = "";
Expand Down Expand Up @@ -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<const unsigned char*>(mapped) + footprint.Offset +
UINT64{y} * footprint.Footprint.RowPitch + (desc.Width / 2) * 4;
log_presented_colour(colour_present, static_cast<UINT>(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);
Expand Down Expand Up @@ -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=<n>
// 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<std::uint32_t>(every));
Expand All @@ -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<ID3D12Resource*>(surface->own().real) : nullptr;
const bool published = sampler->present(device_of(the_device), static_cast<ID3D12CommandQueue*>(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()) {
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<const void*>(words[2]), 8)) {
Wrapper* surface = as_wrapper(*reinterpret_cast<void* const*>(words[2]));
if (surface && surface->own().kind == resource_kind && surface->own().real)
read_back_presented(static_cast<ID3D12CommandQueue*>(self->own().real), static_cast<ID3D12Resource*>(surface->own().real));
read_back_presented(static_cast<ID3D12CommandQueue*>(self->own().real), static_cast<ID3D12Resource*>(surface->own().real),
readback_colour_present);
else
std::snprintf(shown_note, sizeof(shown_note), "presented object %p is not a known resource",
*reinterpret_cast<void* const*>(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<ID3D12CommandQueue*>(self->own().real), raw_surface, static_cast<int>(raw_plane));
if (raw_surface) read_back_presented(static_cast<ID3D12CommandQueue*>(self->own().real), raw_surface, 0, static_cast<int>(raw_plane));
}
if (shown_in_window) {
if (frame % 30 == 0) {
Expand Down
75 changes: 49 additions & 26 deletions src/startup_stubs/present_signature_sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <d3d12.h>

#include <array>
#include <initializer_list>

namespace x2w_present_signature {
Expand All @@ -46,6 +47,22 @@ inline bool layout_of(DXGI_FORMAT format, Layout& layout)
}
}

using Rgba = std::array<unsigned char, 4>;

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<unsigned char>((packed >> 2) & 255), static_cast<unsigned char>((packed >> 12) & 255),
static_cast<unsigned char>((packed >> 22) & 255), static_cast<unsigned char>((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<unsigned char>(255) : texel[3]};
}

class Sampler {
public:
explicit Sampler(int every, bool always = true, bool capture_center = false)
Expand All @@ -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<SIZE_T>(row_stride_ * rows_)};
if (SUCCEEDED(readback_->Map(0, &range, &mapped))) {
const auto* base = static_cast<const unsigned char*>(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<SIZE_T>(row_stride_ * rows_)};
if (SUCCEEDED(readback_->Map(0, &range, &mapped)) && mapped) {
const auto* base = static_cast<const unsigned char*>(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
Expand Down Expand Up @@ -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;
Expand All @@ -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_; }
Expand Down Expand Up @@ -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_;
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tests/cmake/startup_present_colour.cmake
Original file line number Diff line number Diff line change
@@ -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 "$<TARGET_FILE:startup_present_producer>" colour)
set_tests_properties(startup_present_colour_warp PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 105)
endif()
16 changes: 14 additions & 2 deletions tests/startup_present_producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ID3D12Debug> debug;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)))) debug->EnableDebugLayer();
Expand Down Expand Up @@ -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**);
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading