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