Ptex
PtexUtils.h
Go to the documentation of this file.
1 #ifndef PtexUtils_h
2 #define PtexUtils_h
3 
4 /*
5 PTEX SOFTWARE
6 Copyright 2014 Disney Enterprises, Inc. All rights reserved
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11 
12  * Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14 
15  * Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in
17  the documentation and/or other materials provided with the
18  distribution.
19 
20  * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
21  Studios" or the names of its contributors may NOT be used to
22  endorse or promote products derived from this software without
23  specific prior written permission from Walt Disney Pictures.
24 
25 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
26 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
27 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
28 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
29 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37 */
38 
39 #include <cmath>
40 #include "PtexExports.h"
41 #include "Ptexture.h"
42 #include "PtexHalf.h"
43 
44 #ifdef __SSE4_1__
45 #include <smmintrin.h>
46 #endif
47 
48 #include "PtexVersion.h"
49 
51 namespace PtexUtils {
52 
53 inline bool isPowerOfTwo(int x)
54 {
55  return !(x&(x-1));
56 }
57 
58 inline uint32_t ones(uint32_t x)
59 {
60  // count number of ones
61  x = (x & 0x55555555) + ((x >> 1) & 0x55555555); // add pairs of bits
62  x = (x & 0x33333333) + ((x >> 2) & 0x33333333); // add bit pairs
63  x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f); // add nybbles
64  x += (x >> 8); // add bytes
65  x += (x >> 16); // add words
66  return(x & 0xff);
67 }
68 
69 inline uint32_t floor_log2(uint32_t x)
70 {
71  // floor(log2(n))
72  x |= (x >> 1);
73  x |= (x >> 2);
74  x |= (x >> 4);
75  x |= (x >> 8);
76  x |= (x >> 16);
77  return ones(x>>1);
78 }
79 
80 inline uint32_t ceil_log2(uint32_t x)
81 {
82  // ceil(log2(n))
83  bool isPow2 = isPowerOfTwo(x);
84  x |= (x >> 1);
85  x |= (x >> 2);
86  x |= (x >> 4);
87  x |= (x >> 8);
88  x |= (x >> 16);
89  return ones(x>>1) + !isPow2;
90 }
91 
92 inline float reciprocalPow2(int power)
93 {
94  // 1.0/pow(2,power)
95  union {
96  float f;
97  int32_t i;
98  };
99  i = (127-power)<<23;
100  return f;
101 }
102 
103 inline int calcResFromWidth(float w)
104 {
105  // read exponent directly from float32 representation
106  // equiv to ceil(log2(1.0/w)) but much faster and no error
107  union {
108  float wf;
109  int32_t wi;
110  };
111  wf = w;
112  int result = 127 - ((wi >> 23) & 0xff);
113  return result;
114 }
115 
116 inline float smoothstep(float x, float a, float b)
117 {
118  if ( x < a ) return 0;
119  if ( x >= b ) return 1;
120  x = (x - a)/(b - a);
121  return x*x * (3 - 2*x);
122 }
123 
124 inline float qsmoothstep(float x, float a, float b)
125 {
126  // quintic smoothstep (cubic is only C1)
127  if ( x < a ) return 0;
128  if ( x >= b ) return 1;
129  x = (x - a)/(b - a);
130  return x*x*x * (10 + x * (-15 + x*6));
131 }
132 
133 template<typename T>
134 inline T abs(T x) { return x > 0 ? x : -x; }
135 
136 inline float abs(float x)
137 {
138  union {
139  float f;
140  int32_t i;
141  };
142  f = x;
143  i &= 0x7fffffff;
144  return f;
145 }
146 
147 template<typename T>
148 inline T min(T a, T b) { return a < b ? a : b; }
149 
150 template<typename T>
151 inline T max(T a, T b) { return a > b ? a : b; }
152 
153 template<typename T>
154 inline T clamp(T x, T lo, T hi) { return min(max(x,lo),hi); }
155 
156 template<typename T>
157 inline T halve(T val) { return T(val>>1); }
158 
159 inline float halve(float val) { return 0.5f * val; }
160 inline PtexHalf halve(PtexHalf val) { return 0.5f * val; }
161 
162 template<typename T>
163 inline T quarter(T val) { return T(val>>2); }
164 
165 inline float quarter(float val) { return 0.25f * val; }
166 inline PtexHalf quarter(PtexHalf val) { return 0.25f * val; }
167 
168 PTEXAPI
169 bool isConstant(const void* data, int stride, int ures, int vres, int pixelSize);
170 
171 PTEXAPI
172 void interleave(const void* src, int sstride, int ures, int vres,
173  void* dst, int dstride, DataType dt, int nchannels);
174 
175 PTEXAPI
176 void deinterleave(const void* src, int sstride, int ures, int vres,
177  void* dst, int dstride, DataType dt, int nchannels);
178 
179 PTEXAPI
180 void encodeDifference(void* data, int size, DataType dt);
181 
182 PTEXAPI
183 void decodeDifference(void* data, int size, DataType dt);
184 
185 typedef void ReduceFn(const void* src, int sstride, int ures, int vres,
186  void* dst, int dstride, DataType dt, int nchannels);
187 
188 PTEXAPI
189 void reduce(const void* src, int sstride, int ures, int vres,
190  void* dst, int dstride, DataType dt, int nchannels);
191 
192 PTEXAPI
193 void reduceu(const void* src, int sstride, int ures, int vres,
194  void* dst, int dstride, DataType dt, int nchannels);
195 
196 PTEXAPI
197 void reducev(const void* src, int sstride, int ures, int vres,
198  void* dst, int dstride, DataType dt, int nchannels);
199 
200 PTEXAPI
201 void reduceTri(const void* src, int sstride, int ures, int vres,
202  void* dst, int dstride, DataType dt, int nchannels);
203 
204 PTEXAPI
205 void average(const void* src, int sstride, int ures, int vres,
206  void* dst, DataType dt, int nchannels);
207 
208 PTEXAPI
209 void fill(const void* src, void* dst, int dstride,
210  int ures, int vres, int pixelsize);
211 
212 PTEXAPI
213 void copy(const void* src, int sstride, void* dst, int dstride,
214  int nrows, int rowlen);
215 
216 PTEXAPI
217 void blend(const void* src, float weight, void* dst, bool flip,
218  int rowlen, DataType dt, int nchannels);
219 
220 PTEXAPI
221 void multalpha(void* data, int npixels, DataType dt, int nchannels, int alphachan);
222 
223 PTEXAPI
224 void divalpha(void* data, int npixels, DataType dt, int nchannels, int alphachan);
225 
226 
227 PTEXAPI
228 void genRfaceids(const FaceInfo* faces, int nfaces,
229  uint32_t* rfaceids, uint32_t* faceids);
230 
231 // fixed length vector accumulator: dst[i] += val[i] * weight
232 template<typename T, int n>
233 struct VecAccum {
234  VecAccum() {}
235  void operator()(float* dst, const T* val, float weight)
236  {
237  *dst += (float)*val * weight;
238  // use template to unroll loop
239  VecAccum<T,n-1>()(dst+1, val+1, weight);
240  }
241 };
242 template<typename T>
243 struct VecAccum<T,0> { void operator()(float*, const T*, float) {} };
244 
245 // variable length vector accumulator: dst[i] += val[i] * weight
246 template<typename T>
247 struct VecAccumN {
248  void operator()(float* dst, const T* val, int nchan, float weight)
249  {
250  for (int i = 0; i < nchan; i++) dst[i] += (float)val[i] * weight;
251  }
252 };
253 
254 // fixed length vector multiplier: dst[i] += val[i] * weight
255 template<typename T, int n>
256 struct VecMult {
257  VecMult() {}
258  void operator()(float* dst, const T* val, float weight)
259  {
260  *dst = (float)*val * weight;
261  // use template to unroll loop
262  VecMult<T,n-1>()(dst+1, val+1, weight);
263  }
264 };
265 template<typename T>
266 struct VecMult<T,0> { void operator()(float*, const T*, float) {} };
267 
268 // variable length vector multiplier: dst[i] = val[i] * weight
269 template<typename T>
270 struct VecMultN {
271  void operator()(float* dst, const T* val, int nchan, float weight)
272  {
273  for (int i = 0; i < nchan; i++) dst[i] = (float)val[i] * weight;
274  }
275 };
276 
277 typedef void (*ApplyConstFn)(float weight, float* dst, void* data, int nChan);
279 inline void applyConst(float weight, float* dst, void* data, Ptex::DataType dt, int nChan)
280 {
281  // dispatch specialized apply function
282  ApplyConstFn fn = applyConstFunctions[((unsigned)nChan<=4)*nChan*4 + dt];
283  fn(weight, dst, data, nChan);
284 }
285 
286 #ifdef __SSE4_1__
287 inline float floor(float f) {
288  float result;
289  _mm_store_ss(&result, _mm_round_ps(_mm_set1_ps(f), (_MM_FROUND_NO_EXC | _MM_FROUND_TO_NEG_INF)));
290  return result;
291 }
292 inline float ceil(float f) {
293  float result;
294  _mm_store_ss(&result, _mm_round_ps(_mm_set1_ps(f), (_MM_FROUND_NO_EXC | _MM_FROUND_TO_POS_INF)));
295  return result;
296 }
297 #else
298 using std::floor;
299 using std::ceil;
300 #endif
301 
302 } // end namespace Utils
303 
305 
306 #endif
Definitions related to exported Ptex API symbol visibility.
#define PTEXAPI
Definition: PtexExports.h:60
Half-precision floating-point type.
const FaceInfo * faces
Definition: PtexUtils.cpp:540
#define PTEX_NAMESPACE_END
Definition: PtexVersion.h:62
Public API classes for reading, writing, caching, and filtering Ptex files.
float reciprocalPow2(int power)
Definition: PtexUtils.h:92
T clamp(T x, T lo, T hi)
Definition: PtexUtils.h:154
void genRfaceids(const FaceInfo *faces, int nfaces, uint32_t *rfaceids, uint32_t *faceids)
Definition: PtexUtils.cpp:630
void reduceu(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition: PtexUtils.cpp:333
void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition: PtexUtils.h:279
uint32_t ones(uint32_t x)
Definition: PtexUtils.h:58
bool isConstant(const void *data, int stride, int ures, int vres, int pixelSize)
Definition: PtexUtils.cpp:147
T abs(T x)
Definition: PtexUtils.h:134
T min(T a, T b)
Definition: PtexUtils.h:148
void divalpha(void *data, int npixels, DataType dt, int nchannels, int alphachan)
Definition: PtexUtils.cpp:618
void encodeDifference(void *data, int size, DataType dt)
Definition: PtexUtils.cpp:251
void reduce(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition: PtexUtils.cpp:299
void deinterleave(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition: PtexUtils.cpp:226
T quarter(T val)
Definition: PtexUtils.h:163
void blend(const void *src, float weight, void *dst, bool flip, int rowlen, DataType dt, int nchan)
Definition: PtexUtils.cpp:480
int calcResFromWidth(float w)
Definition: PtexUtils.h:103
T max(T a, T b)
Definition: PtexUtils.h:151
void decodeDifference(void *data, int size, DataType dt)
Definition: PtexUtils.cpp:271
void reducev(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition: PtexUtils.cpp:366
void fill(const void *src, void *dst, int dstride, int ures, int vres, int pixelsize)
Definition: PtexUtils.cpp:422
void reduceTri(const void *src, int sstride, int w, int, void *dst, int dstride, DataType dt, int nchan)
Definition: PtexUtils.cpp:406
T halve(T val)
Definition: PtexUtils.h:157
void copy(const void *src, int sstride, void *dst, int dstride, int vres, int rowlen)
Definition: PtexUtils.cpp:438
float smoothstep(float x, float a, float b)
Definition: PtexUtils.h:116
uint32_t floor_log2(uint32_t x)
Definition: PtexUtils.h:69
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.h:185
void multalpha(void *data, int npixels, DataType dt, int nchannels, int alphachan)
Definition: PtexUtils.cpp:579
void interleave(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition: PtexUtils.cpp:189
float qsmoothstep(float x, float a, float b)
Definition: PtexUtils.h:124
void(* ApplyConstFn)(float weight, float *dst, void *data, int nChan)
Definition: PtexUtils.h:277
bool isPowerOfTwo(int x)
Definition: PtexUtils.h:53
void average(const void *src, int sstride, int uw, int vw, void *dst, DataType dt, int nchan)
Definition: PtexUtils.cpp:522
ApplyConstFn applyConstFunctions[20]
Definition: PtexUtils.cpp:668
uint32_t ceil_log2(uint32_t x)
Definition: PtexUtils.h:80
DataType
Type of data stored in texture file.
Definition: Ptexture.h:72
Half-precision (16-bit) floating-point type.
Definition: PtexHalf.h:72
void operator()(float *dst, const T *val, int nchan, float weight)
Definition: PtexUtils.h:248
void operator()(float *, const T *, float)
Definition: PtexUtils.h:243
void operator()(float *dst, const T *val, float weight)
Definition: PtexUtils.h:235
void operator()(float *dst, const T *val, int nchan, float weight)
Definition: PtexUtils.h:271
void operator()(float *, const T *, float)
Definition: PtexUtils.h:266
void operator()(float *dst, const T *val, float weight)
Definition: PtexUtils.h:258