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