| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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 skgpu_graphite_MtlCaps_DEFINED |
| 9 |
#define skgpu_graphite_MtlCaps_DEFINED |
| 10 |
|
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#import <Metal/Metal.h> |
| 14 |
|
| 15 |
#include "src/gpu/graphite/Caps.h" |
| 16 |
|
| 17 |
namespace skgpu::graphite { |
| 18 |
struct ContextOptions; |
| 19 |
|
| 20 |
class MtlCaps final : public Caps { |
| 21 |
public: |
| 22 |
MtlCaps(const id<MTLDevice>, const ContextOptions&); |
| 23 |
~MtlCaps() override {} |
| 24 |
|
| 25 |
bool isSampleCountSupported(TextureFormat, SampleCount requestedSampleCount) const override; |
| 26 |
TextureFormat getDepthStencilFormat(SkEnumBitMask<DepthStencilFlags>) const override; |
| 27 |
|
| 28 |
TextureInfo getDefaultAttachmentTextureInfo(AttachmentDesc, |
| 29 |
Protected, |
| 30 |
Discardable) const override; |
| 31 |
|
| 32 |
TextureInfo getDefaultSampledTextureInfo(SkColorType, |
| 33 |
Mipmapped, |
| 34 |
Protected, |
| 35 |
Renderable) const override; |
| 36 |
|
| 37 |
TextureInfo getTextureInfoForSampledCopy(const TextureInfo&, Mipmapped) const override; |
| 38 |
|
| 39 |
TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType, |
| 40 |
Mipmapped, |
| 41 |
Protected) const override; |
| 42 |
|
| 43 |
TextureInfo getDefaultStorageTextureInfo(SkColorType) const override; |
| 44 |
|
| 45 |
UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc&, |
| 46 |
const RenderPassDesc&) const override; |
| 47 |
UniqueKey makeComputePipelineKey(const ComputePipelineDesc&) const override; |
| 48 |
|
| 49 |
bool extractGraphicsDescs(const UniqueKey&, |
| 50 |
GraphicsPipelineDesc*, |
| 51 |
RenderPassDesc*, |
| 52 |
const RendererProvider*) const override; |
| 53 |
|
| 54 |
// Get a sufficiently unique bit representation for the RenderPassDesc to be embedded in other |
| 55 |
// UniqueKeys (e.g. makeGraphicsPipelineKey). |
| 56 |
uint32_t getRenderPassDescKey(const RenderPassDesc&) const; |
| 57 |
|
| 58 |
bool isMac() const { return fGPUFamily == GPUFamily::kMac || |
| 59 |
fGPUFamily == GPUFamily::kMacIntel; } |
| 60 |
bool isApple() const { return fGPUFamily == GPUFamily::kApple; } |
| 61 |
bool isIntel() const { return fGPUFamily == GPUFamily::kMacIntel; } |
| 62 |
|
| 63 |
bool isRenderable(const TextureInfo&) const override; |
| 64 |
bool isStorage(const TextureInfo&) const override; |
| 65 |
|
| 66 |
void buildKeyForTexture(SkISize dimensions, |
| 67 |
const TextureInfo&, |
| 68 |
ResourceType, |
| 69 |
GraphiteResourceKey*) const override; |
| 70 |
|
| 71 |
private: |
| 72 |
void initGPUFamily(const id<MTLDevice>); |
| 73 |
|
| 74 |
void initCaps(const id<MTLDevice>); |
| 75 |
void initShaderCaps(); |
| 76 |
void initFormatTable(const id<MTLDevice>); |
| 77 |
|
| 78 |
enum class GPUFamily { |
| 79 |
kApple, |
| 80 |
kMac, |
| 81 |
kMacIntel, |
| 82 |
}; |
| 83 |
static bool GetGPUFamily(id<MTLDevice>, GPUFamily*, int* group); |
| 84 |
|
| 85 |
MTLPixelFormat getFormatFromColorType(SkColorType colorType) const { |
| 86 |
int idx = static_cast<int>(colorType); |
| 87 |
return fColorTypeToFormatTable[idx]; |
| 88 |
} |
| 89 |
|
| 90 |
SkSpan<const ColorTypeInfo> getColorTypeInfos(const TextureInfo&) const override; |
| 91 |
|
| 92 |
bool onIsTexturable(const TextureInfo&) const override; |
| 93 |
bool isTexturable(MTLPixelFormat) const; |
| 94 |
|
| 95 |
bool supportsWritePixels(const TextureInfo&) const override; |
| 96 |
bool supportsReadPixels(const TextureInfo&) const override; |
| 97 |
|
| 98 |
struct FormatInfo { |
| 99 |
uint32_t colorTypeFlags(SkColorType colorType) const { |
| 100 |
for (int i = 0; i < fColorTypeInfoCount; ++i) { |
| 101 |
if (fColorTypeInfos[i].fColorType == colorType) { |
| 102 |
return fColorTypeInfos[i].fFlags; |
| 103 |
} |
| 104 |
} |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
enum { |
| 109 |
kTexturable_Flag = 0x01, |
| 110 |
kRenderable_Flag = 0x02, // Render attachment (color or depth/stencil) |
| 111 |
kMSAA_Flag = 0x04, |
| 112 |
kResolve_Flag = 0x08, |
| 113 |
kStorage_Flag = 0x10, |
| 114 |
}; |
| 115 |
static const uint16_t kAllFlags = |
| 116 |
kTexturable_Flag | kRenderable_Flag | kMSAA_Flag | kResolve_Flag | kStorage_Flag; |
| 117 |
|
| 118 |
uint16_t fFlags = 0; |
| 119 |
|
| 120 |
std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; |
| 121 |
int fColorTypeInfoCount = 0; |
| 122 |
}; |
| 123 |
#ifdef SK_BUILD_FOR_MAC |
| 124 |
inline static constexpr size_t kNumMtlFormats = 23; |
| 125 |
#else |
| 126 |
inline static constexpr size_t kNumMtlFormats = 21; |
| 127 |
#endif |
| 128 |
|
| 129 |
static size_t GetFormatIndex(MTLPixelFormat); |
| 130 |
FormatInfo fFormatTable[kNumMtlFormats]; |
| 131 |
|
| 132 |
const FormatInfo& getFormatInfo(const MTLPixelFormat pixelFormat) const { |
| 133 |
size_t index = GetFormatIndex(pixelFormat); |
| 134 |
return fFormatTable[index]; |
| 135 |
} |
| 136 |
|
| 137 |
MTLPixelFormat fColorTypeToFormatTable[kSkColorTypeCnt]; |
| 138 |
void setColorType(SkColorType, std::initializer_list<MTLPixelFormat> formats); |
| 139 |
|
| 140 |
SkEnumBitMask<SampleCount> fSupportedSampleCounts; |
| 141 |
|
| 142 |
GPUFamily fGPUFamily; |
| 143 |
int fFamilyGroup; |
| 144 |
}; |
| 145 |
|
| 146 |
} // namespace skgpu::graphite |
| 147 |
|
| 148 |
#endif // skgpu_graphite_MtlCaps_DEFINED |