|
| 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 | +namespace impeller { |
| 9 | +/// This is a classed use to check that the input slots of fragment shaders |
| 10 | +/// match the output slots of the vertex shaders. |
| 11 | +/// If they don't match it will result in linker errors when creating the |
| 12 | +/// pipeline. It's not used at runtime. |
| 13 | +template <typename VertexShaderT, typename FragmentShaderT> |
| 14 | +class ShaderStageCompatibilityChecker { |
| 15 | + public: |
| 16 | + static constexpr bool CompileTimeStrEqual(const char* str1, |
| 17 | + const char* str2) { |
| 18 | + return *str1 == *str2 && |
| 19 | + (*str1 == '\0' || CompileTimeStrEqual(str1 + 1, str2 + 1)); |
| 20 | + } |
| 21 | + |
| 22 | + /// Returns `true` if the shader input slots for the fragment shader match the |
| 23 | + /// ones declared as outputs in the vertex shader. |
| 24 | + static constexpr bool Check() { |
| 25 | + constexpr size_t num_outputs = VertexShaderT::kAllShaderStageOutputs.size(); |
| 26 | + constexpr size_t num_inputs = FragmentShaderT::kAllShaderStageInputs.size(); |
| 27 | + |
| 28 | + if (num_inputs > num_outputs) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + for (size_t i = 0; i < num_inputs; ++i) { |
| 33 | + const ShaderStageIOSlot* input_slot = |
| 34 | + FragmentShaderT::kAllShaderStageInputs[i]; |
| 35 | + for (size_t j = 0; j < num_outputs; ++j) { |
| 36 | + const ShaderStageIOSlot* output_slot = |
| 37 | + VertexShaderT::kAllShaderStageOutputs[j]; |
| 38 | + if (input_slot->location == output_slot->location) { |
| 39 | + if (!CompileTimeStrEqual(input_slot->name, output_slot->name) || |
| 40 | + input_slot->set != output_slot->set || |
| 41 | + input_slot->binding != output_slot->binding || |
| 42 | + input_slot->type != output_slot->type || |
| 43 | + input_slot->bit_width != output_slot->bit_width || |
| 44 | + input_slot->vec_size != output_slot->vec_size || |
| 45 | + input_slot->columns != output_slot->columns || |
| 46 | + input_slot->offset != output_slot->offset) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return true; |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +// The following shaders don't define output slots. |
| 58 | +// TODO(https://github.com/flutter/flutter/issues/146852): Make impellerc emit |
| 59 | +// an empty array for output slots. |
| 60 | +struct CheckerboardVertexShader; |
| 61 | +struct ClipVertexShader; |
| 62 | + |
| 63 | +template <typename FragmentShaderT> |
| 64 | +class ShaderStageCompatibilityChecker<CheckerboardVertexShader, |
| 65 | + FragmentShaderT> { |
| 66 | + public: |
| 67 | + static constexpr bool Check() { return true; } |
| 68 | +}; |
| 69 | + |
| 70 | +template <typename FragmentShaderT> |
| 71 | +class ShaderStageCompatibilityChecker<ClipVertexShader, FragmentShaderT> { |
| 72 | + public: |
| 73 | + static constexpr bool Check() { return true; } |
| 74 | +}; |
| 75 | +} // namespace impeller |
| 76 | +#endif // FLUTTER_IMPELLER_RENDERER_SHADER_STAGE_COMPATIBILITY_CHECKER_H_ |
0 commit comments