fix(device): add preferred mask + required-only fallback to GetMemoryType (#59)
GetMemoryType was a bare first-superset match that threw whenever no memory type satisfied the requested property flags. Callers combining a mandatory flag with a perf-only one (HOST_VISIBLE | DEVICE_LOCAL for the descriptor heaps) would then fail allocation on any device without a host-visible device-local heap (no resizable BAR). It now takes a `preferred` mask distinct from `required`: a type satisfying both is chosen first, falling back to a required-only match when the preference is unavailable, and throwing only when even `required` cannot be met. VulkanBuffer::Create/Resize gain an optional trailing `preferredPropertyFlags`, and the descriptor heaps now treat DEVICE_LOCAL as a preference on top of mandatory HOST_VISIBLE. This does not touch any flush/barrier behaviour — coherency is not assumed anywhere here. Adds tests/MemoryTypeFallback, a pure-CPU test that installs synthetic memory layouts and exercises selection, preference, fallback, and the unsatisfiable-required throw. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
0b00d9d680
commit
294c1378b5
6 changed files with 218 additions and 13 deletions
|
|
@ -83,9 +83,14 @@ export namespace Crafter {
|
|||
samplerFreelist.clear();
|
||||
samplerFreelist.reserve(samplers);
|
||||
|
||||
// The heaps are written by the CPU every frame (HOST_VISIBLE is
|
||||
// mandatory) and read by the GPU, so DEVICE_LOCAL is a perf
|
||||
// preference, not a requirement: devices without a host-visible
|
||||
// device-local heap (no resizable BAR) get a host-visible-only
|
||||
// allocation instead of a hard allocation failure.
|
||||
for(std::uint8_t i = 0; i < Window::numFrames; i++) {
|
||||
resourceHeap[i].Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, resourceSize);
|
||||
samplerHeap[i].Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, samplerSize);
|
||||
resourceHeap[i].Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, resourceSize, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
samplerHeap[i].Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, samplerSize, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -199,7 +199,12 @@ export namespace Crafter {
|
|||
// ComputeShader read the offset off the pipeline they record.
|
||||
|
||||
static void CheckVkResult(VkResult result);
|
||||
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags properties);
|
||||
// Selects a memory type index from typeBits that satisfies `required`.
|
||||
// When `preferred` bits are also given, a type satisfying both is
|
||||
// chosen first; if none exists we fall back to required-only rather
|
||||
// than throwing. Throws only when even `required` cannot be met (no
|
||||
// valid memory exists for the allocation).
|
||||
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags required, VkMemoryPropertyFlags preferred = 0);
|
||||
|
||||
// ─── Wayland key repeat ────────────────────────────────────────
|
||||
// TickKeyRepeats fires onRawKeyDown / onRawKeyHold / onTextInput on
|
||||
|
|
|
|||
|
|
@ -55,7 +55,12 @@ namespace Crafter {
|
|||
class VulkanBuffer : public VulkanBufferBase, public VulkanBufferMappedConditional<T, Mapped> {
|
||||
public:
|
||||
VulkanBuffer() = default;
|
||||
void Create(VkBufferUsageFlags2 usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count) {
|
||||
// `preferredPropertyFlags` are best-effort: the allocation prefers a
|
||||
// memory type satisfying them on top of `memoryPropertyFlags`, but
|
||||
// falls back to the required flags alone rather than failing. Use it
|
||||
// for perf hints (e.g. DEVICE_LOCAL on a mapped buffer) that aren't
|
||||
// available on every device — see Device::GetMemoryType.
|
||||
void Create(VkBufferUsageFlags2 usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count, VkMemoryPropertyFlags preferredPropertyFlags = 0) {
|
||||
size = count * sizeof(T);
|
||||
|
||||
// Carry usage in the maintenance5 flags2 chain so 64-bit bits
|
||||
|
|
@ -77,7 +82,7 @@ namespace Crafter {
|
|||
VkMemoryAllocateInfo memAlloc {
|
||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||
.allocationSize = memReqs.size,
|
||||
.memoryTypeIndex = Device::GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags)
|
||||
.memoryTypeIndex = Device::GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags, preferredPropertyFlags)
|
||||
};
|
||||
|
||||
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo {
|
||||
|
|
@ -109,11 +114,11 @@ namespace Crafter {
|
|||
buffer = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
void Resize(VkBufferUsageFlags2 usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count) {
|
||||
void Resize(VkBufferUsageFlags2 usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count, VkMemoryPropertyFlags preferredPropertyFlags = 0) {
|
||||
if(buffer != VK_NULL_HANDLE) {
|
||||
Clear();
|
||||
}
|
||||
Create(usageFlags, memoryPropertyFlags, count);
|
||||
Create(usageFlags, memoryPropertyFlags, count, preferredPropertyFlags);
|
||||
}
|
||||
|
||||
void Copy(VkCommandBuffer cmd, VulkanBuffer& dst) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue