HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CE_Context.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: CE_Context.h ( CE Library, C++)
7  *
8  * COMMENTS: Compute Engine Contexts.
9  */
10 
11 #ifndef __CE_Context__
12 #define __CE_Context__
13 
14 #include "CE_API.h"
15 
16 #ifdef CE_ENABLED
17 
18 #include <SYS/SYS_Types.h>
19 
20 #include <UT/UT_ValArray.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_Map.h>
23 #include <UT/UT_Error.h>
24 
25 class CE_MemoryPool;
26 
27 typedef void (*CE_ErrorCB)(const char *errmsg, UT_ErrorSeverity severity,
28  void *data);
29 
31 {
32 public:
35 
36  virtual void rebindOGLBuffer( uint buf_obj ) = 0;
37  virtual void unbindOGLBuffer() = 0;
38  virtual bool isBinded() = 0;
39 };
40 
41 /// CE_Context encapsulates the OpenCL context and provides various convenience
42 /// functions for loading kernel programs and allocating GPU memory.
44 {
45 public:
46  CE_Context();
47  virtual ~CE_Context();
48 
49  /// Returns a pointer to the singleton CE_Context object. This function
50  /// initializes attempts to initialize OpenCL if it has not yet been.
51  static CE_Context *getContext(bool gl_shared=false);
52 
53  /// Returns the underlying cl::Context object.
54  cl::Context getCLContext() const {return myContext;}
55 
56  /// Returns the cl::Queue object that is used to enqueue OpenCL kernels
57  /// and memory transfers.
58  cl::CommandQueue getQueue() const {return myQueue;}
59 
60  /// Returns the OpenCL Device object.
61  cl::Device getDevice() const {return myDevice;}
62 
63  // Write OpenCL Device info to the supplied buffer.
65  void getExtendedInfo(UT_WorkBuffer &buffer );
66 
67  // Write info for all available OpenCL platforms to the supplied buffer.
68  static void getAllPlatformsInfo(UT_WorkBuffer &buffer);
69 
70  /// Get the suggested global and local ranges for the given 1-D kernel over
71  /// the specified number of items.
72  void get1DRanges(const cl::Kernel &k, size_t items,
74 
75  /// Loads the OpenCL program specified by progname. This functions searches
76  /// for the file in the HOUDINI_OCL_PATH environment variable. Any compile-
77  /// time options can be passed in the options parameter. If the program
78  /// load succeeds, the progname will be cached, using the progrname and
79  /// options strings together as a hash value lookup. In this way the same
80  /// OpenCL program can be loaded several times with different compile-time
81  /// flags.
82  cl::Program loadProgram(const char *progname, const char *options = NULL,
83  bool recompile = false);
84  cl::Program compileProgram(const char *progtext, const char *options = NULL,
85  bool recompile = false);
86 
87  /// Create an OpenCL kernel named kernelname from the program specified by
88  /// progname. For some types of devices these kernels will be cached, as
89  /// kernels can be expensive to create. This is the recommended method
90  /// for creating kernels.
91  cl::Kernel loadKernel(const cl::Program &prog, const UT_StringRef &kernelname);
92  cl::Kernel loadKernel(const char *progname, const UT_StringRef &kernelname,
93  const char *options = NULL)
94  { return loadKernel(loadProgram(progname, options), kernelname); }
95 
96  /// Returns whether the CE_Context has been successfully initialized.
97  bool isValid() const {return myIsValid;}
98 
99  /// Returns whether the singleton CE_Context has been initialized yet. This
100  /// can be used to test whether OpenCL has been initialized without calling
101  /// getContext and forcing an attempt at initialization.
102  static bool isInitialized(bool gl_shared=false);
103 
104  /// Returns true if the OpenCL device is running on the CPU.
105  bool isCPU() const;
106 
107  /// Returns true if the OpenCL device supports double precision.
108  bool hasDoubleSupport() const {return mySupportsDouble;}
109 
110  /// Block until any outstanding kernel or memory transfers on the main
111  /// CommandQueue have executed. If sweepPool is true, the context's
112  /// CE_MemoryPool will sweep for any buffers that were in use when their
113  /// CE_Grid's went out of scope, but that were still active in kernels.
114  void finish(bool sweepPool=true);
115 
116  /// Allocate a buffer of specified size on the CE_Device.
117  /// usePool= true, attempts to use the underlying CE_MemoryPool to possibly return
118  /// an already allocated, unused buffer.
119  /// read=true, creates a buffer that is readable inside kernels.
120  /// write=true, creates a buffer that is writable inside kernels.
121  /// ogl_bind, specifies an OGL buffer to bind to.
122  cl::Buffer allocBuffer(int64 size, bool usePool=true, bool read=true, bool write=true, uint32 ogl_bind=SYS_UINT32_MAX);
123 
124  /// Release the specified buffer, possibly to the CE_MemoryPool.
125  void releaseBuffer(const cl::Buffer &buf);
126 
127  /// Keep a map buffer to bind at render time
128  /// The first time a CL::Buffer is created it can be registered to rebing to a OGL vertex buffer at drawing time.
129  /// The uint returned by the register call can be attached to a detail attribute and the drawing code can convert
130  /// the CL Buffer to a CL BufferGL.
131  uint32 registerDelayedOGLBindBuffer(CE_DelayedOGLBindBuffer* buffer);
132  void unregisterDelayedOGLBindBuffer(uint32 id);
133  CE_DelayedOGLBindBuffer* lookupDelayedOGLBindBuffer( uint id );
134 
135  /// Clear the CE_MemoryPool object.
136  void clearMemoryPool();
137 
138  /// Return a pointer to pinned (page-locked) host memory. On some devices
139  /// (Nvidia), using this type of memory for the PCI/E host/device transfers
140  /// can double the throughput. Will return NULL if the memory can't be
141  /// allocated, or if the device is not a GPU.
142  fpreal32 *getPinnedBuffer(int64 size);
143 
144  cl::Buffer getXNoiseData();
145 
146  /// Standard error reporting for OpenCL exceptions. They should generally
147  /// take the form:
148  /// @code
149  /// try
150  /// {
151  /// OpenCL calls...
152  /// }
153  /// catch(cl::Error &err)
154  /// {
155  /// CE_Context::reportError(err);
156  /// ///cleanup
157  /// }
158  /// @endcode
159  /// This will not capture delayed errors, however. Instead
160  /// you will need to add a callback to intercept them.
161  static void reportError(const cl::Error &err);
162  static void outputErrorMessage(const char *errMsg);
163  static void setErrorCB(CE_ErrorCB callback, void *data);
164  static void outputWarningMessage(const char *errMsg);
165 
166  static void initMainSharedGLContext( int devicetype, void* context, void* display );
167  static bool useHalfNormalDelayedBindBuffer();
168 
169  /// Marks that an operation has run out of memory, allowing us
170  /// to report elsewhere.
171  void setOutOfMemoryFailure(bool hasfailed = true) { myOutOfMemoryFailure = true; }
172  bool hasOutOfMemoryFailureHappened() const { return myOutOfMemoryFailure; }
173 
174 protected:
175  cl::Program *doCompileProgram(const char *progtext, const char *options);
176 
177  /// Initialize the context for the given device.
178  void init(cl::Context &context, cl::Device &device);
179 
180  /// Releases the pinned, page-locked memory buffer.
181  void releasePinnedBuffer();
182 
183 
187  bool myIsValid;
190 
191  struct KernelInfo
192  {
195  };
196 
199 
201 
204 
206 
208 
209  static void* theGLContext;
210  static void* theGLDisplay;
211  static int theGLDeviceType;
212 };
213 
214 
215 #endif
216 #endif
#define CE_API
Definition: CE_API.h:10
bool myIsValid
Definition: CE_Context.h:187
cl::Device getDevice() const
Returns the OpenCL Device object.
Definition: CE_Context.h:61
Unsorted map container.
Definition: UT_Map.h:83
bool myOutOfMemoryFailure
Definition: CE_Context.h:205
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLboolean GLboolean g
Definition: glcorearb.h:1221
cl::Kernel loadKernel(const char *progname, const UT_StringRef &kernelname, const char *options=NULL)
Definition: CE_Context.h:92
UT_ErrorSeverity
Definition: UT_Error.h:25
virtual ~CE_DelayedOGLBindBuffer()
Definition: CE_Context.h:34
void read(T &in, bool &v)
Definition: ImfXdr.h:611
GLuint buffer
Definition: glcorearb.h:659
GLsizeiptr size
Definition: glcorearb.h:663
cl::CommandQueue getQueue() const
Definition: CE_Context.h:58
fpreal32 * myPinnedData
Definition: CE_Context.h:203
long long int64
Definition: SYS_Types.h:106
cl::CommandQueue myQueue
Definition: CE_Context.h:185
bool isValid() const
Returns whether the CE_Context has been successfully initialized.
Definition: CE_Context.h:97
UT_Map< uint32, CE_DelayedOGLBindBuffer * > myDelayedOGLBindBuffers
Definition: CE_Context.h:207
CE_MemoryPool * myMemPool
Definition: CE_Context.h:200
cl::Device myDevice
Definition: CE_Context.h:186
static void * theGLDisplay
Definition: CE_Context.h:210
void setOutOfMemoryFailure(bool hasfailed=true)
Definition: CE_Context.h:171
GLenum GLenum severity
Definition: glcorearb.h:2538
GLboolean * data
Definition: glcorearb.h:130
#define SYS_UINT32_MAX
Definition: SYS_Types.h:162
unsigned int uint
Definition: SYS_Types.h:39
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
cl::Context myContext
Definition: CE_Context.h:184
CommandQueue interface for cl_command_queue.
Definition: cl.hpp:2840
static void * theGLContext
Definition: CE_Context.h:209
cl_int getInfo(Func f, cl_uint name, T *param)
Definition: cl.hpp:1028
bool mySupportsDouble
Definition: CE_Context.h:188
Memory buffer interface.
Definition: cl.hpp:1863
cl::Buffer myXNoiseData
Definition: CE_Context.h:189
NDRange interface.
Definition: cl.hpp:2456
UT_StringHolder name
Definition: CE_Context.h:193
void(* CE_ErrorCB)(const char *errmsg, UT_ErrorSeverity severity, void *data)
Definition: CE_Context.h:27
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2534
cl::Context getCLContext() const
Returns the underlying cl::Context object.
Definition: CE_Context.h:54
static int theGLDeviceType
Definition: CE_Context.h:211
UT_Map< const _cl_program *, UT_Array< KernelInfo > * > myKernelTable
Definition: CE_Context.h:198
Device interface for cl_device_id.
Definition: cl.hpp:1261
bool hasDoubleSupport() const
Returns true if the OpenCL device supports double precision.
Definition: CE_Context.h:108
void write(T &out, bool v)
Definition: ImfXdr.h:332
Program interface that implements cl_program.
Definition: cl.hpp:2639
bool hasOutOfMemoryFailureHappened() const
Definition: CE_Context.h:172
float fpreal32
Definition: SYS_Types.h:190
cl::Kernel * kernel
Definition: CE_Context.h:194
cl::Buffer myPinnedBuffer
Definition: CE_Context.h:202
UT_StringMap< cl::Program * > myProgramTable
Definition: CE_Context.h:197
unsigned int uint32
Definition: SYS_Types.h:35