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