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