Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
13namespace impeller {
14
15SamplerGLES::SamplerGLES(const SamplerDescriptor& desc) : Sampler(desc) {}
16
17SamplerGLES::~SamplerGLES() = default;
18
19static GLint ToParam(MinMagFilter minmag_filter) {
20 switch (minmag_filter) {
21 case MinMagFilter::kNearest:
22 return GL_NEAREST;
23 case MinMagFilter::kLinear:
24 return GL_LINEAR;
25 }
27}
28
29static GLint ToParam(MinMagFilter minmag_filter, MipFilter mip_filter) {
30 switch (mip_filter) {
31 case MipFilter::kBase:
32 return ToParam(minmag_filter);
33 case MipFilter::kNearest:
34 switch (minmag_filter) {
35 case MinMagFilter::kNearest:
36 return GL_NEAREST_MIPMAP_NEAREST;
37 case MinMagFilter::kLinear:
38 return GL_LINEAR_MIPMAP_NEAREST;
39 }
40 case MipFilter::kLinear:
41 switch (minmag_filter) {
42 case MinMagFilter::kNearest:
43 return GL_NEAREST_MIPMAP_LINEAR;
44 case MinMagFilter::kLinear:
45 return GL_LINEAR_MIPMAP_LINEAR;
46 }
47 }
49}
50
52 bool supports_decal_sampler_address_mode) {
53 switch (mode) {
54 case SamplerAddressMode::kClampToEdge:
55 return GL_CLAMP_TO_EDGE;
56 case SamplerAddressMode::kRepeat:
57 return GL_REPEAT;
58 case SamplerAddressMode::kMirror:
59 return GL_MIRRORED_REPEAT;
60 case SamplerAddressMode::kDecal:
61 if (supports_decal_sampler_address_mode) {
63 } else {
64 return GL_CLAMP_TO_EDGE;
65 }
66 }
68}
69
70bool SamplerGLES::ConfigureBoundTexture(const TextureGLES& texture,
71 const ProcTableGLES& gl) const {
72 auto target = ToTextureTarget(texture.GetTextureDescriptor().type);
73
74 if (!target.has_value()) {
75 return false;
76 }
77 const SamplerDescriptor& desc = GetDescriptor();
78
79 GLint mag_filter = ToParam(desc.mag_filter);
80
81 // If the texture doesn't have mipmaps, we can't use mip filtering.
82 GLint min_filter;
83 if (texture.GetTextureDescriptor().mip_count > 1) {
84 min_filter = ToParam(desc.min_filter, desc.mip_filter);
85 } else {
86 min_filter = ToParam(desc.min_filter);
87 }
88
89 gl.TexParameteri(*target, GL_TEXTURE_MIN_FILTER, min_filter);
90 gl.TexParameteri(*target, GL_TEXTURE_MAG_FILTER, mag_filter);
91
92 // Bound the sampled mip range to the levels the texture declares. GLES leaves
93 // GL_TEXTURE_MAX_LEVEL at its default of 1000, so a texture sampled with a
94 // mipmap filter reads as black unless every level down to 1x1 is defined.
95 // Metal and Vulkan allocate exactly mip_count levels, and clamping here gives
96 // the same behavior for a partial, manually uploaded mip chain. The parameter
97 // is unavailable on ES 2.0 without GL_APPLE_texture_max_level, and external
98 // textures have no mip levels, so it is skipped in those cases.
99 if (*target != GL_TEXTURE_EXTERNAL_OES &&
100 gl.GetCapabilities()->SupportsTextureMaxLevel()) {
101 const GLint max_level =
102 static_cast<GLint>(texture.GetTextureDescriptor().mip_count) - 1;
103 gl.TexParameteri(*target, GL_TEXTURE_MAX_LEVEL, max_level);
104 }
105
106 const auto supports_decal_mode =
107 gl.GetCapabilities()->SupportsDecalSamplerAddressMode();
108
109 const auto wrap_s =
110 ToAddressMode(desc.width_address_mode, supports_decal_mode);
111 const auto wrap_t =
112 ToAddressMode(desc.height_address_mode, supports_decal_mode);
113
114 gl.TexParameteri(*target, GL_TEXTURE_WRAP_S, wrap_s);
115 gl.TexParameteri(*target, GL_TEXTURE_WRAP_T, wrap_t);
116
117 if (wrap_s == IMPELLER_GL_CLAMP_TO_BORDER ||
118 wrap_t == IMPELLER_GL_CLAMP_TO_BORDER) {
119 // Transparent black.
120 const GLfloat border_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
121 gl.TexParameterfv(*target, IMPELLER_GL_TEXTURE_BORDER_COLOR, border_color);
122 }
123
124 return true;
125}
126
127} // namespace impeller
const std::shared_ptr< const CapabilitiesGLES > & GetCapabilities() const
uint32_t * target
#define FML_UNREACHABLE()
Definition logging.h:128
FlTexture * texture
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
constexpr std::optional< GLenum > ToTextureTarget(TextureType type)
static GLint ToAddressMode(SamplerAddressMode mode, bool supports_decal_sampler_address_mode)
SamplerAddressMode
Definition formats.h:618
static GLint ToParam(MinMagFilter minmag_filter)
MipFilter
Options for selecting and filtering between mipmap levels.
Definition formats.h:602
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition formats.h:592
#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