Flutter Impeller
shader_stage_compatibility_checker.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_RENDERER_SHADER_STAGE_COMPATIBILITY_CHECKER_H_
6 #define FLUTTER_IMPELLER_RENDERER_SHADER_STAGE_COMPATIBILITY_CHECKER_H_
7 
8 #include <cstddef>
9 
11 
12 namespace impeller {
13 /// This is a classed use to check that the input slots of fragment shaders
14 /// match the output slots of the vertex shaders.
15 /// If they don't match it will result in linker errors when creating the
16 /// pipeline. It's not used at runtime.
17 template <typename VertexShaderT, typename FragmentShaderT>
19  public:
20  static constexpr bool CompileTimeStrEqual(const char* str1,
21  const char* str2) {
22  return *str1 == *str2 &&
23  (*str1 == '\0' || CompileTimeStrEqual(str1 + 1, str2 + 1));
24  }
25 
26  /// Returns `true` if the shader input slots for the fragment shader match the
27  /// ones declared as outputs in the vertex shader.
28  static constexpr bool Check() {
29  constexpr size_t num_outputs = VertexShaderT::kAllShaderStageOutputs.size();
30  constexpr size_t num_inputs = FragmentShaderT::kAllShaderStageInputs.size();
31 
32  if (num_inputs > num_outputs) {
33  return false;
34  }
35 
36  for (size_t i = 0; i < num_inputs; ++i) {
37  const ShaderStageIOSlot* input_slot =
38  FragmentShaderT::kAllShaderStageInputs[i];
39  for (size_t j = 0; j < num_outputs; ++j) {
40  const ShaderStageIOSlot* output_slot =
41  VertexShaderT::kAllShaderStageOutputs[j];
42  if (input_slot->location == output_slot->location) {
43  if (!CompileTimeStrEqual(input_slot->name, output_slot->name) ||
44  input_slot->set != output_slot->set ||
45  input_slot->binding != output_slot->binding ||
46  input_slot->type != output_slot->type ||
47  input_slot->bit_width != output_slot->bit_width ||
48  input_slot->vec_size != output_slot->vec_size ||
49  input_slot->columns != output_slot->columns ||
50  input_slot->offset != output_slot->offset) {
51  return false;
52  }
53  }
54  }
55  }
56 
57  return true;
58  }
59 };
60 
61 // The following shaders don't define output slots.
62 // TODO(https://github.com/flutter/flutter/issues/146852): Make impellerc emit
63 // an empty array for output slots.
64 struct ClipVertexShader;
65 struct SolidFillVertexShader;
66 
67 template <typename FragmentShaderT>
68 class ShaderStageCompatibilityChecker<ClipVertexShader, FragmentShaderT> {
69  public:
70  static constexpr bool Check() { return true; }
71 };
72 template <typename FragmentShaderT>
73 class ShaderStageCompatibilityChecker<SolidFillVertexShader, FragmentShaderT> {
74  public:
75  static constexpr bool Check() { return true; }
76 };
77 } // namespace impeller
78 #endif // FLUTTER_IMPELLER_RENDERER_SHADER_STAGE_COMPATIBILITY_CHECKER_H_
impeller::ShaderStageCompatibilityChecker::Check
static constexpr bool Check()
Definition: shader_stage_compatibility_checker.h:28
impeller::ShaderStageIOSlot::binding
size_t binding
Definition: shader_types.h:116
impeller::ShaderStageIOSlot::name
const char * name
Definition: shader_types.h:113
impeller::ShaderStageIOSlot
Definition: shader_types.h:112
impeller::ShaderStageIOSlot::columns
size_t columns
Definition: shader_types.h:120
impeller::ShaderStageCompatibilityChecker< SolidFillVertexShader, FragmentShaderT >::Check
static constexpr bool Check()
Definition: shader_stage_compatibility_checker.h:75
impeller::ShaderStageIOSlot::offset
size_t offset
Definition: shader_types.h:121
impeller::ShaderStageCompatibilityChecker::CompileTimeStrEqual
static constexpr bool CompileTimeStrEqual(const char *str1, const char *str2)
Definition: shader_stage_compatibility_checker.h:20
impeller::ShaderStageIOSlot::bit_width
size_t bit_width
Definition: shader_types.h:118
impeller::ShaderStageIOSlot::set
size_t set
Definition: shader_types.h:115
impeller::ShaderStageIOSlot::location
size_t location
Definition: shader_types.h:114
impeller::ShaderStageIOSlot::type
ShaderType type
Definition: shader_types.h:117
impeller::ShaderStageCompatibilityChecker< ClipVertexShader, FragmentShaderT >::Check
static constexpr bool Check()
Definition: shader_stage_compatibility_checker.h:70
shader_types.h
impeller
Definition: aiks_blend_unittests.cc:18
impeller::ShaderStageIOSlot::vec_size
size_t vec_size
Definition: shader_types.h:119
impeller::ShaderStageCompatibilityChecker
Definition: shader_stage_compatibility_checker.h:18