Add VertexBuffer abstraction with typed streams and per-backend support#1087
Add VertexBuffer abstraction with typed streams and per-backend support#1087EmilioLaiso wants to merge 9 commits intollvm:mainfrom
VertexBuffer abstraction with typed streams and per-backend support#1087Conversation
| struct VertexBuffer { | ||
| VertexBufferDesc Desc; | ||
| std::shared_ptr<Buffer> Data; | ||
|
|
||
| uint32_t getVertexCount() const { | ||
| uint32_t Stride = Desc.getStride(); | ||
| if (Stride == 0) | ||
| return 0; | ||
| return static_cast<uint32_t>(Data->getSizeInBytes()) / Stride; | ||
| } | ||
| }; |
There was a problem hiding this comment.
I don't think this is the right abstraction.
The layout is part of the pipelinelayout/inputlayout which is part of the PipelineState.
A vertex buffer should just be Buffer type with the VertexBuffer usage.
There was a problem hiding this comment.
In the future we want to build RT acceleration structures, right?
It might be useful to have this information at hand when filling BLAS descriptions as you'll need the vertex format and stride.
There was a problem hiding this comment.
For acceleration structures we don't need the semantics name, just offset and stride for the positions. I would first like to see how this fits into my PR #1070 as this creates an interface for specifying the input layout. After the semantics have been specified we don't need them anymore to bind the buffer.
Summary
This PR introduces a dedicated
VertexBufferabstraction into the offload test suite, replacing the previous approach where vertex data was defined as a rawCPUBufferentry underBuffers:and manually described viaVertexAttributes:inBindings:.New YAML schema:
VertexBuffersA new
VertexBuffers:section is added to test pipeline definitions.Each vertex buffer declares named streams with a typed
Formatand inlineData:This replaces the old pattern where vertex data lived in
Buffers:and layout was specified separately inBindings: > VertexAttributes:.Channels,OffsetsandStrideare now inferred.API-level
VertexBuffertypeVertexBuffer/VertexBufferDesc/VertexStream(include/API/VertexBuffer.h) -- a new value type that pairs a GPUBufferwith a layout descriptor (VertexBufferDesc). The descriptor holds an ordered list ofVertexStreamentries (semantic name +Format), and provides helpers for computing stride, per-stream byte offsets, and vertex count.ParsedVertexBuffer/VertexStreamData(include/Support/Pipeline.h) -- YAML-side parsed representation. Stream values are stored asdoubleand then interleaved + type-converted during parsing into a singleInterleavedDatabuffer ready for GPU upload.createVertexBuffer(Device&, ParsedVertexBuffer&)(lib/API/Device.cpp) -- API-agnostic factory that allocates aCpuToGpubuffer withVertexBufferusage and builds theVertexBufferDesc.BufferUsageenum and per-backend changesA
BufferUsageenum (Storage/VertexBuffer) is added toBufferCreateDescso backends can set the correct usage flags:lib/API/VK/Device.cpp): setsVK_BUFFER_USAGE_VERTEX_BUFFER_BITfor vertex buffers; eliminates the old host-staging + device-copy pair in favor of a singleCpuToGpumapped buffer (with a TODO to revisit for discrete GPUs).lib/API/DX/Device.cpp): usesD3D12_HEAP_TYPE_UPLOADforCpuToGpuvertex buffers; buildsD3D12_INPUT_ELEMENT_DESCfromVertexBufferDescstreams instead ofVertexAttribute.lib/API/MTL/MTLDevice.cpp): buildsMTL::VertexDescriptorfromVertexBufferDescstreams; copies interleaved data into a managed Metal buffer.All three backends now use
VertexBuffer::getVertexCount()for draw calls and derive input layout / vertex descriptors fromVertexBufferDescrather than the removedVertexAttributestruct.Buffer::getSizeInBytes()A new pure virtual
getSizeInBytes()is added toBuffer, implemented byDXBuffer,VulkanBuffer, andMTLBuffer. This is used byVertexBuffer::getVertexCount()to derive the vertex count from buffer size and stride.Resources.hhelpersgetComponentCount(Format)-- returns the number of components per element for a format.Pipeline parser changes (
lib/Support/Pipeline.cpp)VertexBuffers:section withVertexStreamDatamapping (Name, Format, Data).isVertexCompatible), data-size-to-component-count alignment, and consistent vertex count across streams.InterleavedData) with format-specific type conversion viawriteComponent().Bindings::VertexBufferagainstPipeline::VertexBuffersinstead ofPipeline::Buffers.VertexAttributemapping and theVertexAttributesbinding key.ScalarEnumerationTraits<Format>so that the newFormatenum can be used directly in YAML fields.Test migrations
All 22 graphics test files are migrated from the old schema to the new
VertexBuffers:+Streams:format.