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 
7 #include <algorithm>
8 
11 
12 namespace impeller {
13 
14 SamplerLibraryMTL::SamplerLibraryMTL(id<MTLDevice> device) : device_(device) {}
15 
16 SamplerLibraryMTL::~SamplerLibraryMTL() = default;
17 
18 raw_ptr<const Sampler> SamplerLibraryMTL::GetSampler(
19  const SamplerDescriptor& descriptor) {
20  uint64_t p_key = SamplerDescriptor::ToKey(descriptor);
21  for (const auto& [key, value] : samplers_) {
22  if (key == p_key) {
23  return raw_ptr(value);
24  }
25  }
26  if (!device_) {
27  return raw_ptr<const Sampler>(nullptr);
28  }
29  auto desc = [[MTLSamplerDescriptor alloc] init];
30  desc.minFilter = ToMTLSamplerMinMagFilter(descriptor.min_filter);
31  desc.magFilter = ToMTLSamplerMinMagFilter(descriptor.mag_filter);
32  desc.mipFilter = ToMTLSamplerMipFilter(descriptor.mip_filter);
33  desc.sAddressMode = ToMTLSamplerAddressMode(descriptor.width_address_mode);
34  desc.tAddressMode = ToMTLSamplerAddressMode(descriptor.height_address_mode);
35  desc.rAddressMode = ToMTLSamplerAddressMode(descriptor.depth_address_mode);
36  // Metal supports a maxAnisotropy in the range [1, 16] on all devices.
37  desc.maxAnisotropy =
38  std::clamp<NSUInteger>(descriptor.max_anisotropy, 1u, 16u);
39  if (@available(iOS 14.0, macos 10.12, *)) {
40  desc.borderColor = MTLSamplerBorderColorTransparentBlack;
41  }
42 #ifdef IMPELLER_DEBUG
43  if (!descriptor.label.empty()) {
44  desc.label = @(descriptor.label.data());
45  }
46 #endif // IMPELLER_DEBUG
47 
48  auto mtl_sampler = [device_ newSamplerStateWithDescriptor:desc];
49  if (!mtl_sampler) {
50  return raw_ptr<const Sampler>(nullptr);
51  ;
52  }
53  auto sampler =
54  std::shared_ptr<SamplerMTL>(new SamplerMTL(descriptor, mtl_sampler));
55  samplers_.push_back(std::make_pair(p_key, std::move(sampler)));
56  return raw_ptr(samplers_.back().second);
57 }
58 
59 } // namespace impeller
int32_t value
constexpr MTLSamplerMipFilter ToMTLSamplerMipFilter(MipFilter filter)
Definition: formats_mtl.h:404
constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(SamplerAddressMode mode)
Definition: formats_mtl.h:416
constexpr MTLSamplerMinMagFilter ToMTLSamplerMinMagFilter(MinMagFilter filter)
Definition: formats_mtl.h:394