RI::formatSupported¶
What it does¶
Not every GPU supports every texture format for every use. RI::formatSupported(device, format, usage, samples) checks whether a texture of format can be created on device with every usage flag in usage and with samples samples per texel (1 by default). It only queries device capabilities and creates no GPU resources.
Call RI::formatSupported before relying on a format, usage, or sample count that varies between GPUs, and choose a fallback when it returns false:
Format::eD24UnormS8Uintis not supported on AMD GPUs with Vulkan; fall back toFormat::eD32FloatS8Uint.- Storage use (
TextureUsage::eStorage) of 8-bit and packed formats, as well as MSAA sample counts above 4, vary between GPUs.
Formats that every GPU at RI's hardware floor supports for a given use (such as Format::eRGBA8Unorm render targets or Format::eD32Float depth textures) do not need to be queried.
Signature¶
// <RI/RI.hpp>, namespace RI
bool formatSupported(Device &device, Format format, TextureUsage usage, uint32 samples = 1);
From the header: Whether the GPU supports format for every usage in usage at this sample count (P58 (2)); e.g. AMD Vulkan has no eD24UnormS8Uint.
Example¶
// D24S8 is missing on some GPUs (AMD on Vulkan): ask, and fall back.
// eReadOnlyDepthSampling: sampled during read-only depth sections (below).
const RI::TextureUsage depthUsage = RI::TextureUsage::eDepthStencil | RI::TextureUsage::eSampled |
RI::TextureUsage::eReadOnlyDepthSampling;
const RI::Format depthFormat = RI::formatSupported(device, RI::Format::eD24UnormS8Uint, depthUsage)
? RI::Format::eD24UnormS8Uint
: RI::Format::eD32FloatS8Uint;
// MSAA: the sample count is part of the question.
const bool msaa4 = RI::formatSupported(device, RI::Format::eRGBA16Float, RI::TextureUsage::eRenderTarget, 4);
The same in native code¶
// D24S8 is missing on some GPUs (AMD on Vulkan): ask, and fall back.
// eReadOnlyDepthSampling: sampled during read-only depth sections (below).
const RI::TextureUsage depthUsage = RI::TextureUsage::eDepthStencil | RI::TextureUsage::eSampled |
RI::TextureUsage::eReadOnlyDepthSampling;
const RI::Format depthFormat = RI::formatSupported(device, RI::Format::eD24UnormS8Uint, depthUsage)
? RI::Format::eD24UnormS8Uint
: RI::Format::eD32FloatS8Uint;
// MSAA: the sample count is part of the question.
const bool msaa4 = RI::formatSupported(device, RI::Format::eRGBA16Float, RI::TextureUsage::eRenderTarget, 4);
// Whether a format has every feature a use needs, with optimal tiling.
bool formatSupports(VkPhysicalDevice gpu, VkFormat format, VkFormatFeatureFlags2 needed) {
VkFormatProperties3 properties3{.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3};
VkFormatProperties2 properties{.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &properties3};
vkGetPhysicalDeviceFormatProperties2(gpu, format, &properties);
return (properties3.optimalTilingFeatures & needed) == needed;
}
// D24S8 is missing on AMD: ask before using it.
const VkFormat depthFormat =
formatSupports(gpu, VK_FORMAT_D24_UNORM_S8_UINT,
VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)
? VK_FORMAT_D24_UNORM_S8_UINT
: VK_FORMAT_D32_SFLOAT_S8_UINT;
// Sample counts are a per-image question: vkGetPhysicalDeviceImageFormatProperties2.
What to watch for: optimalTilingFeatures reports which usages a format supports on its own, whereas supported sample counts and image size limits require a separate query for a specific image configuration (vkGetPhysicalDeviceImageFormatProperties2), which RI also calls.
// What a format supports, as two sets of bits.
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{.Format = DXGI_FORMAT_D24_UNORM_S8_UINT};
check(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support)), "CheckFeatureSupport");
const bool d24 = (support.Support1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL) != 0; // true on every D3D12 GPU
// Sample counts: D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS (NumQualityLevels > 0).
What to watch for: Support1 and Support2 are bitmasks. Sampled depth textures are checked using the shader-readable format of the same bit layout (for example, DXGI_FORMAT_R32_FLOAT for DXGI_FORMAT_D32_FLOAT), and MSAA sample counts above 1 require checking D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS.
Rules and errors¶
- Usage mapping:
TextureUsage::eSampledrequires sampled shader reads,TextureUsage::eStoragerequires typed storage writes,TextureUsage::eRenderTargetrequires color-attachment support, andTextureUsage::eDepthStencilrequires depth-stencil attachment support. On Vulkan,TextureUsage::eCopySrcalso requires transfer-source support, andTextureUsage::eCopyDst,TextureUsage::eStorage,TextureUsage::eRenderTarget, andTextureUsage::eDepthStencilalso require transfer-destination support (because RI's clears use transfer writes). Format::eUndefinedis never supported and always returnsfalse.RI::createTexturedoes not checkRI::formatSupported: if you create a texture with an unsupported combination, the native creation call (vkCreateImageorCreatePlacedResource2) or the backend validation layer fails. CallRI::formatSupportedfirst.