1 /*
2 * Copyright 2020 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 #include "src/gpu/ganesh/d3d/GrD3DCaps.h"
8
9 #include "include/core/SkTextureCompressionType.h"
10 #include "include/gpu/GrBackendSurface.h"
11 #include "include/gpu/GrContextOptions.h"
12 #include "include/gpu/d3d/GrD3DBackendContext.h"
13 #include "include/gpu/d3d/GrD3DTypes.h"
14 #include "src/core/SkCompressedDataUtils.h"
15 #include "src/gpu/KeyBuilder.h"
16 #include "src/gpu/ganesh/GrBackendUtils.h"
17 #include "src/gpu/ganesh/GrProgramDesc.h"
18 #include "src/gpu/ganesh/GrProgramInfo.h"
19 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
20 #include "src/gpu/ganesh/GrShaderCaps.h"
21 #include "src/gpu/ganesh/GrStencilSettings.h"
22 #include "src/gpu/ganesh/TestFormatColorTypeCombination.h"
23 #include "src/gpu/ganesh/d3d/GrD3DGpu.h"
24 #include "src/gpu/ganesh/d3d/GrD3DRenderTarget.h"
25 #include "src/gpu/ganesh/d3d/GrD3DTexture.h"
26 #include "src/gpu/ganesh/d3d/GrD3DUtil.h"
27
GrD3DCaps(const GrContextOptions & contextOptions,IDXGIAdapter1 * adapter,ID3D12Device * device)28 GrD3DCaps::GrD3DCaps(const GrContextOptions& contextOptions, IDXGIAdapter1* adapter,
29 ID3D12Device* device)
30 : INHERITED(contextOptions) {
31 /**************************************************************************
32 * GrCaps fields
33 **************************************************************************/
34 fNPOTTextureTileSupport = true; // available in feature level 10_0 and up
35 fMipmapSupport = true; // always available in Direct3D
36 fAnisoSupport = true; // always available in Direct3D
37 fReuseScratchTextures = true; //TODO: figure this out
38 fGpuTracingSupport = false; //TODO: figure this out
39 fOversizedStencilSupport = false; //TODO: figure this out
40 fDrawInstancedSupport = true;
41 fNativeDrawIndirectSupport = true;
42
43 fSemaphoreSupport = true;
44 fBackendSemaphoreSupport = true;
45 fFinishedProcAsyncCallbackSupport = true;
46 // TODO: implement these
47 fCrossContextTextureSupport = false;
48 fHalfFloatVertexAttributeSupport = false;
49
50 // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
51 fReadPixelsRowBytesSupport = true;
52 fWritePixelsRowBytesSupport = true;
53 fTransferPixelsToRowBytesSupport = true;
54
55 fTransferFromBufferToTextureSupport = true;
56 fTransferFromSurfaceToBufferSupport = true;
57 fTransferFromBufferToBufferSupport = true;
58
59 fMaxRenderTargetSize = 16384; // minimum required by feature level 11_0
60 fMaxTextureSize = 16384; // minimum required by feature level 11_0
61
62 fTransferBufferRowBytesAlignment = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT;
63
64 // TODO: implement
65 fDynamicStateArrayGeometryProcessorTextureSupport = false;
66
67 fShaderCaps = std::make_unique<GrShaderCaps>();
68
69 this->init(contextOptions, adapter, device);
70 }
71
canCopyTexture(DXGI_FORMAT dstFormat,int dstSampleCnt,DXGI_FORMAT srcFormat,int srcSampleCnt) const72 bool GrD3DCaps::canCopyTexture(DXGI_FORMAT dstFormat, int dstSampleCnt,
73 DXGI_FORMAT srcFormat, int srcSampleCnt) const {
74 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
75 return false;
76 }
77
78 // D3D allows us to copy within the same format family but doesn't do conversions
79 // so we require strict identity.
80 return srcFormat == dstFormat;
81 }
82
canCopyAsResolve(DXGI_FORMAT dstFormat,int dstSampleCnt,DXGI_FORMAT srcFormat,int srcSampleCnt) const83 bool GrD3DCaps::canCopyAsResolve(DXGI_FORMAT dstFormat, int dstSampleCnt,
84 DXGI_FORMAT srcFormat, int srcSampleCnt) const {
85 // The src surface must be multisampled.
86 if (srcSampleCnt <= 1) {
87 return false;
88 }
89
90 // The dst must not be multisampled.
91 if (dstSampleCnt > 1) {
92 return false;
93 }
94
95 // Surfaces must have the same format.
96 // D3D12 can resolve between typeless and non-typeless formats, but we are not using
97 // typeless formats. It's not possible to resolve within the same format family otherwise.
98 if (srcFormat != dstFormat) {
99 return false;
100 }
101
102 return true;
103 }
104
onCanCopySurface(const GrSurfaceProxy * dst,const SkIRect & dstRect,const GrSurfaceProxy * src,const SkIRect & srcRect) const105 bool GrD3DCaps::onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect,
106 const GrSurfaceProxy* src, const SkIRect& srcRect) const {
107 // D3D12 does not support scaling copies
108 if (srcRect.size() != dstRect.size()) {
109 return false;
110 }
111 if (src->isProtected() == GrProtected::kYes && dst->isProtected() != GrProtected::kYes) {
112 return false;
113 }
114
115 int dstSampleCnt = 0;
116 int srcSampleCnt = 0;
117 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
118 dstSampleCnt = rtProxy->numSamples();
119 }
120 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
121 srcSampleCnt = rtProxy->numSamples();
122 }
123 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
124 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
125
126 DXGI_FORMAT dstFormat, srcFormat;
127 SkAssertResult(dst->backendFormat().asDxgiFormat(&dstFormat));
128 SkAssertResult(src->backendFormat().asDxgiFormat(&srcFormat));
129
130 return this->canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt) ||
131 this->canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt);
132 }
133
init(const GrContextOptions & contextOptions,IDXGIAdapter1 * adapter,ID3D12Device * device)134 void GrD3DCaps::init(const GrContextOptions& contextOptions, IDXGIAdapter1* adapter,
135 ID3D12Device* device) {
136 D3D_FEATURE_LEVEL featureLevels[] = {
137 D3D_FEATURE_LEVEL_11_0,
138 D3D_FEATURE_LEVEL_11_1,
139 D3D_FEATURE_LEVEL_12_0,
140 D3D_FEATURE_LEVEL_12_1,
141 };
142 D3D12_FEATURE_DATA_FEATURE_LEVELS flDesc = {};
143 flDesc.NumFeatureLevels = _countof(featureLevels);
144 flDesc.pFeatureLevelsRequested = featureLevels;
145 GR_D3D_CALL_ERRCHECK(device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &flDesc,
146 sizeof(flDesc)));
147 // This had better be true
148 SkASSERT(flDesc.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_0);
149
150 DXGI_ADAPTER_DESC adapterDesc;
151 GR_D3D_CALL_ERRCHECK(adapter->GetDesc(&adapterDesc));
152
153 D3D12_FEATURE_DATA_D3D12_OPTIONS optionsDesc;
154 GR_D3D_CALL_ERRCHECK(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &optionsDesc,
155 sizeof(optionsDesc)));
156
157
158 // See https://docs.microsoft.com/en-us/windows/win32/direct3d12/hardware-support
159 if (D3D12_RESOURCE_BINDING_TIER_1 == optionsDesc.ResourceBindingTier) {
160 fMaxPerStageShaderResourceViews = 128;
161 if (D3D_FEATURE_LEVEL_11_0 == flDesc.MaxSupportedFeatureLevel) {
162 fMaxPerStageUnorderedAccessViews = 8;
163 } else {
164 fMaxPerStageUnorderedAccessViews = 64;
165 }
166 } else {
167 // The doc above says "full heap", but practically it seems like it should be
168 // limited by the maximum number of samplers in a heap
169 fMaxPerStageUnorderedAccessViews = 2032;
170 fMaxPerStageShaderResourceViews = 2032;
171 }
172
173 fStandardSwizzleLayoutSupport = (optionsDesc.StandardSwizzle64KBSupported);
174
175 D3D12_FEATURE_DATA_D3D12_OPTIONS2 optionsDesc2;
176 GR_D3D_CALL_ERRCHECK(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &optionsDesc2,
177 sizeof(optionsDesc2)));
178 fResolveSubresourceRegionSupport = (optionsDesc2.ProgrammableSamplePositionsTier !=
179 D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED);
180
181 this->initGrCaps(optionsDesc, device);
182 this->initShaderCaps(adapterDesc.VendorId, optionsDesc);
183
184 this->initFormatTable(adapterDesc, device);
185 this->initStencilFormat(device);
186
187 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
188 this->applyDriverCorrectnessWorkarounds(adapterDesc.VendorId);
189 }
190
191 this->finishInitialization(contextOptions);
192 }
193
initGrCaps(const D3D12_FEATURE_DATA_D3D12_OPTIONS & optionsDesc,ID3D12Device * device)194 void GrD3DCaps::initGrCaps(const D3D12_FEATURE_DATA_D3D12_OPTIONS& optionsDesc,
195 ID3D12Device* device) {
196 // We assume a minimum of Shader Model 5.1, which allows at most 32 vertex inputs.
197 fMaxVertexAttributes = 32;
198
199 // Can use standard sample locations
200 fSampleLocationsSupport = true;
201
202 if (D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED !=
203 optionsDesc.ConservativeRasterizationTier) {
204 fConservativeRasterSupport = true;
205 }
206
207 fWireframeSupport = true;
208
209 // Feature level 11_0 and up support up to 16K in texture dimension
210 fMaxTextureSize = 16384;
211 // There's no specific cap for RT size, so use texture size
212 fMaxRenderTargetSize = fMaxTextureSize;
213 // Our render targets are always created with textures as the color
214 // attachment, hence this min:
215 fMaxRenderTargetSize = fMaxTextureSize;
216
217 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
218
219 // Assuming since we will always map in the end to upload the data we might as well just map
220 // from the get go. There is no hard data to suggest this is faster or slower.
221 fBufferMapThreshold = 0;
222
223 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
224
225 fOversizedStencilSupport = true;
226
227 fTwoSidedStencilRefsAndMasksMustMatch = true;
228
229 // Advanced blend modes don't appear to be supported.
230 }
231
initShaderCaps(int vendorID,const D3D12_FEATURE_DATA_D3D12_OPTIONS & optionsDesc)232 void GrD3DCaps::initShaderCaps(int vendorID, const D3D12_FEATURE_DATA_D3D12_OPTIONS& optionsDesc) {
233 GrShaderCaps* shaderCaps = fShaderCaps.get();
234 shaderCaps->fVersionDeclString = "#version 330\n";
235
236 // Shader Model 5 supports all of the following:
237 shaderCaps->fUsesPrecisionModifiers = true;
238 shaderCaps->fFlatInterpolationSupport = true;
239 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
240 // to be true with D3D as well.
241 shaderCaps->fPreferFlatInterpolation = kQualcomm_D3DVendor != vendorID;
242
243 shaderCaps->fSampleMaskSupport = true;
244
245 shaderCaps->fShaderDerivativeSupport = true;
246 shaderCaps->fExplicitTextureLodSupport = true;
247
248 shaderCaps->fDualSourceBlendingSupport = true;
249
250 shaderCaps->fIntegerSupport = true;
251 shaderCaps->fNonsquareMatrixSupport = true;
252 // TODO(skia:12352) HLSL does not expose asinh/acosh/atanh
253 shaderCaps->fInverseHyperbolicSupport = false;
254 shaderCaps->fVertexIDSupport = true;
255 shaderCaps->fInfinitySupport = true;
256 shaderCaps->fNonconstantArrayIndexSupport = true;
257 shaderCaps->fBitManipulationSupport = true;
258
259 shaderCaps->fFloatIs32Bits = true;
260 shaderCaps->fHalfIs32Bits =
261 D3D12_SHADER_MIN_PRECISION_SUPPORT_NONE == optionsDesc.MinPrecisionSupport;
262
263 // See https://docs.microsoft.com/en-us/windows/win32/direct3d12/hardware-support
264 // The maximum number of samplers in a shader-visible descriptor heap is 2048, but
265 // 16 of those are reserved for the driver.
266 shaderCaps->fMaxFragmentSamplers =
267 (D3D12_RESOURCE_BINDING_TIER_1 == optionsDesc.ResourceBindingTier) ? 16 : 2032;
268 }
269
applyDriverCorrectnessWorkarounds(int vendorID)270 void GrD3DCaps::applyDriverCorrectnessWorkarounds(int vendorID) {
271 // Nothing yet.
272 }
273
274
stencil_format_supported(ID3D12Device * device,DXGI_FORMAT format)275 bool stencil_format_supported(ID3D12Device* device, DXGI_FORMAT format) {
276 D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupportDesc;
277 formatSupportDesc.Format = format;
278 GR_D3D_CALL_ERRCHECK(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT,
279 &formatSupportDesc,
280 sizeof(formatSupportDesc)));
281 return SkToBool(D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL & formatSupportDesc.Support1);
282 }
283
initStencilFormat(ID3D12Device * device)284 void GrD3DCaps::initStencilFormat(ID3D12Device* device) {
285 if (stencil_format_supported(device, DXGI_FORMAT_D24_UNORM_S8_UINT)) {
286 fPreferredStencilFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
287 } else {
288 SkASSERT(stencil_format_supported(device, DXGI_FORMAT_D32_FLOAT_S8X24_UINT));
289 fPreferredStencilFormat = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
290 }
291 }
292
293 // These are all the valid DXGI_FORMATs that we support in Skia. They are roughly ordered from most
294 // frequently used to least to improve look up times in arrays.
295 static constexpr DXGI_FORMAT kDxgiFormats[] = {
296 DXGI_FORMAT_R8G8B8A8_UNORM,
297 DXGI_FORMAT_R8_UNORM,
298 DXGI_FORMAT_B8G8R8A8_UNORM,
299 DXGI_FORMAT_B5G6R5_UNORM,
300 DXGI_FORMAT_R16G16B16A16_FLOAT,
301 DXGI_FORMAT_R16_FLOAT,
302 DXGI_FORMAT_R8G8_UNORM,
303 DXGI_FORMAT_R10G10B10A2_UNORM,
304 DXGI_FORMAT_B4G4R4A4_UNORM,
305 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
306 DXGI_FORMAT_BC1_UNORM,
307 DXGI_FORMAT_R16_UNORM,
308 DXGI_FORMAT_R16G16_UNORM,
309 DXGI_FORMAT_R16G16B16A16_UNORM,
310 DXGI_FORMAT_R16G16_FLOAT
311 };
312
setColorType(GrColorType colorType,std::initializer_list<DXGI_FORMAT> formats)313 void GrD3DCaps::setColorType(GrColorType colorType, std::initializer_list<DXGI_FORMAT> formats) {
314 #ifdef SK_DEBUG
315 for (size_t i = 0; i < kNumDxgiFormats; ++i) {
316 const auto& formatInfo = fFormatTable[i];
317 for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
318 const auto& ctInfo = formatInfo.fColorTypeInfos[j];
319 if (ctInfo.fColorType == colorType &&
320 !SkToBool(ctInfo.fFlags & ColorTypeInfo::kWrappedOnly_Flag)) {
321 bool found = false;
322 for (auto it = formats.begin(); it != formats.end(); ++it) {
323 if (kDxgiFormats[i] == *it) {
324 found = true;
325 }
326 }
327 SkASSERT(found);
328 }
329 }
330 }
331 #endif
332 int idx = static_cast<int>(colorType);
333 for (auto it = formats.begin(); it != formats.end(); ++it) {
334 const auto& info = this->getFormatInfo(*it);
335 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
336 if (info.fColorTypeInfos[i].fColorType == colorType) {
337 fColorTypeToFormatTable[idx] = *it;
338 return;
339 }
340 }
341 }
342 }
343
getFormatInfo(DXGI_FORMAT format) const344 const GrD3DCaps::FormatInfo& GrD3DCaps::getFormatInfo(DXGI_FORMAT format) const {
345 GrD3DCaps* nonConstThis = const_cast<GrD3DCaps*>(this);
346 return nonConstThis->getFormatInfo(format);
347 }
348
getFormatInfo(DXGI_FORMAT format)349 GrD3DCaps::FormatInfo& GrD3DCaps::getFormatInfo(DXGI_FORMAT format) {
350 static_assert(std::size(kDxgiFormats) == GrD3DCaps::kNumDxgiFormats,
351 "Size of DXGI_FORMATs array must match static value in header");
352 for (size_t i = 0; i < std::size(kDxgiFormats); ++i) {
353 if (kDxgiFormats[i] == format) {
354 return fFormatTable[i];
355 }
356 }
357 static FormatInfo kInvalidFormat;
358 return kInvalidFormat;
359 }
360
initFormatTable(const DXGI_ADAPTER_DESC & adapterDesc,ID3D12Device * device)361 void GrD3DCaps::initFormatTable(const DXGI_ADAPTER_DESC& adapterDesc, ID3D12Device* device) {
362 static_assert(std::size(kDxgiFormats) == GrD3DCaps::kNumDxgiFormats,
363 "Size of DXGI_FORMATs array must match static value in header");
364
365 std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, DXGI_FORMAT_UNKNOWN);
366
367 // Go through all the formats and init their support surface and data GrColorTypes.
368 // Format: DXGI_FORMAT_R8G8B8A8_UNORM
369 {
370 constexpr DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
371 auto& info = this->getFormatInfo(format);
372 info.init(adapterDesc, device, format);
373 info.fFormatColorType = GrColorType::kRGBA_8888;
374 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
375 info.fColorTypeInfoCount = 2;
376 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
377 int ctIdx = 0;
378 // Format: DXGI_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
379 {
380 constexpr GrColorType ct = GrColorType::kRGBA_8888;
381 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
382 ctInfo.fColorType = ct;
383 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
384 }
385 // Format: DXGI_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
386 {
387 constexpr GrColorType ct = GrColorType::kRGB_888x;
388 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
389 ctInfo.fColorType = ct;
390 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
391 ctInfo.fReadSwizzle = skgpu::Swizzle("rgb1");
392 }
393 }
394 }
395
396 // Format: DXGI_FORMAT_R8_UNORM
397 {
398 constexpr DXGI_FORMAT format = DXGI_FORMAT_R8_UNORM;
399 auto& info = this->getFormatInfo(format);
400 info.init(adapterDesc, device, format);
401 info.fFormatColorType = GrColorType::kR_8;
402 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
403 info.fColorTypeInfoCount = 3;
404 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
405 int ctIdx = 0;
406 // Format: DXGI_FORMAT_R8_UNORM, Surface: kR_8
407 {
408 constexpr GrColorType ct = GrColorType::kR_8;
409 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
410 ctInfo.fColorType = ct;
411 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
412 }
413 // Format: DXGI_FORMAT_R8_UNORM, Surface: kAlpha_8
414 {
415 constexpr GrColorType ct = GrColorType::kAlpha_8;
416 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
417 ctInfo.fColorType = ct;
418 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
419 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
420 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
421 }
422 // Format: DXGI_FORMAT_R8_UNORM, Surface: kGray_8
423 {
424 constexpr GrColorType ct = GrColorType::kGray_8;
425 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
426 ctInfo.fColorType = ct;
427 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
428 ctInfo.fReadSwizzle = skgpu::Swizzle("rrr1");
429 }
430 }
431 }
432 // Format: DXGI_FORMAT_B8G8R8A8_UNORM
433 {
434 constexpr DXGI_FORMAT format = DXGI_FORMAT_B8G8R8A8_UNORM;
435 auto& info = this->getFormatInfo(format);
436 info.init(adapterDesc, device, format);
437 info.fFormatColorType = GrColorType::kBGRA_8888;
438 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
439 info.fColorTypeInfoCount = 1;
440 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
441 int ctIdx = 0;
442 // Format: DXGI_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
443 {
444 constexpr GrColorType ct = GrColorType::kBGRA_8888;
445 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
446 ctInfo.fColorType = ct;
447 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
448 }
449 }
450 }
451 // Format: DXGI_FORMAT_B5G6R5_UNORM
452 {
453 constexpr DXGI_FORMAT format = DXGI_FORMAT_B5G6R5_UNORM;
454 auto& info = this->getFormatInfo(format);
455 info.init(adapterDesc, device, format);
456 info.fFormatColorType = GrColorType::kBGR_565;
457 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
458 info.fColorTypeInfoCount = 1;
459 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
460 int ctIdx = 0;
461 // Format: DXGI_FORMAT_B5G6R5_UNORM, Surface: kBGR_565
462 {
463 constexpr GrColorType ct = GrColorType::kBGR_565;
464 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
465 ctInfo.fColorType = ct;
466 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
467 }
468 }
469 }
470 // Format: DXGI_FORMAT_R16G16B16A16_FLOAT
471 {
472 constexpr DXGI_FORMAT format = DXGI_FORMAT_R16G16B16A16_FLOAT;
473 auto& info = this->getFormatInfo(format);
474 info.init(adapterDesc, device, format);
475 info.fFormatColorType = GrColorType::kRGBA_F16;
476 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
477 info.fColorTypeInfoCount = 2;
478 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
479 int ctIdx = 0;
480 // Format: DXGI_FORMAT_R16G16B16A16_FLOAT, Surface: GrColorType::kRGBA_F16
481 {
482 constexpr GrColorType ct = GrColorType::kRGBA_F16;
483 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
484 ctInfo.fColorType = ct;
485 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
486 }
487 // Format: DXGI_FORMAT_R16G16B16A16_FLOAT, Surface: GrColorType::kRGBA_F16_Clamped
488 {
489 constexpr GrColorType ct = GrColorType::kRGBA_F16_Clamped;
490 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
491 ctInfo.fColorType = ct;
492 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
493 }
494 }
495 }
496 // Format: DXGI_FORMAT_R16_FLOAT
497 {
498 constexpr DXGI_FORMAT format = DXGI_FORMAT_R16_FLOAT;
499 auto& info = this->getFormatInfo(format);
500 info.init(adapterDesc, device, format);
501 info.fFormatColorType = GrColorType::kR_F16;
502 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
503 info.fColorTypeInfoCount = 1;
504 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
505 int ctIdx = 0;
506 // Format: DXGI_FORMAT_R16_FLOAT, Surface: kAlpha_F16
507 {
508 constexpr GrColorType ct = GrColorType::kAlpha_F16;
509 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
510 ctInfo.fColorType = ct;
511 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
512 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
513 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
514 }
515 }
516 }
517 // Format: DXGI_FORMAT_R8G8_UNORM
518 {
519 constexpr DXGI_FORMAT format = DXGI_FORMAT_R8G8_UNORM;
520 auto& info = this->getFormatInfo(format);
521 info.init(adapterDesc, device, format);
522 info.fFormatColorType = GrColorType::kRG_88;
523 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
524 info.fColorTypeInfoCount = 1;
525 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
526 int ctIdx = 0;
527 // Format: DXGI_FORMAT_R8G8_UNORM, Surface: kRG_88
528 {
529 constexpr GrColorType ct = GrColorType::kRG_88;
530 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
531 ctInfo.fColorType = ct;
532 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
533 }
534 }
535 }
536 // Format: DXGI_FORMAT_R10G10B10A2_UNORM
537 {
538 constexpr DXGI_FORMAT format = DXGI_FORMAT_R10G10B10A2_UNORM;
539 auto& info = this->getFormatInfo(format);
540 info.init(adapterDesc, device, format);
541 info.fFormatColorType = GrColorType::kRGBA_1010102;
542 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
543 info.fColorTypeInfoCount = 1;
544 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
545 int ctIdx = 0;
546 // Format: DXGI_FORMAT_R10G10B10A2_UNORM, Surface: kRGBA_1010102
547 {
548 constexpr GrColorType ct = GrColorType::kRGBA_1010102;
549 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
550 ctInfo.fColorType = ct;
551 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
552 }
553 }
554 }
555 // Format: DXGI_FORMAT_B4G4R4A4_UNORM
556 {
557 constexpr DXGI_FORMAT format = DXGI_FORMAT_B4G4R4A4_UNORM;
558 auto& info = this->getFormatInfo(format);
559 info.init(adapterDesc, device, format);
560 info.fFormatColorType = GrColorType::kBGRA_4444;
561 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
562 info.fColorTypeInfoCount = 1;
563 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
564 int ctIdx = 0;
565 // Format: DXGI_FORMAT_B4G4R4A4_UNORM, Surface: kABGR_4444
566 {
567 constexpr GrColorType ct = GrColorType::kABGR_4444;
568 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
569 ctInfo.fColorType = ct;
570 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
571 ctInfo.fReadSwizzle = skgpu::Swizzle("argb");
572 ctInfo.fWriteSwizzle = skgpu::Swizzle("gbar");
573 }
574 }
575 }
576 // Format: DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
577 {
578 constexpr DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
579 auto& info = this->getFormatInfo(format);
580 info.init(adapterDesc, device, format);
581 info.fFormatColorType = GrColorType::kRGBA_8888_SRGB;
582 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
583 info.fColorTypeInfoCount = 1;
584 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
585 int ctIdx = 0;
586 // Format: DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, Surface: kRGBA_8888_SRGB
587 {
588 constexpr GrColorType ct = GrColorType::kRGBA_8888_SRGB;
589 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
590 ctInfo.fColorType = ct;
591 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
592 }
593 }
594 }
595 // Format: DXGI_FORMAT_R16_UNORM
596 {
597 constexpr DXGI_FORMAT format = DXGI_FORMAT_R16_UNORM;
598 auto& info = this->getFormatInfo(format);
599 info.init(adapterDesc, device, format);
600 info.fFormatColorType = GrColorType::kR_16;
601 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
602 info.fColorTypeInfoCount = 1;
603 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
604 int ctIdx = 0;
605 // Format: DXGI_FORMAT_R16_UNORM, Surface: kAlpha_16
606 {
607 constexpr GrColorType ct = GrColorType::kAlpha_16;
608 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
609 ctInfo.fColorType = ct;
610 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
611 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
612 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
613 }
614 }
615 }
616 // Format: DXGI_FORMAT_R16G16_UNORM
617 {
618 constexpr DXGI_FORMAT format = DXGI_FORMAT_R16G16_UNORM;
619 auto& info = this->getFormatInfo(format);
620 info.init(adapterDesc, device, format);
621 info.fFormatColorType = GrColorType::kRG_1616;
622 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
623 info.fColorTypeInfoCount = 1;
624 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
625 int ctIdx = 0;
626 // Format: DXGI_FORMAT_R16G16_UNORM, Surface: kRG_1616
627 {
628 constexpr GrColorType ct = GrColorType::kRG_1616;
629 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
630 ctInfo.fColorType = ct;
631 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
632 }
633 }
634 }
635 // Format: DXGI_FORMAT_R16G16B16A16_UNORM
636 {
637 constexpr DXGI_FORMAT format = DXGI_FORMAT_R16G16B16A16_UNORM;
638 auto& info = this->getFormatInfo(format);
639 info.init(adapterDesc, device, format);
640 info.fFormatColorType = GrColorType::kRGBA_16161616;
641 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
642 info.fColorTypeInfoCount = 1;
643 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
644 int ctIdx = 0;
645 // Format: DXGI_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
646 {
647 constexpr GrColorType ct = GrColorType::kRGBA_16161616;
648 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
649 ctInfo.fColorType = ct;
650 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
651 }
652 }
653 }
654 // Format: DXGI_FORMAT_R16G16_FLOAT
655 {
656 constexpr DXGI_FORMAT format = DXGI_FORMAT_R16G16_FLOAT;
657 auto& info = this->getFormatInfo(format);
658 info.init(adapterDesc, device, format);
659 info.fFormatColorType = GrColorType::kRG_F16;
660 if (SkToBool(info.fFlags & FormatInfo::kTexturable_Flag)) {
661 info.fColorTypeInfoCount = 1;
662 info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
663 int ctIdx = 0;
664 // Format: DXGI_FORMAT_R16G16_FLOAT, Surface: kRG_F16
665 {
666 constexpr GrColorType ct = GrColorType::kRG_F16;
667 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
668 ctInfo.fColorType = ct;
669 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
670 }
671 }
672 }
673
674 // Format: DXGI_FORMAT_BC1_UNORM
675 {
676 constexpr DXGI_FORMAT format = DXGI_FORMAT_BC1_UNORM;
677 auto& info = this->getFormatInfo(format);
678 info.init(adapterDesc, device, format);
679 // No supported GrColorTypes.
680 }
681
682 ////////////////////////////////////////////////////////////////////////////
683 // Map GrColorTypes (used for creating GrSurfaces) to DXGI_FORMATs. The order in which the
684 // formats are passed into the setColorType function indicates the priority in selecting which
685 // format we use for a given GrcolorType.
686
687 this->setColorType(GrColorType::kAlpha_8, { DXGI_FORMAT_R8_UNORM });
688 this->setColorType(GrColorType::kBGR_565, { DXGI_FORMAT_B5G6R5_UNORM });
689 this->setColorType(GrColorType::kABGR_4444, { DXGI_FORMAT_B4G4R4A4_UNORM });
690 this->setColorType(GrColorType::kRGBA_8888, { DXGI_FORMAT_R8G8B8A8_UNORM });
691 this->setColorType(GrColorType::kRGBA_8888_SRGB, { DXGI_FORMAT_R8G8B8A8_UNORM_SRGB });
692 this->setColorType(GrColorType::kRGB_888x, { DXGI_FORMAT_R8G8B8A8_UNORM });
693 this->setColorType(GrColorType::kRG_88, { DXGI_FORMAT_R8G8_UNORM });
694 this->setColorType(GrColorType::kBGRA_8888, { DXGI_FORMAT_B8G8R8A8_UNORM });
695 this->setColorType(GrColorType::kRGBA_1010102, { DXGI_FORMAT_R10G10B10A2_UNORM });
696 this->setColorType(GrColorType::kGray_8, { DXGI_FORMAT_R8_UNORM });
697 this->setColorType(GrColorType::kAlpha_F16, { DXGI_FORMAT_R16_FLOAT });
698 this->setColorType(GrColorType::kRGBA_F16, { DXGI_FORMAT_R16G16B16A16_FLOAT });
699 this->setColorType(GrColorType::kRGBA_F16_Clamped, { DXGI_FORMAT_R16G16B16A16_FLOAT });
700 this->setColorType(GrColorType::kAlpha_16, { DXGI_FORMAT_R16_UNORM });
701 this->setColorType(GrColorType::kRG_1616, { DXGI_FORMAT_R16G16_UNORM });
702 this->setColorType(GrColorType::kRGBA_16161616, { DXGI_FORMAT_R16G16B16A16_UNORM });
703 this->setColorType(GrColorType::kRG_F16, { DXGI_FORMAT_R16G16_FLOAT });
704 }
705
InitFormatFlags(const D3D12_FEATURE_DATA_FORMAT_SUPPORT & formatSupport,uint16_t * flags)706 void GrD3DCaps::FormatInfo::InitFormatFlags(const D3D12_FEATURE_DATA_FORMAT_SUPPORT& formatSupport,
707 uint16_t* flags) {
708 if (SkToBool(D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE & formatSupport.Support1)) {
709 *flags = *flags | kTexturable_Flag;
710
711 // Ganesh assumes that all renderable surfaces are also texturable
712 if (SkToBool(D3D12_FORMAT_SUPPORT1_RENDER_TARGET & formatSupport.Support1) &&
713 SkToBool(D3D12_FORMAT_SUPPORT1_BLENDABLE & formatSupport.Support1)) {
714 *flags = *flags | kRenderable_Flag;
715 }
716 }
717
718 if (SkToBool(D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RENDERTARGET & formatSupport.Support1)) {
719 *flags = *flags | kMSAA_Flag;
720 }
721
722 if (SkToBool(D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RESOLVE & formatSupport.Support1)) {
723 *flags = *flags | kResolve_Flag;
724 }
725
726 if (SkToBool(D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW & formatSupport.Support1)) {
727 *flags = *flags | kUnorderedAccess_Flag;
728 }
729 }
730
multisample_count_supported(ID3D12Device * device,DXGI_FORMAT format,int sampleCount)731 static bool multisample_count_supported(ID3D12Device* device, DXGI_FORMAT format, int sampleCount) {
732 D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS msqLevels;
733 msqLevels.Format = format;
734 msqLevels.SampleCount = sampleCount;
735 msqLevels.Flags = D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE;
736 GR_D3D_CALL_ERRCHECK(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
737 &msqLevels, sizeof(msqLevels)));
738
739 return msqLevels.NumQualityLevels > 0;
740 }
741
initSampleCounts(const DXGI_ADAPTER_DESC & adapterDesc,ID3D12Device * device,DXGI_FORMAT format)742 void GrD3DCaps::FormatInfo::initSampleCounts(const DXGI_ADAPTER_DESC& adapterDesc,
743 ID3D12Device* device, DXGI_FORMAT format) {
744 if (multisample_count_supported(device, format, 1)) {
745 fColorSampleCounts.push_back(1);
746 }
747 // TODO: test these
748 //if (kImagination_D3DVendor == adapterDesc.VendorId) {
749 // // MSAA does not work on imagination
750 // return;
751 //}
752 //if (kIntel_D3DVendor == adapterDesc.VendorId) {
753 // // MSAA doesn't work well on Intel GPUs chromium:527565, chromium:983926
754 // return;
755 //}
756 if (multisample_count_supported(device, format, 2)) {
757 fColorSampleCounts.push_back(2);
758 }
759 if (multisample_count_supported(device, format, 4)) {
760 fColorSampleCounts.push_back(4);
761 }
762 if (multisample_count_supported(device, format, 8)) {
763 fColorSampleCounts.push_back(8);
764 }
765 if (multisample_count_supported(device, format, 16)) {
766 fColorSampleCounts.push_back(16);
767 }
768 // Standard sample locations are not defined for more than 16 samples, and we don't need more
769 // than 16. Omit 32 and 64.
770 }
771
init(const DXGI_ADAPTER_DESC & adapterDesc,ID3D12Device * device,DXGI_FORMAT format)772 void GrD3DCaps::FormatInfo::init(const DXGI_ADAPTER_DESC& adapterDesc, ID3D12Device* device,
773 DXGI_FORMAT format) {
774 D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupportDesc;
775 formatSupportDesc.Format = format;
776 GR_D3D_CALL_ERRCHECK(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT,
777 &formatSupportDesc,
778 sizeof(formatSupportDesc)));
779
780 InitFormatFlags(formatSupportDesc, &fFlags);
781 if (fFlags & kRenderable_Flag) {
782 this->initSampleCounts(adapterDesc, device, format);
783 }
784 }
785
isFormatSRGB(const GrBackendFormat & format) const786 bool GrD3DCaps::isFormatSRGB(const GrBackendFormat& format) const {
787 DXGI_FORMAT dxgiFormat;
788 if (!format.asDxgiFormat(&dxgiFormat)) {
789 return false;
790 }
791
792 switch (dxgiFormat) {
793 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
794 return true;
795 default:
796 return false;
797 }
798 }
799
isFormatTexturable(const GrBackendFormat & format,GrTextureType) const800 bool GrD3DCaps::isFormatTexturable(const GrBackendFormat& format, GrTextureType) const {
801 DXGI_FORMAT dxgiFormat;
802 if (!format.asDxgiFormat(&dxgiFormat)) {
803 return false;
804 }
805
806 return this->isFormatTexturable(dxgiFormat);
807 }
808
isFormatTexturable(DXGI_FORMAT format) const809 bool GrD3DCaps::isFormatTexturable(DXGI_FORMAT format) const {
810 const FormatInfo& info = this->getFormatInfo(format);
811 return SkToBool(FormatInfo::kTexturable_Flag & info.fFlags);
812 }
813
isFormatAsColorTypeRenderable(GrColorType ct,const GrBackendFormat & format,int sampleCount) const814 bool GrD3DCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
815 int sampleCount) const {
816 DXGI_FORMAT dxgiFormat;
817 if (!format.asDxgiFormat(&dxgiFormat)) {
818 return false;
819 }
820 if (!this->isFormatRenderable(dxgiFormat, sampleCount)) {
821 return false;
822 }
823 const auto& info = this->getFormatInfo(dxgiFormat);
824 if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
825 return false;
826 }
827 return true;
828 }
829
isFormatRenderable(const GrBackendFormat & format,int sampleCount) const830 bool GrD3DCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
831 DXGI_FORMAT dxgiFormat;
832 if (!format.asDxgiFormat(&dxgiFormat)) {
833 return false;
834 }
835 return this->isFormatRenderable(dxgiFormat, sampleCount);
836 }
837
isFormatRenderable(DXGI_FORMAT format,int sampleCount) const838 bool GrD3DCaps::isFormatRenderable(DXGI_FORMAT format, int sampleCount) const {
839 return sampleCount <= this->maxRenderTargetSampleCount(format);
840 }
841
isFormatUnorderedAccessible(DXGI_FORMAT format) const842 bool GrD3DCaps::isFormatUnorderedAccessible(DXGI_FORMAT format) const {
843 const FormatInfo& info = this->getFormatInfo(format);
844 return SkToBool(FormatInfo::kUnorderedAccess_Flag & info.fFlags);
845 }
846
getRenderTargetSampleCount(int requestedCount,const GrBackendFormat & format) const847 int GrD3DCaps::getRenderTargetSampleCount(int requestedCount,
848 const GrBackendFormat& format) const {
849 DXGI_FORMAT dxgiFormat;
850 if (!format.asDxgiFormat(&dxgiFormat)) {
851 return 0;
852 }
853
854 return this->getRenderTargetSampleCount(requestedCount, dxgiFormat);
855 }
856
getRenderTargetSampleCount(int requestedCount,DXGI_FORMAT format) const857 int GrD3DCaps::getRenderTargetSampleCount(int requestedCount, DXGI_FORMAT format) const {
858 requestedCount = std::max(1, requestedCount);
859
860 const FormatInfo& info = this->getFormatInfo(format);
861
862 int count = info.fColorSampleCounts.size();
863
864 if (!count) {
865 return 0;
866 }
867
868 if (1 == requestedCount) {
869 SkASSERT(info.fColorSampleCounts.size() && info.fColorSampleCounts[0] == 1);
870 return 1;
871 }
872
873 for (int i = 0; i < count; ++i) {
874 if (info.fColorSampleCounts[i] >= requestedCount) {
875 return info.fColorSampleCounts[i];
876 }
877 }
878 return 0;
879 }
880
maxRenderTargetSampleCount(const GrBackendFormat & format) const881 int GrD3DCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
882 DXGI_FORMAT dxgiFormat;
883 if (!format.asDxgiFormat(&dxgiFormat)) {
884 return 0;
885 }
886 return this->maxRenderTargetSampleCount(dxgiFormat);
887 }
888
maxRenderTargetSampleCount(DXGI_FORMAT format) const889 int GrD3DCaps::maxRenderTargetSampleCount(DXGI_FORMAT format) const {
890 const FormatInfo& info = this->getFormatInfo(format);
891
892 const auto& table = info.fColorSampleCounts;
893 if (!table.size()) {
894 return 0;
895 }
896 return table[table.size() - 1];
897 }
898
getFormatColorType(DXGI_FORMAT format) const899 GrColorType GrD3DCaps::getFormatColorType(DXGI_FORMAT format) const {
900 const FormatInfo& info = this->getFormatInfo(format);
901 return info.fFormatColorType;
902 }
903
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType) const904 GrCaps::SupportedWrite GrD3DCaps::supportedWritePixelsColorType(
905 GrColorType surfaceColorType, const GrBackendFormat& surfaceFormat,
906 GrColorType srcColorType) const {
907 DXGI_FORMAT dxgiFormat;
908 if (!surfaceFormat.asDxgiFormat(&dxgiFormat)) {
909 return { GrColorType::kUnknown, 0 };
910 }
911
912 // Any buffer data needs to be aligned to 512 bytes and that of a single texel.
913 size_t offsetAlignment = SkAlignTo(GrDxgiFormatBytesPerBlock(dxgiFormat),
914 D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
915
916 const auto& info = this->getFormatInfo(dxgiFormat);
917 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
918 const auto& ctInfo = info.fColorTypeInfos[i];
919 if (ctInfo.fColorType == surfaceColorType) {
920 return { surfaceColorType, offsetAlignment };
921 }
922 }
923 return { GrColorType::kUnknown, 0 };
924 }
925
surfaceSupportsReadPixels(const GrSurface * surface) const926 GrCaps::SurfaceReadPixelsSupport GrD3DCaps::surfaceSupportsReadPixels(
927 const GrSurface* surface) const {
928 if (surface->isProtected()) {
929 return SurfaceReadPixelsSupport::kUnsupported;
930 }
931 if (auto tex = static_cast<const GrD3DTexture*>(surface->asTexture())) {
932 // We can't directly read from a compressed format
933 if (GrDxgiFormatIsCompressed(tex->dxgiFormat())) {
934 return SurfaceReadPixelsSupport::kCopyToTexture2D;
935 }
936 return SurfaceReadPixelsSupport::kSupported;
937 } else if (auto rt = static_cast<const GrD3DRenderTarget*>(surface->asRenderTarget())) {
938 if (rt->numSamples() > 1) {
939 return SurfaceReadPixelsSupport::kCopyToTexture2D;
940 }
941 return SurfaceReadPixelsSupport::kSupported;
942 }
943 return SurfaceReadPixelsSupport::kUnsupported;
944 }
945
onSurfaceSupportsWritePixels(const GrSurface * surface) const946 bool GrD3DCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
947 if (auto rt = surface->asRenderTarget()) {
948 return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
949 }
950 return true;
951 }
952
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format) const953 bool GrD3DCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
954 const GrBackendFormat& format) const {
955 DXGI_FORMAT dxgiFormat;
956 if (!format.asDxgiFormat(&dxgiFormat)) {
957 return false;
958 }
959
960 const auto& info = this->getFormatInfo(dxgiFormat);
961 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
962 if (info.fColorTypeInfos[i].fColorType == ct) {
963 return true;
964 }
965 }
966 return false;
967 }
968
onGetDefaultBackendFormat(GrColorType ct) const969 GrBackendFormat GrD3DCaps::onGetDefaultBackendFormat(GrColorType ct) const {
970 DXGI_FORMAT format = this->getFormatFromColorType(ct);
971 if (format == DXGI_FORMAT_UNKNOWN) {
972 return {};
973 }
974 return GrBackendFormat::MakeDxgi(format);
975 }
976
getBackendFormatFromCompressionType(SkTextureCompressionType compressionType) const977 GrBackendFormat GrD3DCaps::getBackendFormatFromCompressionType(
978 SkTextureCompressionType compressionType) const {
979 switch (compressionType) {
980 case SkTextureCompressionType::kBC1_RGBA8_UNORM:
981 if (this->isFormatTexturable(DXGI_FORMAT_BC1_UNORM)) {
982 return GrBackendFormat::MakeDxgi(DXGI_FORMAT_BC1_UNORM);
983 }
984 return {};
985 default:
986 return {};
987 }
988
989 SkUNREACHABLE;
990 }
991
onGetReadSwizzle(const GrBackendFormat & format,GrColorType colorType) const992 skgpu::Swizzle GrD3DCaps::onGetReadSwizzle(const GrBackendFormat& format,
993 GrColorType colorType) const {
994 DXGI_FORMAT dxgiFormat;
995 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
996 const auto& info = this->getFormatInfo(dxgiFormat);
997 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
998 const auto& ctInfo = info.fColorTypeInfos[i];
999 if (ctInfo.fColorType == colorType) {
1000 return ctInfo.fReadSwizzle;
1001 }
1002 }
1003 SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1004 (int)colorType, (int)dxgiFormat);
1005 return {};
1006 }
1007
getWriteSwizzle(const GrBackendFormat & format,GrColorType colorType) const1008 skgpu::Swizzle GrD3DCaps::getWriteSwizzle(const GrBackendFormat& format,
1009 GrColorType colorType) const {
1010 DXGI_FORMAT dxgiFormat;
1011 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
1012 const auto& info = this->getFormatInfo(dxgiFormat);
1013 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1014 const auto& ctInfo = info.fColorTypeInfos[i];
1015 if (ctInfo.fColorType == colorType) {
1016 return ctInfo.fWriteSwizzle;
1017 }
1018 }
1019 SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1020 (int)colorType, (int)dxgiFormat);
1021 return {};
1022 }
1023
computeFormatKey(const GrBackendFormat & format) const1024 uint64_t GrD3DCaps::computeFormatKey(const GrBackendFormat& format) const {
1025 DXGI_FORMAT dxgiFormat;
1026 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
1027
1028 return (uint64_t)dxgiFormat;
1029 }
1030
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat & srcBackendFormat,GrColorType dstColorType) const1031 GrCaps::SupportedRead GrD3DCaps::onSupportedReadPixelsColorType(
1032 GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1033 GrColorType dstColorType) const {
1034 DXGI_FORMAT dxgiFormat;
1035 if (!srcBackendFormat.asDxgiFormat(&dxgiFormat)) {
1036 return { GrColorType::kUnknown, 0 };
1037 }
1038
1039 SkTextureCompressionType compression = GrBackendFormatToCompressionType(srcBackendFormat);
1040 if (compression != SkTextureCompressionType::kNone) {
1041 return { SkTextureCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1042 : GrColorType::kRGBA_8888, 0 };
1043 }
1044
1045 // Any subresource buffer data offset we copy to needs to be aligned to 512 bytes.
1046 size_t offsetAlignment = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT;
1047
1048 const auto& info = this->getFormatInfo(dxgiFormat);
1049 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1050 const auto& ctInfo = info.fColorTypeInfos[i];
1051 if (ctInfo.fColorType == srcColorType) {
1052 return { srcColorType, offsetAlignment };
1053 }
1054 }
1055 return { GrColorType::kUnknown, 0 };
1056 }
1057
addExtraSamplerKey(skgpu::KeyBuilder * b,GrSamplerState samplerState,const GrBackendFormat & format) const1058 void GrD3DCaps::addExtraSamplerKey(skgpu::KeyBuilder* b,
1059 GrSamplerState samplerState,
1060 const GrBackendFormat& format) const {
1061 // TODO
1062 }
1063
1064 /**
1065 * TODO: Determine what goes in the ProgramDesc
1066 */
makeDesc(GrRenderTarget * rt,const GrProgramInfo & programInfo,ProgramDescOverrideFlags overrideFlags) const1067 GrProgramDesc GrD3DCaps::makeDesc(GrRenderTarget* rt,
1068 const GrProgramInfo& programInfo,
1069 ProgramDescOverrideFlags overrideFlags) const {
1070 SkASSERT(overrideFlags == ProgramDescOverrideFlags::kNone);
1071 GrProgramDesc desc;
1072 GrProgramDesc::Build(&desc, programInfo, *this);
1073
1074 skgpu::KeyBuilder b(desc.key());
1075
1076 GrD3DRenderTarget* d3dRT = (GrD3DRenderTarget*) rt;
1077 d3dRT->genKey(&b);
1078
1079 GrStencilSettings stencil = programInfo.nonGLStencilSettings();
1080 stencil.genKey(&b, false);
1081
1082 programInfo.pipeline().genKey(&b, *this);
1083 // The num samples is already added in the render target key so we don't need to add it here.
1084
1085 // D3D requires the full primitive type as part of its key
1086 b.add32(programInfo.primitiveTypeKey());
1087
1088 b.flush();
1089 return desc;
1090 }
1091
1092 #if defined(GR_TEST_UTILS)
getTestingCombinations() const1093 std::vector<GrTest::TestFormatColorTypeCombination> GrD3DCaps::getTestingCombinations() const {
1094 std::vector<GrTest::TestFormatColorTypeCombination> combos = {
1095 {GrColorType::kAlpha_8, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R8_UNORM) },
1096 {GrColorType::kBGR_565, GrBackendFormat::MakeDxgi(DXGI_FORMAT_B5G6R5_UNORM) },
1097 {GrColorType::kABGR_4444, GrBackendFormat::MakeDxgi(DXGI_FORMAT_B4G4R4A4_UNORM) },
1098 {GrColorType::kRGBA_8888, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R8G8B8A8_UNORM) },
1099 {GrColorType::kRGBA_8888_SRGB, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB)},
1100 {GrColorType::kRGB_888x, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R8G8B8A8_UNORM) },
1101 {GrColorType::kRG_88, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R8G8_UNORM) },
1102 {GrColorType::kBGRA_8888, GrBackendFormat::MakeDxgi(DXGI_FORMAT_B8G8R8A8_UNORM) },
1103 {GrColorType::kRGBA_1010102, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R10G10B10A2_UNORM) },
1104 {GrColorType::kGray_8, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R8_UNORM) },
1105 {GrColorType::kAlpha_F16, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16_FLOAT) },
1106 {GrColorType::kRGBA_F16, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16G16B16A16_FLOAT) },
1107 {GrColorType::kRGBA_F16_Clamped, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16G16B16A16_FLOAT)},
1108 {GrColorType::kAlpha_16, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16_UNORM) },
1109 {GrColorType::kRG_1616, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16G16_UNORM) },
1110 {GrColorType::kRGBA_16161616, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16G16B16A16_UNORM) },
1111 {GrColorType::kRG_F16, GrBackendFormat::MakeDxgi(DXGI_FORMAT_R16G16_FLOAT) },
1112 {GrColorType::kRGBA_8888, GrBackendFormat::MakeDxgi(DXGI_FORMAT_BC1_UNORM) },
1113 };
1114
1115 return combos;
1116 }
1117 #endif
1118