1 2 /* 3 * Copyright 2020 Google LLC 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 #ifndef GrD3DCaps_DEFINED 10 #define GrD3DCaps_DEFINED 11 12 #include "src/gpu/ganesh/GrCaps.h" 13 14 #include "include/gpu/d3d/GrD3DTypes.h" 15 #include "include/private/base/SkTDArray.h" 16 #include "src/gpu/ganesh/d3d/GrD3DAttachment.h" 17 18 enum class SkTextureCompressionType; 19 20 /** 21 * Stores some capabilities of a D3D backend. 22 */ 23 class GrD3DCaps : public GrCaps { 24 public: 25 /** 26 * Creates a GrD3DCaps that is set such that nothing is supported. The init function should 27 * be called to fill out the caps. 28 */ 29 GrD3DCaps(const GrContextOptions& contextOptions, IDXGIAdapter1*, ID3D12Device*); 30 31 bool isFormatSRGB(const GrBackendFormat&) const override; 32 33 bool isFormatTexturable(const GrBackendFormat&, GrTextureType) const override; 34 bool isFormatTexturable(DXGI_FORMAT) const; 35 isFormatCopyable(const GrBackendFormat &)36 bool isFormatCopyable(const GrBackendFormat&) const override { return true; } 37 38 bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format, 39 int sampleCount = 1) const override; 40 bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override; 41 bool isFormatRenderable(DXGI_FORMAT, int sampleCount) const; 42 43 bool isFormatUnorderedAccessible(DXGI_FORMAT) const; 44 45 int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override; 46 int getRenderTargetSampleCount(int requestedCount, DXGI_FORMAT) const; 47 48 int maxRenderTargetSampleCount(const GrBackendFormat&) const override; 49 int maxRenderTargetSampleCount(DXGI_FORMAT) const; 50 51 GrColorType getFormatColorType(DXGI_FORMAT) const; 52 53 SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType, 54 const GrBackendFormat& surfaceFormat, 55 GrColorType srcColorType) const override; 56 57 SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override; 58 59 /** 60 * Returns both a supported and most preferred stencil format to use in draws. 61 */ preferredStencilFormat()62 DXGI_FORMAT preferredStencilFormat() const { 63 return fPreferredStencilFormat; 64 } GetStencilFormatTotalBitCount(DXGI_FORMAT format)65 static int GetStencilFormatTotalBitCount(DXGI_FORMAT format) { 66 switch (format) { 67 case DXGI_FORMAT_D24_UNORM_S8_UINT: 68 return 32; 69 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: 70 // DXGI_FORMAT_D32_FLOAT_S8X24_UINT has 24 unused bits at the end so total bits is 64. 71 return 64; 72 default: 73 SkASSERT(false); 74 return 0; 75 } 76 } 77 78 /** 79 * Helpers used by canCopySurface. In all cases if the SampleCnt parameter is zero that means 80 * the surface is not a render target, otherwise it is the number of samples in the render 81 * target. 82 */ 83 bool canCopyTexture(DXGI_FORMAT dstFormat, int dstSampleCnt, 84 DXGI_FORMAT srcFormat, int srcSamplecnt) const; 85 86 bool canCopyAsResolve(DXGI_FORMAT dstFormat, int dstSampleCnt, 87 DXGI_FORMAT srcFormat, int srcSamplecnt) const; 88 getBackendFormatFromCompressionType(SkTextureCompressionType)89 GrBackendFormat getBackendFormatFromCompressionType(SkTextureCompressionType) const override; 90 91 DXGI_FORMAT getFormatFromColorType(GrColorType colorType) const { 92 int idx = static_cast<int>(colorType); 93 return fColorTypeToFormatTable[idx]; 94 } 95 96 skgpu::Swizzle getWriteSwizzle(const GrBackendFormat&, GrColorType) const override; 97 98 uint64_t computeFormatKey(const GrBackendFormat&) const override; 99 100 void addExtraSamplerKey(skgpu::KeyBuilder*, 101 GrSamplerState, 102 const GrBackendFormat&) const override; 103 104 GrProgramDesc makeDesc(GrRenderTarget*, 105 const GrProgramInfo&, 106 ProgramDescOverrideFlags) const override; 107 resolveSubresourceRegionSupport()108 bool resolveSubresourceRegionSupport() const { return fResolveSubresourceRegionSupport; } standardSwizzleLayoutSupport()109 bool standardSwizzleLayoutSupport() const { return fStandardSwizzleLayoutSupport; } 110 111 #if defined(GR_TEST_UTILS) 112 std::vector<GrTest::TestFormatColorTypeCombination> getTestingCombinations() const override; 113 #endif 114 115 private: 116 enum D3DVendor { 117 kAMD_D3DVendor = 0x1002, 118 kARM_D3DVendor = 0x13B5, 119 kImagination_D3DVendor = 0x1010, 120 kIntel_D3DVendor = 0x8086, 121 kNVIDIA_D3DVendor = 0x10DE, 122 kQualcomm_D3DVendor = 0x5143, 123 }; 124 125 void init(const GrContextOptions& contextOptions, IDXGIAdapter1*, ID3D12Device*); 126 127 void initGrCaps(const D3D12_FEATURE_DATA_D3D12_OPTIONS&, 128 ID3D12Device*); 129 void initShaderCaps(int vendorID, const D3D12_FEATURE_DATA_D3D12_OPTIONS& optionsDesc); 130 131 void initFormatTable(const DXGI_ADAPTER_DESC&, ID3D12Device*); 132 void initStencilFormat(ID3D12Device*); 133 134 void applyDriverCorrectnessWorkarounds(int vendorID); 135 136 bool onSurfaceSupportsWritePixels(const GrSurface*) const override; 137 bool onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect, 138 const GrSurfaceProxy* src, const SkIRect& srcRect) const override; 139 GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override; 140 141 bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override; 142 143 SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&, 144 GrColorType) const override; 145 146 skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override; 147 148 // ColorTypeInfo for a specific format 149 struct ColorTypeInfo { 150 GrColorType fColorType = GrColorType::kUnknown; 151 enum { 152 kUploadData_Flag = 0x1, 153 // Does Ganesh itself support rendering to this colorType & format pair. Renderability 154 // still additionally depends on if the format itself is renderable. 155 kRenderable_Flag = 0x2, 156 // Indicates that this colorType is supported only if we are wrapping a texture with 157 // the given format and colorType. We do not allow creation with this pair. 158 kWrappedOnly_Flag = 0x4, 159 }; 160 uint32_t fFlags = 0; 161 162 skgpu::Swizzle fReadSwizzle; 163 skgpu::Swizzle fWriteSwizzle; 164 }; 165 166 struct FormatInfo { colorTypeFlagsFormatInfo167 uint32_t colorTypeFlags(GrColorType colorType) const { 168 for (int i = 0; i < fColorTypeInfoCount; ++i) { 169 if (fColorTypeInfos[i].fColorType == colorType) { 170 return fColorTypeInfos[i].fFlags; 171 } 172 } 173 return 0; 174 } 175 176 void init(const DXGI_ADAPTER_DESC&, ID3D12Device*, DXGI_FORMAT); 177 static void InitFormatFlags(const D3D12_FEATURE_DATA_FORMAT_SUPPORT&, uint16_t* flags); 178 void initSampleCounts(const DXGI_ADAPTER_DESC& adapterDesc, ID3D12Device*, DXGI_FORMAT); 179 180 enum { 181 kTexturable_Flag = 0x1, // Can be sampled in a shader 182 kRenderable_Flag = 0x2, // Rendertarget and blendable 183 kMSAA_Flag = 0x4, 184 kResolve_Flag = 0x8, 185 kUnorderedAccess_Flag = 0x10, 186 }; 187 188 uint16_t fFlags = 0; 189 190 SkTDArray<int> fColorSampleCounts; 191 192 // This GrColorType represents how the actually GPU format lays out its memory. This is used 193 // for uploading data to backend textures to make sure we've arranged the memory in the 194 // correct order. 195 GrColorType fFormatColorType = GrColorType::kUnknown; 196 197 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; 198 int fColorTypeInfoCount = 0; 199 }; 200 static const size_t kNumDxgiFormats = 15; 201 FormatInfo fFormatTable[kNumDxgiFormats]; 202 203 FormatInfo& getFormatInfo(DXGI_FORMAT); 204 const FormatInfo& getFormatInfo(DXGI_FORMAT) const; 205 206 DXGI_FORMAT fColorTypeToFormatTable[kGrColorTypeCnt]; 207 void setColorType(GrColorType, std::initializer_list<DXGI_FORMAT> formats); 208 209 int fMaxPerStageShaderResourceViews; 210 int fMaxPerStageUnorderedAccessViews; 211 212 DXGI_FORMAT fPreferredStencilFormat; 213 214 bool fResolveSubresourceRegionSupport : 1; 215 bool fStandardSwizzleLayoutSupport : 1; 216 217 using INHERITED = GrCaps; 218 }; 219 220 #endif 221