Halide
CodeGen_OpenGL_Dev.h
Go to the documentation of this file.
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 
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 
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. */
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:
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
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Halide::Internal::CodeGen_GLSL::test
static void test()
Halide::Internal::CodeGen_GLSLBase::visit
void visit(const Cast *) override
Halide::Internal::CodeGen_GLSLBase::map_type
Type map_type(const Type &)
CodeGen_C.h
Halide::Internal::CodeGen_C::visit
void visit(const Variable *) override
Halide::Internal::CodeGen_OpenGL_Dev::~CodeGen_OpenGL_Dev
~CodeGen_OpenGL_Dev() override
Halide::Internal::GE
Is the first expression greater than or equal to the second.
Definition: IR.h:148
Halide::Internal::For
A for loop.
Definition: IR.h:698
Halide::Internal::CodeGen_GLSLBase::print_name
std::string print_name(const std::string &name) override
Emit a version of a string that is a valid identifier in C (.
Halide::Internal::FloatImm
Floating point constants.
Definition: Expr.h:234
Halide::Internal::Broadcast
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:241
Halide::Internal::IntImm
Integer constants.
Definition: Expr.h:216
Target.h
Halide::Internal::Scope< int >
Halide::Internal::Cast
The actual IR nodes begin here.
Definition: IR.h:29
Halide::Internal::LE
Is the first expression less than or equal to the second.
Definition: IR.h:130
Halide::Internal::CodeGen_OpenGL_Dev::api_unique_name
std::string api_unique_name() override
This routine returns the GPU API name that is combined into runtime routine names to ensure each GPU ...
Definition: CodeGen_OpenGL_Dev.h:37
Halide::Internal::NE
Is the first expression not equal to the second.
Definition: IR.h:112
Halide::Internal::CodeGen_OpenGL_Dev
Definition: CodeGen_OpenGL_Dev.h:20
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:409
Halide::Internal::CodeGen_C
This class emits C++ code equivalent to a halide Stmt.
Definition: CodeGen_C.h:24
Halide::Internal::CodeGen_GLSL::visit
void visit(const Let *) override
Halide::Type
Types in the halide type system.
Definition: Type.h:269
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition: IR.h:199
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition: IR.h:388
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::Internal::EQ
Is the first expression equal to the second.
Definition: IR.h:103
Halide::Internal::CodeGen_C::AppendSpaceIfNeeded
AppendSpaceIfNeeded
Definition: CodeGen_C.h:89
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::Max
The greater of two values.
Definition: IR.h:94
Halide::Internal::CodeGen_GPU_Dev
A code generator that emits GPU code from a given Halide stmt.
Definition: CodeGen_GPU_Dev.h:17
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition: IR.h:253
Halide::Internal::Ramp
A linear ramp vector node.
Definition: IR.h:229
CodeGen_GPU_Dev.h
Halide::Internal::CodeGen_OpenGL_Dev::CodeGen_OpenGL_Dev
CodeGen_OpenGL_Dev(const Target &target)
Halide::Internal::Evaluate
Evaluate and discard an expression, presumably because it has some side-effect.
Definition: IR.h:450
Halide::Internal::CodeGen_OpenGL_Dev::get_current_kernel_name
std::string get_current_kernel_name() override
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:315
Halide::Internal::Min
The lesser of two values.
Definition: IR.h:85
Halide::Internal::Mod
The remainder of a / b.
Definition: IR.h:76
Halide::Internal::AssertStmt
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition: IR.h:276
Halide::Internal::CodeGen_GLSL
Compile one statement into GLSL.
Definition: CodeGen_OpenGL_Dev.h:94
Halide::Internal::CodeGen_GLSL::CodeGen_GLSL
CodeGen_GLSL(std::ostream &s, const Target &t)
Halide::Internal::Call
A function call.
Definition: IR.h:464
Halide::Internal::CodeGen_GLSLBase
This class handles GLSL arithmetic, shared by CodeGen_GLSL and CodeGen_OpenGLCompute_C.
Definition: CodeGen_OpenGL_Dev.h:55
Halide::Internal::CodeGen_OpenGL_Dev::init_module
void init_module() override
(Re)initialize the GPU kernel module.
Halide::Internal::CodeGen_GLSLBase::CodeGen_GLSLBase
CodeGen_GLSLBase(std::ostream &s, Target t)
Halide::Internal::Select
A ternary operator.
Definition: IR.h:186
Halide::Output::stmt
@ stmt
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:256
Halide::Internal::GT
Is the first expression greater than the second.
Definition: IR.h:139
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition: IR.h:809
Halide::Internal::CodeGen_GLSL::add_kernel
void add_kernel(const Stmt &stmt, const std::string &name, const std::vector< DeviceArgument > &args)
Halide::Internal::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:729
Halide::Internal::UIntImm
Unsigned integer constants.
Definition: Expr.h:225
Halide::Internal::CodeGen_OpenGL_Dev::add_kernel
void add_kernel(Stmt stmt, const std::string &name, const std::vector< DeviceArgument > &args) override
Compile a GPU kernel into the module.
Halide::Internal::CodeGen_GLSLBase::print_type
std::string print_type(Type type, AppendSpaceIfNeeded space_option=DoNotAppendSpace) override
Emit the C name for a halide type.
Halide::Internal::CodeGen_C::DoNotAppendSpace
@ DoNotAppendSpace
Definition: CodeGen_C.h:90
Halide::Internal::CodeGen_OpenGL_Dev::compile_to_src
std::vector< char > compile_to_src() override
Halide::Target
A struct representing a target machine and os to generate code for.
Definition: Target.h:19
Halide::Internal::CodeGen_OpenGL_Dev::dump
void dump() override
Halide::Internal::LT
Is the first expression less than the second.
Definition: IR.h:121
Halide::Internal::CodeGen_GLSLBase::builtin
std::map< std::string, std::string > builtin
Definition: CodeGen_OpenGL_Dev.h:90