| 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 |
|
| 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 |
*/ |
| 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(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 |
|
| 89 |
bool isMac() const { return fGPUFamily == GPUFamily::kMac || |
| 90 |
fGPUFamily == GPUFamily::kMacIntel; } |
| 91 |
bool isApple() const { return fGPUFamily == GPUFamily::kApple; } |
| 92 |
bool isIntel() const { return fGPUFamily == GPUFamily::kMacIntel; } |
| 93 |
|
| 94 |
size_t getMinBufferAlignment() const { return this->isMac() ? 4 : 1; } |
| 95 |
|
| 96 |
// if true, MTLStoreActionStoreAndMultiplesampleResolve is available |
| 97 |
bool storeAndMultisampleResolveSupport() const { return fStoreAndMultisampleResolveSupport; } |
| 98 |
|
| 99 |
bool renderTargetSupportsDiscardableMSAA(const GrMtlRenderTarget*) const; |
| 100 |
|
| 101 |
#if defined(GPU_TEST_UTILS) |
| 102 |
std::vector<GrTest::TestFormatColorTypeCombination> getTestingCombinations() const override; |
| 103 |
#endif |
| 104 |
void onDumpJSON(SkJSONWriter*) const override; |
| 105 |
|
| 106 |
private: |
| 107 |
void initGPUFamily(id<MTLDevice> device); |
| 108 |
|
| 109 |
void initStencilFormat(id<MTLDevice> device); |
| 110 |
|
| 111 |
void initGrCaps(id<MTLDevice> device); |
| 112 |
void initShaderCaps(); |
| 113 |
|
| 114 |
void applyDriverCorrectnessWorkarounds(const GrContextOptions&, const id<MTLDevice>); |
| 115 |
|
| 116 |
void initFormatTable(); |
| 117 |
|
| 118 |
bool onSurfaceSupportsWritePixels(const GrSurface*) const override; |
| 119 |
bool onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect, |
| 120 |
const GrSurfaceProxy* src, const SkIRect& srcRect) const override; |
| 121 |
GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override; |
| 122 |
bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override; |
| 123 |
|
| 124 |
SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&, |
| 125 |
GrColorType) const override; |
| 126 |
|
| 127 |
skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override; |
| 128 |
|
| 129 |
// ColorTypeInfo for a specific format |
| 130 |
struct ColorTypeInfo { |
| 131 |
GrColorType fColorType = GrColorType::kUnknown; |
| 132 |
enum { |
| 133 |
kUploadData_Flag = 0x1, |
| 134 |
// Does Ganesh itself support rendering to this colorType & format pair. Renderability |
| 135 |
// still additionally depends on if the format itself is renderable. |
| 136 |
kRenderable_Flag = 0x2, |
| 137 |
}; |
| 138 |
uint32_t fFlags = 0; |
| 139 |
|
| 140 |
skgpu::Swizzle fReadSwizzle; |
| 141 |
skgpu::Swizzle fWriteSwizzle; |
| 142 |
}; |
| 143 |
|
| 144 |
struct FormatInfo { |
| 145 |
uint32_t colorTypeFlags(GrColorType colorType) const { |
| 146 |
for (int i = 0; i < fColorTypeInfoCount; ++i) { |
| 147 |
if (fColorTypeInfos[i].fColorType == colorType) { |
| 148 |
return fColorTypeInfos[i].fFlags; |
| 149 |
} |
| 150 |
} |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
enum { |
| 155 |
kTexturable_Flag = 0x1, |
| 156 |
kRenderable_Flag = 0x2, // Color attachment and blendable |
| 157 |
kMSAA_Flag = 0x4, |
| 158 |
kResolve_Flag = 0x8, |
| 159 |
}; |
| 160 |
static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag | |
| 161 |
kMSAA_Flag | kResolve_Flag; |
| 162 |
|
| 163 |
uint16_t fFlags = 0; |
| 164 |
|
| 165 |
std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; |
| 166 |
int fColorTypeInfoCount = 0; |
| 167 |
}; |
| 168 |
#ifdef SK_BUILD_FOR_IOS |
| 169 |
inline static constexpr size_t kNumMtlFormats = 18; |
| 170 |
#else |
| 171 |
inline static constexpr size_t kNumMtlFormats = 19; |
| 172 |
#endif |
| 173 |
static size_t GetFormatIndex(MTLPixelFormat); |
| 174 |
FormatInfo fFormatTable[kNumMtlFormats]; |
| 175 |
|
| 176 |
const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const { |
| 177 |
size_t index = GetFormatIndex(pixelFormat); |
| 178 |
return fFormatTable[index]; |
| 179 |
} |
| 180 |
|
| 181 |
MTLPixelFormat fColorTypeToFormatTable[kGrColorTypeCnt]; |
| 182 |
void setColorType(GrColorType, std::initializer_list<MTLPixelFormat> formats); |
| 183 |
|
| 184 |
enum class GPUFamily { |
| 185 |
kApple, |
| 186 |
kMac, |
| 187 |
kMacIntel, |
| 188 |
}; |
| 189 |
bool getGPUFamily(id<MTLDevice> device, GPUFamily* gpuFamily, int* group); |
| 190 |
bool getGPUFamilyFromFeatureSet(id<MTLDevice> device, GrMtlCaps::GPUFamily* gpuFamily, |
| 191 |
int* group); |
| 192 |
|
| 193 |
GPUFamily fGPUFamily; |
| 194 |
int fFamilyGroup; |
| 195 |
|
| 196 |
SkTDArray<int> fSampleCounts; |
| 197 |
|
| 198 |
MTLPixelFormat fPreferredStencilFormat; |
| 199 |
|
| 200 |
bool fStoreAndMultisampleResolveSupport : 1; |
| 201 |
|
| 202 |
using INHERITED = GrCaps; |
| 203 |
}; |
| 204 |
|
| 205 |
#endif |