RixShadingParam.h Source File

RixShadingParam.h
1 #ifndef RixShadingParam_h
2 #define RixShadingParam_h
3 /*
4 * $Date: 2015/09/01 $ $Revision: #3 $
5 # ------------------------------------------------------------------------------
6 #
7 # Copyright (c) 2014 Pixar Animation Studios. All rights reserved.
8 #
9 # The information in this file (the "Software") is provided for the
10 # exclusive use of the software licensees of Pixar. Licensees have
11 # the right to incorporate the Software into other products for use
12 # by other authorized software licensees of Pixar, without fee.
13 # Except as expressly permitted herein, the Software may not be
14 # disclosed to third parties, copied or duplicated in any form, in
15 # whole or in part, without the prior written permission of
16 # Pixar Animation Studios.
17 #
18 # The copyright notices in the Software and this entire statement,
19 # including the above license grant, this restriction and the
20 # following disclaimer, must be included in all copies of the
21 # Software, in whole or in part, and all permitted derivative works of
22 # the Software, unless such copies or derivative works are solely
23 # in the form of machine-executable object code generated by a
24 # source language processor.
25 #
26 # PIXAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
27 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
28 # SHALL PIXAR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
29 # OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
30 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
31 # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32 # SOFTWARE.
33 #
34 # Pixar
35 # 1200 Park Ave
36 # Emeryville CA 94608
37 #
38 # ------------------------------------------------------------------------------
39 */
40 #include "RixShading.h"
41 #include "RixPattern.h"
42 #include <cassert>
43 
64 // To-do --
65 // * smooth rough edges for detail promotion of RtFloat3 and friends.
66 // * fill-in SetValue implementations if called for.
67 // * transition PxrLM/lmParam.h to here.
68 // * add support for array parameters.
69 //
70 
75 
76 #ifndef RIX_SHADING_PARAM_MEMCATEGORY
77 #error RIX_SHADING_PARAM_MEMCATEGORY must be defined prior to inclusion
78 #endif
79 
81 {
82 public:
84  {
85  type = k_RixSCInvalidType;
86  detail = k_RixSCInvalidDetail;
87  data.f1 = NULL;
88  }
89 
91  union
92  {
93  RtInt *i1;
94  RtFloat *f1;
95  RtFloat3 *f3;
96  RtColorRGB *rgb;
97  } data;
98  RixSCDetail detail;
99  RixSCType type;
100 
101  bool IsValid() const { return detail != k_RixSCInvalidDetail; }
102 
106  void InitEvalF(RixShadingContext const *sctx, int pid, RtFloat *dflt,
107  bool promote=false)
108  {
109  type = k_RixSCFloat;
110  detail = sctx->EvalParam(pid, -1, (RtFloat const **) &data.f1, dflt,
111  promote);
113  if(promote)
114  detail = k_RixSCVarying;
115  }
116  void InitEvalV(RixShadingContext const *sctx, int pid, RtFloat3 *dflt,
117  bool promote=false)
118  {
119  type = k_RixSCFloat3;
120  detail = sctx->EvalParam(pid, -1, (RtFloat3 const **) &data.f3, dflt,
121  promote);
122  if(detail != k_RixSCVarying)
123  detail = k_RixSCInvalidDetail;
124  }
125  void InitEvalC(RixShadingContext const *sctx, int pid, RtColorRGB *dflt,
126  bool promote=false)
127  {
128  type = k_RixSCColor;
129  detail = sctx->EvalParam(pid, -1, (RtColorRGB const **)&data.rgb, dflt,
130  promote);
132  if(promote)
133  detail = k_RixSCVarying;
134  }
135  void InitEvalI(RixShadingContext const *sctx, int pid, RtInt *dflt,
136  bool promote=false)
137  {
138  type = k_RixSCInteger;
139  detail = sctx->EvalParam(pid, -1, (RtInt const **) &data.i1, dflt,
140  promote);
142  if(promote)
143  detail = k_RixSCVarying;
144  }
145 
146 
148  void InitEmptyF(RtFloat const *dflt=NULL)
149  {
150  type = k_RixSCFloat;
151  if(!dflt)
152  detail = k_RixSCInvalidDetail;
153  else
154  detail = k_RixSCUniform;
155  data.f1 = const_cast<RtFloat *>(dflt);
156  }
157  void InitEmptyI(RtInt const *dflt=NULL)
158  {
159  type = k_RixSCInteger;
160  if(!dflt)
161  detail = k_RixSCInvalidDetail;
162  else
163  detail = k_RixSCUniform;
164  data.i1 = const_cast<RtInt *>(dflt);
165  }
166  void InitEmptyV(RtFloat3 *dflt=NULL)
167  {
168  type = k_RixSCFloat3;
169  if(!dflt)
170  detail = k_RixSCInvalidDetail;
171  else
172  detail = k_RixSCUniform;
173  data.f3 = const_cast<RtFloat3 *>(dflt);
174  }
175  void InitEmptyC(RtColorRGB const *dflt=NULL)
176  {
177  type = k_RixSCColor;
178  if(!dflt)
179  detail = k_RixSCInvalidDetail;
180  else
181  detail = k_RixSCUniform;
182  data.rgb = const_cast<RtColorRGB *>(dflt);
183  }
184 
189  RtFloat dflt)
190  {
191  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
192  assert(npts != 0);
193  type = k_RixSCFloat;
194  detail = k_RixSCUniform;
195  data.f1 = pool.AllocFor<RtFloat>(memcat, npts);
196  data.f1[0] = dflt;
197  }
198 
199  void InitReserveI(RixShadingContext::Allocator &pool, int npts,
200  RtInt dflt)
201  {
202  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
203  assert(npts != 0);
204  type = k_RixSCInteger;
205  detail = k_RixSCUniform;
206  data.i1 = pool.AllocFor<RtInt>(memcat, npts);
207  data.i1[0] = dflt;
208  }
209 
210  void InitReserveC(RixShadingContext::Allocator &pool, int npts,
211  RtColorRGB const &dflt)
212  {
213  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
214  assert(npts != 0);
215  type = k_RixSCColor;
216  detail = k_RixSCUniform;
217  data.rgb = pool.AllocFor<RtColorRGB>(memcat, npts);
218  data.rgb[0] = dflt;
219  }
220 
221  void InitReserveV(RixShadingContext::Allocator &pool, int npts)
222  {
223  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
224  assert(npts != 0);
225  type = k_RixSCFloat3;
226  detail = k_RixSCInvalidDetail;
227  data.f3 = pool.AllocFor<RtFloat3>(memcat, npts);
228  }
229 
232  RtInt GetValueI() const
233  {
234  assert(type == k_RixSCInteger && detail == k_RixSCUniform);
235  return data.i1[0];
236  }
237  RtInt GetValueI(int i) const
238  {
239  if(detail == k_RixSCUniform)
240  return data.i1[0];
241  else
242  if(detail == k_RixSCVarying)
243  return data.i1[i];
244  else
245  return 0;
246  }
247  RtFloat GetValueF() const
248  {
249  assert(type == k_RixSCFloat && detail == k_RixSCUniform);
250  return data.f1[0];
251  }
252  RtFloat GetValueF(int i) const
253  {
254  if(detail == k_RixSCUniform)
255  return data.f1[0];
256  else
257  if(detail == k_RixSCVarying)
258  return data.f1[i];
259  else
260  return 0.f;
261  }
262  RtFloat3 const&GetValueV() const
263  {
264  assert(type == k_RixSCFloat3 && detail == k_RixSCUniform);
265  return data.f3[0];
266  }
267  RtFloat3 const &GetValueV(int i) const
268  {
269  if(detail == k_RixSCUniform)
270  return data.f3[0];
271  else
272  if(detail == k_RixSCVarying)
273  return data.f3[i];
274  else
275  return RixConstants::k_ZeroF3;
276  }
277  RtColorRGB const &GetValueC() const
278  {
279  assert(type == k_RixSCColor && detail == k_RixSCUniform);
280  return data.rgb[0];
281  }
282  RtColorRGB const &GetValueC(int i) const
283  {
284  if(detail == k_RixSCUniform)
285  return data.rgb[0];
286  else
287  if(detail == k_RixSCVarying)
288  return data.rgb[i];
289  else
290  return RixConstants::k_ZeroRGB;
291  }
292 
294  void SetValue(int i, RtFloat v)
295  {
296  if(detail == k_RixSCUniform)
297  if(i==0) data.f1[0] = v;
298  else
299  if(detail == k_RixSCVarying)
300  data.f1[i] = v;
301  }
302 
305  void Bind(RixPattern::OutputSpec &o, int pid)
306  {
307  o.paramId = pid;
308  o.detail = detail;
309  o.value = (RtConstPointer) data.f1; // f1 works for all types
310  }
311 
314  void PromoteToVarying(int npts)
315  {
316  if(this->detail != k_RixSCUniform)
317  return; // can't do it
318  else
319  {
321  switch(type)
322  {
323  case k_RixSCFloat:
324  this->detail = k_RixSCVarying;
325  for(int i=1;i<npts;i++)
326  data.f1[i] = data.f1[0];
327  break;
328  case k_RixSCColor:
329  this->detail = k_RixSCVarying;
330  for(int i=1;i<npts;i++)
331  data.rgb[i] = data.rgb[0];
332  break;
333  case k_RixSCNormal:
334  case k_RixSCPoint:
335  case k_RixSCVector:
336  case k_RixSCFloat3:
337  for(int i=1;i<npts;i++)
338  data.f3[i] = data.f3[0];
339  break;
340  case k_RixSCInteger:
341  default:
342  assert(0); // unexpected
343  break;
344  }
345  }
346  }
347 
348 };
349 
350 #endif
351