• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GL_PROGRAM_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_PROGRAM_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/common/types.h"
24 #include "tensorflow/lite/delegates/gpu/gl/gl_shader.h"
25 #include "tensorflow/lite/delegates/gpu/gl/portable_gl31.h"
26 #include "tensorflow/lite/delegates/gpu/gl/variable.h"
27 
28 namespace tflite {
29 namespace gpu {
30 namespace gl {
31 
32 // A wrapper around opengl program id that needs to be recycled when not needed.
33 // Encapsulates logic needed to bind parameters, link a program and execute it.
34 class GlProgram {
35  public:
36   // Creates invalid program.
GlProgram()37   GlProgram() : id_(0) {}
38 
39   // Creates new program, initializes it, attaches the given shader and links
40   // a program. Thus, if this call returns a program, one may set parameters and
41   // finally execute a program.
42   // therefore it needs to be handled elsewhere.
43   static absl::Status CreateWithShader(const GlShader& shader,
44                                        GlProgram* gl_program);
45 
46   // Same as CreateWithShader but takes compiled shader in a binary form,
47   // therefore compilation step is avoided.
48   static absl::Status CreateWithBinaryShader(const BinaryShader& shader,
49                                              GlProgram* gl_program);
50 
51   // move-only
52   GlProgram(GlProgram&& program);
53   GlProgram& operator=(GlProgram&& program);
54   GlProgram(const GlProgram&) = delete;
55   GlProgram& operator=(const GlProgram&) = delete;
56 
57   ~GlProgram();
58 
id()59   GLuint id() const { return id_; }
60 
61   // Returns a binary representation for a shader currently attached and linked
62   // into this program.
63   absl::Status GetBinary(BinaryShader* binary_shader);
64 
65   absl::Status SetParameter(const Variable& param);
66 
67   // Executes program
68   absl::Status Dispatch(const uint3& workgroups) const;
69 
is_valid()70   bool is_valid() const { return id_ != 0; }
71 
72  private:
GlProgram(GLuint program_id)73   explicit GlProgram(GLuint program_id) : id_(program_id) {}
74 
75   void Invalidate();
76 
77   GLint GetUniformId(const std::string& name);
78 
79   GLuint id_;
80 };
81 
82 }  // namespace gl
83 }  // namespace gpu
84 }  // namespace tflite
85 
86 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_PROGRAM_H_
87