Flutter Impeller
sampler_gles.cc
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 
12 
13 namespace impeller {
14 
15 SamplerGLES::SamplerGLES(SamplerDescriptor desc) : Sampler(std::move(desc)) {}
16 
17 SamplerGLES::~SamplerGLES() = default;
18 
19 static GLint ToParam(MinMagFilter minmag_filter) {
20  switch (minmag_filter) {
22  return GL_NEAREST;
24  return GL_LINEAR;
25  }
26  FML_UNREACHABLE();
27 }
28 
29 static GLint ToParam(MinMagFilter minmag_filter, MipFilter mip_filter) {
30  switch (mip_filter) {
31  case MipFilter::kBase:
32  return ToParam(minmag_filter);
34  switch (minmag_filter) {
36  return GL_NEAREST_MIPMAP_NEAREST;
38  return GL_LINEAR_MIPMAP_NEAREST;
39  }
40  case MipFilter::kLinear:
41  switch (minmag_filter) {
43  return GL_NEAREST_MIPMAP_LINEAR;
45  return GL_LINEAR_MIPMAP_LINEAR;
46  }
47  }
48  FML_UNREACHABLE();
49 }
50 
52  bool supports_decal_sampler_address_mode) {
53  switch (mode) {
55  return GL_CLAMP_TO_EDGE;
57  return GL_REPEAT;
59  return GL_MIRRORED_REPEAT;
61  if (supports_decal_sampler_address_mode) {
63  } else {
64  return GL_CLAMP_TO_EDGE;
65  }
66  }
67  FML_UNREACHABLE();
68 }
69 
71  const ProcTableGLES& gl) const {
72  if (texture.NeedsMipmapGeneration()) {
74  << "Texture mip count is > 1, but the mipmap has not been generated. "
75  "Texture can not be sampled safely.";
76  return false;
77  }
78 
79  auto target = ToTextureTarget(texture.GetTextureDescriptor().type);
80 
81  if (!target.has_value()) {
82  return false;
83  }
84  const auto& desc = GetDescriptor();
85 
86  GLint mag_filter = ToParam(desc.mag_filter);
87 
88  // If the texture doesn't have mipmaps, we can't use mip filtering.
89  GLint min_filter;
90  if (texture.GetTextureDescriptor().mip_count > 1) {
91  min_filter = ToParam(desc.min_filter, desc.mip_filter);
92  } else {
93  min_filter = ToParam(desc.min_filter);
94  }
95 
96  gl.TexParameteri(*target, GL_TEXTURE_MIN_FILTER, min_filter);
97  gl.TexParameteri(*target, GL_TEXTURE_MAG_FILTER, mag_filter);
98 
99  const auto supports_decal_mode =
100  gl.GetCapabilities()->SupportsDecalSamplerAddressMode();
101 
102  const auto wrap_s =
103  ToAddressMode(desc.width_address_mode, supports_decal_mode);
104  const auto wrap_t =
105  ToAddressMode(desc.height_address_mode, supports_decal_mode);
106 
107  gl.TexParameteri(*target, GL_TEXTURE_WRAP_S, wrap_s);
108  gl.TexParameteri(*target, GL_TEXTURE_WRAP_T, wrap_t);
109 
110  if (wrap_s == IMPELLER_GL_CLAMP_TO_BORDER ||
111  wrap_t == IMPELLER_GL_CLAMP_TO_BORDER) {
112  // Transparent black.
113  const GLfloat border_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
114  gl.TexParameterfv(*target, IMPELLER_GL_TEXTURE_BORDER_COLOR, border_color);
115  }
116 
117  return true;
118 }
119 
120 } // namespace impeller
impeller::ToParam
static GLint ToParam(MinMagFilter minmag_filter)
Definition: sampler_gles.cc:19
impeller::Texture::NeedsMipmapGeneration
bool NeedsMipmapGeneration() const
Definition: texture.cc:85
impeller::ToAddressMode
static GLint ToAddressMode(SamplerAddressMode mode, bool supports_decal_sampler_address_mode)
Definition: sampler_gles.cc:51
impeller::SamplerAddressMode
SamplerAddressMode
Definition: formats.h:441
impeller::Texture::GetTextureDescriptor
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
impeller::SamplerGLES::~SamplerGLES
~SamplerGLES()
texture_gles.h
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:43
formats.h
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
validation.h
sampler_gles.h
impeller::SamplerGLES::ConfigureBoundTexture
bool ConfigureBoundTexture(const TextureGLES &texture, const ProcTableGLES &gl) const
Definition: sampler_gles.cc:70
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::MipFilter::kNearest
@ kNearest
The nearst mipmap level is selected.
impeller::MipFilter
MipFilter
Options for selecting and filtering between mipmap levels.
Definition: formats.h:425
impeller::MinMagFilter::kLinear
@ kLinear
IMPELLER_GL_TEXTURE_BORDER_COLOR
#define IMPELLER_GL_TEXTURE_BORDER_COLOR
Definition: gles.h:13
impeller::ProcTableGLES::GetCapabilities
const std::shared_ptr< const CapabilitiesGLES > & GetCapabilities() const
Definition: proc_table_gles.cc:203
impeller::MinMagFilter
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:415
impeller::ProcTableGLES
Definition: proc_table_gles.h:226
proc_table_gles.h
formats_gles.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
impeller::SamplerAddressMode::kMirror
@ kMirror
std
Definition: comparable.h:95
impeller::MipFilter::kLinear
@ kLinear
Sample from the two nearest mip levels and linearly interpolate.
impeller::Sampler::GetDescriptor
const SamplerDescriptor & GetDescriptor() const
Definition: sampler.cc:13
impeller::ToTextureTarget
constexpr std::optional< GLenum > ToTextureTarget(TextureType type)
Definition: formats_gles.h:185
IMPELLER_GL_CLAMP_TO_BORDER
#define IMPELLER_GL_CLAMP_TO_BORDER
Definition: gles.h:12
impeller::TextureGLES
Definition: texture_gles.h:17
impeller
Definition: allocation.cc:12
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...