Compare commits

..
Author SHA1 Message Date
Ateş f0a45544ee Merge pull request #809 from MacosPorters/track-c/dispatchx-copyresourcex
d3d12_xs: implement DispatchX and CopyResourceX
2026-12-02 14:46:20 +03:00
ates 123afb2e9a Merge remote-tracking branch 'origin/main' into track-c/dispatchx-copyresourcex
Signed-off-by: ates <ahmetrizaates@hotmail.com>

# Conflicts:
#	CHANGELOG.md
#	shims/d3d12_xs/xbox_tables_generated.cpp
2026-12-02 14:42:49 +03:00
Ateş b805b2bd81 Merge pull request #808 from MacosPorters/track-c/806-allocate-from-command-allocator
d3d12_xs: AllocateFromCommandAllocatorX returns memory instead of null (#806)
2026-12-02 14:42:35 +03:00
ates 6d8c0874ab d3d12_xs: DispatchX knows every GDK dispatch flag; CopyResourceX logs flags it does not apply
Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-12-02 14:36:59 +03:00
ates 95ea34eeb0 d3d12_xs: implement DispatchX and CopyResourceX
Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-12-02 14:28:57 +03:00
7 changed files with 343 additions and 7 deletions

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ remains unsupported.
- Tests: `test_stub_caller`, plus unit tests for the report and the lookup.
### Fixed
- **`DispatchX` and `CopyResourceX` are implemented.** Both Xbox command-list slots (80 and 86) were logging stubs. `DispatchX` forwards to `Dispatch`; with `USE_THREAD_DIMENSIONS` the counts are threads, so they are divided (rounding up) by the bound compute shader's `[numthreads]`, read from its `PSV0` part when the pipeline is created. A thread-count dispatch on a pipeline whose `[numthreads]` is unknown is logged and skipped rather than run with wrong counts; unknown flags are logged. `CopyResourceX` forwards to `CopyResource`. New `test_d3d12_dispatch_copy_x` (the `[numthreads]` conversion is not covered by it).
- **`AllocateFromCommandAllocatorX` returns memory instead of null (#806).** The Xbox command-list slot 148 was a logging stub. The GDK's ray-tracing BVH builder takes its upload buffer from it, and on null it throws an exception the target build does not catch, which ended the run through the title's crash reporter (exit status 99). The slot now returns aligned memory carved from 1 MiB chunks owned by the list's command allocator: the chunks are reused when the allocator is reset and freed when it is released, and a non-power-of-two alignment is refused and logged. New `test_d3d12_allocate_from_allocator`.
- **`d3d12_xs`: host memory for read-only placed textures is mapped at first use** (#782).
- **Before:** every texture a title placed got a full host copy at once. On the target build's Series S runs, 23,105 live host textures held 5.9 GB while translated draws read 16 of them (24 MB).
+122
View File
@@ -25,6 +25,7 @@
#include "proxy.h"
#include "shader_gate.h"
#include <array>
#include <atomic>
#include <cstdio>
#include <cstring>
@@ -243,6 +244,42 @@ void log_extended(const char* kind, UINT count) {
* (null_render_skip), as every dispatch of untranslated compute is today. Serialized and derived
* pipelines still fail (refused_always_fail), so a title creates them from shaders instead.
*/
/*
* [numthreads] of compute pipelines, for DispatchX with USE_THREAD_DIMENSIONS (thread counts, not
* group counts). A DXIL container's PSV0 part carries it: a uint32 size, then PSVRuntimeInfo, whose
* version 2 adds NumThreadsX/Y/Z at byte 36 (after the 36 bytes of PSVRuntimeInfo1; public DXC
* DxilPipelineStateValidation.h, MIT; noted for console containers on #755).
*/
std::mutex g_threads_lock;
std::unordered_map<const void*, std::array<UINT, 3>> g_pipeline_threads; // host compute pipeline -> numthreads
std::unordered_map<const void*, const void*> g_list_pipeline; // host list -> bound host pipeline
bool psv0_numthreads(const void* bytes, size_t len, std::array<UINT, 3>* out) {
const auto* b = static_cast<const uint8_t*>(bytes);
auto u32 = [&](size_t at) {
uint32_t v = 0;
std::memcpy(&v, b + at, 4);
return v;
};
if (!b || len < 32 || std::memcmp(b, "DXBC", 4) != 0) return false;
const uint32_t count = u32(28);
if (count > 64 || 32 + 4ull * count > len) return false;
for (uint32_t i = 0; i < count; ++i) {
const uint32_t at = u32(32 + 4 * i);
if (at + 8ull > len || std::memcmp(b + at, "PSV0", 4) != 0) continue;
const uint32_t size = u32(at + 4);
if (at + 8ull + size > len || size < 4) return false;
const uint32_t info = u32(at + 8);
if (info < 48 || 4ull + 48 > size) return false;
const size_t base = at + 12;
(*out)[0] = u32(base + 36);
(*out)[1] = u32(base + 40);
(*out)[2] = u32(base + 44);
return (*out)[0] && (*out)[1] && (*out)[2];
}
return false;
}
HRESULT create_compute(void* self, const D3D12_COMPUTE_PIPELINE_STATE_DESC* desc, REFIID riid, void** ppv) {
if (!desc || !ppv) return E_INVALIDARG;
*ppv = nullptr;
@@ -253,6 +290,11 @@ HRESULT create_compute(void* self, const D3D12_COMPUTE_PIPELINE_STATE_DESC* desc
HRESULT hr = gate.admit("compute", "CS", desc->CS, admitted.CS);
if (SUCCEEDED(hr)) hr = device->CreateComputePipelineState(&admitted, riid, ppv);
if (FAILED(hr)) return refused(device, hr, "compute", riid, ppv, nullptr, true);
std::array<UINT, 3> threads{};
if (*ppv && psv0_numthreads(desc->CS.pShaderBytecode, desc->CS.BytecodeLength, &threads)) {
std::lock_guard<std::mutex> guard(g_threads_lock);
g_pipeline_threads[*ppv] = threads;
}
return bx_xbox_wrap_result(hr, riid, ppv);
}
@@ -349,11 +391,19 @@ bool bx_xbox_pipeline_is_null(ID3D12PipelineState* title) {
}
void bx_xbox_pipeline_forget(IUnknown* host) {
{
std::lock_guard<std::mutex> threads(g_threads_lock);
g_pipeline_threads.erase(host);
}
std::lock_guard<std::mutex> guard(g_lock);
erase(g_nulls, g_null_count, static_cast<NullPipelineState*>(static_cast<void*>(host)));
}
void bx_xbox_list_forget(IUnknown* host) {
{
std::lock_guard<std::mutex> threads(g_threads_lock);
g_list_pipeline.erase(host);
}
bx_xbox_rect_list_forget(static_cast<ID3D12GraphicsCommandList*>(static_cast<void*>(host)));
bx_console_list_forget(static_cast<ID3D12GraphicsCommandList*>(static_cast<void*>(host)));
bx_console_descriptors_reset(static_cast<ID3D12GraphicsCommandList*>(static_cast<void*>(host)));
@@ -413,6 +463,10 @@ void STDMETHODCALLTYPE bx_xbox_list_set_pipeline_state(void* self, ID3D12Pipelin
ID3D12PipelineState* host = host_pipeline(pso, &is_null, &console);
set_list_null(list, is_null, std::move(console));
bx_xbox_rect_note_pipeline(list, host);
{
std::lock_guard<std::mutex> guard(g_threads_lock);
g_list_pipeline[list] = is_null ? nullptr : host;
}
if (!is_null) list->SetPipelineState(host);
}
@@ -459,6 +513,74 @@ void STDMETHODCALLTYPE bx_xbox_list_dispatch(void* self, UINT x, UINT y, UINT z)
if (!list_skips(list, "Dispatch")) list->Dispatch(x, y, z);
}
/* DispatchX (GDK d3d12_xs.h): Dispatch plus D3D12XBOX_DISPATCH_FLAGS. Most flags steer the
console's wave launch (start at 0,0,0, ordered append, out-of-order waves, vector L1), which the
host does not have, so the dispatch is the PC one. USE_THREAD_DIMENSIONS (0x20) makes the counts
thread counts; turning them into group counts needs the bound shader's [numthreads], which this
layer does not know, so such a dispatch is skipped and logged rather than run with the wrong size. */
void STDMETHODCALLTYPE bx_xbox_list_dispatch_x(void* self, UINT x, UINT y, UINT z, UINT flags) {
BX_D3D_TRACE("XboxGraphicsCommandList", "DispatchX");
auto* list = bx_xbox_host<ID3D12GraphicsCommandList>(self);
if (list_skips(list, "DispatchX")) return;
constexpr UINT kUseThreadDimensions = 0x20;
if (flags & kUseThreadDimensions) {
// Thread counts: groups = ceil(threads / [numthreads]) of the bound pipeline.
std::array<UINT, 3> n{};
bool known = false;
{
std::lock_guard<std::mutex> guard(g_threads_lock);
auto bound = g_list_pipeline.find(list);
if (bound != g_list_pipeline.end() && bound->second) {
auto t = g_pipeline_threads.find(bound->second);
if (t != g_pipeline_threads.end()) {
n = t->second;
known = true;
}
}
}
if (!known) {
BX_STUB_LOG("d3d12_xs.dll", "DispatchX(thread dimensions, [numthreads] unknown)");
return;
}
x = (x + n[0] - 1) / n[0];
y = (y + n[1] - 1) / n[1];
z = (z + n[2] - 1) / n[2];
// A partial last group runs whole: its extra threads are not masked (the shader's own bounds
// checks, if any, apply).
}
// Every flag the GDK header defines. Other than the thread dimensions they tune how the console
// GPU launches and caches waves (ordering, ordered append, volatile L1 lines, wave size) and have
// no host equivalent.
constexpr UINT kKnown = 0x04 | 0x08 | 0x10 | kUseThreadDimensions | 0x40 | 0x80 | 0x200 | 0x400 | 0x800 | 0x8000;
if (flags & ~kKnown) {
static std::atomic<bool> logged{false};
if (!logged.exchange(true)) {
char detail[96];
std::snprintf(detail, sizeof(detail), "DispatchX flags 0x%x include bits this layer does not know; dispatched as Dispatch", flags);
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "dispatch_x", detail);
}
}
list->Dispatch(x, y, z);
}
/* CopyResourceX (GDK d3d12_xs.h): CopyResource plus D3D12XBOX_COPY_FLAGS, which steer the console's
compression metadata only, as for CopyTextureRegionX. */
void STDMETHODCALLTYPE bx_xbox_list_copy_resource_x(void* self, ID3D12Resource* dst, ID3D12Resource* src, UINT flags) {
BX_D3D_TRACE("XboxGraphicsCommandList", "CopyResourceX");
if (flags) {
// D3D12XBOX_COPY_FLAGS ask for console-side decompression, resummarizing or sample-to-red
// copies, which a plain host copy does not perform.
static std::atomic<int> logged{0};
if (logged.fetch_add(1) < 4) {
char detail[96];
std::snprintf(detail, sizeof(detail), "CopyResourceX flags 0x%x are not applied; copied as CopyResource", flags);
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "copy_resource_x", detail);
}
}
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyResource(bx_xbox_unwrap_as<ID3D12Resource>(dst),
bx_xbox_unwrap_as<ID3D12Resource>(src));
}
void STDMETHODCALLTYPE bx_xbox_list_execute_indirect(void* self, ID3D12CommandSignature* signature, UINT max_commands,
ID3D12Resource* arguments, UINT64 arguments_offset,
ID3D12Resource* count, UINT64 count_offset) {
+2
View File
@@ -145,6 +145,8 @@ CUSTOM = {
"DrawInstanced": "bx_xbox_list_draw_instanced",
"DrawIndexedInstanced": "bx_xbox_list_draw_indexed_instanced",
"Dispatch": "bx_xbox_list_dispatch",
"DispatchX": "bx_xbox_list_dispatch_x",
"CopyResourceX": "bx_xbox_list_copy_resource_x",
"ExecuteIndirect": "bx_xbox_list_execute_indirect"},
}
SHARED = {"QueryInterface": "bx_xbox_query_interface", "AddRef": "bx_xbox_add_ref",
+2
View File
@@ -336,6 +336,8 @@ void STDMETHODCALLTYPE bx_xbox_list_draw_instanced(void* self, UINT vertices, UI
void STDMETHODCALLTYPE bx_xbox_list_draw_indexed_instanced(void* self, UINT indices, UINT instances, UINT first_index,
INT base_vertex, UINT first_instance);
void STDMETHODCALLTYPE bx_xbox_list_dispatch(void* self, UINT x, UINT y, UINT z);
void STDMETHODCALLTYPE bx_xbox_list_dispatch_x(void* self, UINT x, UINT y, UINT z, UINT flags);
void STDMETHODCALLTYPE bx_xbox_list_copy_resource_x(void* self, ID3D12Resource* dst, ID3D12Resource* src, UINT flags);
void STDMETHODCALLTYPE bx_xbox_list_execute_indirect(void* self, ID3D12CommandSignature* signature, UINT max_commands,
ID3D12Resource* arguments, UINT64 arguments_offset,
ID3D12Resource* count, UINT64 count_offset);
+3 -7
View File
@@ -1417,8 +1417,6 @@ static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_78(void*) { BX_VTABLE_STUB
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_79(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 79u, "ReadGDSX"); return 0; }
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_80(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 80u, "DispatchX"); return 0; }
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_81(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 81u, "SetHiStencilStateX"); return 0; }
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_82(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 82u, "RSSetMSAAParametersX"); return 0; }
@@ -1427,8 +1425,6 @@ static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_83(void*) { BX_VTABLE_STUB
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_84(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 84u, "NopX"); return 0; }
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_86(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 86u, "CopyResourceX"); return 0; }
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_87(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 87u, "Deprecated_SetPredicationBufferX"); return 0; }
/* Xbox-only method */
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_88(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 88u, "Deprecated_AdvancePredicationX"); return 0; }
@@ -1646,13 +1642,13 @@ const void* const kGraphicsCommandListTable[] = {
reinterpret_cast<const void*>(&x_GraphicsCommandList_77),
reinterpret_cast<const void*>(&x_GraphicsCommandList_78),
reinterpret_cast<const void*>(&x_GraphicsCommandList_79),
reinterpret_cast<const void*>(&x_GraphicsCommandList_80),
reinterpret_cast<const void*>(&bx_xbox_list_dispatch_x),
reinterpret_cast<const void*>(&x_GraphicsCommandList_81),
reinterpret_cast<const void*>(&x_GraphicsCommandList_82),
reinterpret_cast<const void*>(&x_GraphicsCommandList_83),
reinterpret_cast<const void*>(&x_GraphicsCommandList_84),
reinterpret_cast<const void*>(&bx_xbox_list_copy_texture_region_x),
reinterpret_cast<const void*>(&x_GraphicsCommandList_86),
reinterpret_cast<const void*>(&bx_xbox_list_copy_resource_x),
reinterpret_cast<const void*>(&x_GraphicsCommandList_87),
reinterpret_cast<const void*>(&x_GraphicsCommandList_88),
reinterpret_cast<const void*>(&x_GraphicsCommandList_89),
@@ -1733,7 +1729,7 @@ const IID kGraphicsCommandListIids[] = {
{0xc4fec28f, 0x7966, 0x4e95, {0x9f, 0x94, 0xf4, 0x31, 0xcb, 0x56, 0xc3, 0xb8}}, /* ID3D12Object */
};
/* ID3D12GraphicsCommandList8: 150 slots (53 forwarded, 39 shared, 58 stubs); hosted on ID3D12GraphicsCommandList7. */
/* ID3D12GraphicsCommandList8: 150 slots (53 forwarded, 41 shared, 56 stubs); hosted on ID3D12GraphicsCommandList7. */
const IID kHostGraphicsCommandList = __uuidof(ID3D12GraphicsCommandList7);
} // namespace
@@ -0,0 +1,5 @@
# DispatchX and CopyResourceX follow Dispatch and CopyResource. Needs a host with the Xbox-layout
# device interface; skips without.
add_executable(test_d3d12_dispatch_copy_x main.cpp)
target_compile_definitions(test_d3d12_dispatch_copy_x PRIVATE WIDL_EXPLICIT_AGGREGATE_RETURNS)
target_link_libraries(test_d3d12_dispatch_copy_x PRIVATE d3d12_xs kernel32 uuid)
@@ -0,0 +1,208 @@
/*
* Conformance test: DispatchX and CopyResourceX (Xbox-layout command-list slots 80 and 86).
*
* Contract (authorized GDK d3d12_xs.h and reference pages): DispatchX(x, y, z, Flags) is Dispatch
* with D3D12XBOX_DISPATCH_FLAGS; CopyResourceX(dst, src, Flags) is CopyResource with
* D3D12XBOX_COPY_FLAGS ("copies the entire contents of the source resource to the destination").
* Both were stubs; the target build calls them once it leaves its loading screen.
*
* Checked, from the contract and the program alone:
* 1. CopyResourceX copies a 64 KiB pattern from an upload buffer into a readback buffer.
* 2. With a compute pipeline the shim refuses bound (it comes back as the null pipeline, #722),
* DispatchX with flags 0 and with USE_THREAD_DIMENSIONS (0x20) records nothing harmful: the
* list executes and completes, and the dispatches are skipped as Dispatch's are.
* 3. Neither method is logged as an unimplemented slot.
*
* Slots (Xbox-layout tables, ADR 0009): 2 Release; device 9 CreateCommandAllocator,
* 27 CreateCommittedResource, 36 CreateFence, 48 CreateCommandQueueX, 62 CreateComputePipelineStateX,
* 73 CreateCommandListX; resource 8 Map; command list 9 Close, 25 SetPipelineState, 80 DispatchX,
* 86 CopyResourceX; queue 10 ExecuteCommandLists, 16 Signal; fence 9 SetEventOnCompletion.
*/
#include <windows.h>
#include <d3d12.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(PVOID adapter, const void* params, REFIID riid,
void** ppv);
namespace {
constexpr int kRelease = 2;
constexpr int kDevCreateCommandAllocator = 9, kDevCreateCommittedResource = 27, kDevCreateFence = 36;
constexpr int kDevCreateCommandQueueX = 48, kDevCreateComputePipelineStateX = 62, kDevCreateCommandListX = 73;
constexpr int kResMap = 8;
constexpr int kListClose = 9, kListSetPipelineState = 25, kListDispatchX = 80, kListCopyResourceX = 86;
constexpr int kQueueExecuteCommandLists = 10, kQueueSignal = 16, kFenceSetEventOnCompletion = 9;
constexpr UINT64 kBytes = 64 * 1024;
int g_checks = 0, g_failed = 0;
#define CHECK(cond, ...) \
do { \
++g_checks; \
if (!(cond)) { \
++g_failed; \
std::fprintf(stderr, "FAIL: test_d3d12_dispatch_copy_x:%d: ", __LINE__); \
std::fprintf(stderr, __VA_ARGS__); \
std::fprintf(stderr, "\n"); \
} \
} while (0)
template <typename Fn>
Fn slot(void* object, int index) {
return reinterpret_cast<Fn>((*static_cast<void* const* const*>(object))[index]);
}
struct QueueDescX {
D3D12_COMMAND_LIST_TYPE Type;
UINT Flags, EngineOrPipeIndex, QueueIndex;
};
struct ListDescX {
D3D12_COMMAND_LIST_TYPE Type;
UINT Flags, NodeMask, ResourceBarrierBatchSize;
};
using ReleaseFn = ULONG(STDMETHODCALLTYPE*)(void*);
using CreateQueueXFn = HRESULT(STDMETHODCALLTYPE*)(void*, const QueueDescX*, REFIID, void**);
using CreateAllocatorFn = HRESULT(STDMETHODCALLTYPE*)(void*, D3D12_COMMAND_LIST_TYPE, REFIID, void**);
using CreateListXFn = HRESULT(STDMETHODCALLTYPE*)(void*, const ListDescX*, void*, void*, REFIID, void**);
using CreateCommittedFn = HRESULT(STDMETHODCALLTYPE*)(void*, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS,
const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_STATES,
const D3D12_CLEAR_VALUE*, REFIID, void**);
using CreateComputeXFn = HRESULT(STDMETHODCALLTYPE*)(void*, const D3D12_COMPUTE_PIPELINE_STATE_DESC*, UINT, const void*, REFIID,
void**);
using CreateFenceFn = HRESULT(STDMETHODCALLTYPE*)(void*, UINT64, D3D12_FENCE_FLAGS, REFIID, void**);
using MapFn = HRESULT(STDMETHODCALLTYPE*)(void*, UINT, const D3D12_RANGE*, void**);
using CloseFn = HRESULT(STDMETHODCALLTYPE*)(void*);
using SetPsoFn = void(STDMETHODCALLTYPE*)(void*, void*);
using DispatchXFn = void(STDMETHODCALLTYPE*)(void*, UINT, UINT, UINT, UINT);
using CopyResourceXFn = void(STDMETHODCALLTYPE*)(void*, void*, void*, UINT);
using ExecuteFn = void(STDMETHODCALLTYPE*)(void*, UINT, void* const*);
using QueueSignalFn = HRESULT(STDMETHODCALLTYPE*)(void*, void*, UINT64);
using FenceEventFn = HRESULT(STDMETHODCALLTYPE*)(void*, UINT64, HANDLE);
std::string read_log() {
char path[MAX_PATH];
const DWORD n = GetEnvironmentVariableA("BX_LOG_FILE", path, sizeof(path));
if (n == 0 || n >= sizeof(path)) return {};
std::string text;
if (FILE* f = std::fopen(path, "rb")) {
char buffer[4096];
size_t got;
while ((got = std::fread(buffer, 1, sizeof(buffer), f)) > 0) text.append(buffer, got);
std::fclose(f);
}
return text;
}
} // namespace
int main() {
setvbuf(stdout, nullptr, _IONBF, 0);
SetEnvironmentVariableA("BX_NULL_RENDER", nullptr);
alignas(8) unsigned char title_params[80] = {};
const UINT version = 0x20004;
std::memcpy(title_params, &version, sizeof(version));
void* dev = nullptr;
HRESULT hr = D3D12XboxCreateDevice(nullptr, title_params, __uuidof(ID3D12Device), &dev);
if (hr == E_NOINTERFACE) {
std::printf("SKIP: test_d3d12_dispatch_copy_x: host lacks the Xbox-layout device interface (E_NOINTERFACE)\n");
return 77;
}
CHECK(hr == S_OK && dev, "Xbox-layout device creation hr=0x%08lx", static_cast<unsigned long>(hr));
if (!dev) return 1;
const std::string before = read_log();
const QueueDescX qdesc{D3D12_COMMAND_LIST_TYPE_DIRECT, 0, 0, 0};
const ListDescX ldesc{D3D12_COMMAND_LIST_TYPE_DIRECT, 0, 0, 0};
void* queue = nullptr;
void* allocator = nullptr;
void* list = nullptr;
void* fence = nullptr;
slot<CreateQueueXFn>(dev, kDevCreateCommandQueueX)(dev, &qdesc, __uuidof(ID3D12CommandQueue), &queue);
slot<CreateAllocatorFn>(dev, kDevCreateCommandAllocator)(dev, D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator),
&allocator);
if (allocator) {
slot<CreateListXFn>(dev, kDevCreateCommandListX)(dev, &ldesc, allocator, nullptr, __uuidof(ID3D12GraphicsCommandList), &list);
}
slot<CreateFenceFn>(dev, kDevCreateFence)(dev, 0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), &fence);
D3D12_HEAP_PROPERTIES upload_heap{}, readback_heap{};
upload_heap.Type = D3D12_HEAP_TYPE_UPLOAD;
readback_heap.Type = D3D12_HEAP_TYPE_READBACK;
D3D12_RESOURCE_DESC buf{};
buf.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
buf.Width = kBytes;
buf.Height = 1;
buf.DepthOrArraySize = 1;
buf.MipLevels = 1;
buf.SampleDesc.Count = 1;
buf.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
auto committed = slot<CreateCommittedFn>(dev, kDevCreateCommittedResource);
void* src = nullptr;
void* dst = nullptr;
committed(dev, &upload_heap, D3D12_HEAP_FLAG_NONE, &buf, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &src);
committed(dev, &readback_heap, D3D12_HEAP_FLAG_NONE, &buf, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, __uuidof(ID3D12Resource), &dst);
// A compute pipeline the shader gate refuses: the null pipeline comes back (#722).
static const uint8_t kRefused[16] = {0x44, 0x58, 0x42, 0x43};
D3D12_COMPUTE_PIPELINE_STATE_DESC cdesc{};
cdesc.CS.pShaderBytecode = kRefused;
cdesc.CS.BytecodeLength = sizeof(kRefused);
void* pso = nullptr;
slot<CreateComputeXFn>(dev, kDevCreateComputePipelineStateX)(dev, &cdesc, 0, nullptr, __uuidof(ID3D12PipelineState), &pso);
if (!queue || !allocator || !list || !fence || !src || !dst || !pso) {
std::fprintf(stderr, "FAIL: test_d3d12_dispatch_copy_x: setup\n");
return 1;
}
void* mapped = nullptr;
CHECK(slot<MapFn>(src, kResMap)(src, 0, nullptr, &mapped) == S_OK && mapped, "upload Map");
if (mapped) {
for (UINT64 i = 0; i < kBytes; ++i) static_cast<uint8_t*>(mapped)[i] = static_cast<uint8_t>(i * 11 + 5);
}
slot<CopyResourceXFn>(list, kListCopyResourceX)(list, dst, src, 0x60); // flags steer compression metadata only
slot<SetPsoFn>(list, kListSetPipelineState)(list, pso);
slot<DispatchXFn>(list, kListDispatchX)(list, 2, 1, 1, 0);
slot<DispatchXFn>(list, kListDispatchX)(list, 64, 1, 1, 0x20);
CHECK(slot<CloseFn>(list, kListClose)(list) == S_OK, "list Close");
void* lists[] = {list};
slot<ExecuteFn>(queue, kQueueExecuteCommandLists)(queue, 1, lists);
CHECK(slot<QueueSignalFn>(queue, kQueueSignal)(queue, fence, 1) == S_OK, "queue Signal");
HANDLE done = CreateEventW(nullptr, FALSE, FALSE, nullptr);
CHECK(slot<FenceEventFn>(fence, kFenceSetEventOnCompletion)(fence, 1, done) == S_OK, "fence event");
CHECK(WaitForSingleObject(done, 5000) == WAIT_OBJECT_0, "the list with the copy and both dispatches completes");
CloseHandle(done);
void* data = nullptr;
const D3D12_RANGE all{0, static_cast<SIZE_T>(kBytes)};
CHECK(slot<MapFn>(dst, kResMap)(dst, 0, &all, &data) == S_OK && data, "readback Map");
UINT64 wrong = 0;
if (data) {
for (UINT64 i = 0; i < kBytes; ++i) {
if (static_cast<const uint8_t*>(data)[i] != static_cast<uint8_t>(i * 11 + 5)) ++wrong;
}
}
CHECK(data && wrong == 0, "CopyResourceX copied the whole buffer (%llu of %llu bytes differ)", static_cast<unsigned long long>(wrong),
static_cast<unsigned long long>(kBytes));
const std::string log = read_log();
if (!log.empty()) {
const std::string mine = log.substr(before.size() <= log.size() ? before.size() : 0);
CHECK(mine.find("XboxGraphicsCommandList[80]::DispatchX") == std::string::npos, "DispatchX is not an unimplemented slot");
CHECK(mine.find("XboxGraphicsCommandList[86]::CopyResourceX") == std::string::npos, "CopyResourceX is not an unimplemented slot");
CHECK(mine.find("null pipeline bound") != std::string::npos, "the dispatches with the null pipeline are skipped, as Dispatch's are");
} else {
std::printf("note: no BX_LOG_FILE; the log checks did not run\n");
}
for (void* object : {pso, dst, src, fence, list, allocator, queue}) slot<ReleaseFn>(object, kRelease)(object);
slot<ReleaseFn>(dev, kRelease)(dev);
if (g_failed) {
std::fprintf(stderr, "FAIL: test_d3d12_dispatch_copy_x (%d of %d checks failed)\n", g_failed, g_checks);
return 1;
}
std::printf("PASS: test_d3d12_dispatch_copy_x (%d of %d checks passed)\n", g_checks, g_checks);
return 0;
}