Skip to content

Commit 30d67e6

Browse files
[material_ui] Use named uniforms instead of setFloat in InkSparkle (#13445)
Moves `InkSparkle` from the index-based `FragmentShader.setFloat` to the named uniform API (`getUniformFloat`, `getUniformVec2`, `getUniformVec4`), as part of deprecating `setFloat` (flutter/flutter#195357). `InkSparkle` looks up its 13 uniforms once, when it creates its shader, and sets them through those slots on each paint. Every uniform gets the same value as before, so there is no visual change. The named API is available in Flutter 3.47.0, the package's minimum, so the SDK constraint doesn't change. flutter/flutter#195358 covers both `InkSparkle` and `StretchEffect`. Since Material is frozen in flutter/flutter (flutter/flutter#188444), the work is split: `StretchEffect` is in flutter/flutter#195372, and `InkSparkle` is here. Part of flutter/flutter#195358 Version and CHANGELOG: adds a `pending_changelogs` entry with `version: patch`. Tests: this is a refactor with no semantic change, so I think it qualifies for a test exemption. It's covered by the existing golden tests in `test/ink_sparkle_test.dart`, and a misspelled uniform name would throw when those tests paint. ## Pre-Review Checklist
1 parent cc93a31 commit 30d67e6

2 files changed

Lines changed: 66 additions & 60 deletions

File tree

‎packages/material_ui/lib/src/ink_sparkle.dart‎

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class InkSparkle extends InteractiveInkFeature {
209209
final TextDirection _textDirection;
210210

211211
late final ui.FragmentShader _fragmentShader;
212+
late final _InkSparkleUniforms _uniforms;
212213
bool _fragmentShaderInitialized = false;
213214

214215
/// Used to specify this type of ink splash for an [InkWell], [InkResponse],
@@ -250,6 +251,7 @@ class InkSparkle extends InteractiveInkFeature {
250251

251252
if (!_fragmentShaderInitialized) {
252253
_fragmentShader = _InkSparkleFactory._program!.fragmentShader();
254+
_uniforms = _InkSparkleUniforms(_fragmentShader);
253255
_fragmentShaderInitialized = true;
254256
}
255257

@@ -292,66 +294,34 @@ class InkSparkle extends InteractiveInkFeature {
292294
final double rotation2 = turbulencePhase * _rotateLeft + 2.0 * math.pi;
293295
final double rotation3 = turbulencePhase * _rotateRight + 2.75 * math.pi;
294296

295-
_fragmentShader
296-
// uColor
297-
..setFloat(0, _color.red / 255.0)
298-
..setFloat(1, _color.green / 255.0)
299-
..setFloat(2, _color.blue / 255.0)
300-
..setFloat(3, _color.alpha / 255.0)
301-
// Composite 1 (u_alpha, u_sparkle_alpha, u_blur, u_radius_scale)
302-
..setFloat(4, _alpha.value)
303-
..setFloat(5, _sparkleAlpha.value)
304-
..setFloat(6, 1.0)
305-
..setFloat(7, _radiusScale.value)
306-
// uCenter
307-
..setFloat(8, _center.value.x)
308-
..setFloat(9, _center.value.y)
309-
// uMaxRadius
310-
..setFloat(10, _targetRadius)
311-
// uResolutionScale
312-
..setFloat(11, 1.0 / _width)
313-
..setFloat(12, 1.0 / _height)
314-
// uNoiseScale
315-
..setFloat(13, _noiseDensity / _width)
316-
..setFloat(14, _noiseDensity / _height)
317-
// uNoisePhase
318-
..setFloat(15, noisePhase / 1000.0)
319-
// uCircle1
320-
..setFloat(
321-
16,
322-
turbulenceScale * 0.5 + (turbulencePhase * 0.01 * math.cos(turbulenceScale * 0.55)),
323-
)
324-
..setFloat(
325-
17,
326-
turbulenceScale * 0.5 + (turbulencePhase * 0.01 * math.sin(turbulenceScale * 0.55)),
327-
)
328-
// uCircle2
329-
..setFloat(
330-
18,
331-
turbulenceScale * 0.2 + (turbulencePhase * -0.0066 * math.cos(turbulenceScale * 0.45)),
332-
)
333-
..setFloat(
334-
19,
335-
turbulenceScale * 0.2 + (turbulencePhase * -0.0066 * math.sin(turbulenceScale * 0.45)),
336-
)
337-
// uCircle3
338-
..setFloat(
339-
20,
340-
turbulenceScale + (turbulencePhase * -0.0066 * math.cos(turbulenceScale * 0.35)),
341-
)
342-
..setFloat(
343-
21,
344-
turbulenceScale + (turbulencePhase * -0.0066 * math.sin(turbulenceScale * 0.35)),
345-
)
346-
// uRotation1
347-
..setFloat(22, math.cos(rotation1))
348-
..setFloat(23, math.sin(rotation1))
349-
// uRotation2
350-
..setFloat(24, math.cos(rotation2))
351-
..setFloat(25, math.sin(rotation2))
352-
// uRotation3
353-
..setFloat(26, math.cos(rotation3))
354-
..setFloat(27, math.sin(rotation3));
297+
_uniforms.color.set(
298+
_color.red / 255.0,
299+
_color.green / 255.0,
300+
_color.blue / 255.0,
301+
_color.alpha / 255.0,
302+
);
303+
// u_composite_1 packs u_alpha, u_sparkle_alpha, u_blur and u_radius_scale.
304+
_uniforms.composite1.set(_alpha.value, _sparkleAlpha.value, 1.0, _radiusScale.value);
305+
_uniforms.center.set(_center.value.x, _center.value.y);
306+
_uniforms.maxRadius.set(_targetRadius);
307+
_uniforms.resolutionScale.set(1.0 / _width, 1.0 / _height);
308+
_uniforms.noiseScale.set(_noiseDensity / _width, _noiseDensity / _height);
309+
_uniforms.noisePhase.set(noisePhase / 1000.0);
310+
_uniforms.circle1.set(
311+
turbulenceScale * 0.5 + (turbulencePhase * 0.01 * math.cos(turbulenceScale * 0.55)),
312+
turbulenceScale * 0.5 + (turbulencePhase * 0.01 * math.sin(turbulenceScale * 0.55)),
313+
);
314+
_uniforms.circle2.set(
315+
turbulenceScale * 0.2 + (turbulencePhase * -0.0066 * math.cos(turbulenceScale * 0.45)),
316+
turbulenceScale * 0.2 + (turbulencePhase * -0.0066 * math.sin(turbulenceScale * 0.45)),
317+
);
318+
_uniforms.circle3.set(
319+
turbulenceScale + (turbulencePhase * -0.0066 * math.cos(turbulenceScale * 0.35)),
320+
turbulenceScale + (turbulencePhase * -0.0066 * math.sin(turbulenceScale * 0.35)),
321+
);
322+
_uniforms.rotation1.set(math.cos(rotation1), math.sin(rotation1));
323+
_uniforms.rotation2.set(math.cos(rotation2), math.sin(rotation2));
324+
_uniforms.rotation3.set(math.cos(rotation3), math.sin(rotation3));
355325
}
356326

357327
/// Transforms the canvas for an ink feature to be painted on the [canvas].
@@ -414,6 +384,39 @@ class InkSparkle extends InteractiveInkFeature {
414384
}
415385
}
416386

387+
/// The uniforms declared in `lib/shaders/ink_sparkle.frag`, looked up by name once
388+
/// so that [InkSparkle] only has to set their values on each frame.
389+
class _InkSparkleUniforms {
390+
_InkSparkleUniforms(ui.FragmentShader shader)
391+
: color = shader.getUniformVec4('u_color'),
392+
composite1 = shader.getUniformVec4('u_composite_1'),
393+
center = shader.getUniformVec2('u_center'),
394+
maxRadius = shader.getUniformFloat('u_max_radius'),
395+
resolutionScale = shader.getUniformVec2('u_resolution_scale'),
396+
noiseScale = shader.getUniformVec2('u_noise_scale'),
397+
noisePhase = shader.getUniformFloat('u_noise_phase'),
398+
circle1 = shader.getUniformVec2('u_circle1'),
399+
circle2 = shader.getUniformVec2('u_circle2'),
400+
circle3 = shader.getUniformVec2('u_circle3'),
401+
rotation1 = shader.getUniformVec2('u_rotation1'),
402+
rotation2 = shader.getUniformVec2('u_rotation2'),
403+
rotation3 = shader.getUniformVec2('u_rotation3');
404+
405+
final ui.UniformVec4Slot color;
406+
final ui.UniformVec4Slot composite1;
407+
final ui.UniformVec2Slot center;
408+
final ui.UniformFloatSlot maxRadius;
409+
final ui.UniformVec2Slot resolutionScale;
410+
final ui.UniformVec2Slot noiseScale;
411+
final ui.UniformFloatSlot noisePhase;
412+
final ui.UniformVec2Slot circle1;
413+
final ui.UniformVec2Slot circle2;
414+
final ui.UniformVec2Slot circle3;
415+
final ui.UniformVec2Slot rotation1;
416+
final ui.UniformVec2Slot rotation2;
417+
final ui.UniformVec2Slot rotation3;
418+
}
419+
417420
class _InkSparkleFactory extends InteractiveInkFeatureFactory {
418421
const _InkSparkleFactory() : turbulenceSeed = null;
419422

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Updates `InkSparkle` to set its shader uniforms by name instead of with `FragmentShader.setFloat`.
3+
version: patch

0 commit comments

Comments
 (0)