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