1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*!
21  * \file codegen_opengl.h
22  * \brief Generate OpenGL device code.
23  */
24 #ifndef TVM_CODEGEN_CODEGEN_OPENGL_H_
25 #define TVM_CODEGEN_CODEGEN_OPENGL_H_
26 
27 #include <tvm/codegen.h>
28 #include <tvm/packed_func_ext.h>
29 #include <string>
30 #include <unordered_set>
31 #include <unordered_map>
32 #include "codegen_c.h"
33 #include "../runtime/opengl/opengl_module.h"
34 
35 namespace tvm {
36 namespace codegen {
37 
38 class CodeGenOpenGL final : public CodeGenC {
39  public:
40   CodeGenOpenGL();
41   void AddFunction(LoweredFunc f);
42   std::unordered_map<std::string, runtime::OpenGLShader> Finish();
43 
44   void InitFuncState(LoweredFunc f) final;
45   void BindThreadIndex(const IterVar& iv) final;
46   void VisitStmt_(const Store* op) final;
47   std::string TexelFetch(const Variable* buffer, Expr index);
48   std::string GetBufferRef(Type t, const Variable* buffer, Expr index) final;
49   void PrintType(Type t, std::ostream& os) final; // NOLINT(*)
50 
51   // Codegen for immediate values
52   void VisitExpr_(const IntImm* op, std::ostream& os) final;  // NOLINT(*)
53   void VisitExpr_(const UIntImm* op, std::ostream& os) final;  // NOLINT(*)
54   void VisitExpr_(const FloatImm* op, std::ostream& os) final;  // NOLINT(*)
55   void VisitExpr_(const StringImm* op, std::ostream& os) final;  // NOLINT(*)
56 
57   // Match glsl_texture_store Call.
58   void VisitStmt_(const Evaluate* op) final;  // NOLINT(*)
59 
60  private:
61   const Variable* output_{nullptr};
62   std::unordered_set<const Variable*> inputs_;
63   const Variable* output_iter_var_{nullptr};
64   std::unordered_map<std::string, runtime::OpenGLShader> shaders_;
65   std::string thread_extent_var_;
66 };
67 
68 }  // namespace codegen
69 }  // namespace tvm
70 
71 #endif  // TVM_CODEGEN_CODEGEN_OPENGL_H_
72