blob: 8503bea5d69a4ecc503cec6de23d2c717605f90d [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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#include "GrVkCaps.h"
Greg Danielf5d87582017-12-18 14:48:15 -05009#include "GrBackendSurface.h"
Robert Phillipsbf25d432017-04-07 10:08:53 -040010#include "GrRenderTargetProxy.h"
Brian Salomon3d86a192018-02-27 16:46:11 -050011#include "GrRenderTarget.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050012#include "GrShaderCaps.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrVkUtil.h"
jvanverthfd7bd452016-03-25 06:29:52 -070014#include "vk/GrVkBackendContext.h"
Brian Salomon467921e2017-03-06 16:17:12 -050015#include "vk/GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016
17GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Danieldc13c212018-06-28 23:29:35 +000018 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags)
egdanielc5ec1402016-03-28 12:14:42 -070019 : INHERITED(contextOptions) {
Greg Danieldc13c212018-06-28 23:29:35 +000020 fCanUseGLSLForShaderModule = false;
egdaniel6fa0a912016-09-12 11:51:29 -070021 fMustDoCopiesFromOrigin = false;
egdanielfd016d72016-09-27 12:13:05 -070022 fMustSubmitCommandsBeforeCopyOp = false;
Greg Daniel80a08dd2017-01-20 10:45:49 -050023 fMustSleepOnTearDown = false;
Greg Daniele3cd6912017-05-17 11:15:55 -040024 fNewCBOnPipelineChange = false;
Greg Danielddc0c602018-06-18 11:26:30 -040025 fShouldAlwaysUseDedicatedImageMemory = false;
egdanielc5ec1402016-03-28 12:14:42 -070026
Greg Daniel164a9f02016-02-22 09:56:40 -050027 /**************************************************************************
28 * GrDrawTargetCaps fields
29 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070030 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070031 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070032 fNPOTTextureTileSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070033 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050034 fReuseScratchTextures = true; //TODO: figure this out
35 fGpuTracingSupport = false; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050036 fOversizedStencilSupport = false; //TODO: figure this out
Chris Dalton1d616352017-05-31 12:51:23 -060037 fInstanceAttribSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050038
Greg Daniel0fe4ed82018-03-15 12:27:29 +000039 fBlacklistCoverageCounting = true; // blacklisting ccpr until we work through a few issues.
jvanverth84741b32016-09-30 08:39:02 -070040 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel691f5e72018-02-28 14:21:34 -050041 fCrossContextTextureSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050042
43 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070044 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050045
46 fMaxRenderTargetSize = 4096; // minimum required by spec
47 fMaxTextureSize = 4096; // minimum required by spec
Greg Daniel164a9f02016-02-22 09:56:40 -050048
Brian Salomon94efbf52016-11-29 13:43:05 -050049 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050050
Greg Danieldc13c212018-06-28 23:29:35 +000051 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050052}
53
Robert Phillipsbf25d432017-04-07 10:08:53 -040054bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050055 GrSurfaceOrigin* origin, bool* rectsMustMatch,
56 bool* disallowSubrect) const {
Eric Karl74480882017-04-03 14:49:05 -070057 // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
58 *rectsMustMatch = false;
59 *disallowSubrect = false;
60
Brian Salomon467921e2017-03-06 16:17:12 -050061 // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
62 // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
63 // render target as well.
Brian Salomon2a4f9832018-03-03 22:43:43 -050064 *origin = src->origin();
Brian Salomon467921e2017-03-06 16:17:12 -050065 desc->fConfig = src->config();
Greg Daniel55fa6472018-03-16 16:13:10 -040066 if (src->numColorSamples() > 1 || src->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -050067 desc->fFlags = kRenderTarget_GrSurfaceFlag;
68 } else {
69 // Just going to use CopyImage here
70 desc->fFlags = kNone_GrSurfaceFlags;
71 }
72
73 return true;
74}
75
Greg Daniel25af6712018-04-25 10:44:38 -040076bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, GrSurfaceOrigin dstOrigin,
77 GrPixelConfig srcConfig, int srcSampleCnt,
78 GrSurfaceOrigin srcOrigin) const {
79 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
80 return false;
81 }
82
83 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
84 // as image usage flags.
85 if (srcOrigin != dstOrigin || GrBytesPerPixel(srcConfig) != GrBytesPerPixel(dstConfig)) {
86 return false;
87 }
88
89 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
90 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
91 return false;
92 }
93
94 return true;
95}
96
97bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
98 GrPixelConfig srcConfig, int srcSampleCnt, bool srcIsLinear) const {
99 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
100 // as image usage flags.
101 if (!this->configCanBeDstofBlit(dstConfig, dstIsLinear) ||
102 !this->configCanBeSrcofBlit(srcConfig, srcIsLinear)) {
103 return false;
104 }
105
106 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
107 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
108 return false;
109 }
110
111 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
112 // resolved msaa though.
113 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
114 return false;
115 }
116
117 return true;
118}
119
120bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt,
121 GrSurfaceOrigin dstOrigin, GrPixelConfig srcConfig,
122 int srcSampleCnt, GrSurfaceOrigin srcOrigin) const {
123 // The src surface must be multisampled.
124 if (srcSampleCnt <= 1) {
125 return false;
126 }
127
128 // The dst must not be multisampled.
129 if (dstSampleCnt > 1) {
130 return false;
131 }
132
133 // Surfaces must have the same format.
134 if (dstConfig != srcConfig) {
135 return false;
136 }
137
138 // Surfaces must have the same origin.
139 if (srcOrigin != dstOrigin) {
140 return false;
141 }
142
143 return true;
144}
145
146bool GrVkCaps::canCopyAsDraw(GrPixelConfig dstConfig, bool dstIsRenderable,
147 GrPixelConfig srcConfig, bool srcIsTextureable) const {
148 // TODO: Make copySurfaceAsDraw handle the swizzle
149 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
150 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
151 return false;
152 }
153
154 // Make sure the dst is a render target and the src is a texture.
155 if (!dstIsRenderable || !srcIsTextureable) {
156 return false;
157 }
158
159 return true;
160}
161
162bool GrVkCaps::canCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
163 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
164 GrSurfaceOrigin dstOrigin = dst->origin();
165 GrSurfaceOrigin srcOrigin = src->origin();
166
167 GrPixelConfig dstConfig = dst->config();
168 GrPixelConfig srcConfig = src->config();
169
170 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
171 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
172 // tracking of external or rectangle textures in GL? For now we don't create linear textures
173 // internally, and I don't believe anyone is wrapping them.
174 bool srcIsLinear = false;
175 bool dstIsLinear = false;
176
177 int dstSampleCnt = 0;
178 int srcSampleCnt = 0;
179 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
180 dstSampleCnt = rtProxy->numColorSamples();
181 }
182 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
183 srcSampleCnt = rtProxy->numColorSamples();
184 }
185 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
186 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
187
188 return this->canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
189 srcConfig, srcSampleCnt, srcOrigin) ||
190 this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear,
191 srcConfig, srcSampleCnt, srcIsLinear) ||
192 this->canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
193 srcConfig, srcSampleCnt, srcOrigin) ||
194 this->canCopyAsDraw(dstConfig, dstSampleCnt > 0,
195 srcConfig, SkToBool(src->asTextureProxy()));
196}
197
Greg Daniel164a9f02016-02-22 09:56:40 -0500198void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Danieldc13c212018-06-28 23:29:35 +0000199 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500200
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000201 VkPhysicalDeviceProperties properties;
202 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -0800203
egdanield5e3b9e2016-03-08 12:19:54 -0800204 VkPhysicalDeviceMemoryProperties memoryProperties;
205 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
206
jvanverthfd7bd452016-03-25 06:29:52 -0700207 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500208 this->initShaderCaps(properties, featureFlags);
Greg Danielf3b11622018-03-01 15:01:27 -0500209
210 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
211#if defined(SK_CPU_X86)
212 // We need to do this before initing the config table since it uses fSRGBSupport
213 if (kImagination_VkVendor == properties.vendorID) {
214 fSRGBSupport = false;
215 }
216#endif
217 }
218
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000219 this->initConfigTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700220 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -0500221
Greg Daniel691f5e72018-02-28 14:21:34 -0500222 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
223 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -0700224 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500225
Greg Danielddc0c602018-06-18 11:26:30 -0400226 // On nexus player we disable suballocating VkImage memory since we've seen large slow downs on
227 // bot run times.
228 if (kImagination_VkVendor == properties.vendorID) {
229 fShouldAlwaysUseDedicatedImageMemory = true;
230 }
231
Greg Daniel691f5e72018-02-28 14:21:34 -0500232 this->applyOptionsOverrides(contextOptions);
233 fShaderCaps->applyOptionsOverrides(contextOptions);
234}
235
236void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700237 if (kQualcomm_VkVendor == properties.vendorID) {
238 fMustDoCopiesFromOrigin = true;
239 }
240
egdanielfd016d72016-09-27 12:13:05 -0700241 if (kNvidia_VkVendor == properties.vendorID) {
egdanielfd016d72016-09-27 12:13:05 -0700242 fMustSubmitCommandsBeforeCopyOp = true;
243 }
244
Greg Daniel80a08dd2017-01-20 10:45:49 -0500245#if defined(SK_BUILD_FOR_WIN)
246 if (kNvidia_VkVendor == properties.vendorID) {
247 fMustSleepOnTearDown = true;
248 }
249#elif defined(SK_BUILD_FOR_ANDROID)
250 if (kImagination_VkVendor == properties.vendorID) {
251 fMustSleepOnTearDown = true;
252 }
253#endif
Greg Danielbce5eb92018-03-01 13:13:44 -0500254
255 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
256 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
257 if (kAMD_VkVendor == properties.vendorID) {
258 fNewCBOnPipelineChange = true;
259 }
260
Greg Danielddc0c602018-06-18 11:26:30 -0400261 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
262 if (kARM_VkVendor == properties.vendorID) {
263 fShouldAlwaysUseDedicatedImageMemory = true;
264 }
265
Greg Danielbce5eb92018-03-01 13:13:44 -0500266 ////////////////////////////////////////////////////////////////////////////
267 // GrCaps workarounds
268 ////////////////////////////////////////////////////////////////////////////
269
270 if (kARM_VkVendor == properties.vendorID) {
271 fInstanceAttribSupport = false;
272 }
273
274 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
275 if (kAMD_VkVendor == properties.vendorID) {
276 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
277 }
278
Greg Danielbce5eb92018-03-01 13:13:44 -0500279 ////////////////////////////////////////////////////////////////////////////
280 // GrShaderCaps workarounds
281 ////////////////////////////////////////////////////////////////////////////
282
Greg Danielbce5eb92018-03-01 13:13:44 -0500283 if (kImagination_VkVendor == properties.vendorID) {
284 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
285 }
286
Greg Daniel164a9f02016-02-22 09:56:40 -0500287}
288
289int get_max_sample_count(VkSampleCountFlags flags) {
290 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
291 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
292 return 0;
293 }
294 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
295 return 2;
296 }
297 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
298 return 4;
299 }
300 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
301 return 8;
302 }
303 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
304 return 16;
305 }
306 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
307 return 32;
308 }
309 return 64;
310}
311
egdanield5e3b9e2016-03-08 12:19:54 -0800312void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700313 const VkPhysicalDeviceMemoryProperties& memoryProperties,
314 uint32_t featureFlags) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400315 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
316 // need for us ever to support that amount, and it makes tests which tests all the vertex
317 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
318 // we ever find that need.
319 static const uint32_t kMaxVertexAttributes = 64;
320 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
Greg Danielc5cc2de2017-03-20 11:40:58 -0400321
egdanield5e3b9e2016-03-08 12:19:54 -0800322 // We could actually query and get a max size for each config, however maxImageDimension2D will
323 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700324 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
325 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
Adrienne Walker724afe82018-05-15 11:36:26 -0700326 if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
327 fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
328 }
329 // Our render targets are always created with textures as the color
330 // attachment, hence this min:
331 fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
egdanield5e3b9e2016-03-08 12:19:54 -0800332
Chris Dalton2612bae2018-02-22 13:41:37 -0700333 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
334 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
335
egdanield5e3b9e2016-03-08 12:19:54 -0800336 // Assuming since we will always map in the end to upload the data we might as well just map
337 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700338 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800339
340 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
341
egdanield5e3b9e2016-03-08 12:19:54 -0800342 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700343 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
Greg Daniel22bc8652017-03-22 15:45:43 -0400344
Greg Daniel01907872017-05-23 15:21:02 -0400345
egdanield5e3b9e2016-03-08 12:19:54 -0800346}
347
Brian Salomon1edc5b92016-11-29 13:43:46 -0500348void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
349 GrShaderCaps* shaderCaps = fShaderCaps.get();
350 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700351
Greg Daniel164a9f02016-02-22 09:56:40 -0500352
353 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
354 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
355 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500356 // Vulkan doesn't support a single channel format stored in alpha.
357 if (GrPixelConfigIsAlphaOnly(config) &&
358 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500359 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
360 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500361 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500362 if (kGray_8_GrPixelConfig == config ||
363 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500364 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
365 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700366 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
367 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
368 // or writing to outputs. Since we're not actually changing the data at all, the
369 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500370 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
371 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700372 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500373 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700374 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500375 }
376 }
egdaniel67acb832016-02-26 08:32:20 -0800377
egdanield5e3b9e2016-03-08 12:19:54 -0800378 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500379 shaderCaps->fUsesPrecisionModifiers = true;
380 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700381 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
382 // to be true with Vulkan as well.
383 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800384
385 // GrShaderCaps
386
Brian Salomon1edc5b92016-11-29 13:43:46 -0500387 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600388
Brian Salomon1edc5b92016-11-29 13:43:46 -0500389 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600390 shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
egdanield632bb42016-03-30 12:06:48 -0700391
Brian Salomon1edc5b92016-11-29 13:43:46 -0500392 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700393
Brian Salomon1edc5b92016-11-29 13:43:46 -0500394 shaderCaps->fIntegerSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600395 shaderCaps->fVertexIDSupport = true;
Chris Dalton7c7ff032018-03-28 20:09:58 -0600396 shaderCaps->fFPManipulationSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800397
cdaltona6b92ad2016-04-11 12:03:08 -0700398 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700399 shaderCaps->fFloatIs32Bits = true;
400 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700401
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400402 // SPIR-V supports unsigned integers.
403 shaderCaps->fUnsignedSupport = true;
404
Brian Salomon1edc5b92016-11-29 13:43:46 -0500405 shaderCaps->fMaxVertexSamplers =
406 shaderCaps->fMaxGeometrySamplers =
407 shaderCaps->fMaxFragmentSamplers = SkTMin(
408 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
409 properties.limits.maxPerStageDescriptorSamplers),
410 (uint32_t)INT_MAX);
411 shaderCaps->fMaxCombinedSamplers = SkTMin(
412 SkTMin(properties.limits.maxDescriptorSetSampledImages,
413 properties.limits.maxDescriptorSetSamplers),
414 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500415}
416
egdaniel8f1dcaa2016-04-01 10:10:45 -0700417bool stencil_format_supported(const GrVkInterface* interface,
418 VkPhysicalDevice physDev,
419 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500420 VkFormatProperties props;
421 memset(&props, 0, sizeof(VkFormatProperties));
422 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700423 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500424}
425
egdaniel8f1dcaa2016-04-01 10:10:45 -0700426void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
427 // List of legal stencil formats (though perhaps not supported on
428 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700429 // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
egdaniel8f1dcaa2016-04-01 10:10:45 -0700430 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500431 static const StencilFormat
432 // internal Format stencil bits total bits packed?
433 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700434 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
435 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500436
egdaniel8f1dcaa2016-04-01 10:10:45 -0700437 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
438 fPreferedStencilFormat = gS8;
439 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
440 fPreferedStencilFormat = gD24S8;
441 } else {
442 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
443 fPreferedStencilFormat = gD32S8;
444 }
445}
446
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000447void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
448 const VkPhysicalDeviceProperties& properties) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700449 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
450 VkFormat format;
451 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
Greg Daniel01907872017-05-23 15:21:02 -0400452 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000453 fConfigTable[i].init(interface, physDev, properties, format);
Greg Daniel01907872017-05-23 15:21:02 -0400454 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700455 }
456 }
457}
458
459void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
460 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
461 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
462 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700463
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400464 // Ganesh assumes that all renderable surfaces are also texturable
465 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
466 *flags = *flags | kRenderable_Flag;
467 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700468 }
469
470 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
471 *flags = *flags | kBlitSrc_Flag;
472 }
473
474 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
475 *flags = *flags | kBlitDst_Flag;
476 }
477}
478
Greg Daniel81e7bf82017-07-19 14:47:42 -0400479void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
480 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000481 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400482 VkFormat format) {
483 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
484 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
485 VK_IMAGE_USAGE_SAMPLED_BIT |
486 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400487 VkImageFormatProperties properties;
488 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
489 format,
490 VK_IMAGE_TYPE_2D,
491 VK_IMAGE_TILING_OPTIMAL,
492 usage,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400493 0, // createFlags
Greg Daniel81e7bf82017-07-19 14:47:42 -0400494 &properties));
495 VkSampleCountFlags flags = properties.sampleCounts;
496 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500497 fColorSampleCounts.push(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400498 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000499 if (kImagination_VkVendor == physProps.vendorID) {
500 // MSAA does not work on imagination
501 return;
502 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400503 if (flags & VK_SAMPLE_COUNT_2_BIT) {
504 fColorSampleCounts.push(2);
505 }
506 if (flags & VK_SAMPLE_COUNT_4_BIT) {
507 fColorSampleCounts.push(4);
508 }
509 if (flags & VK_SAMPLE_COUNT_8_BIT) {
510 fColorSampleCounts.push(8);
511 }
512 if (flags & VK_SAMPLE_COUNT_16_BIT) {
513 fColorSampleCounts.push(16);
514 }
515 if (flags & VK_SAMPLE_COUNT_32_BIT) {
516 fColorSampleCounts.push(32);
517 }
518 if (flags & VK_SAMPLE_COUNT_64_BIT) {
519 fColorSampleCounts.push(64);
520 }
521}
522
egdaniel8f1dcaa2016-04-01 10:10:45 -0700523void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
524 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000525 const VkPhysicalDeviceProperties& properties,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700526 VkFormat format) {
527 VkFormatProperties props;
528 memset(&props, 0, sizeof(VkFormatProperties));
529 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
530 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
531 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400532 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000533 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400534 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500535}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400536
Brian Salomonbdecacf2018-02-02 20:32:49 -0500537int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
538 requestedCount = SkTMax(1, requestedCount);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400539 int count = fConfigTable[config].fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500540
541 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400542 return 0;
543 }
544
Brian Salomonbdecacf2018-02-02 20:32:49 -0500545 if (1 == requestedCount) {
546 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
547 fConfigTable[config].fColorSampleCounts[0] == 1);
548 return 1;
549 }
550
Greg Daniel81e7bf82017-07-19 14:47:42 -0400551 for (int i = 0; i < count; ++i) {
552 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
553 return fConfigTable[config].fColorSampleCounts[i];
554 }
555 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500556 return 0;
557}
558
559int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
560 const auto& table = fConfigTable[config].fColorSampleCounts;
561 if (!table.count()) {
562 return 0;
563 }
564 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500565}
566
Brian Salomon3d86a192018-02-27 16:46:11 -0500567bool GrVkCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
568 if (auto rt = surface->asRenderTarget()) {
569 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
570 }
571 return true;
572}
573
Robert Phillipsfc711a22018-02-13 17:03:00 -0500574bool validate_image_info(VkFormat format, SkColorType ct, GrPixelConfig* config) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500575 *config = kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500576
577 switch (ct) {
578 case kUnknown_SkColorType:
579 return false;
580 case kAlpha_8_SkColorType:
581 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500582 *config = kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500583 }
584 break;
585 case kRGB_565_SkColorType:
586 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500587 *config = kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500588 }
589 break;
590 case kARGB_4444_SkColorType:
591 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500592 *config = kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500593 }
594 break;
595 case kRGBA_8888_SkColorType:
596 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500597 *config = kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500598 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500599 *config = kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500600 }
601 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500602 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400603 // TODO: VK_FORMAT_R8G8B8_UNORM
Brian Salomone41e1762018-01-25 14:07:47 -0500604 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500605 case kBGRA_8888_SkColorType:
606 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500607 *config = kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500608 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500609 *config = kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500610 }
611 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500612 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500613 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
614 *config = kRGBA_1010102_GrPixelConfig;
615 }
616 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500617 case kRGB_101010x_SkColorType:
618 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500619 case kGray_8_SkColorType:
620 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500621 *config = kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500622 }
623 break;
624 case kRGBA_F16_SkColorType:
625 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500626 *config = kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500627 }
628 break;
Mike Klein37854712018-06-26 11:43:06 -0400629 case kRGBA_F32_SkColorType:
630 if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
631 *config = kRGBA_float_GrPixelConfig;
632 }
633 break;
Greg Danielf5d87582017-12-18 14:48:15 -0500634 }
635
Greg Danielfaa095e2017-12-19 13:15:02 -0500636 return kUnknown_GrPixelConfig != *config;
Greg Danielf5d87582017-12-18 14:48:15 -0500637}
638
Greg Danielfaa095e2017-12-19 13:15:02 -0500639bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
640 GrPixelConfig* config) const {
Greg Daniel52e16d92018-04-10 09:34:07 -0400641 GrVkImageInfo imageInfo;
642 if (!tex.getVkImageInfo(&imageInfo)) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500643 return false;
644 }
645
Greg Daniel52e16d92018-04-10 09:34:07 -0400646 return validate_image_info(imageInfo.fFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500647}
648
649bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
650 GrPixelConfig* config) const {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400651 GrVkImageInfo imageInfo;
652 if (!rt.getVkImageInfo(&imageInfo)) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500653 return false;
654 }
655
Greg Daniel323fbcf2018-04-10 13:46:30 -0400656 return validate_image_info(imageInfo.fFormat, ct, config);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500657}
658
659bool GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
660 GrPixelConfig* config) const {
661 const VkFormat* vkFormat = format.getVkFormat();
662 if (!vkFormat) {
663 return false;
664 }
665 return validate_image_info(*vkFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500666}
Greg Danielf5d87582017-12-18 14:48:15 -0500667
Timothy Liang036fdfe2018-06-28 15:50:36 -0400668#ifdef GR_TEST_UTILS
669GrBackendFormat GrVkCaps::onCreateFormatFromBackendTexture(
670 const GrBackendTexture& backendTex) const {
671 GrVkImageInfo vkInfo;
672 SkAssertResult(backendTex.getVkImageInfo(&vkInfo));
673 return GrBackendFormat::MakeVk(vkInfo.fFormat);
674}
675#endif
676