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_OBJECT_ACCESSOR_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_OBJECT_ACCESSOR_H_
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "tensorflow/lite/delegates/gpu/gl/compiler/preprocessor.h"
24 #include "tensorflow/lite/delegates/gpu/gl/compiler/variable_accessor.h"
25 #include "tensorflow/lite/delegates/gpu/gl/object.h"
26 
27 namespace tflite {
28 namespace gpu {
29 namespace gl {
30 
31 // This rewrite handles access to objects both reads and writes.
32 //
33 // The following syntax is supported to access objects:
34 //
35 //   READ:
36 //     vec4 value = $data[i]$;
37 //       where data is a buffer or 1D texture
38 //     vec4 value = $data[i,j]$;
39 //       where data is 2D texture
40 //     vec4 value = $data[i,j,k]$;
41 //       where data is 3D texture
42 //
43 //   WRITE:
44 //     $data[i] = value$;
45 //       where data is a buffer or 1D texture
46 //     $data[i,j] = value$;
47 //       where data is 2D texture
48 //     $data[i,j,k] = value$;
49 //       where data is 3D texture
50 //
51 // Accessor supports all types (gvecN) as well as float16.
52 //
53 // TODO(akulik): support field in data[x,y,z].x
54 //
55 class ObjectAccessor : public InlineRewrite {
56  public:
ObjectAccessor(bool is_mali,VariableAccessor * variable_accessor)57   ObjectAccessor(bool is_mali, VariableAccessor* variable_accessor)
58       : ObjectAccessor(is_mali, /*sampler_textures=*/false, variable_accessor) {
59   }
60 
ObjectAccessor(bool is_mali,bool sampler_textures,VariableAccessor * variable_accessor)61   ObjectAccessor(bool is_mali, bool sampler_textures,
62                  VariableAccessor* variable_accessor)
63       : is_mali_(is_mali),
64         sampler_textures_(sampler_textures),
65         variable_accessor_(variable_accessor) {}
66 
67   RewriteStatus Rewrite(absl::string_view input, std::string* output) final;
68 
69   // Return true if object was successfully added.
70   bool AddObject(const std::string& name, Object object);
71 
72   // Returns objects declarations that need to be added in a shader's code.
73   std::string GetObjectDeclarations() const;
74 
75   // Returns functions declarations that need to be added in a shader's code.
76   // These functions are used by code accessing objects.
77   std::string GetFunctionsDeclarations() const;
78 
79   // Returns a collection of registered objects
80   std::vector<Object> GetObjects() const;
81 
82  private:
83   RewriteStatus RewriteRead(absl::string_view location, std::string* output);
84 
85   RewriteStatus RewriteWrite(absl::string_view location,
86                              absl::string_view value, std::string* output);
87 
88   std::map<std::string, Object> name_to_object_;
89 
90   const bool is_mali_;
91   const bool sampler_textures_;
92   VariableAccessor* variable_accessor_;
93 };
94 
95 // Implementation details below.
96 
97 namespace object_accessor_internal {
98 
99 // Refers to an element in an object.
100 struct IndexedElement {
101   absl::string_view object_name;
102   std::vector<absl::string_view> indices;
103 };
104 
105 // Splits name[index1, index2...] into 'name' and {'index1', 'index2'...}.
106 IndexedElement ParseElement(absl::string_view input);
107 
108 }  // namespace object_accessor_internal
109 }  // namespace gl
110 }  // namespace gpu
111 }  // namespace tflite
112 
113 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_OBJECT_ACCESSOR_H_
114