xref: /aosp_15_r20/external/skia/site/docs/user/special/vulkan.md (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker
2*c8dee2aaSAndroid Build Coastguard Worker---
3*c8dee2aaSAndroid Build Coastguard Workertitle: "Vulkan"
4*c8dee2aaSAndroid Build Coastguard WorkerlinkTitle: "Vulkan"
5*c8dee2aaSAndroid Build Coastguard Worker
6*c8dee2aaSAndroid Build Coastguard Worker---
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker
9*c8dee2aaSAndroid Build Coastguard WorkerSkia has a Vulkan implementation of its GPU backend. The Vulkan backend can be
10*c8dee2aaSAndroid Build Coastguard Workerbuilt alongside the OpenGL backend. The client can select between the OpenGL
11*c8dee2aaSAndroid Build Coastguard Workerand Vulkan implementation at runtime. The Vulkan backend has reached feature
12*c8dee2aaSAndroid Build Coastguard Workerparity with the OpenGL backend. At this time we find that many Vulkan drivers
13*c8dee2aaSAndroid Build Coastguard Workerhave bugs that Skia triggers for which we have no workaround. We are reporting
14*c8dee2aaSAndroid Build Coastguard Workerbugs to vendors as we find them.
15*c8dee2aaSAndroid Build Coastguard Worker
16*c8dee2aaSAndroid Build Coastguard WorkerWindows and Linux
17*c8dee2aaSAndroid Build Coastguard Worker-----------------
18*c8dee2aaSAndroid Build Coastguard WorkerTo build the Vulkan backend, set `skia_use_vulkan=true` in `args.gn`.
19*c8dee2aaSAndroid Build Coastguard Worker
20*c8dee2aaSAndroid Build Coastguard WorkerAndroid
21*c8dee2aaSAndroid Build Coastguard Worker-------
22*c8dee2aaSAndroid Build Coastguard WorkerThe Vulkan backend can run on any device with Vulkan drivers, including all Android N+ devices.
23*c8dee2aaSAndroid Build Coastguard WorkerTo build the Vulkan backend, set `ndk_api = 24` in `args.gn` to target Android N.
24*c8dee2aaSAndroid Build Coastguard Worker
25*c8dee2aaSAndroid Build Coastguard WorkerMac
26*c8dee2aaSAndroid Build Coastguard Worker---
27*c8dee2aaSAndroid Build Coastguard WorkerThe Vulkan backend can be run in software emulation using SwiftShader. This will allow for
28*c8dee2aaSAndroid Build Coastguard Workertesting and debugging via `dm`. (Vulkan is not supported in interactive apps like `viewer`.)
29*c8dee2aaSAndroid Build Coastguard Worker
30*c8dee2aaSAndroid Build Coastguard WorkerSkia already includes the SwiftShader library as an external dependency. To build it, you
31*c8dee2aaSAndroid Build Coastguard Workerwill first need to install [CMake](https://cmake.org/download/). Set up CMake for command
32*c8dee2aaSAndroid Build Coastguard Workerline use by opening the app and following the instructions in _Tools > How to Install For
33*c8dee2aaSAndroid Build Coastguard WorkerCommand Line Use_. Once CMake has been prepared, SwiftShader needs to be compiled. Follow
34*c8dee2aaSAndroid Build Coastguard Workerthese steps, substituting your actual Skia directory instead of `$(SKIA_DIR)` below:
35*c8dee2aaSAndroid Build Coastguard Worker
36*c8dee2aaSAndroid Build Coastguard Worker<!--?prettify lang=bash-->
37*c8dee2aaSAndroid Build Coastguard Worker    $ cd $(SKIA_DIR)/third_party/externals/swiftshader/build
38*c8dee2aaSAndroid Build Coastguard Worker    $ cmake ..
39*c8dee2aaSAndroid Build Coastguard Worker    $ cmake --build . --parallel
40*c8dee2aaSAndroid Build Coastguard Worker
41*c8dee2aaSAndroid Build Coastguard WorkerOnce its build completes, SwiftShader's `build` directory should include a `Darwin`
42*c8dee2aaSAndroid Build Coastguard Workersubdirectory containing `libvk_swiftshader.dylib`. To allow Skia to see this library,
43*c8dee2aaSAndroid Build Coastguard Workerwe need to reference it in `args.gn` like so:
44*c8dee2aaSAndroid Build Coastguard Worker
45*c8dee2aaSAndroid Build Coastguard Worker```
46*c8dee2aaSAndroid Build Coastguard Workerskia_use_vulkan = true
47*c8dee2aaSAndroid Build Coastguard Workerextra_cflags = [ "-D", "SK_GPU_TOOLS_VK_LIBRARY_NAME=$(SKIA_DIR)/third_party/externals/swiftshader/build/Darwin/libvk_swiftshader.dylib" ]
48*c8dee2aaSAndroid Build Coastguard Worker```
49*c8dee2aaSAndroid Build Coastguard Worker
50*c8dee2aaSAndroid Build Coastguard WorkerUsing the Vulkan Backend
51*c8dee2aaSAndroid Build Coastguard Worker------------------------
52*c8dee2aaSAndroid Build Coastguard Worker
53*c8dee2aaSAndroid Build Coastguard WorkerTo create a GrContext that is backed by Vulkan the client creates a Vulkan device and queue, initializes a skgpu::VulkanBackendContext to describe the context, and then calls GrDirectContexts::MakeVulkan:
54*c8dee2aaSAndroid Build Coastguard Worker
55*c8dee2aaSAndroid Build Coastguard Worker<!--?prettify lang=c++?-->
56*c8dee2aaSAndroid Build Coastguard Worker    skgpu::VulkanBackendContext vkContext;
57*c8dee2aaSAndroid Build Coastguard Worker    vkBackendContext.fInstance = vkInstance;
58*c8dee2aaSAndroid Build Coastguard Worker    vkBackendContext.fPhysicalDevice = vkPhysDevice;
59*c8dee2aaSAndroid Build Coastguard Worker    ...
60*c8dee2aaSAndroid Build Coastguard Worker
61*c8dee2aaSAndroid Build Coastguard Worker    sk_sp<GrContext> context = GrDirectContexts::MakeVulkan::MakeVulkan(vkBackendContext);
62*c8dee2aaSAndroid Build Coastguard Worker
63*c8dee2aaSAndroid Build Coastguard WorkerWhen using the Vulkan backend, GrVkImageInfo is used to construct GrBackendTexture
64*c8dee2aaSAndroid Build Coastguard Workerand GrBackendRenderTarget objects that in turn are used to create SkSurface and SkImage
65*c8dee2aaSAndroid Build Coastguard Workerobjects that refer to VkImages created by the Skia client.
66*c8dee2aaSAndroid Build Coastguard Worker
67*c8dee2aaSAndroid Build Coastguard WorkerThe GrBackendObject returned by SkImage::getTextureHandle(),
68*c8dee2aaSAndroid Build Coastguard WorkerSkSurface::getTextureHandle(), and SkSurface::getRenderTargetHandle() should be
69*c8dee2aaSAndroid Build Coastguard Workerinterpreted as a GrVkImageInfo*. This allows a client to get the backing VkImage
70*c8dee2aaSAndroid Build Coastguard Workerof a SkImage or SkSurface.
71*c8dee2aaSAndroid Build Coastguard Worker
72*c8dee2aaSAndroid Build Coastguard WorkerGrVkImageInfo specifies a VkImage and associated state (tiling, layout, format, etc).
73*c8dee2aaSAndroid Build Coastguard WorkerAfter getting a GrVkImageInfo* via getTextureHandle() or
74*c8dee2aaSAndroid Build Coastguard WorkergetRenderTargetHandle(), the client should check the fImageLayout field to know
75*c8dee2aaSAndroid Build Coastguard Workerwhat layout Skia left the VkImage in before using the VkImage. If the client
76*c8dee2aaSAndroid Build Coastguard Workerchanges the layout of the VkImage,
77*c8dee2aaSAndroid Build Coastguard WorkerGrVkImageInfo::updateImageLayout(VkImageLayout layout) should be called before
78*c8dee2aaSAndroid Build Coastguard Workerresuming Skia rendering.
79*c8dee2aaSAndroid Build Coastguard Worker
80*c8dee2aaSAndroid Build Coastguard WorkerThe client is responsible for any synchronization or barriers needed before
81*c8dee2aaSAndroid Build Coastguard WorkerSkia performs I/O on a VkImage imported into Skia via GrVkImageInfo.  Skia will
82*c8dee2aaSAndroid Build Coastguard Workerassume it can start issuing commands referencing the VkImage without the need
83*c8dee2aaSAndroid Build Coastguard Workerfor additional synchronization.
84*c8dee2aaSAndroid Build Coastguard Worker
85