d3d12: create 1D textures and 1D arrays

CheckFeatureSupport already reported TEXTURE1D, but CreateCommittedResource
rejected the dimension. Create 1D images with a height-one extent, reject
non-unit heights and attachment flags, and teach GetCopyableFootprints and
GetResourceAllocationInfo the 1D shape. The device test now uploads and reads
back a 1D texture and a layer of a 1D array, and compares allocation and
footprint results with the PC runtime.
This commit is contained in:
Ayinaki
2026-11-22 00:47:44 +00:00
parent 4a36bb1997
commit c7d5f8e8a4
3 changed files with 125 additions and 20 deletions

View File

@ -240,10 +240,10 @@ What is implemented (all of it testable without the game):
- `CopyDescriptors` / `CopyDescriptorsSimple`. A descriptor is the bytes of its slot, so a copy is a byte copy:
the destination and source range lists are walked together, one descriptor at a time, and null size arrays
mean one descriptor per range;
- committed buffers (upload and readback heaps mapped persistently and zeroed) and 2D textures in the common
render-target, depth and texture formats (typeless families become mutable-format images), 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);
- committed buffers (upload and readback heaps mapped persistently and zeroed), 1D textures and 1D arrays, and
2D textures in the common render-target, depth and texture formats (typeless families become mutable-format
images), 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);
- packed `R9G9B9E5_SHAREDEXP` textures map to `VK_FORMAT_E5B9G9R9_UFLOAT_PACK32` for uploads and sampled views;
- the descriptor shader samples `B5G5R5A1_UNORM` through a 2D SRV. Opaque and transparent red check bit 15,
and mixed-channel texels check the five-bit color fields;
@ -264,7 +264,8 @@ What is implemented (all of it testable without the game):
- reserved resources and `GetResourceTiling` (section 4.2);
- query heaps for occlusion, timestamps and pipeline statistics, and `GetTimestampFrequency` on the queue
(section 4.6);
- `GetCopyableFootprints` for buffers, 2D textures and volumes, including BC1–7 and the two-plane depth-stencil formats.
- `GetCopyableFootprints` for buffers, 1D and 1D-array textures, 2D textures and volumes, including BC1–7 and the
two-plane depth-stencil formats.
The test compares every output with the PC runtime's; that comparison found that planar
depth-stencil rows are padded to 512 bytes, not 256.
- the `1`/`2`/`3` variants as wrappers over the methods above (D3D-28): `CreateCommittedResource1/2/3`,
@ -403,7 +404,7 @@ rectangular clears of overlapping slice ranges against both Windows WARP and exp
Known gaps: buffer and constant-buffer views (UAV clears read the descriptor bytes, so they do not wait on view
creation), root signatures, PSOs, draws, typeless volume RTVs,
placed resources, `GetGPUVirtualAddress` (the shared buffer arena), depth-stencil resolves,
1D textures and the swapchain;
the swapchain;
default-heap buffers are not zero-filled yet; a fence cannot be signalled to a lower value (Vulkan timelines only
increase; such a signal is reported and dropped); the GCL8
front/back stencil references are recorded but not applied, because there is no pipeline or draw path for them to
@ -456,7 +457,7 @@ and any validation error fails them.
Results come back through D3D12 itself (`CopyTextureRegion` into a readback heap, then `Map`): the render
target, the depth and stencil planes, and a never-written texture (which must be zero). A second list uploads a
pattern and copies it on: whole into a texture created in `COMMON`, a boxed part into a smaller texture, a BC1
round trip and a buffer region. A 4x multisampled render target is cleared through a `TEXTURE2DMS` view and
round trip, a 1D texture and a 1D-array layer, and a buffer region. A 4x multisampled render target is cleared through a `TEXTURE2DMS` view and
resolved into a texture that is read back; the multisampled shapes D3D12 refuses are refused, and so is a
resolve from a single-sampled texture. `GetCopyableFootprints` is compared with the PC runtime's, and a short
run repeats on a standalone device, the path `LEONIDA_D3D12=vulkan` takes.

View File

