Fix issue with getUniformFloat crashing with hot reload (#180138)

Fixes https://github.com/flutter/flutter/issues/180101.

Our hot reloading behavior for shader uniforms only handled the case
where uniforms were reordered. In the case that a uniform was renamed or
deleted, and a user had acquired a handle to that uniform via
`getUniformFloat`, we would try (and fail) to locate that uniform in the
shader, and the program would crash.

This PR fixes that issue and adds a test to verify. Also manually
verified on mac and android

## Pre-launch Checklist

- [ x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ 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].

**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
[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
This commit is contained in:
walley892 2025-12-30 13:38:26 -08:00 committed by GitHub
parent d37058bf2b
commit 560cb50dae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 0 deletions

View File

@ -26,6 +26,7 @@ if (enable_unittests) {
"uniforms_reordered.frag",
"uniforms_sorted.frag",
"uniforms.frag",
"uniforms_renamed.frag",
"vec3_uniform.frag",
]

View File

@ -0,0 +1,20 @@
#version 320 es
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Note: Don't update this file without updating `uniforms_reordered.frag`.
// Note: Don't update this file without updating `uniforms_inserted.frag`.
precision highp float;
layout(location = 0) out vec4 oColor;
layout(location = 0) uniform float iFloatUniformRenamed;
layout(location = 1) uniform vec2 iVec2Uniform;
layout(location = 2) uniform mat2 iMat2Uniform;
void main() {
oColor = vec4(iFloatUniformRenamed, iVec2Uniform, iMat2Uniform[1][1]);
}

View File

@ -5391,6 +5391,10 @@ base class FragmentProgram extends NativeFieldWrapperClass1 {
return true;
}
if (!program._hasUniform(slot.name)) {
return true;
}
slot._shaderIndex = program._getUniformFloatIndex(slot.name, slot.index);
return false;
});
@ -5409,6 +5413,10 @@ base class FragmentProgram extends NativeFieldWrapperClass1 {
});
}
bool _hasUniform(String name) {
return _uniformInfo.any((dynamic entry) => (entry! as Map<String, Object>)['name'] == name);
}
int _getImageSamplerIndex(String name) {
var index = 0;
var found = false;

View File

@ -130,6 +130,43 @@ void main() {
}
});
test('rename uniforms', () async {
if (impellerEnabled) {
// Needs https://github.com/flutter/flutter/issues/129659
return;
}
final testAssetName = 'test_rename_uniforms_${DateTime.now().millisecondsSinceEpoch}.frag.iplr';
final String buildDir = Platform.environment['FLUTTER_BUILD_DIRECTORY']!;
final String assetsDir = path.join(buildDir, 'gen/flutter/lib/ui/assets');
final String shaderSrcA = path.join(assetsDir, 'uniforms.frag.iplr');
final String shaderSrcB = path.join(assetsDir, 'uniforms_renamed.frag.iplr');
final String shaderSrcC = path.join(assetsDir, testAssetName);
final fileC = File(shaderSrcC);
final Uint8List sourceA = File(shaderSrcA).readAsBytesSync();
final Uint8List sourceB = File(shaderSrcB).readAsBytesSync();
fileC.writeAsBytesSync(sourceA, flush: true);
try {
final FragmentProgram program = await FragmentProgram.fromAsset(testAssetName);
final FragmentShader shader = program.fragmentShader();
final UniformFloatSlot slotA = shader.getUniformFloat('iFloatUniform');
slotA.set(1.0);
fileC.writeAsBytesSync(sourceB, flush: true);
await _performReload(testAssetName);
final UniformFloatSlot slotB = shader.getUniformFloat('iFloatUniformRenamed');
// Make sure this doesn't break.
slotB.set(0.0);
} finally {
if (fileC.existsSync()) {
fileC.deleteSync();
}
}
});
test('reorder samplers', () async {
if (impellerEnabled) {
// Needs https://github.com/flutter/flutter/issues/129659