1 2--- 3title: "Vulkan" 4linkTitle: "Vulkan" 5 6--- 7 8 9Skia has a Vulkan implementation of its GPU backend. The Vulkan backend can be 10built alongside the OpenGL backend. The client can select between the OpenGL 11and Vulkan implementation at runtime. The Vulkan backend has reached feature 12parity with the OpenGL backend. At this time we find that many Vulkan drivers 13have bugs that Skia triggers for which we have no workaround. We are reporting 14bugs to vendors as we find them. 15 16Windows and Linux 17----------------- 18To build the Vulkan backend, set `skia_use_vulkan=true` in `args.gn`. 19 20Android 21------- 22The Vulkan backend can run on any device with Vulkan drivers, including all Android N+ devices. 23To build the Vulkan backend, set `ndk_api = 24` in `args.gn` to target Android N. 24 25Mac 26--- 27The Vulkan backend can be run in software emulation using SwiftShader. This will allow for 28testing and debugging via `dm`. (Vulkan is not supported in interactive apps like `viewer`.) 29 30Skia already includes the SwiftShader library as an external dependency. To build it, you 31will first need to install [CMake](https://cmake.org/download/). Set up CMake for command 32line use by opening the app and following the instructions in _Tools > How to Install For 33Command Line Use_. Once CMake has been prepared, SwiftShader needs to be compiled. Follow 34these steps, substituting your actual Skia directory instead of `$(SKIA_DIR)` below: 35 36<!--?prettify lang=bash--> 37 $ cd $(SKIA_DIR)/third_party/externals/swiftshader/build 38 $ cmake .. 39 $ cmake --build . --parallel 40 41Once its build completes, SwiftShader's `build` directory should include a `Darwin` 42subdirectory containing `libvk_swiftshader.dylib`. To allow Skia to see this library, 43we need to reference it in `args.gn` like so: 44 45``` 46skia_use_vulkan = true 47extra_cflags = [ "-D", "SK_GPU_TOOLS_VK_LIBRARY_NAME=$(SKIA_DIR)/third_party/externals/swiftshader/build/Darwin/libvk_swiftshader.dylib" ] 48``` 49 50Using the Vulkan Backend 51------------------------ 52 53To 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 55<!--?prettify lang=c++?--> 56 skgpu::VulkanBackendContext vkContext; 57 vkBackendContext.fInstance = vkInstance; 58 vkBackendContext.fPhysicalDevice = vkPhysDevice; 59 ... 60 61 sk_sp<GrContext> context = GrDirectContexts::MakeVulkan::MakeVulkan(vkBackendContext); 62 63When using the Vulkan backend, GrVkImageInfo is used to construct GrBackendTexture 64and GrBackendRenderTarget objects that in turn are used to create SkSurface and SkImage 65objects that refer to VkImages created by the Skia client. 66 67The GrBackendObject returned by SkImage::getTextureHandle(), 68SkSurface::getTextureHandle(), and SkSurface::getRenderTargetHandle() should be 69interpreted as a GrVkImageInfo*. This allows a client to get the backing VkImage 70of a SkImage or SkSurface. 71 72GrVkImageInfo specifies a VkImage and associated state (tiling, layout, format, etc). 73After getting a GrVkImageInfo* via getTextureHandle() or 74getRenderTargetHandle(), the client should check the fImageLayout field to know 75what layout Skia left the VkImage in before using the VkImage. If the client 76changes the layout of the VkImage, 77GrVkImageInfo::updateImageLayout(VkImageLayout layout) should be called before 78resuming Skia rendering. 79 80The client is responsible for any synchronization or barriers needed before 81Skia performs I/O on a VkImage imported into Skia via GrVkImageInfo. Skia will 82assume it can start issuing commands referencing the VkImage without the need 83for additional synchronization. 84 85