HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VEX_Example.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  * This is a sample VEX operator DSO
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <math.h>
34 #include <iostream>
35 #include <UT/UT_DSOVersion.h>
36 #include <UT/UT_Thread.h>
37 #include <VEX/VEX_VexOp.h>
38 
39 using namespace UT::Literal;
40 
41 namespace HDK_Sample {
42 
43 #if !defined(WIN32)
44 template <VEX_Precision PREC>
45 static void
46 drand_Evaluate(int, void *argv[], void *)
47 {
48  VEXfloat<PREC> *result = (VEXfloat<PREC> *)argv[0];
49  const VEXint<PREC> *seed = (const VEXint<PREC> *)argv[1];
50 
51  SYSsrand48(*seed);
52  *result = SYSdrand48();
53 }
54 #endif
55 
56 template <VEX_Precision PREC>
57 static void
58 time_Evaluate(int, void *argv[], void *)
59 {
60  VEXint<PREC> *result = (VEXint<PREC> *)argv[0];
61 
62  *result = time(0);
63 }
64 
65 // Simple class to show shared storage. A single gamma table is shared between
66 // all instances of the gamma() function.
67 template <VEX_Precision PREC>
69 {
70 public:
71  gamma_Table() : myRefCount(1) { }
73 
75 
77 
79 };
80 
81 template <VEX_Precision PREC> gamma_Table<PREC>*
83 
84 template <VEX_Precision PREC> static void *
85 gamma_Init()
86 {
87 
90  else
93 }
94 
95 template <VEX_Precision PREC> static void
96 gamma_Cleanup(void *data)
97 {
98  gamma_Table<PREC> *table = (gamma_Table<PREC> *)data;
99 
101  table->myRefCount--;
102  if (!table->myRefCount)
103  {
104  delete table;
105  gamma_Table<PREC>::instance = nullptr;
106  }
107 }
108 
109 template <VEX_Precision PREC>
110 static void
111 gamma_Evaluate(int, void *argv[], void *data)
112 {
113  VEXfloat<PREC> *result = (VEXfloat<PREC> *)argv[0];
114  const VEXfloat<PREC> *value = (const VEXfloat<PREC> *)argv[1];
115 
116  gamma_Table<VEX_32> *table = (gamma_Table<VEX_32> *)data;
117  *result = table->evaluate(*value);
118 }
119 
120 template <VEX_Precision PREC>
121 static void
122 myprint_Evaluate(int argc, VEX_VexOpArg argv[], void *data)
123 {
124  printf("%d args:\n", argc);
125  for (int i = 0; i < argc; i++)
126  {
127  if (argv[i].myArray)
128  continue; // Doesn't support arrays
129  switch (argv[i].myType)
130  {
131  case VEX_TYPE_INTEGER:
132  std::cout << " int " << *(const VEXint<PREC> *)argv[i].myArg << '\n';
133  break;
134  case VEX_TYPE_FLOAT:
135  std::cout << " float " << *(const VEXfloat<PREC> *)argv[i].myArg << '\n';
136  break;
137  case VEX_TYPE_STRING:
138  std::cout << " string " << *(const char *)argv[i].myArg << '\n';
139  break;
140  default:
141  break;
142  }
143  }
144 }
145 
146 }
147 
148 //
149 // Installation function
150 //
151 using namespace HDK_Sample;
152 void
153 newVEXOp(void *)
154 {
155 #if !defined(WIN32)
156  // Returns a random number based on the seed argument
157  new VEX_VexOp("drand@&FI"_sh, // Signature
158  drand_Evaluate<VEX_32>, // Evaluator 32
159  drand_Evaluate<VEX_64>, // Evaluator 64
160  VEX_ALL_CONTEXT, // Context mask
161  nullptr,nullptr, // init function 32,64
162  nullptr,nullptr); // cleanup function 32,64
163 #endif
164 
165  // Return the time() function. This is non-deterministic, so the
166  // optimization level has to be lowered.
167  new VEX_VexOp("time@&I"_sh, // Signature
168  time_Evaluate<VEX_32>, // Evaluator 32
169  time_Evaluate<VEX_64>, // Evaluator 64
170  VEX_ALL_CONTEXT, // Context mask
171  nullptr,nullptr, // init function 32, 64
172  nullptr,nullptr, // cleanup function 32, 64
173  VEX_OPTIMIZE_1); // Optimization level
174 
175  // Use the default optimization (better performance)
176  new VEX_VexOp("gamma@&FF"_sh, // Signature
177  gamma_Evaluate<VEX_32>, // Evaluator 32
178  gamma_Evaluate<VEX_64>, // Evaluator 64
179  VEX_ALL_CONTEXT, // Context mask
180  gamma_Init<VEX_32>, // init function 32
181  gamma_Init<VEX_64>, // init function 64
182  gamma_Cleanup<VEX_32>, // Cleanup function 32
183  gamma_Cleanup<VEX_64>); // Cleanup function 64
184 
185  // A variadic function to print integers and floats
186  new VEX_VexOp("myprint@+"_sh, // Signature
187  myprint_Evaluate<VEX_32>, // Evaluator 32
188  myprint_Evaluate<VEX_64>, // Evaluator 64
189  VEX_ALL_CONTEXT, // Context mask
190  nullptr,nullptr, // init function 32,64
191  nullptr,nullptr, // Cleanup function 32,64
192  VEX_OPTIMIZE_0); // Optimization level
193 }
void newVEXOp(void *)
Definition: VEX_Example.C:153
GT_API const UT_StringHolder time
const GLdouble * v
Definition: glcorearb.h:836
SYS_API void SYSsrand48(long seed)
Use this class to extend VEX by adding custom VEX functions.
Definition: VEX_VexOp.h:144
png_uint_32 i
Definition: png.h:2877
#define VEX_OPTIMIZE_0
Definition: VEX_VexOp.h:120
typename VEX_PrecisionResolver< P >::float_type VEXfloat
Definition: VEX_PodTypes.h:67
SYS_API double SYSdrand48()
GLboolean * data
Definition: glcorearb.h:130
#define VEX_OPTIMIZE_1
Definition: VEX_VexOp.h:121
static gamma_Table< PREC > * instance
Definition: VEX_Example.C:78
GLsizei const GLfloat * value
Definition: glcorearb.h:823
typename VEX_PrecisionResolver< P >::int_type VEXint
Definition: VEX_PodTypes.h:68
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
VEXfloat< PREC > evaluate(VEXfloat< PREC > v)
Definition: VEX_Example.C:74
#define VEX_ALL_CONTEXT
Definition: VEX_VexTypes.h:93