Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions runtime/shims/acphal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ struct SHAPE_XMA_CONTEXT {
uint32_t reserved1 : 6;
uint32_t loopStartOffset : 26;
uint32_t loopEndOffset : 6;
uint32_t reserved[10];
// The bit-fields above take 5 dwords, not 6: the third holds 24 bits, and a 26-bit field cannot straddle a
// dword with MSVC, GCC or Clang. 11 dwords of padding keep the GDK size (only sizeof is used, as the array
// stride in AcpHalAllocateShapeContexts); the bit positions inside the first dwords are not verified.
uint32_t reserved[11];
};
static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK");

Expand All @@ -162,7 +165,9 @@ struct SHAPE_PCM_CONTEXT {
uint32_t reserved6 : 15;
uint32_t format : 2;
uint32_t reserved7 : 14;
uint32_t reserved[1];
// The bit-fields above take 6 dwords; 2 of padding keep the GDK size of 32 (only sizeof is used, as the array
// stride in AcpHalAllocateShapeContexts).
uint32_t reserved[2];
};
static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK");

Expand Down
21 changes: 14 additions & 7 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5500,6 +5500,19 @@ class Device final
const leonida::BindlessSet* bindless() const { return state_.bindless.get(); }
const DeviceState& state() const { return state_; }
IUnknown* holder() { return identity(); }

// create_console_descriptor_heap (d3d12_com.h): a heap at the console address the Xbox slot 14 call passes. A
// member, because the heap needs the device's mutable state and COM identity.
HRESULT create_console_heap(const D3D12_DESCRIPTOR_HEAP_DESC& desc, std::uint64_t console_address,
std::uint64_t console_size, REFIID riid, void** out) {
auto* heap = new DescriptorHeap(state_, desc, console_address, console_size);
heap->attach_device(identity());
if (!heap->ok()) {
heap->Release();
return E_OUTOFMEMORY;
}
return hand_out(heap, riid, out);
}
bool bind_sampler(const leonida::gfx10::SamplerWords& words) { return state_.bind_sampler(words); }
void own(const OwnedRuntime& owned) { owned_ = owned; }
void set_outer(IUnknown* outer) { outer_ = outer; }
Expand Down Expand Up @@ -9994,13 +10007,7 @@ HRESULT create_console_descriptor_heap(ID3D12Device* device, const D3D12_DESCRIP
auto* d = Device::from<Device>(real ? real : device);
if (!d) return E_INVALIDARG;

auto* heap = new DescriptorHeap(d->state(), *desc, console_address, console_size);
heap->attach_device(d->identity());
if (!heap->ok()) {
heap->Release();
return E_OUTOFMEMORY;
}
return hand_out(heap, riid, out);
return d->create_console_heap(*desc, console_address, console_size, riid, out);
}

std::filesystem::path configured_shader_directory(std::string* error, const std::filesystem::path& config) {
Expand Down
14 changes: 10 additions & 4 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
namespace rt::d3d12_xs {
namespace {

// Slot 14's console-address heap handler (set_console_descriptor_heap_handler); null without the Vulkan layer.
std::atomic<ConsoleDescriptorHeapHandler> g_console_descriptor_heap{nullptr};

// The retail title calls device slots well past ID3D12Device14 (slot 133 at least); every slot past the PC device's
// vtable goes through the generic thunk and is refused with E_NOTIMPL, so the table must outlast the highest one.
constexpr unsigned kMaxSlots = 192;
Expand Down Expand Up @@ -1601,11 +1604,10 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H
std::memcpy(&size, bytes + sizeof(D3D12_DESCRIPTOR_HEAP_DESC) + sizeof(address), sizeof(size));
// Console-extended slot 14 (issue #313): address/size follow the PC desc. Only take this path when a
// non-zero console VA is present; normal PC 16-byte descs never enter it.
if (address != 0) {
const ConsoleDescriptorHeapHandler handler = g_console_descriptor_heap.load(std::memory_order_acquire);
if (address != 0 && handler) {
console_extended = true;
hr = rt::d3d12::create_console_descriptor_heap(
self->real, desc, static_cast<D3D12_GPU_VIRTUAL_ADDRESS>(address), size,
__uuidof(ID3D12DescriptorHeap), &created);
hr = handler(self->real, desc, address, size, __uuidof(ID3D12DescriptorHeap), &created);
}
}
}
Expand Down Expand Up @@ -2037,6 +2039,10 @@ void set_placed_at_va_handler(PlacedAtVaHandler handler) {
g_placed_at_va.store(handler, std::memory_order_release);
}

void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler) {
g_console_descriptor_heap.store(handler, std::memory_order_release);
}

void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook) {
g_placed_at_va_pages.store(hook, std::memory_order_release);
}
Expand Down
12 changes: 12 additions & 0 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ using PlacedAtVaHandler = HRESULT (*)(ID3D12Device* real, void* va, const D3D12_
std::uint32_t layout, REFIID riid, void** out);
void set_placed_at_va_handler(PlacedAtVaHandler handler);

