feat(device): applications ask for the device features their shaders use

Device
- Device::Initialize(required, optional) takes DeviceFeatures, the five
  core feature structs vulkan10 to vulkan14. The library enables only what
  its own shaders and code use. Device::enabledFeatures says what was
  enabled, supportedFeatures what the GPU has, apiVersion the version in
  use.
- shaderInt16, shaderFloat16, storageBuffer16BitAccess, scalarBlockLayout
  and the uniform-buffer, storage-buffer and storage-image non-uniform
  indexing bits are no longer enabled for everyone. An application whose
  shaders take uint16_t spec constants passes
  {.vulkan10 = {.shaderInt16 = VK_TRUE}}. scalarBlockLayout stays on with
  the NVIDIA workaround, whose TLAS address may sit in a struct's tail
  padding.
- Every GPU is weighed before one is chosen: Vulkan 1.3, the swapchain and,
  below 1.4, maintenance5 extensions, the library's needs, the required
  features. One that can't run the application is skipped, saying why when
  it ranks higher. A CPU device runs it only when nothing else is listed,
  and CRAFTER_GRAPHICS_DEVICE never falls back. A GPU two drivers both list
  is tried through each.
- When no GPU qualifies, Initialize throws naming each GPU's gaps. It
  destroys the instance it made, before any Wayland connection, so it can
  be called again asking for less.
- maintenance5 is vulkan14's on a 1.4 GPU and VK_KHR_maintenance5's below.
  CRAFTER_GRAPHICS_NO_VULKAN_1_4 runs as a 1.3 application, to exercise
  that path on a 1.4 GPU.
- VK_KHR_spirv_1_4 and VK_KHR_shader_float_controls, core since 1.2, are no
  longer enabled, nor VK_EXT_shader_atomic_float, which was enabled without
  its feature struct. memoryDecompression's feature bit is queried before
  it is enabled.

Shaders
- VulkanShader checks a module's capabilities, declared directly or
  implicitly, against the enabled core features, the GPU's subgroup
  operations and the optional groups, and throws naming the field to pass,
  or saying the GPU lacks it. The tables are generated from vk.xml and
  SPIR-V's grammar.
- WorkaroundNvidiaAS::Patch sized a row-major matrix push member by its
  columns, so the TLAS address could land inside it.

Window
- The swapchain format is one whose own feature flags allow storage reads
  and writes without a format, which since Vulkan 1.3 each format says for
  itself; shaderStorageImageRead/WriteWithoutFormat are no longer required.

Tests
- DeviceFeatures: ResolveFeatures, ChooseDevice and CheckShaderCapabilities,
  without a GPU.
- FeatureRequests: required and optional features end to end, the 1.3
  path, and a retry after a refusal.
- PushConstantRewrite: a row-major mat3x4 push member.
- HeapASRead, HeapDynamicIndexHit, RTPassPushData and the ray-tracing
  examples ask for the features their shaders use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-10-31 06:40:02 +01:00
commit bcdc885de8
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
18 changed files with 1721 additions and 333 deletions

View file

@ -34,6 +34,7 @@ module;
#endif
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
#include "vulkan/vulkan.h"
#include "vulkan/vk_enum_string_helper.h"
#endif
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
#include "vulkan/vulkan_wayland.h"
@ -448,23 +449,46 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
Device::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(Device::physDevice, vulkanSurface, &formatCount, surfaceFormats.data()));
// We want to get a format that best suits our needs, so we try to get one from a set of preferred formats
// Initialize the format to the first one returned by the implementation in case we can't find one of the preffered formats
VkSurfaceFormatKHR selectedFormat = surfaceFormats[0];
std::vector<VkFormat> preferredImageFormats = {
VK_FORMAT_R8G8B8A8_UNORM,
VK_FORMAT_B8G8R8A8_UNORM
// The swapchain image is a storage image that compute and ray tracing
// shaders write, and the UI kernels also read, without a format. From
// Vulkan 1.3 that is up to each format
// (VK_FORMAT_FEATURE_2_STORAGE_READ/WRITE_WITHOUT_FORMAT_BIT), not a device
// feature. Take the first surface format that allows both, R8G8B8A8_UNORM
// or B8G8R8A8_UNORM before others; failing that, one that allows writing.
const auto formatFeatures = [](VkFormat format) {
VkFormatProperties3 properties3 { .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3 };
VkFormatProperties2 properties2 { .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &properties3 };
vkGetPhysicalDeviceFormatProperties2(Device::physDevice, format, &properties2);
return properties3.optimalTilingFeatures;
};
for (auto& availableFormat : surfaceFormats) {
if (std::find(preferredImageFormats.begin(), preferredImageFormats.end(), availableFormat.format) != preferredImageFormats.end()) {
selectedFormat = availableFormat;
break;
constexpr VkFormatFeatureFlags2 writable = VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
constexpr VkFormatFeatureFlags2 readWritable = writable | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT;
const VkSurfaceFormatKHR* selectedFormat = nullptr;
int selectedScore = -1;
for (const VkSurfaceFormatKHR& available : surfaceFormats) {
const VkFormatFeatureFlags2 features = formatFeatures(available.format);
if ((features & writable) != writable) {
continue;
}
const bool preferred = available.format == VK_FORMAT_R8G8B8A8_UNORM || available.format == VK_FORMAT_B8G8R8A8_UNORM;
const int score = ((features & readWritable) == readWritable ? 2 : 0) + (preferred ? 1 : 0);
if (score > selectedScore) {
selectedFormat = &available;
selectedScore = score;
}
}
if (selectedFormat == nullptr) {
throw std::runtime_error("Crafter.Graphics: no format this window's surface offers can be written as a storage image "
"without a format (VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT), which drawing into "
"the swapchain needs");
}
if (selectedScore < 2) {
std::println(stderr, "Crafter.Graphics: the swapchain format {} can't be read as a storage image without a format, "
"which the UI kernels do", string_VkFormat(selectedFormat->format));
}
colorFormat = selectedFormat.format;
colorSpace = selectedFormat.colorSpace;
colorFormat = selectedFormat->format;
colorSpace = selectedFormat->colorSpace;
// Select a supported composite alpha format (not all devices support alpha
// opaque). This is a surface property, so pick it once here rather than on