2 * Copyright 2021 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/gpu/graphite/mtl/MtlTexture.h"
10 #include "include/gpu/graphite/mtl/MtlTypes.h"
11 #include "include/private/gpu/graphite/MtlTypesPriv.h"
12 #include "src/gpu/graphite/mtl/MtlCaps.h"
13 #include "src/gpu/graphite/mtl/MtlGpu.h"
14 #include "src/gpu/graphite/mtl/MtlUtils.h"
16 namespace skgpu::graphite {
18 sk_cfp<id<MTLTexture>> MtlTexture::MakeMtlTexture(const MtlGpu* gpu,
20 const TextureInfo& info) {
21 const skgpu::graphite::Caps* caps = gpu->caps();
22 if (dimensions.width() > caps->maxTextureSize() ||
23 dimensions.height() > caps->maxTextureSize()) {
27 const MtlTextureSpec& mtlSpec = info.mtlTextureSpec();
28 SkASSERT(!mtlSpec.fFramebufferOnly);
30 if (mtlSpec.fUsage & MTLTextureUsageShaderRead && !caps->isTexturable(info)) {
34 if (mtlSpec.fUsage & MTLTextureUsageRenderTarget &&
35 !(caps->isRenderable(info) || MtlFormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat))) {
39 sk_cfp<MTLTextureDescriptor*> desc([[MTLTextureDescriptor alloc] init]);
40 (*desc).textureType = (info.numSamples() > 1) ? MTLTextureType2DMultisample : MTLTextureType2D;
41 (*desc).pixelFormat = (MTLPixelFormat)mtlSpec.fFormat;
42 (*desc).width = dimensions.width();
43 (*desc).height = dimensions.height();
45 (*desc).mipmapLevelCount = info.numMipLevels();
46 (*desc).sampleCount = info.numSamples();
47 (*desc).arrayLength = 1;
48 (*desc).usage = mtlSpec.fUsage;
49 (*desc).storageMode = (MTLStorageMode)mtlSpec.fStorageMode;
51 sk_cfp<id<MTLTexture>> texture([gpu->device() newTextureWithDescriptor:desc.get()]);
52 #ifdef SK_ENABLE_MTL_DEBUG_INFO
53 if (mtlSpec.fUsage & MTLTextureUsageRenderTarget) {
54 if (MtlFormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat)) {
55 (*texture).label = @"DepthStencil";
57 if (info.numSamples() > 1) {
58 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) {
59 (*texture).label = @"MSAA SampledTexture-ColorAttachment";
61 (*texture).label = @"MSAA ColorAttachment";
64 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) {
65 (*texture).label = @"SampledTexture-ColorAttachment";
67 (*texture).label = @"ColorAttachment";
72 SkASSERT(mtlSpec.fUsage & MTLTextureUsageShaderRead);
73 (*texture).label = @"SampledTexture";
80 MtlTexture::MtlTexture(const MtlGpu* gpu,
82 const TextureInfo& info,
83 sk_cfp<id<MTLTexture>> texture,
86 : Texture(gpu, dimensions, info, ownership, budgeted)
87 , fTexture(std::move(texture)) {}
89 sk_sp<Texture> MtlTexture::Make(const MtlGpu* gpu,
91 const TextureInfo& info,
92 SkBudgeted budgeted) {
93 sk_cfp<id<MTLTexture>> texture = MakeMtlTexture(gpu, dimensions, info);
97 return sk_sp<Texture>(new MtlTexture(gpu,
105 sk_sp<Texture> MtlTexture::MakeWrapped(const MtlGpu* gpu,
107 const TextureInfo& info,
108 sk_cfp<id<MTLTexture>> texture) {
109 return sk_sp<Texture>(new MtlTexture(gpu,
117 void MtlTexture::freeGpuData() {
121 } // namespace skgpu::graphite