2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrMtlCaps_DEFINED
9 #define GrMtlCaps_DEFINED
11 #include "include/private/SkTDArray.h"
12 #include "src/gpu/ganesh/GrCaps.h"
13 #include "src/gpu/ganesh/mtl/GrMtlAttachment.h"
15 #import <Metal/Metal.h>
17 class GrMtlRenderTarget;
20 * Stores some capabilities of a Mtl backend.
22 class GrMtlCaps : public GrCaps {
24 GrMtlCaps(const GrContextOptions& contextOptions, id<MTLDevice> device);
26 bool isFormatSRGB(const GrBackendFormat&) const override;
28 bool isFormatTexturable(const GrBackendFormat&, GrTextureType) const override;
29 bool isFormatTexturable(MTLPixelFormat) const;
31 bool isFormatCopyable(const GrBackendFormat&) const override { return true; }
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;
38 int getRenderTargetSampleCount(int requestedCount, const GrBackendFormat&) const override;
39 int getRenderTargetSampleCount(int requestedCount, MTLPixelFormat) const;
41 int maxRenderTargetSampleCount(const GrBackendFormat&) const override;
42 int maxRenderTargetSampleCount(MTLPixelFormat) const;
44 SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
45 const GrBackendFormat& surfaceFormat,
46 GrColorType srcColorType) const override;
48 SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override;
50 DstCopyRestrictions getDstCopyRestrictions(const GrRenderTargetProxy* src,
51 GrColorType ct) const override;
54 * Returns both a supported and most prefered stencil format to use in draws.
56 MTLPixelFormat preferredStencilFormat() const {
57 return fPreferredStencilFormat;
60 bool canCopyAsBlit(MTLPixelFormat dstFormat, int dstSampleCount,
61 MTLPixelFormat srcFormat, int srcSampleCount,
62 const SkIRect& srcRect, const SkIPoint& dstPoint,
63 bool areDstSrcSameObj) const;
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;
72 GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override;
74 MTLPixelFormat getFormatFromColorType(GrColorType colorType) const {
75 int idx = static_cast<int>(colorType);
76 return fColorTypeToFormatTable[idx];
79 skgpu::Swizzle getWriteSwizzle(const GrBackendFormat&, GrColorType) const override;
81 uint64_t computeFormatKey(const GrBackendFormat&) const override;
83 GrProgramDesc makeDesc(GrRenderTarget*,
85 ProgramDescOverrideFlags) const override;
86 MTLPixelFormat getStencilPixelFormat(const GrProgramDesc& desc);
88 bool isMac() const { return fGPUFamily == GPUFamily::kMac; }
89 bool isApple() const { return fGPUFamily == GPUFamily::kApple; }
91 size_t getMinBufferAlignment() const { return this->isMac() ? 4 : 1; }
93 // if true, MTLStoreActionStoreAndMultiplesampleResolve is available
94 bool storeAndMultisampleResolveSupport() const { return fStoreAndMultisampleResolveSupport; }
96 bool renderTargetSupportsDiscardableMSAA(const GrMtlRenderTarget*) const;
99 std::vector<TestFormatColorTypeCombination> getTestingCombinations() const override;
101 void onDumpJSON(SkJSONWriter*) const override;
104 void initGPUFamily(id<MTLDevice> device);
106 void initStencilFormat(id<MTLDevice> device);
108 void initGrCaps(id<MTLDevice> device);
109 void initShaderCaps();
111 void applyDriverCorrectnessWorkarounds(const GrContextOptions&, const id<MTLDevice>);
113 void initFormatTable();
115 bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
116 bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
117 const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
118 GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
119 bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
121 SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
122 GrColorType) const override;
124 skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override;
126 // ColorTypeInfo for a specific format
127 struct ColorTypeInfo {
128 GrColorType fColorType = GrColorType::kUnknown;
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,
137 skgpu::Swizzle fReadSwizzle;
138 skgpu::Swizzle fWriteSwizzle;
142 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;
152 kTexturable_Flag = 0x1,
153 kRenderable_Flag = 0x2, // Color attachment and blendable
157 static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag |
158 kMSAA_Flag | kResolve_Flag;
162 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
163 int fColorTypeInfoCount = 0;
165 #ifdef SK_BUILD_FOR_IOS
166 inline static constexpr size_t kNumMtlFormats = 17;
168 inline static constexpr size_t kNumMtlFormats = 16;
170 static size_t GetFormatIndex(MTLPixelFormat);
171 FormatInfo fFormatTable[kNumMtlFormats];
173 const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const {
174 size_t index = GetFormatIndex(pixelFormat);
175 return fFormatTable[index];
178 MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt];
179 void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats);
181 enum class GPUFamily {
185 bool getGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group);
186 bool getGPUFamilyFromFeatureSet(id<MTLDevice> device, GrMtlCaps::GPUFamily* gpuFamily,
189 GPUFamily fGPUFamily;
192 SkTDArray<int> fSampleCounts;
194 MTLPixelFormat fPreferredStencilFormat;
196 bool fStoreAndMultisampleResolveSupport : 1;
198 using INHERITED = GrCaps;