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
7 changes: 4 additions & 3 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ What is implemented (all of it testable without the game):
targets and depth-stencils too: their attachment views are `VK_IMAGE_VIEW_TYPE_1D_ARRAY`), and
2D textures in the common render-target, depth and texture formats (typeless families become mutable-format
images; `R24G8_TYPELESS` and `R32G8X24_TYPELESS` are depth-stencil images with or without
`ALLOW_DEPTH_STENCIL`, as Vulkan has no colour format with their layout), plus non-attachment 3D textures with
mipmapped upload, image copy and readback, and multisampled 2D textures (single-mip render targets or
depth-stencils, in the sample counts the GPU has for the format);
`ALLOW_DEPTH_STENCIL`, as Vulkan has no colour format with their layout), plus 3D textures with mipmapped
upload, image copy and readback, render-target slices and, where Vulkan reports the format combination,
depth/stencil slice attachments through `VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT`, and multisampled 2D textures
(single-mip render targets or depth-stencils, in the sample counts the GPU has for the format);
- packed `R9G9B9E5_SHAREDEXP` textures map to `VK_FORMAT_E5B9G9R9_UFLOAT_PACK32` for uploads and sampled views.
When `ALLOW_UNORDERED_ACCESS` is requested on a GPU whose `VK_FORMAT_E5B9G9R9_UFLOAT_PACK32` lacks native
`STORAGE_IMAGE` support (for example NVIDIA), the layer backs the texture with a mutable `VK_FORMAT_R32_UINT`
Expand Down
11 changes: 5 additions & 6 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ struct TextureCreateInfo {
const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const bool one_d = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
info.flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
if (volume && (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET))
if (volume && (desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET |
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)))
info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
info.imageType = volume ? VK_IMAGE_TYPE_3D : one_d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D;
info.format = format.format;
Expand Down Expand Up @@ -1020,8 +1021,6 @@ struct DeviceState {
}
}
Format format = texture_storage_format(desc.Format, desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
if (desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D && (format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
return {};
format.d24 = format.format == VK_FORMAT_D24_UNORM_S8_UINT; // native or not: clears are quantised
if (format.format == VK_FORMAT_D24_UNORM_S8_UINT && !depth_usable(format.format)) {
log_once("D24_UNORM_S8_UINT unavailable, using D32_SFLOAT_S8_UINT; format", desc.Format);
Expand Down Expand Up @@ -8371,9 +8370,9 @@ class Device final
(desc.Flags & attachment) == attachment ||
(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)))
return kInvalidAllocation;
// The PC runtime: simultaneous access only single-sampled and without depth-stencil, and no volume depth.
if (((desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS) && (depth_stencil || samples > 1)) ||
(volume && depth_stencil))
// The PC runtime: simultaneous access only single-sampled and without depth-stencil. A 3D depth/stencil
// resource is valid; Vulkan represents its slice attachments with VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT.
if ((desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS) && (depth_stencil || samples > 1))
return kInvalidAllocation;
const bool tiled = is_tiled_layout(desc.Layout);
if (tiled) {
Expand Down
8 changes: 5 additions & 3 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1616,10 +1616,12 @@ class VulkanContext {
info.viewType = r.line ? VK_IMAGE_VIEW_TYPE_1D_ARRAY : VK_IMAGE_VIEW_TYPE_2D_ARRAY; // 1D images take no 2D view
info.format = r.format;
info.subresourceRange = range;
// Attachment views need only color-attachment usage. Inheriting STORAGE usage from a mutable/extended
// resource (typeless views such as RGBA8 sRGB, or R32_UINT-backed R9G9B9E5 UAV images) would require unrelated usages.
// Attachment views need only their attachment usage. Inheriting STORAGE usage from a mutable/extended
// resource (typeless views such as RGBA8 sRGB, or R32_UINT-backed R9G9B9E5 UAV images) would require unrelated
// usages; a typeless depth view likewise needs only DEPTH_STENCIL_ATTACHMENT.
VkImageViewUsageCreateInfo usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO};
usage.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
usage.usage = (range.aspectMask & depth_stencil_aspects) ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
: VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
if (r.typeless || (range.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT && r.color_attachment))
info.pNext = &usage;
VkImageView view = VK_NULL_HANDLE;
Expand Down
24 changes: 18 additions & 6 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -1254,9 +1254,13 @@ inline bool view_range(const ResourceBinding& resource, const TargetView& view,
if (view.mip >= resource.mip_levels) return false;
if (resource.volume) {
const auto depth = std::max(1u, resource.depth >> view.mip);
if (aspects != VK_IMAGE_ASPECT_COLOR_BIT || view.first_layer >= depth ||
!view.layer_count || view.layer_count > depth - view.first_layer) return false;
// Layouts and transfers address one array layer for the whole 3D mip.
constexpr VkImageAspectFlags supported = VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT |
VK_IMAGE_ASPECT_STENCIL_BIT;
if ((aspects & ~supported) || view.first_layer >= depth || !view.layer_count ||
view.layer_count > depth - view.first_layer)
return false;
// Layouts and transfers address the whole 3D mip as one array layer. Attachment callers replace this
// range with the selected W slices when they build a 2D-array view.
out = {aspects, view.mip, 1, 0, 1};
return true;
}
Expand Down Expand Up @@ -2727,9 +2731,17 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
if (!detail::view_range(p.binding, depth_clear->target, aspects, p.range))
return ReplayResult::invalid_subresource;
if (!aspects) p.skip = true;
if (const auto r = rect_view(clear_rects(depth_clear->rects), depth_clear->target, p.binding.aspects);
r != ReplayResult::success)
return r;
const bool partial_volume = p.binding.volume &&
depth_clear->target.layer_count != std::max(1u, p.binding.depth >> depth_clear->target.mip);
if (partial_volume && depth_clear->rects.empty()) {
// vkCmdClearDepthStencilImage cannot select W slices of a 3D mip. Clear through a 2D-array
// attachment view, as the native 2D DSV into the volume does.
const auto extent = detail::mip_extent(p.binding, depth_clear->target.mip);
const Rect full{0, 0, std::int32_t(extent.width), std::int32_t(extent.height)};
if (const auto r = rect_view(std::span<const Rect>(&full, 1), depth_clear->target, p.binding.aspects);
r != ReplayResult::success) return r;
} else if (const auto r = rect_view(clear_rects(depth_clear->rects), depth_clear->target,
p.binding.aspects); r != ReplayResult::success) return r;
if (!p.skip && p.rects.empty() && !vk.clear_depth_stencil_image) return ReplayResult::unsupported;
} else if (const auto* uav = std::get_if<ClearUnorderedAccess>(&command)) {
const UavView& view = uav->view;
Expand Down
128 changes: 128 additions & 0 deletions runtime/test/d3d12_clear_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,117 @@ bool check_volume_rtvs(ID3D12Device* host, ID3D12Device* ours) {
return true;
}

bool clear_volume_depth_and_read(ID3D12Device* device, std::vector<std::uint16_t>& pixels) {
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12Fence> fence;
D3D12_COMMAND_QUEUE_DESC queue_desc{};
CHECK(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
CHECK(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
CHECK(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
CHECK(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));

constexpr UINT width = 4, height = 4, depth = 4;
Ref<ID3D12Resource> image, readback;
D3D12_HEAP_PROPERTIES heap_properties{D3D12_HEAP_TYPE_DEFAULT};
D3D12_RESOURCE_DESC texture{};
texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
texture.Width = width;
texture.Height = height;
texture.DepthOrArraySize = depth;
texture.MipLevels = 1;
texture.Format = DXGI_FORMAT_R16_TYPELESS;
texture.SampleDesc.Count = 1;
texture.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
CHECK(SUCCEEDED(device->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &texture,
D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, IID_PPV_ARGS(&image.p))));
const auto got_desc = image->GetDesc();
CHECK(got_desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D && got_desc.DepthOrArraySize == depth &&
(got_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL));

D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{};
UINT rows = 0;
UINT64 total = 0;
device->GetCopyableFootprints(&texture, 0, 1, 0, &placed, &rows, nullptr, &total);
D3D12_RESOURCE_DESC buffer{};
buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
buffer.Width = total;
buffer.Height = buffer.SampleDesc.Count = 1;
buffer.DepthOrArraySize = buffer.MipLevels = 1;
buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
heap_properties.Type = D3D12_HEAP_TYPE_READBACK;
CHECK(SUCCEEDED(device->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &buffer,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&readback.p))));

Ref<ID3D12DescriptorHeap> heap;
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
CHECK(SUCCEEDED(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&heap.p))));
const auto first = heap->GetCPUDescriptorHandleForHeapStart();
const UINT increment = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
const D3D12_CPU_DESCRIPTOR_HANDLE slice0{first.ptr}, slice2{first.ptr + increment};
D3D12_DEPTH_STENCIL_VIEW_DESC view{};
view.Format = DXGI_FORMAT_D16_UNORM;
view.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
view.Texture2D.MipSlice = 0;
device->CreateDepthStencilView(image.p, &view, slice0);
view.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY;
view.Texture2DArray = {0, 2, 1};
device->CreateDepthStencilView(image.p, &view, slice2);
list->ClearDepthStencilView(slice0, D3D12_CLEAR_FLAG_DEPTH, 0.25f, 0, 0, nullptr);
list->ClearDepthStencilView(slice2, D3D12_CLEAR_FLAG_DEPTH, 0.75f, 0, 0, nullptr);

D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = image.p;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_DEPTH_WRITE;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
list->ResourceBarrier(1, &barrier);
D3D12_TEXTURE_COPY_LOCATION src{}, dst{};
src.pResource = image.p;
src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dst.pResource = readback.p;
dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
dst.PlacedFootprint = placed;
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
CHECK(SUCCEEDED(list->Close()));
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
CHECK(SUCCEEDED(queue->Signal(fence.p, 1)));
Event event;
CHECK(event.handle && SUCCEEDED(fence->SetEventOnCompletion(1, event.handle)));
CHECK(WaitForSingleObject(event.handle, 5000) == WAIT_OBJECT_0);
void* mapped = nullptr;
const D3D12_RANGE range{0, SIZE_T(total)};
CHECK(SUCCEEDED(readback->Map(0, &range, &mapped)) && mapped);
const auto* base = static_cast<const unsigned char*>(mapped);
pixels.resize(width * height * depth);
for (UINT z = 0; z < depth; ++z)
for (UINT y = 0; y < height; ++y)
std::memcpy(pixels.data() + (z * height + y) * width,
base + placed.Offset + z * placed.Footprint.RowPitch * height + y * placed.Footprint.RowPitch,
width * sizeof(std::uint16_t));
const D3D12_RANGE none{};
readback->Unmap(0, &none);
return true;
}

bool check_volume_depth(ID3D12Device* host, ID3D12Device* ours) {
std::vector<std::uint16_t> expected, actual;
if (!clear_volume_depth_and_read(host, expected) || !clear_volume_depth_and_read(ours, actual)) return false;
if (actual != expected) {
std::size_t wrong = 0;
for (std::size_t i = 0; i < actual.size(); ++i) wrong += actual[i] != expected[i];
std::fprintf(stderr, "3D depth slices: %zu of %zu words differ from WARP\n", wrong, actual.size());
return false;
}
CHECK(actual[0] != actual[32]); // slice 2 was cleared independently
std::printf("3D depth DSVs: %zu D16 words match WARP across four slices\n", actual.size());
return true;
}

// A texture with a castable list takes an RTV in its own format and in the listed one, and none in a format the
// list doesn't name (the descriptor stays empty): in the other channel order, across B8G8R8X8 and B8G8R8A8, or the
// texture's own sRGB format. Typed R8G8B8A8, B8G8R8A8 and B8G8R8X8 textures refuse alike.
Expand Down Expand Up @@ -480,6 +591,23 @@ int main(int argc, char** argv) {
}
if (restricted_depth)
return check_depth_clear_edge_values(host.p, ours.p, false) ? 0 : 1;
VkPhysicalDeviceImageFormatInfo2 volume_depth_query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2};
volume_depth_query.format = VK_FORMAT_D16_UNORM;
volume_depth_query.type = VK_IMAGE_TYPE_3D;
volume_depth_query.tiling = VK_IMAGE_TILING_OPTIMAL;
volume_depth_query.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
volume_depth_query.flags = VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
VkImageFormatProperties2 volume_depth_properties{VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2};
const auto volume_depth_formats = vk.instance_fn<PFN_vkGetPhysicalDeviceImageFormatProperties2>(
"vkGetPhysicalDeviceImageFormatProperties2");
const bool volume_depth_supported = volume_depth_formats &&
volume_depth_formats(vk.physical, &volume_depth_query, &volume_depth_properties) == VK_SUCCESS;
if (volume_depth_supported) {
if (!check_volume_depth(host.p, ours.p)) return 1;
} else {
std::puts("3D depth images unavailable on this Vulkan adapter, skipped");
}
if (!check_volume_rtvs(host.p, ours.p)) return 1;
if (volume_only) return 0;
if (!check_unlisted_rtvs(ours.p)) return 1;
Expand Down
31 changes: 30 additions & 1 deletion runtime/test/replay_clear_gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace {
unsigned clears = 0, attachment_clears = 0;
VkImageSubresourceRange captured_range{};
VKAPI_ATTR void VKAPI_CALL barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags,
std::uint32_t, const VkMemoryBarrier*, std::uint32_t, const VkBufferMemoryBarrier*,
std::uint32_t, const VkImageMemoryBarrier*) {}
Expand Down Expand Up @@ -45,13 +46,17 @@ leonida::ReplayFunctions functions() {
vk.begin_rendering = begin;
vk.end_rendering = end;
vk.clear_attachments = attachments;
vk.image_view = [](const auto&, const auto&) { return reinterpret_cast<VkImageView>(std::uintptr_t{1}); };
vk.image_view = [](const auto&, const auto& range) {
captured_range = range;
return reinterpret_cast<VkImageView>(std::uintptr_t{1});
};
return vk;
}

