Skip to content

Rewrite Buffer CPU-mapping API to lift vertex buffer creation into the common API layer#1113

Merged
manon-traverse merged 4 commits intollvm:mainfrom
Traverse-Research:render-backend-api-vertex-buffer
May 5, 2026
Merged

Rewrite Buffer CPU-mapping API to lift vertex buffer creation into the common API layer#1113
manon-traverse merged 4 commits intollvm:mainfrom
Traverse-Research:render-backend-api-vertex-buffer

Conversation

@EmilioLaiso
Copy link
Copy Markdown
Contributor

Lift vertex buffer creation into the common API layer and unifies it across the DX, Vulkan, and Metal backends.

Buffer API

  • Add pure-virtual getSizeInBytes(), map(), unmap() to Buffer.
  • Add BufferUsage { Storage, VertexBuffer } on BufferCreateDesc.
  • Add createVertexBufferFromCPUBuffer(Device&, const CPUBuffer&)

DirectX

  • Tighten the UAV flag logic: only HEAP_TYPE_DEFAULT gets ALLOW_UNORDERED_ACCESS

Comment thread include/API/Buffer.h
Comment on lines +40 to +44
// Maps the buffer's memory for host access. Only valid for CpuToGpu and
// GpuToCpu buffers; returns an error for GpuOnly. Each successful map() must
// be paired with a call to unmap().
virtual llvm::Expected<void *> map() = 0;
virtual llvm::Error unmap() = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fine change for now, as the framework already maps and unmaps buffers. However, the modern APIs allow for persistently mapped resources and I think we should embrace those as well. Although I think that's better for a follow-up PR once the abstraction layer is in a more complete state.

@EmilioLaiso EmilioLaiso changed the title ⬆️ Lift vertex buffer creation into the common API layer Lift vertex buffer creation into the common API layer Apr 23, 2026
@EmilioLaiso EmilioLaiso force-pushed the render-backend-api-vertex-buffer branch from 9090b1b to a4c884c Compare April 30, 2026 12:53
Copy link
Copy Markdown
Collaborator

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan -at all- of the CpuToGpu and related enums with UMA drastically increasing in popularity, but that's out of context for this PR even if affecting it significantly.

Secondly I don't think the title does this PR justice, you're factoring out the (somewhat unsafe) (un)map() APIs to be able to copy in and out of buffers on the CPU, and only as a small consequence of that are able to able to generalize vertex buffer creation which maps the buffer to write into it. This PR doesn't seem to update functions like readBackData() to use the new mapping API (and demonstrate its usefulness)?

Comment thread include/API/Buffer.h Outdated
Comment thread lib/API/VK/Device.cpp Outdated
Comment thread lib/API/MTL/MTLDevice.cpp
Comment on lines +147 to +151
// Managed storage (CpuToGpu) requires an explicit didModifyRange to
// propagate CPU-side writes to the GPU. Shared storage (GpuToCpu) is
// coherent and needs no action.
if (Desc.Location == MemoryLocation::CpuToGpu)
Buf->didModifyRange(NS::Range::Make(0, SizeInBytes));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is painful, it means CpuToGpu is not host coherent on Metal but it is mapped as such on Vulkan.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also curious if Metal forces/resets this managed mode request back to shared, because that's not available outside of Intel-based Macs: https://developer.apple.com/documentation/metal/mtlstoragemode/managed?language=objc#discussion

@MarijnS95 MarijnS95 changed the title Lift vertex buffer creation into the common API layer Rewrite Buffer CPU-mapping API to lift vertex buffer creation into the common API layer May 4, 2026
Add `VertexBuffers` parsing to pipeline yaml

use new parsed vtx buffers in backends

rebase fixups

fixup vertex buffer definition in new sampletexture2d test

migrate new tests to new VB yaml definition

createVertexBuffer in api-agnostic device

rebase fixup: `getFormatSize` -> `getFormatSizeInBytes` rename

fmt

const

remove VertexBuffer abstraction. Rely on ParsedVertexBuffer for input layout and buffer metadata

revert test VB definition changes
@EmilioLaiso EmilioLaiso force-pushed the render-backend-api-vertex-buffer branch from ae341ed to d620b0c Compare May 4, 2026 16:10
@manon-traverse manon-traverse merged commit 60b9450 into llvm:main May 5, 2026
21 of 22 checks passed
@MarijnS95 MarijnS95 deleted the render-backend-api-vertex-buffer branch May 5, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants