• 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_SHADER_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_SHADER_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/gl/portable_gl31.h"
24 
25 namespace tflite {
26 namespace gpu {
27 namespace gl {
28 
29 // A wrapper around opengl shader id that needs to be recycled when not needed.
30 class GlShader {
31  public:
32   // Creates and compiles a shader.
33   //
34   // @param shader_type is one of GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, or
35   // GL_COMPUTE_SHADER.
36   static absl::Status CompileShader(GLenum shader_type,
37                                     const std::string& shader_source,
38                                     GlShader* gl_shader);
39 
GlShader()40   GlShader() : id_(0) {}
41 
42   // move-only
43   GlShader(GlShader&& shader);
44   GlShader& operator=(GlShader&& shader);
45   GlShader(const GlShader&) = delete;
46   GlShader& operator=(const GlShader&) = delete;
47 
48   ~GlShader();
49 
id()50   GLuint id() const { return id_; }
51 
52  private:
GlShader(GLuint id)53   explicit GlShader(GLuint id) : id_(id) {}
54 
55   void Invalidate();
56 
57   GLuint id_;
58 };
59 
60 // Holds binary blob for compiled shader. It can be used to instantiate
61 // a program instead of plain Shader that will need to be compiled first.
62 //
63 // Some OpenGL implementations allow to extract binary representation once it
64 // is compiled. Call Program::GetBinary after program is successfully created
65 // with a shader from sources.
66 class BinaryShader {
67  public:
BinaryShader(GLenum format,std::vector<uint8_t> binary)68   BinaryShader(GLenum format, std::vector<uint8_t> binary)
69       : format_(format), binary_(std::move(binary)) {}
70 
format()71   GLenum format() const { return format_; }
72 
binary()73   const std::vector<uint8_t>& binary() const { return binary_; }
74 
75  private:
76   GLenum format_;
77   std::vector<uint8_t> binary_;
78 };
79 
80 }  // namespace gl
81 }  // namespace gpu
82 }  // namespace tflite
83 
84 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_SHADER_H_
85