RenderMan API  23.0
RixShadingParam.h
Go to the documentation of this file.
1 /*
2 # ------------------------------------------------------------------------------
3 #
4 # Copyright (c) 1986-2019 Pixar. All rights reserved.
5 #
6 # The information in this file (the "Software") is provided for the exclusive
7 # use of the software licensees of Pixar ("Licensees"). Licensees have the
8 # right to incorporate the Software into other products for use by other
9 # authorized software licensees of Pixar, without fee. Except as expressly
10 # permitted herein, the Software may not be disclosed to third parties, copied
11 # or duplicated in any form, in whole or in part, without the prior written
12 # permission of Pixar.
13 #
14 # The copyright notices in the Software and this entire statement, including the
15 # above license grant, this restriction and the following disclaimer, must be
16 # included in all copies of the Software, in whole or in part, and all permitted
17 # derivative works of the Software, unless such copies or derivative works are
18 # solely in the form of machine-executable object code generated by a source
19 # language processor.
20 #
21 # PIXAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL PIXAR BE
23 # LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
25 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. IN NO CASE WILL
27 # PIXAR'S TOTAL LIABILITY FOR ALL DAMAGES ARISING OUT OF OR IN CONNECTION WITH
28 # THE USE OR PERFORMANCE OF THIS SOFTWARE EXCEED $50.
29 #
30 # Pixar
31 # 1200 Park Ave
32 # Emeryville CA 94608
33 #
34 # ------------------------------------------------------------------------------
35 */
36 
37 
38 #ifndef RixShadingParam_h
39 #define RixShadingParam_h
40 
41 #include <cassert> // for assert
42 #include <cstddef> // for NULL
43 #include "RixPattern.h" // for RixPattern::OutputSpec, etc
44 #include "RixShading.h" // for RixSCDetail::k_RixSCUniform, etc
45 #include "RixShadingUtils.h" // for k_ZeroF3, k_ZeroRGB
46 #include "ri.h" // for RtFloat, RtInt, RtColorRGB, etc
47 
48 // RixShadingParam is a convenience class to encapsulate the type, detail and
49 // value(s) associated with an individual POD parameter of a RixShadingPlugin.
50 // Client is expected to know/define the types of its paramters and can use
51 // this class to initialize and invoke EvalParam on them.
52 //
53 // Usage --
54 //
55 // InitEval: initialize a parameter by invoking Eval. Optionally promote
56 // to varying, to simplify coding logic.
57 //
58 // InitEmpty: initialize to invalid or valid-but-uniform-default state.
59 //
60 // InitReserve: reserve space for varying parameter but start as
61 // uniform in an attempt to express minimum computational requirements.
62 // Usually called to initialize an output or target parameter.
63 //
64 // Bind: exposes our internal state as standard RixPattern output.
65 //
66 // GetValue: retrieves the value of the parameter at shading point i.
67 //
68 // To-do --
69 // * smooth rough edges for detail promotion of RtFloat3 and friends.
70 // * fill-in SetValue implementations if called for.
71 // * transition PxrLM/lmParam.h to here.
72 // * add support for array parameters.
73 //
74 
75 // Bxdf clients:
76 // #define RIX_SHADING_PARAM_MEMCATEGORY RixShadingContext::k_BxdfMem
77 // Pattern clients:
78 // #define RIX_SHADING_PARAM_MEMCATEGORY RixShadingContext::k_PatternMem
79 
80 #ifndef RIX_SHADING_PARAM_MEMCATEGORY
81 #error RIX_SHADING_PARAM_MEMCATEGORY must be defined prior to inclusion
82 #endif
83 
85 {
86 public:
88  {
91  data.f1 = NULL;
92  }
93 
94  // publicly visible member variables ..... (we're struct-like)
95  union
96  {
101  } data;
104 
105  bool IsValid() const { return detail != k_RixSCInvalidDetail; }
106 
107  // InitEval:
108  // initialize a parmeter by invoking Eval. We don't promote
109  // to varying to preserve the detail of the upstream data.
110  void InitEvalF(
111  RixShadingContext const *sCtx,
112  int paramId,
113  RtFloat *dflt,
114  bool promoteToVarying = false)
115  {
116  type = k_RixSCFloat;
117  detail = sCtx->EvalParam(
118  paramId, -1, const_cast<RtFloat const **>(&data.f1), dflt, promoteToVarying);
119  // returned detail is that of real connection
120  if(promoteToVarying)
121  detail = k_RixSCVarying;
122  }
123 
124  void InitEvalV(
125  RixShadingContext const *sCtx,
126  int paramId,
127  RtFloat3 *dflt,
128  bool promoteToVarying = false)
129  {
130  type = k_RixSCFloat3;
131  detail = sCtx->EvalParam(
132  paramId, -1, const_cast<RtFloat3 const **>(&data.f3), dflt, promoteToVarying);
133  if(detail != k_RixSCVarying)
134  detail = k_RixSCInvalidDetail;
135  }
136 
137  void InitEvalC(
138  RixShadingContext const *sCtx,
139  int paramId,
140  RtColorRGB *dflt,
141  bool promoteToVarying = false)
142  {
143  type = k_RixSCColor;
144  detail = sCtx->EvalParam(
145  paramId, -1, const_cast<RtColorRGB const **>(&data.rgb), dflt, promoteToVarying);
146  // returned detail is that of real connection
147  if(promoteToVarying)
148  detail = k_RixSCVarying;
149  }
150 
151  void InitEvalI(
152  RixShadingContext const *sCtx,
153  int paramId,
154  RtInt *dflt,
155  bool promoteToVarying = false)
156  {
157  type = k_RixSCInteger;
158  detail = sCtx->EvalParam(
159  paramId, -1, const_cast<RtInt const **>(&data.i1), dflt, promoteToVarying);
160  // returned detail is that of real connection
161  if(promoteToVarying)
162  detail = k_RixSCVarying;
163  }
164 
165 
166  // InitEmpty: initialize to invalid or valid-but-uniform-default state.
167  void InitEmptyF(RtFloat const *dflt=NULL)
168  {
169  type = k_RixSCFloat;
170  if(!dflt)
171  {
172  detail = k_RixSCInvalidDetail;
173  }
174  else
175  {
176  detail = k_RixSCUniform;
177  }
178  data.f1 = const_cast<RtFloat *>(dflt);
179  }
180 
181  void InitEmptyI(RtInt const *dflt=NULL)
182  {
183  type = k_RixSCInteger;
184  if(!dflt)
185  {
186  detail = k_RixSCInvalidDetail;
187  }
188  else
189  {
190  detail = k_RixSCUniform;
191  }
192  data.i1 = const_cast<RtInt *>(dflt);
193  }
194 
195  void InitEmptyV(RtFloat3 *dflt=NULL)
196  {
197  type = k_RixSCFloat3;
198  if(!dflt)
199  {
200  detail = k_RixSCInvalidDetail;
201  }
202  else
203  {
204  detail = k_RixSCUniform;
205  }
206  data.f3 = const_cast<RtFloat3 *>(dflt);
207  }
208 
209  void InitEmptyC(RtColorRGB const *dflt=NULL)
210  {
211  type = k_RixSCColor;
212  if(!dflt)
213  {
214  detail = k_RixSCInvalidDetail;
215  }
216  else
217  {
218  detail = k_RixSCUniform;
219  }
220  data.rgb = const_cast<RtColorRGB *>(dflt);
221  }
222 
223  // InitReserve: reserve space for varying parameter but start as
224  // uniform in an attempt to express minimum computational requirements.
225  // Usually called to initialize an output or target parameter.
228  int numPts,
229  RtFloat dflt)
230  {
231  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
232  assert(numPts != 0);
233  type = k_RixSCFloat;
234  detail = k_RixSCUniform;
235  data.f1 = pool.AllocFor<RtFloat>(memcat, numPts);
236  data.f1[0] = dflt;
237  }
238 
241  int numPts,
242  RtInt dflt)
243  {
244  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
245  assert(numPts != 0);
246  type = k_RixSCInteger;
247  detail = k_RixSCUniform;
248  data.i1 = pool.AllocFor<RtInt>(memcat, numPts);
249  data.i1[0] = dflt;
250  }
251 
254  int numPts,
255  RtColorRGB const &dflt)
256  {
257  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
258  assert(numPts != 0);
259  type = k_RixSCColor;
260  detail = k_RixSCUniform;
261  data.rgb = pool.AllocFor<RtColorRGB>(memcat, numPts);
262  data.rgb[0] = dflt;
263  }
264 
266  {
267  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
268  assert(numPts != 0);
269  type = k_RixSCFloat3;
270  detail = k_RixSCInvalidDetail;
271  data.f3 = pool.AllocFor<RtFloat3>(memcat, numPts);
272  }
273 
274  // GetValue:
275  // return value handling detail
276  RtInt GetValueI() const
277  {
278  assert(type == k_RixSCInteger && detail == k_RixSCUniform);
279  return data.i1[0];
280  }
281 
282  RtInt GetValueI(int i) const
283  {
284  if(detail == k_RixSCUniform)
285  {
286  return data.i1[0];
287  }
288  else if(detail == k_RixSCVarying)
289  {
290  return data.i1[i];
291  }
292  else
293  {
294  return 0;
295  }
296  }
297 
299  {
300  assert(type == k_RixSCFloat && detail == k_RixSCUniform);
301  return data.f1[0];
302  }
303 
304  RtFloat GetValueF(int i) const
305  {
306  if(detail == k_RixSCUniform)
307  {
308  return data.f1[0];
309  }
310  else if(detail == k_RixSCVarying)
311  {
312  return data.f1[i];
313  }
314  else
315  {
316  return 0.f;
317  }
318  }
319 
320  RtFloat3 const&GetValueV() const
321  {
322  assert(type == k_RixSCFloat3 && detail == k_RixSCUniform);
323  return data.f3[0];
324  }
325 
326  RtFloat3 const &GetValueV(int i) const
327  {
328  if(detail == k_RixSCUniform)
329  {
330  return data.f3[0];
331  }
332  else if(detail == k_RixSCVarying)
333  {
334  return data.f3[i];
335  }
336  else
337  {
338  return RixConstants::k_ZeroF3;
339  }
340  }
341 
342  RtColorRGB const &GetValueC() const
343  {
344  assert(type == k_RixSCColor && detail == k_RixSCUniform);
345  return data.rgb[0];
346  }
347 
348  RtColorRGB const &GetValueC(int i) const
349  {
350  if(detail == k_RixSCUniform)
351  {
352  return data.rgb[0];
353  }
354  else if(detail == k_RixSCVarying)
355  {
356  return data.rgb[i];
357  }
358  else
359  {
360  return RixConstants::k_ZeroRGB;
361  }
362  }
363 
364  // SetValue
365  void SetValue(int i, RtFloat v)
366  {
367  if(detail == k_RixSCUniform)
368  {
369  if(i==0) { data.f1[0] = v; }
370  }
371  else if(detail == k_RixSCVarying)
372  {
373  data.f1[i] = v;
374  }
375  }
376 
377  // Bind:
378  // exposes our internal state as standard RixPattern output
379  void Bind(RixPattern::OutputSpec &o, int paramId)
380  {
381  o.paramId = paramId;
382  o.detail = detail;
383  o.value = (RtConstPointer) data.f1; // f1 works for all types
384  }
385 
386  // PromoteToVarying: convert uniform to varying. NB: this assumes that
387  // data has been allocated for worse-case (via InitReserve). Tread carefully.
388  void PromoteToVarying(int numPts)
389  {
390  if(this->detail != k_RixSCUniform)
391  {
392  return; // can't do it
393  }
394  else
395  {
396  // "smear" our uniform result into all elements.
397  switch(type)
398  {
399  case k_RixSCFloat:
400  this->detail = k_RixSCVarying;
401  for(int i = 1; i < numPts; i++)
402  {
403  data.f1[i] = data.f1[0];
404  }
405  break;
406  case k_RixSCColor:
407  this->detail = k_RixSCVarying;
408  for(int i = 1; i < numPts; i++)
409  {
410  data.rgb[i] = data.rgb[0];
411  }
412  break;
413  case k_RixSCNormal:
414  case k_RixSCPoint:
415  case k_RixSCVector:
416  case k_RixSCFloat3:
417  for(int i = 1; i < numPts; i++)
418  {
419  data.f3[i] = data.f3[0];
420  }
421  break;
422  case k_RixSCInteger:
423  default:
424  assert(0); // unexpected
425  break;
426  }
427  }
428  }
429 
430 }; // end of RixShadingParam definition
431 
432 #endif
433 
void SetValue(int i, RtFloat v)
T * AllocFor(MemCategory memcat, int num)
Definition: RixShading.h:1307
pxrcore::ColorRGB RtColorRGB
void InitEvalF(RixShadingContext const *sCtx, int paramId, RtFloat *dflt, bool promoteToVarying=false)
RixSCDetail detail
output detail may be determined by inputs
Definition: RixPattern.h:66
RtColorRGB const & GetValueC() const
RtColorRGB * rgb
float RtFloat
Definition: ri.h:54
RtInt GetValueI(int i) const
void InitEmptyV(RtFloat3 *dflt=NULL)
void InitReserveI(RixShadingContext::Allocator &pool, int numPts, RtInt dflt)
RixSCDetail
Definition: RixShading.h:376
RtColorRGB const & GetValueC(int i) const
void InitEmptyC(RtColorRGB const *dflt=NULL)
RixSCType
Definition: RixShading.h:349
RtFloat3 const & GetValueV() const
void InitEvalV(RixShadingContext const *sCtx, int paramId, RtFloat3 *dflt, bool promoteToVarying=false)
void InitEmptyF(RtFloat const *dflt=NULL)
void InitEvalI(RixShadingContext const *sCtx, int paramId, RtInt *dflt, bool promoteToVarying=false)
RtConstPointer value
output values
Definition: RixPattern.h:67
void Bind(RixPattern::OutputSpec &o, int paramId)
RixSCDetail detail
RtInt GetValueI() const
RtFloat3 const & GetValueV(int i) const
void InitEmptyI(RtInt const *dflt=NULL)
RtFloat GetValueF() const
void const * RtConstPointer
Definition: ri.h:86
RtFloat GetValueF(int i) const
pxrcore::Float3 RtFloat3
Definition: RiTypesHelper.h:68
void PromoteToVarying(int numPts)
bool IsValid() const
union RixShadingParam::@4 data
int paramId
into param table
Definition: RixPattern.h:65
virtual RixSCDetail EvalParam(int paramId, int arrayIndex, int const **result, int const *dflt=NULL, bool promoteToVarying=false) const =0
void InitReserveV(RixShadingContext::Allocator &pool, int numPts)
void InitReserveC(RixShadingContext::Allocator &pool, int numPts, RtColorRGB const &dflt)
void InitReserveF(RixShadingContext::Allocator &pool, int numPts, RtFloat dflt)
void InitEvalC(RixShadingContext const *sCtx, int paramId, RtColorRGB *dflt, bool promoteToVarying=false)
int RtInt
Definition: ri.h:55