Drawing
Vertex pulling and mega-buffers
Normally the GPU's fixed-function hardware reads vertices from vertex buffers and hands them to the vertex shader. With vertex pulling, the vertex shader gets only a number, and reads the vertex from memory itself. Together with one big buffer for all meshes, a mega-buffer, this removes all per-mesh bindings, which GPU-driven rendering needs. This page explains both. The last part shows how RASM does it.
In short
- The classic path: the pipeline describes the vertex layout, the CPU binds vertex buffers, and fixed-function hardware fetches and converts each vertex.
- Vertex pulling: the pipeline has no vertex input. The shader gets the vertex index and reads a struct from a storage buffer.
- The index buffer stays. It still decides which vertex each corner uses, and lets the GPU reuse shaded vertices.
- A mega-buffer holds every mesh. A mesh is a range in it; a draw finds it with firstIndex and vertexOffset.
- The shader decides the vertex format, so any packing or compression is possible, decoded in a few instructions.
- Speed is about the same on most desktop GPUs. The real gain is flexibility, and no bindings between draws.
1How the GPU usually gets its vertices
A vertex is a small record: a position, a normal, texture coordinates, maybe more. A mesh stores its vertices in a vertex buffer. In the classic path, the application tells the pipeline what one vertex looks like, with a vertex input layout: how many bytes per vertex (the stride), and for each attribute, its format and its byte offset. Then, before each draw, it binds the vertex buffer.
The fetch hardware also converts. An attribute stored as 4 bytes of 0 to 255 can arrive in the shader as a float4 from 0 to 1. The shader does not know how the data was stored.
What the classic path costs you
The layout is part of the pipeline
Meshes with different vertex formats need different pipelines, even with the same shaders. Each one splits a draw list (see runs).
Vertex buffers are bound per draw
Each mesh in its own buffer means a bind between draws. One indirect call over many meshes cannot do that.
Only listed formats
The hardware converts a fixed set of formats. Custom packing, such as octahedral normals, needs shader code anyway.
Limits on attributes and bindings
A pipeline has a maximum number of attributes and buffer bindings.
2Vertex pulling: the shader reads its own vertex
The idea is simple. Give the pipeline an empty vertex input layout. The vertex shader still runs once per vertex, and it still gets one built-in input: the vertex index, the number of the vertex it must produce. It uses that number to read the vertex from a storage buffer, like any other array.
In Slang or HLSL, a minimal vertex shader looks like this:
struct Vertex
{
float3 position;
float3 normal;
float2 uv;
};
StructuredBuffer<Vertex> vertices;
float4 vertexMain(uint vertexIndex : SV_VertexID) : SV_Position
{
Vertex v = vertices[vertexIndex];
return mul(viewProjection, float4(v.position, 1.0));
}
The struct is now a contract between the code that writes the buffer and the shader that reads it. Both sides must agree on every byte: see struct layout rules. The buffer can be a descriptor binding, as here, or a raw GPU address (see buffer device addresses).
3The index buffer still does its job
Vertex pulling replaces the reading of vertex data. It does not replace the index buffer. Indexed drawing works as before: the hardware reads one index per triangle corner, and that index becomes the vertex index the shader receives.
This matters for speed. A vertex is usually shared by several triangles. The GPU keeps the results of recent vertex shader runs in a small cache, keyed by the index. When the same index comes again soon, it reuses the result instead of running the shader again. With pulling, the cache still works, because the hardware still sees the index.
Which number the shader receives
Indexed draws add vertexOffset (also called base vertex) to every index value. Whether the vertex-index built-in includes it depends on the API, and on how the shading language maps its built-in. Check before you index a buffer with it:
| API, built-in | Value for an indexed draw |
|---|---|
| Vulkan, VertexIndex | index value + vertexOffset |
| Direct3D, SV_VertexID | index value only: the base vertex is not added |
| A shading language that targets both | picks one meaning, and may subtract the base vertex on Vulkan to match Direct3D |
Non-indexed draws are simpler: the vertex index counts from firstVertex upward, one per vertex.
4One buffer for every mesh
Vertex pulling removes the vertex layout from the pipeline. The second half of the job is to remove the bind between draws. The usual answer is a mega-buffer: one big vertex buffer and one big index buffer for all meshes. Each mesh is a range in each of them. The buffers are bound once, and every draw finds its mesh with two numbers from its draw record.
Local or global indices
Mesh B's indices can be stored in two ways. Local is the usual choice.
Local indices + vertexOffset
Stored as the mesh was made, 0 to 3, and the draw adds vertexOffset = 24.
- Moving a mesh in the buffer: change one number in the table.
- Index size: a mesh under 65,536 vertices can use 16-bit indices, wherever it sits in the buffer.
- Uploading: copy the mesh's indices as they are.
Global indices
Already moved, 24 to 27, with vertexOffset = 0.
- Moving a mesh in the buffer: rewrite every index.
- Index size: 32-bit as soon as the buffer passes 65,536 vertices.
- Uploading: add the offset to each index first.
Sharing out the space
A mega-buffer is created once, with a fixed size. Giving a mesh a range of it is sub-allocation, the same problem as a memory allocator:
Bump (linear)
Each mesh goes right after the last one. Nothing is freed.
Good for: content loaded once: a level, a model viewer.
Problem: the buffer only fills up.
Free list
Freed ranges go into a list; a new mesh takes the first range that fits.
Good for: meshes that come and go.
Problem: holes: the free space splits into pieces too small to use (fragmentation).
Buddy or TLSF allocators
Ranges in size classes, merged with their neighbours when freed.
Good for: streaming worlds, map tiles.
Problem: more code. Some space is lost to rounding.
Defragment
Move live meshes together on the GPU, then fix their table rows.
Good for: long sessions.
Problem: every draw record and table row that names a moved range must change in the same frame.
Freeing a range is also a timing problem: the GPU may still be drawing from it in a frame that is in flight. See frames in flight.
5Worked example: one vertex of mesh B
Use the buffers from the picture above, with vertices of 64 bytes. The GPU is drawing mesh B, and it reaches the 6th corner (corner 5).
1. The index
The hardware reads the index. The shader is not involved yet.
2. The vertex index
The local index becomes a place in the shared buffer.
3. The address
An array read: element 27 of an array of 64-byte structs.
4. The shader has its vertex
From here on, the shader is an ordinary vertex shader.
Try it. The picture below shows the part of both buffers around mesh B, and does steps 1 to 3 for the corner you pick.
6One interleaved struct, or separate streams
With pulling, the shader decides how vertices are laid out in memory. There are two basic choices:
7Smaller vertices
Vertex data is read for every vertex of every draw. Smaller vertices mean less memory, and less memory traffic each frame. With pulling, the shader can decode any packing it likes, so engines often store far less than full 32-bit floats:
| Attribute | Full | Packed | How it is decoded |
|---|---|---|---|
| Position | 3 × float32 = 12 B | 3 × 16-bit + 2 B padding = 8 B | Quantized inside the mesh's bounding box: p = min + q / 65535 × (max − min). The box is stored once per mesh. |
| Normal | 3 × float32 = 12 B | 2 × 16-bit = 4 B | Octahedral encoding: the unit sphere is folded onto a square, so 2 numbers are enough. |
| Tangent + sign | 4 × float32 = 16 B | 2 × 8-bit + sign, 4 B | Octahedral too, with the bitangent sign in a spare byte. |
| Texture coordinate | 2 × float32 = 8 B | 2 × 16-bit = 4 B | Unsigned normalized 16-bit for UVs in 0 to 1; half floats for UVs that repeat. |
Example: 64 bytes down to 20
A 16-bit half float has 10 bits of fraction. Between 0.5 and 1.0, its step is 2−11 ≈ 0.00049. On a 4096-texel texture, that is 4096 × 0.00049 = 2 texels: visible as wobbly, swimming textures. Unsigned normalized 16-bit has the same step everywhere, 1 / 65,535, which is 0.06 texels on the same texture.
8Performance notes
Alignment
A struct whose size is a multiple of 16 bytes lets the compiler use wide 16-byte loads. A 12-byte float3 at an odd offset may turn into several small loads.
Cache lines
Memory moves in lines of 64 or 128 bytes. A 64-byte vertex that starts on a line boundary costs one line; a 20-byte vertex costs a third of one.
Index order
Reorder the triangles so that shared vertices come close together. The vertex cache then hits more often (Forsyth's algorithm, meshoptimizer).
Vertex order
Reorder the vertices into the order the indices first use them. Reads then walk the buffer forward, line by line.
On many desktop GPUs, vertex fetch already runs as ordinary shader loads, so pulling costs about the same. Some GPUs, often mobile ones, still have fast fixed-function fetch. Measure before you depend on either.
A wrong vertex index in a pulled read is just a read of the wrong memory: another mesh, or past the end of the buffer. Debug builds can check the index against the mesh's range.
9In RASM
RASM uses vertex pulling for every graphics pipeline: no pipeline has a vertex input layout. All meshes live in one geometry pool: one vertex buffer, one index buffer, one color buffer for the meshes with vertex colors, one skin buffer for the skinned meshes' joints and weights, one morph buffer for the morph targets' deltas, and one table of sub-mesh rows, created at startup and sized by EngineConfig.
EngineConfig defaults. The pool never frees anything: each upload appends after the last one.One cache line per vertex, and optional streams
The vertex is exactly 64 bytes: one cache line. A vertex that starts on a line ends on it. Adding one more attribute to every vertex, even 4 bytes, would make every vertex span two lines (a 68-byte vertex always holds a line boundary), and every mesh would pay for it, though most glTF files have no vertex colors. So an attribute that only some meshes have is a stream of its own (section 6), and the sub-mesh's row says where its part starts.
Color in the vertex
4 more bytes for every vertex of every mesh, and two cache lines for every vertex.
A color stream
The vertex stays one line. The shader reads a color only when the row's colorOffset is not -1, a branch that is the same for the whole draw.
A color is packed by packVertexColor: R in the low byte, RGB sRGB-encoded so 8 bits keep the dark shades, alpha linear. getVertexColor decodes it to linear in vertMain, before interpolation. The application gives colors to createMesh(vertices, indices, colors); glTF import reads COLOR_0. A skinned glTF mesh's JOINTS_0 / WEIGHTS_0 are a second stream of the same kind: a SkinVertex of 16 bytes (4 joints as 16-bit integers, 4 weights as UNORM16 that add up to 65535), at the row's skinOffset. Only the Skin pass reads it; the views read the posed vertices it writes (skinning). Morph targets are a third stream: a MorphDelta of 24 bytes per vertex and target (the position as floats, the normal and tangent as half floats), at the row's morphOffset, with morphTargetCount targets one after another. The Skin pass reads it too (morph targets).
Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Vertex input layout | empty | Empty for every graphics pipeline (VulkanDevice::createGraphicsPipeline). Core Vulkan, so it also runs where dynamic vertex input is missing, such as MoltenVK. |
| How the shader reaches the buffer | a descriptor or an address | A raw address: SceneData::vertsPtr, a VertexFull pointer. No descriptor. See buffer device addresses. |
| The vertex index | depends on the API and language | Slang lowers SV_VertexID to VertexIndex − BaseVertex, as in Direct3D. So vertMain gets the local index and adds draw->vertexOffset itself. The hardware adds the offset, Slang removes it, the shader adds it back. |
| Index buffer | kept | Kept. One index buffer for all meshes, bound once per scene pass (Renderer::drawSceneView) with bindIndexBuffer, format U32_UINT. The UI pass pulls too, from buffers of its own: Dear ImGui's 20-byte vertices through UiPushConstants::vertices, with U16_UINT indices (shaders/passes/ui.slang). |
| Indices | local + vertexOffset | Local to the sub-mesh. vertexOffset = firstVertex, set in rebuildDrawList. Always 32-bit, even for small meshes. |
| Sub-allocation | bump, free list, buddy… | A free list per stream: GeometryPool::allocate takes the first free range that fits (RangeAllocator), destroyMesh gives the ranges back after the frames in flight (deferred destruction). A mesh that does not fit is not uploaded, and the error names the largest free ranges. |
| Layout | interleaved or streams | One interleaved 64-byte struct: pos, normal, uv[3], tangent. The same bytes as the public Vertex, checked by static_asserts. Vertex colors, skin joints and weights, and morph target deltas are optional streams next to it (above). VertexPosOnly is declared but not used: there is no position-only stream. |
| Compression | 16-bit, octahedral… | None on the GPU. Full 32-bit floats, and all 3 UV sets are always stored (unused ones are zero). A quantized glTF (KHR_mesh_quantization: positions, normals, tangents and UVs in 8- or 16-bit integers) is decoded to floats on import; the node transform and the texture transform undo the quantization's scale, as the extension intends. |
| Index and vertex order | optimize both | Only the coarser LODs go through meshopt_optimizeVertexCache. LOD 0 keeps the order of the import. No vertex fetch reordering. |
| LODs | — | All LODs of a sub-mesh share its vertices and cost only their indices, placed right after LOD 0's. Culling writes the chosen LOD's range into the draw. See mesh simplification and LOD selection. |
| Upload | — | Once, when the mesh finishes loading, with fillBuffer at the range's offset. The CPU copy is dropped afterwards (P6). See GPU memory and uploads. |
Where it lives in code
| What | Where |
|---|---|
| The pool and its buffers | GeometryPool::create, GeometryPool::upload, GeometryPool::fits in src/renderer/geometryPool.cpp |
| Pool sizes | geometryPoolVertices, geometryPoolIndices, geometryPoolColors, geometryPoolSkins, geometryPoolMorphs, geometryPoolSubMeshes in EngineConfig, include/rasm/types.h |
| Skin joints and weights | SkinVertex in include/rasm/mesh.h; readSkinVertices in src/assets/skinImport.cpp; read by skinMain in shaders/passes/skin.slang |
| Morph target deltas | MorphDelta in include/rasm/mesh.h; readMorphTargets, packMorphDelta in src/assets/morphImport.cpp; read by skinMain |
| Vertex colors | packVertexColor in src/assets/mesh.cpp; ObjRaw::colors; getVertexColor in shaders/lib/core.slang |
| The vertex struct | Vertex in include/rasm/mesh.h; gfx::VertexFull in src/renderer/uberMaterialTypes.h; VertexFull in shaders/lib/geometryTypes.slang |
| A sub-mesh's ranges | MeshGeometry in include/rasm/mesh.h; GeometryData, GeometryLod in shaders/lib/geometryTypes.slang |
| The empty vertex input | VulkanDevice::createGraphicsPipeline in src/rhi/vulkan/vulkanDevice.cpp |
| The pull | vertMain in shaders/passes/rasm.slang, through getVertex in shaders/lib/core.slang |
Binding the index buffer, setting vertsPtr | Renderer::drawSceneView, Renderer::prepareSceneView in src/renderer/gpuScene.cpp |
| LOD indices | buildMeshLods in src/assets/meshLod.cpp |
Vertex,gfx::VertexFulland the SlangVertexFullhave the same 64 bytes. A change to one changes all three and thestatic_asserts (see struct layout).vertMainaddsdraw->vertexOffsettoSV_VertexID. Removing it makes every mesh read the first mesh's vertices.- Every vertex shader of a scene draw reads through
getVertex, so a skinned or morphed sub-mesh is drawn posed in every view. - Indices are local to their sub-mesh. Every LOD of a sub-mesh uses the same
vertexOffset, and its colors are indexed by the same local index. - Keep the vertex at 64 bytes. An attribute that not every mesh has is a stream with an offset in
GeometryData, not a field inVertex. - The index buffer is bound as
U32_UINT. 16-bit indices would need a second index buffer and their own draws.
- A pool fragments, and nothing moves live ranges to close the holes.
- No vertex compression and no position-only stream.
Further reading
- Daniel Rákos, Programmable Vertex Pulling, in OpenGL Insights (2012).
- Zina Cigolle, Sam Donow, Daniel Evangelakos, Michael Mara, Morgan McGuire and Quirin Meyer, A Survey of Efficient Representations for Independent Unit Vectors, Journal of Computer Graphics Techniques (2014): octahedral encoding.
- Tom Forsyth, Linear-Speed Vertex Cache Optimisation (2006).
- Arseny Kapoulkine, meshoptimizer: the library and its documentation on vertex cache, fetch and quantization.