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) 2024
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 <UT/UT_DoubleLock.h>
38 #include <VEX/VEX_VexOp.h>
39 
40 using namespace UT::Literal;
41 
42 namespace HDK_Sample {
43 
44 #if !defined(WIN32)
45 template <VEX_Precision PREC>
46 static void
47 drand_Evaluate(int, void *argv[], void *)
48 {
49  VEXfloat<PREC> *result = (VEXfloat<PREC> *)argv[0];
50  const VEXint<PREC> *seed = (const VEXint<PREC> *)argv[1];
51 
52  SYSsrand48(*seed);
53  *result = SYSdrand48();
54 }
55 #endif
56 
57 template <VEX_Precision PREC>
58 static void
59 time_Evaluate(int, void *argv[], void *)
60 {
61  VEXint<PREC> *result = (VEXint<PREC> *)argv[0];
62 
63  *result = time(0);
64 }
65 
66 // Simple class to show shared storage. A single gamma table is shared between
67 // all instances of the gamma() function.
68 template <VEX_Precision PREC>
70 {
71 public:
73  {
74  }
76  {
77  }
78 
80 };
81 
82 template <VEX_Precision PREC> static void *
83 gamma_Init()
84 {
85  // Note that multiple threads may call init simultaneously, so this method
86  // should be thread-safe. If there's complicated initialization required,
87  // you should likely lock around the initialization.
88  // The method below uses C++11 static initialization.
89  static gamma_Table<PREC> theTable;
90  return &theTable;
91 }
92 
93 template <VEX_Precision PREC> static void
94 gamma_Cleanup(void *data)
95 {
96  // Nothing required here.
97 }
98 
99 template <VEX_Precision PREC>
100 static void
101 gamma_Evaluate(int, void *argv[], void *data)
102 {
103  VEXfloat<PREC> *result = (VEXfloat<PREC> *)argv[0];
104  const VEXfloat<PREC> *value = (const VEXfloat<PREC> *)argv[1];
105 
107  *result = table->evaluate(*value);
108 }
109 
110 template <VEX_Precision PREC>
111 static void
112 myprint_Evaluate(int argc, VEX_VexOpArg argv[], void *data)
113 {
114  printf("%d args:\n", argc);
115  for (int i = 0; i < argc; i++)
116  {
117  if (argv[i].myArray)
118  continue; // Doesn't support arrays
119  switch (argv[i].myType)
120  {
121  case VEX_TYPE_INTEGER:
122  std::cout << " int " << *(const VEXint<PREC> *)argv[i].myArg << '\n';
123  break;
124  case VEX_TYPE_FLOAT:
125  std::cout << " float " << *(const VEXfloat<PREC> *)argv[i].myArg << '\n';
126  break;
127  case VEX_TYPE_STRING:
128  std::cout << " string " << *(const char *)argv[i].myArg << '\n';
129  break;
130  default:
131  break;
132  }
133  }
134 }
135 
136 }
137 
138 //
139 // Installation function
140 //
141 using namespace HDK_Sample;
142 void
143 newVEXOp(void *)
144 {
145 #if !defined(WIN32)
146  // Returns a random number based on the seed argument
147  new VEX_VexOp("drand@&FI"_sh, // Signature
148  drand_Evaluate<VEX_32>, // Evaluator 32
149  drand_Evaluate<VEX_64>, // Evaluator 64
150  VEX_ALL_CONTEXT, // Context mask
151  nullptr,nullptr, // init function 32,64
152  nullptr,nullptr); // cleanup function 32,64
153 #endif
154 
155  // Return the time() function. This is non-deterministic, so the
156  // optimization level has to be lowered.
157  new VEX_VexOp("time@&I"_sh, // Signature
158  time_Evaluate<VEX_32>, // Evaluator 32
159  time_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  VEX_OPTIMIZE_1); // Optimization level
164 
165  // Use the default optimization (better performance)
166  new VEX_VexOp("gamma@&FF"_sh, // Signature
167  gamma_Evaluate<VEX_32>, // Evaluator 32
168  gamma_Evaluate<VEX_64>, // Evaluator 64
169  VEX_ALL_CONTEXT, // Context mask
170  gamma_Init<VEX_32>, // init function 32
171  gamma_Init<VEX_64>, // init function 64
172  gamma_Cleanup<VEX_32>, // Cleanup function 32
173  gamma_Cleanup<VEX_64>); // Cleanup function 64
174 
175  // A variadic function to print integers and floats
176  new VEX_VexOp("myprint@+"_sh, // Signature
177  myprint_Evaluate<VEX_32>, // Evaluator 32
178  myprint_Evaluate<VEX_64>, // Evaluator 64
179  VEX_ALL_CONTEXT, // Context mask
180  nullptr,nullptr, // init function 32,64
181  nullptr,nullptr, // Cleanup function 32,64
182  VEX_OPTIMIZE_0); // Optimization level
183 }
void newVEXOp(void *)
Definition: VEX_Example.C:143
GT_API const UT_StringHolder time
const GLdouble * v
Definition: glcorearb.h:837
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
SYS_API void SYSsrand48(long seed)
Use this class to extend VEX by adding custom VEX functions.
Definition: VEX_VexOp.h:150
**But if you need a result
Definition: thread.h:613
#define VEX_OPTIMIZE_0
Definition: VEX_VexOp.h:126
typename VEX_PrecisionResolver< P >::float_type VEXfloat
Definition: VEX_PodTypes.h:67
SYS_API double SYSdrand48()
#define VEX_OPTIMIZE_1
Definition: VEX_VexOp.h:127
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
typename VEX_PrecisionResolver< P >::int_type VEXint
Definition: VEX_PodTypes.h:68
Definition: core.h:1131
VEXfloat< PREC > evaluate(VEXfloat< PREC > v)
Definition: VEX_Example.C:79
#define VEX_ALL_CONTEXT
Definition: VEX_VexTypes.h:91
Definition: format.h:895