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
4 changes: 4 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4097,6 +4097,10 @@ if(TARGET Vulkan::Headers)
set_tests_properties(xbox_raw_2d_stripes PROPERTIES ENVIRONMENT "LEONIDA_TEST_LARGE_RAW_TEXTURE=1" SKIP_RETURN_CODE 77 TIMEOUT 35)
add_test(NAME xbox_raw_2d_surface COMMAND d3d12_m2_textured_test)
set_tests_properties(xbox_raw_2d_surface PROPERTIES ENVIRONMENT "LEONIDA_TEST_RAW_TEXTURE=1" SKIP_RETURN_CODE 77 TIMEOUT 25)
add_test(NAME xbox_raw_2d_same_va COMMAND d3d12_m2_textured_test)
set_tests_properties(xbox_raw_2d_same_va PROPERTIES ENVIRONMENT "LEONIDA_TEST_RAW_TEXTURE=1;LEONIDA_TEST_RAW_SAME_VA=1;LEONIDA_VK_VALIDATION=1" SKIP_RETURN_CODE 77 TIMEOUT 25)
add_test(NAME xbox_raw_2d_takeover COMMAND d3d12_m2_textured_test)
set_tests_properties(xbox_raw_2d_takeover PROPERTIES ENVIRONMENT "LEONIDA_TEST_RAW_TEXTURE=1;LEONIDA_TEST_RAW_TAKEOVER=1;LEONIDA_VK_VALIDATION=1" SKIP_RETURN_CODE 77 TIMEOUT 25)
add_test(NAME xbox_packed4444_clear_sample COMMAND d3d12_m2_textured_test)
set_tests_properties(xbox_packed4444_clear_sample PROPERTIES ENVIRONMENT "LEONIDA_TEST_FIXED4=1" SKIP_RETURN_CODE 77)
add_test(NAME d3d12_m2_sampler_refresh COMMAND d3d12_m2_textured_test --refresh-samplers)
Expand Down
14 changes: 11 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14465,7 +14465,7 @@ class Resource final
std::lock_guard lock(device_.descriptor_mutex);
device_.pending_image_keys.erase(image_);
}
if (image_address_) device_.forget_image_of(image_address_, this);
if (view_key()) device_.forget_image_of(view_key(), this);
device_.context->remove_resource(id());
// Before the memory goes to the deferred queue: no submission copies into it after this.
if (shadow_) device_.remove_shadow(shadow_);
Expand Down Expand Up @@ -14542,6 +14542,7 @@ class Resource final
VkImageAspectFlags aspects() const { return aspects_; }
bool rgb96() const { return rgb96_; } // a 96-bit texture in an RGBA32 image
std::uint64_t image_address() const { return image_address_; } // what a T# names the texture by
std::uint64_t view_key() const { return view_key_ ? view_key_ : image_address_; } // what its views are kept by
const Resource* depth_stencil_uav(UINT plane) const {
return plane < storage_planes_.size() ? storage_planes_[plane].get() : nullptr;
}
Expand Down Expand Up @@ -15914,8 +15915,13 @@ class Resource final
device_.title_images.emplace(va_, this);
title_image_registered_ = true;
}
// No descriptors have been built for a newly materialized raw surface.
void set_raw_image_address(std::uint64_t address) { image_address_ = address; }
// No descriptors have been built for a newly materialized raw surface. Its T#s name the title VA, which other
// raw surfaces and slot-133 textures can share: its views stay under its own registered key until it goes.
void set_raw_image_address(std::uint64_t address) {
if (!image_ || !image_address_ || (address & 0xff)) return;
view_key_ = image_address_;
image_address_ = address;
}