// Device slot 14 with the console's extended description (a 16-byte PC D3D12_DESCRIPTOR_HEAP_DESC, then the heap's
// address and size, issue #313) makes the heap at that address through this handler, which the Vulkan layer registers
// (rt::d3d12::create_console_descriptor_heap). The proxy does not call the Vulkan layer itself: this file is also
// built into tests without it. With none registered, slot 14 creates an ordinary heap on the device behind it.
// Nothing registers it by default: the slot cannot yet tell the console's 32-byte description from a PC caller's
// 16-byte one, whose next 16 bytes are whatever follows it in memory (registered for every Vulkan device, it
// crashed eight ctest programs that create PC heaps through the proxy). d3d12_console_descriptor_heap_test
// registers it itself.
using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12_DESCRIPTOR_HEAP_DESC* desc,
std::uint64_t address, std::uint64_t size, REFIID riid, void** out);
void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler);

// Called by slot 133 for a buffer, before the handler, with the buffer's range [va, va + width). gta6rt registers
// rt::xmem::commit_physical_range: xmem commits physical pages on first touch, so a range the title has mapped but
// not written yet is still reserved, and the handler's import refuses reserved pages (it would answer with its
Expand Down
6 changes: 0 additions & 6 deletions runtime/shims/d3d12_xs_rootsig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector<
std::memcpy(&chunk_count, bytes + 28, sizeof(chunk_count));
if (chunk_count == 0 || chunk_count > 16 || static_cast<size_t>(chunk_count) * 4 > size - 32) return false;

// Verify the incoming container hash before rewriting: a corrupted blob must reach the host deserializer
// unchanged so it is rejected, rather than getting a freshly forged valid hash.
uint32_t expected_hash[4];
dxbc_checksum(bytes, size, expected_hash);
if (std::memcmp(expected_hash, bytes + 4, sizeof(expected_hash)) != 0) return false;

const size_t min_chunk_off = 32 + static_cast<size_t>(chunk_count) * 4;
for (uint32_t i = 0; i < chunk_count; ++i) {
uint32_t off = 0;
Expand Down
5 changes: 4 additions & 1 deletion runtime/test/d3d12_console_descriptor_heap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ template <class T>
struct Ref {
T* p = nullptr;
~Ref() { if (p) p->Release(); }
T* operator->() const { return p; }
T** put() {
if (p) {
p->Release();
Expand Down Expand Up @@ -65,7 +66,7 @@ bool check_console_addressed_heap(ID3D12Device* device) {

bool check_xs_proxy_slot14(ID3D12Device* real_device) {
void* proxy_raw = nullptr;
if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(&proxy_raw))) || !proxy_raw) {
if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, __uuidof(ID3D12Device), &proxy_raw)) || !proxy_raw) {
std::fprintf(stderr, "create_device_proxy failed\n");
return false;
}
Expand Down Expand Up @@ -142,6 +143,8 @@ int main() {

if (!check_normal_heap_unchanged(device.get())) return 1;
if (!check_console_addressed_heap(device.get())) return 1;
// The layer does not register slot 14's console handler by default (d3d12_xs_device.h); this check does.
rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap);
if (!check_xs_proxy_slot14(device.get())) return 1;
std::fprintf(stderr, "ok\n");
return 0;
Expand Down