1# ANGLE: Vulkan Back-end 2 3ANGLE's Vulkan back-end implementation lives in this folder. 4 5[Vulkan](https://www.khronos.org/vulkan/) is an explicit graphics API. Compared to APIs like OpenGL 6or D3D11 explicit APIs can offer a number of significant benefits: 7 8 * Lower API call CPU overhead. 9 * A smaller API surface with more direct hardware control. 10 * Better support for multi-core programming. 11 * Vulkan in particular has open-source tooling and tests. 12 13## Back-end Design 14 15The [`vk::Renderer`](Renderer.cpp) class represents an `EGLDisplay`. `vk::Renderer` owns shared global 16resources like the [VkDevice][VkDevice], [VkQueue][VkQueue], the [Vulkan format tables](vk_format_utils.h) 17and [internal Vulkan shaders](shaders). The [ContextVk](ContextVk.cpp) class implements the back-end 18of a front-end OpenGL Context. ContextVk processes state changes and handles action commands like 19`glDrawArrays` and `glDrawElements`. 20 21## Command recording 22 23A render pass has three states: `unstarted`, started and active (we call it `active` in short), 24started but inactive (we call it `inactive` in short). The back-end records commands into command 25buffers via the following `ContextVk` APIs: 26 27 * `beginNewRenderPass`: Writes out (aka flushes) prior pending commands into a primary command 28 buffer, then starts a new render pass. Returns a secondary command buffer *inside* a render pass 29instance. 30 * `getOutsideRenderPassCommandBuffer`: May flush prior command buffers and close the render pass if 31 necessary, in addition to issuing the appropriate barriers. Returns a secondary command buffer 32*outside* a render pass instance. 33 * `getStartedRenderPassCommands`: Returns a reference to the currently open render pass' commands 34 buffer. 35 * `onRenderPassFinished`: Puts render pass into inactive state where you can not record more 36 commands into secondary command buffer, except in some special cases where ANGLE does some 37optimization internally. 38 * `flushCommandsAndEndRenderPassWithoutSubmit`: Marks the end of render pass. It flushes secondary 39 command buffer into vulkan's primary command buffer, puts secondary command buffer back to 40unstarted state and then goes into recycler for reuse. 41 42The back-end (mostly) records Image and Buffer barriers through additional `CommandBufferAccess` 43APIs, the result of which is passed to `getOutsideRenderPassCommandBuffer`. Note that the barriers 44are not actually recorded until `getOutsideRenderPassCommandBuffer` is called: 45 46 * `onBufferTransferRead` and `onBufferComputeShaderRead` accumulate `VkBuffer` read barriers. 47 * `onBufferTransferWrite` and `onBufferComputeShaderWrite` accumulate `VkBuffer` write barriers. 48 * `onBuffferSelfCopy` is a special case for `VkBuffer` self copies. It behaves the same as write. 49 * `onImageTransferRead` and `onImageComputerShadeRead` accumulate `VkImage` read barriers. 50 * `onImageTransferWrite` and `onImageComputerShadeWrite` accumulate `VkImage` write barriers. 51 * `onImageRenderPassRead` and `onImageRenderPassWrite` accumulate `VkImage` barriers inside a 52 started RenderPass. 53 54After the back-end records commands to the primary buffer and we flush (e.g. on swap) or when we call 55`vk::Renderer::finishQueueSerial`, ANGLE submits the primary command buffer to a `VkQueue`. 56 57See the [code][CommandAPIs] for more details. 58 59### Simple command recording example 60 61In this example we'll be recording a buffer copy command: 62 63``` 64 // Ensure that ANGLE sets proper read and write barriers for the Buffers. 65 vk::CommandBufferAccess access; 66 access.onBufferTransferWrite(dstBuffer); 67 access.onBufferTransferRead(srcBuffer); 68 69 // Get a pointer to a secondary command buffer for command recording. 70 vk::OutsideRenderPassCommandBuffer *commandBuffer; 71 ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer)); 72 73 // Record the copy command into the secondary buffer. We're done! 74 commandBuffer->copyBuffer(srcBuffer->getBuffer(), dstBuffer->getBuffer(), copyCount, copies); 75``` 76 77## Additional Reading 78 79More implementation details can be found in the `doc` directory: 80 81- [Fast OpenGL State Transitions](doc/FastOpenGLStateTransitions.md) 82- [Shader Module Compilation](doc/ShaderModuleCompilation.md) 83- [Format Tables and Emulation](doc/FormatTablesAndEmulation.md) 84- [Deferred Clears](doc/DeferredClears.md) 85- [Queries](doc/Queries.md) 86- [Present Semaphores](doc/PresentSemaphores.md) 87 88[VkDevice]: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkDevice.html 89[VkQueue]: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkQueue.html 90[CommandAPIs]: https://chromium.googlesource.com/angle/angle/+/df31624eaf3df986a0bdf3f58a87b79b0cc8db5c/src/libANGLE/renderer/vulkan/ContextVk.h#620 91 92