Skip to content

Commit e26b384

Browse files
authored
[Impeller] Validate GLES texture units against the combined limit (#189332)
Fixes #189331. The GLES backend allocates fragment stage texture units after the vertex stage's, but validated the running unit index against the per-stage maximum. On a driver reporting the minimum 16 fragment units (ANGLE on D3D11), a draw using all 16 fragment samplers plus a vertex stage texture failed validation and the render pass aborted, crashing skinned-mesh rendering in Flutter GPU apps on Windows. Texture units are a combined resource in GL; the per-stage limits bound only how many samplers one stage references. This validates the unit index against the combined limit and the per-stage sampler count against the per-stage limit. Adds unit tests for the previously rejected case and both overflow cases. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent 710981c commit e26b384

4 files changed

Lines changed: 161 additions & 1 deletion

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ std::optional<size_t> BufferBindingsGLES::BindTextures(
493493
ShaderStage stage,
494494
size_t unit_start_index) {
495495
size_t active_index = unit_start_index;
496+
size_t stage_texture_count = 0;
496497
for (auto i = 0u; i < texture_range.length; i++) {
497498
const TextureAndSampler& data = bound_textures[texture_range.offset + i];
498499
if (data.stage != stage) {
@@ -513,11 +514,21 @@ std::optional<size_t> BufferBindingsGLES::BindTextures(
513514
//--------------------------------------------------------------------------
514515
/// Set the active texture unit.
515516
///
516-
if (active_index >= gl.GetCapabilities()->GetMaxTextureUnits(stage)) {
517+
/// Units are a combined resource; the per-stage limits cap only how many
518+
/// samplers one stage references, not the unit indices they bind to.
519+
///
520+
stage_texture_count++;
521+
if (stage_texture_count > gl.GetCapabilities()->GetMaxTextureUnits(stage)) {
517522
VALIDATION_LOG << "Texture units specified exceed the capabilities for "
518523
"this shader stage.";
519524
return std::nullopt;
520525
}
526+
if (active_index >=
527+
gl.GetCapabilities()->max_combined_texture_image_units) {
528+
VALIDATION_LOG << "Texture units specified exceed the combined texture "
529+
"unit limit.";
530+
return std::nullopt;
531+
}
521532
gl.ActiveTexture(GL_TEXTURE0 + active_index);
522533

523534
//--------------------------------------------------------------------------

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ FML_TEST_CLASS(BufferBindingsGLESTest, BindUniformData);
2121
FML_TEST_CLASS(BufferBindingsGLESTest, BindArrayData);
2222
FML_TEST_CLASS(BufferBindingsGLESTest, BindUniformDataVerticesAndMatrices);
2323
FML_TEST_CLASS(BufferBindingsGLESTest, BindUniformFailsWithoutFloatType);
24+
FML_TEST_CLASS(BufferBindingsGLESTest,
25+
BindsTexturesAcrossThePerStageUnitBoundary);
26+
FML_TEST_CLASS(BufferBindingsGLESTest, RejectsTexturesBeyondThePerStageLimit);
27+
FML_TEST_CLASS(BufferBindingsGLESTest, RejectsTexturesBeyondTheCombinedLimit);
2428
} // namespace testing
2529

2630
//------------------------------------------------------------------------------
@@ -66,6 +70,12 @@ class BufferBindingsGLES {
6670
BindUniformDataVerticesAndMatrices);
6771
FML_FRIEND_TEST(testing::BufferBindingsGLESTest,
6872
BindUniformFailsWithoutFloatType);
73+
FML_FRIEND_TEST(testing::BufferBindingsGLESTest,
74+
BindsTexturesAcrossThePerStageUnitBoundary);
75+
FML_FRIEND_TEST(testing::BufferBindingsGLESTest,
76+
RejectsTexturesBeyondThePerStageLimit);
77+
FML_FRIEND_TEST(testing::BufferBindingsGLESTest,
78+
RejectsTexturesBeyondTheCombinedLimit);
6979
//----------------------------------------------------------------------------
7080
/// @brief The arguments to glVertexAttribPointer.
7181
///

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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
#include "flutter/testing/testing.h" // IWYU pragma: keep
66
#include "gtest/gtest.h"
77
#include "impeller/core/shader_types.h"
8+
#include "impeller/core/texture_descriptor.h"
89
#include "impeller/renderer/backend/gles/buffer_bindings_gles.h"
910
#include "impeller/renderer/backend/gles/device_buffer_gles.h"
1011
#include "impeller/renderer/backend/gles/formats_gles.h"
1112
#include "impeller/renderer/backend/gles/reactor_gles.h"
13+
#include "impeller/renderer/backend/gles/sampler_library_gles.h"
1214
#include "impeller/renderer/backend/gles/test/mock_gles.h"
15+
#include "impeller/renderer/backend/gles/texture_gles.h"
1316
#include "impeller/renderer/command.h"
1417

1518
namespace impeller {
@@ -19,6 +22,7 @@ const GLint kBlockDataSize = 16;
1922
}
2023

2124
using ::testing::_;
25+
using ::testing::NiceMock;
2226

2327
TEST(BufferBindingsGLESTest, ToVertexAttribTypeSupportedFormats) {
2428
EXPECT_EQ(ToVertexAttribType(VertexAttributeFormat::kFloat32x3),
@@ -361,5 +365,138 @@ TEST(BufferBindingsGLESTest,
361365
/*expected_bound_size=*/32);
362366
}
363367

368+
namespace {
369+
370+
// Owns the reactor, sampler, and metadata behind a set of texture bindings.
371+
struct BoundTexturesFixture {
372+
std::shared_ptr<ReactorGLES> reactor;
373+
std::shared_ptr<TestWorker> worker;
374+
std::unique_ptr<SamplerLibrary> sampler_library;
375+
raw_ptr<const Sampler> sampler;
376+
std::vector<std::unique_ptr<ShaderMetadata>> metadata;
377+
std::vector<TextureAndSampler> bound_textures;
378+
absl::flat_hash_map<std::string, GLint> uniform_bindings;
379+
380+
explicit BoundTexturesFixture(std::unique_ptr<ProcTableGLES> proc_table) {
381+
reactor = std::make_shared<ReactorGLES>(std::move(proc_table));
382+
worker = std::make_shared<TestWorker>();
383+
reactor->AddWorker(worker);
384+
sampler_library = std::make_unique<SamplerLibraryGLES>(
385+
/*supports_decal_sampler_address_mode=*/false);
386+
sampler = sampler_library->GetSampler({});
387+
}
388+
389+
void AddTextures(ShaderStage stage, size_t count) {
390+
for (size_t i = 0; i < count; i++) {
391+
TextureDescriptor desc;
392+
desc.storage_mode = StorageMode::kDevicePrivate;
393+
desc.type = TextureType::kTexture2D;
394+
desc.format = PixelFormat::kR8G8B8A8UNormInt;
395+
desc.size = {1, 1};
396+
desc.mip_count = 1u;
397+
desc.usage = TextureUsage::kShaderRead;
398+
auto texture = std::make_shared<TextureGLES>(reactor, desc);
399+
const std::string name = "tex" + std::to_string(metadata.size());
400+
const std::string key = "TEX" + std::to_string(metadata.size());
401+
uniform_bindings[key] = static_cast<GLint>(100 + metadata.size());
402+
auto meta = std::make_unique<ShaderMetadata>();
403+
meta->name = name;
404+
TextureAndSampler data = {};
405+
data.stage = stage;
406+
data.texture = TextureResource(meta.get(), std::move(texture));
407+
data.sampler = sampler;
408+
metadata.push_back(std::move(meta));
409+
bound_textures.push_back(std::move(data));
410+
}
411+
}
412+
};
413+
414+
// Capabilities of a minimum-spec ES3 driver (16 per stage, 32 combined).
415+
std::unique_ptr<NiceMock<MockGLESImpl>> MakeSixteenUnitMockImpl() {
416+
auto impl = std::make_unique<NiceMock<MockGLESImpl>>();
417+
EXPECT_CALL(*impl, GetIntegerv(_, _))
418+
.WillRepeatedly([](GLenum name, GLint* value) {
419+
switch (name) {
420+
case GL_MAX_TEXTURE_IMAGE_UNITS:
421+
case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
422+
*value = 16;
423+
break;
424+
case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
425+
*value = 32;
426+
break;
427+
default:
428+
break;
429+
}
430+
});
431+
return impl;
432+
}
433+
434+
} // namespace
435+
436+
// One vertex texture pushes the last of 16 fragment samplers onto unit 16,
437+
// which must bind on a 16-per-stage driver since units are combined in GL.
438+
TEST(BufferBindingsGLESTest, BindsTexturesAcrossThePerStageUnitBoundary) {
439+
std::shared_ptr<MockGLES> mock_gl = MockGLES::Init(MakeSixteenUnitMockImpl());
440+
BoundTexturesFixture fixture(
441+
std::make_unique<ProcTableGLES>(kMockResolverGLES));
442+
fixture.AddTextures(ShaderStage::kVertex, 1);
443+
fixture.AddTextures(ShaderStage::kFragment, 16);
444+
ASSERT_TRUE(fixture.reactor->React());
445+
446+
BufferBindingsGLES bindings;
447+
bindings.SetUniformBindings(std::move(fixture.uniform_bindings));
448+
std::vector<BufferResource> bound_buffers;
449+
EXPECT_TRUE(bindings.BindUniformData(
450+
fixture.reactor->GetProcTable(), fixture.bound_textures, bound_buffers,
451+
Range{0, fixture.bound_textures.size()}, Range{0, 0}));
452+
}
453+
454+
// More samplers in one stage than its limit is still rejected.
455+
TEST(BufferBindingsGLESTest, RejectsTexturesBeyondThePerStageLimit) {
456+
std::shared_ptr<MockGLES> mock_gl = MockGLES::Init(MakeSixteenUnitMockImpl());
457+
BoundTexturesFixture fixture(
458+
std::make_unique<ProcTableGLES>(kMockResolverGLES));
459+
fixture.AddTextures(ShaderStage::kFragment, 17);
460+
ASSERT_TRUE(fixture.reactor->React());
461+
462+
BufferBindingsGLES bindings;
463+
bindings.SetUniformBindings(std::move(fixture.uniform_bindings));
464+
std::vector<BufferResource> bound_buffers;
465+
EXPECT_FALSE(bindings.BindUniformData(
466+
fixture.reactor->GetProcTable(), fixture.bound_textures, bound_buffers,
467+
Range{0, fixture.bound_textures.size()}, Range{0, 0}));
468+
}
469+
470+
// Units past the combined limit are rejected even when each stage is within
471+
// its per-stage limit.
472+
TEST(BufferBindingsGLESTest, RejectsTexturesBeyondTheCombinedLimit) {
473+
auto impl = std::make_unique<NiceMock<MockGLESImpl>>();
474+
EXPECT_CALL(*impl, GetIntegerv(_, _))
475+
.WillRepeatedly([](GLenum name, GLint* value) {
476+
switch (name) {
477+
case GL_MAX_TEXTURE_IMAGE_UNITS:
478+
case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
479+
case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
480+
*value = 8;
481+
break;
482+
default:
483+
break;
484+
}
485+
});
486+
std::shared_ptr<MockGLES> mock_gl = MockGLES::Init(std::move(impl));
487+
BoundTexturesFixture fixture(
488+
std::make_unique<ProcTableGLES>(kMockResolverGLES));
489+
fixture.AddTextures(ShaderStage::kVertex, 8);
490+
fixture.AddTextures(ShaderStage::kFragment, 8);
491+
ASSERT_TRUE(fixture.reactor->React());
492+
493+
BufferBindingsGLES bindings;
494+
bindings.SetUniformBindings(std::move(fixture.uniform_bindings));
495+
std::vector<BufferResource> bound_buffers;
496+
EXPECT_FALSE(bindings.BindUniformData(
497+
fixture.reactor->GetProcTable(), fixture.bound_textures, bound_buffers,
498+
Range{0, fixture.bound_textures.size()}, Range{0, 0}));
499+
}
500+
364501
} // namespace testing
365502
} // namespace impeller

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ void mockGetIntegerv(GLenum name, int* value) {
9898
*value = g_extensions.size();
9999
} break;
100100
case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
101+
// Minimum default; a registered mock may overwrite it.
101102
*value = 8;
103+
CallMockMethod(&IMockGLESImpl::GetIntegerv, name, value);
102104
break;
103105
case GL_MAX_LABEL_LENGTH_KHR:
104106
*value = 64;

0 commit comments

Comments
 (0)