Flutter Impeller
sampler_library_mtl.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
9 
10 namespace impeller {
11 
12 SamplerLibraryMTL::SamplerLibraryMTL(id<MTLDevice> device) : device_(device) {}
13 
14 SamplerLibraryMTL::~SamplerLibraryMTL() = default;
15 
16 raw_ptr<const Sampler> SamplerLibraryMTL::GetSampler(
17  const SamplerDescriptor& descriptor) {
18  uint64_t p_key = SamplerDescriptor::ToKey(descriptor);
19  for (const auto& [key, value] : samplers_) {
20  if (key == p_key) {
21  return raw_ptr(value);
22  }
23  }
24  if (!device_) {
25  return raw_ptr<const Sampler>(nullptr);
26  }
27  auto desc = [[MTLSamplerDescriptor alloc] init];
28  desc.minFilter = ToMTLSamplerMinMagFilter(descriptor.min_filter);
29  desc.magFilter = ToMTLSamplerMinMagFilter(descriptor.mag_filter);
30  desc.mipFilter = ToMTLSamplerMipFilter(descriptor.mip_filter);
31  desc.sAddressMode = ToMTLSamplerAddressMode(descriptor.width_address_mode);
32  desc.tAddressMode = ToMTLSamplerAddressMode(descriptor.height_address_mode);
33  desc.rAddressMode = ToMTLSamplerAddressMode(descriptor.depth_address_mode);
34  if (@available(iOS 14.0, macos 10.12, *)) {
35  desc.borderColor = MTLSamplerBorderColorTransparentBlack;
36  }
37 #ifdef IMPELLER_DEBUG
38  if (!descriptor.label.empty()) {
39  desc.label = @(descriptor.label.data());
40  }
41 #endif // IMPELLER_DEBUG
42 
43  auto mtl_sampler = [device_ newSamplerStateWithDescriptor:desc];
44  if (!mtl_sampler) {
45  return raw_ptr<const Sampler>(nullptr);
46  ;
47  }
48  auto sampler =
49  std::shared_ptr<SamplerMTL>(new SamplerMTL(descriptor, mtl_sampler));
50  samplers_.push_back(std::make_pair(p_key, std::move(sampler)));
51  return raw_ptr(samplers_.back().second);
52 }
53 
54 } // namespace impeller
int32_t value
constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter)
Definition: formats_mtl.h:347
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(SamplerAddressMode mode)
Definition: formats_mtl.h:359
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
Definition: formats_mtl.h:337