Skip to content
Open
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
10 changes: 9 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ if(BUILD_TESTING)
add_executable(buffer_va_index_test test/buffer_va_index_test.cpp)
add_test(NAME buffer_va_index COMMAND buffer_va_index_test)

# Thread-safe host VA -> GPU BDA region table (runtime/src/title_host_regions.h): pure stdlib, no Vulkan instance.
add_executable(title_host_regions_test test/title_host_regions_test.cpp)
target_include_directories(title_host_regions_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
find_package(Threads REQUIRED)
target_link_libraries(title_host_regions_test PRIVATE Threads::Threads)
add_test(NAME title_host_regions COMMAND title_host_regions_test)

add_executable(xbox_present_readback_size_test test/xbox_present_readback_size_test.cpp)
add_test(NAME xbox_present_readback_size COMMAND xbox_present_readback_size_test)

Expand Down Expand Up @@ -1482,13 +1489,14 @@ endif()
if(TARGET Vulkan::Headers)
foreach(tool vk_harness vk_ps_sweep vk_texture_test vk_sampler_test vk_compute_test vk_store_test vk_pipeline_check vk_command_replay_test
vk_bindless_test vk_image_fuzz vk_descriptor_bench depth24_convert_test vk_predicate_capture_test vk_predicated_copy_test
vk_xdxr_build_test vk_queue_test vk_sandbox)
vk_xdxr_build_test vk_queue_test vk_sandbox title_host_region_gpu_test)
add_executable(${tool} test/${tool}.cpp)
target_link_libraries(${tool} PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS})
if(WIN32)
target_compile_definitions(${tool} PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
endforeach()
add_test(NAME title_host_region_gpu COMMAND title_host_region_gpu_test)
# OpFmaKHR width gate of vk_shader_fma.h (shaderFmaFloat32 / shaderFmaFloat64): no Vulkan instance, no GPU.
add_executable(vk_shader_fma_test test/vk_shader_fma_test.cpp)
target_link_libraries(vk_shader_fma_test PRIVATE Vulkan::Headers)
Expand Down
23 changes: 18 additions & 5 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5153,9 +5153,12 @@ class Device final
}
}
if (info.host_image_copy) load_host_image_copy(get_device);
if (info.host_pointer_alignment)
if (info.host_pointer_alignment) {
state_.get_host_pointer_properties = reinterpret_cast<PFN_vkGetMemoryHostPointerPropertiesEXT>(
get_device(info.device, "vkGetMemoryHostPointerPropertiesEXT"));
state_.context->configure_title_regions(state_.get_host_pointer_properties, info.host_pointer_alignment,
state_.memory);
}
// Depth <-> colour copies are staged through scratch buffers (vulkan_scratch.h), on every GPU.
state_.context->enable_copy_scratch(get_device, [this](const VkMemoryRequirements& requirements) {
return state_.allocate(requirements, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Expand Down Expand Up @@ -6402,14 +6405,24 @@ class Device final

// A constant-buffer view is a 16-byte GFX10 raw buffer V# in the first half of the slot (zeros in the rest).
// A null description, zero address, or zero size leaves the whole slot zeroed (null V#: reads return 0).
// Translates an external/title host VA into an arena-relative offset so a shader adding
// heap_base (arena.address) lands on the Vulkan-imported BDA.
std::uint64_t resolve_shader_va(std::uint64_t va) const {
if (!va || va < state_.arena.size || !state_.context || !state_.arena.address) return va;
const auto bda = state_.context->resolve_title_bda(va);
if (!bda || *bda < state_.arena.address) return va;
const std::uint64_t rel = *bda - state_.arena.address;
return (rel >> 48) == 0 ? rel : va;
}

void STDMETHODCALLTYPE CreateConstantBufferView(const D3D12_CONSTANT_BUFFER_VIEW_DESC* desc,
D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
d3d12_trace::record("ID3D12Device::CreateConstantBufferView");
if (!destination.ptr) return;
std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize);
if (!desc || !desc->BufferLocation || !desc->SizeInBytes) return;
leonida::gfx10::Buffer buffer{};
buffer.address = desc->BufferLocation;
buffer.address = resolve_shader_va(desc->BufferLocation);
buffer.stride = 0;
buffer.num_records = desc->SizeInBytes;
buffer.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT);
Expand Down Expand Up @@ -6446,7 +6459,7 @@ class Device final
log_once("shader-resource view dimension not supported for buffer:", desc->ViewDimension);
return;
}
const UINT64 base_va = const_cast<Resource*>(r)->GetGPUVirtualAddress();
const UINT64 base_va = resolve_shader_va(const_cast<Resource*>(r)->GetGPUVirtualAddress());
const UINT64 width = r->desc().Width;
if (!base_va || !desc->Buffer.NumElements || (desc->Buffer.Flags & ~D3D12_BUFFER_SRV_FLAG_RAW)) return;
leonida::gfx10::Buffer buffer{};
Expand Down Expand Up @@ -6651,8 +6664,8 @@ class Device final
return;
}
}
const UINT64 va = resource->GetGPUVirtualAddress();
const UINT64 counter_va = counter ? counter->GetGPUVirtualAddress() : 0;
const UINT64 va = resolve_shader_va(resource->GetGPUVirtualAddress());
const UINT64 counter_va = counter ? resolve_shader_va(counter->GetGPUVirtualAddress()) : 0;
if (!va || (counter && !counter_va)) return;
g::Buffer buffer{};
buffer.address = va + view.FirstElement * unit;
Expand Down
53 changes: 46 additions & 7 deletions runtime/shims/xmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <malloc.h>
#include <psapi.h>

