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