HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ComputeGPU.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: UT_ComputeGPU.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * A high level abstract interface to allow GP-GPU computing (general purpose
10  * computation on the GPU, or video card).
11  *
12  * Setting up the computation is relatively expensive, so this should only
13  * be used for large volumes of data.
14  *
15  * Once you have set up the program, output and input(s), you can change one
16  * or all of them for the next run. If you call setConstant() or setArray()
17  * with the same name as an existing constant or array, the data will be
18  * updated as long as the data format and size remains the same (otherwise
19  * another constant or texture will be created for it).
20  *
21  * You can use special macros to read from the input arrays (which are
22  * textures) using integer indices rather than floating point texture
23  * coords:
24  *
25  * IN_array_name // at current output pos.
26  * ABS_array_name (int_pos) // at an absolute position.
27  * REL_array_name (int_offset) // offset from current pos.
28  *
29  * (Note: 2D Arrays will have 2 pos / offset parms)
30  *
31  * You can use the first version to grab the corresponding input element
32  * only if it matches the output in size and dimensions (1d/2d). Otherwise,
33  * you'll get array bounds issues.
34  */
35 #ifndef UT_ComputeGPU_h
36 #define UT_ComputeGPU_h
37 
38 #include "UT_API.h"
39 #include "UT_NonCopyable.h"
40 
41 // Data type abtractions for OpenGL types.
43 {
44  UT_GPU_UINT1 = 0, // corresponds to GL_STENCIL_INDEX1
45  UT_GPU_UINT4, // corresponds to GL_STENCIL_INDEX4
49 
53 
55  UT_GPU_FLOAT24, // corresponds to GL_DEPTH_COMPONENT24
58 
59  // These can only be used in the context of a GLSL shader.
63 };
64 
65 class UT_String;
66 class UT_ComputeGPU;
67 
68 typedef UT_ComputeGPU *(*UT_ComputeGPUCreateFunc)();
69 
71 {
72 public:
73  virtual ~UT_ComputeGPU();
74 
76 
78  {
79  OGL_GLSL, // OpenGL GLSL (must have OpenGL 2.0 or shader exts)
80  };
81 
82  /// Create a new UT_ComputeGPU object.
83  /// The caller takes ownership of the returned object and is responsible
84  /// for deleting it.
85  static UT_ComputeGPU *create();
86 
87  /// Register a function for creating concrete UT_ComputeGPU objects.
88  static void registerCreateFunc(UT_ComputeGPUCreateFunc create_func);
89 
90  // 0. If you're doing multiple passes, you should set how many passes you
91  // wish to do. This is optional - it defaults to 1 pass.
92  // TODO: This is not supported yet.
93  virtual void setNumPasses(int passes) = 0;
94 
95  // If enabled, your shader is using integer bit operations. Not all cards
96  // support this.
97  virtual void needBitOps(bool enable) = 0;
98  virtual bool needsBitOps() const = 0;
99 
100  // 1. Inputs to computation.
101  // You may rewrite to existing values to begin another computation.
102  // This class does not take ownership of the value array you pass in -
103  // you are responsible for deleting it, but do not do so until compute()
104  // has been called.
105  // The OpenGL texture size limits the maximum array size. 1D Arrays
106  // have a max of this size squared, 2D arrays must have both dimensions
107  // under this size (see RE_OGLRender::getMaxTextureSize()). setArray()
108  // will return NULL if these limits are exceeeded.
109 
110  // Data types available for I/O
111  // - constants are usually int32's, float32's or matrices.
112  // - input and output arrays cannot be arrays of matrices (currently)
113 
114  // Constants are usually UT_GPU_FLOAT32, UT_GPU_(U)INT32 OR
115  // UT_GPU_MATRIX[n]. They can be a small array, but GLSL will not allow
116  // you to dynamically index them (ie, [x], only [1] or using fixed range
117  // loop variables like x=1 to 10). If you need to dynamically index,
118  // use an Array. Constant arrays are very lightweight, though.
119  virtual void setConstant(const char *name,
121  int vectorsize,
122  const void *value,
123  int array_size = 1,
124  bool global_var = false) = 0;
125 
126  virtual void * setArray(const char *name,
128  int vectorsize,
129  int size,
130  const void *values) = 0;
131 
132  virtual void * setArray2D(const char *name,
134  int vectorsize,
135  int width,
136  int height,
137  const void *values) = 0; // in rows.
138 
139  // flat arrays to vectors (interleaves to 1,2,3,4, 1,2,3,4). Not all
140  // components need to be specified.
141  virtual void * setArray(const char *name,
143  int vectorsize,
144  int size,
145  const void *values1,
146  const void *values2,
147  const void *values3 = 0,
148  const void *values4 = 0) = 0;
149 
150  virtual void * setArray2D(const char *name,
152  int vectorsize,
153  int width,
154  int height,
155  const void *values1,
156  const void *values2,
157  const void *values3 = 0,
158  const void *values4 = 0) = 0;
159 
160  // 2. Output data.
161  // You can add multiple outputs, but they all must be the same size, type
162  // and vectorsize. 'dest_data' is where the results are written.
163 
164  virtual void setOutputArray(const char *name,
166  int vectorsize,
167  int size,
168  void *dest_data) = 0;
169 
170  virtual void setOutputArray2D(const char *name,
172  int vectorsize,
173  int width,
174  int height,
175  void *dest_data) = 0;
176 
177  // Convienience method to determine the size of the array you will need
178  // for an output buffer (or need to provide for an input buffer).
179  // 1D arrays only need to pass xsize.
180  virtual int getDataByteSize(UT_GPUType type, int vectorsize,
181  int xsize, int ysize = 1) = 0;
182 
183  // 3. GLSL/Cg Program (false if it fails to compile)
184  // 'name' is arbitrary, for debug messages.
185  // 'program' is the actual program code.
186  // If 'auto_generate_framework' is true, this class prepends the GLSL
187  // constant and texture declarations (as named above), then
188  // 'void main(void) {', your code, and then '}' (so all you need to do is
189  // fill in the actual code). If non-NULL, 'preamble_code' will be placed
190  // before the declarations.
191 
192  virtual void setLanguage(UT_OGLComputeLanguage lang) = 0;
193 
194  virtual bool setProgram(const char *name,
195  const char *program,
196  bool auto_generate_framework = true,
197  const char *preamble_code = 0,
198  const char *function_code = 0) = 0;
199 
200  // This allows you to define macros and symbols for preprocessing.
201  virtual void defineSymbol(const char *symbol,
202  const char *value) = 0;
203 
204  // 4. Computation
205  // Runs the program on the inputs to produce the output. Returns a
206  // pointer to the result (in the output format and dimensions specified
207  // above). Returns false if an opengl error occured.
208  // Once you have called compute, you may begin another computation
209  // before fetching the results (just go back to step 1).
210 
211  // 'async_read' starts the DMA transfer of the results after the
212  // computation is finshed. Useful when you have more than one output,
213  // or more work to do between compute() and getResult(). Note
214  // that this call will block until the computation is done, so for long
215  // computations, you may want to take the latency hit. If pixel buffer
216  // objects are not supported, this is ignored.
217 
218  virtual bool compute(UT_String &errors, bool async_read = false) = 0;
219 
220  // 5. Fetch Results
221  // Notify the engine that you'd like to use your results. With multiple
222  // active computations, this removes the oldest and another getResult()
223  // will begin fetching from the next oldest computation.
224  //
225  // Your results will be available _after_ this method is called. This
226  // returns false if your results could not be read.
227  virtual bool getResults() = 0;
228 
229  // Alternatively, you can use a deferred scheme to retrive the data.
230  // 1. Call deferred fetch to grab a key to the results.
231  // 2. You can optionally call beginDeferredRead() some time afterward
232  // to start the asynchronouse read.
233  // 3. Call endDeferredRead() to ensure that all results have been read.
234  // The key is no longer valid after step 3.
235 
236  virtual void * useDeferredFetch() = 0;
237 
238 protected:
239  UT_ComputeGPU();
240 };
241 
242 /// Class for manipulating global state needed for GPU computation.
243 /// This is a singleton class. You can access the singleton by calling
244 /// the static get() method.
246 {
247 public:
248  virtual ~UT_ComputeGPUState();
249 
251 
252  static UT_ComputeGPUState *get();
253  static void set(UT_ComputeGPUState *gpu_state);
254 
255  virtual void initStandalone(bool one_per_thread = true) {}
256  virtual void cleanupStandalone() {}
257  virtual void getGLSLComputeDir(UT_String &dir) {}
258 
259  // Returns true if the graphics hardware has proper support to use this
260  // class.
261  virtual bool hasGPUSupport() { return false; }
262 
263  virtual bool areBitOpsSupported() { return false; }
264 
265  virtual void beginDeferredRead(void *key) {}
266  virtual bool endDeferredRead(void *key) { return false; }
267  virtual void cancelDeferredRead(void *key) {}
268 
269  // Returns true if texture rectangles must be used (because standard 2D
270  // textures do not support most higher formats on some hardware)
271  virtual bool useTextureRectangles() { return false; }
272 
273  // The largest sizes that this GPU can handle.
274  virtual int getMaxArraySize() { return 0; }
275  virtual int getMaxArraySize2D() { return 0; }
276 
277  // Clears all the GP-GPU allocated objects. You don't normally need to
278  // call this.
279  virtual void reset() {}
280 
281  virtual void restoreState() {}
282  virtual void cleanupState() {}
283 
284 protected:
286 };
287 
288 #endif
virtual bool hasGPUSupport()
virtual int getMaxArraySize()
virtual bool areBitOpsSupported()
virtual void reset()
virtual int getMaxArraySize2D()
#define UT_API
Definition: UT_API.h:14
virtual void cleanupState()
virtual void getGLSLComputeDir(UT_String &dir)
virtual void initStandalone(bool one_per_thread=true)
UT_GPUType
Definition: UT_ComputeGPU.h:42
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
virtual void cancelDeferredRead(void *key)
virtual void restoreState()
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual void cleanupStandalone()
virtual bool endDeferredRead(void *key)
GLsizeiptr size
Definition: glcorearb.h:664
virtual void beginDeferredRead(void *key)
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
GLint GLsizei width
Definition: glcorearb.h:103
Definition: core.h:1131
UT_ComputeGPU *(* UT_ComputeGPUCreateFunc)()
Definition: UT_ComputeGPU.h:68
type
Definition: core.h:1059
virtual bool useTextureRectangles()
GLbitfield GLuint program
Definition: glcorearb.h:1931