Skip to content

Commit d41651d

Browse files
committed
[Flutter GPU] Add 2D texture array support
TextureType.texture2DArray plus a layerCount argument on GpuContext.createTexture create the texture, Texture.overwrite and ColorAttachment.slice address each layer as a slice, and GpuContext.doesSupportTextureArrays reports availability. Capabilities::SupportsTextureArrays reports backend support, BlitPass::AddCopy validates slices against the texture type rather than a fixed cubemap bound, and the GLES backend gains buffer-to-array-layer blits. The capability reports false on GLES for now because shader bundles cannot carry an ES 3.0 shader variant yet, so user shaders there cannot sample arrays.
1 parent 9b8cb9c commit d41651d

24 files changed

Lines changed: 388 additions & 36 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
uniform sampler2DArray tex;
6+
7+
uniform FragInfo {
8+
float layer;
9+
}
10+
frag_info;
11+
12+
in vec2 v_texture_coords;
13+
in vec4 v_color;
14+
out vec4 frag_color;
15+
16+
void main() {
17+
frag_color =
18+
v_color * texture(tex, vec3(v_texture_coords, frag_info.layer));
19+
}

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

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,14 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
182182
texture_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
183183
break;
184184
case TextureType::kTexture2DArray:
185-
// TODO(bdero): Upload into 2D array layers via glTexSubImage3D from the
186-
// blit path. Direct uploads via Texture::SetContents are supported.
187-
VALIDATION_LOG << "Blitting into a 2D array texture is not yet supported "
188-
"on the OpenGLES backend.";
189-
return false;
185+
if (!reactor.GetProcTable().GetCapabilities()->SupportsTextureArrays()) {
186+
VALIDATION_LOG
187+
<< "2D array textures are not supported on this context.";
188+
return false;
189+
}
190+
texture_type = GL_TEXTURE_2D_ARRAY;
191+
texture_target = GL_TEXTURE_2D_ARRAY;
192+
break;
190193
case TextureType::kTextureExternalOES:
191194
texture_type = GL_TEXTURE_EXTERNAL_OES;
192195
texture_target = GL_TEXTURE_EXTERNAL_OES;
@@ -218,6 +221,14 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
218221
// sub-image; glCompressedTexImage2D redefines the entire mip level. Require
219222
// the upload to cover the full mip level starting at the origin.
220223
if (gles_format->is_compressed) {
224+
if (tex_descriptor.type == TextureType::kTexture2DArray) {
225+
// TODO(bdero): Support compressed 2D array uploads. The level must be
226+
// allocated with glCompressedTexImage3D covering every layer and filled
227+
// per layer with glCompressedTexSubImage3D.
228+
VALIDATION_LOG << "Compressed 2D array textures are not yet supported "
229+
"on the OpenGLES backend.";
230+
return false;
231+
}
221232
const auto mip_width =
222233
std::max<int32_t>(1, tex_descriptor.size.width >> mip_level);
223234
const auto mip_height =
@@ -242,6 +253,44 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
242253
return true;
243254
}
244255

256+
if (tex_descriptor.type == TextureType::kTexture2DArray) {
257+
// glTexImage3D allocates this mip level for every layer at once, so the
258+
// level's storage is tracked with a single entry (slice 0).
259+
if (!texture_gles.IsSliceMipLevelInitialized(0, mip_level)) {
260+
const auto level_width =
261+
std::max<int32_t>(1, tex_descriptor.size.width >> mip_level);
262+
const auto level_height =
263+
std::max<int32_t>(1, tex_descriptor.size.height >> mip_level);
264+
gl.TexImage3D(
265+
/*target=*/texture_target, //
266+
/*level=*/mip_level, //
267+
/*internal_format=*/gles_format->internal_format, //
268+
/*width=*/level_width, //
269+
/*height=*/level_height, //
270+
/*depth=*/tex_descriptor.array_layer_count, //
271+
/*border=*/0u, //
272+
/*format=*/gles_format->external_format, //
273+
/*type=*/gles_format->type, //
274+
/*data=*/nullptr //
275+
);
276+
texture_gles.MarkSliceMipLevelInitialized(0, mip_level);
277+
}
278+
gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
279+
gl.TexSubImage3D(
280+
/*target=*/texture_target, //
281+
/*level=*/mip_level, //
282+
/*xoffset=*/destination_region.GetX(), //
283+
/*yoffset=*/destination_region.GetY(), //
284+
/*zoffset=*/static_cast<GLint>(slice), //
285+
/*width=*/destination_region.GetWidth(), //
286+
/*height=*/destination_region.GetHeight(), //
287+
/*depth=*/1, //
288+
/*format=*/gles_format->external_format, //
289+
/*type=*/gles_format->type, //
290+
/*data=*/tex_data); //
291+
return true;
292+
}
293+
245294
// GL_INVALID_OPERATION if the requested mip level has not been defined by
246295
// a previous glTexImage2D operation. Allocate the requested mip lazily on
247296
// first write, only for the level the upload is actually targeting. The

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ bool CapabilitiesGLES::SupportsTextureMaxLevel() const {
249249
return supports_texture_max_level_;
250250
}
251251

