Skip to content

Allow uniform bindings by name for FragmentShader #176417

@gaaclarke

Description

@gaaclarke

Description of problem

When using the FragmentShader API today one has to carefully count uniform offsets by hand in order to set uniforms. There should be a way to instead to set uniforms by name to make changing uniforms easier and for easier debugging.

Example

shader

uniform float uScale;
uniform sampler2D uTexture;
uniform vec2 uMagnitude;
uniform vec4 uColor;

today

Generate dart code for shaders that has the uniform indices as constants.

shader.setFloat(0, 23);  // uScale
shader.setFloat(1, 114); // uMagnitude x
shader.setFloat(2, 83);  // uMagnitude y
shader.setFloat(3, color.red / 255 * color.opacity);   // uColor r
shader.setFloat(4, color.green / 255 * color.opacity); // uColor g
shader.setFloat(5, color.blue / 255 * color.opacity);  // uColor b
shader.setFloat(6, color.opacity);                     // uColor a
shader.setImageSampler(0, image);

proposal

shader.setFloat(SHADER_USCALE, 23);
shader.setFloat(SHADER_UMAGNITUDE_X, 114);
shader.setFloat(SHADER_UMAGNITUDE_Y, 83);
shader.setFloat(SHADER_UCOLOR_RED, color.red / 255 * color.opacity);
shader.setFloat(SHADER_UCOLOR_GREEN, color.green / 255 * color.opacity);
shader.setFloat(SHADER_UCOLOR_BLUE, color.blue / 255 * color.opacity);
shader.setFloat(SHADER_UCOLOR_ALPHA, color.opacity);
shader.setImageSampler(SHADER_UTEXTURE, image);

Alternatives considered

runtime access

shader.setFloat('uScale', 23);
shader.setFloat('uMagnitude', 114, 0);
shader.setFloat('uMagnitude', 83, 1);

Having compile time checks for the validity of uniform names is a better user experience and better performance.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listc: proposalA detailed proposal for a change to Fluttere: impellerImpeller rendering backend issues and features requeststeam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions