1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_VARIABLE_ACCESSOR_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_VARIABLE_ACCESSOR_H_ 18 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 #include "absl/container/flat_hash_map.h" 24 #include "tensorflow/lite/delegates/gpu/gl/compiler/preprocessor.h" 25 #include "tensorflow/lite/delegates/gpu/gl/variable.h" 26 27 namespace tflite { 28 namespace gpu { 29 namespace gl { 30 31 // This rewrite handles access to variables. It may rewrite a variable with 32 // actual values if 'inline_values' is set to true. 33 // 34 // The following syntax is supported to access variables: 35 // - simple variable: name 36 // - variable with field: name.(x|y|z|w) 37 // - variable with index: name[i] 38 // - variable with index and field: name[i].(x|y|z|w) 39 // 40 // If 'inline_values' is set to true, non-variable-length variables will be 41 // inlined. For example, 'base.x' will be replaced with value of 'x' field from 42 // 'base'. Variable-length variables are declared as const and accessed via 43 // index. These declarations are returned by GetConstDeclarations. 44 // 45 // If 'inline_values' is set to false, all variables will be declared as 46 // uniforms. Uniform declarations are returned by GetUniformDeclarations. 47 class VariableAccessor : public InlineRewrite { 48 public: 49 explicit VariableAccessor(bool inline_values, bool vulkan_support = false) inline_values_(inline_values)50 : inline_values_(inline_values), vulkan_support_(vulkan_support) {} 51 52 RewriteStatus Rewrite(absl::string_view input, std::string* output) final; 53 54 // Returns true if variable was successfully added. 55 bool AddSharedVariable(Variable&& variable); 56 57 // Returns true if variable was successfully added. 58 bool AddUniformParameter(Variable&& variable); 59 60 // Returns true if variable value is an empty vector. 61 bool IsEmptyVariableLength(const Variable& variable) const; 62 63 // Returns const variables that need to be inlined in the a shader's code. 64 std::string GetConstDeclarations() const; 65 66 // Returns shared variable declarations that need to be inlined. 67 std::string GetSharedVariableDeclarations() const; 68 69 // Returns uniform parameter declarations that need to be inlined. 70 std::string GetUniformParameterDeclarations() const; 71 72 // Returns a collection of uniform parameters. 73 std::vector<Variable> GetUniformParameters() const; 74 75 private: 76 const bool inline_values_; 77 const bool vulkan_support_; 78 absl::flat_hash_map<std::string, Variable> name_to_variable_; 79 std::set<std::string> shared_variables_; 80 std::set<std::string> uniform_parameters_; 81 }; 82 83 // Implementation details below. 84 85 namespace variable_accessor_internal { 86 87 struct VariableReference { 88 absl::string_view name; 89 absl::string_view index; 90 absl::string_view field; 91 }; 92 93 // Parse the following regex manually 94 // name(\[index\])?(\.field)? 95 VariableReference Parse(absl::string_view input); 96 97 } // namespace variable_accessor_internal 98 } // namespace gl 99 } // namespace gpu 100 } // namespace tflite 101 102 #endif // TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_VARIABLE_ACCESSOR_H_ 103