Flutter Engine
The Flutter Engine
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
12namespace 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.
17template <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.
64struct ClipVertexShader;
65struct SolidFillVertexShader;
66
67template <typename FragmentShaderT>
68class ShaderStageCompatibilityChecker<ClipVertexShader, FragmentShaderT> {
69 public:
70 static constexpr bool Check() { return true; }
71};
72template <typename FragmentShaderT>
73class 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_
static constexpr bool CompileTimeStrEqual(const char *str1, const char *str2)