Skip to content

Vulkan: implement BufferManager read - #40

Merged
resetius merged 1 commit into
masterfrom
vk-buffer-read
Nov 19, 2026
Merged

resetius merged 1 commit into
masterfrom
vk-buffer-read

Conversation

@resetius

@resetius resetius commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Problem

The Vulkan BufferManager had no read in its vtable. buffer_read called a NULL pointer on Vulkan: particles3 with debug=1 (maybe_debug_output) and the poisson test crashed.

Change

read for Vulkan (vulkan/buffer.c):

  • Host-visible memory (MEMORY_DYNAMIC buffers, or a device-local type that is also host-visible): record a barrier ALL_COMMANDS / MEMORY_WRITE -> HOST / HOST_READ, then map the memory. If the memory type is not HOST_COHERENT, call vkInvalidateMappedMemoryRanges before memcpy. The whole allocation is mapped and invalidated, so the range is always aligned to nonCoherentAtomSize.
  • Other memory (device-local): create a temporary HOST_VISIBLE | HOST_COHERENT staging buffer, record ALL_COMMANDS / MEMORY_WRITE -> TRANSFER / TRANSFER_READ, vkCmdCopyBuffer, then TRANSFER / TRANSFER_WRITE -> HOST / HOST_READ. Device-local buffers now also get TRANSFER_SRC usage for this copy.
  • Uniform buffers with one copy per swapchain image use image_index, like update.

The memory type is known now: create_buffer returns the property flags of the chosen memory type (new nullable out parameter), and BufferImpl stores them in memory_flags.

Synchronisation

All callers read a buffer right after pl_start_compute in the same frame, and they expect the result of those dispatches (OpenGL glGetBufferSubData gives it). In Vulkan these dispatches are only recorded in compute_buffer and are submitted in draw_end. So read records its barriers and copy at the end of compute_buffer and calls the new vk_compute_flush (vulkan/render.c):

  1. end compute_buffer and submit it to the compute queue with a new fence;
  2. the submit waits on graphics_semaphore (if it is signaled) at ALL_COMMANDS, because the recorded copy may read what the previous graphics submit wrote; after that graphics_semaphore_signaled = 0, so the compute submit in draw_end does not wait on the same signal again;
  3. it does not signal the frame's compute_semaphore: the last compute submit in draw_end still signals it once, and the graphics submit waits for it as before;
  4. wait for the fence, destroy it, reset the frame's compute pool and begin compute_buffer again for the rest of the frame. start_compute binds the pipeline and descriptor set on every call, so no state is lost.

read outside draw_begin/draw_end (no compute buffer is recording then) stops with a clear error. There are no such callers now. RenderImpl.in_frame tracks this.

Side effect in debug mode: the compute timestamp counter that spans a read also includes the host time of the read.

Testing

  • Build as in CI (cmake -B build -G Ninja && cmake --build build) on macOS, base 6b87835 and this branch, both clean builds: 5 warnings each, the same list (GLSL #extension and one in contrib/astc-codec). GCC 16 -Wall -fsyntax-only on the changed files: no warnings before and after.
  • ctest: test_base64 and test_config pass; test_gltf fails in the same way on base, because the assets/khr submodule is not checked out locally (CI checks it out).
  • The Vulkan demos were not run: a run with VK_LAYER_KHRONOS_validation needs the owner's agreement. Proposed check (MoltenVK, from the repository root after the build):
export DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation
mkdir -p /tmp/p3-debug && cd /tmp/p3-debug
gtimeout 15 <repo>/build/main particles3 --render:api=vulkan --particles3:debug=1 > p3.log 2>&1
grep -c -E "Validation (Error|Warning)" p3.log; ls density.*.txt | wc -l
gtimeout 60 <repo>/build/main poisson --render:api=vulkan > poisson.log 2>&1; grep -E "err =|Validation" poisson.log

Expected: no validation messages in both logs; particles3 keeps drawing for 15 s and writes one density.NNNNNN.txt per frame (about 0.4 MB each for nn=32, non-zero values); poisson exits by itself after the first frame and prints err = ... much smaller than 1 (err = 1 would mean that the read returned zeros). About 30 seconds in total.

The Vulkan buffer manager had no read, so buffer_read crashed on a NULL
call (particles3 with debug=1, poisson). read now records a barrier
(and, for device-local memory, a copy to a host-visible staging buffer)
into the frame's compute command buffer, submits the compute work
recorded so far, waits for it with a fence and maps the memory,
invalidating it when it is not host-coherent. Like glGetBufferSubData,
the result includes the dispatches recorded earlier in the frame.
@github-actions

Copy link
Copy Markdown

Test Results

9 tests  ±0   9 ✅ ±0   0s ⏱️ ±0s
3 suites ±0   0 💤 ±0 
3 files   ±0   0 ❌ ±0 

Results for commit 5822344. ± Comparison against base commit 6b87835.

@resetius
resetius merged commit 1682d1b into master Nov 19, 2026
2 checks passed
@resetius
resetius deleted the vk-buffer-read branch November 19, 2026 19:10
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.

1 participant