Skip to content

Commit 05a9902

Browse files
committed
Address review comments on 2D texture array PR
Restore slice_mip_initialized_ to an inline std::array of 6 entries. Array textures need only one entry because glTexImage3D allocates every layer of a mip level at once, so there is nothing per-layer to track. This undoes the heap-allocated std::vector and its locality regression. Also switch the new glTex(Sub)Image3D calls to /*arg=*/ argument comments, use EXPECT in the array-layer-count allocator test, and add a TODO for honoring GL_EXT_texture_array on ES 2.0 contexts.
1 parent 95ec6fe commit 05a9902

4 files changed

Lines changed: 69 additions & 70 deletions

File tree

‎engine/src/flutter/impeller/core/allocator_unittests.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ TEST(AllocatorTest, TextureDescriptorCompatibility) {
8686
TextureDescriptor desc_c = {.type = TextureType::kTexture2DArray,
8787
.array_layer_count = 8};
8888

89-
ASSERT_EQ(desc_a, desc_b);
90-
ASSERT_NE(desc_a, desc_c);
89+
EXPECT_EQ(desc_a, desc_b);
90+
EXPECT_NE(desc_a, desc_c);
9191
}
9292
}
9393

‎engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ CapabilitiesGLES::CapabilitiesGLES(const ProcTableGLES& gl) {
208208
desc->HasExtension(kAppleTextureMaxLevelExt);
209209

210210
// GL_TEXTURE_2D_ARRAY and sampler2DArray are core in desktop GL 3.0 and
211-
// OpenGL ES 3.0; there is no ES 2.0 fallback.
211+
// OpenGL ES 3.0. ES 2.0 has no core fallback, though some ES 2.0 devices
212+
// expose them via GL_EXT_texture_array.
213+
// TODO(bdero): Also honor GL_EXT_texture_array on ES 2.0 contexts that expose
214+
// it, resolving the array procs from the extension.
212215
supports_texture_array_ = desc->GetGlVersion().major_version >= 3;
213216

214217
// Anisotropic filtering is not part of any core GL or GLES version; it is

‎engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc‎

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,6 @@ TextureGLES::TextureGLES(std::shared_ptr<ReactorGLES> reactor,
164164
return;
165165
}
166166

167-
// One storage-tracking entry per slice: 6 for a cube, the layer count for an
168-
// array, 1 otherwise.
169-
const auto& tracked_desc = GetTextureDescriptor();
170-
size_t slice_count = 1u;
171-
if (tracked_desc.type == TextureType::kTextureCube) {
172-
slice_count = 6u;
173-
} else if (tracked_desc.type == TextureType::kTexture2DArray) {
174-
slice_count = tracked_desc.array_layer_count;
175-
}
176-
slice_mip_initialized_.resize(slice_count);
177167
// Ensure the texture doesn't exceed device capabilities.
178168
const auto tex_size = GetTextureDescriptor().size;
179169
const auto max_size =
@@ -340,29 +330,30 @@ bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
340330
// requested layer (slice) of the already-allocated base mip level.
341331
if (format.is_compressed) {
342332
gl.CompressedTexSubImage3D(
343-
texture_target, // target
344-
0u, // LOD level
345-
0u, // x offset
346-
0u, // y offset
347-
static_cast<GLint>(slice), // z offset (layer)
348-
size.width, // width
349-
size.height, // height
350-
1, // depth (one layer)
351-
format.internal_format, // format
352-
image_size, // image size
353-
tex_data); // data
333+
/*target=*/texture_target, //
334+
/*level=*/0u, //
335+
/*xoffset=*/0u, //
336+
/*yoffset=*/0u, //
337+
/*zoffset=*/static_cast<GLint>(slice), //
338+
/*width=*/size.width, //
339+
/*height=*/size.height, //
340+
/*depth=*/1, //
341+
/*format=*/format.internal_format, //
342+
/*image_size=*/image_size, //
343+
/*data=*/tex_data); //
354344
} else {
355-
gl.TexSubImage3D(texture_target, // target
356-
0u, // LOD level
357-
0u, // x offset
358-
0u, // y offset
359-
static_cast<GLint>(slice), // z offset (layer)
360-
size.width, // width
361-
size.height, // height
362-
1, // depth (one layer)
363-
format.external_format, // format
364-
format.type, // type
365-
tex_data); // data
345+
gl.TexSubImage3D(
346+
/*target=*/texture_target, //
347+
/*level=*/0u, //
348+
/*xoffset=*/0u, //
349+
/*yoffset=*/0u, //
350+
/*zoffset=*/static_cast<GLint>(slice), //
351+
/*width=*/size.width, //
352+
/*height=*/size.height, //
353+
/*depth=*/1, //
354+
/*format=*/format.external_format, //
355+
/*type=*/format.type, //
356+
/*data=*/tex_data); //
366357
}
367358
} else if (format.is_compressed) {
368359
gl.CompressedTexImage2D(texture_target, // target
@@ -525,21 +516,21 @@ void TextureGLES::InitializeContentsIfNecessary() {
525516
// glTexImage3D call; individual layers are then filled with
526517
// glTexSubImage3D. Non-zero mip levels are allocated lazily.
527518
gl.BindTexture(GL_TEXTURE_2D_ARRAY, handle.value());
528-
gl.TexImage3D(GL_TEXTURE_2D_ARRAY, // target
529-
0u, // LOD level
530-
gles_format->internal_format, // internal
531-
size.width, // width
532-
size.height, // height
533-
desc.array_layer_count, // depth/layers
534-
0u, // border
535-
gles_format->external_format, // format
536-
gles_format->type, // type
537-
nullptr // data
519+
gl.TexImage3D(
520+
/*target=*/GL_TEXTURE_2D_ARRAY, //
521+
/*level=*/0u, //
522+
/*internal_format=*/gles_format->internal_format, //
523+
/*width=*/size.width, //
524+
/*height=*/size.height, //
525+
/*depth=*/desc.array_layer_count, //
526+
/*border=*/0u, //
527+
/*format=*/gles_format->external_format, //
528+
/*type=*/gles_format->type, //
529+
/*data=*/nullptr //
538530
);
539-
for (size_t layer = 0;
540-
layer < static_cast<size_t>(desc.array_layer_count); ++layer) {
541-
MarkSliceMipLevelInitialized(layer, 0);
542-
}
531+
// glTexImage3D allocated every layer of the base level at once, so a
532+
// single entry covers the whole level.
533+
MarkSliceMipLevelInitialized(0, 0);
543534
} else {
544535
// 2D / multisampled. External-OES textures are always wrapped, so
545536
// they returned at the is_wrapped_ check above. Only the base mip
@@ -760,24 +751,27 @@ bool TextureGLES::EnsureSliceMipLevelStorage(size_t slice, size_t mip_level) {
760751
static_cast<GLsizei>(std::max<int64_t>(1, size.height >> mip_level));
761752

762753
if (desc.type == TextureType::kTexture2DArray) {
763-
// glTexImage3D allocates this mip level for every layer at once, so mark
764-
// all layers initialized for this level.
754+
// glTexImage3D allocates this mip level for every layer at once, so a
755+
// single entry (slice 0) tracks the whole level regardless of which layer
756+
// was requested. Guard on that entry so a request for a non-zero layer
757+
// does not re-allocate and orphan already-uploaded layers.
758+
if (IsSliceMipLevelInitialized(0, mip_level)) {
759+
return true;
760+
}
765761
gl.BindTexture(GL_TEXTURE_2D_ARRAY, handle.value());
766-
gl.TexImage3D(GL_TEXTURE_2D_ARRAY, // target
767-
static_cast<GLint>(mip_level), // LOD level
768-
gles_format->internal_format, // internal
769-
mip_width, // width
770-
mip_height, // height
771-
desc.array_layer_count, // depth/layers
772-
0u, // border
773-
gles_format->external_format, // format
774-
gles_format->type, // type
775-
nullptr // data
762+
gl.TexImage3D(
763+
/*target=*/GL_TEXTURE_2D_ARRAY, //
764+
/*level=*/static_cast<GLint>(mip_level), //
765+
/*internal_format=*/gles_format->internal_format, //
766+
/*width=*/mip_width, //
767+
/*height=*/mip_height, //
768+
/*depth=*/desc.array_layer_count, //
769+
/*border=*/0u, //
770+
/*format=*/gles_format->external_format, //
771+
/*type=*/gles_format->type, //
772+
/*data=*/nullptr //
776773
);
777-
for (size_t layer = 0; layer < static_cast<size_t>(desc.array_layer_count);
778-
++layer) {
779-
MarkSliceMipLevelInitialized(layer, mip_level);
780-
}
774+
MarkSliceMipLevelInitialized(0, mip_level);
781775
return true;
782776
}
783777

‎engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <array>
99
#include <bitset>
10-
#include <vector>
1110

1211
#include "fml/logging.h"
1312
#include "impeller/base/backend_cast.h"
@@ -189,11 +188,14 @@ class TextureGLES final : public Texture,
189188
// pipeline) keeps its single base-level allocation, and per-level uploads
190189
// only pay for the levels they actually touch.
191190
//
192-
// One entry per slice (1 for 2D, 6 for a cube, the layer count for an
193-
// array), sized at construction. Each tracks up to 16 mip levels (covers a
194-
// 32k base dimension); requested levels above this are simply not tracked.
191+
// One entry per independently-allocated slice: 6 for a cube (each face is a
192+
// separate `glTexImage2D`), 1 otherwise. Array textures also use a single
193+
// entry because `glTexImage3D` allocates every layer of a mip level at once,
194+
// so there is nothing per-layer to track. Each entry covers up to 16 mip
195+
// levels (a 32k base dimension); requested levels above this are simply not
196+
// tracked.
195197
static constexpr size_t kMaxTrackedMipLevels = 16;
196-
std::vector<std::bitset<kMaxTrackedMipLevels>> slice_mip_initialized_;
198+
std::array<std::bitset<kMaxTrackedMipLevels>, 6> slice_mip_initialized_ = {};
197199
const bool is_wrapped_;
198200
const std::optional<GLuint> wrapped_fbo_;
199201
UniqueHandleGLES cached_fbo_;

0 commit comments

Comments
 (0)