Skip to content

Commit 7adec80

Browse files
authored
[wimp] fixes ubo padding size issue (flutter#189958)
issue flutter#187212 This clamps the minimum bound size of a UBO to GL_UNIFORM_BLOCK_DATA_SIZE. This was causing a crash in firefox's webgl2 implementation. ## 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 5506de2 commit 7adec80

5 files changed

Lines changed: 222 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,13 @@ bool BufferBindingsGLES::ReadUniformsBindingsV3(const ProcTableGLES& gl,
130130

131131
GLuint block_index = gl.GetUniformBlockIndex(program, name.data());
132132
gl.UniformBlockBinding(program_handle_, block_index, i);
133+
GLint block_data_size = 0;
134+
gl.GetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_DATA_SIZE,
135+
&block_data_size);
133136

134137
ubo_locations_[std::string{name.data(), static_cast<size_t>(length)}] =
135-
std::make_pair(block_index, i);
138+
UBOInfo{static_cast<GLint>(block_index), static_cast<GLuint>(i),
139+
block_data_size};
136140
}
137141
use_ubo_ = true;
138142
return ReadUniformsBindingsV2(gl, program);
@@ -370,15 +374,14 @@ bool BufferBindingsGLES::BindUniformBufferV3(
370374
const BufferView& buffer,
371375
const ShaderMetadata* metadata,
372376
const DeviceBufferGLES& device_buffer_gles) {
373-
absl::flat_hash_map<std::string, std::pair<GLint, GLuint>>::iterator it =
374-
ubo_locations_.find(metadata->name);
377+
auto it = ubo_locations_.find(metadata->name);
375378
if (it == ubo_locations_.end()) {
376379
// This should only happen if we have GLESv3 but are using v2 shaders,
377380
// as GLESv3 shaders compiled by impeller always have
378381
// **named** uniform buffer blocks
379382
return BindUniformBufferV2(gl, buffer, metadata, device_buffer_gles);
380383
}
381-
const auto& [block_index, binding_point] = it->second;
384+
const auto& ubo_info = it->second;
382385
if (!device_buffer_gles.BindAndUploadDataIfNecessary(
383386
DeviceBufferGLES::BindingType::kUniformBuffer)) {
384387
return false;
@@ -387,8 +390,15 @@ bool BufferBindingsGLES::BindUniformBufferV3(
387390
if (!handle.has_value()) {
388391
return false;
389392
}
390-
gl.BindBufferRange(GL_UNIFORM_BUFFER, binding_point, handle.value(),
391-
buffer.GetRange().offset, buffer.GetRange().length);
393+
size_t length = std::max<size_t>(buffer.GetRange().length,
394+
static_cast<size_t>(ubo_info.data_size));
395+
if (buffer.GetRange().offset + length >
396+
device_buffer_gles.GetDeviceBufferDescriptor().size) {
397+
VALIDATION_LOG << "Uniform buffer range exceeds device buffer size.";
398+
return false;
399+
}
400+
gl.BindBufferRange(GL_UNIFORM_BUFFER, ubo_info.binding_point, handle.value(),
401+
buffer.GetRange().offset, length);
392402
return true;
393403
}
394404

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ class BufferBindingsGLES {
8282
std::vector<std::vector<VertexAttribPointer>> vertex_attrib_arrays_;
8383

8484
absl::flat_hash_map<std::string, GLint> uniform_locations_;
85-
absl::flat_hash_map<std::string, std::pair<GLint, GLuint>> ubo_locations_;
85+
struct UBOInfo {
86+
GLint block_index = 0;
87+
GLuint binding_point = 0;
88+
GLint data_size = 0;
89+
};
90+
absl::flat_hash_map<std::string, UBOInfo> ubo_locations_;
8691

8792
using BindingMap = absl::flat_hash_map<std::string, std::vector<GLint>>;
8893
BindingMap binding_map_ = {};

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
#include "impeller/renderer/backend/gles/buffer_bindings_gles.h"
99
#include "impeller/renderer/backend/gles/device_buffer_gles.h"
1010
#include "impeller/renderer/backend/gles/formats_gles.h"
11+
#include "impeller/renderer/backend/gles/reactor_gles.h"
1112
#include "impeller/renderer/backend/gles/test/mock_gles.h"
1213
#include "impeller/renderer/command.h"
1314

1415
namespace impeller {
1516
namespace testing {
17+
namespace {
18+
const GLint kBlockDataSize = 16;
19+
}
1620

1721
using ::testing::_;
1822

@@ -265,5 +269,97 @@ TEST(BufferBindingsGLESTest, BindVertexAttributesSetsInstanceRateDivisor) {
265269
/*instance=*/0));
266270
}
267271

272+
namespace {
273+
class TestWorker : public ReactorGLES::Worker {
274+
public:
275+
bool CanReactorReactOnCurrentThreadNow(
276+
const ReactorGLES& reactor) const override {
277+
return true;
278+
}
279+
};
280+
} // namespace
281+
282+
void TestBindUniformBufferRange(size_t buffer_view_length,
283+
size_t expected_bound_size) {
284+
BufferBindingsGLES bindings;
285+
auto mock_gles_impl = std::make_unique<::testing::NiceMock<MockGLESImpl>>();
286+
287+
const GLuint kProgram = 1;
288+
289+
ON_CALL(*mock_gles_impl,
290+
GetProgramiv(/*program=*/kProgram,
291+
/*pname=*/GL_ACTIVE_UNIFORM_BLOCKS, /*params=*/_))
292+
.WillByDefault(::testing::SetArgPointee<2>(1));
293+
ON_CALL(*mock_gles_impl,
294+
GetActiveUniformBlockiv(/*program=*/kProgram,
295+
/*uniformBlockIndex=*/0,
296+
/*pname=*/GL_UNIFORM_BLOCK_NAME_LENGTH,
297+
/*params=*/_))
298+
.WillByDefault(::testing::SetArgPointee<3>(9));
299+
ON_CALL(*mock_gles_impl,
300+
GetActiveUniformBlockName(/*program=*/kProgram,
301+
/*uniformBlockIndex=*/0,
302+
/*bufSize=*/9, /*length=*/_,
303+
/*uniformBlockName=*/_))
304+
.WillByDefault([](GLuint program, GLuint index, GLsizei bufSize,
305+
GLsizei* length, GLchar* name) {
306+
*length = 8;
307+
std::memcpy(name, "FragInfo", 9);
308+
});
309+
ON_CALL(
310+
*mock_gles_impl,
311+
GetUniformBlockIndex(/*program=*/kProgram,
312+
/*uniformBlockName=*/::testing::StrEq("FragInfo")))
313+
.WillByDefault(::testing::Return(0));
314+
ON_CALL(*mock_gles_impl,
315+
GetActiveUniformBlockiv(/*program=*/kProgram,
316+
/*uniformBlockIndex=*/0,
317+
/*pname=*/GL_UNIFORM_BLOCK_DATA_SIZE,
318+
/*params=*/_))
319+
.WillByDefault(::testing::SetArgPointee<3>(kBlockDataSize));
320+
321+
EXPECT_CALL(*mock_gles_impl,
322+
BindBufferRange(/*target=*/GL_UNIFORM_BUFFER, /*index=*/0,
323+
/*buffer=*/_, /*offset=*/0,
324+
/*size=*/expected_bound_size))
325+
.Times(1);
326+
327+
std::shared_ptr<MockGLES> mock_gl = MockGLES::Init(std::move(mock_gles_impl));
328+
ASSERT_TRUE(bindings.ReadUniformsBindings(mock_gl->GetProcTable(), kProgram));
329+
330+
ProcTableGLES::Resolver resolver = kMockResolverGLES;
331+
auto proc_table = std::make_unique<ProcTableGLES>(resolver);
332+
auto worker = std::make_shared<TestWorker>();
333+
auto reactor = std::make_shared<ReactorGLES>(std::move(proc_table));
334+
reactor->AddWorker(worker);
335+
336+
std::vector<BufferResource> bound_buffers;
337+
std::vector<TextureAndSampler> bound_textures;
338+
339+
ShaderMetadata shader_metadata = {.name = "FragInfo"};
340+
auto backing_store = std::make_unique<Allocation>();
341+
ASSERT_TRUE(backing_store->Truncate(Bytes{1024}));
342+
DeviceBufferGLES device_buffer(DeviceBufferDescriptor{.size = 1024}, reactor,
343+
std::move(backing_store));
344+
BufferView buffer_view(&device_buffer, Range(0, buffer_view_length));
345+
bound_buffers.push_back(BufferResource(&shader_metadata, buffer_view));
346+
347+
EXPECT_TRUE(bindings.BindUniformData(mock_gl->GetProcTable(), bound_textures,
348+
bound_buffers, Range{0, 0},
349+
Range{0, 1}));
350+
}
351+
352+
TEST(BufferBindingsGLESTest,
353+
BindUniformBufferUsesMaxOfBufferViewAndBlockDataSize) {
354+
TestBindUniformBufferRange(/*buffer_view_length=*/4,
355+
/*expected_bound_size=*/kBlockDataSize);
356+
}
357+
358+
TEST(BufferBindingsGLESTest,
359+
BindUniformBufferUsesBufferViewLengthWhenGreaterThanBlockDataSize) {
360+
TestBindUniformBufferRange(/*buffer_view_length=*/32,
361+
/*expected_bound_size=*/32);
362+
}
363+
268364
} // namespace testing
269365
} // namespace impeller

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,52 @@ void mockBindTexture(GLenum target, GLuint texture) {
303303
static_assert(CheckSameSignature<decltype(mockBindTexture), //
304304
decltype(glBindTexture)>::value);
305305

306+
void mockBindBufferRange(GLenum target,
307+
GLuint index,
308+
GLuint buffer,
309+
GLintptr offset,
310+
GLsizeiptr size) {
311+
CallMockMethod(&IMockGLESImpl::BindBufferRange, target, index, buffer, offset,
312+
size);
313+
}
314+
static_assert(CheckSameSignature<decltype(mockBindBufferRange), //
315+
decltype(glBindBufferRange)>::value);
316+
317+
void mockGetProgramiv(GLuint program, GLenum pname, GLint* params) {
318+
CallMockMethod(&IMockGLESImpl::GetProgramiv, program, pname, params);
319+
}
320+
static_assert(CheckSameSignature<decltype(mockGetProgramiv), //
321+
decltype(glGetProgramiv)>::value);
322+
323+
void mockGetActiveUniformBlockiv(GLuint program,
324+
GLuint uniformBlockIndex,
325+
GLenum pname,
326+
GLint* params) {
327+
CallMockMethod(&IMockGLESImpl::GetActiveUniformBlockiv, program,
328+
uniformBlockIndex, pname, params);
329+
}
330+
static_assert(CheckSameSignature<decltype(mockGetActiveUniformBlockiv), //
331+
decltype(glGetActiveUniformBlockiv)>::value);
332+
333+
void mockGetActiveUniformBlockName(GLuint program,
334+
GLuint uniformBlockIndex,
335+
GLsizei bufSize,
336+
GLsizei* length,
337+
GLchar* uniformBlockName) {
338+
CallMockMethod(&IMockGLESImpl::GetActiveUniformBlockName, program,
339+
uniformBlockIndex, bufSize, length, uniformBlockName);
340+
}
341+
static_assert(CheckSameSignature<decltype(mockGetActiveUniformBlockName), //
342+
decltype(glGetActiveUniformBlockName)>::value);
343+
344+
GLuint mockGetUniformBlockIndex(GLuint program,
345+
const GLchar* uniformBlockName) {
346+
return CallMockMethod(&IMockGLESImpl::GetUniformBlockIndex, program,
347+
uniformBlockName);
348+
}
349+
static_assert(CheckSameSignature<decltype(mockGetUniformBlockIndex), //
350+
decltype(glGetUniformBlockIndex)>::value);
351+
306352
GLboolean mockIsTexture(GLuint texture) {
307353
return CallMockMethod(&IMockGLESImpl::IsTexture, texture);
308354
}
@@ -537,6 +583,16 @@ const ProcTableGLES::Resolver kMockResolverGLES = [](const char* name) {
537583
return reinterpret_cast<void*>(mockDrawElementsInstanced);
538584
} else if (strcmp(name, "glVertexAttribDivisor") == 0) {
539585
return reinterpret_cast<void*>(mockVertexAttribDivisor);
586+
} else if (strcmp(name, "glBindBufferRange") == 0) {
587+
return reinterpret_cast<void*>(mockBindBufferRange);
588+
} else if (strcmp(name, "glGetProgramiv") == 0) {
589+
return reinterpret_cast<void*>(mockGetProgramiv);
590+
} else if (strcmp(name, "glGetActiveUniformBlockiv") == 0) {
591+
return reinterpret_cast<void*>(mockGetActiveUniformBlockiv);
592+
} else if (strcmp(name, "glGetActiveUniformBlockName") == 0) {
593+
return reinterpret_cast<void*>(mockGetActiveUniformBlockName);
594+
} else if (strcmp(name, "glGetUniformBlockIndex") == 0) {
595+
return reinterpret_cast<void*>(mockGetUniformBlockIndex);
540596
} else {
541597
return reinterpret_cast<void*>(&doNothing);
542598
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ class IMockGLESImpl {
125125
const void* indices,
126126
GLsizei instancecount) {}
127127
virtual void VertexAttribDivisor(GLuint index, GLuint divisor) {}
128+
virtual void BindBufferRange(GLenum target,
129+
GLuint index,
130+
GLuint buffer,
131+
GLintptr offset,
132+
GLsizeiptr size) {}
133+
virtual void GetProgramiv(GLuint program, GLenum pname, GLint* params) {}
134+
virtual void GetActiveUniformBlockiv(GLuint program,
135+
GLuint uniformBlockIndex,
136+
GLenum pname,
137+
GLint* params) {}
138+
virtual void GetActiveUniformBlockName(GLuint program,
139+
GLuint uniformBlockIndex,
140+
GLsizei bufSize,
141+
GLsizei* length,
142+
GLchar* uniformBlockName) {}
143+
virtual GLuint GetUniformBlockIndex(GLuint program,
144+
const GLchar* uniformBlockName) {
145+
return 0;
146+
}
128147
};
129148

130149
class MockGLESImpl : public IMockGLESImpl {
@@ -299,6 +318,35 @@ class MockGLESImpl : public IMockGLESImpl {
299318
VertexAttribDivisor,
300319
(GLuint index, GLuint divisor),
301320
(override));
321+
MOCK_METHOD(void,
322+
BindBufferRange,
323+
(GLenum target,
324+
GLuint index,
325+
GLuint buffer,
326+
GLintptr offset,
327+
GLsizeiptr size),
328+
(override));
329+
MOCK_METHOD(void,
330+
GetProgramiv,
331+
(GLuint program, GLenum pname, GLint* params),
332+
(override));
333+
MOCK_METHOD(
334+
void,
335+
GetActiveUniformBlockiv,
336+
(GLuint program, GLuint uniformBlockIndex, GLenum pname, GLint* params),
337+
(override));
338+
MOCK_METHOD(void,
339+
GetActiveUniformBlockName,
340+
(GLuint program,
341+
GLuint uniformBlockIndex,
342+
GLsizei bufSize,
343+
GLsizei* length,
344+
GLchar* uniformBlockName),
345+
(override));
346+
MOCK_METHOD(GLuint,
347+
GetUniformBlockIndex,
348+
(GLuint program, const GLchar* uniformBlockName),
349+
(override));
302350
};
303351

304352
/// @brief Provides a mocked version of the |ProcTableGLES| class.

0 commit comments

Comments
 (0)