1 /* 2 * Copyright 2017 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 #ifndef GrMtlCaps_DEFINED 9 #define GrMtlCaps_DEFINED 10 11 #include "include/private/base/SkTDArray.h" 12 #include "src/gpu/ganesh/GrCaps.h" 13 #include "src/gpu/ganesh/mtl/GrMtlAttachment.h" 14 15 #import <Metal/Metal.h> 16 17 class GrMtlRenderTarget; 18 enum class SkTextureCompressionType; 19 20 /** 21 * Stores some capabilities of a Mtl backend. 22 */ 23 class GrMtlCaps : public GrCaps { 24 public: 25 GrMtlCaps(const GrContextOptions& contextOptions, id<MTLDevice> device); 26 27 bool isFormatSRGB(const GrBackendFormat&) const override; 28 29 bool isFormatTexturable(const GrBackendFormat&, GrTextureType) const override; 30 bool isFormatTexturable(MTLPixelFormat) const; 31 isFormatCopyable(const GrBackendFormat &)32 bool isFormatCopyable(const GrBackendFormat&) const override { return true; } 33 34 bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format, 35 int sampleCount = 1) const override; 36 bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override; 37 bool isFormatRenderable(MTLPixelFormat, int sampleCount) const; 38 39 int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override; 40 int getRenderTargetSampleCount(int requestedCount, MTLPixelFormat) const; 41 42 int maxRenderTargetSampleCount(const GrBackendFormat&) const override; 43 int maxRenderTargetSampleCount(MTLPixelFormat) const; 44 45 SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType, 46 const GrBackendFormat& surfaceFormat, 47 GrColorType srcColorType) const override; 48 49 SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override; 50 51 DstCopyRestrictions getDstCopyRestrictions(const GrRenderTargetProxy* src, 52 GrColorType ct) const override; 53 54 /** 55 * Returns both a supported and most prefered stencil format to use in draws. 56 */ preferredStencilFormat()57 MTLPixelFormat preferredStencilFormat() const { 58 return fPreferredStencilFormat; 59 } 60 61 bool canCopyAsBlit(MTLPixelFormat dstFormat, int dstSampleCount, 62 MTLPixelFormat srcFormat, int srcSampleCount, 63 const SkIRect& srcRect, const SkIPoint& dstPoint, 64 bool areDstSrcSameObj) const; 65 66 bool canCopyAsResolve(MTLPixelFormat dstFormat, int dstSampleCount, 67 MTLPixelFormat srcFormat, int srcSampleCount, 68 bool srcIsRenderTarget, const SkISize srcDimensions, 69 const SkIRect& srcRect, 70 const SkIPoint& dstPoint, 71 bool areDstSrcSameObj) const; 72 getBackendFormatFromCompressionType(SkTextureCompressionType)73 GrBackendFormat getBackendFormatFromCompressionType(SkTextureCompressionType) const override; 74 75 MTLPixelFormat getFormatFromColorType(GrColorType colorType) const { 76 int idx = static_cast<int>(colorType); 77 return fColorTypeToFormatTable[idx]; 78 } 79 80 skgpu::Swizzle getWriteSwizzle(const GrBackendFormat&, GrColorType) const override; 81 82 uint64_t computeFormatKey(const GrBackendFormat&) const override; 83 84 GrProgramDesc makeDesc(GrRenderTarget*, 85 const GrProgramInfo&, 86 ProgramDescOverrideFlags) const override; 87 MTLPixelFormat getStencilPixelFormat(const GrProgramDesc& desc) const; 88 isMac()89 bool isMac() const { return fGPUFamily == GPUFamily::kMac; } isApple()90 bool isApple() const { return fGPUFamily == GPUFamily::kApple; } 91 getMinBufferAlignment()92 size_t getMinBufferAlignment() const { return this->isMac() ? 4 : 1; } 93 94 // if true, MTLStoreActionStoreAndMultiplesampleResolve is available storeAndMultisampleResolveSupport()95 bool storeAndMultisampleResolveSupport() const { return fStoreAndMultisampleResolveSupport; } 96 97 bool renderTargetSupportsDiscardableMSAA(const GrMtlRenderTarget*) const; 98 99 #if defined(GPU_TEST_UTILS) 100 std::vector<GrTest::TestFormatColorTypeCombination> getTestingCombinations() const override; 101 #endif 102 void onDumpJSON(SkJSONWriter*) const override; 103 104 private: 105 void initGPUFamily(id<MTLDevice> device); 106 107 void initStencilFormat(id<MTLDevice> device); 108 109 void initGrCaps(id<MTLDevice> device); 110 void initShaderCaps(); 111 112 void applyDriverCorrectnessWorkarounds(const GrContextOptions&, const id<MTLDevice>); 113 114 void initFormatTable(); 115 116 bool onSurfaceSupportsWritePixels(const GrSurface*) const override; 117 bool onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect, 118 const GrSurfaceProxy* src, const SkIRect& srcRect) const override; 119 GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override; 120 bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override; 121 122 SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&, 123 GrColorType) const override; 124 125 skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override; 126 127 // ColorTypeInfo for a specific format 128 struct ColorTypeInfo { 129 GrColorType fColorType = GrColorType::kUnknown; 130 enum { 131 kUploadData_Flag = 0x1, 132 // Does Ganesh itself support rendering to this colorType & format pair. Renderability 133 // still additionally depends on if the format itself is renderable. 134 kRenderable_Flag = 0x2, 135 }; 136 uint32_t fFlags = 0; 137 138 skgpu::Swizzle fReadSwizzle; 139 skgpu::Swizzle fWriteSwizzle; 140 }; 141 142 struct FormatInfo { colorTypeFlagsFormatInfo143 uint32_t colorTypeFlags(GrColorType colorType) const { 144 for (int i = 0; i < fColorTypeInfoCount; ++i) { 145 if (fColorTypeInfos[i].fColorType == colorType) { 146 return fColorTypeInfos[i].fFlags; 147 } 148 } 149 return 0; 150 } 151 152 enum { 153 kTexturable_Flag = 0x1, 154 kRenderable_Flag = 0x2, // Color attachment and blendable 155 kMSAA_Flag = 0x4, 156 kResolve_Flag = 0x8, 157 }; 158 static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag | 159 kMSAA_Flag | kResolve_Flag; 160 161 uint16_t fFlags = 0; 162 163 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; 164 int fColorTypeInfoCount = 0; 165 }; 166 #ifdef SK_BUILD_FOR_IOS 167 inline static constexpr size_t kNumMtlFormats = 18; 168 #else 169 inline static constexpr size_t kNumMtlFormats = 19; 170 #endif 171 static size_t GetFormatIndex(MTLPixelFormat); 172 FormatInfo fFormatTable[kNumMtlFormats]; 173 getFormatInfo(const MTLPixelFormat pixelFormat)174 const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const { 175 size_t index = GetFormatIndex(pixelFormat); 176 return fFormatTable[index]; 177 } 178 179 MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt]; 180 void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats); 181 182 enum class GPUFamily { 183 kMac, 184 kApple, 185 }; 186 bool getGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group); 187 bool getGPUFamilyFromFeatureSet(id<MTLDevice> device, GrMtlCaps::GPUFamily* gpuFamily, 188 int* group); 189 190 GPUFamily fGPUFamily; 191 int fFamilyGroup; 192 193 SkTDArray<int> fSampleCounts; 194 195 MTLPixelFormat fPreferredStencilFormat; 196 197 bool fStoreAndMultisampleResolveSupport : 1; 198 199 using INHERITED = GrCaps; 200 }; 201 202 #endif 203