252-
bool CapabilitiesGLES::SupportsTextureArray() const {
252+
bool CapabilitiesGLES::SupportsTextureArrays() const {
253253
return supports_texture_array_;
254254
}
255255

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class CapabilitiesGLES final
9797
/// available below them through GL_EXT_texture_array (desktop GL 2.x)
9898
/// or GL_NV_texture_array (OpenGL ES 2.0). When absent, callers must
9999
/// fall back to a texture atlas.
100-
bool SupportsTextureArray() const;
100+
// |Capabilities|
101+
bool SupportsTextureArrays() const override;
101102

102103
// |Capabilities|
103104
bool SupportsOffscreenMSAA() const override;

‎engine/src/flutter/impeller/renderer/backend/gles/test/capabilities_unittests.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ TEST(CapabilitiesGLES, SupportsTextureArrayOnES3) {
9393
// 2D array textures are core on OpenGL ES 3.0, no extension needed.
9494
auto mock_gles = MockGLES::Init(std::nullopt, "OpenGL ES 3.0");
9595
auto capabilities = mock_gles->GetProcTable().GetCapabilities();
96-
EXPECT_TRUE(capabilities->SupportsTextureArray());
96+
EXPECT_TRUE(capabilities->SupportsTextureArrays());
9797
}
9898

9999
TEST(CapabilitiesGLES, DoesNotSupportTextureArrayOnES2WithoutExtension) {
100100
auto const extensions = std::vector<const char*>{"GL_KHR_debug"};
101101
auto mock_gles = MockGLES::Init(extensions, "OpenGL ES 2.0");
102102
auto capabilities = mock_gles->GetProcTable().GetCapabilities();
103-
EXPECT_FALSE(capabilities->SupportsTextureArray());
103+
EXPECT_FALSE(capabilities->SupportsTextureArrays());
104104
}
105105

106106
TEST(CapabilitiesGLES, SupportsTextureArrayViaNVExtensionOnES2) {
@@ -112,7 +112,7 @@ TEST(CapabilitiesGLES, SupportsTextureArrayViaNVExtensionOnES2) {
112112
};
113113
auto mock_gles = MockGLES::Init(extensions, "OpenGL ES 2.0");
114114
auto capabilities = mock_gles->GetProcTable().GetCapabilities();
115-
EXPECT_TRUE(capabilities->SupportsTextureArray());
115+
EXPECT_TRUE(capabilities->SupportsTextureArrays());
116116
}
117117

118118
TEST(CapabilitiesGLES, SupportsTextureArrayViaEXTExtension) {
@@ -124,7 +124,7 @@ TEST(CapabilitiesGLES, SupportsTextureArrayViaEXTExtension) {
124124
};
125125
auto mock_gles = MockGLES::Init(extensions, "OpenGL ES 2.0");
126126
auto capabilities = mock_gles->GetProcTable().GetCapabilities();
127-
EXPECT_TRUE(capabilities->SupportsTextureArray());
127+
EXPECT_TRUE(capabilities->SupportsTextureArrays());
128128
}
129129

130130
} // namespace testing

‎engine/src/flutter/impeller/renderer/backend/gles/test/texture_gles_unittests.cc‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
#include "impeller/base/validation.h"
1313
#include "impeller/core/formats.h"
1414
#include "impeller/core/texture_descriptor.h"
15+
#include "impeller/core/buffer_view.h"
16+
#include "impeller/core/range.h"
1517
#include "impeller/renderer/backend/gles/handle_gles.h"
1618
#include "impeller/renderer/backend/gles/proc_table_gles.h"
1719
#include "impeller/renderer/backend/gles/test/mock_gles.h"
20+
#include "impeller/renderer/blit_pass.h"
21+
#include "impeller/renderer/command_buffer.h"
22+
#include "impeller/renderer/command_queue.h"
1823

