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 <algorithm>
40#include <cmath>
41#include <cstring>
42#include "PtexExports.h"
43#include "Ptexture.h"
44#include "PtexHalf.h"
45
46#ifdef __SSE4_1__
47#include <smmintrin.h>
48#endif
49
50#include "PtexVersion.h"
51
52 PTEX_NAMESPACE_BEGIN
53 namespace PtexUtils {
54
55// (keeping these as aliases for code outside of the Ptex library that might have been using them)
56 using std::abs;
57 using std::min;
58 using std::max;
59 using std::clamp;
60 using std::floor;
61 using std::ceil;
62
63 inline bool isPowerOfTwo(int x)
64{
65 return !(x&(x-1));
66}
67
68 inline uint32_t ones(uint32_t x)
69{
70 // count number of ones
71 x = (x & 0x55555555) + ((x >> 1) & 0x55555555); // add pairs of bits
72 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); // add bit pairs
73 x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f); // add nybbles
74 x += (x >> 8); // add bytes
75 x += (x >> 16); // add words
76 return(x & 0xff);
77}
78
79 inline uint32_t floor_log2(uint32_t x)
80{
81 // floor(log2(n))
82 x |= (x >> 1);
83 x |= (x >> 2);
84 x |= (x >> 4);
85 x |= (x >> 8);
86 x |= (x >> 16);
87 return ones(x>>1);
88}
89
90 inline uint32_t ceil_log2(uint32_t x)
91{
92 // ceil(log2(n))
93 bool isPow2 = isPowerOfTwo(x);
94 x |= (x >> 1);
95 x |= (x >> 2);
96 x |= (x >> 4);
97 x |= (x >> 8);
98 x |= (x >> 16);
99 return ones(x>>1) + !isPow2;
100}
101
102 inline float reciprocalPow2(int power)
103{
104 // 1.0/pow(2,power)
105 int32_t i = (127-power)<<23;
106 float f;
107 memcpy(&f, &i, sizeof(f));
108 return f;
109}
110
111 inline int calcResFromWidth(float w)
112{
113 // read exponent directly from float32 representation
114 // equiv to ceil(log2(1.0/w)) but much faster and no error
115 int32_t wi;
116 memcpy(&wi, &w, sizeof(wi));
117 int result = 127 - ((wi >> 23) & 0xff);
118 return result;
119}
120
121 inline float smoothstep(float x, float a, float b)
122{
123 if ( x < a ) return 0;
124 if ( x >= b ) return 1;
125 x = (x - a)/(b - a);
126 return x*x * (3 - 2*x);
127}
128
129 inline float qsmoothstep(float x, float a, float b)
130{
131 // quintic smoothstep (cubic is only C1)
132 if ( x < a ) return 0;
133 if ( x >= b ) return 1;
134 x = (x - a)/(b - a);
135 return x*x*x * (10 + x * (-15 + x*6));
136}
137
138 template<typename T>
139 inline T halve(T val) { return T(val>>1); }
140
141 inline float halve(float val) { return 0.5f * val; }
142 inline PtexHalf halve(PtexHalf val) { return 0.5f * val; }
143
144 template<typename T>
145 inline T quarter(T val) { return T(val>>2); }
146
147 inline float quarter(float val) { return 0.25f * val; }
148 inline PtexHalf quarter(PtexHalf val) { return 0.25f * val; }
149
150 PTEXAPI
151 bool isConstant(const void* data, int stride, int ures, int vres, int pixelSize);
152
153 PTEXAPI
154 void interleave(const void* src, int sstride, int ures, int vres,
155 void* dst, int dstride, DataType dt, int nchannels);
156
157 PTEXAPI
158 void deinterleave(const void* src, int sstride, int ures, int vres,
159 void* dst, int dstride, DataType dt, int nchannels);
160
161 PTEXAPI
162 void encodeDifference(void* data, size_t size, DataType dt);
163
164 PTEXAPI
165 void decodeDifference(void* data, size_t size, DataType dt);
166
167 typedef void ReduceFn(const void* src, int sstride, int ures, int vres,
168 void* dst, int dstride, DataType dt, int nchannels);
169
170 PTEXAPI
171 void reduce(const void* src, int sstride, int ures, int vres,
172 void* dst, int dstride, DataType dt, int nchannels);
173
174 PTEXAPI
175 void reduceu(const void* src, int sstride, int ures, int vres,
176 void* dst, int dstride, DataType dt, int nchannels);
177
178 PTEXAPI
179 void reducev(const void* src, int sstride, int ures, int vres,
180 void* dst, int dstride, DataType dt, int nchannels);
181
182 PTEXAPI
183 void reduceTri(const void* src, int sstride, int ures, int vres,
184 void* dst, int dstride, DataType dt, int nchannels);
185
186 PTEXAPI
187 void average(const void* src, int sstride, int ures, int vres,
188 void* dst, DataType dt, int nchannels);
189
190 PTEXAPI
191 void fill(const void* src, void* dst, int dstride,
192 int ures, int vres, int pixelsize);
193
194 PTEXAPI
195 void copy(const void* src, int sstride, void* dst, int dstride,
196 int nrows, int rowlen);
197
198 PTEXAPI
199 void blend(const void* src, float weight, void* dst, bool flip,
200 int rowlen, DataType dt, int nchannels);
201
202 PTEXAPI
203 void multalpha(void* data, int npixels, DataType dt, int nchannels, int alphachan);
204
205 PTEXAPI
206 void divalpha(void* data, int npixels, DataType dt, int nchannels, int alphachan);
207
208
209 PTEXAPI
210 void genRfaceids(const FaceInfo* faces, int nfaces,
211 uint32_t* rfaceids, uint32_t* faceids);
212
213// fixed length vector accumulator: dst[i] += val[i] * weight
214 template<typename T, int n>
215 struct VecAccum {
216 VecAccum() {}
217 void operator()(float* dst, const T* val, float weight)
218 {
219 *dst += (float)*val * weight;
220 // use template to unroll loop
221 VecAccum<T,n-1>()(dst+1, val+1, weight);
222 }
223};
224 template<typename T>
225 struct VecAccum<T,0> { void operator()(float*, const T*, float) {} };
226
227// variable length vector accumulator: dst[i] += val[i] * weight
228 template<typename T>
229 struct VecAccumN {
230 void operator()(float* dst, const T* val, int nchan, float weight)
231 {
232 for (int i = 0; i < nchan; i++) dst[i] += (float)val[i] * weight;
233 }
234};
235
236// fixed length vector multiplier: dst[i] += val[i] * weight
237 template<typename T, int n>
238 struct VecMult {
239 VecMult() {}
240 void operator()(float* dst, const T* val, float weight)
241 {
242 *dst = (float)*val * weight;
243 // use template to unroll loop
244 VecMult<T,n-1>()(dst+1, val+1, weight);
245 }
246};
247 template<typename T>
248 struct VecMult<T,0> { void operator()(float*, const T*, float) {} };
249
250// variable length vector multiplier: dst[i] = val[i] * weight
251 template<typename T>
252 struct VecMultN {
253 void operator()(float* dst, const T* val, int nchan, float weight)
254 {
255 for (int i = 0; i < nchan; i++) dst[i] = (float)val[i] * weight;
256 }
257};
258
259 typedef void (*ApplyConstFn)(float weight, float* dst, void* data, int nChan);
260 extern ApplyConstFn applyConstFunctions[20];
261 inline void applyConst(float weight, float* dst, void* data, Ptex::DataType dt, int nChan)
262{
263 // dispatch specialized apply function
264 ApplyConstFn fn = applyConstFunctions[((unsigned)nChan<=4)*nChan*4 + dt];
265 fn(weight, dst, data, nChan);
266}
267
268} // end namespace Utils
269
270 PTEX_NAMESPACE_END
271
272#endif
PtexExports.h
Definitions related to exported Ptex API symbol visibility.
PTEXAPI
#define PTEXAPI
Definition PtexExports.h:60
PtexHalf.h
Half-precision floating-point type.
PtexVersion.h
PTEX_NAMESPACE_END
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
Ptexture.h
Public API classes for reading, writing, caching, and filtering Ptex files.
PTEX_NAMESPACE_BEGIN
Definition PtexSeparableKernel.cpp:42
PtexUtils
Definition PtexUtils.cpp:145
PtexUtils::reciprocalPow2
float reciprocalPow2(int power)
Definition PtexUtils.h:102
PtexUtils::decodeDifference
void decodeDifference(void *data, size_t size, DataType dt)
Definition PtexUtils.cpp:271
PtexUtils::genRfaceids
void genRfaceids(const FaceInfo *faces, int nfaces, uint32_t *rfaceids, uint32_t *faceids)
Definition PtexUtils.cpp:630
PtexUtils::reduceu
void reduceu(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition PtexUtils.cpp:333
PtexUtils::applyConst
void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition PtexUtils.h:261
PtexUtils::ones
uint32_t ones(uint32_t x)
Definition PtexUtils.h:68
PtexUtils::isConstant
bool isConstant(const void *data, int stride, int ures, int vres, int pixelSize)
Definition PtexUtils.cpp:147
PtexUtils::divalpha
void divalpha(void *data, int npixels, DataType dt, int nchannels, int alphachan)
Definition PtexUtils.cpp:618
PtexUtils::reduce
void reduce(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition PtexUtils.cpp:299
PtexUtils::deinterleave
void deinterleave(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition PtexUtils.cpp:226
PtexUtils::quarter
T quarter(T val)
Definition PtexUtils.h:145
PtexUtils::blend
void blend(const void *src, float weight, void *dst, bool flip, int rowlen, DataType dt, int nchan)
Definition PtexUtils.cpp:480
PtexUtils::calcResFromWidth
int calcResFromWidth(float w)
Definition PtexUtils.h:111
PtexUtils::encodeDifference
void encodeDifference(void *data, size_t size, DataType dt)
Definition PtexUtils.cpp:251
PtexUtils::reducev
void reducev(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition PtexUtils.cpp:366
PtexUtils::fill
void fill(const void *src, void *dst, int dstride, int ures, int vres, int pixelsize)
Definition PtexUtils.cpp:422
PtexUtils::reduceTri
void reduceTri(const void *src, int sstride, int w, int, void *dst, int dstride, DataType dt, int nchan)
Definition PtexUtils.cpp:406
PtexUtils::halve
T halve(T val)
Definition PtexUtils.h:139
PtexUtils::copy
void copy(const void *src, int sstride, void *dst, int dstride, int vres, int rowlen)
Definition PtexUtils.cpp:438
PtexUtils::smoothstep
float smoothstep(float x, float a, float b)
Definition PtexUtils.h:121
PtexUtils::floor_log2
uint32_t floor_log2(uint32_t x)
Definition PtexUtils.h:79
PtexUtils::ReduceFn
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition PtexUtils.h:167
PtexUtils::multalpha
void multalpha(void *data, int npixels, DataType dt, int nchannels, int alphachan)
Definition PtexUtils.cpp:579
PtexUtils::interleave
void interleave(const void *src, int sstride, int uw, int vw, void *dst, int dstride, DataType dt, int nchan)
Definition PtexUtils.cpp:189
PtexUtils::qsmoothstep
float qsmoothstep(float x, float a, float b)
Definition PtexUtils.h:129
PtexUtils::ApplyConstFn
void(* ApplyConstFn)(float weight, float *dst, void *data, int nChan)
Definition PtexUtils.h:259
PtexUtils::isPowerOfTwo
bool isPowerOfTwo(int x)
Definition PtexUtils.h:63
PtexUtils::average
void average(const void *src, int sstride, int uw, int vw, void *dst, DataType dt, int nchan)
Definition PtexUtils.cpp:522
PtexUtils::applyConstFunctions
ApplyConstFn applyConstFunctions[20]
Definition PtexUtils.cpp:668
PtexUtils::ceil_log2
uint32_t ceil_log2(uint32_t x)
Definition PtexUtils.h:90
Ptex::DataType
DataType
Type of data stored in texture file.
Definition Ptexture.h:72
PtexHalf
Half-precision (16-bit) floating-point type.
Definition PtexHalf.h:72
PtexUtils::VecAccumN
Definition PtexUtils.h:229
PtexUtils::VecAccumN::operator()
void operator()(float *dst, const T *val, int nchan, float weight)
Definition PtexUtils.h:230
PtexUtils::VecAccum< T, 0 >::operator()
void operator()(float *, const T *, float)
Definition PtexUtils.h:225
PtexUtils::VecAccum::operator()
void operator()(float *dst, const T *val, float weight)
Definition PtexUtils.h:217
PtexUtils::VecAccum::VecAccum
VecAccum()
Definition PtexUtils.h:216
PtexUtils::VecMultN
Definition PtexUtils.h:252
PtexUtils::VecMultN::operator()
void operator()(float *dst, const T *val, int nchan, float weight)
Definition PtexUtils.h:253
PtexUtils::VecMult< T, 0 >::operator()
void operator()(float *, const T *, float)
Definition PtexUtils.h:248
PtexUtils::VecMult::operator()
void operator()(float *dst, const T *val, float weight)
Definition PtexUtils.h:240
PtexUtils::VecMult::VecMult
VecMult()
Definition PtexUtils.h:239