Debian Codegraph @

src/gpu/graphite/vk/VulkanCaps.h

raw
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
8 #ifndef skgpu_graphite_VulkanCaps_DEFINED
9 #define skgpu_graphite_VulkanCaps_DEFINED
10
11 #include "include/private/base/SkTDArray.h"
12 #include "src/gpu/graphite/Caps.h"
13 #include "src/gpu/vk/VulkanInterface.h"
14 #include "src/gpu/vk/VulkanUtilsPriv.h"
15
16 namespace skgpu::graphite {
17 struct ContextOptions;
18 class VulkanTextureInfo;
19
20 class VulkanCaps final : public Caps {
21 public:
22 VulkanCaps(const ContextOptions&,
23 const skgpu::VulkanInterface*,
24 VkPhysicalDevice,
25 uint32_t physicalDeviceVersion,
26 const VkPhysicalDeviceFeatures2*,
27 const skgpu::VulkanExtensions*,
28 Protected);
29 ~VulkanCaps() override;
30
31 bool isSampleCountSupported(TextureFormat, SampleCount requestedSampleCount) const override;
32 TextureFormat getDepthStencilFormat(SkEnumBitMask<DepthStencilFlags>) const override;
33
34 TextureInfo getDefaultAttachmentTextureInfo(AttachmentDesc,
35 Protected,
36 Discardable) const override;
37
38 TextureInfo getDefaultSampledTextureInfo(SkColorType,
39 Mipmapped,
40 Protected,
41 Renderable) const override;
42
43 TextureInfo getTextureInfoForSampledCopy(const TextureInfo&, Mipmapped) const override;
44
45 TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType,
46 Mipmapped,
47 Protected) const override;
48
49 TextureInfo getDefaultStorageTextureInfo(SkColorType) const override;
50
51 // Override Caps's implementation in order to consult Vulkan-specific texture properties.
52 DstReadStrategy getDstReadStrategy() const override;
53
54 ImmutableSamplerInfo getImmutableSamplerInfo(const TextureInfo&) const override;
55 std::string toString(const ImmutableSamplerInfo&) const override;
56
57 UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc&,
58 const RenderPassDesc&) const override;
59 bool extractGraphicsDescs(const UniqueKey&,
60 GraphicsPipelineDesc*,
61 RenderPassDesc*,
62 const RendererProvider*) const override;
63 UniqueKey makeComputePipelineKey(const ComputePipelineDesc&) const override { return {}; }
64
65
66 bool isRenderable(const TextureInfo&) const override;
67 bool isStorage(const TextureInfo&) const override;
68
69 bool isFormatSupported(VkFormat) const;
70 bool isTexturable(const VulkanTextureInfo&) const;
71 bool isRenderable(const VulkanTextureInfo&) const;
72 bool isTransferSrc(const VulkanTextureInfo&) const;
73 bool isTransferDst(const VulkanTextureInfo&) const;
74
75 void buildKeyForTexture(SkISize dimensions,
76 const TextureInfo&,
77 ResourceType,
78 GraphiteResourceKey*) const override;
79
80 bool shouldAlwaysUseDedicatedImageMemory() const {
81 return fShouldAlwaysUseDedicatedImageMemory;
82 }
83
84 // Returns whether a pure GPU accessible buffer is more performant to read than a buffer that is
85 // also host visible. If so then in some cases we may prefer the cost of doing a copy to the
86 // buffer. This typically would only be the case for buffers that are written once and read
87 // many times on the gpu.
88 bool gpuOnlyBuffersMorePerformant() const { return fGpuOnlyBuffersMorePerformant; }
89
90 // For our CPU write and GPU read buffers (vertex, uniform, etc.), we should keep these buffers
91 // persistently mapped. In general, the answer will be yes. The main case where we don't do this
92 // is when using special memory that is DEVICE_LOCAL and HOST_VISIBLE on discrete GPUs.
93 bool shouldPersistentlyMapCpuToGpuBuffers() const {
94 return fShouldPersistentlyMapCpuToGpuBuffers;
95 }
96
97 bool supportsYcbcrConversion() const { return fSupportsYcbcrConversion; }
98 bool supportsDeviceFaultInfo() const { return fSupportsDeviceFaultInfo; }
99
100 // Whether a barrier is required before reading from input attachments (barrier is needed if
101 // !coherent).
102 bool isInputAttachmentReadCoherent() const { return fIsInputAttachmentReadCoherent; }
103 // isInputAttachmentReadCoherent() is based on whether
104 // VK_EXT_rasterization_order_attachment_access is supported, but is also enabled on a few
105 // architectures where it's known a priori that input attachment reads are coherent. The
106 // following determines whether that extension is enabled (in which case a pipeline creation
107 // flag is necessary) or not. When disabled, a subpass self-dependency is needed instead.
108 bool supportsRasterizationOrderColorAttachmentAccess() const {
109 return fSupportsRasterizationOrderColorAttachmentAccess;
110 }
111
112 uint32_t maxVertexAttributes() const { return fMaxVertexAttributes; }
113 uint64_t maxUniformBufferRange() const { return fMaxUniformBufferRange; }
114 uint64_t maxStorageBufferRange() const { return fMaxStorageBufferRange; }
115
116 const VkPhysicalDeviceMemoryProperties2& physicalDeviceMemoryProperties2() const {
117 return fPhysicalDeviceMemoryProperties2;
118 }
119
120 bool mustLoadFullImageForMSAA() const { return fMustLoadFullImageForMSAA; }
121
122 bool supportsFrameBoundary() const { return fSupportsFrameBoundary; }
123
124 bool supportsPipelineCreationCacheControl() const {
125 return fSupportsPipelineCreationCacheControl;
126 }
127
128 private:
129 void init(const ContextOptions&,
130 const skgpu::VulkanInterface*,
131 VkPhysicalDevice,
132 uint32_t physicalDeviceVersion,
133 const VkPhysicalDeviceFeatures2*,
134 const skgpu::VulkanExtensions*,
135 Protected);
136
137 struct EnabledFeatures {
138 // VkPhysicalDeviceFeatures
139 bool fDualSrcBlend = false;
140 // From VkPhysicalDeviceSamplerYcbcrConversionFeatures or VkPhysicalDeviceVulkan11Features:
141 bool fSamplerYcbcrConversion = false;
142 // From VkPhysicalDeviceFaultFeaturesEXT:
143 bool fDeviceFault = false;
144 // From VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT:
145 bool fAdvancedBlendModes = false;
146 bool fCoherentAdvancedBlendModes = false;
147 // From VK_EXT_rasterization_order_attachment_access:
148 bool fRasterizationOrderColorAttachmentAccess = false;
149 // From VkPhysicalDeviceExtendedDynamicStateFeaturesEXT or Vulkan 1.3 (no features):
150 bool fExtendedDynamicState = false;
151 // From VkPhysicalDeviceExtendedDynamicState2FeaturesEXT or Vulkan 1.3 (no features):
152 bool fExtendedDynamicState2 = false;
153 // From VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT:
154 bool fVertexInputDynamicState = false;
155 // From VkPhysicalDeviceGraphicsPipelineLibraryFeaturesEXT:
156 bool fGraphicsPipelineLibrary = false;
157 // From VkPhysicalDeviceMultisampledRenderToSingleSampledFeaturesEXT:
158 bool fMultisampledRenderToSingleSampled = false;
159 // From VkPhysicalDeviceHostImageCopyFeatures:
160 bool fHostImageCopy = false;
161 // From VkPhysicalDeviceFrameBoundaryFeaturesEXT:
162 bool fFrameBoundary = false;
163 // From VkPhysicalDevicePipelineCreationCacheControlFeatures or
164 // VkPhysicalDeviceVulkan13Features
165 bool fPipelineCreationCacheControl = false;
166 // From VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT
167 bool fFormatRGBA10x6WithoutYCbCrSampler = false;
168 };
169 EnabledFeatures getEnabledFeatures(const VkPhysicalDeviceFeatures2*,
170 uint32_t physicalDeviceVersion);
171
172 struct PhysicalDeviceProperties {
173 VkPhysicalDeviceProperties2 fBase;
174 VkPhysicalDeviceDriverProperties fDriver;
175 VkPhysicalDeviceGraphicsPipelineLibraryPropertiesEXT fGpl;
176 VkPhysicalDeviceHostImageCopyPropertiesEXT fHic;
177 bool fHicHasShaderReadOnlyDstLayout = false;
178 };
179 void getProperties(const skgpu::VulkanInterface*,
180 VkPhysicalDevice,
181 uint32_t physicalDeviceVersion,
182 const skgpu::VulkanExtensions*,
183 const EnabledFeatures&,
184 PhysicalDeviceProperties*);
185
186 void applyDriverCorrectnessWorkarounds(const PhysicalDeviceProperties&);
187
188 void initShaderCaps(const EnabledFeatures, const uint32_t vendorID);
189
190 void initFormatTable(const skgpu::VulkanInterface*,
191 VkPhysicalDevice,
192 const VkPhysicalDeviceProperties&,
193 const EnabledFeatures&);
194
195 void initDepthStencilFormatTable(const skgpu::VulkanInterface*,
196 VkPhysicalDevice,
197 const VkPhysicalDeviceProperties&);
198
199 SkSpan<const ColorTypeInfo> getColorTypeInfos(const TextureInfo&) const override;
200
201 bool onIsTexturable(const TextureInfo&) const override;
202
203 bool supportsWritePixels(const TextureInfo&) const override;
204 bool supportsReadPixels(const TextureInfo&) const override;
205
206 /*
207 * Whether the texture supports multisampled-render-to-single-sampled. When
208 * VK_EXT_multisampled_render_to_single_sampled is supported, all textures created by Graphite
209 * that are renderable will support this feature. Textures imported into Graphite however
210 * depend on whether the application has created the image with the
211 * VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT flag.
212 */
213 bool msaaTextureRenderToSingleSampledSupport(const TextureInfo&) const override;
214
215 // Struct that determines and stores which sample count quantities a VkFormat supports.
216 struct SupportedSampleCounts {
217 void initSampleCounts(const skgpu::VulkanInterface*,
218 const VulkanCaps&,
219 VkPhysicalDevice,
220 VkFormat,
221 VkImageUsageFlags);
222
223 bool isSampleCountSupported(SampleCount requestedCount) const;
224
225 VkSampleCountFlags fSampleCounts = 0;
226 };
227
228 // Struct that determines and stores useful information about VkFormats.
229 struct FormatInfo {
230 uint32_t colorTypeFlags(SkColorType colorType) const {
231 for (int i = 0; i < fColorTypeInfoCount; ++i) {
232 if (fColorTypeInfos[i].fColorType == colorType) {
233 return fColorTypeInfos[i].fFlags;
234 }
235 }
236 return 0;
237 }
238
239 void init(const skgpu::VulkanInterface*, const VulkanCaps&, VkPhysicalDevice, VkFormat);
240
241 bool isTexturable(VkImageTiling) const;
242 bool isRenderable(VkImageTiling, SampleCount sampleCount) const;
243 bool isStorage(VkImageTiling) const;
244 bool isTransferSrc(VkImageTiling) const;
245 bool isTransferDst(VkImageTiling) const;
246 bool isEfficientWithHostImageCopy(VkImageTiling, Protected) const;
247
248 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
249 int fColorTypeInfoCount = 0;
250
251 VkFormatProperties fFormatProperties = {};
252 SupportedSampleCounts fSupportedSampleCounts;
253 /*
254 * The VK_IMAGE_USAGE_HOST_TRANSFER_BIT flag may cause the image to be put in a suboptimal
255 * physical layout. In practice, images that could have had framebuffer compression end up
256 * with framebuffer compression disabled. Using `VkHostImageCopyDevicePerformanceQuery`, we
257 * can determine if the layout is going to be suboptimal and avoid this flag.
258 *
259 * `fIsEfficientWithHostImageCopy` indicates whether the VK_IMAGE_USAGE_HOST_TRANSFER_BIT is
260 * efficient for this format with the following assumptions:
261 *
262 * - Image tiling is VK_IMAGE_TILING_OPTIMAL (note that VK_IMAGE_TILING_LINEAR is always
263 * efficient for host image copy).
264 * - Image type is 2D.
265 * - Image create flags is 0.
266 * - Image usage flags is a subset of VK_IMAGE_USAGE_SAMPLED_BIT |
267 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
268 * VK_IMAGE_USAGE_TRANSFER_DST_BIT
269 */
270 bool fIsEfficientWithHostImageCopy = false;
271
272 // Indicates that a format is only supported if we are wrapping a texture with it.
273 SkDEBUGCODE(bool fIsWrappedOnly = false;)
274 };
275
276 // Map SkColorType to VkFormat.
277 VkFormat fColorTypeToFormatTable[kSkColorTypeCnt];
278 void setColorType(SkColorType, std::initializer_list<VkFormat> formats);
279 VkFormat getFormatFromColorType(SkColorType) const;
280
281 // Map VkFormat to FormatInfo.
282 static const size_t kNumVkFormats = 24;
283 FormatInfo fFormatTable[kNumVkFormats];
284
285 FormatInfo& getFormatInfoForInit(VkFormat);
286 const FormatInfo& getFormatInfo(VkFormat) const;
287
288 // A more lightweight equivalent to FormatInfo for depth/stencil VkFormats.
289 struct DepthStencilFormatInfo {
290 void init(const skgpu::VulkanInterface*, const VulkanCaps&, VkPhysicalDevice, VkFormat);
291 bool isDepthStencilSupported() const;
292
293 VkFormatProperties fFormatProperties = {};
294 SupportedSampleCounts fSupportedSampleCounts;
295 };
296
297 // Map DepthStencilFlags to VkFormat.
298 static const size_t kNumDepthStencilFlags = 4;
299 VkFormat fDepthStencilFlagsToFormatTable[kNumDepthStencilFlags];
300
301 // Map depth/stencil VkFormats to DepthStencilFormatInfo.
302 static const size_t kNumDepthStencilVkFormats = 5;
303 DepthStencilFormatInfo fDepthStencilFormatTable[kNumDepthStencilVkFormats];
304
305 DepthStencilFormatInfo& getDepthStencilFormatInfoForInit(VkFormat);
306 const DepthStencilFormatInfo& getDepthStencilFormatInfo(VkFormat) const;
307
308 uint32_t fMaxVertexAttributes;
309 uint64_t fMaxUniformBufferRange;
310 uint64_t fMaxStorageBufferRange;
311 VkPhysicalDeviceMemoryProperties2 fPhysicalDeviceMemoryProperties2;
312
313 // Various bools to define whether certain Vulkan features are supported.
314 bool fSupportsMemorylessAttachments = false;
315 bool fSupportsYcbcrConversion = false;
316 bool fShouldAlwaysUseDedicatedImageMemory = false;
317 bool fGpuOnlyBuffersMorePerformant = false;
318 bool fShouldPersistentlyMapCpuToGpuBuffers = true;
319 bool fSupportsDeviceFaultInfo = false;
320 bool fSupportsRasterizationOrderColorAttachmentAccess = false;
321 bool fIsInputAttachmentReadCoherent = false;
322 bool fSupportsFrameBoundary = false;
323 bool fSupportsPipelineCreationCacheControl = false;
324
325 // Flags to enable workarounds for driver bugs
326 bool fMustLoadFullImageForMSAA = false;
327 };
328
329 } // namespace skgpu::graphite
330
331 #endif // skgpu_graphite_VulkanCaps_DEFINED