blob: 1c36a1494cd5ffb3a75d90a4487580d172aa8492 [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,
egdanielc5ec1402016-03-28 12:14:42 -070018 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags)
19 : INHERITED(contextOptions) {
20 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 Daniel8385a8a2018-02-26 13:29:37 -050025 fCanUseWholeSizeOnFlushMappedMemory = true;
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
Brian Osman57bc3ea2017-07-27 09:58:11 -040032 fSRGBDecodeDisableSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070033 fNPOTTextureTileSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070034 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050035 fReuseScratchTextures = true; //TODO: figure this out
36 fGpuTracingSupport = false; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050037 fOversizedStencilSupport = false; //TODO: figure this out
Chris Dalton1d616352017-05-31 12:51:23 -060038 fInstanceAttribSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050039
Greg Daniel0fe4ed82018-03-15 12:27:29 +000040 fBlacklistCoverageCounting = true; // blacklisting ccpr until we work through a few issues.
jvanverth84741b32016-09-30 08:39:02 -070041 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel691f5e72018-02-28 14:21:34 -050042 fCrossContextTextureSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050043
44 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070045 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050046
47 fMaxRenderTargetSize = 4096; // minimum required by spec
48 fMaxTextureSize = 4096; // minimum required by spec
Greg Daniel164a9f02016-02-22 09:56:40 -050049
Brian Salomon94efbf52016-11-29 13:43:05 -050050 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050051
egdanielc5ec1402016-03-28 12:14:42 -070052 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050053}
54
Robert Phillipsbf25d432017-04-07 10:08:53 -040055bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050056 GrSurfaceOrigin* origin, bool* rectsMustMatch,
57 bool* disallowSubrect) const {
Eric Karl74480882017-04-03 14:49:05 -070058 // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
59 *rectsMustMatch = false;
60 *disallowSubrect = false;
61
Brian Salomon467921e2017-03-06 16:17:12 -050062 // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
63 // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
64 // render target as well.
Brian Salomon2a4f9832018-03-03 22:43:43 -050065 *origin = src->origin();
Brian Salomon467921e2017-03-06 16:17:12 -050066 desc->fConfig = src->config();
Greg Daniel55fa6472018-03-16 16:13:10 -040067 if (src->numColorSamples() > 1 || src->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -050068 desc->fFlags = kRenderTarget_GrSurfaceFlag;
69 } else {
70 // Just going to use CopyImage here
71 desc->fFlags = kNone_GrSurfaceFlags;
72 }
73
74 return true;
75}
76
Greg Daniel25af6712018-04-25 10:44:38 -040077bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, GrSurfaceOrigin dstOrigin,
78 GrPixelConfig srcConfig, int srcSampleCnt,
79 GrSurfaceOrigin srcOrigin) const {
80 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
81 return false;
82 }
83
84 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
85 // as image usage flags.
86 if (srcOrigin != dstOrigin || GrBytesPerPixel(srcConfig) != GrBytesPerPixel(dstConfig)) {
87 return false;
88 }
89
90 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
91 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
92 return false;
93 }
94
95 return true;
96}
97
98bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
99 GrPixelConfig srcConfig, int srcSampleCnt, bool srcIsLinear) const {
100 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
101 // as image usage flags.
102 if (!this->configCanBeDstofBlit(dstConfig, dstIsLinear) ||
103 !this->configCanBeSrcofBlit(srcConfig, srcIsLinear)) {
104 return false;
105 }
106
107 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
108 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
109 return false;
110 }
111
112 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
113 // resolved msaa though.
114 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
115 return false;
116 }
117
118 return true;
119}
120
121bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt,
122 GrSurfaceOrigin dstOrigin, GrPixelConfig srcConfig,
123 int srcSampleCnt, GrSurfaceOrigin srcOrigin) const {
124 // The src surface must be multisampled.
125 if (srcSampleCnt <= 1) {
126 return false;
127 }
128
129 // The dst must not be multisampled.
130 if (dstSampleCnt > 1) {
131 return false;
132 }
133
134 // Surfaces must have the same format.
135 if (dstConfig != srcConfig) {
136 return false;
137 }
138
139 // Surfaces must have the same origin.
140 if (srcOrigin != dstOrigin) {
141 return false;
142 }
143
144 return true;
145}
146
147bool GrVkCaps::canCopyAsDraw(GrPixelConfig dstConfig, bool dstIsRenderable,
148 GrPixelConfig srcConfig, bool srcIsTextureable) const {
149 // TODO: Make copySurfaceAsDraw handle the swizzle
150 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
151 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
152 return false;
153 }
154
155 // Make sure the dst is a render target and the src is a texture.
156 if (!dstIsRenderable || !srcIsTextureable) {
157 return false;
158 }
159
160 return true;
161}
162
163bool GrVkCaps::canCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
164 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
165 GrSurfaceOrigin dstOrigin = dst->origin();
166 GrSurfaceOrigin srcOrigin = src->origin();
167
168 GrPixelConfig dstConfig = dst->config();
169 GrPixelConfig srcConfig = src->config();
170
171 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
172 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
173 // tracking of external or rectangle textures in GL? For now we don't create linear textures
174 // internally, and I don't believe anyone is wrapping them.
175 bool srcIsLinear = false;
176 bool dstIsLinear = false;
177
178 int dstSampleCnt = 0;
179 int srcSampleCnt = 0;
180 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
181 dstSampleCnt = rtProxy->numColorSamples();
182 }
183 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
184 srcSampleCnt = rtProxy->numColorSamples();
185 }
186 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
187 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
188
189 return this->canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
190 srcConfig, srcSampleCnt, srcOrigin) ||
191 this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear,
192 srcConfig, srcSampleCnt, srcIsLinear) ||
193 this->canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
194 srcConfig, srcSampleCnt, srcOrigin) ||
195 this->canCopyAsDraw(dstConfig, dstSampleCnt > 0,
196 srcConfig, SkToBool(src->asTextureProxy()));
197}
198
Greg Daniel164a9f02016-02-22 09:56:40 -0500199void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -0700200 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500201
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000202 VkPhysicalDeviceProperties properties;
203 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -0800204
egdanield5e3b9e2016-03-08 12:19:54 -0800205 VkPhysicalDeviceMemoryProperties memoryProperties;
206 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
207
jvanverthfd7bd452016-03-25 06:29:52 -0700208 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500209 this->initShaderCaps(properties, featureFlags);
Greg Danielf3b11622018-03-01 15:01:27 -0500210
211 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
212#if defined(SK_CPU_X86)
213 // We need to do this before initing the config table since it uses fSRGBSupport
214 if (kImagination_VkVendor == properties.vendorID) {
215 fSRGBSupport = false;
216 }
217#endif
218 }
219
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000220 this->initConfigTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700221 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -0500222
Greg Daniel691f5e72018-02-28 14:21:34 -0500223 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
224 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -0700225 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500226
Greg Daniel691f5e72018-02-28 14:21:34 -0500227 this->applyOptionsOverrides(contextOptions);
228 fShaderCaps->applyOptionsOverrides(contextOptions);
229}
230
231void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700232 if (kQualcomm_VkVendor == properties.vendorID) {
233 fMustDoCopiesFromOrigin = true;
234 }
235
egdanielfd016d72016-09-27 12:13:05 -0700236 if (kNvidia_VkVendor == properties.vendorID) {
egdanielfd016d72016-09-27 12:13:05 -0700237 fMustSubmitCommandsBeforeCopyOp = true;
238 }
239
Greg Daniel80a08dd2017-01-20 10:45:49 -0500240#if defined(SK_BUILD_FOR_WIN)
241 if (kNvidia_VkVendor == properties.vendorID) {
242 fMustSleepOnTearDown = true;
243 }
244#elif defined(SK_BUILD_FOR_ANDROID)
245 if (kImagination_VkVendor == properties.vendorID) {
246 fMustSleepOnTearDown = true;
247 }
248#endif
Greg Danielbce5eb92018-03-01 13:13:44 -0500249
250 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
251 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
252 if (kAMD_VkVendor == properties.vendorID) {
253 fNewCBOnPipelineChange = true;
254 }
255
256 ////////////////////////////////////////////////////////////////////////////
257 // GrCaps workarounds
258 ////////////////////////////////////////////////////////////////////////////
259
260 if (kARM_VkVendor == properties.vendorID) {
261 fInstanceAttribSupport = false;
262 }
263
264 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
265 if (kAMD_VkVendor == properties.vendorID) {
266 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
267 }
268
Chris Daltondf1681e2018-03-29 23:16:55 +0000269 if (kIntel_VkVendor == properties.vendorID) {
270 fCanUseWholeSizeOnFlushMappedMemory = false;
Greg Danielbce5eb92018-03-01 13:13:44 -0500271 }
272
Greg Danielbce5eb92018-03-01 13:13:44 -0500273 ////////////////////////////////////////////////////////////////////////////
274 // GrShaderCaps workarounds
275 ////////////////////////////////////////////////////////////////////////////
276
Greg Danielbce5eb92018-03-01 13:13:44 -0500277 if (kImagination_VkVendor == properties.vendorID) {
278 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
279 }
280
Greg Daniel164a9f02016-02-22 09:56:40 -0500281}
282
283int get_max_sample_count(VkSampleCountFlags flags) {
284 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
285 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
286 return 0;
287 }
288 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
289 return 2;
290 }
291 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
292 return 4;
293 }
294 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
295 return 8;
296 }
297 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
298 return 16;
299 }
300 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
301 return 32;
302 }
303 return 64;
304}
305
egdanield5e3b9e2016-03-08 12:19:54 -0800306void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700307 const VkPhysicalDeviceMemoryProperties& memoryProperties,
308 uint32_t featureFlags) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400309 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
310 // need for us ever to support that amount, and it makes tests which tests all the vertex
311 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
312 // we ever find that need.
313 static const uint32_t kMaxVertexAttributes = 64;
314 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
Greg Danielc5cc2de2017-03-20 11:40:58 -0400315
egdanield5e3b9e2016-03-08 12:19:54 -0800316 // We could actually query and get a max size for each config, however maxImageDimension2D will
317 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700318 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
319 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800320
Chris Dalton2612bae2018-02-22 13:41:37 -0700321 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
322 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
323
egdanield5e3b9e2016-03-08 12:19:54 -0800324 // Assuming since we will always map in the end to upload the data we might as well just map
325 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700326 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800327
328 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
329
egdanield5e3b9e2016-03-08 12:19:54 -0800330 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700331 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
Greg Daniel22bc8652017-03-22 15:45:43 -0400332
Greg Daniel01907872017-05-23 15:21:02 -0400333
egdanield5e3b9e2016-03-08 12:19:54 -0800334}
335
Brian Salomon1edc5b92016-11-29 13:43:46 -0500336void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
337 GrShaderCaps* shaderCaps = fShaderCaps.get();
338 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700339
Greg Daniel164a9f02016-02-22 09:56:40 -0500340
341 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
342 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
343 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500344 // Vulkan doesn't support a single channel format stored in alpha.
345 if (GrPixelConfigIsAlphaOnly(config) &&
346 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500347 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
348 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500349 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500350 if (kGray_8_GrPixelConfig == config ||
351 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500352 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
353 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700354 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
355 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
356 // or writing to outputs. Since we're not actually changing the data at all, the
357 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500358 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
359 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700360 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500361 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700362 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500363 }
364 }
egdaniel67acb832016-02-26 08:32:20 -0800365
egdanield5e3b9e2016-03-08 12:19:54 -0800366 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500367 shaderCaps->fUsesPrecisionModifiers = true;
368 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700369 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
370 // to be true with Vulkan as well.
371 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800372
373 // GrShaderCaps
374
Brian Salomon1edc5b92016-11-29 13:43:46 -0500375 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600376
Brian Salomon1edc5b92016-11-29 13:43:46 -0500377 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600378 shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
egdanield632bb42016-03-30 12:06:48 -0700379
Brian Salomon1edc5b92016-11-29 13:43:46 -0500380 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700381
Brian Salomon1edc5b92016-11-29 13:43:46 -0500382 shaderCaps->fIntegerSupport = true;
Chris Dalton6b65b982017-07-06 11:04:00 -0600383 shaderCaps->fTexelBufferSupport = true;
384 shaderCaps->fTexelFetchSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600385 shaderCaps->fVertexIDSupport = true;
Chris Dalton7c7ff032018-03-28 20:09:58 -0600386 shaderCaps->fFPManipulationSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800387
cdaltona6b92ad2016-04-11 12:03:08 -0700388 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700389 shaderCaps->fFloatIs32Bits = true;
390 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700391
Brian Salomon1edc5b92016-11-29 13:43:46 -0500392 shaderCaps->fMaxVertexSamplers =
393 shaderCaps->fMaxGeometrySamplers =
394 shaderCaps->fMaxFragmentSamplers = SkTMin(
395 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
396 properties.limits.maxPerStageDescriptorSamplers),
397 (uint32_t)INT_MAX);
398 shaderCaps->fMaxCombinedSamplers = SkTMin(
399 SkTMin(properties.limits.maxDescriptorSetSampledImages,
400 properties.limits.maxDescriptorSetSamplers),
401 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500402}
403
egdaniel8f1dcaa2016-04-01 10:10:45 -0700404bool stencil_format_supported(const GrVkInterface* interface,
405 VkPhysicalDevice physDev,
406 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500407 VkFormatProperties props;
408 memset(&props, 0, sizeof(VkFormatProperties));
409 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700410 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500411}
412
egdaniel8f1dcaa2016-04-01 10:10:45 -0700413void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
414 // List of legal stencil formats (though perhaps not supported on
415 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700416 // 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 -0700417 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500418 static const StencilFormat
419 // internal Format stencil bits total bits packed?
420 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700421 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
422 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500423
egdaniel8f1dcaa2016-04-01 10:10:45 -0700424 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
425 fPreferedStencilFormat = gS8;
426 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
427 fPreferedStencilFormat = gD24S8;
428 } else {
429 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
430 fPreferedStencilFormat = gD32S8;
431 }
432}
433
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000434void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
435 const VkPhysicalDeviceProperties& properties) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700436 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
437 VkFormat format;
438 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
Greg Daniel01907872017-05-23 15:21:02 -0400439 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000440 fConfigTable[i].init(interface, physDev, properties, format);
Greg Daniel01907872017-05-23 15:21:02 -0400441 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700442 }
443 }
444}
445
446void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
447 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
448 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
449 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700450
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400451 // Ganesh assumes that all renderable surfaces are also texturable
452 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
453 *flags = *flags | kRenderable_Flag;
454 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700455 }
456
457 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
458 *flags = *flags | kBlitSrc_Flag;
459 }
460
461 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
462 *flags = *flags | kBlitDst_Flag;
463 }
464}
465
Greg Daniel81e7bf82017-07-19 14:47:42 -0400466void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
467 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000468 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400469 VkFormat format) {
470 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
471 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
472 VK_IMAGE_USAGE_SAMPLED_BIT |
473 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
474 VkImageCreateFlags createFlags = GrVkFormatIsSRGB(format, nullptr)
475 ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
476 VkImageFormatProperties properties;
477 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
478 format,
479 VK_IMAGE_TYPE_2D,
480 VK_IMAGE_TILING_OPTIMAL,
481 usage,
482 createFlags,
483 &properties));
484 VkSampleCountFlags flags = properties.sampleCounts;
485 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500486 fColorSampleCounts.push(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400487 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000488 if (kImagination_VkVendor == physProps.vendorID) {
489 // MSAA does not work on imagination
490 return;
491 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400492 if (flags & VK_SAMPLE_COUNT_2_BIT) {
493 fColorSampleCounts.push(2);
494 }
495 if (flags & VK_SAMPLE_COUNT_4_BIT) {
496 fColorSampleCounts.push(4);
497 }
498 if (flags & VK_SAMPLE_COUNT_8_BIT) {
499 fColorSampleCounts.push(8);
500 }
501 if (flags & VK_SAMPLE_COUNT_16_BIT) {
502 fColorSampleCounts.push(16);
503 }
504 if (flags & VK_SAMPLE_COUNT_32_BIT) {
505 fColorSampleCounts.push(32);
506 }
507 if (flags & VK_SAMPLE_COUNT_64_BIT) {
508 fColorSampleCounts.push(64);
509 }
510}
511
egdaniel8f1dcaa2016-04-01 10:10:45 -0700512void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
513 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000514 const VkPhysicalDeviceProperties& properties,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700515 VkFormat format) {
516 VkFormatProperties props;
517 memset(&props, 0, sizeof(VkFormatProperties));
518 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
519 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
520 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400521 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000522 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400523 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500524}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400525
Brian Salomonbdecacf2018-02-02 20:32:49 -0500526int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
527 requestedCount = SkTMax(1, requestedCount);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400528 int count = fConfigTable[config].fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500529
530 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400531 return 0;
532 }
533
Brian Salomonbdecacf2018-02-02 20:32:49 -0500534 if (1 == requestedCount) {
535 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
536 fConfigTable[config].fColorSampleCounts[0] == 1);
537 return 1;
538 }
539
Greg Daniel81e7bf82017-07-19 14:47:42 -0400540 for (int i = 0; i < count; ++i) {
541 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
542 return fConfigTable[config].fColorSampleCounts[i];
543 }
544 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500545 return 0;
546}
547
548int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
549 const auto& table = fConfigTable[config].fColorSampleCounts;
550 if (!table.count()) {
551 return 0;
552 }
553 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500554}
555
Brian Salomon3d86a192018-02-27 16:46:11 -0500556bool GrVkCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
557 if (auto rt = surface->asRenderTarget()) {
558 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
559 }
560 return true;
561}
562
Robert Phillipsfc711a22018-02-13 17:03:00 -0500563bool validate_image_info(VkFormat format, SkColorType ct, GrPixelConfig* config) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500564 *config = kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500565
566 switch (ct) {
567 case kUnknown_SkColorType:
568 return false;
569 case kAlpha_8_SkColorType:
570 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500571 *config = kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500572 }
573 break;
574 case kRGB_565_SkColorType:
575 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500576 *config = kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500577 }
578 break;
579 case kARGB_4444_SkColorType:
580 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500581 *config = kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500582 }
583 break;
584 case kRGBA_8888_SkColorType:
585 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500586 *config = kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500587 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500588 *config = kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500589 }
590 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500591 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400592 // TODO: VK_FORMAT_R8G8B8_UNORM
Brian Salomone41e1762018-01-25 14:07:47 -0500593 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500594 case kBGRA_8888_SkColorType:
595 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500596 *config = kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500597 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500598 *config = kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500599 }
600 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500601 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500602 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
603 *config = kRGBA_1010102_GrPixelConfig;
604 }
605 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500606 case kRGB_101010x_SkColorType:
607 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500608 case kGray_8_SkColorType:
609 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500610 *config = kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500611 }
612 break;
613 case kRGBA_F16_SkColorType:
614 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500615 *config = kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500616 }
617 break;
618 }
619
Greg Danielfaa095e2017-12-19 13:15:02 -0500620 return kUnknown_GrPixelConfig != *config;
Greg Danielf5d87582017-12-18 14:48:15 -0500621}
622
Greg Danielfaa095e2017-12-19 13:15:02 -0500623bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
624 GrPixelConfig* config) const {
Greg Daniel52e16d92018-04-10 09:34:07 -0400625 GrVkImageInfo imageInfo;
626 if (!tex.getVkImageInfo(&imageInfo)) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500627 return false;
628 }
629
Greg Daniel52e16d92018-04-10 09:34:07 -0400630 return validate_image_info(imageInfo.fFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500631}
632
633bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
634 GrPixelConfig* config) const {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400635 GrVkImageInfo imageInfo;
636 if (!rt.getVkImageInfo(&imageInfo)) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500637 return false;
638 }
639
Greg Daniel323fbcf2018-04-10 13:46:30 -0400640 return validate_image_info(imageInfo.fFormat, ct, config);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500641}
642
643bool GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
644 GrPixelConfig* config) const {
645 const VkFormat* vkFormat = format.getVkFormat();
646 if (!vkFormat) {
647 return false;
648 }
649 return validate_image_info(*vkFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500650}
Greg Danielf5d87582017-12-18 14:48:15 -0500651