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