| 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/renderer/backend/gles/formats_gles.h"
10 #include "impeller/renderer/backend/gles/proc_table_gles.h"
11 #include "impeller/renderer/backend/gles/texture_gles.h"
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 std::optional<MipFilter> mip_filter = std::nullopt) {
21 if (!mip_filter.has_value()) {
22 switch (minmag_filter) {
23 case MinMagFilter::kNearest:
24 return GL_NEAREST;
25 case MinMagFilter::kLinear:
26 return GL_LINEAR;
27 }
28 FML_UNREACHABLE();
29 }
30
31 switch (mip_filter.value()) {
32 case MipFilter::kNearest:
33 switch (minmag_filter) {
34 case MinMagFilter::kNearest:
35 return GL_NEAREST_MIPMAP_NEAREST;
36 case MinMagFilter::kLinear:
37 return GL_LINEAR_MIPMAP_NEAREST;
38 }
39 case MipFilter::kLinear:
40 switch (minmag_filter) {
41 case MinMagFilter::kNearest:
42 return GL_NEAREST_MIPMAP_LINEAR;
43 case MinMagFilter::kLinear:
44 return GL_LINEAR_MIPMAP_LINEAR;
45 }
46 }
47 FML_UNREACHABLE();
48 }
49
50 static GLint ToAddressMode(SamplerAddressMode mode,
51 bool supports_decal_sampler_address_mode) {
52 switch (mode) {
53 case SamplerAddressMode::kClampToEdge:
54 return GL_CLAMP_TO_EDGE;
55 case SamplerAddressMode::kRepeat:
56 return GL_REPEAT;
57 case SamplerAddressMode::kMirror:
58 return GL_MIRRORED_REPEAT;
59 case SamplerAddressMode::kDecal:
60 if (supports_decal_sampler_address_mode) {
61 return IMPELLER_GL_CLAMP_TO_BORDER;
62 } else {
63 return GL_CLAMP_TO_EDGE;
64 }
65 }
66 FML_UNREACHABLE();
67 }
68
69 bool SamplerGLES::ConfigureBoundTexture(const TextureGLES& texture,
70 const ProcTableGLES& gl) const {
71 if (texture.NeedsMipmapGeneration()) {
72 VALIDATION_LOG
73 << "Texture mip count is > 1, but the mipmap has not been generated. "
74 "Texture can not be sampled safely.";
75 return false;
76 }
77
78 auto target = ToTextureTarget(texture.GetTextureDescriptor().type);
79
80 if (!target.has_value()) {
81 return false;
82 }
83 const auto& desc = GetDescriptor();
84
85 std::optional<MipFilter> mip_filter = std::nullopt;
86 if (texture.GetTextureDescriptor().mip_count > 1) {
87 mip_filter = desc.mip_filter;
88 }
89
90 gl.TexParameteri(*target, GL_TEXTURE_MIN_FILTER,
91 ToParam(desc.min_filter, mip_filter));
92 gl.TexParameteri(*target, GL_TEXTURE_MAG_FILTER, ToParam(desc.mag_filter));
93
94 const auto supports_decal_mode =
95 gl.GetCapabilities()->SupportsDecalSamplerAddressMode();
96
97 const auto wrap_s =
98 ToAddressMode(desc.width_address_mode, supports_decal_mode);
99 const auto wrap_t =
100 ToAddressMode(desc.height_address_mode, supports_decal_mode);
101
102 gl.TexParameteri(*target, GL_TEXTURE_WRAP_S, wrap_s);
103 gl.TexParameteri(*target, GL_TEXTURE_WRAP_T, wrap_t);
104
105 if (wrap_s == IMPELLER_GL_CLAMP_TO_BORDER ||
106 wrap_t == IMPELLER_GL_CLAMP_TO_BORDER) {
107 // Transparent black.
108 const GLfloat border_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
109 gl.TexParameterfv(*target, IMPELLER_GL_TEXTURE_BORDER_COLOR, border_color);
110 }
111
112 return true;
113 }
114
115 } // namespace impeller
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:50
impeller::SamplerAddressMode
SamplerAddressMode
Definition: formats.h:423
impeller::Texture::GetTextureDescriptor
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
impeller::SamplerGLES::~SamplerGLES
~SamplerGLES()
formats.h
texture_gles.h
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:42
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:69
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:39
impeller::MipFilter::kNearest
@ kNearest
Sample from the nearest mip level.
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
Definition: formats.h:407
impeller::ProcTableGLES
Definition: proc_table_gles.h:229
proc_table_gles.h
impeller::ToParam
static GLint ToParam(MinMagFilter minmag_filter, std::optional< MipFilter > mip_filter=std::nullopt)
Definition: sampler_gles.cc:19
formats_gles.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::SamplerAddressMode::kMirror
@ kMirror
std
Definition: comparable.h:95
impeller::MipFilter::kLinear
@ kLinear
impeller::Sampler::GetDescriptor
const SamplerDescriptor & GetDescriptor() const
Definition: sampler.cc:13
impeller::ToTextureTarget
constexpr std::optional< GLenum > ToTextureTarget(TextureType type)
Definition: formats_gles.h:184
IMPELLER_GL_CLAMP_TO_BORDER
#define IMPELLER_GL_CLAMP_TO_BORDER
Definition: gles.h:12
impeller::TextureGLES
Definition: texture_gles.h:15
impeller
Definition: aiks_blur_unittests.cc:20
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...