Debian Codegraph @

src/gpu/ganesh/vk/GrVkCaps.h

raw
1 /*
2 * Copyright 2015 Google Inc.
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 GrVkCaps_DEFINED
9 #define GrVkCaps_DEFINED
10
11 #include "include/gpu/ganesh/GrBackendSurface.h"
12 #include "include/gpu/ganesh/GrTypes.h"
13 #include "include/gpu/vk/VulkanTypes.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkTArray.h"
16 #include "include/private/base/SkTDArray.h"
17 #include "include/private/base/SkTo.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "include/private/gpu/vk/SkiaVulkan.h"
20 #include "src/gpu/Swizzle.h"
21 #include "src/gpu/ganesh/GrCaps.h"
22 #include "src/gpu/ganesh/GrProgramDesc.h"
23 #include "src/gpu/ganesh/GrSamplerState.h"
24
25 #include <cstddef>
26 #include <cstdint>
27 #include <initializer_list>
28 #include <memory>
29 #include <vector>
30
31 class GrProgramInfo;
32 class GrRenderTarget;
33 class GrRenderTargetProxy;
34 class GrSurface;
35 class GrSurfaceProxy;
36 class GrVkRenderTarget;
37 enum class SkTextureCompressionType;
38 struct GrContextOptions;
39 struct SkIRect;
40
41 namespace GrTest {
42 struct TestFormatColorTypeCombination;
43 }
44
45 namespace skgpu {
46 class KeyBuilder;
47 class VulkanExtensions;
48 enum class Protected : bool;
49 struct VulkanInterface;
50 } // namespace skgpu
51
52 /**
53 * Stores some capabilities of a Vk backend.
54 */
55 class GrVkCaps : public GrCaps {
56 public:
57 /**
58 * Creates a GrVkCaps that is set such that nothing is supported. The init function should
59 * be called to fill out the caps.
60 */
61 GrVkCaps(const GrContextOptions&,
62 const skgpu::VulkanInterface*,
63 VkPhysicalDevice,
64 const VkPhysicalDeviceFeatures2&,
65 uint32_t instanceVersion,
66 uint32_t physicalDeviceVersion,
67 const skgpu::VulkanExtensions&,
68 skgpu::Protected);
69
70 bool isFormatSRGB(const GrBackendFormat&) const override;
71
72 bool isFormatTexturable(const GrBackendFormat&, GrTextureType) const override;
73 bool isVkFormatTexturable(VkFormat) const;
74
75 bool isFormatCopyable(const GrBackendFormat&) const override { return true; }
76
77 bool isFormatAsColorTypeRenderable(GrColorType ct,
78 const GrBackendFormat& format,
79 int sampleCount = 1) const override;
80 bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override;
81 bool isFormatRenderable(VkFormat, int sampleCount) const;
82
83 int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override;
84 int getRenderTargetSampleCount(int requestedCount, VkFormat) const;
85
86 int maxRenderTargetSampleCount(const GrBackendFormat&) const override;
87 int maxRenderTargetSampleCount(VkFormat format) const;
88
89 SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
90 const GrBackendFormat& surfaceFormat,
91 GrColorType srcColorType) const override;
92
93 SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override;
94
95 bool isVkFormatTexturableLinearly(VkFormat format) const {
96 return SkToBool(FormatInfo::kTexturable_Flag & this->getFormatInfo(format).fLinearFlags);
97 }
98
99 bool formatCanBeDstofBlit(VkFormat format, bool linearTiled) const {
100 const FormatInfo& info = this->getFormatInfo(format);
101 const uint16_t& flags = linearTiled ? info.fLinearFlags : info.fOptimalFlags;
102 return SkToBool(FormatInfo::kBlitDst_Flag & flags);
103 }
104
105 bool formatCanBeSrcofBlit(VkFormat format, bool linearTiled) const {
106 const FormatInfo& info = this->getFormatInfo(format);
107 const uint16_t& flags = linearTiled ? info.fLinearFlags : info.fOptimalFlags;
108 return SkToBool(FormatInfo::kBlitSrc_Flag & flags);
109 }
110
111 // Gets the GrColorType that should be used to transfer data in/out of a transfer buffer to
112 // write/read data when using a VkFormat with a specified color type.
113 GrColorType transferColorType(VkFormat, GrColorType surfaceColorType) const;
114
115 // On some GPUs (Windows Nvidia and Imagination) calls to QueueWaitIdle return before actually
116 // signalling the fences on the command buffers even though they have completed. This causes
117 // issues when then deleting the command buffers. Therefore we additionally will call
118 // vkWaitForFences on each outstanding command buffer to make sure the driver signals the fence.
119 bool mustSyncCommandBuffersWithQueue() const { return fMustSyncCommandBuffersWithQueue; }
120
121 // Returns true if we should always make dedicated allocations for VkImages.
122 bool shouldAlwaysUseDedicatedImageMemory() const {
123 return fShouldAlwaysUseDedicatedImageMemory;
124 }
125
126 // Always use a transfer buffer instead of vkCmdUpdateBuffer to upload data to a VkBuffer.
127 bool avoidUpdateBuffers() const { return fAvoidUpdateBuffers; }
128
129 /**
130 * Returns both a supported and most preferred stencil format to use in draws.
131 */
132 VkFormat preferredStencilFormat() const { return fPreferredStencilFormat; }
133
134 // Returns total number of bits used by stencil + depth + padding
135 static int GetStencilFormatTotalBitCount(VkFormat format) {
136 switch (format) {
137 case VK_FORMAT_S8_UINT:
138 return 8;
139 case VK_FORMAT_D24_UNORM_S8_UINT:
140 return 32;
141 case VK_FORMAT_D32_SFLOAT_S8_UINT:
142 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
143 return 64;
144 default:
145 SkASSERT(false);
146 return 0;
147 }
148 }
149
150 // Returns whether the device supports VK_KHR_Swapchain. Internally Skia never uses any of the
151 // swapchain functions, but we may need to transition to and from the
152 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR image layout, so we must know whether that layout is
153 // supported.
154 bool supportsSwapchain() const { return fSupportsSwapchain; }
155
156 // Returns true if the device supports importing Android hardware buffers into Vulkan memory.
157 bool supportsAndroidHWBExternalMemory() const { return fSupportsAndroidHWBExternalMemory; }
158
159 // Returns true if it supports ycbcr conversion for samplers
160 bool supportsYcbcrConversion() const { return fSupportsYcbcrConversion; }
161
162 // Returns the number of descriptor slots used by immutable ycbcr VkImages.
163 //
164 // TODO: We should update this to return a count for a specific format or external format. We
165 // can use vkGetPhysicalDeviceImageFormatProperties2 with a
166 // VkSamplerYcbcrConversionImageFormatProperties to query this. However, right now that call
167 // does not support external android formats which is where the majority of ycbcr images are
168 // coming from. So for now we stay safe and always return 3 here which is the max value that the
169 // count could be for any format.
170 uint32_t ycbcrCombinedImageSamplerDescriptorCount() const {
171 return 3;
172 }
173
174 // Returns true if the VK_EXT_image_drm_format_modifier is enabled.
175 bool supportsDRMFormatModifiers() const { return fSupportsDRMFormatModifiers; }
176
177 bool supportsDeviceFaultInfo() const { return fSupportsDeviceFaultInfo; }
178
179 bool supportsFrameBoundary() const { return fSupportsFrameBoundary; }
180
181 bool supportsPipelineCreationCacheControl() const {
182 return fSupportsPipelineCreationCacheControl;
183 }
184
185 // Returns whether we prefer to record draws directly into a primary command buffer.
186 bool preferPrimaryOverSecondaryCommandBuffers() const {
187 return fPreferPrimaryOverSecondaryCommandBuffers;
188 }
189
190 int maxPerPoolCachedSecondaryCommandBuffers() const {
191 return fMaxPerPoolCachedSecondaryCommandBuffers;
192 }
193
194 uint32_t maxInputAttachmentDescriptors() const { return fMaxInputAttachmentDescriptors; }
195
196 float maxSamplerAnisotropy() const { return fMaxSamplerAnisotropy; }
197
198 bool mustInvalidatePrimaryCmdBufferStateAfterClearAttachments() const {
199 return fMustInvalidatePrimaryCmdBufferStateAfterClearAttachments;
200 }
201
202 // Returns whether a pure GPU accessible buffer is more performant to read than a buffer that is
203 // also host visible. If so then in some cases we may prefer the cost of doing a copy to the
204 // buffer. This typically would only be the case for buffers that are written once and read
205 // many times on the gpu.
206 bool gpuOnlyBuffersMorePerformant() const { return fGpuOnlyBuffersMorePerformant; }
207
208 // For our CPU write and GPU read buffers (vertex, uniform, etc.), should we keep these buffers
209 // persistently mapped. In general the answer will be yes. The main case we don't do this is
210 // when using special memory that is DEVICE_LOCAL and HOST_VISIBLE on discrete GPUs.
211 bool shouldPersistentlyMapCpuToGpuBuffers() const {
212 return fShouldPersistentlyMapCpuToGpuBuffers;
213 }
214
215 // The max draw count that can be passed into indirect draw calls.
216 uint32_t maxDrawIndirectDrawCount() const { return fMaxDrawIndirectDrawCount; }
217
218 /**
219 * Helpers used by canCopySurface. In all cases if the SampleCnt parameter is zero that means
220 * the surface is not a render target, otherwise it is the number of samples in the render
221 * target.
222 */
223 bool canCopyImage(VkFormat dstFormat,
224 int dstSampleCnt,
225 bool dstHasYcbcr,
226 VkFormat srcFormat,
227 int srcSamplecnt,
228 bool srcHasYcbcr) const;
229
230 bool canCopyAsBlit(VkFormat dstConfig,
231 int dstSampleCnt,
232 bool dstIsLinear,
233 bool dstHasYcbcr,
234 VkFormat srcConfig,
235 int srcSampleCnt,
236 bool srcIsLinear,
237 bool srcHasYcbcr) const;
238
239 bool canCopyAsResolve(VkFormat dstConfig,
240 int dstSampleCnt,
241 bool dstHasYcbcr,
242 VkFormat srcConfig,
243 int srcSamplecnt,
244 bool srcHasYcbcr) const;
245
246 GrBackendFormat getBackendFormatFromCompressionType(SkTextureCompressionType) const override;
247
248 VkFormat getFormatFromColorType(GrColorType colorType) const {
249 int idx = static_cast<int>(colorType);
250 return fColorTypeToFormatTable[idx];
251 }
252
253 skgpu::Swizzle getWriteSwizzle(const GrBackendFormat&, GrColorType) const override;
254
255 uint64_t computeFormatKey(const GrBackendFormat&) const override;
256
257 int getFragmentUniformBinding() const;
258 int getFragmentUniformSet() const;
259
260 void addExtraSamplerKey(skgpu::KeyBuilder*,
261 GrSamplerState,
262 const GrBackendFormat&) const override;
263
264 GrProgramDesc makeDesc(GrRenderTarget*,
265 const GrProgramInfo&,
266 ProgramDescOverrideFlags) const override;
267
268 GrInternalSurfaceFlags getExtraSurfaceFlagsForDeferredRT() const override;
269
270 VkShaderStageFlags getPushConstantStageFlags() const;
271
272 bool mustLoadFullImageWithDiscardableMSAA() const {
273 return fMustLoadFullImageWithDiscardableMSAA;
274 }
275 bool supportsDiscardableMSAAForDMSAA() const { return fSupportsDiscardableMSAAForDMSAA; }
276 bool renderTargetSupportsDiscardableMSAA(const GrVkRenderTarget*) const;
277 bool programInfoWillUseDiscardableMSAA(const GrProgramInfo&) const;
278
279 bool dmsaaResolveCanBeUsedAsTextureInSameRenderPass() const override { return false; }
280
281 bool supportsMemorylessAttachments() const { return fSupportsMemorylessAttachments; }
282
283 #if defined(GPU_TEST_UTILS)
284 std::vector<GrTest::TestFormatColorTypeCombination> getTestingCombinations() const override;
285 #endif
286
287 private:
288 enum class IntelGPUType {
289 // 9th gen
290 kSkyLake,
291
292 // 11th gen
293 kIceLake,
294 kJasperLake,
295
296 // 12th gen or above
297 kRocketLake,
298 kTigerLake,
299 kAlderLake,
300 kRaptorLake,
301 kAlchemist,
302 kLunarLake,
303 kMeteorLake,
304 kArrowLake,
305 kBattlemage,
306 kPantherLake,
307
308 kOther
309 };
310
311 enum DeviceID {
312 kSwiftshader_DeviceID = 0xC0DE, // As listed in Swiftshader code this may be a placeholder
313 // value but works for now.
314 };
315
316 static IntelGPUType GetIntelGPUType(uint32_t deviceID);
317 static int GetIntelGen(IntelGPUType type) {
318 switch (type) {
319 case IntelGPUType::kSkyLake:
320 return 9;
321 case IntelGPUType::kIceLake: // fall through
322 case IntelGPUType::kJasperLake:
323 return 11;
324 case IntelGPUType::kRocketLake: // fall through
325 case IntelGPUType::kTigerLake: // fall through
326 case IntelGPUType::kAlderLake: // fall through
327 case IntelGPUType::kRaptorLake: // fall through
328 case IntelGPUType::kAlchemist: // fall through
329 case IntelGPUType::kLunarLake: // fall through
330 case IntelGPUType::kMeteorLake: // fall through
331 case IntelGPUType::kArrowLake: // fall through
332 case IntelGPUType::kBattlemage: // fall through
333 case IntelGPUType::kPantherLake:
334 return 12;
335 case IntelGPUType::kOther:
336 // For now all our workaround checks are in the form of "if gen > some_value". So
337 // we can return 0 for kOther which means we won't put in the new workaround for
338 // older gens which is fine. If we stay on top of adding support for new gen
339 // intel devices we shouldn't hit cases where we'd need to change this pattern.
340 return 0;
341 }
342 SkUNREACHABLE;
343 }
344
345 void init(const GrContextOptions&,
346 const skgpu::VulkanInterface*,
347 VkPhysicalDevice,
348 const VkPhysicalDeviceFeatures2&,
349 uint32_t physicalDeviceVersion,
350 const skgpu::VulkanExtensions&,
351 GrProtected);
352 void initGrCaps(const skgpu::VulkanInterface* vkInterface,
353 VkPhysicalDevice physDev,
354 const VkPhysicalDeviceProperties&,
355 const VkPhysicalDeviceMemoryProperties&,
356 const VkPhysicalDeviceFeatures2&,
357 const skgpu::VulkanExtensions&);
358 void initShaderCaps(const VkPhysicalDeviceProperties&, const VkPhysicalDeviceFeatures2&);
359
360 void initFormatTable(const GrContextOptions&,
361 const skgpu::VulkanInterface*,
362 VkPhysicalDevice,
363 const VkPhysicalDeviceProperties&,
364 const VkPhysicalDeviceFeatures2&,
365 const skgpu::VulkanExtensions&);
366 void initStencilFormat(const skgpu::VulkanInterface* iface, VkPhysicalDevice physDev);
367
368 void applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties&);
369
370 bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
371 bool onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect,
372 const GrSurfaceProxy* src, const SkIRect& srcRect) const override;
373 GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
374
375 bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
376
377 SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
378 GrColorType) const override;
379
380 skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override;
381
382 GrDstSampleFlags onGetDstSampleFlagsForProxy(const GrRenderTargetProxy*) const override;
383
384 bool onSupportsDynamicMSAA(const GrRenderTargetProxy*) const override;
385
386 // ColorTypeInfo for a specific format
387 struct ColorTypeInfo {
388 GrColorType fColorType = GrColorType::kUnknown;
389 GrColorType fTransferColorType = GrColorType::kUnknown;
390 enum {
391 kUploadData_Flag = 0x1,
392 // Does Ganesh itself support rendering to this colorType & format pair. Renderability
393 // still additionally depends on if the format itself is renderable.
394 kRenderable_Flag = 0x2,
395 // Indicates that this colorType is supported only if we are wrapping a texture with
396 // the given format and colorType. We do not allow creation with this pair.
397 kWrappedOnly_Flag = 0x4,
398 };
399 uint32_t fFlags = 0;
400
401 skgpu::Swizzle fReadSwizzle;
402 skgpu::Swizzle fWriteSwizzle;
403 };
404
405 struct FormatInfo {
406 uint32_t colorTypeFlags(GrColorType colorType) const {
407 for (int i = 0; i < fColorTypeInfoCount; ++i) {
408 if (fColorTypeInfos[i].fColorType == colorType) {
409 return fColorTypeInfos[i].fFlags;
410 }
411 }
412 return 0;
413 }
414
415 void init(const GrContextOptions&,
416 const skgpu::VulkanInterface*,
417 VkPhysicalDevice,
418 const VkPhysicalDeviceProperties&,
419 VkFormat);
420 static void InitFormatFlags(VkFormatFeatureFlags, uint16_t* flags);
421 void initSampleCounts(const GrContextOptions&,
422 const skgpu::VulkanInterface*,
423 VkPhysicalDevice,
424 const VkPhysicalDeviceProperties&,
425 VkFormat);
426
427 enum {
428 kTexturable_Flag = 0x1,
429 kRenderable_Flag = 0x2,
430 kBlitSrc_Flag = 0x4,
431 kBlitDst_Flag = 0x8,
432 };
433
434 uint16_t fOptimalFlags = 0;
435 uint16_t fLinearFlags = 0;
436
437 SkTDArray<int> fColorSampleCounts;
438
439 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
440 int fColorTypeInfoCount = 0;
441 };
442 static const size_t kNumVkFormats = 25;
443 FormatInfo fFormatTable[kNumVkFormats];
444
445 FormatInfo& getFormatInfo(VkFormat);
446 const FormatInfo& getFormatInfo(VkFormat) const;
447
448 VkFormat fColorTypeToFormatTable[kGrColorTypeCnt];
449 void setColorType(GrColorType, std::initializer_list<VkFormat> formats);
450
451 VkFormat fPreferredStencilFormat;
452
453 skia_private::STArray<1, skgpu::VulkanYcbcrConversionInfo> fYcbcrInfos;
454
455 bool fMustSyncCommandBuffersWithQueue = false;
456 bool fShouldAlwaysUseDedicatedImageMemory = false;
457
458 bool fAvoidUpdateBuffers = false;
459
460 bool fSupportsSwapchain = false;
461
462 bool fSupportsAndroidHWBExternalMemory = false;
463
464 bool fSupportsYcbcrConversion = false;
465
466 bool fSupportsDRMFormatModifiers = false;
467
468 bool fSupportsDeviceFaultInfo = false;
469
470 bool fSupportsFrameBoundary = false;
471
472 bool fSupportsPipelineCreationCacheControl = false;
473
474 bool fPreferPrimaryOverSecondaryCommandBuffers = true;
475 bool fMustInvalidatePrimaryCmdBufferStateAfterClearAttachments = false;
476
477 bool fGpuOnlyBuffersMorePerformant = false;
478 bool fShouldPersistentlyMapCpuToGpuBuffers = true;
479
480 // We default this to 100 since we already cap the max render tasks at 100 before doing a
481 // submission in the GrDrawingManager, so we shouldn't be going over 100 secondary command
482 // buffers per primary anyways.
483 int fMaxPerPoolCachedSecondaryCommandBuffers = 100;
484
485 uint32_t fMaxInputAttachmentDescriptors = 0;
486
487 float fMaxSamplerAnisotropy = 1.f;
488
489 bool fMustLoadFullImageWithDiscardableMSAA = false;
490 bool fSupportsDiscardableMSAAForDMSAA = true;
491 bool fSupportsMemorylessAttachments = false;
492
493 uint32_t fMaxDrawIndirectDrawCount = 0;
494
495 using INHERITED = GrCaps;
496 };
497
498 #endif