leonida::ReplayResult run(const leonida::CommandList& list, const leonida::ResourceBinding& binding,
const leonida::ReplayFunctions& vk, leonida::LayoutTracker* tracker = nullptr) {
clears = attachment_clears = 0;
captured_range = {};
return leonida::replay(list, VK_NULL_HANDLE, [&](auto) { return &binding; }, vk, tracker);
}
} // namespace
Expand Down Expand Up @@ -102,6 +107,30 @@ int main() {
expect(run(rect_color, color, vk, &tracker) == ReplayResult::success && attachment_clears == 1,
"rectangle clear on an initialized image needs no image clear");

// A 3D depth image accepts depth aspects. A DSV that selects one W slice must clear through a 2D-array
// attachment view; vkCmdClearDepthStencilImage cannot select slices of a 3D mip.
auto volume_depth = image(VK_FORMAT_D16_UNORM, VK_IMAGE_ASPECT_DEPTH_BIT);
volume_depth.volume = true;
volume_depth.depth = 4;
leonida::CommandList slice_depth;
expect(slice_depth.clear_depth_stencil({1, 0, 2, 1}, leonida::clear_depth, 0.75f, 0) && slice_depth.close(),
"record a one-slice 3D depth clear");
vk = functions();
expect(run(slice_depth, volume_depth, vk) == ReplayResult::success && clears == 0 && attachment_clears == 1,
"one-slice 3D depth clear uses an attachment, not an image clear");
expect(captured_range.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT && captured_range.baseArrayLayer == 2 &&
captured_range.layerCount == 1,
"one-slice 3D depth view selects W slice 2");
leonida::CommandList whole_volume_depth;
expect(whole_volume_depth.clear_depth_stencil({1, 0, 0, 4}, leonida::clear_depth, 0.25f, 0) &&
whole_volume_depth.close(),
"record a whole-volume depth clear");
vk = functions();
vk.clear_depth_stencil_image = clear_depth;
expect(run(whole_volume_depth, volume_depth, vk) == ReplayResult::success && clears == 1 &&
attachment_clears == 0,
"whole-volume depth clear keeps the image-clear path");

// not_zeroed images (D3D12_HEAP_FLAG_CREATE_NOT_ZEROED) are only moved out of UNDEFINED, never cleared.
auto not_zeroed = color;
not_zeroed.not_zeroed = true;
Expand Down
Loading