diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index c2e36319e2..88057ba5b6 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2988,14 +2988,13 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv Where the provider (`Resource::enable_depth_stencil_uav`) has no unordered-access form of a texture it answers `E_INVALIDARG` or `E_NOTIMPL`, and slots 76 and 133 then make the texture without unordered access, as before the provider: slot 76 as a committed depth-stencil texture, slot 133 through its stand-in (the title-placed texture, or - the committed one). The first such refusal per slot is logged. That is the console D16S8 format (118) on a GPU with - D24S8 attachments (every NVIDIA card): `pc_format()` makes it as `D24_UNORM_S8_UINT`, which the provider does not - take; a real boot stopped on an int3 after slot 133 returned that refusal. Its UAVs then write no - descriptor, as for the slot 25 stand-in. Any other failure (out of memory, the backing itself, a refused placement) - is passed on as before. Follow-up: teach `enable_depth_stencil_uav` the D24S8-made form of format 118 (its widened - path already serves a D16S8 target stored as `D32_SFLOAT_S8_UINT`), so these textures keep their UAVs on NVIDIA - too. `xs_proxy_mock` checks the refusal against a mock provider, and `xbox_d16s8_refused_uav` on a GPU with - `LEONIDA_CONSOLE_D16S8=d24`. + the committed one). The first such refusal per slot is logged (`xs_proxy_mock` checks the refusal against a mock + provider). Any other failure (out of memory, the backing itself, a refused placement) is passed on as before. + Console D16S8 (format 118) made as `D24_UNORM_S8_UINT` via `pc_format()` (the default on GPUs with D24S8 + attachments, such as NVIDIA) keeps its UAVs through the widened `R16_TYPELESS` + `R8_UINT` storage planes: + plane-0 barriers convert between 24-bit UNORM words in transfer scratch and `R32_SFLOAT` via `Depth24Converter` + and blit between the intermediate `R32_FLOAT` image and `R16_UNORM` (viewed as `R16_UINT` or `R16_UNORM`), while + plane 1 copies stencil bytes directly (`xbox_d16s8_d24_storage` with `LEONIDA_CONSOLE_D16S8=d24`). - Legacy and enhanced texture transitions copy the selected planes and layers to or from their storage images. Fence-ordered graphics and compute queues can share UAV access. A dedicated compute family can perform the plane transfer itself when `VK_KHR_maintenance10` and the format's depth/stencil copy flags allow it. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 66af48c7da..72ec4a2ad0 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3086,11 +3086,11 @@ if(TARGET Vulkan::Headers) set_tests_properties(xbox_d16s8_storage PROPERTIES ENVIRONMENT "LEONIDA_TEST_D16S8_UAV=1;LEONIDA_CONSOLE_D16S8=native" SKIP_RETURN_CODE 77 TIMEOUT 30) set_tests_properties(xbox_depth_storage PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30) - # Made as D24S8, a D16S8 depth-stencil texture with unordered access is refused by the depth provider; slots 76 - # and 133 make it without unordered access instead (d24 forces what a GPU with D24S8 attachments chooses). - add_test(NAME xbox_d16s8_refused_uav COMMAND xbox_depth_storage_test) - set_tests_properties(xbox_d16s8_refused_uav PROPERTIES - ENVIRONMENT "LEONIDA_TEST_D16S8_REFUSED_UAV=1;LEONIDA_CONSOLE_D16S8=d24" SKIP_RETURN_CODE 77 TIMEOUT 30) + # Made as D24S8 (the default on GPUs with D24S8 attachments, e.g. NVIDIA), a D16S8 depth-stencil texture keeps + # its UAVs through the widened R16_TYPELESS + R8_UINT planes and Depth24Converter. + add_test(NAME xbox_d16s8_d24_storage COMMAND xbox_depth_storage_test) + set_tests_properties(xbox_d16s8_d24_storage PROPERTIES + ENVIRONMENT "LEONIDA_TEST_D16S8_UAV=1;LEONIDA_CONSOLE_D16S8=d24" SKIP_RETURN_CODE 77 TIMEOUT 30) add_executable(xbox_d16s8_surface_test test/xbox_d16s8_surface_test.cpp) target_link_libraries(xbox_d16s8_surface_test PRIVATE gta6rt_d3d12_com) target_compile_definitions(xbox_d16s8_surface_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index c5f72eb272..891424e1c0 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -4968,7 +4968,8 @@ Format texture_format(const DeviceState& device, const D3D12_RESOURCE_DESC& desc // no-op. An A8 render target's VK_FORMAT_A8_UNORM_KHR image (Format::a8_as_alpha) is in a class of its own: no // other format can be a view format of it, so any other castable format is refused here, before Vulkan. HRESULT castable_view_formats(const Format& format, DXGI_FORMAT resource_format, UINT32 count, - const DXGI_FORMAT* castable, std::vector& out, bool depth_uav_planes = false) { + const DXGI_FORMAT* castable, std::vector& out, bool depth_uav_planes = false, + bool console_d16s8 = false) { if (!count || !castable) return S_OK; const bool depth = (format.aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != 0; const leonida::BlockFormat own = leonida::block_format(format.format, format.aspects); @@ -4985,6 +4986,13 @@ HRESULT castable_view_formats(const Format& format, DXGI_FORMAT resource_format, const bool valid = format.format == VK_FORMAT_D16_UNORM ? view == DXGI_FORMAT_R16_TYPELESS || view == DXGI_FORMAT_D16_UNORM || view == DXGI_FORMAT_R16_UNORM || view == DXGI_FORMAT_R16_UINT + : (format.xbox_depth16_stencil8 || console_d16s8) + ? view == xbox_d16s8 || view == xbox_d16_plane || view == xbox_s8_plane || + (format.d24 && (view == DXGI_FORMAT_D24_UNORM_S8_UINT || + view == DXGI_FORMAT_R24_UNORM_X8_TYPELESS || + view == DXGI_FORMAT_X24_TYPELESS_G8_UINT)) || + view == DXGI_FORMAT_R16_TYPELESS || view == DXGI_FORMAT_R16_UNORM || + view == DXGI_FORMAT_R16_UINT || view == DXGI_FORMAT_R8_UINT : view == DXGI_FORMAT_R32G8X24_TYPELESS || view == DXGI_FORMAT_D32_FLOAT_S8X24_UINT || view == DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS || view == DXGI_FORMAT_X32_TYPELESS_G8X24_UINT || view == DXGI_FORMAT_R32_FLOAT || view == DXGI_FORMAT_R8_UINT; @@ -6570,10 +6578,13 @@ class Resource final } public: + void mark_console_d16s8() { console_d16s8_ = true; } + bool console_d16s8() const { return console_d16s8_; } + // The Xbox proxy calls this before publishing an allocated depth texture. Each storage plane owns a // regular color image with its own GFX10 address, so a UAV cannot replace the original SRV in set 0. HRESULT enable_depth_stencil_uav(D3D12_HEAP_FLAGS storage_heap_flags) { - const bool narrow_stencil = desc_.Format == xbox_d16s8; + const bool narrow_stencil = desc_.Format == xbox_d16s8 || console_d16s8_; const bool depth16 = desc_.Format == DXGI_FORMAT_R16_TYPELESS || narrow_stencil; if (!image_ || (!depth16 && desc_.Format != DXGI_FORMAT_R32G8X24_TYPELESS) || !(desc_.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) || @@ -6590,7 +6601,10 @@ class Resource final std::unique_ptr conversion; if (widened) { const auto blit = VK_FORMAT_FEATURE_2_BLIT_SRC_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT; - if (format_ != VK_FORMAT_D32_SFLOAT_S8_UINT || (device_.optimal_features(VK_FORMAT_R16_UNORM) & blit) != blit || + const bool d24_backing = format_ == VK_FORMAT_D24_UNORM_S8_UINT; + if ((format_ != VK_FORMAT_D32_SFLOAT_S8_UINT && !d24_backing) || + (d24_backing && !device_.context->has_depth24()) || + (device_.optimal_features(VK_FORMAT_R16_UNORM) & blit) != blit || (device_.optimal_features(VK_FORMAT_R32_SFLOAT) & blit) != blit) return E_NOTIMPL; auto intermediate = plane_desc; intermediate.Format = DXGI_FORMAT_R32_FLOAT; @@ -7206,6 +7220,7 @@ class Resource final leonida::ResourceBinding binding_{}; // a texture's, as registered with the context std::unique_ptr depth_conversion_; std::array, 2> storage_planes_; // Xbox depth/stencil UAV, by plane + bool console_d16s8_ = false; // created from Xbox format 118 (even when pc_format made it D24S8) VkImage shared_zero_ = VK_NULL_HANDLE; // owned by the device std::unique_ptr deferred_attachment_; std::atomic replay_seen_{false}; @@ -11085,12 +11100,15 @@ class Device final // device addresses the same scratch holds the triangle indices of indexed quad lists (quad_index_expand.h): // a compute pass writes them through the chunk's address and the draw binds the chunk as its index buffer. const bool index_scratch = info.buffer_device_address; + const VkBufferUsageFlags scratch_extra = + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | + (index_scratch ? VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT : 0); state_.context->enable_copy_scratch( get_device, [this, index_scratch](const VkMemoryRequirements& requirements) { return state_.allocate(requirements, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, nullptr, index_scratch); }, - index_scratch ? VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT : 0, state_.families); + scratch_extra, state_.families); if (info.buffer_device_address && info.physical && info.get_instance_proc) { if (const auto get_props = reinterpret_cast( info.get_instance_proc(info.instance, "vkGetPhysicalDeviceProperties"))) { @@ -14483,7 +14501,7 @@ class Device final return; } const Resource* storage = r->depth_stencil_uav(view.plane); - const bool raw_narrow = storage && r->desc().Format == xbox_d16s8 && view.plane == 0 && + const bool raw_narrow = storage && (r->desc().Format == xbox_d16s8 || r->console_d16s8()) && view.plane == 0 && (view.format == DXGI_FORMAT_R16_UINT || (storage->desc().Format == DXGI_FORMAT_R16_TYPELESS && view.format == DXGI_FORMAT_R16_UNORM)); if (!storage || (!raw_narrow && view.format != storage->desc().Format)) { @@ -15219,9 +15237,17 @@ class Device final if (out) *out = nullptr; auto* heap = Heap::from(heap_interface); if (!heap || !desc) return d3d12_trace::result(method, E_INVALIDARG); + const bool from_xbox_d16s8 = desc->Format == xbox_d16s8; D3D12_RESOURCE_DESC canonical = *desc; canonical.Format = pc_format(canonical.Format); // the console's D16S8 family as D24S8 desc = &canonical; + // Its clear value too, or the clear format's family (118) would not match the resource's (D24S8) below. + D3D12_CLEAR_VALUE canonical_clear{}; + if (clear) { + canonical_clear = *clear; + canonical_clear.Format = pc_format(canonical_clear.Format); + clear = &canonical_clear; + } if (heap->host_address()) { // An existing heap is not an arena range, and the placed path addresses resources by arena offset. log_once("a placed resource in an existing heap (OpenExistingHeapFromAddress) is not supported yet; " @@ -15309,6 +15335,7 @@ class Device final // create_texture makes for 1D, 2D and 3D alike (arrays and mips included), as for a committed texture. const VkDeviceSize placed_va = desc->Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? heap->offset() + heap_offset : 0; auto* resource = new Resource(state_, *desc, heap->properties(), heap->flags(), placed_va); + if (from_xbox_d16s8) resource->mark_console_d16s8(); resource->attach_device(identity()); HRESULT hr = E_NOTIMPL; if (buffer) { @@ -15320,7 +15347,8 @@ class Device final hr = E_INVALIDARG; } else { std::vector cast_formats; - hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats); + hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats, false, + from_xbox_d16s8); if (SUCCEEDED(hr)) hr = resource->create_texture(format, cast_formats); if (SUCCEEDED(hr)) resource->keep_castable_formats(castable_count, castable); } @@ -16588,6 +16616,7 @@ class Device final TitleImageOrigin origin = TitleImageOrigin::ordinary) { if (out) *out = nullptr; if (!heap || !desc) return E_INVALIDARG; + const bool from_xbox_d16s8 = desc->Format == xbox_d16s8; D3D12_RESOURCE_DESC canonical = *desc; canonical.Format = pc_format(canonical.Format); // the console's D16S8 family as D24S8 desc = &canonical; @@ -16596,6 +16625,7 @@ class Device final return E_INVALIDARG; } auto* resource = new Resource(state_, *desc, *heap, heap_flags); + if (from_xbox_d16s8) resource->mark_console_d16s8(); resource->attach_device(identity()); HRESULT hr = E_NOTIMPL; if (desc->Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) { @@ -16632,7 +16662,8 @@ class Device final hr = E_INVALIDARG; } else { std::vector cast_formats; - hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats, storage_heap_flags.has_value()); + hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats, + storage_heap_flags.has_value(), from_xbox_d16s8); if (SUCCEEDED(hr)) hr = resource->create_texture(format, cast_formats, origin); if (SUCCEEDED(hr)) resource->keep_castable_formats(castable_count, castable); if (SUCCEEDED(hr) && storage_heap_flags) hr = resource->enable_depth_stencil_uav(*storage_heap_flags); diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 145fa11ee5..a18e83aa7a 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -1802,6 +1802,11 @@ class VulkanContext { }; } + bool has_depth24() { + std::shared_lock lock(resource_mutex_); + return depth24_ != nullptr; + } + // Copies the replay stages through a buffer (depth <-> colour, vulkan_replay.h) take device-local scratch from // `allocate`. Without this such a copy is refused as unsupported. `extra_usage` is added to the chunks' transfer // usage, for configure_quad_indices. diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 359fabff07..6344745c0d 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -1355,6 +1355,9 @@ struct DepthUavCopy { ResourceBinding conversion{}; VkBuffer scratch; VkDeviceSize offset; + VkBuffer depth24_scratch = VK_NULL_HANDLE; + VkDeviceSize depth24_scratch_offset = 0; + std::vector depth24_parts; bool to_storage; }; @@ -2475,10 +2478,13 @@ void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const // Vulkan cannot copy a depth or stencil image aspect directly to or from a color image. The transfer // buffer preserves the native D16, D32 or S8 bits. Only the requested layers and planes are copied, even when the device // requires both parent aspects to transition layouts together. A storage descriptor always names GENERAL. +template inline void copy_depth_uav(const ReplayFunctions& vk, VkCommandBuffer cb, const Prepared& p, const DepthUavCopy& copy, - VkImageLayout parent_layout, LayoutTracker* tracker) { + VkImageLayout parent_layout, LayoutTracker* tracker, RunDepth24&& run_depth24, + EndStatistics&& end_statistics, BeginStatistics&& begin_statistics) { constexpr auto all = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; constexpr auto transfer = VK_PIPELINE_STAGE_TRANSFER_BIT; + constexpr auto compute = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; constexpr VkAccessFlags any = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; const auto parent_access = copy.to_storage ? VK_ACCESS_TRANSFER_READ_BIT : VK_ACCESS_TRANSFER_WRITE_BIT; const auto child_access = copy.to_storage ? VK_ACCESS_TRANSFER_WRITE_BIT : VK_ACCESS_TRANSFER_READ_BIT; @@ -2506,10 +2512,12 @@ inline void copy_depth_uav(const ReplayFunctions& vk, VkCommandBuffer cb, const child_region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // Last use of this staging range was a read by CopyBufferToImage, possibly on the previous barrier. memory_barrier(vk, cb, VK_ACCESS_TRANSFER_READ_BIT, transfer, VK_ACCESS_TRANSFER_WRITE_BIT, transfer); - // A D32 depth aspect and R32_FLOAT colour carry the same bits through transfer scratch. The - // intermediate colour image permits a real normalized blit between R32_FLOAT and R16_UNORM; - // shaders see the latter's exact bits through an R16_UINT view. + // A D32 depth aspect and R32_FLOAT colour carry the same bits through transfer scratch. A native D24 + // depth aspect carries 24-bit UNORM words in copy.scratch and converts to/from floats in depth24_scratch + // via Depth24Converter. The intermediate R32_FLOAT colour image then permits a normalized blit to/from + // R16_UNORM; shaders see the latter's exact bits through an R16_UINT (or R16_UNORM) view. const bool convert = plane == 0 && copy.conversion.image; + const bool d24_convert = convert && !copy.depth24_parts.empty(); const VkImage colour = convert ? copy.conversion.image : child.image; VkImageBlit blit{}; if (convert) { @@ -2527,14 +2535,40 @@ inline void copy_depth_uav(const ReplayFunctions& vk, VkCommandBuffer cb, const memory_barrier(vk, cb, VK_ACCESS_TRANSFER_WRITE_BIT, transfer, VK_ACCESS_TRANSFER_READ_BIT, transfer); } } - vk.copy_image_to_buffer(cb, copy.to_storage ? p.binding.image : colour, VK_IMAGE_LAYOUT_GENERAL, - copy.scratch, 1, copy.to_storage ? &parent_region : &child_region); - memory_barrier(vk, cb, VK_ACCESS_TRANSFER_WRITE_BIT, transfer, VK_ACCESS_TRANSFER_READ_BIT, transfer); - vk.copy_buffer_to_image(cb, copy.scratch, copy.to_storage ? colour : p.binding.image, VK_IMAGE_LAYOUT_GENERAL, - 1, copy.to_storage ? &child_region : &parent_region); - if (convert && copy.to_storage) { + if (d24_convert) { + child_region.bufferOffset = copy.depth24_scratch_offset; + if (copy.to_storage) { + vk.copy_image_to_buffer(cb, p.binding.image, VK_IMAGE_LAYOUT_GENERAL, copy.scratch, 1, &parent_region); + memory_barrier(vk, cb, VK_ACCESS_TRANSFER_WRITE_BIT, transfer, + VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, compute); + end_statistics(); + for (const auto& part : copy.depth24_parts) run_depth24(part); + begin_statistics(); + memory_barrier(vk, cb, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, compute, + VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, transfer); + vk.copy_buffer_to_image(cb, copy.depth24_scratch, colour, VK_IMAGE_LAYOUT_GENERAL, 1, &child_region); + memory_barrier(vk, cb, VK_ACCESS_TRANSFER_WRITE_BIT, transfer, VK_ACCESS_TRANSFER_READ_BIT, transfer); + vk.blit_image(cb, colour, VK_IMAGE_LAYOUT_GENERAL, child.image, VK_IMAGE_LAYOUT_GENERAL, 1, &blit, VK_FILTER_NEAREST); + } else { + vk.copy_image_to_buffer(cb, colour, VK_IMAGE_LAYOUT_GENERAL, copy.depth24_scratch, 1, &child_region); + memory_barrier(vk, cb, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT, + transfer | compute, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, compute); + end_statistics(); + for (const auto& part : copy.depth24_parts) run_depth24(part); + begin_statistics(); + memory_barrier(vk, cb, VK_ACCESS_SHADER_WRITE_BIT, compute, VK_ACCESS_TRANSFER_READ_BIT, transfer); + vk.copy_buffer_to_image(cb, copy.scratch, p.binding.image, VK_IMAGE_LAYOUT_GENERAL, 1, &parent_region); + } + } else { + vk.copy_image_to_buffer(cb, copy.to_storage ? p.binding.image : colour, VK_IMAGE_LAYOUT_GENERAL, + copy.scratch, 1, copy.to_storage ? &parent_region : &child_region); memory_barrier(vk, cb, VK_ACCESS_TRANSFER_WRITE_BIT, transfer, VK_ACCESS_TRANSFER_READ_BIT, transfer); - vk.blit_image(cb, colour, VK_IMAGE_LAYOUT_GENERAL, child.image, VK_IMAGE_LAYOUT_GENERAL, 1, &blit, VK_FILTER_NEAREST); + vk.copy_buffer_to_image(cb, copy.scratch, copy.to_storage ? colour : p.binding.image, VK_IMAGE_LAYOUT_GENERAL, + 1, copy.to_storage ? &child_region : &parent_region); + if (convert && copy.to_storage) { + memory_barrier(vk, cb, VK_ACCESS_TRANSFER_WRITE_BIT, transfer, VK_ACCESS_TRANSFER_READ_BIT, transfer); + vk.blit_image(cb, colour, VK_IMAGE_LAYOUT_GENERAL, child.image, VK_IMAGE_LAYOUT_GENERAL, 1, &blit, VK_FILTER_NEAREST); + } } image_barrier(vk, cb, child, color, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, child_access, transfer, any, all); @@ -2989,6 +3023,8 @@ inline bool has_helper_dispatch(const PreparedList& out, std::size_t i) { const auto& p = out.commands[i]; if (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24 || p.counted_dispatch.indirect) return true; + if (p.depth_uav_copy != UINT32_MAX && !out.depth_uav_copies[p.depth_uav_copy].depth24_parts.empty()) + return true; return std::holds_alternative(out.list->commands()[i]) && p.draw < out.draws.size() && out.draws[p.draw].quad_indices.buffer; } @@ -3322,8 +3358,27 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun copy.scratch = plane_scratch; copy.offset = plane_offset; copy.to_storage = after; + if (copy.conversion.image && p.binding.format == VK_FORMAT_D24_UNORM_S8_UINT) { + if (!vk.depth24_prepare || !vk.depth24_run || + (vk.queue_stages && !(vk.queue_stages & VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT)) || + texels > UINT32_MAX || (statistics_active && !statistics_helpers)) + return ReplayResult::unsupported; + Depth24Job job{}; + job.to_float = after; + job.app = plane_scratch; + job.app_offset = plane_offset; + job.app_size = plane_offset + bytes; + job.width = p.binding.width; + job.height = p.binding.height; + job.slices = p.range.layerCount; + job.app_row_words = job.width; + job.app_slice_words = std::uint32_t(texels); + if (!vk.depth24_prepare(job, copy.depth24_parts)) return ReplayResult::unsupported; + copy.depth24_scratch = job.scratch; + copy.depth24_scratch_offset = job.scratch_offset; + } p.depth_uav_copy = static_cast(out.depth_uav_copies.size()); - out.depth_uav_copies.push_back(copy); + out.depth_uav_copies.push_back(std::move(copy)); return ReplayResult::success; }; @@ -4330,9 +4385,11 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ vk.depth24_run(command_buffer, job); if (predicate.buffer) vk.begin_conditional_rendering(command_buffer, &predicate); }; - const auto finish_depth_uav_barrier = [&](const detail::Prepared& p, VkImageLayout layout) { + const auto finish_depth_uav_barrier = [&](const detail::Prepared& p, VkImageLayout layout, + auto&& end_statistics, auto&& begin_statistics) { if (p.depth_uav_copy != UINT32_MAX) { - detail::copy_depth_uav(vk, command_buffer, p, prepared.depth_uav_copies[p.depth_uav_copy], layout, tracker); + detail::copy_depth_uav(vk, command_buffer, p, prepared.depth_uav_copies[p.depth_uav_copy], layout, tracker, + run_depth24, end_statistics, begin_statistics); } else if (p.order_depth_uav) { // A UAV-to-UAV texture barrier names the parent, but the actual shader accesses target its children. detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, @@ -4420,7 +4477,7 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ detail::image_barrier(vk, command_buffer, p.binding, piece.range, piece.layout, new_layout, from.access, from.stages, to.access, to.stages); if (tracker) tracker->set(id, range, new_layout); - finish_depth_uav_barrier(p, new_layout); + finish_depth_uav_barrier(p, new_layout, end_statistics, begin_statistics); } else if (std::holds_alternative(command)) { const VulkanAccess uav = vulkan_access(d3d12_state::unordered_access, false); VkAccessFlags src_access = VK_ACCESS_SHADER_WRITE_BIT; @@ -4520,7 +4577,7 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ vk.pipeline_barrier2(command_buffer, &dependency); } if (tracker && !release) tracker->set(id, range, new_layout); - if (!release) finish_depth_uav_barrier(p, new_layout); + if (!release) finish_depth_uav_barrier(p, new_layout, end_statistics, begin_statistics); } else if (const auto* color_clear = std::get_if(&command)) { if (p.skip) continue; if constexpr (Checkpoints) checkpoints->record(command_buffer, i, CheckpointKind::clear); diff --git a/runtime/test/xbox_depth_storage_test.cpp b/runtime/test/xbox_depth_storage_test.cpp index 4ae4a40a3d..8164e819b1 100644 --- a/runtime/test/xbox_depth_storage_test.cpp +++ b/runtime/test/xbox_depth_storage_test.cpp @@ -2,9 +2,12 @@ #include "d3d12_test_shaders.h" #include "../shims/d3d12_com.h" #include "../shims/d3d12_xs_device.h" +#include "../src/depth24.h" #include "../src/title_va_alias.h" #include #include +#include +#include using namespace d3d12test; #define VERIFY(expr) do { if (!(expr)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #expr); return 1; } } while (0) @@ -56,46 +59,304 @@ int check_lease(ID3D12Device* dev, ID3D12Device* xbox) { } // With the console D16S8 family made as D24S8 (LEONIDA_CONSOLE_D16S8=d24; the default on a GPU with D24S8 -// attachments, every NVIDIA card), the depth provider has no unordered-access form of a format 118 depth-stencil -// texture and refuses it (Resource::enable_depth_stencil_uav). Slots 76 and 133 then make it without unordered -// access, as before the provider, so the title gets its texture instead of asserting. The description is the one -// measured where a real boot stopped: 1280x720, one mip, the all-ones layout through slot 133. -int check_d16s8_refused_uav(ID3D12Device* xbox) { +// attachments, every NVIDIA card), format 118 keeps its UAVs through the widened R16_TYPELESS + R8_UINT storage +// planes and Depth24Converter. Check slot 133 (the retail 1280x720 placement), CreateCommittedResource3 with a +// castable list, R16_UNORM float + R16_UINT uint + R8_UINT stencil UAV clears, and direct readback of the parent +// D24S8 attachment image. +int check_d16s8_d24_uav(ID3D12Device* dev, ID3D12Device* xbox) { + constexpr auto xbox_d16s8 = static_cast(118); + constexpr auto xbox_d16_plane = static_cast(119); + constexpr auto xbox_s8_plane = static_cast(120); D3D12_RESOURCE_DESC tex{}; tex.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; tex.Width = 1280; tex.Height = 720; tex.DepthOrArraySize = tex.MipLevels = 1; tex.SampleDesc.Count = 1; - tex.Format = static_cast(118); + tex.Format = xbox_d16s8; tex.Layout = static_cast(0x101); tex.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT}; D3D12_CLEAR_VALUE clear{}; clear.Format = tex.Format; clear.DepthStencil.Depth = 1; - using Create = HRESULT(WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, UINT, const D3D12_RESOURCE_DESC*, UINT, - const D3D12_CLEAR_VALUE*, const IID*, void**); - Ref committed; - VERIFY(SUCCEEDED(reinterpret_cast(slot(xbox, 76))(xbox, &heap, 0, &tex, D3D12_RESOURCE_STATE_DEPTH_WRITE, - &clear, &__uuidof(ID3D12Resource), - reinterpret_cast(&committed.p)))); - VERIFY(committed.p && committed->GetDesc().Flags == D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); + // Slot 133 placement at 1280x720 with all-ones layout keeps ALLOW_UNORDERED_ACCESS and valid plane UAVs. using Place = HRESULT(WINAPI*)(void*, UINT64, const D3D12_RESOURCE_DESC*, UINT, const D3D12_CLEAR_VALUE*, const IID*, void**); - constexpr SIZE_T pages = 16u << 20; // past the texture's size on any GPU; VirtualAlloc aligns it to 64 KiB + constexpr SIZE_T pages = 16u << 20; void* va = VirtualAlloc(nullptr, pages, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); VERIFY(va); Ref placed; - const HRESULT hr = reinterpret_cast(slot(xbox, 133))(xbox, reinterpret_cast(va), &tex, 0xFFFFFFFFu, - &clear, &__uuidof(ID3D12Resource), - reinterpret_cast(&placed.p)); - const bool made = SUCCEEDED(hr) && placed.p && placed->GetDesc().Flags == D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; - placed = {}; + const HRESULT place_hr = reinterpret_cast(slot(xbox, 133))(xbox, reinterpret_cast(va), &tex, + 0xFFFFFFFFu, &clear, &__uuidof(ID3D12Resource), + reinterpret_cast(&placed.p)); + const bool placed_ok = SUCCEEDED(place_hr) && placed.p && placed->GetDesc().Flags == tex.Flags && + placed->GetDesc().Format == DXGI_FORMAT_D24_UNORM_S8_UINT; + bool placed_views_ok = false; + if (placed_ok) { + Ref heap_uav; + D3D12_DESCRIPTOR_HEAP_DESC hd{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 3}; + if (SUCCEEDED(dev->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&heap_uav.p)))) { + const SIZE_T step = dev->GetDescriptorHandleIncrementSize(hd.Type); + const SIZE_T base = heap_uav->GetCPUDescriptorHandleForHeapStart().ptr; + const std::pair plane_views[3] = { + {DXGI_FORMAT_R16_UINT, 0}, {DXGI_FORMAT_R16_UNORM, 0}, {DXGI_FORMAT_R8_UINT, 1}}; + placed_views_ok = true; + for (UINT i = 0; i < 3; ++i) { + D3D12_UNORDERED_ACCESS_VIEW_DESC uv{}; + uv.Format = plane_views[i].first; + uv.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + uv.Texture2D.PlaneSlice = plane_views[i].second; + dev->CreateUnorderedAccessView(placed.p, nullptr, &uv, {base + i * step}); + leonida::gfx10::ImageWords words{}; + std::memcpy(words.data(), reinterpret_cast(base + i * step), sizeof(words)); + leonida::gfx10::Image image{}; + if (!leonida::gfx10::decode(words, image) || !rt::d3d12::uav_image_resource(placed.p, image.address)) + placed_views_ok = false; + } + } + } + placed.reset(); VirtualFree(va, 0, MEM_RELEASE); - VERIFY(made); - std::printf("Xbox D16S8 made as D24S8: slots 76 and 133 make the depth-stencil texture without unordered access\n"); + VERIFY(placed_ok && placed_views_ok); + + // CreateCommittedResource3 with a castable format list also keeps ALLOW_UNORDERED_ACCESS (direct and slot 127), + // while plain PC D24_UNORM_S8_UINT refuses R16/R8 castable views and refuses ALLOW_UNORDERED_ACCESS. + { + Ref dev10; + VERIFY(SUCCEEDED(dev->QueryInterface(IID_PPV_ARGS(&dev10.p)))); + D3D12_RESOURCE_DESC1 desc1{tex.Dimension, 0, 32, 32, 1, 1, xbox_d16s8, tex.SampleDesc, + D3D12_TEXTURE_LAYOUT_UNKNOWN, tex.Flags, {}}; + const DXGI_FORMAT castable[] = {xbox_d16s8, xbox_d16_plane, xbox_s8_plane, + DXGI_FORMAT_D24_UNORM_S8_UINT, DXGI_FORMAT_R24_UNORM_X8_TYPELESS, + DXGI_FORMAT_X24_TYPELESS_G8_UINT, DXGI_FORMAT_R16_TYPELESS, + DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R8_UINT}; + Ref c3; + VERIFY(SUCCEEDED(dev10->CreateCommittedResource3(&heap, D3D12_HEAP_FLAG_NONE, &desc1, + D3D12_BARRIER_LAYOUT_COMMON, &clear, nullptr, + UINT(std::size(castable)), castable, IID_PPV_ARGS(&c3.p)))); + VERIFY(c3.p && c3->GetDesc().Flags == tex.Flags && c3->GetDesc().Format == DXGI_FORMAT_D24_UNORM_S8_UINT); + const DXGI_FORMAT bad_view = DXGI_FORMAT_R32_FLOAT; + Ref bad_c3; + VERIFY(dev10->CreateCommittedResource3(&heap, D3D12_HEAP_FLAG_NONE, &desc1, D3D12_BARRIER_LAYOUT_COMMON, + &clear, nullptr, 1, &bad_view, IID_PPV_ARGS(&bad_c3.p)) == E_INVALIDARG && + !bad_c3.p); + + // Xbox slot 127 forwards to CreateCommittedResource3 with the Xbox layout word replaced. + using Slot127 = HRESULT(WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS, + const D3D12_RESOURCE_DESC1*, D3D12_BARRIER_LAYOUT, const D3D12_CLEAR_VALUE*, + ID3D12ProtectedResourceSession*, UINT, const DXGI_FORMAT*, REFIID, void**); + D3D12_RESOURCE_DESC1 xbox_desc1 = desc1; + xbox_desc1.Layout = static_cast(0x101); + Ref c3x; + VERIFY(SUCCEEDED(reinterpret_cast(slot(xbox, 127))( + xbox, &heap, D3D12_HEAP_FLAG_NONE, &xbox_desc1, D3D12_BARRIER_LAYOUT_COMMON, &clear, nullptr, + UINT(std::size(castable)), castable, IID_PPV_ARGS(&c3x.p)))); + VERIFY(c3x.p && c3x->GetDesc().Flags == tex.Flags && c3x->GetDesc().Format == DXGI_FORMAT_D24_UNORM_S8_UINT); + + // Plain PC D24_UNORM_S8_UINT: refuses ALLOW_UNORDERED_ACCESS and refuses R16/R8 castable formats. + D3D12_RESOURCE_DESC1 pc_d24 = desc1; + pc_d24.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; + D3D12_CLEAR_VALUE pc_clear = clear; + pc_clear.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; + Ref refused; + VERIFY(dev10->CreateCommittedResource3(&heap, D3D12_HEAP_FLAG_NONE, &pc_d24, D3D12_BARRIER_LAYOUT_COMMON, + &pc_clear, nullptr, 0, nullptr, IID_PPV_ARGS(&refused.p)) == E_INVALIDARG && + !refused.p); + pc_d24.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + for (const DXGI_FORMAT narrow : {DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_UINT, + DXGI_FORMAT_R8_UINT}) { + VERIFY(dev10->CreateCommittedResource3(&heap, D3D12_HEAP_FLAG_NONE, &pc_d24, D3D12_BARRIER_LAYOUT_COMMON, + &pc_clear, nullptr, 1, &narrow, IID_PPV_ARGS(&refused.p)) == + E_INVALIDARG && + !refused.p); + } + + // CreateCommittedResource2, CreatePlacedResource/2 (in a heap), and CreateReservedResource with format 118. + Ref dev8; + VERIFY(SUCCEEDED(dev->QueryInterface(IID_PPV_ARGS(&dev8.p)))); + D3D12_RESOURCE_DESC1 d16_no_uav = desc1; + d16_no_uav.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref c2; + VERIFY(SUCCEEDED(dev8->CreateCommittedResource2(&heap, D3D12_HEAP_FLAG_NONE, &d16_no_uav, + D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear, nullptr, + IID_PPV_ARGS(&c2.p)))); + VERIFY(c2.p && c2->GetDesc().Format == DXGI_FORMAT_D24_UNORM_S8_UINT); + VERIFY(dev8->CreateCommittedResource2(&heap, D3D12_HEAP_FLAG_NONE, &desc1, D3D12_RESOURCE_STATE_DEPTH_WRITE, + &clear, nullptr, IID_PPV_ARGS(&refused.p)) == E_INVALIDARG && + !refused.p); + + D3D12_RESOURCE_DESC placed_desc{}; + placed_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + placed_desc.Width = placed_desc.Height = 32; + placed_desc.DepthOrArraySize = placed_desc.MipLevels = 1; + placed_desc.Format = xbox_d16s8; + placed_desc.SampleDesc.Count = 1; + placed_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + const auto placed_alloc = dev->GetResourceAllocationInfo(0, 1, &placed_desc); + VERIFY(placed_alloc.SizeInBytes > 0 && placed_alloc.SizeInBytes != UINT64_MAX); + D3D12_HEAP_DESC heap_desc{placed_alloc.SizeInBytes, heap, placed_alloc.Alignment, D3D12_HEAP_FLAG_NONE}; + Ref placed_heap; + VERIFY(SUCCEEDED(dev->CreateHeap(&heap_desc, IID_PPV_ARGS(&placed_heap.p)))); + Ref heap_placed; + VERIFY(SUCCEEDED(dev->CreatePlacedResource(placed_heap.p, 0, &placed_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, + &clear, IID_PPV_ARGS(&heap_placed.p)))); + VERIFY(heap_placed.p && heap_placed->GetDesc().Format == DXGI_FORMAT_D24_UNORM_S8_UINT); + D3D12_RESOURCE_DESC placed_uav = placed_desc; + placed_uav.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + VERIFY(dev->CreatePlacedResource(placed_heap.p, 0, &placed_uav, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear, + IID_PPV_ARGS(&refused.p)) == E_INVALIDARG && + !refused.p); + Ref heap_placed2; + VERIFY(SUCCEEDED(dev10->CreatePlacedResource2(placed_heap.p, 0, &d16_no_uav, D3D12_BARRIER_LAYOUT_COMMON, + &clear, 0, nullptr, IID_PPV_ARGS(&heap_placed2.p)))); + VERIFY(heap_placed2.p && heap_placed2->GetDesc().Format == DXGI_FORMAT_D24_UNORM_S8_UINT); + for (const DXGI_FORMAT narrow : {DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R8_UINT}) { + VERIFY(dev10->CreatePlacedResource2(placed_heap.p, 0, &pc_d24, D3D12_BARRIER_LAYOUT_COMMON, &pc_clear, + 1, &narrow, IID_PPV_ARGS(&refused.p)) == E_INVALIDARG && + !refused.p); + } + + // Reserved resources reject stencil formats (including format 118) because they have no tile shape. + D3D12_RESOURCE_DESC reserved_desc = placed_desc; + reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; + VERIFY(dev->CreateReservedResource(&reserved_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear, + IID_PPV_ARGS(&refused.p)) == E_INVALIDARG && + !refused.p); + } + + // Direct parent D24S8 attachment readback after R16_UNORM float + R16_UINT uint + R8_UINT stencil UAV clears. + constexpr UINT w = 32, layers = 2; + tex.Width = tex.Height = w; + tex.DepthOrArraySize = layers; + using Create = HRESULT(WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, UINT, const D3D12_RESOURCE_DESC*, UINT, + const D3D12_CLEAR_VALUE*, const IID*, void**); + Ref depth; + VERIFY(SUCCEEDED(reinterpret_cast(slot(xbox, 76))(xbox, &heap, 0, &tex, D3D12_RESOURCE_STATE_DEPTH_WRITE, + &clear, &__uuidof(ID3D12Resource), + reinterpret_cast(&depth.p)))); + const D3D12_RESOURCE_DESC parent_desc = depth->GetDesc(); + VERIFY(parent_desc.Flags == tex.Flags && parent_desc.Format == DXGI_FORMAT_D24_UNORM_S8_UINT); + + Ref dsv, uav; + D3D12_DESCRIPTOR_HEAP_DESC dsv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, layers}; + D3D12_DESCRIPTOR_HEAP_DESC uav_hd{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 3}; + VERIFY(SUCCEEDED(dev->CreateDescriptorHeap(&dsv_hd, IID_PPV_ARGS(&dsv.p)))); + VERIFY(SUCCEEDED(dev->CreateDescriptorHeap(&uav_hd, IID_PPV_ARGS(&uav.p)))); + const auto hslot = [&](ID3D12DescriptorHeap* h, D3D12_DESCRIPTOR_HEAP_TYPE t, UINT i) { + auto handle = h->GetCPUDescriptorHandleForHeapStart(); + handle.ptr += i * dev->GetDescriptorHandleIncrementSize(t); + return handle; + }; + const UINT16 seed_d[layers] = {0x2000, 0xc000}; + const UINT8 seed_s[layers] = {0x41, 0x42}; + for (UINT i = 0; i < layers; ++i) { + D3D12_DEPTH_STENCIL_VIEW_DESC dv{}; + dv.Format = xbox_d16s8; + dv.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY; + dv.Texture2DArray = {0, i, 1}; + dev->CreateDepthStencilView(depth.p, &dv, hslot(dsv.p, dsv_hd.Type, i)); + } + const std::pair uv_cfg[3] = { + {DXGI_FORMAT_R16_UNORM, 0}, {DXGI_FORMAT_R16_UINT, 0}, {DXGI_FORMAT_R8_UINT, 1}}; + for (UINT i = 0; i < 3; ++i) { + D3D12_UNORDERED_ACCESS_VIEW_DESC uv{}; + uv.Format = uv_cfg[i].first; + uv.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; + uv.Texture2DArray = {0, 0, 1, uv_cfg[i].second}; + dev->CreateUnorderedAccessView(depth.p, nullptr, &uv, hslot(uav.p, uav_hd.Type, i)); + } + D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp[2 * layers]{}; + UINT64 total = 0; + dev->GetCopyableFootprints(&parent_desc, 0, 2 * layers, 0, fp, nullptr, nullptr, &total); + auto rb_desc = buffer_desc(total); + D3D12_HEAP_PROPERTIES host{D3D12_HEAP_TYPE_READBACK}; + Ref readback; + VERIFY(SUCCEEDED(dev->CreateCommittedResource(&host, D3D12_HEAP_FLAG_NONE, &rb_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&readback.p)))); + Ref queue; + Ref alloc; + Ref list; + Ref fence; + D3D12_COMMAND_QUEUE_DESC qd{}; + VERIFY(SUCCEEDED(dev->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue.p)))); + VERIFY(SUCCEEDED(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p)))); + VERIFY(SUCCEEDED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, nullptr, IID_PPV_ARGS(&list.p)))); + VERIFY(SUCCEEDED(dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + + for (UINT i = 0; i < layers; ++i) + list->ClearDepthStencilView(hslot(dsv.p, dsv_hd.Type, i), + D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, + float(seed_d[i]) / 65535.f, seed_s[i], 0, nullptr); + auto to_uav = transition(depth.p, D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + list->ResourceBarrier(1, &to_uav); + const D3D12_RECT r_unorm{2, 3, 10, 11}, r_uint{14, 5, 22, 13}, r_stencil{4, 4, 12, 12}; + const FLOAT fval[4]{0.25f}; + const UINT uval[4]{0x9123u}, sval[4]{0xa7u}; + list->ClearUnorderedAccessViewFloat({}, hslot(uav.p, uav_hd.Type, 0), depth.p, fval, 1, &r_unorm); + D3D12_RESOURCE_BARRIER uav_bar{}; + uav_bar.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; + uav_bar.UAV.pResource = depth.p; + list->ResourceBarrier(1, &uav_bar); + list->ClearUnorderedAccessViewUint({}, hslot(uav.p, uav_hd.Type, 1), depth.p, uval, 1, &r_uint); + list->ClearUnorderedAccessViewUint({}, hslot(uav.p, uav_hd.Type, 2), depth.p, sval, 1, &r_stencil); + auto to_copy = transition(depth.p, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_copy); + for (UINT sub = 0; sub < 2 * layers; ++sub) { + D3D12_TEXTURE_COPY_LOCATION src{}, dst{}; + src.pResource = depth.p; + src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + src.SubresourceIndex = sub; + dst.pResource = readback.p; + dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + dst.PlacedFootprint = fp[sub]; + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + } + VERIFY(SUCCEEDED(list->Close())); + ID3D12CommandList* batch[]{list.p}; + queue->ExecuteCommandLists(1, batch); + VERIFY(SUCCEEDED(queue->Signal(fence.p, 1))); + const auto end = GetTickCount64() + 5000; + while (fence->GetCompletedValue() < 1 && GetTickCount64() < end) Sleep(1); + VERIFY(fence->GetCompletedValue() == 1); + + void* mapped{}; + VERIFY(SUCCEEDED(readback->Map(0, nullptr, &mapped))); + const auto* bytes = static_cast(mapped); + for (UINT i = 0; i < layers; ++i) { + for (int y = 0; y < int(w); ++y) { + for (int x = 0; x < int(w); ++x) { + UINT16 exp_u16 = seed_d[i]; + if (i == 0 && x >= r_unorm.left && x < r_unorm.right && y >= r_unorm.top && y < r_unorm.bottom) + exp_u16 = 0x4000u; + if (i == 0 && x >= r_uint.left && x < r_uint.right && y >= r_uint.top && y < r_uint.bottom) + exp_u16 = 0x9123u; + const std::uint32_t exp_d24 = leonida::depth24_quantize(float(exp_u16) / 65535.f); + std::uint32_t got_word = 0; + std::memcpy(&got_word, bytes + fp[i].Offset + y * fp[i].Footprint.RowPitch + x * 4, 4); + const std::uint32_t got_d24 = got_word & 0xffffffu; + if (got_d24 != exp_d24) { + std::fprintf(stderr, "D24 parent slice %u (%d,%d): expected %06x got %06x\n", + i, x, y, exp_d24, got_d24); + readback->Unmap(0, nullptr); + return 1; + } + const UINT8 exp_s = (i == 0 && x >= r_stencil.left && x < r_stencil.right && + y >= r_stencil.top && y < r_stencil.bottom) ? 0xa7u : seed_s[i]; + const UINT8 got_s = bytes[fp[layers + i].Offset + y * fp[layers + i].Footprint.RowPitch + x]; + if (got_s != exp_s) { + std::fprintf(stderr, "D24 parent stencil slice %u (%d,%d): expected %02x got %02x\n", + i, x, y, exp_s, got_s); + readback->Unmap(0, nullptr); + return 1; + } + } + } + } + readback->Unmap(0, nullptr); + std::printf("Xbox D16S8 made as D24S8: slot 133, CreateCommittedResource3, R16_UNORM/R16_UINT/R8_UINT UAVs " + "and D24 parent readback exact\n"); return 0; } } @@ -140,11 +401,6 @@ int main() { VERIFY(SUCCEEDED(rt::d3d12_xs::create_device_proxy(dev, __uuidof(ID3D12Device), reinterpret_cast(&proxy.p), true))); rt::d3d12::set_device_outer(dev, proxy.p); - if (std::getenv("LEONIDA_TEST_D16S8_REFUSED_UAV")) { - VERIFY(check_d16s8_refused_uav(proxy.p) == 0); - VERIFY(gpu.validation_errors() == 0); - return 0; - } VERIFY(check_lease(dev, proxy.p) == 0); const auto allocation = dev->GetResourceAllocationInfo(0, 1, &tex); VERIFY(allocation.SizeInBytes >= 768u * 768 * layers * 2 && allocation.SizeInBytes != UINT64_MAX); @@ -286,6 +542,9 @@ int main() { std::printf("Xbox D16S8 depth and stencil: %u depth words and %u stencil bytes exact after UAV round trip\n", width*width*layers,width*width*layers); } readback->Unmap(0, nullptr); + const char* d16s8_mode = std::getenv("LEONIDA_CONSOLE_D16S8"); + if (with_stencil && d16s8_mode && std::strcmp(d16s8_mode, "d24") == 0) + VERIFY(check_d16s8_d24_uav(dev, proxy.p) == 0); VERIFY(gpu.validation_errors() == 0); std::printf("Depth D16 / storage R16_UINT, six layers and %u exact texels\n", width * width * layers); return 0;