Skip to content
Open
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
8 changes: 8 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ What is implemented (all of it testable without the game):
| a castable-format list | accepted on a texture when the formats map to Vulkan formats: the image is mutable and carries a `VkImageFormatListCreateInfo`; a buffer or an unmapped format is `E_INVALIDARG`. A cast whose texel size differs from the resource's is also `E_INVALIDARG` — the host refuses other sizes, and a size-mismatched format list is a Vulkan validation error (`VUID-VkImageCreateInfo-pNext-06722`). The list is kept on the resource, so an SRV whose format is neither the resource format nor in the list leaves a null descriptor. The resource's own format is always accepted and is added to the Vulkan format list when the D3D12 list omits it. A listed same-size cross-family cast, such as `R8G8B8A8_UINT` to `R32_UINT`, is accepted. An RTV in a listed RGBA8 family format on a typed `R8G8B8A8_UNORM` texture clears through the cast, the same reinterpretation a typeless RTV uses; without a list a typed resource still refuses a different-format RTV. `D3D12_OPTIONS12` reports `RelaxedFormatCastingSupported = TRUE` (this layer requires Vulkan 1.3, where the format list is core). `CreatePlacedResource2`'s list reaches its placed image through the same rules (D3D-90) |
| `CreateCommandQueue1`'s creator ID | ignored |

Compressed BC views must stay in the same Vulkan compatibility class: equal block byte counts do not make
BC6H and BC7 interchangeable views of one image. Castable lists reject cross-codec entries. Numeric twins
within a codec (such as BC7 UNORM/sRGB) use mutable images. A CPU-filled slot-70 texture requested in another
same-footprint BC codec is recreated in that requested codec, retaining the original console layout for
untile/copy addressing. The replacement does not carry retired codecs in its Vulkan format list.
`gfx10_descriptors` checks format resolution; `d3d12_bc_stream_views` checks native view registration and
retirement with synthetic BC7/BC6H descriptors (run with `LEONIDA_VK_VALIDATION=1` to check Vulkan usage).

