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#include "experimental/graphite/src/mtl/MtlTexture.h" 9 10#include "experimental/graphite/include/mtl/MtlTypes.h" 11#include "experimental/graphite/include/private/MtlTypesPriv.h" 12#include "experimental/graphite/src/mtl/MtlGpu.h" 13#include "experimental/graphite/src/mtl/MtlUtils.h" 14 15namespace skgpu::mtl { 16 17Texture::Texture(SkISize dimensions, 18 const skgpu::TextureInfo& info, 19 sk_cfp<id<MTLTexture>> texture) 20 : skgpu::Texture(dimensions, info) 21 , fTexture(std::move(texture)) {} 22 23sk_sp<Texture> Texture::Make(const Gpu* gpu, 24 SkISize dimensions, 25 const skgpu::TextureInfo& info) { 26 // TODO: get this from Caps 27 if (dimensions.width() > 16384 || dimensions.height() > 16384) { 28 return nullptr; 29 } 30 31 const TextureSpec& mtlSpec = info.mtlTextureSpec(); 32 SkASSERT(!mtlSpec.fFramebufferOnly); 33 34 sk_cfp<MTLTextureDescriptor*> desc([[MTLTextureDescriptor alloc] init]); 35 (*desc).textureType = (info.numSamples() > 1) ? MTLTextureType2DMultisample : MTLTextureType2D; 36 (*desc).pixelFormat = (MTLPixelFormat)mtlSpec.fFormat; 37 (*desc).width = dimensions.width(); 38 (*desc).height = dimensions.height(); 39 (*desc).depth = 1; 40 (*desc).mipmapLevelCount = info.numMipLevels(); 41 (*desc).sampleCount = info.numSamples(); 42 (*desc).arrayLength = 1; 43 (*desc).usage = mtlSpec.fUsage; 44 (*desc).storageMode = (MTLStorageMode)mtlSpec.fStorageMode; 45 46 sk_cfp<id<MTLTexture>> texture([gpu->device() newTextureWithDescriptor:desc.get()]); 47#ifdef SK_ENABLE_MTL_DEBUG_INFO 48 if (mtlSpec.fUsage & MTLTextureUsageRenderTarget) { 49 if (FormatIsDepthOrStencil((MTLPixelFormat)mtlSpec.fFormat)) { 50 (*texture).label = @"DepthStencil"; 51 } else { 52 if (info.numSamples() > 1) { 53 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) { 54 (*texture).label = @"MSAA SampledTexture-ColorAttachment"; 55 } else { 56 (*texture).label = @"MSAA ColorAttachment"; 57 } 58 } else { 59 if (mtlSpec.fUsage & MTLTextureUsageShaderRead) { 60 (*texture).label = @"SampledTexture-ColorAttachment"; 61 } else { 62 (*texture).label = @"ColorAttachment"; 63 } 64 } 65 } 66 } else { 67 SkASSERT(mtlSpec.fUsage & MTLTextureUsageShaderRead); 68 (*texture).label = @"SampledTexture"; 69 } 70#endif 71 72 return sk_sp<Texture>(new Texture(dimensions, info, std::move(texture))); 73} 74 75} // namespace skgpu::mtl 76 77