1 #ifndef HALIDE_CODEGEN_OPENGL_DEV_H
2 #define HALIDE_CODEGEN_OPENGL_DEV_H
3 
4 /** \file
5  * Defines the code-generator for producing GLSL kernel code
6  */
7 
8 #include <map>
9 #include <sstream>
10 
11 #include "CodeGen_C.h"
12 #include "CodeGen_GPU_Dev.h"
13 #include "Target.h"
14 
15 namespace Halide {
16 namespace Internal {
17 
18 class CodeGen_GLSL;
19 
20 class CodeGen_OpenGL_Dev : public CodeGen_GPU_Dev {
21 public:
22     CodeGen_OpenGL_Dev(const Target &target);
23     ~CodeGen_OpenGL_Dev() override;
24 
25     // CodeGen_GPU_Dev interface
26     void add_kernel(Stmt stmt, const std::string &name,
27                     const std::vector<DeviceArgument> &args) override;
28 
29     void init_module() override;
30 
31     std::vector<char> compile_to_src() override;
32 
33     std::string get_current_kernel_name() override;
34 
35     void dump() override;
36 
api_unique_name()37     std::string api_unique_name() override {
38         return "opengl";
39     }
40 
41 private:
42     CodeGen_GLSL *glc;
43 
44     std::string print_gpu_name(const std::string &name) override;
45 
46 private:
47     std::ostringstream src_stream;
48     std::string cur_kernel_name;
49     Target target;
50 };
51 
52 /**
53   * This class handles GLSL arithmetic, shared by CodeGen_GLSL and CodeGen_OpenGLCompute_C.
54   */
55 class CodeGen_GLSLBase : public CodeGen_C {
56 public:
57     CodeGen_GLSLBase(std::ostream &s, Target t);
58 
59     std::string print_name(const std::string &name) override;
60     std::string print_type(Type type, AppendSpaceIfNeeded space_option = DoNotAppendSpace) override;
61 
62 protected:
63     using CodeGen_C::visit;
64 
65     void visit(const Cast *) override;
66 
67     void visit(const FloatImm *) override;
68     void visit(const UIntImm *) override;
69     void visit(const IntImm *) override;
70 
71     void visit(const Max *op) override;
72     void visit(const Min *op) override;
73     void visit(const Call *op) override;
74 
75     void visit(const Mod *) override;
76 
77     // these have specific functions
78     // in GLSL that operate on vectors
79     void visit(const EQ *) override;
80     void visit(const NE *) override;
81     void visit(const LT *) override;
82     void visit(const LE *) override;
83     void visit(const GT *) override;
84     void visit(const GE *) override;
85 
86     void visit(const Shuffle *) override;
87 
88     Type map_type(const Type &);
89 
90     std::map<std::string, std::string> builtin;
91 };
92 
93 /** Compile one statement into GLSL. */
94 class CodeGen_GLSL : public CodeGen_GLSLBase {
95 public:
96     CodeGen_GLSL(std::ostream &s, const Target &t);
97 
98     void add_kernel(const Stmt &stmt,
99                     const std::string &name,
100                     const std::vector<DeviceArgument> &args);
101 
102     static void test();
103 
104 protected:
105     using CodeGen_GLSLBase::visit;
106 
107     void visit(const Let *) override;
108     void visit(const For *) override;
109     void visit(const Select *) override;
110 
111     void visit(const Load *) override;
112     void visit(const Store *) override;
113     void visit(const Allocate *) override;
114     void visit(const Free *) override;
115 
116     void visit(const Call *) override;
117     void visit(const AssertStmt *) override;
118     void visit(const Ramp *op) override;
119     void visit(const Broadcast *) override;
120 
121     void visit(const Evaluate *) override;
122     void visit(const Atomic *) override;
123 
124 private:
125     std::string get_vector_suffix(const Expr &e);
126 
127     std::vector<std::string> print_lanes(const Expr &expr);
128 
129     Scope<int> scalar_vars, vector_vars;
130 };
131 
132 }  // namespace Internal
133 }  // namespace Halide
134 
135 #endif
136