Repository navigation
Vulkan: implement BufferManager read - #40
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Vulkan
BufferManagerhad noreadin its vtable.buffer_readcalled a NULL pointer on Vulkan:particles3withdebug=1(maybe_debug_output) and thepoissontest crashed.Change
readfor Vulkan (vulkan/buffer.c):MEMORY_DYNAMICbuffers, or a device-local type that is also host-visible): record a barrierALL_COMMANDS / MEMORY_WRITE -> HOST / HOST_READ, then map the memory. If the memory type is notHOST_COHERENT, callvkInvalidateMappedMemoryRangesbeforememcpy. The whole allocation is mapped and invalidated, so the range is always aligned tononCoherentAtomSize.HOST_VISIBLE | HOST_COHERENTstaging buffer, recordALL_COMMANDS / MEMORY_WRITE -> TRANSFER / TRANSFER_READ,vkCmdCopyBuffer, thenTRANSFER / TRANSFER_WRITE -> HOST / HOST_READ. Device-local buffers now also getTRANSFER_SRCusage for this copy.image_index, likeupdate.The memory type is known now:
create_bufferreturns the property flags of the chosen memory type (new nullable out parameter), andBufferImplstores them inmemory_flags.Synchronisation
All callers read a buffer right after
pl_start_computein the same frame, and they expect the result of those dispatches (OpenGLglGetBufferSubDatagives it). In Vulkan these dispatches are only recorded incompute_bufferand are submitted indraw_end. Soreadrecords its barriers and copy at the end ofcompute_bufferand calls the newvk_compute_flush(vulkan/render.c):compute_bufferand submit it to the compute queue with a new fence;graphics_semaphore(if it is signaled) atALL_COMMANDS, because the recorded copy may read what the previous graphics submit wrote; after thatgraphics_semaphore_signaled = 0, so the compute submit indraw_enddoes not wait on the same signal again;compute_semaphore: the last compute submit indraw_endstill signals it once, and the graphics submit waits for it as before;compute_bufferagain for the rest of the frame.start_computebinds the pipeline and descriptor set on every call, so no state is lost.readoutsidedraw_begin/draw_end(no compute buffer is recording then) stops with a clear error. There are no such callers now.RenderImpl.in_frametracks this.Side effect in debug mode: the compute timestamp counter that spans a read also includes the host time of the read.
Testing
cmake -B build -G Ninja && cmake --build build) on macOS, base6b87835and this branch, both clean builds: 5 warnings each, the same list (GLSL#extensionand one incontrib/astc-codec). GCC 16-Wall -fsyntax-onlyon the changed files: no warnings before and after.ctest:test_base64andtest_configpass;test_gltffails in the same way on base, because theassets/khrsubmodule is not checked out locally (CI checks it out).VK_LAYER_KHRONOS_validationneeds the owner's agreement. Proposed check (MoltenVK, from the repository root after the build):Expected: no validation messages in both logs;
particles3keeps drawing for 15 s and writes onedensity.NNNNNN.txtper frame (about 0.4 MB each fornn=32, non-zero values);poissonexits by itself after the first frame and printserr = ...much smaller than 1 (err = 1would mean that the read returned zeros). About 30 seconds in total.