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