#include "../src/title_host_regions.h"

#include <atomic>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -428,8 +430,12 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
record_window(address, size, host_prot);
return address;
}
return placeholder_api().virtual_alloc2(GetCurrentProcess(), address, size, host_type | MEM_RESERVE, host_prot,
nullptr, 0);
void* const result = placeholder_api().virtual_alloc2(GetCurrentProcess(), address, size,
host_type | MEM_RESERVE, host_prot, nullptr, 0);
if (result && commit) {
leonida::title_host_regions().register_range(reinterpret_cast<uint64_t>(result), size, 2);
}
return result;
}
if (new_placeholder || window) {
void* const result = reserve_placeholder(address, size, base_granularity);
Expand All @@ -445,17 +451,26 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
MEM_EXTENDED_PARAMETER parameter{};
parameter.Type = MemExtendedParameterAddressRequirements;
parameter.Pointer = &requirements;
return placeholder_api().virtual_alloc2(GetCurrentProcess(), nullptr, size, host_type | MEM_RESERVE, host_prot,
&parameter, 1);
void* const result = placeholder_api().virtual_alloc2(GetCurrentProcess(), nullptr, size,
host_type | MEM_RESERVE, host_prot, &parameter, 1);
if (result && commit) {
leonida::title_host_regions().register_range(reinterpret_cast<uint64_t>(result), size, 2);
}
return result;
}
void* const result = VirtualAlloc(address, size, host_type, host_prot);
if (result && commit) {
leonida::title_host_regions().register_range(reinterpret_cast<uint64_t>(result), size, 2);
}
return VirtualAlloc(address, size, host_type, host_prot);
return result;
}

// With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again.
bool unmap_page(uintptr_t address) {
const FaultLockExclusive lock;
const auto it = g_mapped.find(address);
if (it == g_mapped.end()) return true;
leonida::title_host_regions().unregister_range(address, kPhysicalPageSize);
if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(address),
MEM_PRESERVE_PLACEHOLDER)) {
return false;
Expand All @@ -482,6 +497,7 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
protect, nullptr, 0);
if (!view) return false;
g_mapped[address] = MappedPage{page, it->second.section, it->second.offset};
leonida::title_host_regions().register_range(address, kPhysicalPageSize, 3);
return true;
}

Expand Down Expand Up @@ -797,7 +813,28 @@ constexpr DWORD kMemGraphics = 0x10000000;
constexpr DWORD kMemTitle = 0x40000000;

LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) {
return VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect));
LPVOID const result =
VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect));
if (result && (allocation_type & (kMemGraphics | MEM_COMMIT)) == (kMemGraphics | MEM_COMMIT)) {
leonida::title_host_regions().register_range(reinterpret_cast<uint64_t>(result), size, 1);
}
return result;
}

BOOL WINAPI TitleVirtualFree(LPVOID address, SIZE_T size, DWORD free_type) {
if (address && (free_type & (MEM_DECOMMIT | MEM_RELEASE)) != 0) {
uint64_t unreg_size = size;
if (unreg_size == 0) {
MEMORY_BASIC_INFORMATION mbi{};
if (VirtualQuery(address, &mbi, sizeof(mbi))) {
unreg_size = mbi.RegionSize;
}
}
if (unreg_size != 0) {
leonida::title_host_regions().unregister_range(reinterpret_cast<uint64_t>(address), unreg_size);
}
}
return VirtualFree(address, size, free_type);
}

} // namespace
Expand Down Expand Up @@ -855,7 +892,9 @@ bool commit_physical_pages(const uint64_t* page_array, size_t count) {

}

#define TITLE_VIRTUAL_ALLOC(dll) {dll, "VirtualAlloc", reinterpret_cast<void*>(&TitleVirtualAlloc)}
#define TITLE_VIRTUAL_ALLOC(dll) \
{dll, "VirtualAlloc", reinterpret_cast<void*>(&TitleVirtualAlloc)}, \
{dll, "VirtualFree", reinterpret_cast<void*>(&TitleVirtualFree)}

// The classic libraries and the memory api sets a title's imports name.
SHIM_TABLE(title_memory,
Expand Down
Loading