@ -1,7 +1,7 @@
// A Vulkan-backed ID3D12Device (PC ABI) for the Xbox-ABI device proxy (d3d12_xs_device.cpp) to forward to.
// Implemented: queues, allocators, command lists, fences, descriptor heaps, render-target and depth-stencil views,
// committed buffers and textures (2D, multisampled 2D, 3D) and their 1/2/3 variants, allocation info, CPU
// texture copies (WriteToSubresource/ReadFromSubresource), query heaps, a few feature queries. Everything else is a
// committed buffers and textures (1D, 1D arrays, 2D, multisampled 2D, 3D) and their 1/2/3 variants, allocation
// info, CPU texture copies (WriteToSubresource/ReadFromSubresource), query heaps, a few feature queries. Everything else is a
// generated stub that reports itself once. See docs/d3d12_vulkan.md section 4.1/4.2 for what each answer means and
// what is still open.
#include "d3d12_com.h"
@ -181,13 +181,15 @@ leonida::TargetView volume_rtv(leonida::ResourceId id, const D3D12_RESOURCE_DESC
// ClearUnorderedAccessView* with rectangles clears through an attachment (vkCmdClearAttachments).
VkImageCreateInfo texture_create_info(const D3D12_RESOURCE_DESC& desc, const Format& format, bool attachment_usable) {
const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const bool one_d = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
info.flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
if (volume && (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET))
info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
info.imageType = volume ? VK_IMAGE_TYPE_3D : VK_IMAGE_TYPE_2D;
info.imageType = volume ? VK_IMAGE_TYPE_3D : one_d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D;
info.format = format.format;
info.extent = {std::uint32_t(desc.Width), desc.Height, volume ? UINT(desc.DepthOrArraySize) : 1u};
info.extent = {std::uint32_t(desc.Width), one_d ? 1u : desc.Height,
volume ? UINT(desc.DepthOrArraySize) : 1u};
info.mipLevels = desc.MipLevels;
info.arrayLayers = volume ? 1 : desc.DepthOrArraySize;
info.samples = VkSampleCountFlagBits(std::max(1u, desc.SampleDesc.Count));
@ -605,6 +607,7 @@ public:
auto& vk = device_.context->vk;
const bool volume = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const UINT depth = volume ? desc_.DepthOrArraySize : 1;
const bool one_d = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
if (desc_.MipLevels == 0) desc_.MipLevels = full_mips(desc_.Width, desc_.Height, depth);
VkImageCreateInfo info =
texture_create_info(desc_, format, device_.color_attachment_usable(format.format));
@ -632,7 +635,7 @@ public:
binding.format = format.format;
binding.aspects = format.aspects;
binding.width = std::uint32_t(desc_.Width);
binding.height = desc_.Height;
binding.height = one_d ? 1u : desc_.Height;
binding.mip_levels = desc_.MipLevels;
binding.array_layers = info.arrayLayers;
binding.depth = depth;
@ -1782,9 +1785,18 @@ public:
HRESULT hr = E_NOTIMPL;
if (desc->Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) {
hr = desc->SampleDesc.Count == 1 && desc->SampleDesc.Quality == 0 ? resource->create_buffer() : E_INVALIDARG;
} else if (desc->Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D &&
} else if (desc->Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE1D &&
desc->Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D &&
desc->Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) {
log_once("committed resource dimension not supported:", desc->Dimension);
} else if (desc->Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D && desc->Height != 1) {
log_once("1D texture height must be 1:", int(desc->Height));
hr = E_INVALIDARG;
} else if (desc->Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D &&
(desc->Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET |
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL))) {
log_once("1D texture cannot be an attachment, flags:", int(desc->Flags));
hr = E_INVALIDARG;
} else if (desc->Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D &&
(desc->Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
log_once("volume depth-stencil resources not supported, flags:", int(desc->Flags));
@ -1969,10 +1981,12 @@ public:
return;
}
const bool volume = desc->Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
if (!volume && desc->Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) {
const bool one_d = desc->Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
if (!volume && !one_d && desc->Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) {
log_once("GetCopyableFootprints dimension not supported:", desc->Dimension);
return fail();
}
if (one_d && desc->Height != 1) return fail();
const Format format = texture_storage_format(desc->Format, true); // typeless depth families are planar either way
UINT mips = desc->MipLevels;
const UINT depth = volume ? desc->DepthOrArraySize : 1;
@ -1989,7 +2003,8 @@ public:
planes == 2 ? (plane ? VK_IMAGE_ASPECT_STENCIL_BIT : VK_IMAGE_ASPECT_DEPTH_BIT) : format.aspects;
const leonida::BlockFormat block = leonida::block_format(format.format, aspect);
if (!block.bytes) return fail();
const UINT width = std::max<UINT>(1, UINT(desc->Width >> mip)), height = std::max<UINT>(1, desc->Height >> mip);
const UINT width = std::max<UINT>(1, UINT(desc->Width >> mip));
const UINT height = one_d ? 1 : std::max<UINT>(1, desc->Height >> mip);
const UINT blocks_wide = (width + block.block - 1) / block.block;
const UINT blocks_high = (height + block.block - 1) / block.block;
const UINT64 row_size = UINT64(blocks_wide) * block.bytes;
@ -2493,11 +2508,13 @@ private:
return {align_up(desc.Width, align_64k), align_64k};
}
const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
if (!volume && desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) {
const bool one_d = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
if (!volume && !one_d && desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) {
if (desc.Dimension != D3D12_RESOURCE_DIMENSION_UNKNOWN)
log_once("GetResourceAllocationInfo dimension not supported:", desc.Dimension);
return kInvalidAllocation;
}
if (one_d && desc.Height != 1) return kInvalidAllocation;
const UINT samples = desc.SampleDesc.Count, depth = volume ? desc.DepthOrArraySize : 1;
const UINT64 max_extent =
volume ? D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION : D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
@ -2512,8 +2529,8 @@ private:
D3D12_RESOURCE_DESC resolved = desc;
if (!resolved.MipLevels) resolved.MipLevels = full;
if (resolved.MipLevels > full) return kInvalidAllocation;
if (samples > 1 &&
(volume || resolved.MipLevels != 1 || (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)))
if (samples > 1 && (volume || one_d || resolved.MipLevels != 1 ||
(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)))
return kInvalidAllocation;
if (desc.Layout != D3D12_TEXTURE_LAYOUT_UNKNOWN)
log_once("GetResourceAllocationInfo layout as UNKNOWN:", desc.Layout);

View File

@ -80,6 +80,12 @@ D3D12_RESOURCE_DESC volume_desc(UINT16 mips) {
return desc;
}
D3D12_RESOURCE_DESC line_desc(UINT width, UINT16 mips, UINT16 layers, DXGI_FORMAT format) {
auto desc = texture_desc(width, 1, format, D3D12_RESOURCE_FLAG_NONE, mips, layers);
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE1D;
return desc;
}
D3D12_TEXTURE_COPY_LOCATION subresource(ID3D12Resource* resource, UINT index) {
D3D12_TEXTURE_COPY_LOCATION location{};
location.pResource = resource;
@ -431,6 +437,8 @@ int compare_footprints(ID3D12Device* ours) {
};
const Case cases[] = {
{volume_desc(5), 5},
{line_desc(13, 4, 1, DXGI_FORMAT_R8G8B8A8_UNORM), 4},
{line_desc(5, 3, 2, DXGI_FORMAT_R8G8B8A8_UNORM), 6},
{buffer_desc(300), 1},
{texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
{texture_desc(13, 7, DXGI_FORMAT_R9G9B9E5_SHAREDEXP, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
@ -530,6 +538,10 @@ int compare_allocation_info(ID3D12Device* ours) {
D3D12_RESOURCE_DESC big_volume = volume_desc(1);
big_volume.Width = big_volume.Height = 32;
big_volume.DepthOrArraySize = 8;
D3D12_RESOURCE_DESC bad_line = line_desc(8, 1, 1, rgba8);
bad_line.Height = 2;
D3D12_RESOURCE_DESC line_rt = line_desc(8, 1, 1, rgba8);
line_rt.Flags = rt;
const Case cases[] = {
{"buffer 300", buffer_desc(300), 0},
{"buffer 64 KiB", buffer_desc(65536), 0},
@ -547,6 +559,10 @@ int compare_allocation_info(ID3D12Device* ours) {
64 * 64 * 4},
{"8x8 array of 6", texture_desc(8, 8, rgba8, none, 4, 6), 8 * 8 * 4 * 6},
{"32x32x8 volume", big_volume, 32 * 32 * 8 * 4},
{"1D 13 full chain", line_desc(13, 4, 1, rgba8), 13 * 4},
{"1D array 5x3", line_desc(5, 3, 2, rgba8), 5 * 4 * 2},
{"1D height 2", bad_line, 0},
{"1D render target", line_rt, 8 * 4},
{"64x64 4x MSAA", with(texture_desc(64, 64, rgba8, rt), 0, 4), 64 * 64 * 4 * 4},
{"64x64 32x MSAA", with(texture_desc(64, 64, rgba8, rt), 0, 32), 64 * 64 * 4 * 32},
{"64x64 64x MSAA (invalid)", with(texture_desc(64, 64, rgba8, rt), 0, 64), 0},
@ -1987,7 +2003,8 @@ int main() {
EXPECT(create(default_heap, invalid_buffer_samples, D3D12_RESOURCE_STATE_COPY_DEST, refused_buffer) ==
E_INVALIDARG && !refused_buffer.p);
Ref<ID3D12Resource> rt_resource, packed565, ds, untouched, upload, readback, volume;
Ref<ID3D12Resource> rt_resource, packed565, ds, untouched, upload, readback, volume, line, line_array,
line_refused;
D3D12_RESOURCE_DESC xbox_rt =
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
xbox_rt.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101);
@ -2009,6 +2026,21 @@ int main() {
EXPECT(SUCCEEDED(create(readback_heap, buffer_desc(65536), D3D12_RESOURCE_STATE_COPY_DEST, readback)));
EXPECT(SUCCEEDED(create(default_heap, volume_desc(0), D3D12_RESOURCE_STATE_COPY_DEST, volume)));
EXPECT(volume->GetDesc().MipLevels == 5);
const auto line_shape = line_desc(size, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
const auto line_array_shape = line_desc(small_size, 1, 3, DXGI_FORMAT_R8G8B8A8_UNORM);
EXPECT(SUCCEEDED(create(default_heap, line_shape, D3D12_RESOURCE_STATE_COPY_DEST, line)));
EXPECT(SUCCEEDED(create(default_heap, line_array_shape, D3D12_RESOURCE_STATE_COPY_DEST, line_array)));
EXPECT(line->GetDesc().Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D && line->GetDesc().Height == 1);
EXPECT(line_array->GetDesc().DepthOrArraySize == 3);
EXPECT(rt::d3d12::vulkan_image(line.p) && rt::d3d12::vulkan_image(line_array.p));
auto refused_line = line_shape;
refused_line.Height = 2;
EXPECT(create(default_heap, refused_line, D3D12_RESOURCE_STATE_COMMON, line_refused) == E_INVALIDARG &&
!line_refused.p);
refused_line = line_shape;
refused_line.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
EXPECT(create(default_heap, refused_line, D3D12_RESOURCE_STATE_COMMON, line_refused) == E_INVALIDARG &&
!line_refused.p);
EXPECT(rt_resource->GetDesc().Width == size && rt_resource->GetDesc().Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN);
EXPECT(rt::d3d12::vulkan_image(rt_resource.p) && !rt::d3d12::vulkan_image(upload.p));
@ -2618,6 +2650,60 @@ int main() {
EXPECT(SUCCEEDED(queue->Signal(ms_fence.p, 2)) && SUCCEEDED(ms_fence->SetEventOnCompletion(2, nullptr)));
}
// 1D textures and 1D arrays use the same footprint path; one array layer is enough to prove the image
// type and the array stride. This runs after the resolve fences so it can use a higher fence value.
{
cursor = 0;
const auto line_up = place(device.p, line.p, 0, cursor);
const auto line_array_up = place(device.p, line_array.p, 1, cursor);
EXPECT(line_up.Footprint.Width == size && line_up.Footprint.Height == 1 &&
line_up.Footprint.Depth == 1 && line_up.Footprint.RowPitch == 256);
EXPECT(line_array_up.Footprint.Height == 1 && line_array_up.Footprint.Depth == 1);
for (UINT col = 0; col < size; ++col)
std::memcpy(up + line_up.Offset + col * 4, pattern(col, 0).data(), 4);
for (UINT col = 0; col < small_size; ++col)
std::memcpy(up + line_array_up.Offset + col * 4, pattern(col, 1).data(), 4);
cursor = 0;
const auto line_down = place(device.p, line.p, 0, cursor);
const auto line_array_down = place(device.p, line_array.p, 1, cursor);
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
{
const auto dst = subresource(line.p, 0);
const auto src = footprint(upload.p, line_up);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
{
const auto dst = subresource(line_array.p, 1);
const auto src = footprint(upload.p, line_array_up);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
barrier = transition(line.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
barrier = transition(line_array.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
{
const auto dst = footprint(readback.p, line_down);
const auto src = subresource(line.p, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
{
const auto dst = footprint(readback.p, line_array_down);
const auto src = subresource(line_array.p, 1);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 8)) && SUCCEEDED(fence->SetEventOnCompletion(8, nullptr)));
for (UINT col = 0; col < size; ++col)
report("1D texel", col, down + line_down.Offset + col * 4, pattern(col, 0).data(), 4);
for (UINT col = 0; col < small_size; ++col)
report("1D array texel", col, down + line_array_down.Offset + col * 4,
pattern(col, 1).data(), 4);
}
// Reserved resources (docs/d3d12_vulkan.md section 4.2): fully backed at creation, so they copy like
// committed ones and their first use reads zero; D3D12's description rules hold; tile-mapping calls are
// accepted and change nothing.
@ -2863,7 +2949,8 @@ int main() {
// Every child holds the proxy; once they are gone the proxy holds the only reference.
upload->Unmap(0, nullptr);
readback->Unmap(0, nullptr);
for (auto* r : {&rt_resource, &packed565, &ds, &untouched, &upload, &readback, &source, &whole, &part, &bc}) r->reset();
for (auto* r : {&rt_resource, &packed565, &ds, &untouched, &upload, &readback, &source, &whole, &part, &bc,
&line, &line_array}) r->reset();
list7.reset();
list.reset();
allocator.reset();