• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkTDArray.h"
12 #include "src/gpu/GrCaps.h"
13 #include "src/gpu/mtl/GrMtlAttachment.h"
14 
15 #import <Metal/Metal.h>
16 
17 class GrShaderCaps;
18 class GrMtlRenderTarget;
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 
73     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override;
74 
getFormatFromColorType(GrColorType colorType)75     MTLPixelFormat getFormatFromColorType(GrColorType colorType) const {
76         int idx = static_cast<int>(colorType);
77         return fColorTypeToFormatTable[idx];
78     }
79 
80     GrSwizzle 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);
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     // If true when doing MSAA draws, we will prefer to discard the MSAA attachment on load
98     // and stores. The use of this feature for specific draws depends on the render target having a
99     // resolve attachment, and if we need to load previous data the resolve attachment must
100     // be readable in a shader. Otherwise we will just write out and store the MSAA attachment
101     // like normal.
preferDiscardableMSAAAttachment()102     bool preferDiscardableMSAAAttachment() const { return fPreferDiscardableMSAAAttachment; }
103     bool renderTargetSupportsDiscardableMSAA(const GrMtlRenderTarget*) const;
104 
105 #if GR_TEST_UTILS
106     std::vector<TestFormatColorTypeCombination> getTestingCombinations() const override;
107 #endif
108     void onDumpJSON(SkJSONWriter*) const override;
109 
110 private:
111     void initGPUFamily(id<MTLDevice> device);
112 
113     void initStencilFormat(id<MTLDevice> device);
114 
115     void initGrCaps(id<MTLDevice> device);
116     void initShaderCaps();
117 
118     void applyDriverCorrectnessWorkarounds(const GrContextOptions&, const id<MTLDevice>);
119 
120     void initFormatTable();
121 
122     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
123     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
124                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
125     GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
126     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
127 
128     SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
129                                                  GrColorType) const override;
130 
131     GrSwizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override;
132 
133     // ColorTypeInfo for a specific format
134     struct ColorTypeInfo {
135         GrColorType fColorType = GrColorType::kUnknown;
136         enum {
137             kUploadData_Flag = 0x1,
138             // Does Ganesh itself support rendering to this colorType & format pair. Renderability
139             // still additionally depends on if the format itself is renderable.
140             kRenderable_Flag = 0x2,
141         };
142         uint32_t fFlags = 0;
143 
144         GrSwizzle fReadSwizzle;
145         GrSwizzle fWriteSwizzle;
146     };
147 
148     struct FormatInfo {
colorTypeFlagsFormatInfo149         uint32_t colorTypeFlags(GrColorType colorType) const {
150             for (int i = 0; i < fColorTypeInfoCount; ++i) {
151                 if (fColorTypeInfos[i].fColorType == colorType) {
152                     return fColorTypeInfos[i].fFlags;
153                 }
154             }
155             return 0;
156         }
157 
158         enum {
159             kTexturable_Flag  = 0x1,
160             kRenderable_Flag  = 0x2, // Color attachment and blendable
161             kMSAA_Flag        = 0x4,
162             kResolve_Flag     = 0x8,
163         };
164         static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag |
165                                           kMSAA_Flag | kResolve_Flag;
166 
167         uint16_t fFlags = 0;
168 
169         std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
170         int fColorTypeInfoCount = 0;
171     };
172 #ifdef SK_BUILD_FOR_IOS
173     inline static constexpr size_t kNumMtlFormats = 17;
174 #else
175     inline static constexpr size_t kNumMtlFormats = 16;
176 #endif
177     static size_t GetFormatIndex(MTLPixelFormat);
178     FormatInfo fFormatTable[kNumMtlFormats];
179 
getFormatInfo(const MTLPixelFormat pixelFormat)180     const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const {
181         size_t index = GetFormatIndex(pixelFormat);
182         return fFormatTable[index];
183     }
184 
185     MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt];
186     void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats);
187 
188     enum class GPUFamily {
189         kMac,
190         kApple,
191     };
192     bool getGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group);
193     bool getGPUFamilyFromFeatureSet(id<MTLDevice> device, GrMtlCaps::GPUFamily* gpuFamily,
194                                     int* group);
195 
196     GPUFamily fGPUFamily;
197     int fFamilyGroup;
198 
199     SkTDArray<int> fSampleCounts;
200 
201     MTLPixelFormat fPreferredStencilFormat;
202 
203     bool fStoreAndMultisampleResolveSupport : 1;
204     bool fPreferDiscardableMSAAAttachment : 1;
205 
206     using INHERITED = GrCaps;
207 };
208 
209 #endif
210