| 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
5 #include "impeller/renderer/backend/gles/sampler_gles.h"
6
7 #include "impeller/base/validation.h"
8 #include "impeller/core/formats.h"
9 #include "impeller/core/sampler_descriptor.h"
10 #include "impeller/renderer/backend/gles/formats_gles.h"
11 #include "impeller/renderer/backend/gles/proc_table_gles.h"
12 #include "impeller/renderer/backend/gles/texture_gles.h"
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
52 static GLint ToAddressMode(SamplerAddressMode mode,
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) {
63 return IMPELLER_GL_CLAMP_TO_BORDER;
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()) {
74 VALIDATION_LOG
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
impeller::ProcTableGLES
Definition: proc_table_gles.h:282
impeller::ProcTableGLES::GetCapabilities
const std::shared_ptr< const CapabilitiesGLES > & GetCapabilities() const
Definition: proc_table_gles.cc:211
impeller::TextureGLES
Definition: texture_gles.h:19
impeller::Texture::GetTextureDescriptor
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
impeller::Texture::NeedsMipmapGeneration
bool NeedsMipmapGeneration() const
Definition: texture.cc:85
formats.h
formats_gles.h
impeller
Definition: allocation.cc:12
impeller::ToAddressMode
static GLint ToAddressMode(SamplerAddressMode mode, bool supports_decal_sampler_address_mode)
Definition: sampler_gles.cc:52
impeller::SamplerAddressMode
SamplerAddressMode
Definition: formats.h:441
impeller::ToParam
static GLint ToParam(MinMagFilter minmag_filter, MipFilter mip_filter)
Definition: sampler_gles.cc:30
impeller::MipFilter
MipFilter
Options for selecting and filtering between mipmap levels.
Definition: formats.h:425
impeller::ToTextureTarget
constexpr std::optional< GLenum > ToTextureTarget(TextureType type)
Definition: formats_gles.h:185
impeller::MinMagFilter
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:415
proc_table_gles.h
IMPELLER_GL_TEXTURE_BORDER_COLOR
#define IMPELLER_GL_TEXTURE_BORDER_COLOR
Definition: gles.h:13
IMPELLER_GL_CLAMP_TO_BORDER
#define IMPELLER_GL_CLAMP_TO_BORDER
Definition: gles.h:12
sampler_descriptor.h
sampler_gles.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:17
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:19
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:20
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:16
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:15
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:43
texture_gles.h
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91