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
12 changes: 10 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,17 @@ What is implemented (all of it testable without the game):
enables BC texture compression, and `D3D12_OPTIONS12` reports `RelaxedFormatCastingSupported = TRUE`, so a
committed or placed texture accepts a castable-format list. Other queries (including `OPTIONS6` and `OPTIONS10`)
are logged and refused until the trace shows what the game asks (question 11);
- Single-sampled `A8_UNORM` 2D textures use one byte per texel in `VK_FORMAT_R8_UNORM`. Their SRVs expose
- Single-sampled `A8_UNORM` 1D, 2D and 3D textures use one byte per texel in `VK_FORMAT_R8_UNORM`. Their SRVs expose
(0, 0, 0, A) before applying the caller's component mapping. Format support includes `MIP` when the Vulkan
image supports multiple levels. Render-target, depth-stencil and UAV creation are unsupported for this format.
image supports multiple levels. Where the GPU has `VK_FORMAT_A8_UNORM_KHR` (`VK_KHR_maintenance5`) and can
sample, copy and blend it as a colour attachment, a texture created with `ALLOW_RENDER_TARGET` is stored in that
format instead: its RTVs, PSO `RTVFormats` and clears use the alpha channel, its SRVs view it in the same format
(the T# names its one channel X, gfx10's `channels_a`), and format support adds `RENDER_TARGET` and
`BLENDABLE`. Vulkan puts A8 in a compatibility class of its own, so the two kinds of A8 texture cannot share
an image. A castable-format list naming `A8_UNORM` itself is a no-op; any other format in it is refused with
`E_INVALIDARG` for such a render target, since no other format can view its image. Without that format,
render targets are refused as before; depth-stencil and UAV creation are
unsupported for this format.
- reserved resources and `GetResourceTiling` (section 4.2);
- query heaps for occlusion, timestamps and pipeline statistics, and `GetTimestampFrequency` on the queue
(section 4.6);
Expand Down
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,13 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_clear_formats COMMAND d3d12_clear_formats_test)
set_tests_properties(d3d12_clear_formats PROPERTIES SKIP_RETURN_CODE 77)

