From 64ba56079fd3db809fa83c396f2a0cf4a8b6edf1 Mon Sep 17 00:00:00 2001 From: Arths17 Date: Wed, 25 Nov 2026 14:55:51 -0600 Subject: [PATCH] Add the D3D11 frame-loop workload, negative controls and reproducible workload builds --- scripts/hybrid/build-windows-workloads.sh | 6 +- workloads/d3d11-clear/d3d11_clear.c | 32 +++-- workloads/d3d11-frameloop/frameloop.c | 147 ++++++++++++++++++++++ 3 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 workloads/d3d11-frameloop/frameloop.c diff --git a/scripts/hybrid/build-windows-workloads.sh b/scripts/hybrid/build-windows-workloads.sh index 0604b6a..da14e5f 100755 --- a/scripts/hybrid/build-windows-workloads.sh +++ b/scripts/hybrid/build-windows-workloads.sh @@ -20,10 +20,12 @@ RUSTC_TC=${RUSTUP_TOOLCHAIN_FOR_WORKLOADS:-1.100.0} for arch in x86_64 aarch64; do d="$OUT/$arch"; mkdir -p "$d" - "$TC/bin/$arch-w64-mingw32-clang" -O2 -Wall -Wextra -Werror \ + "$TC/bin/$arch-w64-mingw32-clang" -O2 -Wall -Wextra -Werror -Wl,--no-insert-timestamp \ -o "$d/selfcheck-c.exe" "$ROOT/workloads/win-selfcheck-c/selfcheck.c" - "$TC/bin/$arch-w64-mingw32-clang" -O2 -Wall -Wextra -Werror \ + "$TC/bin/$arch-w64-mingw32-clang" -O2 -Wall -Wextra -Werror -Wl,--no-insert-timestamp \ -o "$d/d3d11-clear.exe" "$ROOT/workloads/d3d11-clear/d3d11_clear.c" -ld3d11 -ldxgi + "$TC/bin/$arch-w64-mingw32-clang" -O2 -Wall -Wextra -Werror -Wl,--no-insert-timestamp \ + -o "$d/d3d11-frameloop.exe" "$ROOT/workloads/d3d11-frameloop/frameloop.c" -ld3d11 -ldxgi -ldxguid -ld3dcompiler -lm env_arch=$(echo "$arch" | tr a-z A-Z) ( export PATH="$TC/bin:$PATH" diff --git a/workloads/d3d11-clear/d3d11_clear.c b/workloads/d3d11-clear/d3d11_clear.c index b924b7a..3219a5c 100644 --- a/workloads/d3d11-clear/d3d11_clear.c +++ b/workloads/d3d11-clear/d3d11_clear.c @@ -1,15 +1,23 @@ // SPDX-License-Identifier: GPL-3.0-or-later // Original BetterXen workload (issue #365, first step): Direct3D 11 clear and readback. // Creates a hardware device, clears a 64x64 RGBA8 render target to (0.25, 0.5, 0.75, 1.0), -// copies it to a staging texture, maps it, and checks every pixel against the expected bytes -// (64, 128, 191, 255; 0.25*255 = 63.75 rounds to 64, 0.5*255 = 127.5 to 128, 0.75*255 = 191.25 -// to 191, under D3D11's round-to-nearest conversion). Exit codes: 0 pass, 1 wrong pixel, -// 2 no device (reported, not a pass), 3 other API failure. +// copies it to a staging texture, maps it, and checks every pixel. Expected bytes by the ideal +// FLOAT->UNORM rule (c*255, add 0.5, drop the fraction): 64, 128, 191, 255. The Direct3D data +// conversion rules also permit a tolerance of 0.6 unit-last-place on the integer side +// (D3D11/D3D12_FLOAT32_TO_INTEGER_TOLERANCE_IN_ULP = 0.6f), so a byte passes when it is +// within 0.6 of c*255. 0.5*255 = 127.5 is an exact tie, so both 127 and 128 are permitted. +// The program prints whether the result matched the ideal exactly. This corrects an earlier +// version that required exact equality, which was stricter than the specification. +// Exit codes: 0 pass, 1 wrong pixel, 2 no device (reported, not a pass), 3 other API failure. #define COBJMACROS #include +#include #include +#include -int main(void) { +// `--negative-control` expects a deliberately wrong colour; the program must then exit 1. +int main(int argc, char **argv) { + const int negative = argc > 1 && !strcmp(argv[1], "--negative-control"); ID3D11Device *dev = NULL; ID3D11DeviceContext *ctx = NULL; D3D_FEATURE_LEVEL got; @@ -37,12 +45,20 @@ int main(void) { if (FAILED(ID3D11DeviceContext_Map(ctx, (ID3D11Resource *)st, 0, D3D11_MAP_READ, 0, &m))) { printf("FAIL map\n"); return 3; } - int bad = 0; + const unsigned char ideal[4] = {64, 128, 191, 255}; + const float tol = 0.6f; + int bad = 0, inexact = 0; for (UINT y = 0; y < 64; y++) { const unsigned char *row = (const unsigned char *)m.pData + (size_t)y * m.RowPitch; for (UINT x = 0; x < 64; x++) { const unsigned char *p = row + x * 4; - if (p[0] != 64 || p[1] != 128 || p[2] != 191 || p[3] != 255) { + int off_ideal = 0, off_tol = 0; + for (int ch = 0; ch < 4; ch++) { + if (p[ch] != ideal[ch] + (negative ? 100 : 0)) off_ideal = 1; + if (fabsf(color[ch] * 255.0f + (negative ? 100.0f : 0.0f) - (float)p[ch]) > tol) off_tol = 1; + } + if (off_ideal) inexact++; + if (off_tol) { if (bad < 3) printf("pixel (%u,%u) = %u %u %u %u\n", x, y, p[0], p[1], p[2], p[3]); bad++; } @@ -50,6 +66,6 @@ int main(void) { } ID3D11DeviceContext_Unmap(ctx, (ID3D11Resource *)st, 0); if (bad) { printf("FAIL %d wrong pixels\n", bad); return 1; } - printf("PASS 4096 pixels\n"); + printf("PASS 4096 pixels within tolerance; %d differ from the ideal rounding\n", inexact); return 0; } diff --git a/workloads/d3d11-frameloop/frameloop.c b/workloads/d3d11-frameloop/frameloop.c new file mode 100644 index 0000000..6685725 --- /dev/null +++ b/workloads/d3d11-frameloop/frameloop.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// Original BetterXen workload (issue #365, #145): Direct3D 11 indexed triangle, shaders, +// offscreen render target and a sustained frame loop with per-frame GPU-completion timing. +// +// Pipeline: HLSL vertex and pixel shaders compiled at run time with D3DCompile, a vertex +// buffer (3 vertices), an index buffer (3 indices), a 640x360 RGBA8 render target, no window +// and no presentation. Each frame clears, draws, issues an event query and waits for it to +// complete; the interval between consecutive completions is printed as `frame_ms `, +// one line per frame, so `betterxen-hybrid frames` can summarise them. This measures +// submit-to-completion of a trivial draw plus synchronisation, NOT presentation or realistic +// rendering cost. +// +// Correctness (read back once after the first frame): pixel (0,0) lies outside the triangle +// and must be the clear colour (0,0,0,255) exactly; the centre pixel must be the triangle +// colour (0.2, 0.4, 0.8, 1.0) within 0.6 of c*255 (the Direct3D float-to-UNORM tolerance). +// Usage: frameloop.exe [--negative-control] [frames] (default 300). Exit 0 pass, 1 wrong pixel, 2 no device, +// 3 other API failure. +#define COBJMACROS +#include +#include +#include +#include +#include +#include +#include + +static const char *VS = + "struct V { float2 p : POSITION; };\n" + "float4 main(V v) : SV_Position { return float4(v.p, 0, 1); }\n"; +static const char *PS = + "float4 main() : SV_Target { return float4(0.2, 0.4, 0.8, 1.0); }\n"; + +#define CHECK(x, what) do { HRESULT hr_ = (x); if (FAILED(hr_)) { \ + printf("FAIL %s hr=0x%08lx\n", what, (unsigned long)hr_); return 3; } } while (0) + +static ID3DBlob *compile(const char *src, const char *target) { + ID3DBlob *code = NULL, *err = NULL; + HRESULT hr = D3DCompile(src, strlen(src), NULL, NULL, NULL, "main", target, 0, 0, &code, &err); + if (FAILED(hr)) { + printf("FAIL D3DCompile %s hr=0x%08lx %s\n", target, (unsigned long)hr, + err ? (const char *)ID3D10Blob_GetBufferPointer(err) : ""); + return NULL; + } + return code; +} + +int main(int argc, char **argv) { + // `--negative-control [frames]` expects a deliberately wrong centre colour and must exit 1. + const int negative = argc > 1 && !strcmp(argv[1], "--negative-control"); + int frames = argc > 1 + negative ? atoi(argv[1 + negative]) : 300; + if (frames < 1) frames = 1; + ID3D11Device *dev = NULL; + ID3D11DeviceContext *ctx = NULL; + D3D_FEATURE_LEVEL fl; + HRESULT hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, NULL, 0, + D3D11_SDK_VERSION, &dev, &fl, &ctx); + if (FAILED(hr)) { printf("NO DEVICE hr=0x%08lx\n", (unsigned long)hr); return 2; } + printf("device ok feature_level=0x%x\n", (unsigned)fl); + { + IDXGIDevice *dd = NULL; IDXGIAdapter *ad = NULL; DXGI_ADAPTER_DESC desc; + if (SUCCEEDED(ID3D11Device_QueryInterface(dev, &IID_IDXGIDevice, (void **)&dd)) && + SUCCEEDED(IDXGIDevice_GetAdapter(dd, &ad)) && SUCCEEDED(IDXGIAdapter_GetDesc(ad, &desc))) { + char name[128]; size_t i = 0; + for (; desc.Description[i] && i < 127; i++) name[i] = (char)desc.Description[i]; + name[i] = 0; + printf("adapter=%s vendor=0x%x dedicated_vram_mb=%u\n", name, desc.VendorId, + (unsigned)(desc.DedicatedVideoMemory >> 20)); + } + } + + ID3DBlob *vsb = compile(VS, "vs_4_0"), *psb = compile(PS, "ps_4_0"); + if (!vsb || !psb) return 3; + ID3D11VertexShader *vs; ID3D11PixelShader *ps; ID3D11InputLayout *il; + CHECK(ID3D11Device_CreateVertexShader(dev, ID3D10Blob_GetBufferPointer(vsb), ID3D10Blob_GetBufferSize(vsb), NULL, &vs), "vs"); + CHECK(ID3D11Device_CreatePixelShader(dev, ID3D10Blob_GetBufferPointer(psb), ID3D10Blob_GetBufferSize(psb), NULL, &ps), "ps"); + D3D11_INPUT_ELEMENT_DESC ied = {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}; + CHECK(ID3D11Device_CreateInputLayout(dev, &ied, 1, ID3D10Blob_GetBufferPointer(vsb), ID3D10Blob_GetBufferSize(vsb), &il), "layout"); + + // A triangle that covers the centre of the target and not the (0,0) corner pixel. + const float verts[6] = {-0.5f, -0.5f, 0.0f, 0.6f, 0.5f, -0.5f}; + const unsigned short idx[3] = {0, 1, 2}; // clockwise in clip space (y up) = front face; D3D11 culls back faces by default + ID3D11Buffer *vb, *ib; + D3D11_BUFFER_DESC bd = {0}; D3D11_SUBRESOURCE_DATA sd = {0}; + bd.ByteWidth = sizeof verts; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; sd.pSysMem = verts; + CHECK(ID3D11Device_CreateBuffer(dev, &bd, &sd, &vb), "vb"); + bd.ByteWidth = sizeof idx; bd.BindFlags = D3D11_BIND_INDEX_BUFFER; sd.pSysMem = idx; + CHECK(ID3D11Device_CreateBuffer(dev, &bd, &sd, &ib), "ib"); + + const UINT W = 640, H = 360; + D3D11_TEXTURE2D_DESC td = {0}; + td.Width = W; td.Height = H; td.MipLevels = 1; td.ArraySize = 1; + td.Format = DXGI_FORMAT_R8G8B8A8_UNORM; td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; td.BindFlags = D3D11_BIND_RENDER_TARGET; + ID3D11Texture2D *rt, *st; ID3D11RenderTargetView *rtv; + CHECK(ID3D11Device_CreateTexture2D(dev, &td, NULL, &rt), "rt"); + td.Usage = D3D11_USAGE_STAGING; td.BindFlags = 0; td.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + CHECK(ID3D11Device_CreateTexture2D(dev, &td, NULL, &st), "staging"); + CHECK(ID3D11Device_CreateRenderTargetView(dev, (ID3D11Resource *)rt, NULL, &rtv), "rtv"); + + D3D11_QUERY_DESC qd = {D3D11_QUERY_EVENT, 0}; + ID3D11Query *q; CHECK(ID3D11Device_CreateQuery(dev, &qd, &q), "query"); + + D3D11_VIEWPORT vp = {0, 0, (float)W, (float)H, 0, 1}; + const float clear[4] = {0, 0, 0, 1}; + UINT stride = 8, off = 0; + LARGE_INTEGER freq, prev, now; QueryPerformanceFrequency(&freq); + QueryPerformanceCounter(&prev); + for (int f = 0; f < frames; f++) { + ID3D11DeviceContext_OMSetRenderTargets(ctx, 1, &rtv, NULL); + ID3D11DeviceContext_RSSetViewports(ctx, 1, &vp); + ID3D11DeviceContext_ClearRenderTargetView(ctx, rtv, clear); + ID3D11DeviceContext_IASetInputLayout(ctx, il); + ID3D11DeviceContext_IASetPrimitiveTopology(ctx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ID3D11DeviceContext_IASetVertexBuffers(ctx, 0, 1, &vb, &stride, &off); + ID3D11DeviceContext_IASetIndexBuffer(ctx, ib, DXGI_FORMAT_R16_UINT, 0); + ID3D11DeviceContext_VSSetShader(ctx, vs, NULL, 0); + ID3D11DeviceContext_PSSetShader(ctx, ps, NULL, 0); + ID3D11DeviceContext_DrawIndexed(ctx, 3, 0, 0); + ID3D11DeviceContext_End(ctx, (ID3D11Asynchronous *)q); + ID3D11DeviceContext_Flush(ctx); + BOOL done = FALSE; + while (ID3D11DeviceContext_GetData(ctx, (ID3D11Asynchronous *)q, &done, sizeof done, 0) != S_OK || !done) { } + QueryPerformanceCounter(&now); + printf("frame_ms %.4f\n", (double)(now.QuadPart - prev.QuadPart) * 1000.0 / (double)freq.QuadPart); + prev = now; + if (f == 0) { + ID3D11DeviceContext_CopyResource(ctx, (ID3D11Resource *)st, (ID3D11Resource *)rt); + D3D11_MAPPED_SUBRESOURCE m; + CHECK(ID3D11DeviceContext_Map(ctx, (ID3D11Resource *)st, 0, D3D11_MAP_READ, 0, &m), "map"); + const unsigned char *c = (const unsigned char *)m.pData; + const unsigned char *k = c + (size_t)(H / 2) * m.RowPitch + (W / 2) * 4; + const float want[4] = {0.2f, 0.4f, 0.8f, 1.0f}; + int bad = 0; + if (c[0] != 0 || c[1] != 0 || c[2] != 0 || c[3] != 255) { + printf("FAIL corner pixel = %u %u %u %u, want 0 0 0 255\n", c[0], c[1], c[2], c[3]); bad = 1; + } + for (int ch = 0; ch < 4; ch++) + if (fabsf(want[ch] * 255.0f + (negative ? 100.0f : 0.0f) - (float)k[ch]) > 0.6f) bad = 1; + printf("centre pixel = %u %u %u %u\n", k[0], k[1], k[2], k[3]); + ID3D11DeviceContext_Unmap(ctx, (ID3D11Resource *)st, 0); + if (bad) { printf("FAIL pixels\n"); return 1; } + QueryPerformanceCounter(&prev); + } + } + printf("PASS frames=%d\n", frames); + return 0; +}