void use_texture_lease(void* data, std::size_t size, UINT layout) {
if (!image_ || !data) return;
Expand Down Expand Up @@ -16463,6 +16469,7 @@ class Resource final
VkFormat uint_backed_ = VK_FORMAT_UNDEFINED;
bool a8_as_alpha_ = false;
std::uint64_t image_address_ = 0; // what a T# names the texture by
std::uint64_t view_key_ = 0; // a raw surface's own key (set_raw_image_address)
void* title_address_ = nullptr; // where the title placed this texture (place_at_title_address), or null
bool title_texture_ = false; // mark_title_texture
mutable std::atomic<std::uint32_t> shader_views_{0}; // T#s written for it (write_image_descriptor), for the census
Expand Down Expand Up @@ -29853,6 +29860,7 @@ class Device final
return;
}
g::ImageWords words = g::encode(image);
image.address = resource.view_key(); // the views' owner; a raw surface's T# keeps the title VA
if (storage && view.type == g::ImageType::image_3d) {
words[4] |= g::volume_uav_flag;
const UINT mip_depth = std::max(1u, image.depth >> image.first_mip);
Expand Down
76 changes: 71 additions & 5 deletions runtime/test/d3d12_m2_textured_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <filesystem>
#include <fstream>
#include <iterator>
#include <memory>
#include <vector>

#ifndef LEONIDA_TEST_M2_DIR
Expand Down Expand Up @@ -77,6 +78,25 @@ struct RawPages {
};
bool large_raw_surface() { return std::getenv("LEONIDA_TEST_LARGE_RAW_TEXTURE") != nullptr; }
bool test_raw_surface() { return large_raw_surface() || std::getenv("LEONIDA_TEST_RAW_TEXTURE") != nullptr; }
// Two raw SRVs at one VA with distinct widths; a sampled slot-133 texture that takes the raw VA over.
bool raw_same_va() { return test_raw_surface() && std::getenv("LEONIDA_TEST_RAW_SAME_VA") != nullptr; }
bool raw_takeover() { return test_raw_surface() && std::getenv("LEONIDA_TEST_RAW_TAKEOVER") != nullptr; }
// CTest turns validation on for xbox_raw_2d_same_va / takeover. vk_test_device.h fails the
// device when the Khronos layer is absent; skip instead, like vk_host_region_import_validation.
bool validation_layer_missing() {
const char* env = std::getenv("LEONIDA_VK_VALIDATION");
if (!env || std::strcmp(env, "0") == 0) return false;
const auto instance_proc = load_vulkan();
if (!instance_proc) return false;
const auto enumerate = reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
instance_proc(nullptr, "vkEnumerateInstanceLayerProperties"));
if (!enumerate) return false;
std::vector<VkLayerProperties> layers;
if (vktest::enumerate_all(layers, enumerate) != VK_SUCCESS) return false;
for (const auto& layer : layers)
if (std::strcmp(layer.layerName, "VK_LAYER_KHRONOS_validation") == 0) return false;
return true;
}
bool test_packed4444(){return std::getenv("LEONIDA_TEST_FIXED4")!=nullptr;}
Texel raw_gradient(UINT x, UINT y) {
return {std::uint8_t(x * 36), std::uint8_t(y * 36), std::uint8_t(255 - (x + y) * 16), 255};
Expand Down Expand Up @@ -115,7 +135,12 @@ int main(int argc, char** argv) {
const bool unbounded = argc == 2 && std::strcmp(argv[1], "--refresh-unbounded-samplers") == 0;
const bool disabled = argc == 2 && std::strcmp(argv[1], "--refresh-samplers-off") == 0;
const bool refresh = unbounded || disabled || (argc == 2 && std::strcmp(argv[1], "--refresh-samplers") == 0);
vktest::Device d;
auto owned_device = std::make_unique<vktest::Device>(); // destroyed before the validation count
vktest::Device& d = *owned_device;
if (validation_layer_missing()) {
std::printf("SKIP: LEONIDA_VK_VALIDATION is set but VK_LAYER_KHRONOS_validation is not installed\n");
return 77;
}
const auto device_status = d.initialize();
if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips
if (device_status != vktest::DeviceStatus::ready) return 1;
Expand Down Expand Up @@ -361,10 +386,13 @@ int main(int argc, char** argv) {
const UINT raw_size = large_raw_surface() ? 1024 : texture_size;
leonida::gfx10::TiledSurface2D layout;
EXPECT(leonida::gfx10::TiledSurface2D::make(leonida::gfx10::TileMode::sw_4kb_s, 4, raw_size, raw_size, layout));
raw_pages[i].data = static_cast<std::byte*>(VirtualAlloc(nullptr,layout.tiled_size(),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE));
// Room at slot 0's VA for the wider raw SRV and the slot-133 texture of the cases below.
const SIZE_T bytes = (raw_same_va() || raw_takeover()) && !i
? std::max<SIZE_T>(SIZE_T(layout.tiled_size()), 1u << 16) : SIZE_T(layout.tiled_size());
raw_pages[i].data = static_cast<std::byte*>(VirtualAlloc(nullptr,bytes,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE));
EXPECT(raw_pages[i].data);
const auto addr = reinterpret_cast<UINT64>(raw_pages[i].data);
leonida::title_va_aliases().add_title_memory(addr,layout.tiled_size());
leonida::title_va_aliases().add_title_memory(addr,bytes);
std::vector<Texel> source(raw_size*raw_size);
for (UINT y=0; y<raw_size; ++y) for (UINT x=0; x<raw_size; ++x) {
const UINT tx=x*texture_size/raw_size,ty=y*texture_size/raw_size;
Expand All @@ -391,6 +419,43 @@ int main(int argc, char** argv) {
device->CreateShaderResourceView(textures[i].p, &srv, handle);
}
}
if (raw_same_va() || raw_takeover()) {
// Slot 0's T# keeps its entry while its descriptor is live: a second raw SRV of another width at the same
// VA, or a sampled slot-133 texture placed there and released before slot 0 is written again.
std::uint32_t key[8];
std::memcpy(key, reinterpret_cast<const void*>(srv_cpu), 32);
const std::uint32_t before = rt::d3d12::bindless_hash_lookup(device.p, key);
const auto addr = reinterpret_cast<UINT64>(raw_pages[0].data);
D3D12_SHADER_RESOURCE_VIEW_DESC srv{};
srv.Format = texture_desc.Format;
srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srv.Texture2D = {0, 1, 0, 0.0f};
auto raw = texture_desc;
raw.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x105);
struct Shape { D3D12_SHADER_RESOURCE_VIEW_DESC view; UINT64 va; } shape{srv, addr};
using RawView = void(WINAPI*)(void*, const D3D12_RESOURCE_DESC*, const Shape*, D3D12_CPU_DESCRIPTOR_HANDLE);
if (raw_same_va()) {
auto wide = raw;
wide.Width = 2 * texture_size;
const D3D12_CPU_DESCRIPTOR_HANDLE third{SIZE_T(srv_cpu + 2 * step)};
reinterpret_cast<RawView>(slot(device.p,70))(device.p,&wide,&shape,third);
leonida::gfx10::ImageWords words{};
std::memcpy(words.data(),reinterpret_cast<const void*>(third.ptr),sizeof(words));
EXPECT(leonida::gfx10::is_image(words));
} else {
Ref<ID3D12Resource> takeover;
using Place = HRESULT(WINAPI*)(void*, UINT64, const D3D12_RESOURCE_DESC*, UINT, const D3D12_CLEAR_VALUE*, const IID*, void**);
EXPECT(SUCCEEDED(reinterpret_cast<Place>(slot(device.p,133))(device.p,addr,&texture_desc,
UINT(D3D12_BARRIER_LAYOUT_COMMON),nullptr,&__uuidof(ID3D12Resource),reinterpret_cast<void**>(&takeover.p))));
takeover.reset();
reinterpret_cast<RawView>(slot(device.p,70))(device.p,&raw,&shape,D3D12_CPU_DESCRIPTOR_HANDLE{SIZE_T(srv_cpu)});
}
const std::uint32_t after = rt::d3d12::bindless_hash_lookup(device.p, key);
std::fprintf(stderr, "raw %s: slot 0 T# index %u before, %u after\n",
raw_same_va() ? "same VA" : "takeover", before, after);
EXPECT(before && after && (raw_takeover() || after == before));
}
D3D12_SAMPLER_DESC sampler_desc{};
sampler_desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
sampler_desc.AddressU = sampler_desc.AddressV = sampler_desc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
Expand Down Expand Up @@ -720,8 +785,9 @@ int main(int argc, char** argv) {
}
EXPECT(!control_mismatches);
EXPECT(differing == 1536); // the right half's columns 4..6 of texels: 3 * 8 * 64
if (d.validation_errors()) {
std::fprintf(stderr, "%u validation errors\n", d.validation_errors());
owned_device.reset(); // vkDestroyDevice reports objects left behind
if (vktest::validation_error_count()) {
std::fprintf(stderr, "%u validation errors\n", vktest::validation_error_count());
return 1;
}
std::printf("M2: a textured quad through descriptor heaps, SRVs, a POINT sampler and two root descriptor tables "
Expand Down
Loading