1924
namespace impeller::testing {
2025

@@ -155,24 +160,25 @@ TEST_P(TextureGLESTest, CanCreateAndUpload2DArrayTexture) {
155160
if (!context_gles.GetReactor()
156161
->GetProcTable()
157162
.GetCapabilities()
158-
->SupportsTextureArray()) {
163+
->SupportsTextureArrays()) {
159164
GTEST_SKIP() << "2D array textures are not supported on this context.";
160165
}
161166

167+
// More than 6 layers, to cover slices beyond the cubemap face range.
162168
TextureDescriptor desc;
163169
desc.storage_mode = StorageMode::kHostVisible;
164170
desc.size = {2, 2};
165171
desc.format = PixelFormat::kR8G8B8A8UNormInt;
166172
desc.type = TextureType::kTexture2DArray;
167-
desc.array_layer_count = 3;
173+
desc.array_layer_count = 8;
168174
desc.mip_count = 1;
169175

170176
auto texture = GetContext()->GetResourceAllocator()->CreateTexture(desc);
171177
ASSERT_TRUE(texture);
172178
EXPECT_EQ(static_cast<int>(texture->GetTextureDescriptor().array_layer_count),
173-
3);
174-
EXPECT_TRUE(texture->IsSliceValid(2));
175-
EXPECT_FALSE(texture->IsSliceValid(3));
179+
8);
180+
EXPECT_TRUE(texture->IsSliceValid(7));
181+
EXPECT_FALSE(texture->IsSliceValid(8));
176182

177183
// Every layer can be uploaded.
178184
std::vector<uint8_t> layer(2u * 2u * 4u, 0xFF);
@@ -181,6 +187,26 @@ TEST_P(TextureGLESTest, CanCreateAndUpload2DArrayTexture) {
181187
EXPECT_TRUE(texture->SetContents(layer.data(), layer.size(), slice));
182188
}
183189
EXPECT_TRUE(context_gles.GetReactor()->React());
190+
191+
// Every layer can also be uploaded through the blit path.
192+
auto staging = GetContext()->GetResourceAllocator()->CreateBufferWithCopy(
193+
layer.data(), layer.size());
194+
ASSERT_TRUE(staging);
195+
auto command_buffer = GetContext()->CreateCommandBuffer();
196+
ASSERT_TRUE(command_buffer);
197+
auto blit_pass = command_buffer->CreateBlitPass();
198+
ASSERT_TRUE(blit_pass);
199+
for (size_t slice = 0; slice < static_cast<size_t>(desc.array_layer_count);
200+
++slice) {
201+
EXPECT_TRUE(blit_pass->AddCopy(BufferView(staging, Range(0, layer.size())),
202+
texture, std::nullopt,
203+
/*label=*/"ArrayLayerUpload",
204+
/*mip_level=*/0, slice));
205+
}
206+
EXPECT_TRUE(blit_pass->EncodeCommands());
207+
EXPECT_TRUE(
208+
GetContext()->GetCommandQueue()->Submit({std::move(command_buffer)}).ok());
209+
EXPECT_TRUE(context_gles.GetReactor()->React());
184210
}
185211

186212
TEST_P(TextureGLESTest, CreatingAndBindingEmptyTexturesDoesNotCrash) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
280280
// Bail out synchronously on contexts without array support (e.g. ES 2.0).
281281
// The glTexImage3D/glTexSubImage3D procs are null there, so queuing the
282282
// upload would dereference a null proc on the reactor thread.
283-
if (!reactor_->GetProcTable().GetCapabilities()->SupportsTextureArray()) {
283+
if (!reactor_->GetProcTable().GetCapabilities()->SupportsTextureArrays()) {
284284
VALIDATION_LOG << "2D array textures are not supported on this context.";
285285
return false;
286286
}
@@ -506,7 +506,7 @@ void TextureGLES::InitializeContentsIfNecessary() {
506506
MarkSliceMipLevelInitialized(face, 0);
507507
}
508508
} else if (desc.type == TextureType::kTexture2DArray) {
509-
if (!gl.GetCapabilities()->SupportsTextureArray()) {
509+
if (!gl.GetCapabilities()->SupportsTextureArrays()) {
510510
VALIDATION_LOG << "2D array textures are not supported on this "
511511
"context.";
512512
return;

‎engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ bool CapabilitiesVK::SupportsManuallyMippedTextures() const {
558558
return true;
559559
}
560560

561+
bool CapabilitiesVK::SupportsTextureArrays() const {
562+
return true;
563+
}
564+
561565
void CapabilitiesVK::SetOffscreenFormat(PixelFormat pixel_format) const {
562566
default_color_format_ = pixel_format;
563567
}

‎engine/src/flutter/impeller/renderer/backend/vulkan/capabilities_vk.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ class CapabilitiesVK final : public Capabilities,
275275
// |Capabilities|
276276
bool SupportsManuallyMippedTextures() const override;
277277

278+
// |Capabilities|
279+
bool SupportsTextureArrays() const override;
280+
278281
// |Capabilities|
279282
bool SupportsExtendedRangeFormats() const override;
280283

‎engine/src/flutter/impeller/renderer/blit_pass.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ bool BlitPass::AddCopy(BufferView source,
152152
<< destination->GetMipCount() << " mip levels.";
153153
return false;
154154
}
155-
if (slice > 5) {
155+
if (!destination->IsSliceValid(slice)) {
156156
VALIDATION_LOG << "Invalid value for slice: " << slice;
157157
return false;
158158
}

0 commit comments

Comments
 (0)