diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 10ee582b12..f0c67e7ff9 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2788,6 +2788,8 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_existing_heap_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_existing_heap COMMAND d3d12_existing_heap_test) add_test(NAME d3d12_host_prefix COMMAND d3d12_existing_heap_test --host-prefix-only) + add_test(NAME d3d12_placed_target_map_readback COMMAND d3d12_existing_heap_test --map-readback-only) + set_tests_properties(d3d12_placed_target_map_readback PROPERTIES SKIP_RETURN_CODE 77) set_tests_properties(d3d12_host_prefix PROPERTIES SKIP_RETURN_CODE 77) set_tests_properties(d3d12_existing_heap PROPERTIES SKIP_RETURN_CODE 77) # LEONIDA_TEXTURE_REFRESH_ASYNC=1 (set by the test itself): a mapped plane's first submission reads its bytes. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 1fb36401e3..a38c90921a 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -8848,7 +8848,7 @@ class Resource final *data = title_address_; rt::placed_image_map_note::note(title_address_, subresource, read, desc_.Width == 1 && desc_.Height == 1 && desc_.Format == DXGI_FORMAT_R32G32B32A32_FLOAT); - read_back_on_map(subresource); + if (!read || read->End > read->Begin) read_back_on_map(subresource); } // From now on the CPU may write its texels whenever it likes (video planes are mapped once): the // registry checks this texture's bytes before submissions (Device::refresh_title_textures). diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index 25fa1f6544..460e95b857 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1234,6 +1234,47 @@ int check_placed_target_backing(ID3D12Device* device) { return 0; } +int check_placed_target_map_readback(ID3D12Device* device) { + const char* who = "slot-133 target Map readback"; + const auto release_pages = [](void* p) { VirtualFree(p, 0, MEM_RELEASE); }; + const std::unique_ptr pages( + VirtualAlloc(nullptr, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE), release_pages); + EXPECT(pages); + + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = desc.Height = desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1; + desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; + desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + Ref target; + EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages.get(), desc, 2, IID_PPV_ARGS(&target.p)), S_OK); + const float gpu_texel[]{1, 2, 3, 4}; + const float cpu_texel[]{5, 6, 7, 8}; + EXPECT_HR(target->WriteToSubresource(0, nullptr, gpu_texel, sizeof(gpu_texel), sizeof(gpu_texel)), S_OK); + + const D3D12_RANGE empty{0, 0}, inverted{sizeof(gpu_texel), 0}, nonempty{0, sizeof(gpu_texel)}; + const struct { + const char* name; + const D3D12_RANGE* range; + const void* expected_texel; + } cases[]{ + {"slot-133 Map empty range", &empty, cpu_texel}, + {"slot-133 Map inverted range", &inverted, cpu_texel}, + {"slot-133 Map nonempty range", &nonempty, gpu_texel}, + {"slot-133 Map null range", nullptr, gpu_texel}, + }; + for (const auto& test : cases) { + who = test.name; + std::memcpy(pages.get(), cpu_texel, sizeof(cpu_texel)); + void* mapped = nullptr; + EXPECT_HR(target->Map(0, test.range, &mapped), S_OK); + EXPECT(mapped == pages.get()); + EXPECT(std::memcmp(mapped, test.expected_texel, sizeof(gpu_texel)) == 0); + target->Unmap(0, &empty); + } + return 0; +} + // Two sampled BC placements have distinct T#s. A CPU write to the first and a GPU copy into // the second must each promote only their destination and keep previously copied descriptors usable. int check_shared_bc_placements(ID3D12Device* device) { @@ -2043,8 +2084,10 @@ void print_cases(const std::vector& cases, const char* who) { int main(int argc, char** argv) { const bool prefix_only = argc == 2 && std::strcmp(argv[1], "--host-prefix-only") == 0; const bool async_plane = argc == 2 && std::strcmp(argv[1], "--mapped-plane-async") == 0; - if (argc != 1 && !prefix_only && !async_plane) return 2; + const bool map_readback_only = argc == 2 && std::strcmp(argv[1], "--map-readback-only") == 0; + if (argc != 1 && !prefix_only && !async_plane && !map_readback_only) return 2; if (async_plane) _putenv_s("LEONIDA_TEXTURE_REFRESH_ASYNC", "1"); // read once, at the device's first refresh + if (map_readback_only && _putenv_s("LEONIDA_MAP_READBACK", "1") != 0) return 1; _putenv_s("LEONIDA_TITLE_TEXTURE_PERIOD_MS", "0"); _putenv_s("LEONIDA_TITLE_TEXTURE_CHECK_KB", "0"); vktest::Device vk; @@ -2081,6 +2124,10 @@ int main(int argc, char** argv) { result += check_mapped_plane_first_use(staged.p); return result || vk.validation_errors() ? 1 : 0; } + if (map_readback_only) { + const int result = check_placed_target_map_readback(ours.p); + return result || vk.validation_errors() ? 1 : 0; + } failures += check(ours.p, "vulkan"); const std::vector vulkan_cases = open_cases(ours.p); print_cases(vulkan_cases, "vulkan");