Ptex
PtexTriangleKernel.cpp
Go to the documentation of this file.
1 /*
2 PTEX SOFTWARE
3 Copyright 2009 Disney Enterprises, Inc. All rights reserved
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in
14  the documentation and/or other materials provided with the
15  distribution.
16 
17  * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
18  Studios" or the names of its contributors may NOT be used to
19  endorse or promote products derived from this software without
20  specific prior written permission from Walt Disney Pictures.
21 
22 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
25 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
26 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 */
35 
36 #include "PtexPlatform.h"
37 #include "PtexUtils.h"
38 #include "PtexHalf.h"
39 #include "PtexTriangleKernel.h"
40 
41 
42 namespace {
43  inline float gaussian(float x_squared)
44  {
45  static const float scale = -0.5f * (PtexTriangleKernelWidth * PtexTriangleKernelWidth);
46  return (float)exp(scale * x_squared);
47  }
48 }
49 
50 
51 namespace {
52 
53  // apply to 1..4 channels (unrolled channel loop) of packed data (nTxChan==nChan)
54  // the ellipse equation, Q, is calculated via finite differences (Heckbert '89 pg 57)
55  template<class T, int nChan>
56  void Apply(PtexTriangleKernelIter& k, float* result, void* data, int /*nChan*/, int /*nTxChan*/)
57  {
58  int nTxChan = nChan;
59  float DDQ = 2.0f*k.A;
60  for (int vi = k.v1; vi != k.v2; vi++) {
61  int xw = k.rowlen - vi;
62  int x1 = PtexUtils::max(k.u1, xw-k.w2);
63  int x2 = PtexUtils::min(k.u2, xw-k.w1);
64  float U = (float)x1 - k.u;
65  float V = (float)vi - k.v;
66  float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
67  float Q = k.A*U*U + (k.B*U + k.C*V)*V;
68  T* p = (T*)data + (vi * k.rowlen + x1) * nTxChan;
69  T* pEnd = p + (x2-x1)*nTxChan;
70  for (; p < pEnd; p += nTxChan) {
71  if (Q < 1.0f) {
72  float weight = gaussian(Q)*k.wscale;
73  k.weight += weight;
74  PtexUtils::VecAccum<T,nChan>()(result, p, weight);
75  }
76  Q += DQ;
77  DQ += DDQ;
78  }
79  }
80  }
81 
82  // apply to 1..4 channels (unrolled channel loop) w/ pixel stride
83  template<class T, int nChan>
84  void ApplyS(PtexTriangleKernelIter& k, float* result, void* data, int /*nChan*/, int nTxChan)
85  {
86  float DDQ = 2.0f*k.A;
87  for (int vi = k.v1; vi != k.v2; vi++) {
88  int xw = k.rowlen - vi;
89  int x1 = PtexUtils::max(k.u1, xw-k.w2);
90  int x2 = PtexUtils::min(k.u2, xw-k.w1);
91  float U = (float)x1 - k.u;
92  float V = (float)vi - k.v;
93  float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
94  float Q = k.A*U*U + (k.B*U + k.C*V)*V;
95  T* p = (T*)data + (vi * k.rowlen + x1) * nTxChan;
96  T* pEnd = p + (x2-x1)*nTxChan;
97  for (; p < pEnd; p += nTxChan) {
98  if (Q < 1.0f) {
99  float weight = gaussian(Q)*k.wscale;
100  k.weight += weight;
101  PtexUtils::VecAccum<T,nChan>()(result, p, weight);
102  }
103  Q += DQ;
104  DQ += DDQ;
105  }
106  }
107  }
108 
109  // apply to N channels (general case)
110  template<class T>
111  void ApplyN(PtexTriangleKernelIter& k, float* result, void* data, int nChan, int nTxChan)
112  {
113  float DDQ = 2.0f*k.A;
114  for (int vi = k.v1; vi != k.v2; vi++) {
115  int xw = k.rowlen - vi;
116  int x1 = PtexUtils::max(k.u1, xw-k.w2);
117  int x2 = PtexUtils::min(k.u2, xw-k.w1);
118  float U = (float)x1 - k.u;
119  float V = (float)vi - k.v;
120  float DQ = k.A*(2.0f*U+1.0f)+k.B*V;
121  float Q = k.A*U*U + (k.B*U + k.C*V)*V;
122  T* p = (T*)data + (vi * k.rowlen + x1) * nTxChan;
123  T* pEnd = p + (x2-x1)*nTxChan;
124  for (; p < pEnd; p += nTxChan) {
125  if (Q < 1.0f) {
126  float weight = gaussian(Q)*k.wscale;
127  k.weight += weight;
128  PtexUtils::VecAccumN<T>()(result, p, nChan, weight);
129  }
130  Q += DQ;
131  DQ += DDQ;
132  }
133  }
134  }
135 }
136 
137 
140  // nChan == nTxChan
141  ApplyN<uint8_t>, ApplyN<uint16_t>, ApplyN<PtexHalf>, ApplyN<float>,
142  Apply<uint8_t,1>, Apply<uint16_t,1>, Apply<PtexHalf,1>, Apply<float,1>,
143  Apply<uint8_t,2>, Apply<uint16_t,2>, Apply<PtexHalf,2>, Apply<float,2>,
144  Apply<uint8_t,3>, Apply<uint16_t,3>, Apply<PtexHalf,3>, Apply<float,3>,
145  Apply<uint8_t,4>, Apply<uint16_t,4>, Apply<PtexHalf,4>, Apply<float,4>,
146 
147  // nChan != nTxChan (need pixel stride)
148  ApplyN<uint8_t>, ApplyN<uint16_t>, ApplyN<PtexHalf>, ApplyN<float>,
149  ApplyS<uint8_t,1>, ApplyS<uint16_t,1>, ApplyS<PtexHalf,1>, ApplyS<float,1>,
150  ApplyS<uint8_t,2>, ApplyS<uint16_t,2>, ApplyS<PtexHalf,2>, ApplyS<float,2>,
151  ApplyS<uint8_t,3>, ApplyS<uint16_t,3>, ApplyS<PtexHalf,3>, ApplyS<float,3>,
152  ApplyS<uint8_t,4>, ApplyS<uint16_t,4>, ApplyS<PtexHalf,4>, ApplyS<float,4>,
153 };
154 
155 
156 void PtexTriangleKernelIter::applyConst(float* dst, void* data, DataType dt, int nChan)
157 {
158  // iterate over texel locations and calculate weight as if texture weren't const
159  float DDQ = 2.0f*A;
160  for (int vi = v1; vi != v2; vi++) {
161  int xw = rowlen - vi;
162  int x1 = PtexUtils::max(u1, xw-w2);
163  int x2 = PtexUtils::min(u2, xw-w1);
164  float U = (float)x1 - u;
165  float V = (float)vi - v;
166  float DQ = A*(2.0f*U+1.0f)+B*V;
167  float Q = A*U*U + (B*U + C*V)*V;
168  for (int x = x1; x < x2; x++) {
169  if (Q < 1.0f) {
170  weight += gaussian(Q)*wscale;
171  }
172  Q += DQ;
173  DQ += DDQ;
174  }
175  }
176 
177  // apply weight to single texel value
178  PtexUtils::applyConst(weight, dst, data, dt, nChan);
179 }
void applyConst(float *dst, void *data, DataType dt, int nChan)
DataType
Type of data stored in texture file.
Definition: Ptexture.h:83
static T min(T a, T b)
Definition: PtexUtils.h:116
Triangle filter kernel iterator (in texel coords)
Platform-specific classes, functions, and includes.
static ApplyFn applyFunctions[40]
void(* ApplyFn)(PtexTriangleKernelIter &k, float *dst, void *data, int nChan, int nTxChan)
static const float PtexTriangleKernelWidth
Half-precision floating-point type.
static void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition: PtexUtils.h:204
static T max(T a, T b)
Definition: PtexUtils.h:119