[Impeller] add support for specialization constants. (flutter/engine#47432)

Adds support for Specialization constants to Impeller for our usage in the engine. A motivating example has been added in the impeller markdown docs.

Fixes https://github.com/flutter/flutter/issues/136210
Fixes https://github.com/flutter/flutter/issues/119357
This commit is contained in:
Jonah Williams 2023-11-02 10:32:08 -07:00 committed by GitHub
parent d6b5a87f11
commit c8d04e64dc
64 changed files with 814 additions and 3561 deletions

View File

@ -3134,42 +3134,13 @@ ORIGIN: ../../../flutter/impeller/entity/inline_pass_context.cc + ../../../flutt
ORIGIN: ../../../flutter/impeller/entity/inline_pass_context.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/render_target_cache.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/render_target_cache.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_color.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colorburn.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colordodge.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_darken.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_difference.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_exclusion.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_lighten.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_multiply.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_overlay.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_screen.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_softlight.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/blend.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/blend.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_color.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_colorburn.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_colordodge.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_darken.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_difference.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_exclusion.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_hardlight.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_hue.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_lighten.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_luminosity.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_multiply.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_overlay.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_saturation.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_screen.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_softlight.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/blend_select.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/framebuffer_blend.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/framebuffer_blend.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/porter_duff_blend.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/porter_duff_blend.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/border_mask_blur.frag + ../../../flutter/LICENSE
@ -5917,42 +5888,13 @@ FILE: ../../../flutter/impeller/entity/inline_pass_context.cc
FILE: ../../../flutter/impeller/entity/inline_pass_context.h
FILE: ../../../flutter/impeller/entity/render_target_cache.cc
FILE: ../../../flutter/impeller/entity/render_target_cache.h
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_color.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colorburn.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colordodge.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_darken.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_difference.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_exclusion.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_lighten.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_multiply.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_overlay.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_screen.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_softlight.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/blend.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend.glsl
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_color.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_colorburn.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_colordodge.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_darken.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_difference.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_exclusion.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_hardlight.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_hue.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_lighten.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_luminosity.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_multiply.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_overlay.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_saturation.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_screen.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/ios/framebuffer_blend_softlight.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/blend_select.glsl
FILE: ../../../flutter/impeller/entity/shaders/blending/framebuffer_blend.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/framebuffer_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/porter_duff_blend.frag
FILE: ../../../flutter/impeller/entity/shaders/blending/porter_duff_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/border_mask_blur.frag

View File

@ -8,12 +8,8 @@
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include "flutter/fml/hash_combine.h"
#include "flutter/fml/macros.h"
namespace impeller {
struct UniqueID {

View File

@ -0,0 +1,93 @@
# Specialization Constants
A specialization constant is a named variable that is known to be constant at runtime but not when the shader is authored. These variables are bound to specific values when the shader is compiled on application start up and allow the backend to perform optimizations such as branch elimination and constant folding.
Specialization constants have two possible benefits when used in a shader:
* Improving performance, by removing branching and conditional code.
* Code organization/size, by removing the number of shader source files required.
These goals are related: The number of shaders can be reduce by adding runtime branching to create more generic shaders. Alternatively, branching can be reduced by adding more specialized shader variants. Specialization constants provide a happy medium where the source files can be combined with branching but done so in a way that has no runtime cost.
## Example Usage
Consider the case of the "decal" texture sampling mode. This is implement via clamp-to-border with
a border color set to transparent black. While this functionality is well supported on the Metal and
Vulkan backends, the GLES backend needs to support devices that do not have this extension. As a
result, the following code was used to conditionally decal:
```glsl
// Decal sample if necessary.
vec4 Sample(sampler2D sampler, vec2 coord) {
#ifdef GLES
return IPSampleDecal(sampler, coord)
#else
return texture(sampler, coord);
#endif
}
```
This works great as long as we know that the GLES backend can never do the decal sample mode. This is also "free" as the ifdef branch is evaluated in the compiler. But eventually, we added a runtime check for decal mode as we need to support this on GLES. So the code turned into (approximately) the following:
```glsl
#ifdef GLES
uniform float supports_decal;
#endif
// Decal sample if necessary.
vec4 Sample(sampler2D sampler, vec2 coord) {
#ifdef GLES
if (supports_decal) {
return texture(sampler, coord);
}
return IPSampleDecal(sampler, coord)
#else
return texture(sampler, coord);
#endif
}
```
Now we've got decal support, but we've also got new problems:
* The code is actually quite messy. We have to track different uniform values depending on the backend.
* The GLES backend is still paying some cost for branching, even though we "know" that decal is or isn't supported when the shader is compiled.
### Specialization constants to the rescue
Instead of using a runtime check, we can create a specialization constant that is set when compiling the
shader. This constant will be `1` if decal is supported and `0` otherwise.
```glsl
layout(constant_id = 0) const int supports_decal = 1;
vec4 Sample(sampler2D sampler, vec2 coord) {
if (supports_decal) {
return texture(sampler, coord);
}
return IPSampleDecal(sampler, coord)
}
```
Immediately we realize a number of benefits:
* Code is the same across all backends
* Runtime branching cost is removed as the branch is compiled out.
## Implementation
Only 32bit ints are supported as const values and can be used to represent:
* true/false via 0/1.
* function selection, such as advanced blends. The specialization value maps to a specific blend function. For example, 0 maps to screen and 1 to overlay via a giant if/else macro.
*AVOID* adding specialization constants for color values or anything more complex.
Specialization constants are provided to the CreateDefault argument in content_context.cc and aren't a
part of variants. This is intentional: specialization constants shouldn't be used to create (potentially unlimited) runtime variants of a shader.
Backend specific information:
* In the Metal backend, the specialization constants are mapped to a MTLFunctionConstantValues. See also: https://developer.apple.com/documentation/metal/using_function_specialization_to_build_pipeline_variants?language=objc
* In the Vulkan backend, the specialization constants are mapped to VkSpecializationINfo. See also: https://blogs.igalia.com/itoral/2018/03/20/improving-shader-performance-with-vulkans-specialization-constants/
* In the GLES backend, the SPIRV Cross compiler will generate defines named `#ifdef SPIRV_CROSS_CONSTANT_i`, where i is the index of constant. The Impeller runtime will insert `#define SPIRV_CROSS_CONSTANT_i` in the header of the shader.

View File

@ -15,21 +15,7 @@ impeller_shaders("entity_shaders") {
shaders = [
"shaders/blending/advanced_blend.vert",
"shaders/blending/advanced_blend_color.frag",
"shaders/blending/advanced_blend_colorburn.frag",
"shaders/blending/advanced_blend_colordodge.frag",
"shaders/blending/advanced_blend_darken.frag",
"shaders/blending/advanced_blend_difference.frag",
"shaders/blending/advanced_blend_exclusion.frag",
"shaders/blending/advanced_blend_hardlight.frag",
"shaders/blending/advanced_blend_hue.frag",
"shaders/blending/advanced_blend_lighten.frag",
"shaders/blending/advanced_blend_luminosity.frag",
"shaders/blending/advanced_blend_multiply.frag",
"shaders/blending/advanced_blend_overlay.frag",
"shaders/blending/advanced_blend_saturation.frag",
"shaders/blending/advanced_blend_screen.frag",
"shaders/blending/advanced_blend_softlight.frag",
"shaders/blending/advanced_blend.frag",
"shaders/blending/blend.frag",
"shaders/blending/blend.vert",
"shaders/border_mask_blur.frag",
@ -115,22 +101,8 @@ impeller_shaders("framebuffer_blend_entity_shaders") {
}
shaders = [
"shaders/blending/ios/framebuffer_blend.vert",
"shaders/blending/ios/framebuffer_blend_color.frag",
"shaders/blending/ios/framebuffer_blend_colorburn.frag",
"shaders/blending/ios/framebuffer_blend_colordodge.frag",
"shaders/blending/ios/framebuffer_blend_darken.frag",
"shaders/blending/ios/framebuffer_blend_difference.frag",
"shaders/blending/ios/framebuffer_blend_exclusion.frag",
"shaders/blending/ios/framebuffer_blend_hardlight.frag",
"shaders/blending/ios/framebuffer_blend_hue.frag",
"shaders/blending/ios/framebuffer_blend_lighten.frag",
"shaders/blending/ios/framebuffer_blend_luminosity.frag",
"shaders/blending/ios/framebuffer_blend_multiply.frag",
"shaders/blending/ios/framebuffer_blend_overlay.frag",
"shaders/blending/ios/framebuffer_blend_saturation.frag",
"shaders/blending/ios/framebuffer_blend_screen.frag",
"shaders/blending/ios/framebuffer_blend_softlight.frag",
"shaders/blending/framebuffer_blend.vert",
"shaders/blending/framebuffer_blend.frag",
]
}

View File

@ -262,8 +262,6 @@ bool AtlasContents::Render(const ContentContext& renderer,
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
frag_info.supports_decal_sampler_address_mode =
renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode();
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(cmd, texture_, dst_sampler);

View File

@ -5,15 +5,15 @@
#include "impeller/entity/contents/content_context.h"
#include <memory>
#include <sstream>
#include "impeller/base/strings.h"
#include "impeller/core/formats.h"
#include "impeller/entity/contents/framebuffer_blend_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/entity/render_target_cache.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/pipeline_descriptor.h"
#include "impeller/renderer/pipeline_library.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"
#include "impeller/tessellator/tessellator.h"
#include "impeller/typographer/typographer_context.h"
@ -197,6 +197,8 @@ ContentContext::ContentContext(
.primitive_type = PrimitiveType::kTriangleStrip,
.color_attachment_pixel_format =
context_->GetCapabilities()->GetDefaultColorFormat()};
const auto supports_decal =
context_->GetCapabilities()->SupportsDecalSamplerAddressMode();
#ifdef IMPELLER_DEBUG
checkerboard_pipelines_.CreateDefault(*context_, options);
@ -217,53 +219,98 @@ ContentContext::ContentContext(
}
if (context_->GetCapabilities()->SupportsFramebufferFetch()) {
framebuffer_blend_color_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_colorburn_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_color_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kColor), supports_decal});
framebuffer_blend_colorburn_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kColorBurn), supports_decal});
framebuffer_blend_colordodge_pipelines_.CreateDefault(
*context_, options_trianglestrip);
framebuffer_blend_darken_pipelines_.CreateDefault(*context_,
options_trianglestrip);
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kColorDodge), supports_decal});
framebuffer_blend_darken_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kDarken), supports_decal});
framebuffer_blend_difference_pipelines_.CreateDefault(
*context_, options_trianglestrip);
framebuffer_blend_exclusion_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_hardlight_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_hue_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_lighten_pipelines_.CreateDefault(*context_,
options_trianglestrip);
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kDifference), supports_decal});
framebuffer_blend_exclusion_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kExclusion), supports_decal});
framebuffer_blend_hardlight_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kHardLight), supports_decal});
framebuffer_blend_hue_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kHue), supports_decal});
framebuffer_blend_lighten_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kLighten), supports_decal});
framebuffer_blend_luminosity_pipelines_.CreateDefault(
*context_, options_trianglestrip);
framebuffer_blend_multiply_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_overlay_pipelines_.CreateDefault(*context_,
options_trianglestrip);
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kLuminosity), supports_decal});
framebuffer_blend_multiply_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kMultiply), supports_decal});
framebuffer_blend_overlay_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kOverlay), supports_decal});
framebuffer_blend_saturation_pipelines_.CreateDefault(
*context_, options_trianglestrip);
framebuffer_blend_screen_pipelines_.CreateDefault(*context_,
options_trianglestrip);
framebuffer_blend_softlight_pipelines_.CreateDefault(*context_,
options_trianglestrip);
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kSaturation), supports_decal});
framebuffer_blend_screen_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kScreen), supports_decal});
framebuffer_blend_softlight_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kSoftLight), supports_decal});
}
blend_color_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_colorburn_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_colordodge_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_darken_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_difference_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_exclusion_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_hardlight_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_hue_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_lighten_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_luminosity_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_multiply_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_overlay_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_saturation_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_screen_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_softlight_pipelines_.CreateDefault(*context_, options_trianglestrip);
blend_color_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kColor), supports_decal});
blend_colorburn_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kColorBurn), supports_decal});
blend_colordodge_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kColorDodge), supports_decal});
blend_darken_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kDarken), supports_decal});
blend_difference_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kDifference), supports_decal});
blend_exclusion_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kExclusion), supports_decal});
blend_hardlight_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kHardLight), supports_decal});
blend_hue_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kHue), supports_decal});
blend_lighten_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kLighten), supports_decal});
blend_luminosity_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kLuminosity), supports_decal});
blend_multiply_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kMultiply), supports_decal});
blend_overlay_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kOverlay), supports_decal});
blend_saturation_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kSaturation), supports_decal});
blend_screen_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kScreen), supports_decal});
blend_softlight_pipelines_.CreateDefault(
*context_, options_trianglestrip,
{static_cast<int32_t>(BlendSelectValues::kSoftLight), supports_decal});
rrect_blur_pipelines_.CreateDefault(*context_, options_trianglestrip);
texture_blend_pipelines_.CreateDefault(*context_, options);
@ -286,9 +333,10 @@ ContentContext::ContentContext(
glyph_atlas_color_pipelines_.CreateDefault(*context_, options);
geometry_color_pipelines_.CreateDefault(*context_, options);
yuv_to_rgb_filter_pipelines_.CreateDefault(*context_, options_trianglestrip);
porter_duff_blend_pipelines_.CreateDefault(*context_, options_trianglestrip);
// GLES only shader.
#ifdef IMPELLER_ENABLE_OPENGLES
porter_duff_blend_pipelines_.CreateDefault(*context_, options_trianglestrip,
{supports_decal});
// GLES only shader that is unsupported on macOS.
#if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_MACOSX)
if (GetContext()->GetBackendType() == Context::BackendType::kOpenGLES) {
texture_external_pipelines_.CreateDefault(*context_, options);
}

View File

@ -4,6 +4,7 @@
#pragma once
#include <initializer_list>
#include <memory>
#include <optional>
#include <unordered_map>
@ -75,39 +76,11 @@
#include "impeller/entity/radial_gradient_ssbo_fill.frag.h"
#include "impeller/entity/sweep_gradient_ssbo_fill.frag.h"
#include "impeller/entity/advanced_blend.frag.h"
#include "impeller/entity/advanced_blend.vert.h"
#include "impeller/entity/advanced_blend_color.frag.h"
#include "impeller/entity/advanced_blend_colorburn.frag.h"
#include "impeller/entity/advanced_blend_colordodge.frag.h"
#include "impeller/entity/advanced_blend_darken.frag.h"
#include "impeller/entity/advanced_blend_difference.frag.h"
#include "impeller/entity/advanced_blend_exclusion.frag.h"
#include "impeller/entity/advanced_blend_hardlight.frag.h"
#include "impeller/entity/advanced_blend_hue.frag.h"
#include "impeller/entity/advanced_blend_lighten.frag.h"
#include "impeller/entity/advanced_blend_luminosity.frag.h"
#include "impeller/entity/advanced_blend_multiply.frag.h"
#include "impeller/entity/advanced_blend_overlay.frag.h"
#include "impeller/entity/advanced_blend_saturation.frag.h"
#include "impeller/entity/advanced_blend_screen.frag.h"
#include "impeller/entity/advanced_blend_softlight.frag.h"
#include "impeller/entity/framebuffer_blend.frag.h"
#include "impeller/entity/framebuffer_blend.vert.h"
#include "impeller/entity/framebuffer_blend_color.frag.h"
#include "impeller/entity/framebuffer_blend_colorburn.frag.h"
#include "impeller/entity/framebuffer_blend_colordodge.frag.h"
#include "impeller/entity/framebuffer_blend_darken.frag.h"
#include "impeller/entity/framebuffer_blend_difference.frag.h"
#include "impeller/entity/framebuffer_blend_exclusion.frag.h"
#include "impeller/entity/framebuffer_blend_hardlight.frag.h"
#include "impeller/entity/framebuffer_blend_hue.frag.h"
#include "impeller/entity/framebuffer_blend_lighten.frag.h"
#include "impeller/entity/framebuffer_blend_luminosity.frag.h"
#include "impeller/entity/framebuffer_blend_multiply.frag.h"
#include "impeller/entity/framebuffer_blend_overlay.frag.h"
#include "impeller/entity/framebuffer_blend_saturation.frag.h"
#include "impeller/entity/framebuffer_blend_screen.frag.h"
#include "impeller/entity/framebuffer_blend_softlight.frag.h"
#ifdef IMPELLER_ENABLE_OPENGLES
#include "impeller/entity/texture_fill_external.frag.h"
@ -192,93 +165,82 @@ using YUVToRGBFilterPipeline =
RenderPipelineT<YuvToRgbFilterVertexShader, YuvToRgbFilterFragmentShader>;
// Advanced blends
using BlendColorPipeline = RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendColorFragmentShader>;
using BlendColorPipeline =
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendColorBurnPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendColorburnFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendColorDodgePipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendColordodgeFragmentShader>;
using BlendDarkenPipeline = RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendDarkenFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendDarkenPipeline =
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendDifferencePipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendDifferenceFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendExclusionPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendExclusionFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendHardLightPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendHardlightFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendHuePipeline =
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendHueFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendLightenPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendLightenFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendLuminosityPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendLuminosityFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendMultiplyPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendMultiplyFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendOverlayPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendOverlayFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendSaturationPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendSaturationFragmentShader>;
using BlendScreenPipeline = RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendScreenFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendScreenPipeline =
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
using BlendSoftLightPipeline =
RenderPipelineT<AdvancedBlendVertexShader,
AdvancedBlendSoftlightFragmentShader>;
RenderPipelineT<AdvancedBlendVertexShader, AdvancedBlendFragmentShader>;
// Framebuffer Advanced Blends
using FramebufferBlendColorPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendColorFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendColorBurnPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendColorburnFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendColorDodgePipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendColordodgeFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendDarkenPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendDarkenFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendDifferencePipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendDifferenceFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendExclusionPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendExclusionFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendHardLightPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendHardlightFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendHuePipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendHueFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendLightenPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendLightenFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendLuminosityPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendLuminosityFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendMultiplyPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendMultiplyFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendOverlayPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendOverlayFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendSaturationPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendSaturationFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendScreenPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendScreenFragmentShader>;
FramebufferBlendFragmentShader>;
using FramebufferBlendSoftLightPipeline =
RenderPipelineT<FramebufferBlendVertexShader,
FramebufferBlendSoftlightFragmentShader>;
FramebufferBlendFragmentShader>;
/// Geometry Pipelines
using PointsComputeShaderPipeline = ComputePipelineBuilder<PointsComputeShader>;
@ -738,8 +700,10 @@ class ContentContext {
}
void CreateDefault(const Context& context,
const ContentContextOptions& options) {
auto desc = PipelineT::Builder::MakeDefaultPipelineDescriptor(context);
const ContentContextOptions& options,
const std::initializer_list<int32_t>& constants = {}) {
auto desc =
PipelineT::Builder::MakeDefaultPipelineDescriptor(context, constants);
if (!desc.has_value()) {
VALIDATION_LOG << "Failed to create default pipeline.";
return;

View File

@ -180,8 +180,6 @@ static std::optional<Entity> AdvancedBlend(
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
blend_info.supports_decal_sampler_address_mode =
renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode();
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, dst_sampler);
@ -354,8 +352,6 @@ std::optional<Entity> BlendFilterContents::CreateForegroundAdvancedBlend(
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
blend_info.supports_decal_sampler_address_mode =
renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode();
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, dst_sampler);
@ -478,8 +474,6 @@ std::optional<Entity> BlendFilterContents::CreateForegroundPorterDuffBlend(
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
frag_info.supports_decal_sampler_address_mode =
renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode();
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_sampler_descriptor);
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, dst_sampler);

View File

@ -4,17 +4,31 @@
#pragma once
#include <functional>
#include <memory>
#include <vector>
#include "flutter/fml/macros.h"
#include "flutter/impeller/core/texture.h"
#include "impeller/entity/contents/color_source_contents.h"
#include "impeller/entity/entity.h"
namespace impeller {
enum class BlendSelectValues {
kScreen = 0,
kOverlay,
kDarken,
kLighten,
kColorDodge,
kColorBurn,
kHardLight,
kSoftLight,
kDifference,
kExclusion,
kMultiply,
kHue,
kSaturation,
kColor,
kLuminosity,
};
class FramebufferBlendContents final : public ColorSourceContents {
public:
FramebufferBlendContents();

View File

@ -2536,6 +2536,26 @@ TEST_P(EntityTest, AdvancedBlendCoverageHintIsNotResetByEntityPass) {
}
}
TEST_P(EntityTest, SpecializationConstantsAreAppliedToVariants) {
auto content_context =
ContentContext(GetContext(), TypographerContextSkia::Make());
auto default_color_burn = content_context.GetBlendColorBurnPipeline(
{.has_stencil_attachment = false});
auto alt_color_burn = content_context.GetBlendColorBurnPipeline(
{.has_stencil_attachment = true});
ASSERT_NE(default_color_burn, alt_color_burn);
ASSERT_EQ(default_color_burn->GetDescriptor().GetSpecializationConstants(),
alt_color_burn->GetDescriptor().GetSpecializationConstants());
auto decal_supported = static_cast<int32_t>(
GetContext()->GetCapabilities()->SupportsDecalSamplerAddressMode());
std::vector<int32_t> expected_constants = {5, decal_supported};
ASSERT_EQ(default_color_burn->GetDescriptor().GetSpecializationConstants(),
expected_constants);
}
} // namespace testing
} // namespace impeller

View File

@ -6,6 +6,10 @@
#include <impeller/color.glsl>
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
#include "blend_select.glsl"
layout(constant_id = 0) const int blend_type = 0;
layout(constant_id = 1) const int supports_decal = 1;
uniform BlendInfo {
float16_t dst_input_alpha;
@ -25,17 +29,14 @@ in vec2 v_src_texture_coords;
out f16vec4 frag_color;
f16vec4 Sample(f16sampler2D texture_sampler, vec2 texture_coords) {
#ifdef IMPELLER_TARGET_OPENGLES
if (blend_info.supports_decal_sampler_address_mode > 0.0) {
if (supports_decal > 0.0) {
return texture(texture_sampler, texture_coords);
} else {
return IPHalfSampleDecal(texture_sampler, texture_coords);
}
#else
return texture(texture_sampler, texture_coords);
#endif
return IPHalfSampleDecal(texture_sampler, texture_coords);
}
AdvancedBlend(blend_type);
void main() {
f16vec4 dst_sample = Sample(texture_sampler_dst, // sampler
v_dst_texture_coords // texture coordinates
@ -50,6 +51,7 @@ void main() {
) *
blend_info.src_input_alpha;
f16vec4 blended = mix(src, f16vec4(Blend(dst.rgb, src.rgb), dst.a), dst.a);
f16vec3 blend_result = Blend(dst.rgb, src.rgb);
f16vec4 blended = mix(src, f16vec4(blend_result, dst.a), dst.a);
frag_color = mix(dst_sample, blended, src.a);
}

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendColor(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendColorBurn(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendColorDodge(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendDarken(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendDifference(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendExclusion(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendHardLight(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendHue(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendLighten(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendLuminosity(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendMultiply(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendOverlay(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendSaturation(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendScreen(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendSoftLight(dst, src);
}
#include "advanced_blend.glsl"

View File

@ -0,0 +1,60 @@
// 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.
#include <impeller/blending.glsl>
#include <impeller/color.glsl>
#include <impeller/types.glsl>
// kScreen = 0,
// kOverlay,
// kDarken,
// kLighten,
// kColorDodge,
// kColorBurn,
// kHardLight,
// kSoftLight,
// kDifference,
// kExclusion,
// kMultiply,
// kHue,
// kSaturation,
// kColor,
// kLuminosity,
// Note, this isn't a switch as GLSL ES 1.0 does not support them.
#define AdvancedBlend(blend_type) \
f16vec3 Blend(f16vec3 dst, f16vec3 src) { \
if (blend_type == 0) { \
return IPBlendScreen(dst, src); \
} else if (blend_type == 1) { \
return IPBlendOverlay(dst, src); \
} else if (blend_type == 2) { \
return IPBlendDarken(dst, src); \
} else if (blend_type == 3) { \
return IPBlendLighten(dst, src); \
} else if (blend_type == 4) { \
return IPBlendColorDodge(dst, src); \
} else if (blend_type == 5) { \
return IPBlendColorBurn(dst, src); \
} else if (blend_type == 6) { \
return IPBlendHardLight(dst, src); \
} else if (blend_type == 7) { \
return IPBlendSoftLight(dst, src); \
} else if (blend_type == 8) { \
return IPBlendDifference(dst, src); \
} else if (blend_type == 9) { \
return IPBlendExclusion(dst, src); \
} else if (blend_type == 10) { \
return IPBlendMultiply(dst, src); \
} else if (blend_type == 11) { \
return IPBlendHue(dst, src); \
} else if (blend_type == 12) { \
return IPBlendSaturation(dst, src); \
} else if (blend_type == 13) { \
return IPBlendColor(dst, src); \
} else if (blend_type == 14) { \
return IPBlendLuminosity(dst, src); \
} else { \
return f16vec3(0.0hf); \
} \
}

View File

@ -8,6 +8,10 @@
#include <impeller/color.glsl>
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
#include "blend_select.glsl"
layout(constant_id = 0) const int blend_type = 0;
layout(constant_id = 1) const int supports_decal = 1;
layout(set = 0,
binding = 0,
@ -29,14 +33,14 @@ in vec2 v_src_texture_coords;
out vec4 frag_color;
vec4 Sample(sampler2D texture_sampler, vec2 texture_coords) {
// gles 2.0 is the only backend without native decal support.
#ifdef IMPELLER_TARGET_OPENGLES
if (supports_decal > 1) {
return texture(texture_sampler, texture_coords);
}
return IPSampleDecal(texture_sampler, texture_coords);
#else
return texture(texture_sampler, texture_coords);
#endif
}
AdvancedBlend(blend_type);
void main() {
f16vec4 dst = f16vec4(ReadDestination());
f16vec4 src = f16vec4(Sample(texture_sampler_src, // sampler
@ -44,6 +48,7 @@ void main() {
)) *
frag_info.src_input_alpha;
f16vec4 blended = mix(src, f16vec4(Blend(dst.rgb, src.rgb), dst.a), dst.a);
f16vec3 blend_result = Blend(dst.rgb, src.rgb);
f16vec4 blended = mix(src, f16vec4(blend_result, dst.a), dst.a);
frag_color = vec4(mix(dst, blended, src.a));
}

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendColor(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendColorBurn(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendColorDodge(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendDarken(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendDifference(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendExclusion(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendHardLight(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendHue(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendLighten(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendLuminosity(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendMultiply(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendOverlay(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendSaturation(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendScreen(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -1,14 +0,0 @@
// 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.
precision mediump float;
#include <impeller/blending.glsl>
#include <impeller/types.glsl>
f16vec3 Blend(f16vec3 dst, f16vec3 src) {
return IPBlendSoftLight(dst, src);
}
#include "framebuffer_blend.glsl"

View File

@ -9,6 +9,8 @@ precision mediump float;
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
layout(constant_id = 0) const int supports_decal = 1;
uniform f16sampler2D texture_sampler_dst;
uniform FragInfo {
@ -19,7 +21,6 @@ uniform FragInfo {
float16_t dst_coeff_src_color;
float16_t input_alpha;
float16_t output_alpha;
float supports_decal_sampler_address_mode;
}
frag_info;
@ -29,15 +30,10 @@ in f16vec4 v_color;
out f16vec4 frag_color;
f16vec4 Sample(f16sampler2D texture_sampler, vec2 texture_coords) {
#ifdef IMPELLER_TARGET_OPENGLES
if (frag_info.supports_decal_sampler_address_mode > 0.0) {
if (supports_decal > 0.0) {
return texture(texture_sampler, texture_coords);
} else {
return IPHalfSampleDecal(texture_sampler, texture_coords);
}
#else
return texture(texture_sampler, texture_coords);
#endif
return IPHalfSampleDecal(texture_sampler, texture_coords);
}
void main() {

View File

@ -20,6 +20,7 @@ impeller_component("gles_unittests") {
"test/mock_gles.cc",
"test/mock_gles.h",
"test/mock_gles_unittests.cc",
"test/specialization_constants_unittests.cc",
]
deps = [
":gles",

View File

@ -33,6 +33,19 @@ static std::string GetShaderInfoLog(const ProcTableGLES& gl, GLuint shader) {
return log_string;
}
static std::string GetShaderSource(const ProcTableGLES& gl, GLuint shader) {
// Arbitrarily chosen size that should be larger than most shaders.
// Since this only fires on compilation errors the performance shouldn't
// matter.
auto data = static_cast<char*>(malloc(10240));
GLsizei length;
gl.GetShaderSource(shader, 10240, &length, data);
auto result = std::string{data, static_cast<size_t>(length)};
free(data);
return result;
}
static void LogShaderCompilationFailure(const ProcTableGLES& gl,
GLuint shader,
const std::string& name,
@ -63,10 +76,7 @@ static void LogShaderCompilationFailure(const ProcTableGLES& gl,
stream << " shader for '" << name << "' with error:" << std::endl;
stream << GetShaderInfoLog(gl, shader) << std::endl;
stream << "Shader source was: " << std::endl;
stream << std::string{reinterpret_cast<const char*>(
source_mapping.GetMapping()),
source_mapping.GetSize()}
<< std::endl;
stream << GetShaderSource(gl, shader) << std::endl;
VALIDATION_LOG << stream.str();
}
@ -105,8 +115,10 @@ static bool LinkProgram(
fml::ScopedCleanupClosure delete_frag_shader(
[&gl, frag_shader]() { gl.DeleteShader(frag_shader); });
gl.ShaderSourceMapping(vert_shader, *vert_mapping);
gl.ShaderSourceMapping(frag_shader, *frag_mapping);
gl.ShaderSourceMapping(vert_shader, *vert_mapping,
descriptor.GetSpecializationConstants());
gl.ShaderSourceMapping(frag_shader, *frag_mapping,
descriptor.GetSpecializationConstants());
gl.CompileShader(vert_shader);
gl.CompileShader(frag_shader);

View File

@ -6,6 +6,7 @@
#include <sstream>
#include "fml/closure.h"
#include "impeller/base/allocation.h"
#include "impeller/base/comparable.h"
#include "impeller/base/validation.h"
@ -138,14 +139,53 @@ bool ProcTableGLES::IsValid() const {
return is_valid_;
}
void ProcTableGLES::ShaderSourceMapping(GLuint shader,
const fml::Mapping& mapping) const {
void ProcTableGLES::ShaderSourceMapping(
GLuint shader,
const fml::Mapping& mapping,
const std::vector<int32_t>& defines) const {
if (defines.empty()) {
const GLchar* sources[] = {
reinterpret_cast<const GLchar*>(mapping.GetMapping())};
const GLint lengths[] = {static_cast<GLint>(mapping.GetSize())};
ShaderSource(shader, 1u, sources, lengths);
return;
}
const auto& shader_source = ComputeShaderWithDefines(mapping, defines);
if (!shader_source.has_value()) {
VALIDATION_LOG << "Failed to append constant data to shader";
return;
}
const GLchar* sources[] = {
reinterpret_cast<const GLchar*>(mapping.GetMapping())};
const GLint lengths[] = {static_cast<GLint>(mapping.GetSize())};
reinterpret_cast<const GLchar*>(shader_source->c_str())};
const GLint lengths[] = {static_cast<GLint>(shader_source->size())};
ShaderSource(shader, 1u, sources, lengths);
}
// Visible For testing.
std::optional<std::string> ProcTableGLES::ComputeShaderWithDefines(
const fml::Mapping& mapping,
const std::vector<int32_t>& defines) const {
auto shader_source = std::string{
reinterpret_cast<const char*>(mapping.GetMapping()), mapping.GetSize()};
// Look for the first newline after the '#version' header, which impellerc
// will always emit as the first line of a compiled shader.
auto index = shader_source.find('\n');
if (index == std::string::npos) {
VALIDATION_LOG << "Failed to append constant data to shader";
return std::nullopt;
}
std::stringstream ss;
for (auto i = 0u; i < defines.size(); i++) {
ss << "#define SPIRV_CROSS_CONSTANT_ID_" << i << " " << defines[i] << '\n';
}
auto define_string = ss.str();
shader_source.insert(index + 1, define_string);
return shader_source;
}
const DescriptionGLES* ProcTableGLES::GetDescription() const {
return description_.get();
}

View File

@ -184,6 +184,7 @@ struct GLProc {
PROC(UseProgram); \
PROC(VertexAttribPointer); \
PROC(Viewport); \
PROC(GetShaderSource); \
PROC(ReadPixels);
#define FOR_EACH_IMPELLER_GLES3_PROC(PROC) PROC(BlitFramebuffer);
@ -231,7 +232,14 @@ class ProcTableGLES {
bool IsValid() const;
void ShaderSourceMapping(GLuint shader, const fml::Mapping& mapping) const;
/// @brief Set the source for the attached [shader].
///
/// Optionally, [defines] may contain a string value that will be
/// append to the shader source after the version marker. This can be used to
/// support static specialization. For example, setting "#define Foo 1".
void ShaderSourceMapping(GLuint shader,
const fml::Mapping& mapping,
const std::vector<int32_t>& defines = {}) const;
const DescriptionGLES* GetDescription() const;
@ -251,6 +259,11 @@ class ProcTableGLES {
void PopDebugGroup() const;
// Visible For testing.
std::optional<std::string> ComputeShaderWithDefines(
const fml::Mapping& mapping,
const std::vector<int32_t>& defines) const;
private:
bool is_valid_ = false;
std::unique_ptr<DescriptionGLES> description_;

View File

@ -30,7 +30,7 @@ class ShaderLibraryGLES final : public ShaderLibrary {
ShaderFunctionMap functions_ IPLR_GUARDED_BY(functions_mutex_);
bool is_valid_ = false;
ShaderLibraryGLES(
explicit ShaderLibraryGLES(
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries);
// |ShaderLibrary|

View File

@ -0,0 +1,75 @@
// 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.
#include "flutter/testing/testing.h" // IWYU pragma: keep
#include "fml/mapping.h"
#include "gtest/gtest.h"
#include "impeller/renderer/backend/gles/proc_table_gles.h"
#include "impeller/renderer/backend/gles/test/mock_gles.h"
namespace impeller {
namespace testing {
TEST(SpecConstant, CanCreateShaderWithSpecializationConstant) {
auto mock_gles = MockGLES::Init();
auto& proc_table = mock_gles->GetProcTable();
auto shader_source =
"#version 100\n"
"#ifndef SPIRV_CROSS_CONSTANT_ID_0\n"
"#define SPIRV_CROSS_CONSTANT_ID_0 1\n"
"#endif\n"
"void main() { return vec4(0.0); }";
auto test_shader = std::make_shared<fml::DataMapping>(shader_source);
auto result = proc_table.ComputeShaderWithDefines(*test_shader, {0});
auto expected_shader_source =
"#version 100\n"
"#define SPIRV_CROSS_CONSTANT_ID_0 0\n"
"#ifndef SPIRV_CROSS_CONSTANT_ID_0\n"
"#define SPIRV_CROSS_CONSTANT_ID_0 1\n"
"#endif\n"
"void main() { return vec4(0.0); }";
if (!result.has_value()) {
GTEST_FAIL() << "Expected shader source";
}
ASSERT_EQ(result.value(), expected_shader_source);
}
TEST(SpecConstant, CanCreateShaderWithSpecializationConstantMultipleValues) {
auto mock_gles = MockGLES::Init();
auto& proc_table = mock_gles->GetProcTable();
auto shader_source =
"#version 100\n"
"#ifndef SPIRV_CROSS_CONSTANT_ID_0\n"
"#define SPIRV_CROSS_CONSTANT_ID_0 1\n"
"#endif\n"
"void main() { return vec4(0.0); }";
auto test_shader = std::make_shared<fml::DataMapping>(shader_source);
auto result =
proc_table.ComputeShaderWithDefines(*test_shader, {0, 1, 2, 3, 4, 5});
auto expected_shader_source =
"#version 100\n"
"#define SPIRV_CROSS_CONSTANT_ID_0 0\n"
"#define SPIRV_CROSS_CONSTANT_ID_1 1\n"
"#define SPIRV_CROSS_CONSTANT_ID_2 2\n"
"#define SPIRV_CROSS_CONSTANT_ID_3 3\n"
"#define SPIRV_CROSS_CONSTANT_ID_4 4\n"
"#define SPIRV_CROSS_CONSTANT_ID_5 5\n"
"#ifndef SPIRV_CROSS_CONSTANT_ID_0\n"
"#define SPIRV_CROSS_CONSTANT_ID_0 1\n"
"#endif\n"
"void main() { return vec4(0.0); }";
if (!result.has_value()) {
GTEST_FAIL() << "Expected shader source";
}
ASSERT_EQ(result.value(), expected_shader_source);
}
} // namespace testing
} // namespace impeller

View File

@ -27,7 +27,7 @@ class PipelineLibraryMTL final : public PipelineLibrary {
PipelineMap pipelines_;
ComputePipelineMap compute_pipelines_;
PipelineLibraryMTL(id<MTLDevice> device);
explicit PipelineLibraryMTL(id<MTLDevice> device);
// |PipelineLibrary|
bool IsValid() const override;

View File

@ -4,6 +4,7 @@
#include "impeller/renderer/backend/metal/pipeline_library_mtl.h"
#include <Foundation/Foundation.h>
#include <Metal/Metal.h>
#include "flutter/fml/build_config.h"
@ -28,14 +29,25 @@ static MTLRenderPipelineDescriptor* GetMTLRenderPipelineDescriptor(
descriptor.label = @(desc.GetLabel().c_str());
descriptor.rasterSampleCount = static_cast<NSUInteger>(desc.GetSampleCount());
const auto& constants = desc.GetSpecializationConstants();
for (const auto& entry : desc.GetStageEntrypoints()) {
if (entry.first == ShaderStage::kVertex) {
descriptor.vertexFunction =
ShaderFunctionMTL::Cast(*entry.second).GetMTLFunction();
if (constants.empty()) {
descriptor.vertexFunction =
ShaderFunctionMTL::Cast(*entry.second).GetMTLFunction();
} else {
descriptor.vertexFunction = ShaderFunctionMTL::Cast(*entry.second)
.GetMTLFunctionSpecialized(constants);
}
}
if (entry.first == ShaderStage::kFragment) {
descriptor.fragmentFunction =
ShaderFunctionMTL::Cast(*entry.second).GetMTLFunction();
if (constants.empty()) {
descriptor.fragmentFunction =
ShaderFunctionMTL::Cast(*entry.second).GetMTLFunction();
} else {
descriptor.fragmentFunction = ShaderFunctionMTL::Cast(*entry.second)
.GetMTLFunctionSpecialized(constants);
}
}
}

View File

@ -21,13 +21,18 @@ class ShaderFunctionMTL final
id<MTLFunction> GetMTLFunction() const;
id<MTLFunction> GetMTLFunctionSpecialized(
const std::vector<int>& constants) const;
private:
friend class ShaderLibraryMTL;
id<MTLFunction> function_ = nullptr;
id<MTLLibrary> library_ = nullptr;
ShaderFunctionMTL(UniqueID parent_library_id,
id<MTLFunction> function,
id<MTLLibrary> library,
std::string name,
ShaderStage stage);

View File

@ -8,13 +8,37 @@ namespace impeller {
ShaderFunctionMTL::ShaderFunctionMTL(UniqueID parent_library_id,
id<MTLFunction> function,
id<MTLLibrary> library,
std::string name,
ShaderStage stage)
: ShaderFunction(parent_library_id, std::move(name), stage),
function_(function) {}
function_(function),
library_(library) {}
ShaderFunctionMTL::~ShaderFunctionMTL() = default;
id<MTLFunction> ShaderFunctionMTL::GetMTLFunctionSpecialized(
const std::vector<int>& constants) const {
MTLFunctionConstantValues* constantValues =
[[MTLFunctionConstantValues alloc] init];
size_t index = 0;
for (const auto value : constants) {
int copied_value = value;
[constantValues setConstantValue:&copied_value
type:MTLDataTypeInt
atIndex:index];
index++;
}
NSError* error = nil;
auto result = [library_ newFunctionWithName:@(GetName().data())
constantValues:constantValues
error:&error];
if (error != nil) {
return nil;
}
return result;
}
id<MTLFunction> ShaderFunctionMTL::GetMTLFunction() const {
return function_;
}

View File

@ -39,7 +39,7 @@ class ShaderLibraryMTL final : public ShaderLibrary {
ShaderFunctionMap functions_;
bool is_valid_ = false;
ShaderLibraryMTL(NSArray<id<MTLLibrary>>* libraries);
explicit ShaderLibraryMTL(NSArray<id<MTLLibrary>>* libraries);
// |ShaderLibrary|
std::shared_ptr<const ShaderFunction> GetFunction(std::string_view name,

View File

@ -55,6 +55,7 @@ std::shared_ptr<const ShaderFunction> ShaderLibraryMTL::GetFunction(
ShaderKey key(name, stage);
id<MTLFunction> function = nil;
id<MTLLibrary> library = nil;
{
ReaderLock lock(libraries_mutex_);
@ -64,7 +65,8 @@ std::shared_ptr<const ShaderFunction> ShaderLibraryMTL::GetFunction(
}
for (size_t i = 0, count = [libraries_ count]; i < count; i++) {
function = [libraries_[i] newFunctionWithName:@(name.data())];
library = libraries_[i];
function = [library newFunctionWithName:@(name.data())];
if (function) {
break;
}
@ -81,7 +83,7 @@ std::shared_ptr<const ShaderFunction> ShaderLibraryMTL::GetFunction(
}
auto func = std::shared_ptr<ShaderFunctionMTL>(new ShaderFunctionMTL(
library_id_, function, {name.data(), name.size()}, stage));
library_id_, function, library, {name.data(), name.size()}, stage));
functions_[key] = func;
return func;

View File

@ -278,7 +278,15 @@ std::unique_ptr<PipelineVK> PipelineLibraryVK::CreatePipeline(
//----------------------------------------------------------------------------
/// Shader Stages
///
const auto& constants = desc.GetSpecializationConstants();
std::vector<std::vector<vk::SpecializationMapEntry>> map_entries(
desc.GetStageEntrypoints().size());
std::vector<vk::SpecializationInfo> specialization_infos(
desc.GetStageEntrypoints().size());
std::vector<vk::PipelineShaderStageCreateInfo> shader_stages;
size_t entrypoint_count = 0;
for (const auto& entrypoint : desc.GetStageEntrypoints()) {
auto stage = ToVKShaderStageFlagBits(entrypoint.first);
if (!stage.has_value()) {
@ -286,12 +294,31 @@ std::unique_ptr<PipelineVK> PipelineLibraryVK::CreatePipeline(
<< desc.GetLabel();
return nullptr;
}
std::vector<vk::SpecializationMapEntry>& entries =
map_entries[entrypoint_count];
for (auto i = 0u; i < constants.size(); i++) {
vk::SpecializationMapEntry entry;
entry.offset = (i * sizeof(int32_t));
entry.size = sizeof(int32_t);
entry.constantID = i;
entries.emplace_back(entry);
}
vk::SpecializationInfo& specialization_info =
specialization_infos[entrypoint_count];
specialization_info.setMapEntries(map_entries[entrypoint_count]);
specialization_info.setPData(constants.data());
specialization_info.setDataSize(sizeof(int32_t) * constants.size());
vk::PipelineShaderStageCreateInfo info;
info.setStage(stage.value());
info.setPName("main");
info.setModule(
ShaderFunctionVK::Cast(entrypoint.second.get())->GetModule());
info.setPSpecializationInfo(&specialization_info);
shader_stages.push_back(info);
entrypoint_count++;
}
pipeline_info.setStages(shader_stages);

View File

@ -48,8 +48,10 @@ struct PipelineBuilder {
/// context, a pipeline descriptor.
///
static std::optional<PipelineDescriptor> MakeDefaultPipelineDescriptor(
const Context& context) {
const Context& context,
const std::vector<int>& constants = {}) {
PipelineDescriptor desc;
desc.SetSpecializationConstants(constants);
if (InitializePipelineDescriptorDefaults(context, desc)) {
return {std::move(desc)};
}

View File

@ -4,6 +4,9 @@
#include "impeller/renderer/pipeline_descriptor.h"
#include <utility>
#include "impeller/base/comparable.h"
#include "impeller/core/formats.h"
#include "impeller/renderer/shader_function.h"
#include "impeller/renderer/shader_library.h"
@ -61,7 +64,8 @@ bool PipelineDescriptor::IsEqual(const PipelineDescriptor& other) const {
winding_order_ == other.winding_order_ &&
cull_mode_ == other.cull_mode_ &&
primitive_type_ == other.primitive_type_ &&
polygon_mode_ == other.polygon_mode_;
polygon_mode_ == other.polygon_mode_ &&
specialization_constants_ == other.specialization_constants_;
}
PipelineDescriptor& PipelineDescriptor::SetLabel(std::string label) {
@ -277,4 +281,14 @@ PolygonMode PipelineDescriptor::GetPolygonMode() const {
return polygon_mode_;
}
void PipelineDescriptor::SetSpecializationConstants(
std::vector<int32_t> values) {
specialization_constants_ = std::move(values);
}
const std::vector<int32_t>& PipelineDescriptor::GetSpecializationConstants()
const {
return specialization_constants_;
}
} // namespace impeller

View File

@ -4,17 +4,10 @@
#pragma once
#include <functional>
#include <future>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include "flutter/fml/hash_combine.h"
#include "flutter/fml/macros.h"
#include "impeller/base/comparable.h"
#include "impeller/core/formats.h"
#include "impeller/core/shader_types.h"
@ -131,6 +124,10 @@ class PipelineDescriptor final : public Comparable<PipelineDescriptor> {
PolygonMode GetPolygonMode() const;
void SetSpecializationConstants(std::vector<int32_t> values);
const std::vector<int32_t>& GetSpecializationConstants() const;
private:
std::string label_;
SampleCount sample_count_ = SampleCount::kCount1;
@ -149,6 +146,7 @@ class PipelineDescriptor final : public Comparable<PipelineDescriptor> {
back_stencil_attachment_descriptor_;
PrimitiveType primitive_type_ = PrimitiveType::kTriangle;
PolygonMode polygon_mode_ = PolygonMode::kFill;
std::vector<int32_t> specialization_constants_;
};
} // namespace impeller

View File

@ -19,6 +19,10 @@ ShaderStage ShaderFunction::GetStage() const {
return stage_;
}
const std::string& ShaderFunction::GetName() const {
return name_;
}
// |Comparable<ShaderFunction>|
std::size_t ShaderFunction::GetHash() const {
return fml::HashCombine(parent_library_id_, name_, stage_);

View File

@ -4,6 +4,7 @@
#pragma once
#include <string>
#include "flutter/fml/hash_combine.h"
#include "flutter/fml/macros.h"
#include "impeller/base/comparable.h"
@ -18,6 +19,8 @@ class ShaderFunction : public Comparable<ShaderFunction> {
ShaderStage GetStage() const;
const std::string& GetName() const;
// |Comparable<ShaderFunction>|
std::size_t GetHash() const override;

File diff suppressed because it is too large Load Diff