Skip to content

[Impeller] support static shader variants using specialization constants #119357

@jonahwilliams

Description

@jonahwilliams

Originally in go/impeller-shader-variants, related to #116103

Problems

Specialization constants

spriv-cross seems to mostly do reasonable things with specialization constants declared in a shader.

For example, with the morphology filter:

uniform FragInfo {
  float texture_sampler_y_coord_scale;
  vec2 texture_size;
  vec2 direction;
  float radius;
}
frag_info;

layout(constant_id = 0) const float morph_type = 0;

For GLES, the following code is generated:

#ifndef SPIRV_CROSS_CONSTANT_ID_0
#define SPIRV_CROSS_CONSTANT_ID_0 0.0
#endif
const float morph_type = SPIRV_CROSS_CONSTANT_ID_0;

For Metal, the following code is generated:

constant float morph_type_tmp [[function_constant(0)]];
constant float morph_type = is_function_constant_defined(morph_type_tmp) ? morph_type_tmp : 0.0;

We'll probably need to do some plumbing to set it but it might do what we need.

We'll still need to declare all of the possible values ahead of time. This will allow us to generate specialized versions for GLES and have correct bindings for metal, which seems to need all possible values during pipeline creation.

From Metal documentation:

This sample uses three different MTLRenderPipelineState objects, each representing a different LOD. Specializing functions and building pipelines is expensive, so the sample performs these tasks asynchronously before starting the render loop. When the AAPLRenderer object is initialized, each LOD pipeline is created asynchronously by using dispatch groups, completion handlers, and notification blocks.

AIs

  • Add PSO benchmarks and compare specialization constants to N shaders
  • Add support for function constant registration in metal backend
  • Ensure that opengl vs vulkan on android can avoid loading opengl shaders if we end up generating a large amount of them.
  • Verify Android on Vulkan sees similar performance improvements from specialization constants compared to shader variants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: performanceRelates to speed or footprint issues (see "perf:" labels)e: impellerImpeller rendering backend issues and features requestsengineflutter/engine related. See also e: labels.team-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions