Skip to content

d3d12_xs: handle slot 25 swapped ABI, slot 127 CreateCommittedResourceX, and __XMemAllocationHooks__ - #8072

Open
JIFO0 wants to merge 3 commits into
ItsNotRexed:mainfrom
JIFO0:feat/d3d12xs-slot25
Open

JIFO0 wants to merge 3 commits into
ItsNotRexed:mainfrom
JIFO0:feat/d3d12xs-slot25

Conversation

@JIFO0

@JIFO0 JIFO0 commented Nov 23, 2026 •

Copy link
Copy Markdown

Depends on: #7503

What and why

Extends the Xbox D3D12_xs device proxy and xmem shim to unblock resource allocation queries, committed resource creation, and title D3D12 memory-budget hooks past #7503:

  1. Device slot 25 (GetResourceAllocationInfo) (
    untime/shims/d3d12_xs_device.cpp):

    • Detects the Xbox struct-returning register order (out_buf, this, visible_mask, count, descs) and swaps (this, out_buf) before forwarding to PC slot 25.
    • Sanitizes incoming D3D12_RESOURCE_DESC entries via shared sanitize_xbox_resource_desc (Flags &= 0x3F, Alignment = 0, Layout mapped to ROW_MAJOR for buffers and UNKNOWN for textures).
  2. Device slot 127 (CreateCommittedResourceX) & shared resource sanitization (
    untime/shims/d3d12_xs_device.cpp):

    • Implements 10-argument CreateCommittedResourceX (proxy_create_committed_resource_x), sanitizing console heap flags (D3D12_HEAP_FLAG_NONE), heap type, resource desc, and D3D12_CLEAR_VALUE (sanitize_xbox_clear_value aligns Format to pc_desc.Format and drops clear values on buffers / non-RT/DS resources).
    • Maps InitialLayout (e.g. x3, xFFFFFFFF) to D3D12_RESOURCE_STATE_COMMON in both slot 127 (CreateCommittedResourceX) and slot 133 (CreatePlacedResourceX).
  3. XMemAllocationHooks dispatch & D3D12 budget hook notification (
    untime/shims/shims.h,
    untime/shims/xmem.cpp,
    untime/shims/d3d12_xs_device.cpp):

    • Adds ind_guest_xmem_hooks() in shims.h to resolve XMemAllocationHooks from the mapped guest PE64 export table (VirtualQuery-validated).
    • Routes XMemAlloc / XMemFree through the guest hooks when present, and notifies the D3D12 allocator hook (x00808000ULL) on successful CreateCommittedResourceX calls using GetResourceAllocationInfo size.
  4. Unit tests (
    untime/test/device_proxy_test.cpp):

    • Adds Xbox-ABI proxy tests covering swapped slot 25 (GetResourceAllocationInfo) and 10-arg slot 127 (CreateCommittedResourceX with console heap flags, Layout=0x101, Flags=0x300005, InitialLayout=0x3, and CLEAR_VALUE with DXGI_FORMAT_UNKNOWN).

Tested

  • Built gta6rt, device_proxy_test, xs_proxy_mock_test, xmem_test, loader_test, winhttp_offline_test, queue_proxy_test (MSVC 14.44, RelWithDebInfo, Vulkan backend).
  • ctest --test-dir runtime/build -R "^(xmem|xs_proxy_mock|loader|winhttp_offline|queue_proxy|device_proxy)$" --output-on-failure: 6/6 passed (100%).
  • Tested gta6rt.exe --run offline with LEONIDA_D3D12=vulkan and translated XboxBvhBuilder.dll SPIR-V shaders (LEONIDA_SHADERS) on Windows 11 / NVIDIA GeForce RTX 2080: clears slot 25, slot 127 (3840x2160 RT/UAV textures + SRV/RTV/UAV), XMemAllocDefault 64 MiB D3D12 budget allocation, CreateRootSignature (slot 16), and CreateComputePipelineState (slot 62, 52 shaders loaded) without crashes.

@JIFO0 JIFO0 changed the title Feat/d3d12xs slot25 d3d12_xs: handle slot 25 swapped ABI, slot 127 CreateCommittedResourceX, and __XMemAllocationHooks__ Nov 23, 2026
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Slot 127 loses the pointer from the guest allocation hook at 388faa1. Each successful resource creation calls notify_xbox_d3d12_committed_alloc. The helper calls the allocator with the full resource size. It only uses the returned pointer in a log message. The resource has no reference to that pointer for release.

I tested the unchanged helper on Windows 11 with MSVC and a counting allocator. The device reports 65,536 bytes per call. After 32 calls, the allocator still owns 32 blocks (2,097,152 bytes). The expected count for a notification with no retained storage is zero. This test exercises the helper with a synthetic device. It does not exercise a GPU or the retail executable.

Please use an accounting operation if this is only a budget notification. If the resource needs the allocation, store the pointer with the resource and call the matching free hook when the resource releases it.

Standalone check

Save as control.cpp. Run cl /EHsc /std:c++17 control.cpp and .\control.exe in an MSVC developer shell.

#include <atomic>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <vector>
struct D3D12_RESOURCE_DESC {};
struct D3D12_RESOURCE_ALLOCATION_INFO { std::uint64_t SizeInBytes; };
struct ID3D12Device { D3D12_RESOURCE_ALLOCATION_INFO GetResourceAllocationInfo(int,int,const D3D12_RESOURCE_DESC*) {return {65536};} };
std::vector<void*> live;
namespace rt::shims {
struct Hooks { void* (*alloc_fn)(size_t,uint64_t); void (*free_fn)(void*,uint64_t); };
Hooks* find_guest_xmem_hooks() { static Hooks hooks{
[](size_t n,uint64_t){auto p=std::malloc(n); if(!p) std::abort(); live.push_back(p); return p;},
[](void* p,uint64_t){for(auto i=live.begin();i!=live.end();++i) if(*i==p){live.erase(i);break;} std::free(p);} }; return &hooks; }
}
bool first_call(std::atomic<bool>& logged){return !logged.exchange(true);}
void notify_xbox_d3d12_committed_alloc(ID3D12Device* real, const D3D12_RESOURCE_DESC& pc_desc) {
    const auto* hooks = rt::shims::find_guest_xmem_hooks();
    if (!hooks || !hooks->alloc_fn || !real) return;
    const D3D12_RESOURCE_ALLOCATION_INFO info = real->GetResourceAllocationInfo(0, 1, &pc_desc);
    if (info.SizeInBytes == 0 || info.SizeInBytes == UINT64_MAX) return;
    // Bits 23:16 = 0x80 (eXMemAllocatorId_D3D12), bit 15 = 0x8000 (D3D12 allocation attribute).
    constexpr std::uint64_t kD3d12CommittedAttr = 0x00808000ULL;
    void* mem = hooks->alloc_fn(static_cast<std::size_t>(info.SizeInBytes), kD3d12CommittedAttr);
    static std::atomic<bool> logged{false};
    if (first_call(logged)) {
        std::fprintf(stderr, "[rt] d3d12_xs: slot 127 D3D12 hook alloc size=0x%llx attr=0x%llx -> %p\n",
                     static_cast<unsigned long long>(info.SizeInBytes),
                     static_cast<unsigned long long>(kD3d12CommittedAttr), mem);
    }
}
int main(){ID3D12Device dev; for(int i=0;i<32;i++) notify_xbox_d3d12_committed_alloc(&dev,{}); std::printf("live blocks %zu, bytes %zu\n",live.size(),live.size()*65536); for(void* p:live) std::free(p);}

AI-assisted.

@JIFO0

JIFO0 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Heads-up: rebasing feat/d3d12xs-slot25 onto the freshly rebased #7503 / latest main and force-pushing (--force-with-lease) so both PRs stay clean against main.

@JIFO0
JIFO0 force-pushed the feat/d3d12xs-slot25 branch from 388faa1 to e710a30 Compare November 23, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants