mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-12-02 14:22:27 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f120ecbc1c | ||
|
|
3f0cce3b34 | ||
|
|
b2a10cb233 | ||
|
|
6797d664d2 | ||
|
|
38e7a6e13f | ||
|
|
d891a17081 | ||
|
|
34f97fd2d6 | ||
|
|
bc66413f29 | ||
|
|
f98bb2de50 | ||
|
|
11c8bf8209 | ||
|
|
3e8805c986 | ||
|
|
5fd69c0be2 | ||
|
|
7beff3e8b6 | ||
|
|
8a88e018a1 | ||
|
|
c76957b7c7 | ||
|
|
0a71d3366b | ||
|
|
2736f24ca2 |
No files matched your search
@@ -18,6 +18,25 @@ remains unsupported.
|
||||
- Tests: `test_stub_caller`, plus unit tests for the report and the lookup.
|
||||
|
||||
### Fixed
|
||||
- **`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).
|
||||
- **Now:** a read-only 2D texture of at least 64 KiB (not a render target, depth-stencil or UAV texture) is a reserved resource with no memory. One heap holding all its tiles is mapped:
|
||||
- when its contents are first uploaded for a draw, on the submitting queue ahead of the upload;
|
||||
- or when a title's `CopyTextureRegion(X)`, `CopyResource` or `ResolveSubresource` writes it, on a queue of the shim's own, waited for.
|
||||
- **Unchanged:** the resource and its views stay the same, and unmapped tiles read as zero, as a texture never filled did before. The memory is released with the texture.
|
||||
- **Measured:** on D3DMetal, 2,000 unmapped 1024×1024 BC1 textures add 9 MB, and 1.3 GB once mapped. On a Series S run of the target build the footprint at 2 minutes was 7.2 GB, against 12 GB before.
|
||||
- **Settings:** `BX_LAZY_TEXTURES=0` restores committed textures; `BX_LAZY_TEXTURES_MIN` changes the 64 KiB threshold.
|
||||
- **Generated tables:** `CopyResource` and `ResolveSubresource` now go through hand-written slots, regenerated with `gen_xbox_tables.py`.
|
||||
- **Tests:** new `test_d3d12_lazy_texture_memory`, which also fails when a copy into a texture without memory is not mapped first; new `test_d3d12_console_texture_contents_reserved`, the texture-contents test with every texture reserved.
|
||||
- **GPU value waits recorded after a write no longer run before it.** Since #791 every `WaitOnBitValueX`-family wait of a command list ran before the list. A title that makes two queues hand over to each other (one list writes a value and then waits for the other's, the other waits and then writes) therefore waited in a cycle that only the 10 s timeout broke, on every frame. Waits recorded before the list's first write still run in front of it; a wait recorded after a write now runs after the list's GPU work and those writes, and later writes are stored once that wait holds. `test_d3d12_gpu_value_order` gains the two-queue case.
|
||||
- **`XCRDAPI`: the Xbox DirectStorage interfaces have the `dstorage_xs.h` slots, and a partial zlib read no longer allocates up to 2 GiB** (#769).
|
||||
- **Factory** (`IDStorageFactoryX`/`X1`/`X2`): slot 3 `CreateQueue(DSTORAGE_QUEUE_DESC*)` creates a queue with auto-submit on, where it used to return `E_NOTIMPL`. Slot 6 is `SetCpuAffinity` and returns an HRESULT; it used to be a `void` `SetDebugFlags`. Slot 7 is `SetDebugFlags`. Slot 9 `GetStats` writes a zeroed 256-byte `DSTORAGE_STATS`, logged as a stub. Slot 10 is `CreateQueue1`, which also checks `DSTORAGE_PRIORITY`. Slots 11 and 12, which don't exist, are gone. `DStorageGetFactory` and `QueryInterface` accept all three factory IIDs.
|
||||
- **File:** slot 4 is `GetHandle`, a new handle the caller closes. It used to be `GetFileInformation`, which returned 0 as the "handle".
|
||||
- **Queue** (`IDStorageQueueX`/`X1`/`X2`): slot 10 `RetrieveErrorRecord` fills the 656-byte `DSTORAGE_ERROR_RECORD`'s count, first HRESULT and command type. Slot 11 `Query` and slot 13 `Query1` report the queue. Slot 12 `EnqueueSetEvent` sets the event, so a waiter wakes; it used to be a stub. The queue answers all three queue IIDs.
|
||||
- **Requests** are read as the 0x48-byte `DSTORAGE_REQUEST`, including `IntermediateSize`, `CancellationTag` and `Name`. `CancelRequestsWithTag` logs its mask and value; requests still run, as the header allows.
|
||||
- **Partial zlib reads:** a read of the first part of a compressed block decodes the whole stream through a 32 KiB window, still checking Adler-32, and writes only `DestinationSize` bytes. It used to allocate `64 × DestinationSize + 256 KiB`, up to 2 GiB.
|
||||
- **Tests:** `test_xcrdapi_xbox_slots` covers the corrected slots and the bounded partial read.
|
||||
- **`xgameruntime`: a delayed task-queue callback no longer holds back the callbacks submitted after it** (#775, part 3). `XTaskQueueSubmitDelayedCallback` put the task in the port's FIFO, and the port waited for it to be due. So one multi-second retry timer blocked every later callback on that port, and on a pool lane a thread-pool thread slept meanwhile. In the GDK the delay elapses before the callback is submitted. Delayed tasks now wait outside the FIFO and a thread-pool timer moves them in when due, so a callback submitted later without a delay runs first. Delayed tasks still pending when the queue closes are drained or cancelled like the rest. `test_xgameruntime` checks both a manual and a serialized queue.
|
||||
- **`d3d12_xs`: `D3D12ConfigureVirtualMemoryX` accepts the DEFAULT page count and the legacy lower bound; `WaitFrameEventX` reports PRIMARY** (#772). These follow the authorized GDK header `d3d12_xs.h`.
|
||||
- `PageTableMemory4MBPageCount` = `D3D12XBOX_PAGE_TABLE_MEMORY_4MB_PAGE_COUNT_DEFAULT` (0) selects the default of 5 pages. It used to be refused with `E_INVALIDARG`.
|
||||
@@ -56,6 +75,7 @@ remains unsupported.
|
||||
- **`gfx10-translate`: scan all `XBCH` candidates and support version 2/3 payloads** (#721). `container_code` previously stopped at the first `b"XBCH"` byte sequence in a container, so incidental `b"XBCH"` bytes earlier in the blob (for example in `STAT` or `DXIL` bitcode) caused it to read garbage sizes and fail with `XBCH record: compressed size past the end`. It now scans all `b"XBCH"` occurrences, validates the 24-byte header, bounds, version (`2` uncompressed `FSSM`, `1` and `3` zlib-compressed `FSSM`), and `FSSM` magic, and skips non-matching candidates. Covered by synthetic unit tests in `gfx10_translate::container::tests`.
|
||||
|
||||
### Changed
|
||||
- **`d3d12_xs` looks up title-memory host resources through indexes instead of scanning them all (#782).** A title can keep 100,000+ of them alive, and every view creation, root-buffer bind, texture-contents check, `Map` and submit-time sync scanned the whole table under one lock. Lookups by host pointer now use a hash map, lookups by address an ordered map (plus a short list of resources of 1 MiB or more), and the submit-time sync visits only mapped buffers. Behaviour is unchanged; the existing placed-memory and console tests cover every replaced lookup. On the target build's post-loading phase this roughly doubled the rate of view creation; creating the host resources themselves remains the larger cost there.
|
||||
- **Open-source DXC (`dxcompiler.dll`) replaces the GDK's shader compiler as the primary source** (#757, ADR 0010).
|
||||
- `.github/workflows/dxc-build.yml` builds x64 Release `dxcompiler.dll` from `microsoft/DirectXShaderCompiler` at pinned tag `v1.9.2609` on a Windows runner and uploads it as a workflow artifact.
|
||||
- `bx-cli setup --dxc-dir <dir>` (and `BX_DXC_DIR`) records the local open-source DXC folder in `local.conf` (`dxc_dir`).
|
||||
|
||||
@@ -351,7 +351,7 @@ void bx_console_submit(ID3D12CommandQueue* queue, ID3D12CommandList* const* host
|
||||
if (draws.empty()) return;
|
||||
UploadContext* ctx = queue ? upload_context(queue) : nullptr;
|
||||
std::unique_lock<std::mutex> held;
|
||||
BxTextureUploads uploads{nullptr, nullptr, 0, false};
|
||||
BxTextureUploads uploads{nullptr, nullptr, 0, false, nullptr};
|
||||
UINT turn = 0;
|
||||
if (ctx) {
|
||||
held = std::unique_lock<std::mutex>(ctx->lock);
|
||||
@@ -361,7 +361,7 @@ void bx_console_submit(ID3D12CommandQueue* queue, ID3D12CommandList* const* host
|
||||
WaitForSingleObject(ctx->event, 2000);
|
||||
}
|
||||
if (SUCCEEDED(ctx->allocators[turn]->Reset()) && SUCCEEDED(ctx->list->Reset(ctx->allocators[turn], nullptr))) {
|
||||
uploads = BxTextureUploads{ctx->list, ctx->fence, ctx->next, false};
|
||||
uploads = BxTextureUploads{ctx->list, ctx->fence, ctx->next, false, queue};
|
||||
}
|
||||
}
|
||||
std::unordered_set<uint64_t> done; // entries written for this submission
|
||||
|
||||
@@ -16,10 +16,15 @@
|
||||
* A wait the worker cannot satisfy within 10 s is released anyway (logged), as is a GPU fence
|
||||
* that does not complete; a host queue never stays blocked for good.
|
||||
*
|
||||
* Accepted difference: inside one list, its waits are treated as being at its start and its
|
||||
* writes at its end (a host command list cannot be split once recorded). A write is never seen
|
||||
* before the work in front of it; a wait in a list on a write earlier in the same list is
|
||||
* released after 10 s.
|
||||
* Accepted difference: a host command list cannot be split once recorded, so inside one list a
|
||||
* write is stored at the list's end, and a wait is placed at the list's start when no write was
|
||||
* recorded before it in that list, or at the list's end (after that list's writes are stored) when
|
||||
* one was. A write is never seen before the work in front of it. A wait recorded after a write
|
||||
* stays after it: a two-queue handshake, where one list writes then waits for the other queue
|
||||
* while the other waits then writes, completes instead of each queue holding the other for the
|
||||
* 10 s release (target build, #806 follow-up). Writes recorded after such a wait are stored only
|
||||
* once it holds. The price is that GPU work recorded after such a wait in the same list may run
|
||||
* before the wait holds.
|
||||
*/
|
||||
#include "xbox_facade.h"
|
||||
|
||||
@@ -272,25 +277,45 @@ void execute(void* self, UINT count, ID3D12CommandList* const* lists) {
|
||||
std::lock_guard<std::mutex> guard(s->submit);
|
||||
UINT start = 0;
|
||||
for (UINT i = 0; i < count; ++i) {
|
||||
bool has_wait = false, has_write = false;
|
||||
for (const Write& w : ops[i]) (w.wait ? has_wait : has_write) = true;
|
||||
if (has_wait) {
|
||||
// In recording order: waits before the list's first write go in front of the list; the
|
||||
// writes and any later waits go after it, the writes first.
|
||||
bool has_pre_wait = false, has_write = false, has_post_wait = false;
|
||||
for (const Write& w : ops[i]) {
|
||||
if (!w.wait) has_write = true;
|
||||
else if (has_write) has_post_wait = true;
|
||||
else has_pre_wait = true;
|
||||
}
|
||||
if (has_pre_wait) {
|
||||
if (i > start) queue->ExecuteCommandLists(i - start, host.data() + start);
|
||||
start = i;
|
||||
for (const Write& w : ops[i]) {
|
||||
if (w.wait) enqueue_wait(s, w);
|
||||
if (!w.wait) break;
|
||||
enqueue_wait(s, w);
|
||||
}
|
||||
}
|
||||
if (has_write) {
|
||||
queue->ExecuteCommandLists(i + 1 - start, host.data() + start);
|
||||
start = i + 1;
|
||||
// The writes up to the list's first later wait are stored when the GPU has finished
|
||||
// the list; each later wait follows, and the writes recorded after it are stored only
|
||||
// once it holds (same GPU fence value: the list is already done by then).
|
||||
const uint64_t done = ++s->gpu_value;
|
||||
queue->Signal(s->gpu, done);
|
||||
QueueState::Item item;
|
||||
item.value = ++s->gpu_value;
|
||||
item.value = done;
|
||||
bool seen_write = false;
|
||||
for (const Write& w : ops[i]) {
|
||||
if (!w.wait) item.writes.push_back(w);
|
||||
if (!w.wait) {
|
||||
seen_write = true;
|
||||
item.writes.push_back(w);
|
||||
} else if (seen_write) {
|
||||
if (!item.writes.empty()) s->push(std::move(item));
|
||||
item = QueueState::Item{};
|
||||
item.value = done;
|
||||
enqueue_wait(s, w);
|
||||
}
|
||||
}
|
||||
queue->Signal(s->gpu, item.value);
|
||||
s->push(std::move(item));
|
||||
if (!item.writes.empty()) s->push(std::move(item));
|
||||
}
|
||||
}
|
||||
if (start < count) queue->ExecuteCommandLists(count - start, host.data() + start);
|
||||
|
||||
@@ -427,6 +427,7 @@ HRESULT STDMETHODCALLTYPE bx_xbox_list_reset(void* self, ID3D12CommandAllocator*
|
||||
bx_console_descriptors_reset(list);
|
||||
bx_xbox_bound_memory_reset(list);
|
||||
bx_xbox_rect_list_reset(list, host);
|
||||
bx_xbox_list_note_allocator(list, bx_xbox_unwrap_as(allocator));
|
||||
return list->Reset(bx_xbox_unwrap_as(allocator), host);
|
||||
}
|
||||
|
||||
@@ -546,6 +547,129 @@ void STDMETHODCALLTYPE bx_xbox_list_sync_x(void* self, UINT, UINT, D3D12_GPU_VIR
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->ResourceBarrier(1, &barrier);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
/*
|
||||
* Memory from a command allocator (#806). AllocateFromCommandAllocatorX (GDK d3d12_xs.h,
|
||||
* ID3D12GraphicsCommandList8) returns memory "with the same lifetime as the associated command
|
||||
* list" whose address "is a valid GPU pointer and may be cast to D3D12_GPU_VIRTUAL_ADDRESS"; null
|
||||
* only when memory is unavailable. The target build's ray-tracing BVH builder (the GDK's XDXR
|
||||
* builder) takes its upload buffer from it and throws when it gets null, which ended the process.
|
||||
*
|
||||
* Here the memory is plain CPU memory, carved from chunks owned by the list's command allocator.
|
||||
* Its address reaches the GPU the way any title memory does: a root buffer in plain title memory
|
||||
* gets a host buffer filled at submission (descriptor_heaps.cpp). A command allocator may only be
|
||||
* reset once the GPU has finished its lists, so its chunks are reused then, and freed with it. A
|
||||
* list whose allocator this layer did not see keeps a pool of its own until it is released.
|
||||
*/
|
||||
namespace {
|
||||
|
||||
struct AllocatorChunk {
|
||||
uint8_t* base;
|
||||
size_t size;
|
||||
size_t used;
|
||||
};
|
||||
std::mutex g_allocator_memory_lock;
|
||||
std::unordered_map<const void*, std::vector<AllocatorChunk>> g_allocator_memory; // allocator (or list) -> chunks
|
||||
std::unordered_map<const void*, const void*> g_list_allocator; // host list -> host allocator
|
||||
constexpr size_t kAllocatorChunkBytes = 1u << 20;
|
||||
std::atomic<uint64_t> g_allocator_bytes{0};
|
||||
|
||||
void free_chunks_locked(const void* owner) {
|
||||
auto it = g_allocator_memory.find(owner);
|
||||
if (it == g_allocator_memory.end()) return;
|
||||
for (const AllocatorChunk& c : it->second) {
|
||||
g_allocator_bytes -= c.size;
|
||||
VirtualFree(c.base, 0, MEM_RELEASE);
|
||||
}
|
||||
g_allocator_memory.erase(it);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void bx_xbox_list_note_allocator(const void* host_list, const void* host_allocator) {
|
||||
if (!host_list || !host_allocator) return;
|
||||
std::lock_guard<std::mutex> guard(g_allocator_memory_lock);
|
||||
g_list_allocator[host_list] = host_allocator;
|
||||
}
|
||||
|
||||
void bx_xbox_command_memory_forget(const void* host) {
|
||||
std::lock_guard<std::mutex> guard(g_allocator_memory_lock);
|
||||
free_chunks_locked(host);
|
||||
g_list_allocator.erase(host);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* STDMETHODCALLTYPE bx_xbox_list_allocate_from_command_allocator_x(void* self, UINT size, UINT alignment) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "AllocateFromCommandAllocatorX");
|
||||
auto* list = bx_xbox_host<ID3D12GraphicsCommandList>(self);
|
||||
const size_t align = alignment ? alignment : 16;
|
||||
if ((align & (align - 1)) != 0 || align > kAllocatorChunkBytes) {
|
||||
static std::atomic<int> logged{0};
|
||||
if (logged.fetch_add(1) < 4) {
|
||||
char detail[128];
|
||||
std::snprintf(detail, sizeof(detail), "alignment %u is not a power of two up to 1 MiB; null returned", alignment);
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "allocate_from_allocator", detail);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(g_allocator_memory_lock);
|
||||
auto owner_it = g_list_allocator.find(list);
|
||||
const void* owner = owner_it != g_list_allocator.end() ? owner_it->second : list;
|
||||
if (owner == list) {
|
||||
static std::atomic<bool> noted{false};
|
||||
if (!noted.exchange(true)) {
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "allocate_from_allocator",
|
||||
"a list whose command allocator was not seen: its memory is kept until the list is released");
|
||||
}
|
||||
}
|
||||
std::vector<AllocatorChunk>& chunks = g_allocator_memory[owner];
|
||||
for (AllocatorChunk& c : chunks) {
|
||||
const size_t at = (reinterpret_cast<uintptr_t>(c.base) + c.used + align - 1) / align * align - reinterpret_cast<uintptr_t>(c.base);
|
||||
if (at + size <= c.size) {
|
||||
c.used = at + size;
|
||||
return c.base + at;
|
||||
}
|
||||
}
|
||||
const size_t bytes = size + align > kAllocatorChunkBytes ? (size + align + 0xFFFF) & ~static_cast<size_t>(0xFFFF)
|
||||
: kAllocatorChunkBytes;
|
||||
auto* base = static_cast<uint8_t*>(VirtualAlloc(nullptr, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
|
||||
if (!base) {
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "allocate_from_allocator", "VirtualAlloc failed; null returned");
|
||||
return nullptr;
|
||||
}
|
||||
const uint64_t total = g_allocator_bytes.fetch_add(bytes) + bytes;
|
||||
const size_t at = (reinterpret_cast<uintptr_t>(base) + align - 1) / align * align - reinterpret_cast<uintptr_t>(base);
|
||||
chunks.push_back(AllocatorChunk{base, bytes, at + size});
|
||||
static std::atomic<uint64_t> made{0};
|
||||
const uint64_t n = ++made;
|
||||
if ((n & (n - 1)) == 0) {
|
||||
char detail[160];
|
||||
std::snprintf(detail, sizeof(detail), "chunk of %zu bytes for a command allocator; chunks=%llu live_bytes=%llu", bytes,
|
||||
static_cast<unsigned long long>(n), static_cast<unsigned long long>(total));
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "allocate_from_allocator", detail);
|
||||
}
|
||||
return base + at;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE bx_xbox_allocator_reset(void* self) {
|
||||
BX_D3D_TRACE("XboxCommandAllocator", "Reset");
|
||||
auto* allocator = bx_xbox_host<ID3D12CommandAllocator>(self);
|
||||
const HRESULT hr = allocator->Reset();
|
||||
if (FAILED(hr)) {
|
||||
bx_xbox_log_host_failure("XboxCommandAllocator", "Reset", hr);
|
||||
return hr;
|
||||
}
|
||||
// The GPU has finished every list recorded with this allocator: its memory can be handed out again.
|
||||
std::lock_guard<std::mutex> guard(g_allocator_memory_lock);
|
||||
auto it = g_allocator_memory.find(allocator);
|
||||
if (it != g_allocator_memory.end()) {
|
||||
for (AllocatorChunk& c : it->second) c.used = 0;
|
||||
}
|
||||
return hr;
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE bx_xbox_allocator_set_callback_x(void*, void* callback, UINT64) {
|
||||
BX_D3D_TRACE("XboxCommandAllocator", "SetAllocatorCallbackX");
|
||||
static std::atomic<bool> logged{false};
|
||||
|
||||
+380
-48
@@ -27,9 +27,12 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
extern "C" __declspec(dllimport) VOID WINAPI XMemFree(PVOID address, ULONGLONG attributes);
|
||||
@@ -47,8 +50,133 @@ struct Shadow {
|
||||
bool implicit = false; // made for a view of placed memory (implicit_shadow); owned by this table
|
||||
bool simultaneous = false; // texture made with D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS
|
||||
uint64_t span = 0; // implicit: the bytes of title memory the resource covers
|
||||
bool reserved = false; // texture made as a reserved resource: memory mapped at first use
|
||||
};
|
||||
|
||||
/*
|
||||
* Texture memory on first use (#782). The host copy of every texture a title places costs as much
|
||||
* as the texture, but only the textures a draw reads ever get contents: 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). A read-only 2D texture of at least 64 KiB is therefore made as a reserved resource (no
|
||||
* memory: 2,000 unmapped 1024x1024 BC1 textures add 9 MB on D3DMetal, mapped 1.3 GB). Its memory,
|
||||
* one heap holding every tile, is mapped when its contents are first uploaded for a draw (on the
|
||||
* submitting queue, ahead of the upload) or when a title's GPU copy or resolve writes it (on a
|
||||
* queue of this layer's own, waited for). The resource and every view of it stay the same. Unmapped
|
||||
* tiles read as zero, as a texture never filled did before. BX_LAZY_TEXTURES=0 makes every texture
|
||||
* committed, as before.
|
||||
*/
|
||||
constexpr uint64_t kLazyTextureMinBytes = 64 * 1024;
|
||||
|
||||
/* The smallest texture made reserved: 64 KiB, one tile, unless BX_LAZY_TEXTURES_MIN (bytes) says
|
||||
otherwise (a conformance test uses 0 to put small textures on this path). */
|
||||
uint64_t lazy_texture_min_bytes() {
|
||||
static const uint64_t min = [] {
|
||||
char v[24] = {};
|
||||
const DWORD n = GetEnvironmentVariableA("BX_LAZY_TEXTURES_MIN", v, sizeof(v));
|
||||
return n > 0 && n < sizeof(v) ? std::strtoull(v, nullptr, 10) : kLazyTextureMinBytes;
|
||||
}();
|
||||
return min;
|
||||
}
|
||||
|
||||
bool lazy_textures_enabled() {
|
||||
static const bool on = [] {
|
||||
char v[4] = {};
|
||||
return !(GetEnvironmentVariableA("BX_LAZY_TEXTURES", v, sizeof(v)) > 0 && v[0] == '0');
|
||||
}();
|
||||
return on;
|
||||
}
|
||||
|
||||
struct TextureMemory {
|
||||
ID3D12Heap* heap = nullptr; // null until mapped
|
||||
uint64_t bytes = 0;
|
||||
};
|
||||
std::mutex g_memory_lock;
|
||||
std::unordered_map<const void*, TextureMemory> g_texture_memory; // reserved host texture -> its memory
|
||||
std::atomic<uint64_t> g_reserved_made{0}, g_mapped_count{0}, g_mapped_bytes{0};
|
||||
|
||||
void note_texture_memory(const char* what) {
|
||||
const uint64_t n = g_mapped_count.load();
|
||||
char detail[200];
|
||||
std::snprintf(detail, sizeof(detail), "%s; reserved=%llu mapped=%llu mapped_bytes=%llu", what,
|
||||
static_cast<unsigned long long>(g_reserved_made.load()), static_cast<unsigned long long>(n),
|
||||
static_cast<unsigned long long>(g_mapped_bytes.load()));
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "texture_memory", detail);
|
||||
}
|
||||
|
||||
/* Maps one heap behind every tile of reserved texture `host` on `queue`, once. g_memory_lock held. */
|
||||
HRESULT map_texture_memory_locked(ID3D12CommandQueue* queue, ID3D12Resource* host, TextureMemory& memory) {
|
||||
if (memory.heap) return S_FALSE;
|
||||
ID3D12Device* device = nullptr;
|
||||
if (FAILED(host->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(&device))) || !device) return E_FAIL;
|
||||
const D3D12_RESOURCE_DESC desc = host->GetDesc();
|
||||
const UINT slices = desc.DepthOrArraySize ? desc.DepthOrArraySize : 1;
|
||||
const UINT mips = desc.MipLevels ? desc.MipLevels : 1;
|
||||
UINT total = 0, count = mips * slices;
|
||||
D3D12_PACKED_MIP_INFO packed{};
|
||||
D3D12_TILE_SHAPE shape{};
|
||||
std::vector<D3D12_SUBRESOURCE_TILING> tiling(count);
|
||||
device->GetResourceTiling(host, &total, &packed, &shape, &count, 0, tiling.data());
|
||||
D3D12_HEAP_DESC heap_desc{};
|
||||
heap_desc.SizeInBytes = static_cast<UINT64>(total) * D3D12_TILED_RESOURCE_TILE_SIZE_IN_BYTES;
|
||||
heap_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
heap_desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
|
||||
ID3D12Heap* heap = nullptr;
|
||||
HRESULT hr = total ? device->CreateHeap(&heap_desc, __uuidof(ID3D12Heap), reinterpret_cast<void**>(&heap)) : E_FAIL;
|
||||
device->Release();
|
||||
if (FAILED(hr) || !heap) return FAILED(hr) ? hr : E_OUTOFMEMORY;
|
||||
// One region per standard subresource, and one per array slice for its packed mips; the heap's
|
||||
// tiles follow in that order.
|
||||
std::vector<D3D12_TILED_RESOURCE_COORDINATE> coords;
|
||||
std::vector<D3D12_TILE_REGION_SIZE> sizes;
|
||||
UINT used = 0;
|
||||
for (UINT slice = 0; slice < slices; ++slice) {
|
||||
for (UINT mip = 0; mip < mips; ++mip) {
|
||||
const UINT sub = slice * mips + mip;
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
if (mip < packed.NumStandardMips) {
|
||||
size.NumTiles = tiling[sub].WidthInTiles * tiling[sub].HeightInTiles * tiling[sub].DepthInTiles;
|
||||
} else if (mip == packed.NumStandardMips && packed.NumPackedMips) {
|
||||
size.NumTiles = packed.NumTilesForPackedMips;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if (!size.NumTiles) continue;
|
||||
coords.push_back(D3D12_TILED_RESOURCE_COORDINATE{0, 0, 0, sub});
|
||||
sizes.push_back(size);
|
||||
used += size.NumTiles;
|
||||
}
|
||||
}
|
||||
if (used > total) {
|
||||
heap->Release();
|
||||
return E_UNEXPECTED;
|
||||
}
|
||||
const D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NONE;
|
||||
const UINT start = 0, range = used;
|
||||
queue->UpdateTileMappings(host, static_cast<UINT>(coords.size()), coords.data(), sizes.data(), heap, 1, &flags, &start,
|
||||
&range, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
memory.heap = heap;
|
||||
memory.bytes = heap_desc.SizeInBytes;
|
||||
const uint64_t before = g_mapped_count.fetch_add(1);
|
||||
g_mapped_bytes += memory.bytes;
|
||||
// Logged for the first 16 mappings, then whenever the count passes a power of two.
|
||||
if (before < 16 || (before & (before + 1)) == 0) note_texture_memory("texture memory mapped at first use");
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
/* The memory of a reserved texture being released (or of a texture this layer drops). */
|
||||
void release_texture_memory(const void* host) {
|
||||
ID3D12Heap* heap = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_memory_lock);
|
||||
auto it = g_texture_memory.find(host);
|
||||
if (it == g_texture_memory.end()) return;
|
||||
heap = it->second.heap;
|
||||
if (heap) g_mapped_bytes -= it->second.bytes;
|
||||
g_texture_memory.erase(it);
|
||||
}
|
||||
if (heap) heap->Release();
|
||||
}
|
||||
|
||||
// Copies between a buffer's title memory and its host resource, when the host is CPU-mappable.
|
||||
bool copy_buffer(const Shadow& s, bool to_host, uint64_t begin, uint64_t end) {
|
||||
if (!s.buffer || end <= begin || end > s.size) return false;
|
||||
@@ -69,6 +197,83 @@ Shadow* g_shadows = nullptr;
|
||||
size_t g_count = 0;
|
||||
size_t g_capacity = 0;
|
||||
|
||||
/*
|
||||
* Indexes over g_shadows (all under g_lock). A title can keep 100,000+ shadows alive (the target
|
||||
* build makes views of new addresses every frame), and every view creation, root-buffer bind,
|
||||
* texture-contents check and Map looked one up by scanning the whole array: on the target build's
|
||||
* post-loading phase that held g_lock for most of each frame. Lookups by host pointer go through a
|
||||
* hash map; lookups by title address through an ordered map of the shadows smaller than 1 MiB (one
|
||||
* that holds or overlaps an address starts at most 1 MiB before it) plus a short list of the larger
|
||||
* ones; buffers with outstanding Map calls are kept in a set for the per-submission sync.
|
||||
*/
|
||||
constexpr uint64_t kSmallExtent = 1ull << 20;
|
||||
std::unordered_map<const void*, size_t> g_by_host;
|
||||
std::multimap<uint64_t, const void*> g_by_va_small; // title_va -> host
|
||||
std::vector<const void*> g_large; // hosts of shadows of 1 MiB or more
|
||||
std::unordered_set<const void*> g_mapped_hosts; // shadows with outstanding Map calls
|
||||
|
||||
uint64_t extent_of(const Shadow& s) { return s.size > s.span ? s.size : s.span; }
|
||||
|
||||
void index_add_locked(size_t i) {
|
||||
const Shadow& s = g_shadows[i];
|
||||
g_by_host[s.host] = i;
|
||||
if (extent_of(s) >= kSmallExtent) g_large.push_back(s.host);
|
||||
else g_by_va_small.emplace(s.title_va, s.host);
|
||||
}
|
||||
|
||||
void index_drop_locked(const Shadow& s) {
|
||||
auto range = g_by_va_small.equal_range(s.title_va);
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
if (it->second == s.host) {
|
||||
g_by_va_small.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (size_t k = 0; k < g_large.size(); ++k) {
|
||||
if (g_large[k] == s.host) {
|
||||
g_large[k] = g_large.back();
|
||||
g_large.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Removes shadow i (swapped with the last) and keeps the indexes in step. g_lock must be held.
|
||||
void remove_at_locked(size_t i) {
|
||||
const Shadow gone = g_shadows[i];
|
||||
index_drop_locked(gone);
|
||||
g_by_host.erase(gone.host);
|
||||
g_mapped_hosts.erase(gone.host);
|
||||
--g_count;
|
||||
if (i != g_count) {
|
||||
g_shadows[i] = g_shadows[g_count];
|
||||
g_by_host[g_shadows[i].host] = i;
|
||||
}
|
||||
}
|
||||
|
||||
size_t find_host_locked(const void* host) {
|
||||
auto it = g_by_host.find(host);
|
||||
return it == g_by_host.end() ? SIZE_MAX : it->second;
|
||||
}
|
||||
|
||||
// Calls f(index) for every shadow that may overlap [lo, hi) (f checks exactly); stops when f returns true.
|
||||
template <typename F>
|
||||
void for_candidates_locked(uint64_t lo, uint64_t hi, F f) {
|
||||
const uint64_t from = lo > kSmallExtent ? lo - kSmallExtent : 0;
|
||||
std::vector<size_t> hits;
|
||||
for (auto it = g_by_va_small.lower_bound(from); it != g_by_va_small.end() && it->first < hi; ++it) {
|
||||
const size_t i = find_host_locked(it->second);
|
||||
if (i != SIZE_MAX) hits.push_back(i);
|
||||
}
|
||||
for (const void* h : g_large) {
|
||||
const size_t i = find_host_locked(h);
|
||||
if (i != SIZE_MAX) hits.push_back(i);
|
||||
}
|
||||
for (size_t i : hits) {
|
||||
if (f(i)) return;
|
||||
}
|
||||
}
|
||||
|
||||
bool remember(const Shadow& shadow) {
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
if (g_count == g_capacity) {
|
||||
@@ -81,6 +286,7 @@ bool remember(const Shadow& shadow) {
|
||||
g_capacity = capacity;
|
||||
}
|
||||
g_shadows[g_count++] = shadow;
|
||||
index_add_locked(g_count - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -117,10 +323,46 @@ HRESULT make_shadow(ID3D12Device* device, uint64_t title_va, const D3D12_RESOURC
|
||||
const bool simultaneous = !buffer && host_desc.SampleDesc.Count <= 1 && !(static_cast<UINT>(host_desc.Flags) & kGpuWrites);
|
||||
if (simultaneous) host_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS;
|
||||
ID3D12Resource* resource = nullptr;
|
||||
HRESULT hr = device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &host_desc, D3D12_RESOURCE_STATE_COMMON,
|
||||
nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&resource));
|
||||
bool made_simultaneous = simultaneous && SUCCEEDED(hr);
|
||||
if (simultaneous && FAILED(hr)) { // the host refuses the flag: without it, uploads use barriers from COMMON
|
||||
HRESULT hr = E_FAIL;
|
||||
bool made_simultaneous = false;
|
||||
bool reserved = false;
|
||||
// A read-only 2D texture of at least 64 KiB: reserved, its memory mapped at first use.
|
||||
if (simultaneous && lazy_textures_enabled() && host_desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D &&
|
||||
host_desc.SampleDesc.Count <= 1) {
|
||||
const UINT subs = (host_desc.MipLevels ? host_desc.MipLevels : 1) * (host_desc.DepthOrArraySize ? host_desc.DepthOrArraySize : 1);
|
||||
UINT64 packed_bytes = 0;
|
||||
device->GetCopyableFootprints(&host_desc, 0, subs, 0, nullptr, nullptr, nullptr, &packed_bytes);
|
||||
if (packed_bytes >= lazy_texture_min_bytes()) {
|
||||
D3D12_RESOURCE_DESC reserved_desc = host_desc;
|
||||
reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
|
||||
hr = device->CreateReservedResource(&reserved_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
|
||||
reinterpret_cast<void**>(&resource));
|
||||
made_simultaneous = SUCCEEDED(hr);
|
||||
if (FAILED(hr)) {
|
||||
reserved_desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(reserved_desc.Flags & ~D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS);
|
||||
hr = device->CreateReservedResource(&reserved_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
|
||||
reinterpret_cast<void**>(&resource));
|
||||
}
|
||||
reserved = SUCCEEDED(hr) && resource;
|
||||
if (!reserved) {
|
||||
static std::atomic<bool> noted{false};
|
||||
if (!noted.exchange(true)) {
|
||||
char detail[160];
|
||||
std::snprintf(detail, sizeof(detail), "the host refused a reserved texture (hr=0x%08lx); textures are committed",
|
||||
static_cast<unsigned long>(hr));
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "texture_memory", detail);
|
||||
}
|
||||
} else if (g_reserved_made.fetch_add(1) == 0) {
|
||||
note_texture_memory("read-only textures of 64 KiB or more are reserved; memory is mapped at first use");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!reserved) {
|
||||
hr = device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &host_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
|
||||
__uuidof(ID3D12Resource), reinterpret_cast<void**>(&resource));
|
||||
made_simultaneous = simultaneous && SUCCEEDED(hr);
|
||||
}
|
||||
if (simultaneous && !reserved && FAILED(hr)) { // the host refuses the flag: without it, uploads use barriers from COMMON
|
||||
static std::atomic<bool> noted{false};
|
||||
if (!noted.exchange(true)) {
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "placed_x_shadow", "the host refuses simultaneous access on a texture; made without it");
|
||||
@@ -154,7 +396,13 @@ HRESULT make_shadow(ID3D12Device* device, uint64_t title_va, const D3D12_RESOURC
|
||||
}
|
||||
Shadow record{resource, title_va, desc.Width, buffer, desc, 0};
|
||||
record.simultaneous = made_simultaneous;
|
||||
record.reserved = reserved;
|
||||
if (reserved) {
|
||||
std::lock_guard<std::mutex> guard(g_memory_lock);
|
||||
g_texture_memory[resource] = TextureMemory{};
|
||||
}
|
||||
if (!remember(record)) {
|
||||
release_texture_memory(resource);
|
||||
resource->Release();
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
@@ -210,29 +458,35 @@ ID3D12Resource* implicit_shadow(ID3D12Device* device, uint64_t title_va, const D
|
||||
std::vector<ID3D12Resource*> replaced;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
if (g_shadows[i].title_va == title_va && same_shape(g_shadows[i].desc, desc)) return g_shadows[i].host;
|
||||
}
|
||||
for (size_t i = 0; i < g_count;) {
|
||||
ID3D12Resource* same = nullptr;
|
||||
std::vector<const void*> overlapping;
|
||||
for_candidates_locked(title_va, title_va + span, [&](size_t i) {
|
||||
const Shadow& s = g_shadows[i];
|
||||
if (s.implicit && title_va < s.title_va + s.span && s.title_va < title_va + span) {
|
||||
replaced.push_back(s.host);
|
||||
g_shadows[i] = g_shadows[--g_count];
|
||||
} else {
|
||||
++i;
|
||||
if (s.title_va == title_va && same_shape(s.desc, desc)) {
|
||||
same = s.host;
|
||||
return true;
|
||||
}
|
||||
if (s.implicit && title_va < s.title_va + s.span && s.title_va < title_va + span) overlapping.push_back(s.host);
|
||||
return false;
|
||||
});
|
||||
if (same) return same;
|
||||
for (const void* h : overlapping) {
|
||||
const size_t i = find_host_locked(h);
|
||||
if (i == SIZE_MAX) continue;
|
||||
replaced.push_back(g_shadows[i].host);
|
||||
remove_at_locked(i);
|
||||
}
|
||||
}
|
||||
if (!replaced.empty()) retire_implicit(replaced);
|
||||
ID3D12Resource* resource = nullptr;
|
||||
if (FAILED(make_shadow(device, title_va, desc, "implicit shadow for a placed view", &resource))) return nullptr;
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = g_count; i-- > 0;) {
|
||||
if (g_shadows[i].host == resource) {
|
||||
g_shadows[i].implicit = true;
|
||||
g_shadows[i].span = span;
|
||||
break;
|
||||
}
|
||||
const size_t i = find_host_locked(resource);
|
||||
if (i != SIZE_MAX) {
|
||||
index_drop_locked(g_shadows[i]); // its extent changes with the span
|
||||
g_shadows[i].implicit = true;
|
||||
g_shadows[i].span = span;
|
||||
index_add_locked(i);
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
@@ -483,6 +737,7 @@ void sweep_implicit() {
|
||||
for (ID3D12Resource* host : release) {
|
||||
bx_console_forget_resource(host);
|
||||
host->Release();
|
||||
release_texture_memory(host);
|
||||
}
|
||||
const uint64_t before = g_implicit_released_total.fetch_add(release.size());
|
||||
const uint64_t after = before + release.size();
|
||||
@@ -612,14 +867,16 @@ void STDMETHODCALLTYPE bx_xbox_device_create_placed_uav_x(void* self, ID3D12Reso
|
||||
|
||||
bool bx_xbox_try_host_gpu_va(D3D12_GPU_VIRTUAL_ADDRESS title, D3D12_GPU_VIRTUAL_ADDRESS* host) {
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
bool found = false;
|
||||
for_candidates_locked(title, title + 1, [&](size_t i) {
|
||||
const Shadow& s = g_shadows[i];
|
||||
if (title >= s.title_va && title < s.title_va + s.size) {
|
||||
*host = s.host->GetGPUVirtualAddress() + (title - s.title_va);
|
||||
return true;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return found;
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -645,11 +902,13 @@ std::unordered_map<const void*, std::vector<BoundRange>> g_bound; // host list
|
||||
|
||||
// The index of a buffer shadow that holds the whole range, or SIZE_MAX. g_lock must be held.
|
||||
size_t buffer_holding_locked(uint64_t va, uint64_t size) {
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
size_t found = SIZE_MAX;
|
||||
for_candidates_locked(va, va + (size ? size : 1), [&](size_t i) {
|
||||
const Shadow& s = g_shadows[i];
|
||||
if (s.buffer && va >= s.title_va && va + size <= s.title_va + s.size) return i;
|
||||
}
|
||||
return SIZE_MAX;
|
||||
if (s.buffer && va >= s.title_va && va + size <= s.title_va + s.size) found = i;
|
||||
return found != SIZE_MAX;
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -725,11 +984,17 @@ void bx_xbox_refresh_bound_memory(ID3D12CommandList* const* host_lists, UINT cou
|
||||
D3D12_GPU_VIRTUAL_ADDRESS bx_xbox_host_gpu_va(D3D12_GPU_VIRTUAL_ADDRESS title) {
|
||||
if (!title) return title;
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
D3D12_GPU_VIRTUAL_ADDRESS out = title; // not a title address this layer knows: a host address already, or untranslated
|
||||
bool found = false;
|
||||
for_candidates_locked(title, title + 1, [&](size_t i) {
|
||||
const Shadow& s = g_shadows[i];
|
||||
if (title >= s.title_va && title < s.title_va + s.size) return s.host->GetGPUVirtualAddress() + (title - s.title_va);
|
||||
}
|
||||
return title; // not a title address this layer knows: a host address already, or untranslated
|
||||
if (title >= s.title_va && title < s.title_va + s.size) {
|
||||
out = s.host->GetGPUVirtualAddress() + (title - s.title_va);
|
||||
found = true;
|
||||
}
|
||||
return found;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
bool bx_xbox_title_memory_attach(ID3D12Resource* host, uint64_t title_va, uint64_t size, ULONGLONG attributes) {
|
||||
@@ -737,14 +1002,59 @@ bool bx_xbox_title_memory_attach(ID3D12Resource* host, uint64_t title_va, uint64
|
||||
attributes});
|
||||
}
|
||||
|
||||
void bx_xbox_texture_memory_for_write(ID3D12Resource* host) {
|
||||
if (!host) return;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_memory_lock);
|
||||
auto it = g_texture_memory.find(host);
|
||||
if (it == g_texture_memory.end() || it->second.heap) return;
|
||||
}
|
||||
// A GPU write into a reserved texture without memory would be dropped. The title's list runs
|
||||
// later on a queue of its own, so the mapping is made on this layer's queue and waited for.
|
||||
static std::mutex queue_lock;
|
||||
static ID3D12CommandQueue* queue = nullptr;
|
||||
static ID3D12Fence* fence = nullptr;
|
||||
static UINT64 value = 0;
|
||||
std::lock_guard<std::mutex> serial(queue_lock);
|
||||
if (!queue) {
|
||||
ID3D12Device* device = nullptr;
|
||||
if (SUCCEEDED(host->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(&device))) && device) {
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
if (FAILED(device->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&queue)))) queue = nullptr;
|
||||
if (FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&fence)))) fence = nullptr;
|
||||
device->Release();
|
||||
}
|
||||
}
|
||||
HRESULT hr = E_FAIL;
|
||||
if (queue && fence) {
|
||||
std::lock_guard<std::mutex> guard(g_memory_lock);
|
||||
auto it = g_texture_memory.find(host);
|
||||
hr = it == g_texture_memory.end() ? S_FALSE : map_texture_memory_locked(queue, host, it->second);
|
||||
}
|
||||
if (hr == S_OK) {
|
||||
queue->Signal(fence, ++value);
|
||||
HANDLE done = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
if (done && SUCCEEDED(fence->SetEventOnCompletion(value, done))) WaitForSingleObject(done, 2000);
|
||||
if (done) CloseHandle(done);
|
||||
} else if (FAILED(hr)) {
|
||||
static std::atomic<int> logged{0};
|
||||
if (logged.fetch_add(1) < 8) {
|
||||
char detail[160];
|
||||
std::snprintf(detail, sizeof(detail), "texture memory for a GPU write not mapped (hr=0x%08lx); the write is lost",
|
||||
static_cast<unsigned long>(hr));
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "texture_memory", detail);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ID3D12Resource* bx_xbox_texture_contents(ID3D12Resource* host, BxTextureUploads* uploads) {
|
||||
if (!uploads || !uploads->list) return host;
|
||||
Shadow shadow{};
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
size_t i = 0;
|
||||
while (i < g_count && g_shadows[i].host != host) ++i;
|
||||
if (i == g_count || g_shadows[i].buffer) return host;
|
||||
const size_t i = find_host_locked(host);
|
||||
if (i == SIZE_MAX || g_shadows[i].buffer) return host;
|
||||
shadow = g_shadows[i];
|
||||
}
|
||||
const TextureKind kind = texture_kind(shadow.desc);
|
||||
@@ -819,6 +1129,26 @@ ID3D12Resource* bx_xbox_texture_contents(ID3D12Resource* host, BxTextureUploads*
|
||||
known.rows.data(), static_cast<UINT>(known.footprints.size()), &subresources);
|
||||
}
|
||||
}
|
||||
if (hr == S_OK && shadow.reserved) {
|
||||
// Texture memory on first use: mapped on the submitting queue, which executes the upload
|
||||
// list after it.
|
||||
HRESULT mapped = E_FAIL;
|
||||
if (uploads->queue) {
|
||||
std::lock_guard<std::mutex> guard(g_memory_lock);
|
||||
auto it = g_texture_memory.find(host);
|
||||
mapped = it == g_texture_memory.end() ? E_UNEXPECTED : map_texture_memory_locked(uploads->queue, host, it->second);
|
||||
}
|
||||
if (FAILED(mapped)) {
|
||||
static std::atomic<int> logged{0};
|
||||
if (logged.fetch_add(1) < 8) {
|
||||
char detail[160];
|
||||
std::snprintf(detail, sizeof(detail), "texture memory not mapped (hr=0x%08lx); contents not uploaded",
|
||||
static_cast<unsigned long>(mapped));
|
||||
bx_log_emit(BX_LOG_WARN, "d3d12_xs", "texture_memory", detail);
|
||||
}
|
||||
hr = mapped;
|
||||
}
|
||||
}
|
||||
if (hr == S_OK) {
|
||||
ID3D12GraphicsCommandList* list = uploads->list;
|
||||
// A simultaneous-access shadow is promoted from COMMON by the copy and decays back after
|
||||
@@ -880,12 +1210,10 @@ void bx_xbox_placed_forget(IUnknown* host) {
|
||||
Shadow gone{};
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
if (static_cast<IUnknown*>(g_shadows[i].host) == host) {
|
||||
gone = g_shadows[i];
|
||||
g_shadows[i] = g_shadows[--g_count];
|
||||
break;
|
||||
}
|
||||
const size_t i = find_host_locked(host);
|
||||
if (i != SIZE_MAX) {
|
||||
gone = g_shadows[i];
|
||||
remove_at_locked(i);
|
||||
}
|
||||
}
|
||||
{
|
||||
@@ -896,6 +1224,7 @@ void bx_xbox_placed_forget(IUnknown* host) {
|
||||
g_texture_uploads.erase(upload);
|
||||
}
|
||||
}
|
||||
release_texture_memory(host);
|
||||
if (gone.title_attributes) XMemFree(reinterpret_cast<void*>(gone.title_va), gone.title_attributes);
|
||||
}
|
||||
|
||||
@@ -904,9 +1233,8 @@ D3D12_GPU_VIRTUAL_ADDRESS STDMETHODCALLTYPE bx_xbox_resource_get_gpu_virtual_add
|
||||
auto* host = bx_xbox_host<ID3D12Resource>(self);
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
if (g_shadows[i].host == host) return g_shadows[i].title_va;
|
||||
}
|
||||
const size_t i = find_host_locked(host);
|
||||
if (i != SIZE_MAX) return g_shadows[i].title_va;
|
||||
}
|
||||
return host->GetGPUVirtualAddress();
|
||||
}
|
||||
@@ -989,14 +1317,15 @@ extern "C" HRESULT STDMETHODCALLTYPE bx_xbox_resource_map(void* self, UINT subre
|
||||
uint64_t title_va = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
const size_t i = find_host_locked(host);
|
||||
if (i != SIZE_MAX) {
|
||||
Shadow& s = g_shadows[i];
|
||||
if (s.host != host) continue;
|
||||
title_va = s.title_va;
|
||||
// Make GPU results visible: refresh the read range of title memory from the host.
|
||||
const uint64_t begin = read ? read->Begin : 0, end = read ? read->End : s.size;
|
||||
if (s.buffer && end > begin) copy_buffer(s, false, begin, end < s.size ? end : s.size);
|
||||
++s.mapped;
|
||||
g_mapped_hosts.insert(s.host);
|
||||
}
|
||||
}
|
||||
if (!title_va) {
|
||||
@@ -1020,13 +1349,14 @@ extern "C" void STDMETHODCALLTYPE bx_xbox_resource_unmap(void* self, UINT subres
|
||||
auto* host = bx_xbox_host<ID3D12Resource>(self);
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
const size_t i = find_host_locked(host);
|
||||
if (i != SIZE_MAX) {
|
||||
Shadow& s = g_shadows[i];
|
||||
if (s.host != host) continue;
|
||||
// Hand the CPU's writes to the host resource (a null range means all of it).
|
||||
const uint64_t begin = written ? written->Begin : 0, end = written ? written->End : s.size;
|
||||
if (s.buffer && end > begin) copy_buffer(s, true, begin, end < s.size ? end : s.size);
|
||||
if (s.mapped > 0) --s.mapped;
|
||||
if (!s.mapped) g_mapped_hosts.erase(s.host);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1040,7 +1370,9 @@ extern "C" void STDMETHODCALLTYPE bx_xbox_resource_unmap(void* self, UINT subres
|
||||
void bx_xbox_sync_mapped_buffers() {
|
||||
constexpr uint64_t kLimit = 16ull << 20;
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
for (const void* h : g_mapped_hosts) {
|
||||
const size_t i = find_host_locked(h);
|
||||
if (i == SIZE_MAX) continue;
|
||||
const Shadow& s = g_shadows[i];
|
||||
if (!s.mapped || !s.buffer) continue;
|
||||
if (s.size > kLimit) {
|
||||
|
||||
@@ -107,12 +107,16 @@ CUSTOM = {
|
||||
"CopyPageMappingsX": "bx_xbox_queue_copy_page_mappings_x",
|
||||
"ClearPageMappingsX": "bx_xbox_queue_clear_page_mappings_x",
|
||||
"ExecuteCommandLists2X": "bx_xbox_queue_execute_command_lists2_x"},
|
||||
"CommandAllocator": {"SetAllocatorCallbackX": "bx_xbox_allocator_set_callback_x"},
|
||||
"CommandAllocator": {"SetAllocatorCallbackX": "bx_xbox_allocator_set_callback_x",
|
||||
"Reset": "bx_xbox_allocator_reset"},
|
||||
"DescriptorHeap": {"GetCPUDescriptorHandleForHeapStart": "bx_xbox_heap_cpu_start",
|
||||
"GetGPUDescriptorHandleForHeapStart": "bx_xbox_heap_gpu_start"},
|
||||
"GraphicsCommandList": {"ResourceBarrier": "bx_xbox_list_resource_barrier",
|
||||
"CopyTextureRegion": "bx_xbox_list_copy_texture_region",
|
||||
"CopyResource": "bx_xbox_list_copy_resource",
|
||||
"ResolveSubresource": "bx_xbox_list_resolve_subresource",
|
||||
"CopyTextureRegionX": "bx_xbox_list_copy_texture_region_x",
|
||||
"AllocateFromCommandAllocatorX": "bx_xbox_list_allocate_from_command_allocator_x",
|
||||
"Barrier": "bx_xbox_list_barrier",
|
||||
"BeginRenderPass": "bx_xbox_list_begin_render_pass",
|
||||
"DispatchRays": "bx_xbox_list_dispatch_rays",
|
||||
|
||||
@@ -236,6 +236,9 @@ ULONG STDMETHODCALLTYPE bx_xbox_release(void* self) {
|
||||
erase_locked(object);
|
||||
guard.unlock();
|
||||
if (object->iface == &bx_xbox_Resource) bx_xbox_placed_forget(object->host);
|
||||
if (object->iface == &bx_xbox_CommandAllocator || object->iface == &bx_xbox_GraphicsCommandList) {
|
||||
bx_xbox_command_memory_forget(object->host);
|
||||
}
|
||||
if (object->iface == &bx_xbox_DescriptorHeap) bx_xbox_descriptor_heap_forget(object->host);
|
||||
if (object->iface == &bx_xbox_Fence) bx_xbox_fence_forget(object->host);
|
||||
if (object->iface == &bx_xbox_PipelineState) bx_xbox_pipeline_forget(object->host);
|
||||
@@ -290,6 +293,7 @@ HRESULT STDMETHODCALLTYPE bx_xbox_device_create_command_list_x(void* self, const
|
||||
desc->NodeMask, desc->Type, bx_xbox_unwrap_as(allocator),
|
||||
bx_xbox_pipeline_is_null(initial_state) ? nullptr : bx_xbox_unwrap_as(initial_state), riid, ppv);
|
||||
if (FAILED(hr)) bx_xbox_log_host_failure("XboxDevice", "CreateCommandListX", hr);
|
||||
if (SUCCEEDED(hr) && *ppv) bx_xbox_list_note_allocator(*ppv, bx_xbox_unwrap_as(allocator));
|
||||
return bx_xbox_wrap_result(hr, riid, ppv);
|
||||
}
|
||||
|
||||
@@ -330,6 +334,7 @@ void STDMETHODCALLTYPE bx_xbox_list_copy_texture_region(void* self, const D3D12_
|
||||
if (dst) {
|
||||
d = *dst;
|
||||
d.pResource = bx_xbox_unwrap_as(d.pResource);
|
||||
bx_xbox_texture_memory_for_write(d.pResource);
|
||||
}
|
||||
if (src) {
|
||||
s = *src;
|
||||
@@ -338,6 +343,22 @@ void STDMETHODCALLTYPE bx_xbox_list_copy_texture_region(void* self, const D3D12_
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyTextureRegion(dst ? &d : nullptr, x, y, z, src ? &s : nullptr, box);
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE bx_xbox_list_copy_resource(void* self, ID3D12Resource* dst, ID3D12Resource* src) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "CopyResource");
|
||||
ID3D12Resource* d = bx_xbox_unwrap_as<ID3D12Resource>(dst);
|
||||
bx_xbox_texture_memory_for_write(d);
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyResource(d, bx_xbox_unwrap_as<ID3D12Resource>(src));
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE bx_xbox_list_resolve_subresource(void* self, ID3D12Resource* dst, UINT dst_sub, ID3D12Resource* src,
|
||||
UINT src_sub, DXGI_FORMAT format) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "ResolveSubresource");
|
||||
ID3D12Resource* d = bx_xbox_unwrap_as<ID3D12Resource>(dst);
|
||||
bx_xbox_texture_memory_for_write(d);
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->ResolveSubresource(d, dst_sub, bx_xbox_unwrap_as<ID3D12Resource>(src), src_sub,
|
||||
format);
|
||||
}
|
||||
|
||||
/* CopyTextureRegionX (GDK d3d12_xs.h): CopyTextureRegion plus D3D12XBOX_COPY_FLAGS, which only
|
||||
steer the console's compression metadata (HTILE resummarize, forced decompress, clear
|
||||
propagation). The host keeps no such metadata, so the copy is the PC one. Leaving the call a
|
||||
|
||||
@@ -230,6 +230,10 @@ bool bx_xbox_bind_title_memory(ID3D12GraphicsCommandList* list, D3D12_GPU_VIRTUA
|
||||
void bx_xbox_bound_memory_reset(IUnknown* host_list);
|
||||
void bx_xbox_refresh_bound_memory(ID3D12CommandList* const* host_lists, UINT count);
|
||||
UINT bx_xbox_descriptor_size(D3D12_DESCRIPTOR_HEAP_TYPE type);
|
||||
/* Command-allocator memory (pipelines.cpp, #806): which allocator a host list records with, and
|
||||
the release of an allocator's (or a list's own) memory. */
|
||||
void bx_xbox_list_note_allocator(const void* host_list, const void* host_allocator);
|
||||
void bx_xbox_command_memory_forget(const void* host);
|
||||
void bx_xbox_descriptor_heap_forget(IUnknown* host);
|
||||
/* Records title-visible memory (from XMemAlloc) for a host resource; freed when it is released. */
|
||||
bool bx_xbox_title_memory_attach(ID3D12Resource* host, uint64_t title_va, uint64_t size, ULONGLONG attributes);
|
||||
@@ -239,7 +243,8 @@ struct BxTextureUploads {
|
||||
ID3D12GraphicsCommandList* list;
|
||||
ID3D12Fence* fence;
|
||||
UINT64 value;
|
||||
bool recorded; // set when a copy was recorded on `list`
|
||||
bool recorded; // set when a copy was recorded on `list`
|
||||
ID3D12CommandQueue* queue; // the submitting queue: texture memory is mapped on it first
|
||||
};
|
||||
/* The resource a view of `host` should read so that it sees the texture's contents in title memory
|
||||
(placed_x.cpp, #745): for a texture placed in title memory, a copy of its host texture filled
|
||||
@@ -248,6 +253,9 @@ struct BxTextureUploads {
|
||||
backed-off interval); `host` itself for everything else, without `uploads`, and until a copy
|
||||
has been recorded (failures are logged). */
|
||||
ID3D12Resource* bx_xbox_texture_contents(ID3D12Resource* host, BxTextureUploads* uploads);
|
||||
/* Before a title's GPU copy or resolve writes `host` (a host resource): a texture whose memory is
|
||||
mapped at first use (placed_x.cpp, #782) gets it now, so the write is kept. */
|
||||
void bx_xbox_texture_memory_for_write(ID3D12Resource* host);
|
||||
|
||||
/* Logs a host call that failed (`host_call_failed`), so a refusal is never silent. */
|
||||
void bx_xbox_log_host_failure(const char* iface, const char* method, HRESULT hr);
|
||||
@@ -321,6 +329,8 @@ HRESULT STDMETHODCALLTYPE bx_xbox_list_reset_x(void* self, ID3D12CommandAllocato
|
||||
void STDMETHODCALLTYPE bx_xbox_list_sync_x(void* self, UINT sync_flags, UINT control_flags, D3D12_GPU_VIRTUAL_ADDRESS base,
|
||||
SIZE_T size);
|
||||
void STDMETHODCALLTYPE bx_xbox_allocator_set_callback_x(void* self, void* callback, UINT64 context);
|
||||
void* STDMETHODCALLTYPE bx_xbox_list_allocate_from_command_allocator_x(void* self, UINT size, UINT alignment);
|
||||
HRESULT STDMETHODCALLTYPE bx_xbox_allocator_reset(void* self);
|
||||
void STDMETHODCALLTYPE bx_xbox_list_draw_instanced(void* self, UINT vertices, UINT instances, UINT first_vertex,
|
||||
UINT first_instance);
|
||||
void STDMETHODCALLTYPE bx_xbox_list_draw_indexed_instanced(void* self, UINT indices, UINT instances, UINT first_index,
|
||||
@@ -471,6 +481,9 @@ void bx_xbox_placed_note_submission();
|
||||
/* Command-list slots whose structures carry interface pointers, unwrapped before the host call.
|
||||
The Xbox ResourceBarrier also returns the actions it took; 0 is returned. */
|
||||
UINT STDMETHODCALLTYPE bx_xbox_list_resource_barrier(void* self, UINT count, const D3D12_RESOURCE_BARRIER* barriers);
|
||||
void STDMETHODCALLTYPE bx_xbox_list_copy_resource(void* self, ID3D12Resource* dst, ID3D12Resource* src);
|
||||
void STDMETHODCALLTYPE bx_xbox_list_resolve_subresource(void* self, ID3D12Resource* dst, UINT dst_sub, ID3D12Resource* src,
|
||||
UINT src_sub, DXGI_FORMAT format);
|
||||
void STDMETHODCALLTYPE bx_xbox_list_copy_texture_region(void* self, const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x,
|
||||
UINT y, UINT z, const D3D12_TEXTURE_COPY_LOCATION* src,
|
||||
const D3D12_BOX* box);
|
||||
|
||||
@@ -964,12 +964,6 @@ static HRESULT STDMETHODCALLTYPE x_CommandAllocator_6(void* self, const WCHAR *
|
||||
if (FAILED(hr)) bx_xbox_log_host_failure("XboxCommandAllocator", "SetName", hr);
|
||||
return hr;
|
||||
}
|
||||
static HRESULT STDMETHODCALLTYPE x_CommandAllocator_8(void* self) {
|
||||
BX_D3D_TRACE("XboxCommandAllocator", "Reset");
|
||||
HRESULT hr = bx_xbox_host<ID3D12CommandAllocator>(self)->Reset();
|
||||
if (FAILED(hr)) bx_xbox_log_host_failure("XboxCommandAllocator", "Reset", hr);
|
||||
return hr;
|
||||
}
|
||||
|
||||
const void* const kCommandAllocatorTable[] = {
|
||||
reinterpret_cast<const void*>(&bx_xbox_query_interface),
|
||||
@@ -980,7 +974,7 @@ const void* const kCommandAllocatorTable[] = {
|
||||
reinterpret_cast<const void*>(&x_CommandAllocator_5),
|
||||
reinterpret_cast<const void*>(&x_CommandAllocator_6),
|
||||
reinterpret_cast<const void*>(&bx_xbox_get_device),
|
||||
reinterpret_cast<const void*>(&x_CommandAllocator_8),
|
||||
reinterpret_cast<const void*>(&bx_xbox_allocator_reset),
|
||||
reinterpret_cast<const void*>(&bx_xbox_allocator_set_callback_x),
|
||||
};
|
||||
|
||||
@@ -991,7 +985,7 @@ const IID kCommandAllocatorIids[] = {
|
||||
{0xc4fec28f, 0x7966, 0x4e95, {0x9f, 0x94, 0xf4, 0x31, 0xcb, 0x56, 0xc3, 0xb8}}, /* ID3D12Object */
|
||||
};
|
||||
|
||||
/* ID3D12CommandAllocator: 10 slots (5 forwarded, 5 shared, 0 stubs); hosted on ID3D12CommandAllocator. */
|
||||
/* ID3D12CommandAllocator: 10 slots (4 forwarded, 6 shared, 0 stubs); hosted on ID3D12CommandAllocator. */
|
||||
const IID kHostCommandAllocator = __uuidof(ID3D12CommandAllocator);
|
||||
|
||||
static HRESULT STDMETHODCALLTYPE x_QueryHeap_3(void* self, REFGUID a0, UINT * a1, void * a2) {
|
||||
@@ -1268,18 +1262,10 @@ static void STDMETHODCALLTYPE x_GraphicsCommandList_15(void* self, ID3D12Resourc
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "CopyBufferRegion");
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyBufferRegion(bx_xbox_unwrap_as<ID3D12Resource>(a0), a1, bx_xbox_unwrap_as<ID3D12Resource>(a2), a3, a4);
|
||||
}
|
||||
static void STDMETHODCALLTYPE x_GraphicsCommandList_17(void* self, ID3D12Resource * a0, ID3D12Resource * a1) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "CopyResource");
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyResource(bx_xbox_unwrap_as<ID3D12Resource>(a0), bx_xbox_unwrap_as<ID3D12Resource>(a1));
|
||||
}
|
||||
static void STDMETHODCALLTYPE x_GraphicsCommandList_18(void* self, ID3D12Resource * a0, const D3D12_TILED_RESOURCE_COORDINATE * a1, const D3D12_TILE_REGION_SIZE * a2, ID3D12Resource * a3, UINT64 a4, D3D12_TILE_COPY_FLAGS a5) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "CopyTiles");
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyTiles(bx_xbox_unwrap_as<ID3D12Resource>(a0), a1, a2, bx_xbox_unwrap_as<ID3D12Resource>(a3), a4, a5);
|
||||
}
|
||||
static void STDMETHODCALLTYPE x_GraphicsCommandList_19(void* self, ID3D12Resource * a0, UINT a1, ID3D12Resource * a2, UINT a3, DXGI_FORMAT a4) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "ResolveSubresource");
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->ResolveSubresource(bx_xbox_unwrap_as<ID3D12Resource>(a0), a1, bx_xbox_unwrap_as<ID3D12Resource>(a2), a3, a4);
|
||||
}
|
||||
static void STDMETHODCALLTYPE x_GraphicsCommandList_21(void* self, UINT a0, const D3D12_VIEWPORT * a1) {
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "RSSetViewports");
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->RSSetViewports(a0, a1);
|
||||
@@ -1577,8 +1563,6 @@ static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_145(void*) { BX_VTABLE_STU
|
||||
/* Xbox-only method */
|
||||
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_147(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 147u, "OMSetFrontAndBackStencilRef"); return 0; }
|
||||
/* Xbox-only method */
|
||||
static UINT64 STDMETHODCALLTYPE x_GraphicsCommandList_148(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 148u, "AllocateFromCommandAllocatorX"); return 0; }
|
||||
/* Xbox-only method */
|
||||
static HRESULT STDMETHODCALLTYPE x_GraphicsCommandList_149(void*) { BX_VTABLE_STUB_LOG("XboxGraphicsCommandList", 149u, "RehydrateRaytracingAccelerationStructureBatchedX"); return E_NOTIMPL; }
|
||||
|
||||
const void* const kGraphicsCommandListTable[] = {
|
||||
@@ -1599,9 +1583,9 @@ const void* const kGraphicsCommandListTable[] = {
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_dispatch),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_15),
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_copy_texture_region),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_17),
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_copy_resource),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_18),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_19),
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_resolve_subresource),
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_ia_set_primitive_topology),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_21),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_22),
|
||||
@@ -1730,7 +1714,7 @@ const void* const kGraphicsCommandListTable[] = {
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_145),
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_barrier),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_147),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_148),
|
||||
reinterpret_cast<const void*>(&bx_xbox_list_allocate_from_command_allocator_x),
|
||||
reinterpret_cast<const void*>(&x_GraphicsCommandList_149),
|
||||
};
|
||||
|
||||
@@ -1749,7 +1733,7 @@ const IID kGraphicsCommandListIids[] = {
|
||||
{0xc4fec28f, 0x7966, 0x4e95, {0x9f, 0x94, 0xf4, 0x31, 0xcb, 0x56, 0xc3, 0xb8}}, /* ID3D12Object */
|
||||
};
|
||||
|
||||
/* ID3D12GraphicsCommandList8: 150 slots (55 forwarded, 36 shared, 59 stubs); hosted on ID3D12GraphicsCommandList7. */
|
||||
/* ID3D12GraphicsCommandList8: 150 slots (53 forwarded, 39 shared, 58 stubs); hosted on ID3D12GraphicsCommandList7. */
|
||||
const IID kHostGraphicsCommandList = __uuidof(ID3D12GraphicsCommandList7);
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -13,3 +13,5 @@
|
||||
The package as a whole is under the proprietary *Microsoft DirectStorage SDK* licence terms (`LICENSE.txt`), whose distributable list (`distributable_files.txt`) names only `dstorage.dll` and `dstoragecore.dll`. BetterXen uses neither DLL and copies nothing else from the package.
|
||||
|
||||
The Xbox console interfaces (`IDStorageFactoryX` and others, `dstorage_xs.h`) are not included: their IIDs have no licence-compatible public source. See the discussion on #483.
|
||||
|
||||
`xcrdapi.cpp` declares its own copies of the Xbox console interfaces, which follow the authorized GDK header `dstorage_xs.h` (#769). That header itself is not vendored.
|
||||
+191
-114
@@ -485,19 +485,15 @@ CopyResult copy_staged(HANDLE file, const Request& request) {
|
||||
}
|
||||
|
||||
/* A title may read only the first part of a compressed block: the stream then holds more than
|
||||
DestinationSize bytes. The whole stream is decoded and checked (Adler-32), and its first
|
||||
`output_size` bytes are delivered. A stream shorter than the destination stays an error. */
|
||||
DestinationSize bytes. The whole stream is decoded and checked (Adler-32), and only its first
|
||||
`output_size` bytes are written; the rest passes through a 32 KiB window, so a partial read
|
||||
needs no buffer for the whole stream (#769: it used to allocate output_size * 64 bytes, up to
|
||||
2 GiB for a 32 MiB destination). A stream shorter than the destination stays an error. */
|
||||
BxInflateResult inflate_first_part(const uint8_t* compressed, const Request& request) {
|
||||
const size_t capacity = static_cast<size_t>(request.output_size) * 64 + 0x40000;
|
||||
auto* whole = static_cast<uint8_t*>(HeapAlloc(GetProcessHeap(), 0, capacity));
|
||||
if (!whole) return BxInflateResult::output_mismatch;
|
||||
size_t produced = 0;
|
||||
BxInflateResult r = bx_zlib_inflate_any(compressed, request.size, whole, capacity, &produced);
|
||||
if (r == BxInflateResult::ok && produced > request.output_size) {
|
||||
std::memcpy(request.destination, whole, request.output_size);
|
||||
} else if (r == BxInflateResult::ok) {
|
||||
r = BxInflateResult::output_mismatch;
|
||||
}
|
||||
BxInflateResult r = bx_zlib_inflate_prefix(compressed, request.size, static_cast<uint8_t*>(request.destination),
|
||||
request.output_size, &produced);
|
||||
if (r == BxInflateResult::ok && produced <= request.output_size) r = BxInflateResult::output_mismatch;
|
||||
static std::atomic<UINT> logged{0};
|
||||
if (logged.fetch_add(1) < 16) {
|
||||
char detail[160];
|
||||
@@ -506,7 +502,6 @@ BxInflateResult inflate_first_part(const uint8_t* compressed, const Request& req
|
||||
r == BxInflateResult::ok ? "first part delivered" : bx_inflate_result_name(r));
|
||||
bx_log_emit(r == BxInflateResult::ok ? BX_LOG_INFO : BX_LOG_WARN, "xcrdapi", "dstorage_zlib_partial", detail);
|
||||
}
|
||||
HeapFree(GetProcessHeap(), 0, whole);
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -1535,47 +1530,81 @@ private:
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Xbox Series DirectStorage COM interfaces (IDStorage*X), inferred from call-site
|
||||
// observation of a project-authorized target build (provenance pending maintainer
|
||||
// review; not a public contract), layered over the same WorkQueue / StorageFile /
|
||||
// StatusArray engine. Unimplemented vtable slots log through BX_VTABLE_STUB_LOG
|
||||
// so any newly reached slot surfaces immediately in bx-cli trace.
|
||||
// Xbox Series DirectStorage COM interfaces (IDStorage*X), as the authorized GDK header
|
||||
// dstorage_xs.h declares them (#769), layered over the same WorkQueue / StorageFile /
|
||||
// StatusArray engine. Methods the engine cannot honour log through BX_VTABLE_STUB_LOG so
|
||||
// they surface in bx-cli trace.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
constexpr IID kXboxFactoryIid{0x62959d11, 0xd45f, 0x4884, {0xa0, 0xa3, 0xf7, 0x86, 0x2e, 0xa2, 0x43, 0xae}};
|
||||
// IIDs from dstorage_xs.h. One object answers its interface and the interfaces it extends: a
|
||||
// factory implements IDStorageFactoryX2 (so X and X1 too), a queue IDStorageQueueX2.
|
||||
constexpr IID kXboxFactoryIid{0xfb3b54ac, 0xbf82, 0x463a, {0xb7, 0x77, 0x73, 0x19, 0x58, 0x37, 0x86, 0x4d}};
|
||||
constexpr IID kXboxFactory1Iid{0xc74c2a5b, 0x1e43, 0x46a5, {0xa2, 0x24, 0x91, 0x16, 0x35, 0x8e, 0x11, 0x18}};
|
||||
constexpr IID kXboxFactory2Iid{0x62959d11, 0xd45f, 0x4884, {0xa0, 0xa3, 0xf7, 0x86, 0x2e, 0xa2, 0x43, 0xae}};
|
||||
constexpr IID kXboxFileIid{0x43704f9f, 0x27c6, 0x4a9f, {0x88, 0x15, 0xd1, 0x65, 0x27, 0x89, 0x4d, 0xf4}};
|
||||
constexpr IID kXboxStatusArrayIid{0xcd66d752, 0x2786, 0x44f7, {0x90, 0x70, 0xe2, 0xa6, 0xfd, 0xad, 0x3f, 0x1b}};
|
||||
constexpr IID kXboxQueueIid{0x3c02cb0c, 0xc050, 0x4195, {0x99, 0xa1, 0xa1, 0xce, 0x94, 0xba, 0x33, 0xc5}};
|
||||
constexpr IID kXboxQueueIid{0xa3f6649a, 0x8c2c, 0x4b3a, {0x8b, 0x6e, 0x10, 0x18, 0x6a, 0x04, 0xb9, 0xbc}};
|
||||
constexpr IID kXboxQueue1Iid{0x3c02cb0c, 0xc050, 0x4195, {0x99, 0xa1, 0xa1, 0xce, 0x94, 0xba, 0x33, 0xc5}};
|
||||
constexpr IID kXboxQueue2Iid{0x3af5c948, 0x6297, 0x426c, {0xbf, 0xfc, 0x8f, 0x18, 0x95, 0x9f, 0xb8, 0x7b}};
|
||||
|
||||
bool is_xbox_factory_iid(REFIID riid) {
|
||||
return IsEqualIID(riid, kXboxFactoryIid) || IsEqualIID(riid, kXboxFactory1Iid) || IsEqualIID(riid, kXboxFactory2Iid);
|
||||
}
|
||||
|
||||
// DSTORAGE_QUEUE_DESC1; DSTORAGE_QUEUE_DESC is its first 24 bytes (no Options).
|
||||
struct XboxQueueDesc {
|
||||
UINT64 source_type;
|
||||
UINT16 capacity;
|
||||
INT8 priority;
|
||||
UINT8 reserved_pad[5];
|
||||
PCSTR name;
|
||||
UINT64 flags;
|
||||
UINT64 flags; // DSTORAGE_QUEUE_OPTIONS
|
||||
};
|
||||
static_assert(sizeof(XboxQueueDesc) == 32, "XboxQueueDesc must be 32 bytes");
|
||||
static_assert(sizeof(XboxQueueDesc) == 32, "DSTORAGE_QUEUE_DESC1 is 32 bytes");
|
||||
static_assert(offsetof(XboxQueueDesc, source_type) == 0x00, "source_type offset");
|
||||
static_assert(offsetof(XboxQueueDesc, capacity) == 0x08, "capacity offset");
|
||||
static_assert(offsetof(XboxQueueDesc, priority) == 0x0a, "priority offset");
|
||||
static_assert(offsetof(XboxQueueDesc, name) == 0x10, "name offset");
|
||||
static_assert(offsetof(XboxQueueDesc, flags) == 0x18, "flags offset");
|
||||
constexpr size_t kXboxQueueDescSize = 24; // DSTORAGE_QUEUE_DESC
|
||||
|
||||
// DSTORAGE_QUEUE_INFO (Desc is a DSTORAGE_QUEUE_DESC) and DSTORAGE_QUEUE_INFO1 (a DESC1).
|
||||
struct XboxQueueInfo {
|
||||
UINT64 source_type;
|
||||
UINT16 capacity;
|
||||
INT8 priority;
|
||||
UINT8 reserved_pad[5];
|
||||
PCSTR name;
|
||||
UINT16 empty_slot_count;
|
||||
UINT16 request_count_until_auto_submit;
|
||||
};
|
||||
static_assert(offsetof(XboxQueueInfo, empty_slot_count) == 24 && sizeof(XboxQueueInfo) == 32,
|
||||
"DSTORAGE_QUEUE_INFO layout");
|
||||
struct XboxQueueInfo1 {
|
||||
XboxQueueDesc desc;
|
||||
UINT16 empty_slot_count;
|
||||
UINT16 request_count_until_auto_submit;
|
||||
};
|
||||
static_assert(offsetof(XboxQueueInfo1, empty_slot_count) == 32 && sizeof(XboxQueueInfo1) == 40,
|
||||
"DSTORAGE_QUEUE_INFO1 layout");
|
||||
|
||||
// DSTORAGE_REQUEST, 0x48 bytes.
|
||||
struct XboxRequest {
|
||||
UINT64 options; // +0x00: bit 22 (0x400000) = memory source, bit 0 = compressed
|
||||
UINT64 options; // +0x00: DSTORAGE_REQUEST_OPTIONS
|
||||
void* destination; // +0x08: buffer, or the 64 KB page-number array (DestinationIsPhysicalPages)
|
||||
UINT16 destination_page_offset; // +0x10: offset into the first page (DestinationIsPhysicalPages)
|
||||
UINT16 reserved12;
|
||||
UINT32 reserved14;
|
||||
UINT32 destination_size; // +0x18
|
||||
UINT32 reserved1c; // +0x1c
|
||||
void* source_ptr; // +0x20: IXboxDStorageFile* (file) or const void* (memory)
|
||||
void* source_ptr; // +0x20: IDStorageFileX* (file) or const void* (memory)
|
||||
UINT64 file_offset; // +0x28
|
||||
UINT32 source_size; // +0x30
|
||||
UINT32 reserved34; // +0x34
|
||||
UINT32 intermediate_size; // +0x34: BCPack bytes when ZlibDecompress and BcpackMode are both set
|
||||
UINT64 cancellation_tag; // +0x38
|
||||
PCSTR name; // +0x40
|
||||
};
|
||||
static_assert(sizeof(XboxRequest) == 56, "XboxRequest must be 56 bytes");
|
||||
static_assert(sizeof(XboxRequest) == 0x48, "DSTORAGE_REQUEST must be 0x48 bytes");
|
||||
static_assert(offsetof(XboxRequest, options) == 0x00, "options offset");
|
||||
static_assert(offsetof(XboxRequest, destination) == 0x08, "destination offset");
|
||||
static_assert(offsetof(XboxRequest, destination_page_offset) == 0x10, "destination_page_offset offset");
|
||||
@@ -1583,6 +1612,26 @@ static_assert(offsetof(XboxRequest, destination_size) == 0x18, "destination_size
|
||||
static_assert(offsetof(XboxRequest, source_ptr) == 0x20, "source_ptr offset");
|
||||
static_assert(offsetof(XboxRequest, file_offset) == 0x28, "file_offset offset");
|
||||
static_assert(offsetof(XboxRequest, source_size) == 0x30, "source_size offset");
|
||||
static_assert(offsetof(XboxRequest, intermediate_size) == 0x34, "intermediate_size offset");
|
||||
static_assert(offsetof(XboxRequest, cancellation_tag) == 0x38, "cancellation_tag offset");
|
||||
static_assert(offsetof(XboxRequest, name) == 0x40, "name offset");
|
||||
|
||||
// DSTORAGE_ERROR_RECORD: FailureCount, then FirstFailure { HResult, CommandType, a union of
|
||||
// the failed command's parameters (Request is the largest: options through Name[64]) }.
|
||||
struct XboxErrorRecord {
|
||||
UINT32 failure_count;
|
||||
HRESULT hresult;
|
||||
INT32 command_type; // DSTORAGE_COMMAND_TYPE
|
||||
UINT32 pad;
|
||||
unsigned char parameters[640];
|
||||
};
|
||||
static_assert(offsetof(XboxErrorRecord, hresult) == 4 && offsetof(XboxErrorRecord, command_type) == 8,
|
||||
"DSTORAGE_ERROR_RECORD header");
|
||||
static_assert(offsetof(XboxErrorRecord, parameters) == 16 && sizeof(XboxErrorRecord) == 656,
|
||||
"DSTORAGE_ERROR_RECORD is 656 bytes");
|
||||
|
||||
// DSTORAGE_STATS: four 48-byte DSTORAGE_PRIORITY_STATS, two UINT8 and fifteen UINT32.
|
||||
constexpr size_t kXboxStatsSize = 256;
|
||||
|
||||
// DSTORAGE_REQUEST_OPTIONS (authorized GDK documentation): ZlibDecompress:1, BcpackMode:4,
|
||||
// SwizzleMode:16, DestinationIsPhysicalPages:1, SourceType:1, SourceIsPhysicalPages:1.
|
||||
@@ -1618,20 +1667,19 @@ void log_request_options(UINT64 options, UINT32 source_size, UINT32 destination_
|
||||
bx_log_emit(BX_LOG_INFO, "xcrdapi", "dstorage_request_options", detail);
|
||||
}
|
||||
|
||||
// IDStorageFileX: slot 3 Close, slot 4 GetHandle.
|
||||
struct IXboxDStorageFile : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE GetFileInformation(BY_HANDLE_FILE_INFORMATION* info) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot5() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot6() = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0;
|
||||
};
|
||||
|
||||
// IDStorageStatusArrayX: slot 3 IsComplete, slot 4 GetHResult.
|
||||
struct IXboxDStorageStatusArray : IUnknown {
|
||||
virtual bool STDMETHODCALLTYPE IsComplete(UINT32 index) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE GetHResult(UINT32 index) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot5() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot6() = 0;
|
||||
};
|
||||
|
||||
// IDStorageQueueX (slots 3-11), IDStorageQueueX1 (12), IDStorageQueueX2 (13).
|
||||
struct IXboxDStorageQueue : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE EnqueueRequest(const XboxRequest* request) = 0;
|
||||
virtual void STDMETHODCALLTYPE EnqueueStatus(IXboxDStorageStatusArray* status_array, UINT32 index) = 0;
|
||||
@@ -1640,25 +1688,22 @@ struct IXboxDStorageQueue : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64 mask, UINT64 value) = 0;
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetErrorEvent() = 0;
|
||||
virtual void STDMETHODCALLTYPE RetrieveErrorRecord(void* record) = 0;
|
||||
virtual void STDMETHODCALLTYPE Query(void* info) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot12() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot13() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot14() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot15() = 0;
|
||||
virtual void STDMETHODCALLTYPE RetrieveErrorRecord(XboxErrorRecord* record) = 0;
|
||||
virtual void STDMETHODCALLTYPE Query(XboxQueueInfo* info) = 0;
|
||||
virtual void STDMETHODCALLTYPE EnqueueSetEvent(HANDLE handle) = 0;
|
||||
virtual void STDMETHODCALLTYPE Query1(XboxQueueInfo1* info) = 0;
|
||||
};
|
||||
|
||||
// IDStorageFactoryX (slots 3-8), IDStorageFactoryX1 (9), IDStorageFactoryX2 (10).
|
||||
struct IXboxDStorageFactory : IUnknown {
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0;
|
||||
virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot7() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot9() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueueX(const XboxQueueDesc* desc, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot11() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot12() = 0;
|
||||
virtual void STDMETHODCALLTYPE GetStats(void* stats) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue1(const XboxQueueDesc* desc, REFIID riid, void** ppv) = 0;
|
||||
};
|
||||
|
||||
class XboxFile final : public PublicObject<IXboxDStorageFile> {
|
||||
@@ -1666,16 +1711,20 @@ public:
|
||||
explicit XboxFile(StorageFile* file) : file_(file) {}
|
||||
|
||||
void STDMETHODCALLTYPE Close() override { (void)file_->Close(); }
|
||||
HRESULT STDMETHODCALLTYPE GetFileInformation(BY_HANDLE_FILE_INFORMATION* info) override {
|
||||
return file_->GetFileInformation(info);
|
||||
}
|
||||
HRESULT STDMETHODCALLTYPE StubSlot5() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageFileX", 5u, "Slot5");
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
HRESULT STDMETHODCALLTYPE StubSlot6() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageFileX", 6u, "Slot6");
|
||||
return E_NOTIMPL;
|
||||
|
||||
// A new handle the caller closes, opened GENERIC_READ with FILE_SHARE_READ (dstorage_xs.h).
|
||||
// ReOpenFile gives one without the engine's FILE_FLAG_OVERLAPPED; a duplicate of the engine's
|
||||
// handle is the fallback. NULL if the file was closed.
|
||||
HANDLE STDMETHODCALLTYPE GetHandle() override {
|
||||
HANDLE engine = nullptr;
|
||||
if (FAILED(file_->GetHandle(&engine)) || !engine) return nullptr;
|
||||
using ReOpenFileFn = HANDLE(WINAPI*)(HANDLE, DWORD, DWORD, DWORD);
|
||||
static const auto reopen = reinterpret_cast<ReOpenFileFn>(
|
||||
reinterpret_cast<void*>(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "ReOpenFile")));
|
||||
const HANDLE fresh = reopen ? reopen(engine, GENERIC_READ, FILE_SHARE_READ, 0) : INVALID_HANDLE_VALUE;
|
||||
if (fresh == INVALID_HANDLE_VALUE) return engine;
|
||||
CloseHandle(engine);
|
||||
return fresh;
|
||||
}
|
||||
|
||||
File* inner() const { return file_; }
|
||||
@@ -1693,14 +1742,6 @@ public:
|
||||
|
||||
bool STDMETHODCALLTYPE IsComplete(UINT32 index) override { return array_->GetResult(index) != E_PENDING; }
|
||||
HRESULT STDMETHODCALLTYPE GetHResult(UINT32 index) override { return array_->GetResult(index); }
|
||||
HRESULT STDMETHODCALLTYPE StubSlot5() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageStatusArrayX", 5u, "Slot5");
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
HRESULT STDMETHODCALLTYPE StubSlot6() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageStatusArrayX", 6u, "Slot6");
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
|
||||
StatusArray* inner() const { return array_; }
|
||||
|
||||
@@ -1713,7 +1754,12 @@ private:
|
||||
|
||||
class XboxQueue final : public PublicObject<IXboxDStorageQueue> {
|
||||
public:
|
||||
XboxQueue(WorkQueue* queue, UINT64 source_type) : queue_(queue), source_type_(source_type) {}
|
||||
XboxQueue(WorkQueue* queue, const XboxQueueDesc& desc) : queue_(queue), desc_(desc) {
|
||||
if (desc.name) {
|
||||
std::snprintf(name_, sizeof(name_), "%s", desc.name);
|
||||
desc_.name = name_;
|
||||
}
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE EnqueueRequest(const XboxRequest* request) override {
|
||||
if (!request) {
|
||||
@@ -1721,11 +1767,12 @@ public:
|
||||
return;
|
||||
}
|
||||
const bool is_memory = (request->options & kXboxRequestSourceMemoryBit) != 0;
|
||||
if ((is_memory && source_type_ == 0) || (!is_memory && source_type_ != 0)) {
|
||||
if ((is_memory && desc_.source_type == 0) || (!is_memory && desc_.source_type != 0)) {
|
||||
reject(E_DSTORAGE_INVALID_SOURCE_TYPE, Kind::request, "EnqueueRequest");
|
||||
return;
|
||||
}
|
||||
log_request_options(request->options, request->source_size, request->destination_size, request->reserved34);
|
||||
log_request_options(request->options, request->source_size, request->destination_size,
|
||||
request->intermediate_size);
|
||||
// BCPack (BcpackMode, DSTORAGE_BCPACK_MODE 1-7) is decoded by the GDK's BCPack.dll when
|
||||
// BX_BCPACK_DLL names it; combined with zlib, or without the codec, it stays refused.
|
||||
const UINT bcpack_mode = static_cast<UINT>((request->options & kXboxRequestBcpackMask) >> 1);
|
||||
@@ -1821,7 +1868,13 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64, UINT64) override {
|
||||
// The header allows cancelled requests to complete anyway, and the engine does not cancel:
|
||||
// every request still runs. Logged with its tag so the trace shows it.
|
||||
void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64 mask, UINT64 value) override {
|
||||
char detail[96];
|
||||
std::snprintf(detail, sizeof(detail), "mask=0x%llx value=0x%llx: requests still run",
|
||||
static_cast<unsigned long long>(mask), static_cast<unsigned long long>(value));
|
||||
bx_log_emit(BX_LOG_INFO, "xcrdapi", "dstorage_cancel_requests", detail);
|
||||
BX_VTABLE_STUB_LOG("IDStorageQueueX", 7u, "CancelRequestsWithTag");
|
||||
}
|
||||
|
||||
@@ -1829,33 +1882,55 @@ public:
|
||||
|
||||
HANDLE STDMETHODCALLTYPE GetErrorEvent() override { return queue_->error_event(); }
|
||||
|
||||
void STDMETHODCALLTYPE RetrieveErrorRecord(void*) override {
|
||||
// The size and layout of the Xbox caller's output are unverified.
|
||||
// Do not write the public PC record into it.
|
||||
BX_VTABLE_STUB_LOG("IDStorageQueueX", 10u, "RetrieveErrorRecord");
|
||||
// FailureCount, FirstFailure.HResult and FirstFailure.CommandType are filled; the failed
|
||||
// command's parameters are left zeroed, as on the public queue.
|
||||
void STDMETHODCALLTYPE RetrieveErrorRecord(XboxErrorRecord* record) override {
|
||||
if (!record) return;
|
||||
std::memset(record, 0, sizeof(*record));
|
||||
ErrorRecord errors{};
|
||||
Kind first = Kind::request;
|
||||
queue_->take_errors(&errors, &first);
|
||||
record->failure_count = errors.failure_count > 0xffffffffull ? 0xffffffffu
|
||||
: static_cast<UINT32>(errors.failure_count);
|
||||
record->hresult = errors.failure_count ? errors.first_error : S_OK;
|
||||
record->command_type = errors.failure_count ? static_cast<INT32>(command_type(first))
|
||||
: static_cast<INT32>(DSTORAGE_COMMAND_TYPE_NONE);
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE Query(void*) override { BX_VTABLE_STUB_LOG("IDStorageQueueX", 11u, "Query"); }
|
||||
HRESULT STDMETHODCALLTYPE StubSlot12() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageQueueX", 12u, "Slot12");
|
||||
return E_NOTIMPL;
|
||||
void STDMETHODCALLTYPE Query(XboxQueueInfo* info) override {
|
||||
if (!info) return;
|
||||
XboxQueueInfo1 full{};
|
||||
Query1(&full);
|
||||
std::memset(info, 0, sizeof(*info));
|
||||
std::memcpy(info, &full.desc, kXboxQueueDescSize);
|
||||
info->empty_slot_count = full.empty_slot_count;
|
||||
info->request_count_until_auto_submit = full.request_count_until_auto_submit;
|
||||
}
|
||||
HRESULT STDMETHODCALLTYPE StubSlot13() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageQueueX", 13u, "Slot13");
|
||||
return E_NOTIMPL;
|
||||
|
||||
void STDMETHODCALLTYPE EnqueueSetEvent(HANDLE handle) override {
|
||||
if (!handle || handle == INVALID_HANDLE_VALUE) {
|
||||
reject(E_INVALIDARG, Kind::event, "EnqueueSetEvent");
|
||||
return;
|
||||
}
|
||||
accept(queue_->EnqueueSetEvent(handle), Kind::event, "EnqueueSetEvent");
|
||||
}
|
||||
HRESULT STDMETHODCALLTYPE StubSlot14() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageQueueX", 14u, "Slot14");
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
HRESULT STDMETHODCALLTYPE StubSlot15() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageQueueX", 15u, "Slot15");
|
||||
return E_NOTIMPL;
|
||||
|
||||
void STDMETHODCALLTYPE Query1(XboxQueueInfo1* info) override {
|
||||
if (!info) return;
|
||||
UINT empty = 0;
|
||||
UINT until_auto_submit = 0;
|
||||
queue_->counts(&empty, &until_auto_submit);
|
||||
std::memset(info, 0, sizeof(*info));
|
||||
info->desc = desc_;
|
||||
info->empty_slot_count = static_cast<UINT16>(empty);
|
||||
info->request_count_until_auto_submit = static_cast<UINT16>(until_auto_submit);
|
||||
}
|
||||
|
||||
private:
|
||||
~XboxQueue() override { queue_->Release(); }
|
||||
bool supports(REFIID riid) const override { return IsEqualIID(riid, kXboxQueueIid); }
|
||||
bool supports(REFIID riid) const override {
|
||||
return IsEqualIID(riid, kXboxQueueIid) || IsEqualIID(riid, kXboxQueue1Iid) || IsEqualIID(riid, kXboxQueue2Iid);
|
||||
}
|
||||
|
||||
void reject(HRESULT hr, Kind kind, const char* method) {
|
||||
char detail[96];
|
||||
@@ -1876,15 +1951,20 @@ private:
|
||||
}
|
||||
|
||||
WorkQueue* queue_;
|
||||
UINT64 source_type_;
|
||||
XboxQueueDesc desc_;
|
||||
char name_[64] = {};
|
||||
};
|
||||
|
||||
class XboxFactory final : public PublicObject<IXboxDStorageFactory> {
|
||||
public:
|
||||
HRESULT STDMETHODCALLTYPE CreateQueue(const void*, REFIID, void** ppv) override {
|
||||
if (ppv) *ppv = nullptr;
|
||||
BX_VTABLE_STUB_LOG("IDStorageFactoryX", 3u, "CreateQueue");
|
||||
return E_NOTIMPL;
|
||||
// A DSTORAGE_QUEUE_DESC is a DSTORAGE_QUEUE_DESC1 without Options: auto-submit stays on.
|
||||
HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) override {
|
||||
if (!ppv) return E_POINTER;
|
||||
*ppv = nullptr;
|
||||
if (!desc) return E_INVALIDARG;
|
||||
XboxQueueDesc full{};
|
||||
std::memcpy(&full, desc, kXboxQueueDescSize);
|
||||
return CreateQueue1(&full, riid, ppv);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) override {
|
||||
@@ -1926,13 +2006,16 @@ public:
|
||||
return publish(wrapper, riid, ppv);
|
||||
}
|
||||
|
||||
void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) override {
|
||||
if (flags) BX_VTABLE_STUB_LOG("IDStorageFactoryX", 6u, "SetDebugFlags");
|
||||
// The engine's work runs on the Win32 thread pool, which has no affinity to set.
|
||||
HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) override {
|
||||
char detail[64];
|
||||
std::snprintf(detail, sizeof(detail), "affinity=0x%llx has no effect", static_cast<unsigned long long>(affinity));
|
||||
bx_log_emit(BX_LOG_INFO, "xcrdapi", "dstorage_cpu_affinity", detail);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE StubSlot7() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageFactoryX", 7u, "Slot7");
|
||||
return E_NOTIMPL;
|
||||
void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) override {
|
||||
if (flags) BX_VTABLE_STUB_LOG("IDStorageFactoryX", 7u, "SetDebugFlags");
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) override {
|
||||
@@ -1943,12 +2026,15 @@ public:
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE StubSlot9() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageFactoryX", 9u, "Slot9");
|
||||
return E_NOTIMPL;
|
||||
// The engine keeps no DSTORAGE_STATS counters: the structure is written as zeros, and the
|
||||
// call logged so the trace shows that the values are not real.
|
||||
void STDMETHODCALLTYPE GetStats(void* stats) override {
|
||||
if (!stats) return;
|
||||
std::memset(stats, 0, kXboxStatsSize);
|
||||
BX_VTABLE_STUB_LOG("IDStorageFactoryX1", 9u, "GetStats");
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE CreateQueueX(const XboxQueueDesc* desc, REFIID riid, void** ppv) override {
|
||||
HRESULT STDMETHODCALLTYPE CreateQueue1(const XboxQueueDesc* desc, REFIID riid, void** ppv) override {
|
||||
if (!ppv) return E_POINTER;
|
||||
*ppv = nullptr;
|
||||
if (!desc) return E_INVALIDARG;
|
||||
@@ -1958,9 +2044,10 @@ public:
|
||||
if (desc->source_type > 1) {
|
||||
return E_DSTORAGE_INVALID_SOURCE_TYPE;
|
||||
}
|
||||
/* desc->priority is ignored on purpose: the Xbox priority range is not part of
|
||||
* the inferred contract, so no value is rejected (the public queue's
|
||||
* E_DSTORAGE_INVALID_QUEUE_PRIORITY range check is not assumed to apply). */
|
||||
// DSTORAGE_PRIORITY: LOW (-1) to REALTIME (2) (dstorage_xs.h). Queues are not prioritised.
|
||||
if (desc->priority < -1 || desc->priority > 2) {
|
||||
return E_DSTORAGE_INVALID_QUEUE_PRIORITY;
|
||||
}
|
||||
const UINT slots = desc->capacity - 1u;
|
||||
auto* entries = new (std::nothrow) Entry[slots];
|
||||
if (!entries) return E_OUTOFMEMORY;
|
||||
@@ -1985,7 +2072,7 @@ public:
|
||||
queue->Release();
|
||||
return error;
|
||||
}
|
||||
auto* wrapper = new (std::nothrow) XboxQueue(queue, desc->source_type);
|
||||
auto* wrapper = new (std::nothrow) XboxQueue(queue, *desc);
|
||||
if (!wrapper) {
|
||||
queue->Release();
|
||||
return E_OUTOFMEMORY;
|
||||
@@ -1993,24 +2080,14 @@ public:
|
||||
return publish(wrapper, riid, ppv);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE StubSlot11() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageFactoryX", 11u, "Slot11");
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE StubSlot12() override {
|
||||
BX_VTABLE_STUB_LOG("IDStorageFactoryX", 12u, "Slot12");
|
||||
return E_NOTIMPL;
|
||||
}
|
||||
|
||||
private:
|
||||
bool supports(REFIID riid) const override { return IsEqualIID(riid, kXboxFactoryIid); }
|
||||
bool supports(REFIID riid) const override { return is_xbox_factory_iid(riid); }
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// Accepts BetterXen's synthetic factory IID and IUnknown (the synthetic factory),
|
||||
// the public PC IDStorageFactory / IDStorageFactory1, and the inferred Xbox
|
||||
// IDStorageFactoryX IID. Any other IID is logged with its value so `bx-cli trace`
|
||||
// the public PC IDStorageFactory / IDStorageFactory1, and the Xbox IDStorageFactoryX, X1 and X2
|
||||
// IIDs (dstorage_xs.h). Any other IID is logged with its value so `bx-cli trace`
|
||||
// names the interface a guest asked for.
|
||||
extern "C" HRESULT WINAPI DStorageGetFactory(REFIID riid, void** ppv) {
|
||||
if (!ppv) return E_POINTER;
|
||||
@@ -2019,7 +2096,7 @@ extern "C" HRESULT WINAPI DStorageGetFactory(REFIID riid, void** ppv) {
|
||||
auto* factory = new (std::nothrow) PublicFactory;
|
||||
return factory ? publish(factory, riid, ppv) : E_OUTOFMEMORY;
|
||||
}
|
||||
if (IsEqualIID(riid, kXboxFactoryIid)) {
|
||||
if (is_xbox_factory_iid(riid)) {
|
||||
auto* factory = new (std::nothrow) XboxFactory;
|
||||
if (!factory) return E_OUTOFMEMORY;
|
||||
bx_log_emit(BX_LOG_INFO, "xcrdapi", "dstorage_get_factory", "dispatch xbox_com=IDStorageFactoryX");
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* zlib (RFC 1950) / deflate (RFC 1951) decoder; see zlib_inflate.h. */
|
||||
#include "zlib_inflate.h"
|
||||
|
||||
#include <new>
|
||||
|
||||
namespace {
|
||||
|
||||
struct Bits {
|
||||
@@ -84,19 +86,68 @@ constexpr uint16_t kDistBase[30] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65,
|
||||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
|
||||
constexpr uint8_t kDistExtra[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
|
||||
|
||||
// Writes the whole output into one buffer of `size` bytes.
|
||||
struct Output {
|
||||
uint8_t* data;
|
||||
size_t size;
|
||||
size_t pos = 0;
|
||||
|
||||
bool put(uint8_t byte) {
|
||||
if (pos >= size) return false;
|
||||
data[pos++] = byte;
|
||||
return true;
|
||||
}
|
||||
BxInflateResult copy(size_t distance, size_t length) {
|
||||
if (distance > pos) return BxInflateResult::bad_data;
|
||||
if (length > size - pos) return BxInflateResult::output_mismatch;
|
||||
for (size_t i = 0; i < length; ++i, ++pos) data[pos] = data[pos - distance];
|
||||
return BxInflateResult::ok;
|
||||
}
|
||||
size_t produced() const { return pos; }
|
||||
uint32_t adler() const;
|
||||
};
|
||||
|
||||
BxInflateResult codes(Bits& in, Output& out, const Huffman& lit, const Huffman& dist) {
|
||||
// Decodes a stream of any length, keeps only its first `limit` bytes at `data`, and checks
|
||||
// Adler-32 over all of it. Back-references read a 32 KiB ring of the latest output (RFC 1951:
|
||||
// distances are at most 32768), so memory does not grow with the stream (#769).
|
||||
struct PrefixOutput {
|
||||
static constexpr size_t kWindow = 32768;
|
||||
uint8_t* data;
|
||||
size_t limit;
|
||||
uint64_t total = 0;
|
||||
uint32_t a = 1, b = 0;
|
||||
uint32_t pending = 0; // bytes summed since the last modulo
|
||||
uint8_t window[kWindow];
|
||||
|
||||
bool put(uint8_t byte) {
|
||||
if (total < limit) data[total] = byte;
|
||||
window[total & (kWindow - 1)] = byte;
|
||||
++total;
|
||||
a += byte;
|
||||
b += a;
|
||||
if (++pending == 5552) { // largest n with no 32-bit overflow before the modulo
|
||||
a %= 65521u;
|
||||
b %= 65521u;
|
||||
pending = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
BxInflateResult copy(size_t distance, size_t length) {
|
||||
if (distance > total || distance > kWindow) return BxInflateResult::bad_data;
|
||||
for (size_t i = 0; i < length; ++i) put(window[(total - distance) & (kWindow - 1)]);
|
||||
return BxInflateResult::ok;
|
||||
}
|
||||
size_t produced() const { return static_cast<size_t>(total); }
|
||||
uint32_t adler() const { return ((b % 65521u) << 16) | (a % 65521u); }
|
||||
};
|
||||
|
||||
template<class Out>
|
||||
BxInflateResult codes(Bits& in, Out& out, const Huffman& lit, const Huffman& dist) {
|
||||
for (;;) {
|
||||
const int symbol = lit.decode(in);
|
||||
if (symbol < 0) return in.overrun ? BxInflateResult::truncated : BxInflateResult::bad_data;
|
||||
if (symbol < 256) {
|
||||
if (out.pos >= out.size) return BxInflateResult::output_mismatch;
|
||||
out.data[out.pos++] = static_cast<uint8_t>(symbol);
|
||||
if (!out.put(static_cast<uint8_t>(symbol))) return BxInflateResult::output_mismatch;
|
||||
continue;
|
||||
}
|
||||
if (symbol == 256) return BxInflateResult::ok;
|
||||
@@ -108,13 +159,13 @@ BxInflateResult codes(Bits& in, Output& out, const Huffman& lit, const Huffman&
|
||||
if (ds >= 30) return BxInflateResult::bad_data;
|
||||
const size_t distance = kDistBase[ds] + in.get(kDistExtra[ds]);
|
||||
if (in.overrun) return BxInflateResult::truncated;
|
||||
if (distance > out.pos) return BxInflateResult::bad_data;
|
||||
if (length > out.size - out.pos) return BxInflateResult::output_mismatch;
|
||||
for (size_t i = 0; i < length; ++i, ++out.pos) out.data[out.pos] = out.data[out.pos - distance];
|
||||
const BxInflateResult r = out.copy(distance, length);
|
||||
if (r != BxInflateResult::ok) return r;
|
||||
}
|
||||
}
|
||||
|
||||
BxInflateResult fixed_block(Bits& in, Output& out) {
|
||||
template<class Out>
|
||||
BxInflateResult fixed_block(Bits& in, Out& out) {
|
||||
static Huffman lit, dist;
|
||||
static bool built = [] {
|
||||
uint8_t lengths[288];
|
||||
@@ -133,7 +184,8 @@ BxInflateResult fixed_block(Bits& in, Output& out) {
|
||||
return codes(in, out, lit, dist);
|
||||
}
|
||||
|
||||
BxInflateResult dynamic_block(Bits& in, Output& out) {
|
||||
template<class Out>
|
||||
BxInflateResult dynamic_block(Bits& in, Out& out) {
|
||||
const int hlit = static_cast<int>(in.get(5)) + 257;
|
||||
const int hdist = static_cast<int>(in.get(5)) + 1;
|
||||
const int hclen = static_cast<int>(in.get(4)) + 4;
|
||||
@@ -189,17 +241,19 @@ uint32_t adler32(const uint8_t* data, size_t size) {
|
||||
return (b << 16) | a;
|
||||
}
|
||||
|
||||
uint32_t Output::adler() const { return adler32(data, pos); }
|
||||
|
||||
} // namespace
|
||||
|
||||
BxInflateResult inflate_impl(const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size, size_t* produced) {
|
||||
template<class Out>
|
||||
BxInflateResult inflate_stream(const uint8_t* input, size_t input_size, Out& out, size_t* produced) {
|
||||
// RFC 1950 2.2: CMF (method 8 = deflate, window <= 32 KiB), FLG (check bits, no FDICT).
|
||||
if (!input || input_size < 6 || (!output && output_size)) return BxInflateResult::bad_header;
|
||||
if (!input || input_size < 6) return BxInflateResult::bad_header;
|
||||
const uint8_t cmf = input[0], flg = input[1];
|
||||
if ((cmf & 0x0F) != 8 || (cmf >> 4) > 7 || ((cmf << 8) | flg) % 31 != 0 || (flg & 0x20)) {
|
||||
return BxInflateResult::bad_header;
|
||||
}
|
||||
Bits in{input + 2, input_size - 2};
|
||||
Output out{output, output_size};
|
||||
for (;;) {
|
||||
const uint32_t final_block = in.get(1);
|
||||
const uint32_t type = in.get(2);
|
||||
@@ -212,8 +266,9 @@ BxInflateResult inflate_impl(const uint8_t* input, size_t input_size, uint8_t* o
|
||||
if ((len ^ 0xFFFFu) != nlen) return BxInflateResult::bad_data;
|
||||
// After aligning, the bit buffer is empty: copy straight from the byte stream.
|
||||
if (len > in.size - in.pos) return BxInflateResult::truncated;
|
||||
if (len > out.size - out.pos) return BxInflateResult::output_mismatch;
|
||||
for (uint32_t i = 0; i < len; ++i) out.data[out.pos++] = in.data[in.pos++];
|
||||
for (uint32_t i = 0; i < len; ++i) {
|
||||
if (!out.put(in.data[in.pos++])) return BxInflateResult::output_mismatch;
|
||||
}
|
||||
r = BxInflateResult::ok;
|
||||
} else if (type == 1) {
|
||||
r = fixed_block(in, out);
|
||||
@@ -225,8 +280,7 @@ BxInflateResult inflate_impl(const uint8_t* input, size_t input_size, uint8_t* o
|
||||
if (r != BxInflateResult::ok) return r;
|
||||
if (final_block) break;
|
||||
}
|
||||
if (produced) *produced = out.pos;
|
||||
else if (out.pos != out.size) return BxInflateResult::output_mismatch;
|
||||
if (produced) *produced = out.produced();
|
||||
// RFC 1950: the Adler-32 trailer follows the deflate data, byte-aligned, most significant first.
|
||||
in.align_to_byte();
|
||||
const size_t trailer = in.pos - static_cast<size_t>(in.count / 8);
|
||||
@@ -234,16 +288,34 @@ BxInflateResult inflate_impl(const uint8_t* input, size_t input_size, uint8_t* o
|
||||
const uint8_t* t = in.data + trailer;
|
||||
const uint32_t expected = (static_cast<uint32_t>(t[0]) << 24) | (static_cast<uint32_t>(t[1]) << 16) |
|
||||
(static_cast<uint32_t>(t[2]) << 8) | t[3];
|
||||
return adler32(output, out.pos) == expected ? BxInflateResult::ok : BxInflateResult::bad_checksum;
|
||||
return out.adler() == expected ? BxInflateResult::ok : BxInflateResult::bad_checksum;
|
||||
}
|
||||
|
||||
BxInflateResult bx_zlib_inflate(const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) {
|
||||
return inflate_impl(input, input_size, output, output_size, nullptr);
|
||||
if (!output && output_size) return BxInflateResult::bad_header;
|
||||
Output out{output, output_size};
|
||||
size_t produced = 0;
|
||||
const BxInflateResult r = inflate_stream(input, input_size, out, &produced);
|
||||
if (r == BxInflateResult::ok && produced != output_size) return BxInflateResult::output_mismatch;
|
||||
return r;
|
||||
}
|
||||
|
||||
BxInflateResult bx_zlib_inflate_any(const uint8_t* input, size_t input_size, uint8_t* output, size_t capacity, size_t* produced) {
|
||||
*produced = 0;
|
||||
return inflate_impl(input, input_size, output, capacity, produced);
|
||||
if (!output && capacity) return BxInflateResult::bad_header;
|
||||
Output out{output, capacity};
|
||||
return inflate_stream(input, input_size, out, produced);
|
||||
}
|
||||
|
||||
BxInflateResult bx_zlib_inflate_prefix(const uint8_t* input, size_t input_size, uint8_t* output, size_t prefix_size,
|
||||
size_t* produced) {
|
||||
*produced = 0;
|
||||
if (!output && prefix_size) return BxInflateResult::bad_header;
|
||||
auto* out = new (std::nothrow) PrefixOutput{output, prefix_size};
|
||||
if (!out) return BxInflateResult::output_mismatch;
|
||||
const BxInflateResult r = inflate_stream(input, input_size, *out, produced);
|
||||
delete out;
|
||||
return r;
|
||||
}
|
||||
|
||||
const char* bx_inflate_result_name(BxInflateResult result) {
|
||||
|
||||
@@ -29,4 +29,10 @@ BxInflateResult bx_zlib_inflate(const uint8_t* input, size_t input_size, uint8_t
|
||||
(checksum verified over those bytes). */
|
||||
BxInflateResult bx_zlib_inflate_any(const uint8_t* input, size_t input_size, uint8_t* output, size_t capacity, size_t* produced);
|
||||
|
||||
/* Decodes the whole of `input`, checks its Adler-32, writes only its first `prefix_size` bytes to
|
||||
`output` and reports how many bytes the stream produced. Memory use does not depend on the
|
||||
stream's length. */
|
||||
BxInflateResult bx_zlib_inflate_prefix(const uint8_t* input, size_t input_size, uint8_t* output, size_t prefix_size,
|
||||
size_t* produced);
|
||||
|
||||
const char* bx_inflate_result_name(BxInflateResult result);
|
||||
@@ -0,0 +1,5 @@
|
||||
# AllocateFromCommandAllocatorX returns memory with the command list's lifetime (#806). Needs a host
|
||||
# with the Xbox-layout device interface; skips without.
|
||||
add_executable(test_d3d12_allocate_from_allocator main.cpp)
|
||||
target_compile_definitions(test_d3d12_allocate_from_allocator PRIVATE WIDL_EXPLICIT_AGGREGATE_RETURNS)
|
||||
target_link_libraries(test_d3d12_allocate_from_allocator PRIVATE d3d12_xs kernel32 uuid)
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Conformance test: ID3D12GraphicsCommandList8::AllocateFromCommandAllocatorX (#806).
|
||||
*
|
||||
* Contract (authorized GDK d3d12_xs.h and its reference page): void* AllocateFromCommandAllocatorX(
|
||||
* UINT sizeBytes, UINT alignmentBytes) allocates memory "with the same lifetime as the associated
|
||||
* command list", for data written on the CPU timeline and read on the GPU timeline. The result "is a
|
||||
* valid GPU pointer and may be cast to D3D12_GPU_VIRTUAL_ADDRESS"; it is null only when memory is
|
||||
* unavailable. The target build's ray-tracing BVH builder throws when it gets null, and the process
|
||||
* then ends (status 99).
|
||||
*
|
||||
* Checked, from the contract alone:
|
||||
* 1. Allocations of 100 bytes at 256-byte alignment, 64 at 16, and 3 MiB at 64 KiB are non-null,
|
||||
* aligned as asked, do not overlap, and hold what the program writes into them.
|
||||
* 2. After the list is closed, the allocator reset (its lists are done) and the list reset with
|
||||
* it, a new allocation is non-null and writable.
|
||||
* 3. The method is not logged as an unimplemented slot.
|
||||
*
|
||||
* Slots (Xbox-layout tables, ADR 0009): 2 Release; device 9 CreateCommandAllocator,
|
||||
* 73 CreateCommandListX; command allocator 8 Reset; command list 9 Close, 10 Reset,
|
||||
* 148 AllocateFromCommandAllocatorX.
|
||||
*/
|
||||
#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, kDevCreateCommandListX = 73;
|
||||
constexpr int kAllocatorReset = 8, kListClose = 9, kListReset = 10, kListAllocate = 148;
|
||||
|
||||
int g_checks = 0, g_failed = 0;
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
++g_checks; \
|
||||
if (!(cond)) { \
|
||||
++g_failed; \
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_allocate_from_allocator:%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 ListDescX {
|
||||
D3D12_COMMAND_LIST_TYPE Type;
|
||||
UINT Flags, NodeMask, ResourceBarrierBatchSize;
|
||||
};
|
||||
using ReleaseFn = ULONG(STDMETHODCALLTYPE*)(void*);
|
||||
using CreateAllocatorFn = HRESULT(STDMETHODCALLTYPE*)(void*, D3D12_COMMAND_LIST_TYPE, REFIID, void**);
|
||||
using CreateListXFn = HRESULT(STDMETHODCALLTYPE*)(void*, const ListDescX*, void*, void*, REFIID, void**);
|
||||
using AllocatorResetFn = HRESULT(STDMETHODCALLTYPE*)(void*);
|
||||
using CloseFn = HRESULT(STDMETHODCALLTYPE*)(void*);
|
||||
using ListResetFn = HRESULT(STDMETHODCALLTYPE*)(void*, void*, void*);
|
||||
using AllocateFn = void*(STDMETHODCALLTYPE*)(void*, UINT, UINT);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool fill_and_check(void* p, size_t n, uint8_t seed) {
|
||||
auto* b = static_cast<uint8_t*>(p);
|
||||
for (size_t i = 0; i < n; ++i) b[i] = static_cast<uint8_t>(i * 13 + seed);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (b[i] != static_cast<uint8_t>(i * 13 + seed)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
setvbuf(stdout, nullptr, _IONBF, 0);
|
||||
alignas(8) unsigned char title_params[80] = {};
|
||||
const UINT version = 0x20004; // a non-zero version selects the Xbox-layout device
|
||||
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_allocate_from_allocator: 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();
|
||||
|
||||
void* allocator = nullptr;
|
||||
void* list = nullptr;
|
||||
const ListDescX ldesc{D3D12_COMMAND_LIST_TYPE_DIRECT, 0, 0, 0};
|
||||
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);
|
||||
}
|
||||
if (!allocator || !list) {
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_allocate_from_allocator: setup\n");
|
||||
return 1;
|
||||
}
|
||||
auto allocate = slot<AllocateFn>(list, kListAllocate);
|
||||
|
||||
// 1. Three allocations, aligned as asked, not overlapping, writable.
|
||||
void* a = allocate(list, 100, 256);
|
||||
void* b = allocate(list, 64, 16);
|
||||
const UINT big = 3u << 20;
|
||||
void* c = allocate(list, big, 65536);
|
||||
CHECK(a && (reinterpret_cast<uintptr_t>(a) % 256) == 0, "100 bytes at 256-byte alignment: %p", a);
|
||||
CHECK(b && (reinterpret_cast<uintptr_t>(b) % 16) == 0, "64 bytes at 16-byte alignment: %p", b);
|
||||
CHECK(c && (reinterpret_cast<uintptr_t>(c) % 65536) == 0, "3 MiB at 64 KiB alignment: %p", c);
|
||||
if (a && b && c) {
|
||||
const auto ua = reinterpret_cast<uintptr_t>(a), ub = reinterpret_cast<uintptr_t>(b), uc = reinterpret_cast<uintptr_t>(c);
|
||||
CHECK((ua + 100 <= ub || ub + 64 <= ua) && (ua + 100 <= uc || uc + big <= ua) && (ub + 64 <= uc || uc + big <= ub),
|
||||
"the three allocations do not overlap");
|
||||
CHECK(fill_and_check(a, 100, 1) && fill_and_check(b, 64, 2) && fill_and_check(c, big, 3), "each allocation holds what is written");
|
||||
CHECK(static_cast<uint8_t*>(a)[0] == 1 && static_cast<uint8_t*>(a)[99] == static_cast<uint8_t>(99 * 13 + 1),
|
||||
"writing the others left the first allocation intact");
|
||||
}
|
||||
|
||||
// 2. After the allocator's lists are done: reset, record again, allocate again.
|
||||
CHECK(slot<CloseFn>(list, kListClose)(list) == S_OK, "list Close");
|
||||
CHECK(slot<AllocatorResetFn>(allocator, kAllocatorReset)(allocator) == S_OK, "allocator Reset");
|
||||
CHECK(slot<ListResetFn>(list, kListReset)(list, allocator, nullptr) == S_OK, "list Reset with the allocator");
|
||||
void* d = allocate(list, 128, 64);
|
||||
CHECK(d && (reinterpret_cast<uintptr_t>(d) % 64) == 0 && fill_and_check(d, 128, 4),
|
||||
"after the reset: 128 bytes at 64-byte alignment, writable (%p)", d);
|
||||
|
||||
// 3. Not an unimplemented slot.
|
||||
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[148]::AllocateFromCommandAllocatorX") == std::string::npos,
|
||||
"AllocateFromCommandAllocatorX is not logged as an unimplemented slot");
|
||||
} else {
|
||||
std::printf("note: no BX_LOG_FILE; the log check did not run\n");
|
||||
}
|
||||
|
||||
slot<CloseFn>(list, kListClose)(list);
|
||||
slot<ReleaseFn>(list, kRelease)(list);
|
||||
slot<ReleaseFn>(allocator, kRelease)(allocator);
|
||||
slot<ReleaseFn>(dev, kRelease)(dev);
|
||||
if (g_failed) {
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_allocate_from_allocator (%d of %d checks failed)\n", g_failed, g_checks);
|
||||
return 1;
|
||||
}
|
||||
std::printf("PASS: test_d3d12_allocate_from_allocator (%d of %d checks passed)\n", g_checks, g_checks);
|
||||
return 0;
|
||||
}
|
||||
@@ -73,6 +73,15 @@
|
||||
extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(PVOID adapter, const void* params, REFIID riid,
|
||||
void** ppv);
|
||||
|
||||
// Built twice: as test_d3d12_console_texture_contents, and with BX_TEST_RESERVED_TEXTURES as
|
||||
// test_d3d12_console_texture_contents_reserved, where every read-only texture is a reserved
|
||||
// resource whose memory is mapped when its contents are first uploaded for a draw (#782).
|
||||
#ifdef BX_TEST_RESERVED_TEXTURES
|
||||
#define BX_TEST_NAME "test_d3d12_console_texture_contents_reserved"
|
||||
#else
|
||||
#define BX_TEST_NAME "test_d3d12_console_texture_contents"
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kRelease = 2;
|
||||
@@ -115,7 +124,7 @@ int g_failed = 0;
|
||||
++g_checks; \
|
||||
if (!(cond)) { \
|
||||
++g_failed; \
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_console_texture_contents:%d: ", __LINE__); \
|
||||
std::fprintf(stderr, "FAIL: " BX_TEST_NAME ":%d: ", __LINE__); \
|
||||
std::fprintf(stderr, __VA_ARGS__); \
|
||||
std::fprintf(stderr, "\n"); \
|
||||
} \
|
||||
@@ -320,6 +329,9 @@ int main() {
|
||||
std::snprintf(cache, sizeof(cache), "%sbx-shader-cache-%lu-%lu", temp, static_cast<unsigned long>(GetCurrentProcessId()),
|
||||
static_cast<unsigned long>(GetTickCount()));
|
||||
SetEnvironmentVariableA("BX_CONSOLE_SHADERS", "1");
|
||||
#ifdef BX_TEST_RESERVED_TEXTURES
|
||||
SetEnvironmentVariableA("BX_LAZY_TEXTURES_MIN", "0"); // even these small textures are reserved
|
||||
#endif
|
||||
SetEnvironmentVariableA("BX_SHADER_CACHE", cache);
|
||||
|
||||
// The stand-in XG, loaded as xg_xs.dll before the device exists (as bx-cli stages the GDK's).
|
||||
@@ -330,7 +342,7 @@ int main() {
|
||||
wcscpy(slash + 1, L"texture_contents_fake_xg\\xg_xs.dll");
|
||||
HMODULE fake = LoadLibraryW(path);
|
||||
if (!fake) {
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_console_texture_contents: the stand-in XG library did not load (error %lu)\n",
|
||||
std::fprintf(stderr, "FAIL: " BX_TEST_NAME ": the stand-in XG library did not load (error %lu)\n",
|
||||
GetLastError());
|
||||
return 1;
|
||||
}
|
||||
@@ -344,7 +356,7 @@ int main() {
|
||||
void* dev = nullptr;
|
||||
HRESULT hr = D3D12XboxCreateDevice(nullptr, title_params, __uuidof(ID3D12Device), &dev);
|
||||
if (hr == E_NOINTERFACE) {
|
||||
std::printf("SKIP: test_d3d12_console_texture_contents: host lacks the Xbox-layout device interface (E_NOINTERFACE)\n");
|
||||
std::printf("SKIP: " BX_TEST_NAME ": host lacks the Xbox-layout device interface (E_NOINTERFACE)\n");
|
||||
return 77;
|
||||
}
|
||||
CHECK(hr == S_OK && dev != nullptr, "Xbox-layout device creation hr=0x%08lx", static_cast<unsigned long>(hr));
|
||||
@@ -355,7 +367,7 @@ int main() {
|
||||
: nullptr;
|
||||
HMODULE dxc = LoadLibraryA("dxcompiler.dll");
|
||||
if (!serialize || !dxc) {
|
||||
std::printf("SKIP: test_d3d12_console_texture_contents: %s\n",
|
||||
std::printf("SKIP: " BX_TEST_NAME ": %s\n",
|
||||
dxc ? "no root-signature serializer in this bottle"
|
||||
: "no dxcompiler.dll beside the shims (run with bx-cli run --gdk-libraries)");
|
||||
return 77;
|
||||
@@ -514,7 +526,7 @@ int main() {
|
||||
bool made = queue && allocator && list && fence && root && pipeline && textures[0] && textures[1] && upload && rtv_heap && view_heap;
|
||||
for (int i = 0; i < kViews; ++i) made = made && targets[i] && readbacks[i];
|
||||
if (!made) {
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_console_texture_contents: setup\n");
|
||||
std::fprintf(stderr, "FAIL: " BX_TEST_NAME ": setup\n");
|
||||
return 1;
|
||||
}
|
||||
// Console descriptor handles are addresses; an RTV descriptor is 112 bytes there.
|
||||
@@ -709,6 +721,14 @@ int main() {
|
||||
count_of(mine, "copied again: its title memory changed"));
|
||||
CHECK(count_of(mine, "CreatePlacedResourceX(texture contents)") == 0,
|
||||
"no texture-contents stub is logged with translation on (%zu)", count_of(mine, "CreatePlacedResourceX(texture contents)"));
|
||||
#ifdef BX_TEST_RESERVED_TEXTURES
|
||||
// The three textures whose contents were copied got their memory first; the one copied
|
||||
// from unreadable memory never did.
|
||||
CHECK(count_of(mine, "texture memory mapped at first use") == 3, "three textures mapped at first use (%zu)",
|
||||
count_of(mine, "texture memory mapped at first use"));
|
||||
CHECK(count_of(mine, "refused a reserved texture") == 0 && count_of(mine, "not mapped") == 0,
|
||||
"no reserved texture refused and no mapping failed");
|
||||
#endif
|
||||
} else {
|
||||
std::printf("note: no BX_LOG_FILE; the log checks did not run\n");
|
||||
}
|
||||
@@ -719,7 +739,7 @@ int main() {
|
||||
slot<ReleaseFn>(object, kRelease)(object);
|
||||
}
|
||||
VirtualFree(memory, 0, MEM_RELEASE);
|
||||
std::printf("%s: test_d3d12_console_texture_contents (%d of %d checks passed)\n", g_failed ? "FAIL" : "PASS",
|
||||
std::printf("%s: " BX_TEST_NAME " (%d of %d checks passed)\n", g_failed ? "FAIL" : "PASS",
|
||||
g_checks - g_failed, g_checks);
|
||||
return g_failed ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# test_d3d12_console_texture_contents with every read-only texture made reserved, its memory mapped
|
||||
# when its contents are first uploaded for a draw (#782). Same program, same expectations, plus the
|
||||
# mappings in the log. Needs what that test needs; skips without.
|
||||
add_executable(test_d3d12_console_texture_contents_reserved ../test_d3d12_console_texture_contents/main.cpp)
|
||||
target_compile_definitions(test_d3d12_console_texture_contents_reserved PRIVATE WIDL_EXPLICIT_AGGREGATE_RETURNS BX_TEST_RESERVED_TEXTURES)
|
||||
target_link_libraries(test_d3d12_console_texture_contents_reserved PRIVATE d3d12_xs kernel32 uuid)
|
||||
add_dependencies(test_d3d12_console_texture_contents_reserved test_texture_contents_fake_xg)
|
||||
@@ -198,6 +198,37 @@ int main() {
|
||||
execute(queue, 1, &lists[3]);
|
||||
CHECK(becomes(e, 2u, 5000), "case 3: E=%u, expected 2", *e);
|
||||
|
||||
// 5. A two-queue handshake: queue 0's list writes F, then waits for G, then writes H; queue 1's
|
||||
// list waits for F, then writes G. On the console this completes at once. Treating every wait
|
||||
// as at its list's start made queue 0 wait for G before writing F: each queue held the other
|
||||
// until the 10 s release (seen on the target build).
|
||||
void* queue1 = nullptr;
|
||||
slot<CreateQueueXFn>(dev, kDevCreateCommandQueueX)(dev, &qdesc, __uuidof(ID3D12CommandQueue), &queue1);
|
||||
void* hs_allocators[2] = {};
|
||||
void* hs_lists[2] = {make_list(dev, &hs_allocators[0]), make_list(dev, &hs_allocators[1])};
|
||||
CHECK(queue1 && hs_lists[0] && hs_lists[1], "case 5: second queue and lists");
|
||||
volatile uint64_t* f = cells + 5;
|
||||
volatile uint64_t* g = cells + 6;
|
||||
volatile uint64_t* h = cells + 7;
|
||||
double case5_ms = -1;
|
||||
if (queue1 && hs_lists[0] && hs_lists[1]) {
|
||||
slot<Write64Fn>(hs_lists[0], kListWrite64Bop)(hs_lists[0], va(f), 1, 0, 0);
|
||||
slot<Wait64Fn>(hs_lists[0], kListWait64)(hs_lists[0], va(g), D3D12_COMPARISON_FUNC_GREATER_EQUAL, 1, 0);
|
||||
slot<Write64Fn>(hs_lists[0], kListWrite64Bop)(hs_lists[0], va(h), 1, 0, 0);
|
||||
CHECK(slot<CloseFn>(hs_lists[0], kListClose)(hs_lists[0]) == S_OK, "case 5: list on queue 0 Close");
|
||||
slot<Wait64Fn>(hs_lists[1], kListWait64)(hs_lists[1], va(f), D3D12_COMPARISON_FUNC_GREATER_EQUAL, 1, 0);
|
||||
slot<Write64Fn>(hs_lists[1], kListWrite64Bop)(hs_lists[1], va(g), 1, 0, 0);
|
||||
CHECK(slot<CloseFn>(hs_lists[1], kListClose)(hs_lists[1]) == S_OK, "case 5: list on queue 1 Close");
|
||||
const ULONGLONG t0 = GetTickCount64();
|
||||
execute(queue, 1, &hs_lists[0]);
|
||||
slot<ExecuteFn>(queue1, kQueueExecuteCommandLists)(queue1, 1, &hs_lists[1]);
|
||||
CHECK(becomes(h, uint64_t{1}, 3000), "case 5: the handshake did not complete within 3 s (F=%llu G=%llu H=%llu)",
|
||||
static_cast<unsigned long long>(*f), static_cast<unsigned long long>(*g), static_cast<unsigned long long>(*h));
|
||||
case5_ms = static_cast<double>(GetTickCount64() - t0);
|
||||
CHECK(*f == 1 && *g == 1, "case 5: F=%llu G=%llu, expected 1 and 1", static_cast<unsigned long long>(*f),
|
||||
static_cast<unsigned long long>(*g));
|
||||
}
|
||||
|
||||
// 4. No wait ran into the 10 s limit.
|
||||
const std::string log = read_log();
|
||||
if (!log.empty()) {
|
||||
@@ -207,7 +238,12 @@ int main() {
|
||||
std::printf("note: no BX_LOG_FILE; case 4 did not run\n");
|
||||
}
|
||||
|
||||
std::printf("case 1 Execute: %.1f ms, case 2 Execute: %.1f ms\n", case1_ms, case2_ms);
|
||||
std::printf("case 1 Execute: %.1f ms, case 2 Execute: %.1f ms, case 5 handshake: %.0f ms\n", case1_ms, case2_ms, case5_ms);
|
||||
for (int i = 1; i >= 0; --i) {
|
||||
if (hs_lists[i]) slot<ReleaseFn>(hs_lists[i], kRelease)(hs_lists[i]);
|
||||
if (hs_allocators[i]) slot<ReleaseFn>(hs_allocators[i], kRelease)(hs_allocators[i]);
|
||||
}
|
||||
if (queue1) slot<ReleaseFn>(queue1, kRelease)(queue1);
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
slot<ReleaseFn>(lists[i], kRelease)(lists[i]);
|
||||
slot<ReleaseFn>(allocators[i], kRelease)(allocators[i]);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# Read-only placed textures get host memory at first use, and a title's GPU copy into one keeps its
|
||||
# data (#782). Needs a host with the Xbox-layout device interface; skips without.
|
||||
add_executable(test_d3d12_lazy_texture_memory main.cpp)
|
||||
target_compile_definitions(test_d3d12_lazy_texture_memory PRIVATE WIDL_EXPLICIT_AGGREGATE_RETURNS)
|
||||
target_link_libraries(test_d3d12_lazy_texture_memory PRIVATE d3d12_xs kernel32 uuid)
|
||||
@@ -0,0 +1,297 @@
|
||||
/*
|
||||
* Conformance test: host memory for read-only placed textures is mapped at first use (#782).
|
||||
*
|
||||
* d3d12_xs backs a texture a title places in its memory (CreatePlacedResourceX) with a host
|
||||
* texture. A read-only 2D texture of at least 64 KiB is now a reserved resource whose memory is
|
||||
* mapped when a draw first needs its contents or when a GPU copy or resolve of the title writes it.
|
||||
* A write into a reserved texture without memory is dropped, so the title's copies must map first.
|
||||
*
|
||||
* The program places five RGBA8 textures in its own memory (VirtualAlloc, 1 MiB apart):
|
||||
* T1, T2, T5: 512x512, no flags (1 MiB each): reserved;
|
||||
* T3: 64x64 (16 KiB): under 64 KiB, committed as before;
|
||||
* T4: 512x512 with ALLOW_RENDER_TARGET: the GPU writes it, committed as before.
|
||||
* One command list then copies a pattern from an upload buffer into T1 (CopyTextureRegion), copies
|
||||
* T1 into T5 (CopyResource), and copies T1 and T5 into two readback buffers. T2 is never used.
|
||||
* Expected, from the program alone:
|
||||
* - both readbacks hold the pattern (the copies' writes were kept);
|
||||
* - the shim logs that read-only textures are reserved, and exactly two first-use mappings, the
|
||||
* second with `reserved=3 mapped=2` (T1, T2 and T5 reserved; T1 and T5 mapped; T2 never).
|
||||
* Before #782 every texture was committed: no such log lines (the readbacks pass).
|
||||
*
|
||||
* Slots (Xbox-layout tables, ADR 0009): 2 Release; device 9 CreateCommandAllocator,
|
||||
* 27 CreateCommittedResource, 36 CreateFence, 44 CreatePlacedResourceX, 48 CreateCommandQueueX,
|
||||
* 73 CreateCommandListX; resource 8 Map; command list 9 Close, 16 CopyTextureRegion,
|
||||
* 17 CopyResource, 26 ResourceBarrier; 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 kDevCreatePlacedResourceX = 44, kDevCreateCommandQueueX = 48, kDevCreateCommandListX = 73;
|
||||
constexpr int kResMap = 8;
|
||||
constexpr int kListClose = 9, kListCopyTextureRegion = 16, kListCopyResource = 17, kListResourceBarrier = 26;
|
||||
constexpr int kQueueExecuteCommandLists = 10, kQueueSignal = 16, kFenceSetEventOnCompletion = 9;
|
||||
constexpr UINT kSize = 512, kPitch = kSize * 4;
|
||||
constexpr UINT64 kBytes = UINT64(kPitch) * kSize;
|
||||
constexpr UINT64 kApart = 1 << 20;
|
||||
|
||||
int g_checks = 0, g_failed = 0;
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
++g_checks; \
|
||||
if (!(cond)) { \
|
||||
++g_failed; \
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_lazy_texture_memory:%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 CreatePlacedXFn = HRESULT(STDMETHODCALLTYPE*)(void*, D3D12_GPU_VIRTUAL_ADDRESS, const D3D12_RESOURCE_DESC*,
|
||||
D3D12_RESOURCE_STATES, const D3D12_CLEAR_VALUE*, 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 CopyRegionFn = void(STDMETHODCALLTYPE*)(void*, const D3D12_TEXTURE_COPY_LOCATION*, UINT, UINT, UINT,
|
||||
const D3D12_TEXTURE_COPY_LOCATION*, const D3D12_BOX*);
|
||||
using CopyResourceFn = void(STDMETHODCALLTYPE*)(void*, void*, void*);
|
||||
using BarrierFn = UINT(STDMETHODCALLTYPE*)(void*, UINT, const D3D12_RESOURCE_BARRIER*);
|
||||
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;
|
||||
}
|
||||
|
||||
size_t count_of(const std::string& text, const char* needle) {
|
||||
size_t n = 0;
|
||||
for (size_t pos = text.find(needle); pos != std::string::npos; pos = text.find(needle, pos + 1)) ++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_DESC texture(UINT size, D3D12_RESOURCE_FLAGS flags) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
d.Width = size;
|
||||
d.Height = size;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x100); // the console's linear layout
|
||||
d.Flags = flags;
|
||||
return d;
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_BARRIER transition(void* resource, D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after) {
|
||||
D3D12_RESOURCE_BARRIER b{};
|
||||
b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||
b.Transition.pResource = static_cast<ID3D12Resource*>(resource);
|
||||
b.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
||||
b.Transition.StateBefore = before;
|
||||
b.Transition.StateAfter = after;
|
||||
return b;
|
||||
}
|
||||
|
||||
uint8_t pattern(UINT64 i) {
|
||||
return static_cast<uint8_t>(i * 7 + (i >> 11) * 3 + 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
setvbuf(stdout, nullptr, _IONBF, 0);
|
||||
alignas(8) unsigned char title_params[80] = {};
|
||||
const UINT version = 0x20004; // a non-zero version selects the Xbox-layout device
|
||||
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_lazy_texture_memory: host lacks the Xbox-layout device interface (E_NOINTERFACE)\n");
|
||||
return 77;
|
||||
}
|
||||
CHECK(hr == S_OK && dev != nullptr, "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);
|
||||
|
||||
// Upload and readback buffers.
|
||||
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* upload = nullptr;
|
||||
void* readback1 = nullptr;
|
||||
void* readback5 = nullptr;
|
||||
committed(dev, &upload_heap, D3D12_HEAP_FLAG_NONE, &buf, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &upload);
|
||||
committed(dev, &readback_heap, D3D12_HEAP_FLAG_NONE, &buf, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, __uuidof(ID3D12Resource), &readback1);
|
||||
committed(dev, &readback_heap, D3D12_HEAP_FLAG_NONE, &buf, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, __uuidof(ID3D12Resource), &readback5);
|
||||
|
||||
// Five placed textures in the program's own memory, 1 MiB apart.
|
||||
auto* memory = static_cast<uint8_t*>(VirtualAlloc(nullptr, 5 * kApart, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
|
||||
if (!queue || !allocator || !list || !fence || !upload || !readback1 || !readback5 || !memory) {
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_lazy_texture_memory: setup\n");
|
||||
return 1;
|
||||
}
|
||||
const uint64_t base = reinterpret_cast<uint64_t>(memory);
|
||||
auto placed = slot<CreatePlacedXFn>(dev, kDevCreatePlacedResourceX);
|
||||
void* t[5] = {};
|
||||
const D3D12_RESOURCE_DESC read_only = texture(kSize, D3D12_RESOURCE_FLAG_NONE);
|
||||
const D3D12_RESOURCE_DESC small = texture(64, D3D12_RESOURCE_FLAG_NONE);
|
||||
const D3D12_RESOURCE_DESC target = texture(kSize, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
|
||||
const D3D12_RESOURCE_DESC* descs[5] = {&read_only, &read_only, &small, &target, &read_only};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
hr = placed(dev, base + i * kApart, descs[i], D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), &t[i]);
|
||||
CHECK(hr == S_OK && t[i], "CreatePlacedResourceX T%d hr=0x%08lx", i + 1, static_cast<unsigned long>(hr));
|
||||
}
|
||||
if (!t[0] || !t[4]) return 1;
|
||||
|
||||
void* mapped = nullptr;
|
||||
CHECK(slot<MapFn>(upload, kResMap)(upload, 0, nullptr, &mapped) == S_OK && mapped, "upload Map");
|
||||
if (mapped) {
|
||||
for (UINT64 i = 0; i < kBytes; ++i) static_cast<uint8_t*>(mapped)[i] = pattern(i);
|
||||
}
|
||||
|
||||
// The upload into T1, T1 into T5, both read back.
|
||||
D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp{};
|
||||
fp.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, kSize, kSize, 1, kPitch};
|
||||
auto copy_region = slot<CopyRegionFn>(list, kListCopyTextureRegion);
|
||||
auto barrier = slot<BarrierFn>(list, kListResourceBarrier);
|
||||
D3D12_TEXTURE_COPY_LOCATION tex1{static_cast<ID3D12Resource*>(t[0]), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
|
||||
D3D12_TEXTURE_COPY_LOCATION tex5{static_cast<ID3D12Resource*>(t[4]), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
|
||||
D3D12_TEXTURE_COPY_LOCATION up{static_cast<ID3D12Resource*>(upload), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}};
|
||||
up.PlacedFootprint = fp;
|
||||
D3D12_TEXTURE_COPY_LOCATION rb1{static_cast<ID3D12Resource*>(readback1), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}};
|
||||
rb1.PlacedFootprint = fp;
|
||||
D3D12_TEXTURE_COPY_LOCATION rb5{static_cast<ID3D12Resource*>(readback5), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}};
|
||||
rb5.PlacedFootprint = fp;
|
||||
D3D12_RESOURCE_BARRIER b[2] = {transition(t[0], D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST),
|
||||
transition(t[4], D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST)};
|
||||
barrier(list, 2, b);
|
||||
copy_region(list, &tex1, 0, 0, 0, &up, nullptr);
|
||||
b[0] = transition(t[0], D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
barrier(list, 1, b);
|
||||
slot<CopyResourceFn>(list, kListCopyResource)(list, t[4], t[0]);
|
||||
b[0] = transition(t[4], D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
barrier(list, 1, b);
|
||||
copy_region(list, &rb1, 0, 0, 0, &tex1, nullptr);
|
||||
copy_region(list, &rb5, 0, 0, 0, &tex5, nullptr);
|
||||
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 copies complete");
|
||||
CloseHandle(done);
|
||||
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
void* rb = k ? readback5 : readback1;
|
||||
void* data = nullptr;
|
||||
const D3D12_RANGE all{0, static_cast<SIZE_T>(kBytes)};
|
||||
CHECK(slot<MapFn>(rb, kResMap)(rb, 0, &all, &data) == S_OK && data, "readback Map");
|
||||
UINT64 wrong = 0, first = kBytes;
|
||||
if (data) {
|
||||
for (UINT64 i = 0; i < kBytes; ++i) {
|
||||
if (static_cast<const uint8_t*>(data)[i] != pattern(i)) {
|
||||
if (!wrong) first = i;
|
||||
++wrong;
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(data && wrong == 0, "%s holds the uploaded pattern (%llu of %llu bytes differ, first at %llu)",
|
||||
k ? "T5 (CopyResource from T1)" : "T1 (CopyTextureRegion from the upload)", static_cast<unsigned long long>(wrong),
|
||||
static_cast<unsigned long long>(kBytes), static_cast<unsigned long long>(first));
|
||||
}
|
||||
|
||||
const std::string log = read_log();
|
||||
if (!log.empty()) {
|
||||
const std::string mine = log.substr(before.size() <= log.size() ? before.size() : 0);
|
||||
CHECK(count_of(mine, "read-only textures of 64 KiB or more are reserved") == 1, "the shim reports reserved textures");
|
||||
CHECK(count_of(mine, "texture memory mapped at first use") == 2, "exactly two first-use mappings (T1, T5): %zu",
|
||||
count_of(mine, "texture memory mapped at first use"));
|
||||
CHECK(mine.find("reserved=3 mapped=2") != std::string::npos,
|
||||
"three textures reserved (T1, T2, T5; not the small one or the render target), two mapped (not T2)");
|
||||
CHECK(mine.find("refused a reserved texture") == std::string::npos && mine.find("not mapped") == std::string::npos,
|
||||
"no refusal or failed mapping is logged");
|
||||
} else {
|
||||
std::printf("note: no BX_LOG_FILE; the log checks did not run\n");
|
||||
}
|
||||
|
||||
for (void* object : {t[0], t[1], t[2], t[3], t[4], readback5, readback1, upload, fence, list, allocator, queue}) {
|
||||
if (object) slot<ReleaseFn>(object, kRelease)(object);
|
||||
}
|
||||
slot<ReleaseFn>(dev, kRelease)(dev);
|
||||
VirtualFree(memory, 0, MEM_RELEASE);
|
||||
if (g_failed) {
|
||||
std::fprintf(stderr, "FAIL: test_d3d12_lazy_texture_memory (%d of %d checks failed)\n", g_failed, g_checks);
|
||||
return 1;
|
||||
}
|
||||
std::printf("PASS: test_d3d12_lazy_texture_memory (%d of %d checks passed)\n", g_checks, g_checks);
|
||||
return 0;
|
||||
}
|
||||
@@ -52,12 +52,14 @@ struct XboxRequest {
|
||||
UINT64 file_offset;
|
||||
UINT32 source_size;
|
||||
UINT32 reserved34;
|
||||
UINT64 cancellation_tag; /* DSTORAGE_REQUEST is 0x48 bytes (dstorage_xs.h) */
|
||||
PCSTR name;
|
||||
};
|
||||
static_assert(sizeof(XboxRequest) == 56, "DSTORAGE_REQUEST is 56 bytes");
|
||||
static_assert(sizeof(XboxRequest) == 0x48, "DSTORAGE_REQUEST is 0x48 bytes (dstorage_xs.h)");
|
||||
|
||||
struct IXboxDStorageFile : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE GetFileInformation(BY_HANDLE_FILE_INFORMATION* info) = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0;
|
||||
};
|
||||
|
||||
struct IXboxDStorageStatusArray : IUnknown {
|
||||
@@ -78,10 +80,10 @@ struct IXboxDStorageFactory : IUnknown {
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0;
|
||||
virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot7() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot9() = 0;
|
||||
virtual void STDMETHODCALLTYPE GetStats(void* stats) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueueX(const XboxQueueDesc* desc, REFIID riid, void** ppv) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -52,14 +52,16 @@ struct XboxRequest {
|
||||
UINT64 file_offset;
|
||||
UINT32 source_size;
|
||||
UINT32 reserved34;
|
||||
UINT64 cancellation_tag; /* DSTORAGE_REQUEST is 0x48 bytes (dstorage_xs.h) */
|
||||
PCSTR name;
|
||||
};
|
||||
static_assert(sizeof(XboxRequest) == 56, "DSTORAGE_REQUEST is 56 bytes");
|
||||
static_assert(sizeof(XboxRequest) == 0x48, "DSTORAGE_REQUEST is 0x48 bytes (dstorage_xs.h)");
|
||||
static_assert(offsetof(XboxRequest, destination_page_offset) == 0x10, "DestinationPageOffset at +0x10");
|
||||
static_assert(offsetof(XboxRequest, destination_size) == 0x18, "DestinationSize at +0x18");
|
||||
|
||||
struct IXboxDStorageFile : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE GetFileInformation(BY_HANDLE_FILE_INFORMATION* info) = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0;
|
||||
};
|
||||
|
||||
struct IXboxDStorageStatusArray : IUnknown {
|
||||
@@ -80,10 +82,10 @@ struct IXboxDStorageFactory : IUnknown {
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0;
|
||||
virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot7() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot9() = 0;
|
||||
virtual void STDMETHODCALLTYPE GetStats(void* stats) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueueX(const XboxQueueDesc* desc, REFIID riid, void** ppv) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -37,11 +37,13 @@ struct XboxRequest {
|
||||
UINT64 file_offset;
|
||||
UINT32 source_size;
|
||||
UINT32 reserved34;
|
||||
UINT64 cancellation_tag; /* DSTORAGE_REQUEST is 0x48 bytes (dstorage_xs.h) */
|
||||
PCSTR name;
|
||||
};
|
||||
|
||||
struct IXboxDStorageFile : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE GetFileInformation(BY_HANDLE_FILE_INFORMATION* info) = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0;
|
||||
};
|
||||
|
||||
struct IXboxDStorageStatusArray : IUnknown {
|
||||
@@ -65,10 +67,10 @@ struct IXboxDStorageFactory : IUnknown {
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0;
|
||||
virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot7() = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE StubSlot9() = 0;
|
||||
virtual void STDMETHODCALLTYPE GetStats(void* stats) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueueX(const XboxQueueDesc* desc, REFIID riid, void** ppv) = 0;
|
||||
};
|
||||
|
||||
@@ -250,10 +252,12 @@ int main() {
|
||||
hr = factory->OpenFile(temp_file, kXboxFileIid, reinterpret_cast<void**>(&file));
|
||||
check(hr == S_OK && file != nullptr, "OpenFile(Xbox) must succeed (hr=0x%08lx)", static_cast<unsigned long>(hr));
|
||||
if (file) {
|
||||
BY_HANDLE_FILE_INFORMATION info{};
|
||||
hr = file->GetFileInformation(&info);
|
||||
check(hr == S_OK && info.nFileSizeLow == sizeof(payload) && info.nFileSizeHigh == 0,
|
||||
"GetFileInformation must report exact payload size");
|
||||
/* Slot 4: GetHandle, a new GENERIC_READ handle the caller closes (dstorage_xs.h). */
|
||||
HANDLE own = file->GetHandle();
|
||||
LARGE_INTEGER size{};
|
||||
check(own && own != INVALID_HANDLE_VALUE && GetFileSizeEx(own, &size) && size.QuadPart == sizeof(payload),
|
||||
"GetHandle must return a handle reporting the exact payload size");
|
||||
if (own && own != INVALID_HANDLE_VALUE) CloseHandle(own);
|
||||
}
|
||||
|
||||
/* Slot 5: CreateStatusArray. */
|
||||
@@ -364,15 +368,20 @@ int main() {
|
||||
}
|
||||
DeleteFileW(zlib_file);
|
||||
|
||||
/* The Xbox output layout is unverified. A PC-sized test buffer
|
||||
* detects any accidental writes while keeping the test memory safe. */
|
||||
DSTORAGE_ERROR_RECORD record;
|
||||
std::memset(&record, 0xa5, sizeof(record));
|
||||
unsigned char before[sizeof(record)];
|
||||
std::memcpy(before, &record, sizeof(record));
|
||||
file_q->RetrieveErrorRecord(&record);
|
||||
check(std::memcmp(&record, before, sizeof(record)) == 0,
|
||||
"Xbox RetrieveErrorRecord must leave the caller's buffer unchanged");
|
||||
/* DSTORAGE_ERROR_RECORD (dstorage_xs.h) is 656 bytes: FailureCount, then FirstFailure's
|
||||
* HResult and CommandType. The corrupted zlib request above failed on this queue. */
|
||||
unsigned char record[656 + 16];
|
||||
std::memset(record, 0xa5, sizeof(record));
|
||||
file_q->RetrieveErrorRecord(record);
|
||||
UINT32 failures = 0;
|
||||
HRESULT first_hr = S_OK;
|
||||
std::memcpy(&failures, record, sizeof(failures));
|
||||
std::memcpy(&first_hr, record + 4, sizeof(first_hr));
|
||||
check(failures >= 1 && FAILED(first_hr), "Xbox RetrieveErrorRecord must report the failed request (count=%u hr=0x%08lx)",
|
||||
failures, static_cast<unsigned long>(first_hr));
|
||||
bool tail_untouched = true;
|
||||
for (size_t i = 656; i < sizeof(record); ++i) tail_untouched = tail_untouched && record[i] == 0xa5;
|
||||
check(tail_untouched, "Xbox RetrieveErrorRecord must write no more than 656 bytes");
|
||||
file_q->Close();
|
||||
file_q->Release();
|
||||
}
|
||||
@@ -622,8 +631,6 @@ int main() {
|
||||
check(file_contains(log_path, "IDStorageQueueX[3]::EnqueueRequest_bcpack"),
|
||||
"log must record the BCPack request stub");
|
||||
check(file_contains(log_path, "dstorage_zlib_failed"), "log must record the corrupted zlib stream");
|
||||
check(file_contains(log_path, "IDStorageQueueX[10]::RetrieveErrorRecord"),
|
||||
"log must record the unimplemented Xbox error-record output");
|
||||
const auto check_rejection = [&](const char* method, HRESULT hr) {
|
||||
char needle[96];
|
||||
std::snprintf(needle, sizeof(needle), "xbox %s hr=0x%08lx", method, static_cast<unsigned long>(hr));
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# Xbox DirectStorage slots as dstorage_xs.h declares them (#769).
|
||||
add_executable(test_xcrdapi_xbox_slots main.cpp)
|
||||
target_include_directories(test_xcrdapi_xbox_slots PRIVATE "${BX_ABI_INCLUDE_DIR}" "${BX_ROOT_DIR}/shims/xcrdapi/dstorage")
|
||||
target_link_libraries(test_xcrdapi_xbox_slots PRIVATE xcrdapi bx_runtime_veh kernel32)
|
||||
@@ -0,0 +1,338 @@
|
||||
/*
|
||||
* Xbox DirectStorage slots as the authorized GDK header dstorage_xs.h declares them (#769).
|
||||
* Expected values come from the header:
|
||||
* IDStorageFactoryX 3 CreateQueue(DSTORAGE_QUEUE_DESC*), 4 OpenFile, 5 CreateStatusArray,
|
||||
* 6 SetCpuAffinity(UINT64) -> HRESULT, 7 SetDebugFlags(UINT32),
|
||||
* 8 SetStagingBufferSize; X1: 9 GetStats(DSTORAGE_STATS*);
|
||||
* X2: 10 CreateQueue1(DSTORAGE_QUEUE_DESC1*).
|
||||
* IDStorageFileX 3 Close, 4 GetHandle (a new GENERIC_READ handle the caller closes).
|
||||
* IDStorageQueueX 3-11 (... 10 RetrieveErrorRecord, 11 Query(DSTORAGE_QUEUE_INFO*));
|
||||
* X1: 12 EnqueueSetEvent(HANDLE); X2: 13 Query1(DSTORAGE_QUEUE_INFO1*).
|
||||
* DSTORAGE_PRIORITY LOW -1 .. REALTIME 2.
|
||||
* IIDs of the factory X/X1/X2 and queue X/X1/X2 interfaces.
|
||||
* Also: a zlib stream that holds far more than the destination delivers its first
|
||||
* DestinationSize bytes without a buffer for the whole stream.
|
||||
*/
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
/* Public DirectStorage error codes (SDK 1.4.0, MIT). */
|
||||
#include "dstorageerr.h"
|
||||
|
||||
extern "C" __declspec(dllimport) HRESULT WINAPI DStorageGetFactory(REFIID riid, void** ppv);
|
||||
|
||||
// The interfaces have external linkage: in an anonymous namespace with no class deriving from them
|
||||
// in this file, GCC may resolve their calls to the pure-virtual stubs.
|
||||
struct QueueDesc1 {
|
||||
UINT64 source_type;
|
||||
UINT16 capacity;
|
||||
INT8 priority;
|
||||
UINT8 pad[5];
|
||||
PCSTR name;
|
||||
UINT64 options;
|
||||
};
|
||||
static_assert(sizeof(QueueDesc1) == 32);
|
||||
|
||||
struct Request {
|
||||
UINT64 options;
|
||||
void* destination;
|
||||
UINT64 destination_page_offset;
|
||||
UINT32 destination_size;
|
||||
UINT32 pad1c;
|
||||
const void* source;
|
||||
UINT64 file_offset;
|
||||
UINT32 source_size;
|
||||
UINT32 intermediate_size;
|
||||
UINT64 cancellation_tag;
|
||||
PCSTR name;
|
||||
};
|
||||
static_assert(sizeof(Request) == 0x48);
|
||||
|
||||
struct IFileX : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0;
|
||||
};
|
||||
|
||||
struct IStatusX : IUnknown {
|
||||
virtual bool STDMETHODCALLTYPE IsComplete(UINT32 index) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE GetHResult(UINT32 index) = 0;
|
||||
};
|
||||
|
||||
struct IQueueX2 : IUnknown {
|
||||
virtual void STDMETHODCALLTYPE EnqueueRequest(const Request* request) = 0;
|
||||
virtual void STDMETHODCALLTYPE EnqueueStatus(IStatusX* status, UINT32 index) = 0;
|
||||
virtual void STDMETHODCALLTYPE EnqueueSignal(void* fence, UINT64 value) = 0;
|
||||
virtual void STDMETHODCALLTYPE Submit() = 0;
|
||||
virtual void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64 mask, UINT64 value) = 0;
|
||||
virtual void STDMETHODCALLTYPE Close() = 0;
|
||||
virtual HANDLE STDMETHODCALLTYPE GetErrorEvent() = 0;
|
||||
virtual void STDMETHODCALLTYPE RetrieveErrorRecord(void* record) = 0;
|
||||
virtual void STDMETHODCALLTYPE Query(void* info) = 0;
|
||||
virtual void STDMETHODCALLTYPE EnqueueSetEvent(HANDLE handle) = 0;
|
||||
virtual void STDMETHODCALLTYPE Query1(void* info) = 0;
|
||||
};
|
||||
|
||||
struct IFactoryX2 : IUnknown {
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0;
|
||||
virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0;
|
||||
virtual void STDMETHODCALLTYPE GetStats(void* stats) = 0;
|
||||
virtual HRESULT STDMETHODCALLTYPE CreateQueue1(const QueueDesc1* desc, REFIID riid, void** ppv) = 0;
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
// dstorage_xs.h IIDs.
|
||||
constexpr IID kFactoryX{0xfb3b54ac, 0xbf82, 0x463a, {0xb7, 0x77, 0x73, 0x19, 0x58, 0x37, 0x86, 0x4d}};
|
||||
constexpr IID kFactoryX1{0xc74c2a5b, 0x1e43, 0x46a5, {0xa2, 0x24, 0x91, 0x16, 0x35, 0x8e, 0x11, 0x18}};
|
||||
constexpr IID kFactoryX2{0x62959d11, 0xd45f, 0x4884, {0xa0, 0xa3, 0xf7, 0x86, 0x2e, 0xa2, 0x43, 0xae}};
|
||||
constexpr IID kFileX{0x43704f9f, 0x27c6, 0x4a9f, {0x88, 0x15, 0xd1, 0x65, 0x27, 0x89, 0x4d, 0xf4}};
|
||||
constexpr IID kStatusX{0xcd66d752, 0x2786, 0x44f7, {0x90, 0x70, 0xe2, 0xa6, 0xfd, 0xad, 0x3f, 0x1b}};
|
||||
constexpr IID kQueueX{0xa3f6649a, 0x8c2c, 0x4b3a, {0x8b, 0x6e, 0x10, 0x18, 0x6a, 0x04, 0xb9, 0xbc}};
|
||||
constexpr IID kQueueX1{0x3c02cb0c, 0xc050, 0x4195, {0x99, 0xa1, 0xa1, 0xce, 0x94, 0xba, 0x33, 0xc5}};
|
||||
constexpr IID kQueueX2{0x3af5c948, 0x6297, 0x426c, {0xbf, 0xfc, 0x8f, 0x18, 0x95, 0x9f, 0xb8, 0x7b}};
|
||||
|
||||
// DSTORAGE_REQUEST_OPTIONS: ZlibDecompress bit 0, SourceType bit 22.
|
||||
constexpr UINT64 kZlib = 1;
|
||||
constexpr UINT64 kSourceMemory = 1ull << 22;
|
||||
constexpr HRESULT kInvalidPriority = E_DSTORAGE_INVALID_QUEUE_PRIORITY;
|
||||
|
||||
int g_failed = 0;
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
++g_failed; \
|
||||
std::fprintf(stderr, "FAIL: test_xcrdapi_xbox_slots:%d: ", __LINE__); \
|
||||
std::fprintf(stderr, __VA_ARGS__); \
|
||||
std::fprintf(stderr, "\n"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
bool wait_complete(IStatusX* status, UINT32 index) {
|
||||
for (int i = 0; i < 1000 && !status->IsComplete(index); ++i) Sleep(5);
|
||||
return status->IsComplete(index);
|
||||
}
|
||||
|
||||
// RFC 1950/1951 by hand: one fixed-Huffman block holding a literal 0 and `matches` copies of
|
||||
// (length 258, distance 1), so the stream expands to 1 + 258 * matches zero bytes.
|
||||
struct BitWriter {
|
||||
std::vector<uint8_t> bytes;
|
||||
uint32_t buffer = 0;
|
||||
int count = 0;
|
||||
void bits(uint32_t value, int n) { // least significant bit first
|
||||
for (int i = 0; i < n; ++i) {
|
||||
buffer |= ((value >> i) & 1u) << count;
|
||||
if (++count == 8) {
|
||||
bytes.push_back(static_cast<uint8_t>(buffer));
|
||||
buffer = 0;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
void huffman(uint32_t code, int n) { // Huffman codes are packed most significant bit first
|
||||
for (int i = n - 1; i >= 0; --i) bits((code >> i) & 1u, 1);
|
||||
}
|
||||
void flush() {
|
||||
if (count) bytes.push_back(static_cast<uint8_t>(buffer));
|
||||
buffer = 0;
|
||||
count = 0;
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<uint8_t> zero_stream(uint32_t matches, size_t* produced) {
|
||||
BitWriter w;
|
||||
w.bytes = {0x78, 0x01};
|
||||
w.bits(1, 1); // BFINAL
|
||||
w.bits(1, 2); // BTYPE = 01, fixed Huffman
|
||||
w.huffman(0x30, 8); // literal 0: codes 0x30 + value, 8 bits
|
||||
for (uint32_t i = 0; i < matches; ++i) {
|
||||
w.huffman(0xC0 + (285 - 280), 8); // length symbol 285 (258, no extra bits)
|
||||
w.huffman(0, 5); // distance code 0 (distance 1)
|
||||
}
|
||||
w.huffman(0, 7); // end of block (256)
|
||||
w.flush();
|
||||
const uint64_t n = 1 + 258ull * matches;
|
||||
const uint32_t adler = (static_cast<uint32_t>(n % 65521u) << 16) | 1u; // all zeros: a = 1, b = n
|
||||
for (int shift = 24; shift >= 0; shift -= 8) w.bytes.push_back(static_cast<uint8_t>(adler >> shift));
|
||||
*produced = static_cast<size_t>(n);
|
||||
return w.bytes;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
// Every factory IID gives the factory.
|
||||
IFactoryX2* factory = nullptr;
|
||||
for (const IID* iid : {&kFactoryX, &kFactoryX1, &kFactoryX2}) {
|
||||
IFactoryX2* f = nullptr;
|
||||
const HRESULT hr = DStorageGetFactory(*iid, reinterpret_cast<void**>(&f));
|
||||
CHECK(hr == S_OK && f, "DStorageGetFactory(factory IID %08lx) hr=0x%08lx", static_cast<unsigned long>(iid->Data1),
|
||||
static_cast<unsigned long>(hr));
|
||||
if (f && !factory) factory = f;
|
||||
else if (f) f->Release();
|
||||
}
|
||||
if (!factory) return 1;
|
||||
|
||||
// Slot 6 SetCpuAffinity returns an HRESULT; slot 7 SetDebugFlags; slot 8 SetStagingBufferSize.
|
||||
CHECK(factory->SetCpuAffinity(0x3) == S_OK, "SetCpuAffinity must return S_OK");
|
||||
factory->SetDebugFlags(0);
|
||||
CHECK(factory->SetStagingBufferSize(16u * 1048576u) == S_OK, "SetStagingBufferSize");
|
||||
|
||||
// Slot 9 GetStats writes the 256-byte DSTORAGE_STATS and nothing past it.
|
||||
unsigned char stats[256 + 16];
|
||||
std::memset(stats, 0xA5, sizeof(stats));
|
||||
factory->GetStats(stats);
|
||||
bool stats_written = true, tail_kept = true;
|
||||
for (size_t i = 0; i < 256; ++i) stats_written = stats_written && stats[i] != 0xA5;
|
||||
for (size_t i = 256; i < sizeof(stats); ++i) tail_kept = tail_kept && stats[i] == 0xA5;
|
||||
CHECK(stats_written && tail_kept, "GetStats must write exactly 256 bytes");
|
||||
|
||||
// Slot 10 CreateQueue1 checks DSTORAGE_PRIORITY.
|
||||
QueueDesc1 desc{};
|
||||
desc.source_type = 1; // memory
|
||||
desc.capacity = 128;
|
||||
desc.priority = 3;
|
||||
desc.name = "slots";
|
||||
IQueueX2* queue = nullptr;
|
||||
CHECK(factory->CreateQueue1(&desc, kQueueX1, reinterpret_cast<void**>(&queue)) == kInvalidPriority && !queue,
|
||||
"priority 3 must fail with E_DSTORAGE_INVALID_QUEUE_PRIORITY");
|
||||
desc.priority = -1;
|
||||
desc.options = 1; // AutoSubmitDisabled
|
||||
HRESULT hr = factory->CreateQueue1(&desc, kQueueX2, reinterpret_cast<void**>(&queue));
|
||||
CHECK(hr == S_OK && queue, "CreateQueue1(priority LOW) hr=0x%08lx", static_cast<unsigned long>(hr));
|
||||
if (!queue) return 1;
|
||||
for (const IID* iid : {&kQueueX, &kQueueX1, &kQueueX2}) {
|
||||
IUnknown* other = nullptr;
|
||||
CHECK(queue->QueryInterface(*iid, reinterpret_cast<void**>(&other)) == S_OK && other,
|
||||
"queue must answer IID %08lx", static_cast<unsigned long>(iid->Data1));
|
||||
if (other) other->Release();
|
||||
}
|
||||
|
||||
// Slot 11 Query (DSTORAGE_QUEUE_INFO: a 24-byte DESC, then EmptySlotCount, RequestCountUntilAutoSubmit).
|
||||
unsigned char info[32 + 8];
|
||||
std::memset(info, 0xA5, sizeof(info));
|
||||
queue->Query(info);
|
||||
UINT16 capacity = 0, empty = 0;
|
||||
INT8 priority = 0;
|
||||
PCSTR name = nullptr;
|
||||
std::memcpy(&capacity, info + 8, 2);
|
||||
std::memcpy(&priority, info + 10, 1);
|
||||
std::memcpy(&name, info + 16, 8);
|
||||
std::memcpy(&empty, info + 24, 2);
|
||||
CHECK(capacity == 128 && priority == -1 && name && std::strcmp(name, "slots") == 0 && empty == 127,
|
||||
"Query: capacity=%u priority=%d empty=%u", capacity, priority, empty);
|
||||
bool info_tail = true;
|
||||
for (size_t i = 32; i < sizeof(info); ++i) info_tail = info_tail && info[i] == 0xA5;
|
||||
CHECK(info_tail, "Query must write exactly 32 bytes");
|
||||
// Slot 13 Query1 (DSTORAGE_QUEUE_INFO1: a 32-byte DESC1 with Options, then the counts).
|
||||
unsigned char info1[40];
|
||||
queue->Query1(info1);
|
||||
UINT64 options = 0;
|
||||
std::memcpy(&options, info1 + 24, 8);
|
||||
std::memcpy(&empty, info1 + 32, 2);
|
||||
CHECK(options == 1 && empty == 127, "Query1: options=%llu empty=%u", static_cast<unsigned long long>(options), empty);
|
||||
|
||||
// Slot 12 EnqueueSetEvent: the event is set once the commands before it are done.
|
||||
static const char kText[] = "event after request";
|
||||
char copy[sizeof(kText)] = {};
|
||||
Request request{};
|
||||
request.options = kSourceMemory;
|
||||
request.destination = copy;
|
||||
request.destination_size = sizeof(kText);
|
||||
request.source = kText;
|
||||
request.source_size = sizeof(kText);
|
||||
HANDLE done = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
queue->EnqueueRequest(&request);
|
||||
queue->EnqueueSetEvent(done);
|
||||
CHECK(WaitForSingleObject(done, 0) == WAIT_TIMEOUT, "AutoSubmitDisabled: nothing runs before Submit");
|
||||
queue->Submit();
|
||||
CHECK(WaitForSingleObject(done, 5000) == WAIT_OBJECT_0, "EnqueueSetEvent must set the event");
|
||||
CHECK(std::memcmp(copy, kText, sizeof(kText)) == 0, "the request before the event must be done");
|
||||
CloseHandle(done);
|
||||
|
||||
// A zlib stream holding about 8 MiB, read into a 64 KiB destination, delivers its first 64 KiB.
|
||||
size_t produced = 0;
|
||||
const std::vector<uint8_t> stream = zero_stream(32512, &produced);
|
||||
std::vector<uint8_t> destination(65536, 0xCC);
|
||||
IStatusX* status = nullptr;
|
||||
CHECK(factory->CreateStatusArray(4, "slots", kStatusX, reinterpret_cast<void**>(&status)) == S_OK && status,
|
||||
"CreateStatusArray");
|
||||
if (status) {
|
||||
Request partial{};
|
||||
partial.options = kSourceMemory | kZlib;
|
||||
partial.destination = destination.data();
|
||||
partial.destination_size = static_cast<UINT32>(destination.size());
|
||||
partial.source = stream.data();
|
||||
partial.source_size = static_cast<UINT32>(stream.size());
|
||||
CHECK(stream.size() < destination.size() && produced > 64 * destination.size() + 0x40000,
|
||||
"the stream must be smaller than the destination and expand past the old buffer");
|
||||
queue->EnqueueRequest(&partial);
|
||||
queue->EnqueueStatus(status, 0);
|
||||
queue->Submit();
|
||||
CHECK(wait_complete(status, 0) && status->GetHResult(0) == S_OK, "partial zlib read hr=0x%08lx",
|
||||
static_cast<unsigned long>(status->GetHResult(0)));
|
||||
bool zeros = true;
|
||||
for (uint8_t b : destination) zeros = zeros && b == 0;
|
||||
CHECK(zeros, "the first 64 KiB of the stream must be delivered");
|
||||
}
|
||||
|
||||
// Slot 3 CreateQueue takes a 24-byte DSTORAGE_QUEUE_DESC (no Options).
|
||||
unsigned char short_desc[24 + 8];
|
||||
std::memset(short_desc, 0xEE, sizeof(short_desc)); // bytes past the DESC must not be read as Options
|
||||
std::memcpy(short_desc, &desc, 24);
|
||||
IQueueX2* plain = nullptr;
|
||||
hr = factory->CreateQueue(short_desc, kQueueX, reinterpret_cast<void**>(&plain));
|
||||
CHECK(hr == S_OK && plain, "CreateQueue(DSTORAGE_QUEUE_DESC) hr=0x%08lx", static_cast<unsigned long>(hr));
|
||||
if (plain) {
|
||||
unsigned char plain_info[40];
|
||||
plain->Query1(plain_info);
|
||||
std::memcpy(&options, plain_info + 24, 8);
|
||||
CHECK(options == 0, "a queue from CreateQueue has no Options (got %llu)", static_cast<unsigned long long>(options));
|
||||
plain->Close();
|
||||
plain->Release();
|
||||
}
|
||||
|
||||
// IDStorageFileX slot 4 GetHandle: a new handle the caller closes, valid after the file object.
|
||||
WCHAR dir[MAX_PATH], path[MAX_PATH];
|
||||
GetTempPathW(MAX_PATH, dir);
|
||||
GetTempFileNameW(dir, L"bxs", 0, path);
|
||||
HANDLE writer = CreateFileW(path, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
DWORD written = 0;
|
||||
WriteFile(writer, kText, sizeof(kText), &written, nullptr);
|
||||
CloseHandle(writer);
|
||||
IFileX* file = nullptr;
|
||||
CHECK(factory->OpenFile(path, kFileX, reinterpret_cast<void**>(&file)) == S_OK && file, "OpenFile");
|
||||
if (file) {
|
||||
HANDLE own = file->GetHandle();
|
||||
CHECK(own && own != INVALID_HANDLE_VALUE, "GetHandle must return a handle");
|
||||
file->Close();
|
||||
file->Release();
|
||||
LARGE_INTEGER size{};
|
||||
char head[4] = {};
|
||||
DWORD got = 0;
|
||||
CHECK(own && GetFileSizeEx(own, &size) && size.QuadPart == sizeof(kText), "the handle reports the file size");
|
||||
CHECK(own && ReadFile(own, head, sizeof(head), &got, nullptr) && got == sizeof(head) &&
|
||||
std::memcmp(head, kText, sizeof(head)) == 0,
|
||||
"the handle reads the file synchronously after the file object is gone");
|
||||
if (own && own != INVALID_HANDLE_VALUE) CloseHandle(own);
|
||||
}
|
||||
DeleteFileW(path);
|
||||
|
||||
if (status) status->Release();
|
||||
queue->Close();
|
||||
queue->Release();
|
||||
factory->Release();
|
||||
if (g_failed) {
|
||||
std::fprintf(stderr, "FAIL: test_xcrdapi_xbox_slots (%d failed)\n", g_failed);
|
||||
return 1;
|
||||
}
|
||||
std::puts("PASS: test_xcrdapi_xbox_slots");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user