HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RU_PixelFunction.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: RU_PixelFunctions.h
7  *
8  * COMMENTS:
9  * Defines the base class for simple pixel functions (ie, functions that
10  * depend only on the value of the pixel being operated on; bright, gamma, etc,
11  * not blurs or transforms)
12  */
13 
14 #ifndef RU_PixelFunction_h
15 #define RU_PixelFunction_h
16 
17 #include "RU_API.h"
18 #include <UT/UT_ComputeGPU.h>
19 #include <UT/UT_ValArray.h>
20 #include <UT/UT_Lock.h>
21 #include <UT/UT_String.h>
22 #include <UT/UT_StringArray.h>
23 
24 class RU_PixelFunction;
25 
27 typedef void (*RUVectorFunc)(RU_PixelFunction *, float **, const bool *);
28 
30 {
31 public:
32  RU_PixelFunction() : myNextFunc(0), myBuildLock(0L)
33  { myScope[0] = myScope[1] = myScope[2] = myScope[3] = true; }
34  virtual ~RU_PixelFunction();
35 
36  void setScope(bool *scope);
37  bool isScoped(int i) const { return myScope[i]; }
38  const bool *getScope() const { return myScope; }
39  bool isPartialScope() const;
40 
41  // returns true if different components have different function outcomes.
42  bool isComponentDependent() const;
43 
44  // returns true if all components must be computed at once.
45  bool areAllComponentsNeeded() const;
46 
47  void buildPixelEngine();
48  void buildVectorEngine();
49  void buildShaderEngine(UT_ComputeGPU *gpgpu);
50 
51  // this function runs the entire linked list of functions on val. Normally
52  // you will want to call this from the head of the list.
53  inline float processValue(float val, int comp) const
54  {
55  for(int i= 0; i<myFunctions.entries(); i++)
56  if(myParms(i)->isScoped(comp))
57  val = myFunctions(i)( myParms(i), val, comp );
58  return val;
59  }
60 
61  inline void processValues(float **vals) const
62  {
63  for(int i= 0; i<myVectorFunctions.entries(); i++)
64  {
65  myVectorFunctions(i)( myParms(i), vals,myParms(i)->getScope());
66  }
67  }
68 
69  const char *getShaderCode() const { return myShaderCode; }
70  const char *getFunctionCode() const { return myFunctionCode; }
71  const char *getLocals() const { return myLocalVars; }
72  bool usesNumCompsGlobal() const { return myUseNumComps;}
73 
74  void setNext(RU_PixelFunction *next) { myNextFunc = next; }
75  RU_PixelFunction *next() const { return myNextFunc; }
76 
77  void setNumComps(int comps) { myNumComps = comps; }
78 
79 protected:
80  static float defaultPixelFunction(RU_PixelFunction *func,
81  float val, int comp);
82  static void defaultVectorFunction(RU_PixelFunction *func,
83  float **vals,const bool *scope);
84 
85  // most functions can be done piece-wise. HSV correct, ie, cannot - it
86  // needs a chunk of 3.
87  virtual bool needAllComponents() const { return false; }
88 
89  // each component different?
90  virtual bool eachComponentDifferent() const { return false; }
91 
92  // Override this to return the function to modify the actual pixel.
93  virtual RUPixelFunc getPixelFunction() const;
94 
95  // Override this to return the function to modify the pixel instead of
96  // getPixelFunction if needAllComponents() returns true.
97  virtual RUVectorFunc getVectorFunction() const;
98 
99  virtual void getPixelShader(UT_String &frag_shader);
100 
101  void addConstant(const char *name,
102  UT_GPUType type, int vectorsize,
103  const void *data,
104  int array_size = 1);
105  void addLocal(const char *name,
107  int vectorsize);
108  void addArray(const char *name,
110  int vectorsize,
111  int width,
112  int height,
113  const void *data);
114  void addArray(const char *name,
116  int vectorsize,
117  int width,
118  int height,
119  const void *data1,
120  const void *data2,
121  const void *data3,
122  const void *data4);
123 
124  // function_name should be a unique name to the shader; append or prepend
125  // the pixel function name to it. 'code' still needs to include this name
126  // in the declaration.
127  void addFunction(const char *function_name,
128  const char *code);
129 
130  // call if you need to access a global variable. Variables are:
131  // num_comps - number of components being processed (1-4)
132  void usesGlobal(const char *name);
133 
134  int getFunctionIndex() const
135  { return myHead->myFunctionIndex; }
136  int getNumComps() const
137  { return myHead->myNumComps; }
138 
139 private:
140  RU_PixelFunction *myNextFunc;
141 
142  UT_Lock myBuildLock;
143  UT_ValArray<RUPixelFunc> myFunctions;
144  UT_ValArray<RUVectorFunc> myVectorFunctions;
146 
147  mutable RUPixelFunc myVectorizedFunction;
148  bool myScope[4];
149 
150  // For GPU pixel shaders
151  RU_PixelFunction *myHead;
152  UT_String myShaderCode, myFunctionCode;
153  UT_String myLocalVars;
154  UT_String myParmCall;
155  UT_String myParmDecl;
156  UT_String myLocals;
157  UT_StringArray myUserFunctions;
158  UT_String myUserFunctionCode;
159  UT_ComputeGPU *myGPU;
160  int myFunctionIndex;
161  int myNumComps;
162  bool myUseNumComps;
163 
164  friend class RU_ColorCurve;
165 };
166 
167 #endif
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
float processValue(float val, int comp) const
const char * getFunctionCode() const
virtual bool eachComponentDifferent() const
void
Definition: png.h:1083
#define RU_API
Definition: RU_API.h:10
virtual bool needAllComponents() const
int getFunctionIndex() const
UT_GPUType
Definition: UT_ComputeGPU.h:42
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
IMATH_NAMESPACE::V2f float
void processValues(float **vals) const
const bool * getScope() const
GLuint const GLchar * name
Definition: glcorearb.h:786
const char * getLocals() const
GLenum func
Definition: glcorearb.h:783
GLuint GLfloat * val
Definition: glcorearb.h:1608
bool isScoped(int i) const
int getNumComps() const
const char * getShaderCode() const
GLint GLsizei width
Definition: glcorearb.h:103
void setNext(RU_PixelFunction *next)
bool usesNumCompsGlobal() const
void(* RUVectorFunc)(RU_PixelFunction *, float **, const bool *)
void setNumComps(int comps)
type
Definition: core.h:1059
float(* RUPixelFunc)(RU_PixelFunction *, float, int)
Definition: format.h:895
RU_PixelFunction * next() const