From 594e1612f7932180ce18289024cbe4c0d7a44a34 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Thu, 26 Nov 2026 09:25:32 +0100 Subject: [PATCH] fix(device): stop requiring samplerAnisotropy, and remove SamplerVulkan, the class it was required for Device::Initialize counted samplerAnisotropy among the library's own features because SamplerVulkan turned anisotropic filtering on (#168). Nothing in this repository has made a SamplerVulkan since 6e94cc6, which replaced Sponza's with a sampler-heap write. So Initialize enabled the feature for every application where the GPU had it, and skipped a GPU without it, for a class this repository no longer used. Design rule 8 has the library enable only the device features its own shaders use. SamplerVulkan: removed. #168's other suggestion keeps it, with deferred deletion, a clamp to maxSamplerAnisotropy and anisotropy left to the application. But the VkSampler it makes has no way into the heap, which takes a sampler's create-info rather than a sampler object: vkWriteSamplerDescriptorsEXT writes the descriptor straight from the VkSamplerCreateInfo. In descriptor-set mode the heap makes each slot's VkSampler itself, in DescriptorHeapVulkan::WriteSamplerSets, from the same create-info. A class that fitted the heap would hold a SamplerSlot and that write; a one-call sampler write for code without a UIRenderer is #177's, which asks for heap-mode writers on DescriptorHeapVulkan for images, buffers and samplers alike. The partition leaves project.cpp's ifaces and the umbrella module. Device: samplerAnisotropy leaves the library's features. No sampler the library, its tests or its examples make sets anisotropyEnable. An application that wants anisotropic filtering asks Device::Initialize for the feature. Docs: AGENTS.template.md counts 40 partitions and no longer lists :SamplerVulkan. AGENTS.consumer.md describes samplers as slots written from a create-info: through UIRenderer::RegisterSampler, or, without a UIRenderer, through vkWriteSamplerDescriptorsEXT as Sponza does, or WriteSamplerSets in descriptor-set mode. It says that anisotropyEnable needs .vulkan10.samplerAnisotropy from the application. The README names samplerAnisotropy among the features an application asks for. Tests: FeatureRequests checks, in its main run and in its Vulkan 1.3 child (CRAFTER_GRAPHICS_NO_VULKAN_1_4), that samplerAnisotropy is off after a Device::Initialize that does not ask for it. With the requirement put back, the check fails in both, on the RTX 4090 here, which has the feature. --- AGENTS.consumer.md | 2 +- AGENTS.template.md | 4 +- README.md | 3 +- implementations/Crafter.Graphics-Device.cpp | 2 - .../Crafter.Graphics-SamplerVulkan.cppm | 48 ------------------- interfaces/Crafter.Graphics.cppm | 1 - project.cpp | 3 +- tests/FeatureRequests/main.cpp | 8 +++- 8 files changed, 13 insertions(+), 58 deletions(-) delete mode 100644 interfaces/Crafter.Graphics-SamplerVulkan.cppm diff --git a/AGENTS.consumer.md b/AGENTS.consumer.md index 6fbc3a8..506bb9d 100644 --- a/AGENTS.consumer.md +++ b/AGENTS.consumer.md @@ -69,7 +69,7 @@ Frames are pipelined three deep and nothing waits for the GPU at steady state, s - `VulkanBuffer`: `Create(usage, memoryFlags, count, preferredFlags = 0)`; `Resize(...)` reuses the allocation when the request fits and the usage matches, otherwise queues the old one and creates; `size` (bytes), `buffer`, `address` (non-zero only with `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT`), `memoryPropertyFlagsChosen`. With `Mapped`, `value` is the `T*` of a persistent map: after writing, call `FlushDevice()` (whole buffer), `FlushDevice(offset, bytes)` (a range, rounded to `nonCoherentAtomSize`) or `FlushDevice(cmd, dstAccess, dstStage)`, which flushes and records the host-write barrier; all three are no-ops on coherent memory. `FlushHost()` is the invalidate for readback. A UI item buffer is `GraphicsBuffer` with `STORAGE_BUFFER | SHADER_DEVICE_ADDRESS` usage and `HOST_VISIBLE` memory with `DEVICE_LOCAL` as the preferred fourth argument (the examples pass both as required, which throws on a GPU without a host-visible device-local type), flushed each frame before the dispatch that reads it. - Static geometry: `VulkanBuffer::UploadDeviceLocal(usage, src, count, cmd, dstAccess, dstStage)` places the buffer in device memory, written directly on ReBAR and UMA and staged through a per-frame staging ring on a small BAR, and records the barrier to `dstStage`. `UploadDeviceLocalRange(src, offset, count, cmd, dstAccess, dstStage)` re-uploads the dirty window `[offset, offset + count)`; `src` points at the first dirty element (`vertices.data() + offset`), not at the array's start. A re-upload into a buffer a frame may still read is ordered after those reads on the GPU. - `ImageVulkan` (`Image2D` is the portable alias): `Create(width, height, mipLevels, cmd, format, usage, layout, consumerStage = RAY_TRACING_SHADER, streamed = false)` makes the image and a mapped staging buffer `image.buffer`; fill `image.buffer.value` and `Update(cmd, layout)` copies it in, builds the mip chain by blits and transitions to `layout` for `consumerStage`. `Update(const CompressedTextureAsset&, cmd, layout)` decompresses a `.ctex` on the GPU, or the CPU. A static image releases its staging after the first upload; `streamed = true` keeps it (the font atlas, through `UpdateRegion`). Pass `VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT` as `consumerStage` for an image a compute shader samples; the RT default is corrected to compute on a device without ray tracing. `image`, `imageView` and `descriptor` are public for raw descriptor writes; `Destroy()` releases. -- `SamplerVulkan` makes one linear, repeat, 16x anisotropic sampler at construction (`textureSampler`). The UI has `UIRenderer::RegisterSampler(const VkSamplerCreateInfo&)` and `RegisterLinearClampSampler()`. +- Samplers live in sampler slots, written from a `VkSamplerCreateInfo`. `UIRenderer::RegisterSampler(const VkSamplerCreateInfo&)` writes the sampler into every frame's sampler heap (in descriptor-set mode into every frame's set, through a `VkSampler` the heap creates and owns) and returns a `SamplerSlot`; `RegisterLinearClampSampler()` registers a linear, clamp-to-edge one. Without a `UIRenderer`, in heap mode (ray tracing), call `Device::vkWriteSamplerDescriptorsEXT` with the create-info for each frame `f`, into `heap.samplerHeap[f].value + heap.SamplerByteOffset(range.firstElement)`, `Device::descriptorHeapProperties.samplerDescriptorSize` bytes, then flush, as `examples/Sponza/main.cpp` does; in descriptor-set mode `heap.WriteSamplerSets(range.firstElement, info)` does it. The library makes no anisotropic sampler and does not enable `samplerAnisotropy`: a create-info with `anisotropyEnable` needs `.vulkan10.samplerAnisotropy` among `Device::Initialize`'s required features (or its optional ones, checked in `Device::enabledFeatures`). ### UI diff --git a/AGENTS.template.md b/AGENTS.template.md index 6fc515f..dc812ae 100644 --- a/AGENTS.template.md +++ b/AGENTS.template.md @@ -4,7 +4,7 @@ Guidance for coding agents and contributors working on Crafter.Graphics itself. ## Project overview -Crafter.Graphics is the Crafter engine's graphics library: one C++ module, `Crafter.Graphics`, with 41 partitions, built with [crafter-build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build) from `project.cpp`. On native targets it is a Vulkan 1.3+ renderer built around `VK_EXT_descriptor_heap` (bindless, no descriptor-set layouts, push data through `vkCmdPushDataEXT`), with a bindless-descriptor-set fallback for drivers without the heap, hardware ray tracing, a compute-shader UI, a Wayland or Win32 window, and input. On `wasm32-*` targets the same C++ surface runs in a browser over WebGPU through a JS bridge, with a software wavefront ray tracer. It depends on Crafter.Event (events), Crafter.Math (vectors, matrices) and Crafter.Asset (`.cmesh`, `.ctex`), all pulled from `forgejo.catcrafts.net` unpinned, and, on native targets, on the Khronos `Vulkan-Headers` and `Vulkan-Utility-Libraries` from GitHub as header-only `ExternalDependency` entries. Its consumers are the examples under `examples/` (each a `LocalProject` of `../../project.cpp`) and the maintainer's applications, which pull `master`. +Crafter.Graphics is the Crafter engine's graphics library: one C++ module, `Crafter.Graphics`, with 40 partitions, built with [crafter-build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build) from `project.cpp`. On native targets it is a Vulkan 1.3+ renderer built around `VK_EXT_descriptor_heap` (bindless, no descriptor-set layouts, push data through `vkCmdPushDataEXT`), with a bindless-descriptor-set fallback for drivers without the heap, hardware ray tracing, a compute-shader UI, a Wayland or Win32 window, and input. On `wasm32-*` targets the same C++ surface runs in a browser over WebGPU through a JS bridge, with a software wavefront ray tracer. It depends on Crafter.Event (events), Crafter.Math (vectors, matrices) and Crafter.Asset (`.cmesh`, `.ctex`), all pulled from `forgejo.catcrafts.net` unpinned, and, on native targets, on the Khronos `Vulkan-Headers` and `Vulkan-Utility-Libraries` from GitHub as header-only `ExternalDependency` entries. Its consumers are the examples under `examples/` (each a `LocalProject` of `../../project.cpp`) and the maintainer's applications, which pull `master`. | Path | What lives there | |---|---| @@ -25,7 +25,7 @@ Crafter.Graphics is the Crafter engine's graphics library: one C++ module, `Craf Partitions, by what they are for. Every partition file exists on every target; one that does not apply compiles to an empty module behind `#ifdef CRAFTER_GRAPHICS_WINDOW_DOM` (or `#ifndef`), and several hold both halves in one file (`Window`, `Device`, `UI`, `Mesh`, `RenderingElement3D`, `RTPass`, `RenderPass`, `Image2D`, `FontAtlas`, `RT`). - Both backends: `:Types` (`FrameTime`, `TouchPoint`, `CrafterKeys`, `AlignUp`), `:Keys` (`consteval Key(CrafterKeys)` to the platform's raw code), `:Window`, `:Device`, `:Input` (named actions, rebinding), `:Gamepad`, `:Clipboard`, `:Animation`, `:Font` (stb_truetype), `:FontAtlas` (the SDF atlas), `:UI` (`UIRenderer`, the item structs, `Rect`), `:UIComponents` (Tier 3), `:InputField`, `:RenderPass`, `:RTPass`, `:Mesh`, `:RenderingElement3D`, `:Image2D`, `:RT` (portable RT PODs and flag constants), `:GraphicsTypes` (the `using` aliases `GraphicsBuffer`, `GraphicsDescriptorHeap`, `GraphicsComputeShader`, `GraphicsCommandBuffer`), `:ForwardDeclarations`. -- Vulkan only: `:VulkanBuffer`, `:ImageVulkan`, `:SamplerVulkan`, `:DescriptorHeapVulkan` (heap and descriptor-set mode, the RAII slots), `:ComputeShader` (Tier 1), `:ShaderVulkan` (`VulkanShader` and the NVIDIA SPIR-V rewrite), `:ShaderBindingTableVulkan`, `:PipelineRTVulkan`, `:VulkanTransition`, `:Decompress` (`VK_EXT_memory_decompression`). +- Vulkan only: `:VulkanBuffer`, `:ImageVulkan`, `:DescriptorHeapVulkan` (heap and descriptor-set mode, the RAII slots), `:ComputeShader` (Tier 1), `:ShaderVulkan` (`VulkanShader` and the NVIDIA SPIR-V rewrite), `:ShaderBindingTableVulkan`, `:PipelineRTVulkan`, `:VulkanTransition`, `:Decompress` (`VK_EXT_memory_decompression`). - DOM only: `:WebGPU` (the `extern "C"` bridge declarations), `:WebGPUBuffer`, `:DescriptorHeapWebGPU`, `:WebGPUComputeShader`, `:PlainComputeShader`, `:ShaderBindingTableWebGPU`, `:PipelineRTWebGPU`, `:Dom`, `:DomEvents`, `:Router`. ## Dev environment diff --git a/README.md b/README.md index e6150d0..ea407ea 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,8 @@ shaded through an intersection shader with an any-hit cut-out. library's own shaders need are the library's to enable; arithmetic, storage and layout features an application's shaders use (`shaderInt16` for a `uint16_t` spec - constant, `scalarBlockLayout`, fp16, 16-bit storage) are the + constant, `scalarBlockLayout`, fp16, 16-bit storage), and + `samplerAnisotropy` for a sampler with `anisotropyEnable`, are the application's to ask for, as core feature structs: `Device::Initialize({.vulkan10 = {.shaderInt16 = VK_TRUE}})`. A second argument takes features to use where the GPU has them, and diff --git a/implementations/Crafter.Graphics-Device.cpp b/implementations/Crafter.Graphics-Device.cpp index 6818336..3b19541 100644 --- a/implementations/Crafter.Graphics-Device.cpp +++ b/implementations/Crafter.Graphics-Device.cpp @@ -1161,8 +1161,6 @@ namespace { DeviceFeatures library; // Enabled where the GPU has them, never a reason to skip it. DeviceFeatures wanted = optional; - // SamplerVulkan turns anisotropic filtering on. - library.vulkan10.samplerAnisotropy = VK_TRUE; // Every UI kernel reads and writes its storage images without a // format. From Vulkan 1.3 those capabilities need no feature: each // format says whether it allows it diff --git a/interfaces/Crafter.Graphics-SamplerVulkan.cppm b/interfaces/Crafter.Graphics-SamplerVulkan.cppm deleted file mode 100644 index d011568..0000000 --- a/interfaces/Crafter.Graphics-SamplerVulkan.cppm +++ /dev/null @@ -1,48 +0,0 @@ -//SPDX-License-Identifier: LGPL-3.0-only -//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® - -module; - -#ifndef CRAFTER_GRAPHICS_WINDOW_DOM -#include "vulkan/vulkan.h" - -#endif // !CRAFTER_GRAPHICS_WINDOW_DOM -export module Crafter.Graphics:SamplerVulkan; -#ifndef CRAFTER_GRAPHICS_WINDOW_DOM -import std; -import :VulkanBuffer; -import :ImageVulkan; - -export namespace Crafter { - template - class SamplerVulkan { - public: - VkSampler textureSampler; - VkDescriptorImageInfo imageInfo; - SamplerVulkan() { - VkSamplerCreateInfo samplerInfo{}; - samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; - samplerInfo.magFilter = VK_FILTER_LINEAR; - samplerInfo.minFilter = VK_FILTER_LINEAR; - samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; - samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; - samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; - samplerInfo.anisotropyEnable = VK_TRUE; - samplerInfo.maxAnisotropy = 16; - samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK; - samplerInfo.unnormalizedCoordinates = VK_FALSE; - samplerInfo.compareEnable = VK_FALSE; - samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS; - samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; - samplerInfo.mipLodBias = 0.0f; - samplerInfo.minLod = 0.0f; - samplerInfo.maxLod = VK_LOD_CLAMP_NONE; - Device::CheckVkResult(vkCreateSampler(Device::device, &samplerInfo, nullptr, &textureSampler)); - - imageInfo = { - .sampler = textureSampler - }; - } - }; -} -#endif // !CRAFTER_GRAPHICS_WINDOW_DOM diff --git a/interfaces/Crafter.Graphics.cppm b/interfaces/Crafter.Graphics.cppm index 6b359f7..7489826 100644 --- a/interfaces/Crafter.Graphics.cppm +++ b/interfaces/Crafter.Graphics.cppm @@ -31,7 +31,6 @@ export import :PipelineRTVulkan; export import :RenderingElement3D; export import :ImageVulkan; export import :Image2D; -export import :SamplerVulkan; export import :DescriptorHeapVulkan; export import :RenderPass; export import :RTPass; diff --git a/project.cpp b/project.cpp index ec2d633..c2d78b0 100644 --- a/project.cpp +++ b/project.cpp @@ -229,7 +229,7 @@ extern "C" Configuration CrafterBuildProject(std::span a // when its body is gated out. Vulkan-typed partitions stub to empty // modules under CRAFTER_GRAPHICS_WINDOW_DOM; the Dom/DomEvents/Router // partitions stub to empty modules in the opposite direction. - std::array ifaces = { + std::array ifaces = { "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-Clipboard", @@ -259,7 +259,6 @@ extern "C" Configuration CrafterBuildProject(std::span a "interfaces/Crafter.Graphics-Router", "interfaces/Crafter.Graphics-RT", "interfaces/Crafter.Graphics-RTPass", - "interfaces/Crafter.Graphics-SamplerVulkan", "interfaces/Crafter.Graphics-ShaderBindingTableVulkan", "interfaces/Crafter.Graphics-ShaderBindingTableWebGPU", "interfaces/Crafter.Graphics-ShaderVulkan", diff --git a/tests/FeatureRequests/main.cpp b/tests/FeatureRequests/main.cpp index 4d9f142..49b7638 100644 --- a/tests/FeatureRequests/main.cpp +++ b/tests/FeatureRequests/main.cpp @@ -3,7 +3,8 @@ // Device::Initialize's feature requests on a real GPU, end to end: a required // feature ends up enabled, an optional one exactly where the GPU supports it, -// and one nobody asked for stays off. On the Vulkan 1.3 path +// and one nobody asked for stays off, samplerAnisotropy among them, which the +// library required for itself until #168. On the Vulkan 1.3 path // (CRAFTER_GRAPHICS_NO_VULKAN_1_4, run in a child process, since an instance's // version is fixed for the process) maintenance5 comes from // VK_KHR_maintenance5 and no other vulkan14 feature can be had. And a refusal @@ -61,6 +62,11 @@ void Run(bool vulkan13) { std::format("{}an optional feature is enabled exactly where the GPU supports it", path)); Check(!enabled.vulkan10.geometryShader && !enabled.vulkan12.shaderInt8, std::format("{}a feature nobody asked for stays off", path)); + // The library asked for this one itself until #168, for a sampler class + // that nothing in this repository made. None of its samplers is + // anisotropic, so the feature is the application's to ask for. + Check(!enabled.vulkan10.samplerAnisotropy, + std::format("{}samplerAnisotropy, which none of the library's samplers uses, stays off when the application does not ask for it", path)); Check(enabled.vulkan12.bufferDeviceAddress && enabled.vulkan14.maintenance5, std::format("{}the library's own needs are on, maintenance5 among them", path)); if (vulkan13) {