HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 <UT/UT_DSOVersion.h>
35 #include <UT/UT_Thread.h>
36 #include <VEX/VEX_VexOp.h>
37 
38 using namespace UT::Literal;
39 
40 namespace HDK_Sample {
41 
42 #if !defined(WIN32)
43 template <VEX_Precision PREC>
44 static void
45 drand_Evaluate(int, void *argv[], void *)
46 {
47  VEXfloat<PREC> *result = (VEXfloat<PREC> *)argv[0];
48  const VEXint<PREC> *seed = (const VEXint<PREC> *)argv[1];
49 
50  SYSsrand48(*seed);
51  *result = SYSdrand48();
52 }
53 #endif
54 
55 template <VEX_Precision PREC>
56 static void
57 time_Evaluate(int, void *argv[], void *)
58 {
59  VEXint<PREC> *result = (VEXint<PREC> *)argv[0];
60 
61  *result = time(0);
62 }
63 
64 // Simple class to show shared storage. A single gamma table is shared between
65 // all instances of the gamma() function.
66 template <VEX_Precision PREC>
68 {
69 public:
70  gamma_Table() : myRefCount(1) { }
72 
74 
76 };
77 
78 static gamma_Table<VEX_32> *theGammaTable = nullptr;
79 
80 static void *
81 gamma_Init()
82 {
83 
84  if (!theGammaTable)
85  theGammaTable = new gamma_Table<VEX_32>();
86  else
87  theGammaTable->myRefCount++;
88  return theGammaTable;
89 }
90 
91 static void
92 gamma_Cleanup(void *data)
93 {
95 
96  UT_ASSERT(table == theGammaTable);
97  table->myRefCount--;
98  if (!table->myRefCount)
99  {
100  delete table;
101  theGammaTable = nullptr;
102  }
103 }
104 
105 template <VEX_Precision PREC>
106 static void
107 gamma_Evaluate(int, void *argv[], void *data)
108 {
109  VEXfloat<PREC> *result = (VEXfloat<PREC> *)argv[0];
110  const VEXfloat<PREC> *value = (const VEXfloat<PREC> *)argv[1];
111 
112  gamma_Table<VEX_32> *table = (gamma_Table<VEX_32> *)data;
113  *result = table->evaluate(*value);
114 }
115 
116 template <VEX_Precision PREC>
117 static void
118 myprint_Evaluate(int argc, VEX_VexOpArg argv[], void *data)
119 {
120  printf("%d args:\n", argc);
121  for (int i = 0; i < argc; i++)
122  {
123  if (argv[i].myArray)
124  continue; // Doesn't support arrays
125  switch (argv[i].myType)
126  {
127  case VEX_TYPE_INTEGER:
128  printf(" int %d\n", *(const VEXint<PREC> *)argv[i].myArg);
129  break;
130  case VEX_TYPE_FLOAT:
131  printf(" float %f\n", *(const VEXfloat<PREC> *)argv[i].myArg);
132  break;
133  case VEX_TYPE_STRING:
134  printf(" string %s\n", (const char *)argv[i].myArg);
135  break;
136  default:
137  break;
138  }
139  }
140 }
141 
142 }
143 
144 //
145 // Installation function
146 //
147 using namespace HDK_Sample;
148 void
149 newVEXOp(void *)
150 {
151 #if !defined(WIN32)
152  // Returns a random number based on the seed argument
153  new VEX_VexOp("drand@&FI"_sh, // Signature
154  drand_Evaluate<VEX_32>, // Evaluator
155  VEX_ALL_CONTEXT, // Context mask
156  nullptr, // init function
157  nullptr); // cleanup function
158 #endif
159 
160  // Return the time() function. This is non-deterministic, so the
161  // optimization level has to be lowered.
162  new VEX_VexOp("time@&I"_sh, // Signature
163  time_Evaluate<VEX_32>, // Evaluator
164  VEX_ALL_CONTEXT, // Context mask
165  nullptr, // init function
166  nullptr, // cleanup function
167  VEX_OPTIMIZE_1); // Optimization level
168 
169  // Use the default optimization (better performance)
170  new VEX_VexOp("gamma@&FF"_sh, // Signature
171  gamma_Evaluate<VEX_32>, // Evaluator
172  VEX_ALL_CONTEXT, // Context mask
173  gamma_Init, // init function
174  gamma_Cleanup); // Cleanup function
175 
176  // A variadic function to print integers and floats
177  new VEX_VexOp("myprint@+"_sh, // Signature
178  myprint_Evaluate<VEX_32>, // Evaluator
179  VEX_ALL_CONTEXT, // Context mask
180  nullptr, // init function
181  nullptr, // Cleanup function
182  VEX_OPTIMIZE_0); // Optimization level
183 }
void newVEXOp(void *)
Definition: VEX_Example.C:149
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
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
typename VEX_PrecisionResolver< P >::float_type VEXfloat
Definition: VEX_PodTypes.h:64
SYS_API double SYSdrand48()
GLboolean * data
Definition: glcorearb.h:130
#define VEX_OPTIMIZE_1
Definition: VEX_VexOp.h:121
GLsizei const GLfloat * value
Definition: glcorearb.h:823
typename VEX_PrecisionResolver< P >::int_type VEXint
Definition: VEX_PodTypes.h:65
VEXfloat< PREC > evaluate(VEXfloat< PREC > v)
Definition: VEX_Example.C:73
#define VEX_ALL_CONTEXT
Definition: VEX_VexTypes.h:93