add_executable(d3d12_a8_dimensions_test test/d3d12_a8_dimensions_test.cpp)
target_link_libraries(d3d12_a8_dimensions_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
target_compile_definitions(d3d12_a8_dimensions_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_a8_dimensions_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_a8_dimensions COMMAND d3d12_a8_dimensions_test)
set_tests_properties(d3d12_a8_dimensions PROPERTIES SKIP_RETURN_CODE 77)

# SetDescriptorHeaps validation, Reset/ClearState/bundle parity, lifetime pinning, direct-index flags.
add_executable(d3d12_set_descriptor_heaps_test test/d3d12_set_descriptor_heaps_test.cpp)
target_link_libraries(d3d12_set_descriptor_heaps_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
Expand Down
3 changes: 3 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ struct VulkanDeviceInfo {
bool fragment_shader_barycentric = false;
bool fragment_shader_interlock = false;
bool maintenance8 = false;
// VK_KHR_maintenance5, enabled where its VK_FORMAT_A8_UNORM_KHR can be sampled and is a colour attachment the GPU
// can blend: A8_UNORM render targets are then stored in that format (other A8_UNORM textures stay R8_UNORM).
bool a8_render_target = false;
// The instance enabled VK_EXT_debug_utils: SetMarker/BeginEvent/EndEvent become debug labels. Otherwise they
// do nothing.
bool debug_utils = false;
Expand Down
93 changes: 80 additions & 13 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ struct Format {
// for R9G9B9E5_SHAREDEXP, R16_UINT for B5G5R5A1_UNORM): the format its SRVs, RTVs and T#s use. Its storage view is
// `format`, in the uint class; shaders write the packed bits. UNDEFINED for every other texture.
VkFormat uint_backed = VK_FORMAT_UNDEFINED;
// An A8_UNORM render target stored as VK_FORMAT_A8_UNORM_KHR (VulkanDeviceInfo::a8_render_target): its RTVs render
// to the alpha channel and its SRVs view it in that format, whose one channel the T# names as X (gfx10's
// channels_a). Other A8_UNORM textures are R8_UNORM images. Vulkan puts A8 in a compatibility class of its own,
// so one image cannot be viewed both ways.
bool a8_as_alpha = false;
// A 96-bit R32G32B32 format, held in the R32G32B32A32 image `format` names (ResourceBinding::rgb96).
bool rgb96 = false;
};
Expand Down Expand Up @@ -953,11 +958,24 @@ struct DeviceState {
// The format a texture description is created with, D24S8 and R9G9B9E5 UAV fallbacks included.
Format texture_format(const D3D12_RESOURCE_DESC& desc) const {
if (!xr_bias_compatible(desc)) return {};
if (desc.Format == DXGI_FORMAT_A8_UNORM &&
(desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.SampleDesc.Count != 1 ||
(desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))))
return {};
// A8_UNORM: a 2D render target is VK_FORMAT_A8_UNORM_KHR; any other A8 texture, in 1D, 2D or 3D, is an
// R8_UNORM image, as the host makes all three (a title's boot sizes a 1D one).
if (desc.Format == DXGI_FORMAT_A8_UNORM) {
if (desc.SampleDesc.Count != 1 ||
(desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)))
return {};
if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) {
if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return {};
if (!info.a8_render_target) {
log_once("A8_UNORM render targets need VK_FORMAT_A8_UNORM_KHR (VK_KHR_maintenance5); format",
desc.Format);
return {};
}
Format alpha{VK_FORMAT_A8_UNORM_KHR, VK_IMAGE_ASPECT_COLOR_BIT};
alpha.a8_as_alpha = true;
return alpha;
}
}
if (desc.Format == DXGI_FORMAT_R9G9B9E5_SHAREDEXP) {
if ((desc.SampleDesc.Count != 1 && (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) ||
(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))) ||
Expand Down Expand Up @@ -1413,12 +1431,20 @@ Format texture_format(const DeviceState& device, const D3D12_RESOURCE_DESC& desc
// path, D3D-90's placed one). Each format must map and be the same texel size as the resource's: the host PC
// runtime refuses other sizes, and a size-mismatched VkImageFormatListCreateInfo is a Vulkan validation error
// (VUID-VkImageCreateInfo-pNext-06722). A nonzero count with a null list is no list. E_INVALIDARG on a bad format.
HRESULT castable_view_formats(const Format& format, UINT32 count, const DXGI_FORMAT* castable,
std::vector<VkFormat>& out) {
// An A8_UNORM texture's own format is always viewable (create_texture adds the image's format), so naming it is a
// 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<VkFormat>& out) {
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);
for (UINT32 i = 0; i < count; ++i) {
if (resource_format == DXGI_FORMAT_A8_UNORM && castable[i] == DXGI_FORMAT_A8_UNORM) continue;
if (format.a8_as_alpha) {
log_once("castable format of an A8_UNORM render target not supported (only A8_UNORM is):", castable[i]);
return E_INVALIDARG;
}
const Format cast = vulkan_format(castable[i], depth);
if (cast.format == VK_FORMAT_UNDEFINED) {
log_once("castable format not supported yet:", castable[i]);
Expand Down Expand Up @@ -1838,6 +1864,7 @@ class Resource final
}
bool has_castable_formats() const { return !castable_formats_.empty(); }
VkFormat uint_backed() const { return uint_backed_; } // Format::uint_backed
bool a8_as_alpha() const { return a8_as_alpha_; }
VkImageAspectFlags aspects() const { return aspects_; }
bool rgb96() const { return rgb96_; } // a 96-bit texture in an RGBA32 image
std::uint64_t image_address() const { return image_address_; } // what a T# names the texture by
Expand Down Expand Up @@ -2268,6 +2295,7 @@ class Resource final
binding.sampled_in_general = device_.bindless != nullptr;
format_ = format.format;
uint_backed_ = format.uint_backed;
a8_as_alpha_ = format.a8_as_alpha;
aspects_ = format.aspects;
image_address_ = device_.next_image_address.fetch_add(256);
binding_ = binding;
Expand Down Expand Up @@ -2615,6 +2643,7 @@ class Resource final
std::vector<VkFormat> view_formats_; // creation-time castable list; empty means no restriction
std::vector<DXGI_FORMAT> castable_formats_; // the same list as the application gave it
VkFormat uint_backed_ = VK_FORMAT_UNDEFINED;
bool a8_as_alpha_ = false;
std::uint64_t image_address_ = 0; // what a T# names the texture by
bool rgb96_ = false;
VkImageAspectFlags aspects_ = 0;
Expand Down Expand Up @@ -3853,7 +3882,9 @@ class PipelineState final
graphics_.color_formats.assign(desc.NumRenderTargets, VK_FORMAT_UNDEFINED);
for (UINT i = 0; i < desc.NumRenderTargets; ++i) {
if (desc.RTVFormats[i] == DXGI_FORMAT_UNKNOWN) continue;
graphics_.color_formats[i] = vulkan_format(desc.RTVFormats[i], false).format;
graphics_.color_formats[i] = desc.RTVFormats[i] == DXGI_FORMAT_A8_UNORM && device_.info.a8_render_target
? VK_FORMAT_A8_UNORM_KHR
: vulkan_format(desc.RTVFormats[i], false).format;
if (graphics_.color_formats[i] == VK_FORMAT_UNDEFINED) {
log_once("render-target format not supported yet:", desc.RTVFormats[i]);
return E_INVALIDARG;
Expand Down Expand Up @@ -4762,7 +4793,8 @@ bool image_descriptor(const Resource& resource, const TextureView& view, leonida
log_once("incompatible R9G9B9E5 view format:", int(view.format));
return false;
}
named = texture_storage_format(view.format, false).format;
// An A8 render target is viewed in its own A8 format (Format::a8_as_alpha), other A8 textures through R8.
named = resource.a8_as_alpha() ? resource_format : texture_storage_format(view.format, false).format;
if (g::is_depth_stencil_format(named)) {
log_once("depth-stencil view format on a colour texture:", int(view.format));
return false;
Expand Down Expand Up @@ -6250,7 +6282,13 @@ class Device final
const bool typed_bgra8_listed = r && !typed_rgba8 && rgba8_any_order(rd.Format) &&
r->has_castable_formats();
if (rd.Format == DXGI_FORMAT_A8_UNORM || (desc && desc->Format == DXGI_FORMAT_A8_UNORM)) {
view = {};
// Only an A8 render target stored as VK_FORMAT_A8_UNORM_KHR takes an RTV, in its own format: the
// attachment view is the image's.
const bool plain_2d = !desc || desc->ViewDimension == D3D12_RTV_DIMENSION_TEXTURE2D ||
desc->ViewDimension == D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
if (!r || !r->a8_as_alpha() || rd.Format != DXGI_FORMAT_A8_UNORM || !plain_2d ||
(desc && desc->Format != DXGI_FORMAT_UNKNOWN && desc->Format != DXGI_FORMAT_A8_UNORM))
view = {};
} else if (r && r->uint_backed() == VK_FORMAT_A1R5G5B5_UNORM_PACK16) {
// An R16_UINT-backed B5G5R5A1 UAV texture (Format::uint_backed) renders through an A1R5G5B5 view, in
// its own format only.
Expand Down Expand Up @@ -7392,7 +7430,7 @@ class Device final
hr = E_INVALIDARG;
} else {
std::vector<VkFormat> cast_formats;
hr = castable_view_formats(format, castable_count, castable, cast_formats);
hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats);
if (SUCCEEDED(hr)) hr = resource->create_texture(format, cast_formats);
if (SUCCEEDED(hr)) resource->keep_castable_formats(castable_count, castable);
}
Expand Down Expand Up @@ -7931,7 +7969,8 @@ class Device final

if (fs->Format == DXGI_FORMAT_A8_UNORM) {
if (optimal & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
fs->Support1 = D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TEXTURECUBE |
fs->Support1 = D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TEXTURE2D |
D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TEXTURECUBE |
D3D12_FORMAT_SUPPORT1_SHADER_LOAD;
if (optimal & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)
fs->Support1 |= D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE | D3D12_FORMAT_SUPPORT1_SHADER_GATHER;
Expand All @@ -7943,6 +7982,9 @@ class Device final
VK_IMAGE_TILING_OPTIMAL, usage, 0, &limits) == VK_SUCCESS &&
limits.maxMipLevels > 1)
fs->Support1 |= D3D12_FORMAT_SUPPORT1_MIP;
// Render targets are stored as VK_FORMAT_A8_UNORM_KHR, which the device checked can blend.
if (state_.info.a8_render_target)
fs->Support1 |= D3D12_FORMAT_SUPPORT1_RENDER_TARGET | D3D12_FORMAT_SUPPORT1_BLENDABLE;
}
return d3d12_trace::result(method, S_OK);
}
Expand Down Expand Up @@ -8554,7 +8596,7 @@ class Device final
hr = E_INVALIDARG;
} else {
std::vector<VkFormat> cast_formats;
hr = castable_view_formats(format, castable_count, castable, cast_formats);
hr = castable_view_formats(format, desc->Format, castable_count, castable, cast_formats);
if (SUCCEEDED(hr)) hr = resource->create_texture(format, cast_formats);
if (SUCCEEDED(hr)) resource->keep_castable_formats(castable_count, castable);
}
Expand Down Expand Up @@ -9388,6 +9430,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
LEONIDA_INSTANCE(vkGetPhysicalDeviceProperties);
LEONIDA_INSTANCE(vkGetPhysicalDeviceQueueFamilyProperties);
LEONIDA_INSTANCE(vkGetPhysicalDeviceFeatures2);
LEONIDA_INSTANCE(vkGetPhysicalDeviceFormatProperties);
LEONIDA_INSTANCE(vkGetPhysicalDeviceProperties2);
LEONIDA_INSTANCE(vkEnumerateDeviceExtensionProperties);
LEONIDA_INSTANCE(vkCreateDevice);
Expand Down Expand Up @@ -9760,6 +9803,27 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
}
#endif

#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME
// VK_KHR_maintenance5 brings VK_FORMAT_A8_UNORM_KHR (VulkanDeviceInfo::a8_render_target). It is enabled only where
// that format can be sampled, copied (every texture is a transfer source and destination) and is a colour
// attachment the GPU can blend; nothing else here relies on it.
VkPhysicalDeviceMaintenance5FeaturesKHR maintenance5{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR};
if (has_device_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME) && vkGetPhysicalDeviceFormatProperties) {
VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &maintenance5};
vkGetPhysicalDeviceFeatures2(info.physical, &query);
VkFormatProperties a8{};
vkGetPhysicalDeviceFormatProperties(info.physical, VK_FORMAT_A8_UNORM_KHR, &a8);
const VkFormatFeatureFlags needed = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT |
VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
info.a8_render_target = maintenance5.maintenance5 == VK_TRUE && (a8.optimalTilingFeatures & needed) == needed;
if (info.a8_render_target) {
maintenance5.pNext = *v13.next();
*v13.next() = &maintenance5;
}
}
#endif

#ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME
// VK_KHR_shader_fma: OpFmaKHR is a single-rounding multiply-add in SPIR-V (GLSL.std.450 Fma may round twice).
// Translated shaders built with `rdna2spirv.py --fma khr` need it (index capability FMAKHR, feature
Expand Down Expand Up @@ -9909,6 +9973,9 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
if (info.fragment_shader_interlock) device_extensions.push_back(VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME);
#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME
if (info.maintenance8) device_extensions.push_back(VK_KHR_MAINTENANCE_8_EXTENSION_NAME);
#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME
if (info.a8_render_target) device_extensions.push_back(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
#endif
#endif
#ifdef VK_KHR_SHADER_FMA_EXTENSION_NAME
if (info.shader_fma) device_extensions.push_back(VK_KHR_SHADER_FMA_EXTENSION_NAME);
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/gfx10_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ struct FormatRow {
};

inline constexpr Swizzle channels_r{select_x, select_zero, select_zero, select_one};
inline constexpr Swizzle channels_a{select_zero, select_zero, select_zero, select_x};
inline constexpr Swizzle channels_rg{select_x, select_y, select_zero, select_one};
inline constexpr Swizzle channels_rgb{select_x, select_y, select_z, select_one};
inline constexpr Swizzle channels_rgba = identity_swizzle;
Expand All @@ -227,6 +228,9 @@ inline constexpr Swizzle channels_abgr{select_w, select_z, select_y, select_x};

inline constexpr FormatRow format_table[] = {
{1, VK_FORMAT_R8_UNORM, channels_r}, // GFX10_FORMAT_8_UNORM
#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME
{1, VK_FORMAT_A8_UNORM_KHR, channels_a}, // GFX10_FORMAT_8_UNORM (an A8 render target)
#endif
{2, VK_FORMAT_R8_SNORM, channels_r}, // GFX10_FORMAT_8_SNORM
{3, VK_FORMAT_R8_USCALED, channels_r}, // GFX10_FORMAT_8_USCALED
{4, VK_FORMAT_R8_SSCALED, channels_r}, // GFX10_FORMAT_8_SSCALED
Expand Down
9 changes: 8 additions & 1 deletion runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) {
case VK_FORMAT_R8_UNORM:
case VK_FORMAT_R8_SNORM:
case VK_FORMAT_R8_UINT:
case VK_FORMAT_R8_SINT: return {1, 1};
case VK_FORMAT_R8_SINT:
#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME
case VK_FORMAT_A8_UNORM_KHR: // an A8_UNORM render target (VulkanDeviceInfo::a8_render_target)
#endif
return {1, 1};
case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
case VK_FORMAT_BC4_UNORM_BLOCK:
Expand Down Expand Up @@ -1156,6 +1160,9 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC
case VK_FORMAT_R8G8_UNORM:
case VK_FORMAT_R8G8B8A8_UNORM:
case VK_FORMAT_B8G8R8A8_UNORM: is_integer = false; unorm = {255, 255, 255, 255}; break;
#ifdef VK_KHR_MAINTENANCE_5_EXTENSION_NAME
case VK_FORMAT_A8_UNORM_KHR: is_integer = false; unorm = {0, 0, 0, 255}; break; // alpha only
#endif
case VK_FORMAT_R8G8B8A8_SRGB:
case VK_FORMAT_B8G8R8A8_SRGB: // the colour channels go through the sRGB curve on the GPU
is_integer = false;
Expand Down
Loading
Loading