`CreatePlacedResource1/2` wrap the placed path above; `2` carries the castable-format list. `CreateHeap1`
remains a stub.
- `CreateSampler2` as a wrapper over `CreateSampler` (D3D-28, part 2). Without flags it writes the same S#.
Expand Down
2 changes: 2 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_bc_stream_views COMMAND d3d12_existing_heap_test --bc-views-only)
set_tests_properties(d3d12_bc_stream_views 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.
Expand Down
30 changes: 28 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,10 @@ struct TextureCreateInfo {
TextureCreateInfo(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;
info.flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
// Console descriptors can select the numeric twin of a BC image (e.g. sRGB).
// Cross-codec views are rejected separately, even when block sizes match.
info.flags = (format.typeless || leonida::gfx10::bc_format_class(format.format))
? 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 : one_d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D;
Expand Down Expand Up @@ -7029,6 +7032,10 @@ HRESULT castable_view_formats(const Format& format, DXGI_FORMAT resource_format,
return E_INVALIDARG;
}
const leonida::BlockFormat block = leonida::block_format(cast.format, cast.aspects);
if (!leonida::gfx10::compatible_bc_formats(format.format, cast.format)) {
log_once("castable format belongs to another BC compatibility class:", castable[i]);
return E_INVALIDARG;
}
if (own.bytes && block.bytes && (block.bytes != own.bytes || block.block != own.block)) {
log_once("castable format is a different texel size than the resource:", castable[i]);
return E_INVALIDARG;
Expand Down Expand Up @@ -15390,7 +15397,26 @@ class Device final
if (f != DXGI_FORMAT_UNKNOWN && std::find(formats.begin(), formats.end(), f) == formats.end())
formats.push_back(f);
};
add(DXGI_FORMAT(d.format));
const DXGI_FORMAT requested = srv.Format == DXGI_FORMAT_UNKNOWN ? DXGI_FORMAT(d.format) : srv.Format;
const VkFormat requested_host = vulkan_format(requested, false).format;
const VkFormat original_host = vulkan_format(DXGI_FORMAT(d.format), false).format;
const bool bc_reinterpret = !leonida::gfx10::compatible_bc_formats(original_host, requested_host);
// A streamed allocation can be reused for a different BC codec. Untiling still follows
// its original block footprint, but Vulkan needs a new image in the requested codec.
// Never put the retired codec into this image's mutable-format list.
if (bc_reinterpret) {
const xg::Format a = xg::format_info(d.format), b = xg::format_info(requested);
if (!a.bytes || a.bytes != b.bytes || a.side != b.side ||
!leonida::gfx10::bc_format_class(original_host) ||
!leonida::gfx10::bc_format_class(requested_host))
return refuse_title_view("incompatible compressed view footprint", d, address), nullptr;
}
if (leonida::gfx10::bc_format_class(requested_host)) {
formats.erase(std::remove_if(formats.begin(), formats.end(), [&](DXGI_FORMAT f) {
return !leonida::gfx10::compatible_bc_formats(requested_host, vulkan_format(f, false).format);
}), formats.end());
}
add(bc_reinterpret ? requested : DXGI_FORMAT(d.format));
if (srv.Format != DXGI_FORMAT_UNKNOWN && srv.Format != DXGI_FORMAT(d.format)) {
const xg::Format a = xg::format_info(d.format), b = xg::format_info(srv.Format);
if (a.bytes != b.bytes || a.side != b.side)
Expand Down
15 changes: 14 additions & 1 deletion runtime/src/gfx10_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ inline constexpr bool is_depth_stencil_format(VkFormat format) {
// when no channel-order match exists (R8G8B8A8_UINT viewed as R32_UINT, say). Depth/stencil images are only
// compatible with their own format in Vulkan (VUID-VkImageViewCreateInfo-image-01762 / 09594), and colour
// images can never be viewed as depth/stencil. VK_FORMAT_UNDEFINED: no compatible format exists.
// Vulkan BC compatibility classes contain only the numeric variants of one codec.
// Equal block footprints alone are insufficient (e.g. BC6H and BC7 are both 16 bytes).
inline constexpr unsigned bc_format_class(VkFormat format) {
return format >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && format <= VK_FORMAT_BC7_SRGB_BLOCK
? unsigned(format - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2 + 1 : 0;
}

inline constexpr bool compatible_bc_formats(VkFormat image, VkFormat view) {
const auto a = bc_format_class(image), b = bc_format_class(view);
return (!a && !b) || (a && a == b);
}

inline constexpr VkFormat vulkan_view_format(VkFormat image_format, std::uint32_t gfx10_format,
bool allow_channel_cast = false) {
const FormatRow* image = format_row(image_format);
Expand All @@ -436,7 +448,8 @@ inline constexpr VkFormat vulkan_view_format(VkFormat image_format, std::uint32_
if (image_format == VK_FORMAT_B8G8R8A8_UNORM && gfx10_format == 136) return image_format;
const FormatRow* fallback = nullptr;
for (const FormatRow& row : format_table) {
if (row.gfx10 != gfx10_format || is_depth_stencil_format(row.vulkan)) continue;
if (row.gfx10 != gfx10_format || is_depth_stencil_format(row.vulkan) ||
!compatible_bc_formats(image_format, row.vulkan)) continue;
if (row.channels == image->channels) return row.vulkan;
if (!fallback) fallback = &row;
}
Expand Down
56 changes: 55 additions & 1 deletion runtime/test/d3d12_existing_heap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2040,17 +2040,67 @@ void print_cases(const std::vector<Case>& cases, const char* who) {
}
} // namespace

// Synthetic slot-70 reuse across same-footprint BC codecs must create legal native views.
int check_bc_stream_views(ID3D12Device* device, void* pages) {
const char* who = "BC stream view compatibility";
EXPECT(pages);
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Width = desc.Height = 8;
desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1;
desc.Format = DXGI_FORMAT_BC7_UNORM_SRGB;
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
for (const DXGI_FORMAT format : {DXGI_FORMAT_BC7_UNORM_SRGB, DXGI_FORMAT_BC6H_UF16,
DXGI_FORMAT_BC6H_SF16, DXGI_FORMAT_BC7_UNORM}) {
D3D12_SHADER_RESOURCE_VIEW_DESC srv{};
srv.Format = format;
srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srv.Texture2D.MipLevels = 1;
std::array<UINT, 14> resource{};
std::array<UINT, 16> view{};
std::memcpy(resource.data(), &desc, sizeof(desc));
std::memcpy(view.data(), &srv, sizeof(srv));
leonida::gfx10::ImageWords slot{};
EXPECT(rt::d3d12::write_placed_texture_view(device, false, resource.data(), view.data(),
reinterpret_cast<UINT64>(pages), {reinterpret_cast<SIZE_T>(slot.data())}));
std::uint32_t key[8];
std::memcpy(key, slot.data(), sizeof(key));
EXPECT(rt::d3d12::bindless_hash_lookup(device, key));
}
// Replace the stream-owned image, then let the normal submission path retire its views.
Ref<ID3D12Resource> placement;
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, desc, 6, IID_PPV_ARGS(&placement.p)), S_OK);
GpuReader gpu;
EXPECT(!gpu.init(device, 65536));
Sleep(3100); // the production descriptor retirement grace period is three seconds
ID3D12CommandList* lists[]{gpu.list.p};
gpu.queue->ExecuteCommandLists(1, lists);
EXPECT_HR(gpu.queue->Signal(gpu.fence.p, 1), S_OK);
EXPECT_HR(gpu.fence->SetEventOnCompletion(1, gpu.done), S_OK);
EXPECT(WaitForSingleObject(gpu.done, 30000) == WAIT_OBJECT_0);
return 0;
}

int main(int argc, char** argv) {
const bool prefix_only = argc == 2 && std::strcmp(argv[1], "--host-prefix-only") == 0;
const bool bc_only = argc == 2 && std::strcmp(argv[1], "--bc-views-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;
if (argc != 1 && !prefix_only && !bc_only && !async_plane) return 2;
if (bc_only) _putenv_s("LEONIDA_TITLE_IMAGE_DEFER", "0");
if (async_plane) _putenv_s("LEONIDA_TEXTURE_REFRESH_ASYNC", "1"); // read once, at the device's first refresh
_putenv_s("LEONIDA_TITLE_TEXTURE_PERIOD_MS", "0");
_putenv_s("LEONIDA_TITLE_TEXTURE_CHECK_KB", "0");
const auto free_pages = [](void* p) { if (p) VirtualFree(p, 0, MEM_RELEASE); };
// Declared before the device: backing outlives its stream trackers and native images.
std::unique_ptr<void, decltype(free_pages)> bc_pages(
bc_only ? VirtualAlloc(nullptr, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) : nullptr, free_pages);
vktest::Device vk;
const auto status = vk.initialize();
if (status == vktest::DeviceStatus::unavailable) return 77;
if (status != vktest::DeviceStatus::ready) return 1;
if (bc_only && !vk.bindless) return 77;
int failures = 0;
{
Ref<ID3D12Device> ours;
Expand All @@ -2067,6 +2117,10 @@ int main(int argc, char** argv) {
info.bindless_partially_bound = vk.partially_bound;
info.robust_image_access = vk.robust_image_access; // a Texture3D UAV is bound only with it
if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1;
if (bc_only) {
const int result = check_bc_stream_views(ours.p, bc_pages.get());
return result || vk.validation_errors() ? 1 : 0;
}
if (prefix_only) {
if (!vk.host_pointer_alignment) return 77;
const int result = check_host_prefix_buffers(ours.p);
Expand Down
19 changes: 19 additions & 0 deletions runtime/test/gfx10_descriptors_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ int main() {
CHECK(vulkan_view_format(VK_FORMAT_R8G8B8A8_UINT, 20, true) == VK_FORMAT_R32_UINT);
CHECK(vulkan_view_format(VK_FORMAT_UNDEFINED, 56) == VK_FORMAT_UNDEFINED);

// Equal compressed block sizes and channel orders do not make BC codecs compatible.
for (const bool cast : {false, true}) {
for (const auto& source : format_table) {
if (source.vulkan < VK_FORMAT_BC1_RGB_UNORM_BLOCK || source.vulkan > VK_FORMAT_BC7_SRGB_BLOCK)
continue;
for (const auto& target : format_table) {
const auto resolved = vulkan_view_format(source.vulkan, target.gfx10, cast);
if (resolved == VK_FORMAT_UNDEFINED) continue;
CHECK(resolved >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && resolved <= VK_FORMAT_BC7_SRGB_BLOCK);
CHECK((resolved - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2 ==
(source.vulkan - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2);
}
}
CHECK(vulkan_view_format(VK_FORMAT_BC7_UNORM_BLOCK,
format_from_vulkan(VK_FORMAT_BC7_SRGB_BLOCK), cast) == VK_FORMAT_BC7_SRGB_BLOCK);
CHECK(vulkan_view_format(VK_FORMAT_BC6H_UFLOAT_BLOCK,
format_from_vulkan(VK_FORMAT_BC6H_SFLOAT_BLOCK), cast) == VK_FORMAT_BC6H_SFLOAT_BLOCK);
}

// Depth/stencil images are only compatible with their own format, even with allow_channel_cast = true,
// and colour images never resolve to a depth/stencil view format.
for (const VkFormat ds : {VK_FORMAT_D16_UNORM, VK_FORMAT_X8_D24_UNORM_PACK32, VK_FORMAT_D32_SFLOAT,
Expand Down
Loading