Flutter Engine
The Flutter Engine
VulkanGraphiteUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
10
19
21
22std::unique_ptr<Context> MakeVulkan(const VulkanBackendContext& backendContext,
23 const ContextOptions& options) {
24 sk_sp<SharedContext> sharedContext = VulkanSharedContext::Make(backendContext, options);
25 if (!sharedContext) {
26 return nullptr;
27 }
28
29 std::unique_ptr<QueueManager> queueManager(new VulkanQueueManager(backendContext.fQueue,
30 sharedContext.get()));
31 if (!queueManager) {
32 return nullptr;
33 }
34
35 return ContextCtorAccessor::MakeContext(std::move(sharedContext),
36 std::move(queueManager),
37 options);
38}
39
40} // namespace skgpu::graphite::ContextFactory
41
42namespace skgpu::graphite {
43
44VkShaderModule createVulkanShaderModule(const VulkanSharedContext* context,
45 const std::string& spirv,
47 TRACE_EVENT0("skia.shaders", "InstallVkShaderModule");
48 VkShaderModuleCreateInfo moduleCreateInfo;
49 memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo));
51 moduleCreateInfo.pNext = nullptr;
52 moduleCreateInfo.flags = 0;
53 moduleCreateInfo.codeSize = spirv.size();
54 moduleCreateInfo.pCode = (const uint32_t*)spirv.c_str();
55
56 VkShaderModule shaderModule;
58 VULKAN_CALL_RESULT(context,
59 result,
60 CreateShaderModule(context->device(),
61 &moduleCreateInfo,
62 /*const VkAllocationCallbacks*=*/nullptr,
63 &shaderModule));
64 if (result != VK_SUCCESS) {
65 SKGPU_LOG_E("Failed to create VkShaderModule");
66 return VK_NULL_HANDLE;
67 }
68 return shaderModule;
69}
70
72 const SkSpan<DescriptorData>& requestedDescriptors,
73 VkDescriptorSetLayout* outLayout) {
75 for (size_t i = 0; i < requestedDescriptors.size(); i++) {
76 if (requestedDescriptors[i].fCount != 0) {
77 const DescriptorData& currDescriptor = requestedDescriptors[i];
78 VkDescriptorSetLayoutBinding& layoutBinding = bindingLayouts.push_back();
79 memset(&layoutBinding, 0, sizeof(VkDescriptorSetLayoutBinding));
80 layoutBinding.binding = currDescriptor.fBindingIndex;
81 layoutBinding.descriptorType = DsTypeEnumToVkDs(currDescriptor.fType);
82 layoutBinding.descriptorCount = currDescriptor.fCount;
83 layoutBinding.stageFlags =
85 // TODO(b/302126498): Set pImmutableSampler to currDescriptor.fImmutableSampler once
86 // immutable samplers are created and used within graphite.
87 layoutBinding.pImmutableSamplers = nullptr;
88 }
89 }
90
91 VkDescriptorSetLayoutCreateInfo layoutCreateInfo;
92 memset(&layoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreateInfo));
94 layoutCreateInfo.pNext = nullptr;
95 layoutCreateInfo.flags = 0;
96 layoutCreateInfo.bindingCount = bindingLayouts.size();
97 layoutCreateInfo.pBindings = &bindingLayouts.front();
98
101 ctxt,
102 result,
103 CreateDescriptorSetLayout(ctxt->device(), &layoutCreateInfo, nullptr, outLayout));
104 if (result != VK_SUCCESS) {
105 SkDebugf("Failed to create VkDescriptorSetLayout\n");
106 outLayout = VK_NULL_HANDLE;
107 }
108}
109
111 switch (type) {
124 }
126}
127
129 switch (format) {
157 return true;
158 default:
159 return false;
160 }
161}
162
165 VkShaderStageFlags vkStageFlags = 0;
166 if (stageFlags & PipelineStageFlags::kVertexShader) {
167 vkStageFlags |= VK_SHADER_STAGE_VERTEX_BIT;
168 }
169 if (stageFlags & PipelineStageFlags::kFragmentShader) {
170 vkStageFlags |= VK_SHADER_STAGE_FRAGMENT_BIT;
171 }
172 if (stageFlags & PipelineStageFlags::kCompute) {
173 vkStageFlags |= VK_SHADER_STAGE_COMPUTE_BIT;
174 }
175 return vkStageFlags;
176}
177
178namespace ycbcrPackaging {
179uint32_t nonFormatInfoAsUInt32(const VulkanYcbcrConversionInfo& conversionInfo) {
180 static_assert(kComponentAShift + kComponentBits <= 32);
181
182 SkASSERT(conversionInfo.fYcbcrModel < (1u << kYcbcrModelBits ));
183 SkASSERT(conversionInfo.fYcbcrRange < (1u << kYcbcrRangeBits ));
184 SkASSERT(conversionInfo.fXChromaOffset < (1u << kXChromaOffsetBits ));
185 SkASSERT(conversionInfo.fYChromaOffset < (1u << kYChromaOffsetBits ));
186 SkASSERT(conversionInfo.fChromaFilter < (1u << kChromaFilterBits ));
188 SkASSERT(conversionInfo.fComponents.r < (1u << kComponentBits ));
189 SkASSERT(conversionInfo.fComponents.g < (1u << kComponentBits ));
190 SkASSERT(conversionInfo.fComponents.b < (1u << kComponentBits ));
191 SkASSERT(conversionInfo.fComponents.a < (1u << kComponentBits ));
192
193 bool usesExternalFormat = conversionInfo.fFormat == VK_FORMAT_UNDEFINED;
194
195 return (((uint32_t)(usesExternalFormat ) << kUsesExternalFormatShift) |
196 ((uint32_t)(conversionInfo.fYcbcrModel ) << kYcbcrModelShift ) |
197 ((uint32_t)(conversionInfo.fYcbcrRange ) << kYcbcrRangeShift ) |
198 ((uint32_t)(conversionInfo.fXChromaOffset ) << kXChromaOffsetShift ) |
199 ((uint32_t)(conversionInfo.fYChromaOffset ) << kYChromaOffsetShift ) |
200 ((uint32_t)(conversionInfo.fChromaFilter ) << kChromaFilterShift ) |
201 ((uint32_t)(conversionInfo.fForceExplicitReconstruction) << kForceExplicitReconShift) |
202 ((uint32_t)(conversionInfo.fComponents.r ) << kComponentRShift ) |
203 ((uint32_t)(conversionInfo.fComponents.g ) << kComponentGShift ) |
204 ((uint32_t)(conversionInfo.fComponents.b ) << kComponentBShift ) |
205 ((uint32_t)(conversionInfo.fComponents.a ) << kComponentAShift ));
206}
207
208int numInt32sNeeded(const VulkanYcbcrConversionInfo& conversionInfo) {
209 if (!conversionInfo.isValid()) {
210 return 0;
211 }
212 return (conversionInfo.fFormat == VK_FORMAT_UNDEFINED) ? kInt32sNeededExternalFormat
214}
215} // namespace ycbcrPackaging
216
217} // namespace skgpu::graphite
const char * options
#define SKGPU_LOG_E(fmt,...)
Definition: Log.h:38
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define VULKAN_CALL_RESULT(SHARED_CONTEXT, RESULT, X)
GLenum type
constexpr size_t size() const
Definition: SkSpan_impl.h:95
T * get() const
Definition: SkRefCnt.h:303
static std::unique_ptr< Context > MakeContext(sk_sp< SharedContext >, std::unique_ptr< QueueManager >, const ContextOptions &)
Definition: Context.cpp:903
static sk_sp< SharedContext > Make(const VulkanBackendContext &, const ContextOptions &)
int size() const
Definition: SkTArray.h:421
GAsyncResult * result
uint32_t uint32_t * format
SK_API std::unique_ptr< Context > MakeVulkan(const VulkanBackendContext &, const ContextOptions &)
static constexpr int kInt32sNeededKnownFormat
static constexpr int kInt32sNeededExternalFormat
int numInt32sNeeded(const VulkanYcbcrConversionInfo &conversionInfo)
static constexpr int kUsesExternalFormatShift
static constexpr int kForceExplicitReconShift
static constexpr int kForceExplicitReconBits
uint32_t nonFormatInfoAsUInt32(const VulkanYcbcrConversionInfo &conversionInfo)
VkShaderModule createVulkanShaderModule(const VulkanSharedContext *context, const std::string &spirv, VkShaderStageFlagBits stage)
VkDescriptorType DsTypeEnumToVkDs(DescriptorType type)
bool vkFormatIsSupported(VkFormat format)
void DescriptorDataToVkDescSetLayout(const VulkanSharedContext *ctxt, const SkSpan< DescriptorData > &requestedDescriptors, VkDescriptorSetLayout *outLayout)
VkShaderStageFlags PipelineStageFlagsToVkShaderStageFlags(SkEnumBitMask< PipelineStageFlags > stageFlags)
VkComponentSwizzle r
Definition: vulkan_core.h:3460
VkComponentSwizzle a
Definition: vulkan_core.h:3463
VkComponentSwizzle g
Definition: vulkan_core.h:3461
VkComponentSwizzle b
Definition: vulkan_core.h:3462
VkDescriptorType descriptorType
Definition: vulkan_core.h:3773
const VkSampler * pImmutableSamplers
Definition: vulkan_core.h:3776
VkShaderStageFlags stageFlags
Definition: vulkan_core.h:3775
const VkDescriptorSetLayoutBinding * pBindings
Definition: vulkan_core.h:3784
VkDescriptorSetLayoutCreateFlags flags
Definition: vulkan_core.h:3782
VkShaderModuleCreateFlags flags
Definition: vulkan_core.h:3480
const uint32_t * pCode
Definition: vulkan_core.h:3482
VkStructureType sType
Definition: vulkan_core.h:3478
VkSamplerYcbcrRange fYcbcrRange
Definition: VulkanTypes.h:104
VkSamplerYcbcrModelConversion fYcbcrModel
Definition: VulkanTypes.h:103
VkComponentMapping fComponents
Definition: VulkanTypes.h:115
SkEnumBitMask< PipelineStageFlags > fPipelineStageFlags
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131
VkShaderStageFlagBits
Definition: vulkan_core.h:2664
@ VK_SHADER_STAGE_VERTEX_BIT
Definition: vulkan_core.h:2665
@ VK_SHADER_STAGE_COMPUTE_BIT
Definition: vulkan_core.h:2670
@ VK_SHADER_STAGE_FRAGMENT_BIT
Definition: vulkan_core.h:2669
VkFlags VkShaderStageFlags
Definition: vulkan_core.h:2731
VkResult
Definition: vulkan_core.h:140
@ VK_SUCCESS
Definition: vulkan_core.h:141
VkDescriptorType
Definition: vulkan_core.h:2124
@ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
Definition: vulkan_core.h:2127
@ VK_DESCRIPTOR_TYPE_SAMPLER
Definition: vulkan_core.h:2125
@ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
Definition: vulkan_core.h:2133
@ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
Definition: vulkan_core.h:2132
@ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
Definition: vulkan_core.h:2135
@ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
Definition: vulkan_core.h:2126
#define VK_NULL_HANDLE
Definition: vulkan_core.h:46
VkFormat
Definition: vulkan_core.h:1458
@ VK_FORMAT_R16G16B16A16_UNORM
Definition: vulkan_core.h:1550
@ VK_FORMAT_R8G8B8A8_SRGB
Definition: vulkan_core.h:1502
@ VK_FORMAT_R8G8B8_UNORM
Definition: vulkan_core.h:1482
@ VK_FORMAT_D24_UNORM_S8_UINT
Definition: vulkan_core.h:1588
@ VK_FORMAT_D32_SFLOAT
Definition: vulkan_core.h:1585
@ VK_FORMAT_B8G8R8A8_UNORM
Definition: vulkan_core.h:1503
@ VK_FORMAT_R16G16_SFLOAT
Definition: vulkan_core.h:1542
@ VK_FORMAT_B4G4R4A4_UNORM_PACK16
Definition: vulkan_core.h:1462
@ VK_FORMAT_R16_SFLOAT
Definition: vulkan_core.h:1535
@ VK_FORMAT_R8G8_UNORM
Definition: vulkan_core.h:1475
@ VK_FORMAT_S8_UINT
Definition: vulkan_core.h:1586
@ VK_FORMAT_R8_UNORM
Definition: vulkan_core.h:1468
@ VK_FORMAT_R5G6B5_UNORM_PACK16
Definition: vulkan_core.h:1463
@ VK_FORMAT_R4G4B4A4_UNORM_PACK16
Definition: vulkan_core.h:1461
@ VK_FORMAT_A2B10G10R10_UNORM_PACK32
Definition: vulkan_core.h:1523
@ VK_FORMAT_R8G8B8A8_UNORM
Definition: vulkan_core.h:1496
@ VK_FORMAT_UNDEFINED
Definition: vulkan_core.h:1459
@ VK_FORMAT_BC1_RGB_UNORM_BLOCK
Definition: vulkan_core.h:1590
@ VK_FORMAT_R16_UNORM
Definition: vulkan_core.h:1529
@ VK_FORMAT_D16_UNORM
Definition: vulkan_core.h:1583
@ VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
Definition: vulkan_core.h:1646
@ VK_FORMAT_R16G16B16A16_SFLOAT
Definition: vulkan_core.h:1556
@ VK_FORMAT_R16G16_UNORM
Definition: vulkan_core.h:1536
@ VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
Definition: vulkan_core.h:1606
@ VK_FORMAT_A2R10G10B10_UNORM_PACK32
Definition: vulkan_core.h:1517
@ VK_FORMAT_BC1_RGBA_UNORM_BLOCK
Definition: vulkan_core.h:1592
@ VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
Definition: vulkan_core.h:1647
@ VK_FORMAT_D32_SFLOAT_S8_UINT
Definition: vulkan_core.h:1589
@ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
Definition: vulkan_core.h:234
@ VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
Definition: vulkan_core.h:218