RenderManAPI  24.0
RixShadingParam.h
Go to the documentation of this file.
1 /*
2 # ------------------------------------------------------------------------------
3 #
4 # Copyright (c) 2020 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 #ifndef RixShadingParam_h
38 #define RixShadingParam_h
39 
40 #include <cassert> // for assert
41 #include <cstddef> // for NULL
42 #include "RixPattern.h" // for RixPattern::OutputSpec, etc
43 #include "RixShading.h" // for RixSCDetail::k_RixSCUniform, etc
44 #include "RixShadingUtils.h" // for k_ZeroF3, k_ZeroRGB
45 #include "RiTypesHelper.h" // for RtFloat, int, RtColorRGB, etc
46 
47 // RixShadingParam is a convenience class to encapsulate the type, detail and
48 // value(s) associated with an individual POD parameter of a RixShadingPlugin.
49 // Client is expected to know/define the types of its paramters and can use
50 // this class to initialize and invoke EvalParam on them.
51 //
52 // Usage --
53 //
54 // InitEval: initialize a parameter by invoking Eval. Optionally promote
55 // to varying, to simplify coding logic.
56 //
57 // InitEmpty: initialize to invalid or valid-but-uniform-default state.
58 //
59 // InitReserve: reserve space for varying parameter but start as
60 // uniform in an attempt to express minimum computational requirements.
61 // Usually called to initialize an output or target parameter.
62 //
63 // Bind: exposes our internal state as standard RixPattern output.
64 //
65 // GetValue: retrieves the value of the parameter at shading point i.
66 //
67 // To-do --
68 // * smooth rough edges for detail promotion of RtFloat3 and friends.
69 // * fill-in SetValue implementations if called for.
70 // * transition PxrLM/lmParam.h to here.
71 // * add support for array parameters.
72 //
73 
74 // Bxdf clients:
75 // #define RIX_SHADING_PARAM_MEMCATEGORY RixShadingContext::k_BxdfMem
76 // Pattern clients:
77 // #define RIX_SHADING_PARAM_MEMCATEGORY RixShadingContext::k_PatternMem
78 
79 #ifndef RIX_SHADING_PARAM_MEMCATEGORY
80 #error RIX_SHADING_PARAM_MEMCATEGORY must be defined prior to inclusion
81 #endif
82 
84 {
85 public:
87  {
90  data.f1 = NULL;
91  }
92 
93  // publicly visible member variables ..... (we're struct-like)
94  union
95  {
96  int *i1;
97  float *f1;
100  } data;
103 
104  bool IsValid() const { return detail != k_RixSCInvalidDetail; }
105 
106  // InitEval:
107  // initialize a parmeter by invoking Eval. We don't promote
108  // to varying to preserve the detail of the upstream data.
109  void InitEvalF(
110  RixShadingContext const *sCtx,
111  int paramId,
112  float *dflt,
113  bool promoteToVarying = false)
114  {
115  type = k_RixSCFloat;
116  detail = sCtx->EvalParam(
117  paramId, -1, const_cast<float const **>(&data.f1), dflt, promoteToVarying);
118  // returned detail is that of real connection
119  if(promoteToVarying)
121  }
122 
123  void InitEvalV(
124  RixShadingContext const *sCtx,
125  int paramId,
126  RtFloat3 *dflt,
127  bool promoteToVarying = false)
128  {
130  detail = sCtx->EvalParam(
131  paramId, -1, const_cast<RtFloat3 const **>(&data.f3), dflt, promoteToVarying);
132  if(detail != k_RixSCVarying)
134  }
135 
136  void InitEvalC(
137  RixShadingContext const *sCtx,
138  int paramId,
139  RtColorRGB *dflt,
140  bool promoteToVarying = false)
141  {
142  type = k_RixSCColor;
143  detail = sCtx->EvalParam(
144  paramId, -1, const_cast<RtColorRGB const **>(&data.rgb), dflt, promoteToVarying);
145  // returned detail is that of real connection
146  if(promoteToVarying)
148  }
149 
150  void InitEvalI(
151  RixShadingContext const *sCtx,
152  int paramId,
153  int *dflt,
154  bool promoteToVarying = false)
155  {
157  detail = sCtx->EvalParam(
158  paramId, -1, const_cast<int const **>(&data.i1), dflt, promoteToVarying);
159  // returned detail is that of real connection
160  if(promoteToVarying)
162  }
163 
164 
165  // InitEmpty: initialize to invalid or valid-but-uniform-default state.
166  void InitEmptyF(float const *dflt=NULL)
167  {
168  type = k_RixSCFloat;
169  if(!dflt)
170  {
172  }
173  else
174  {
176  }
177  data.f1 = const_cast<float *>(dflt);
178  }
179 
180  void InitEmptyI(int const *dflt=NULL)
181  {
183  if(!dflt)
184  {
186  }
187  else
188  {
190  }
191  data.i1 = const_cast<int *>(dflt);
192  }
193 
194  void InitEmptyV(RtFloat3 *dflt=NULL)
195  {
197  if(!dflt)
198  {
200  }
201  else
202  {
204  }
205  data.f3 = const_cast<RtFloat3 *>(dflt);
206  }
207 
208  void InitEmptyC(RtColorRGB const *dflt=NULL)
209  {
210  type = k_RixSCColor;
211  if(!dflt)
212  {
214  }
215  else
216  {
218  }
219  data.rgb = const_cast<RtColorRGB *>(dflt);
220  }
221 
222  // InitReserve: reserve space for varying parameter but start as
223  // uniform in an attempt to express minimum computational requirements.
224  // Usually called to initialize an output or target parameter.
227  int numPts,
228  float dflt)
229  {
230  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
231  assert(numPts != 0);
232  type = k_RixSCFloat;
234  data.f1 = pool.AllocFor<RtFloat>(memcat, numPts);
235  data.f1[0] = dflt;
236  }
237 
240  int numPts,
241  int dflt)
242  {
243  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
244  assert(numPts != 0);
247  data.i1 = pool.AllocFor<int>(memcat, numPts);
248  data.i1[0] = dflt;
249  }
250 
253  int numPts,
254  RtColorRGB const &dflt)
255  {
256  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
257  assert(numPts != 0);
258  type = k_RixSCColor;
260  data.rgb = pool.AllocFor<RtColorRGB>(memcat, numPts);
261  data.rgb[0] = dflt;
262  }
263 
265  {
266  RixShadingContext::MemCategory memcat = RIX_SHADING_PARAM_MEMCATEGORY;
267  assert(numPts != 0);
270  data.f3 = pool.AllocFor<RtFloat3>(memcat, numPts);
271  }
272 
273  // GetValue:
274  // return value handling detail
275  int GetValueI() const
276  {
277  assert(type == k_RixSCInteger && detail == k_RixSCUniform);
278  return data.i1[0];
279  }
280 
281  int GetValueI(int i) const
282  {
283  if(detail == k_RixSCUniform)
284  {
285  return data.i1[0];
286  }
287  else if(detail == k_RixSCVarying)
288  {
289  return data.i1[i];
290  }
291  else
292  {
293  return 0;
294  }
295  }
296 
297  float GetValueF() const
298  {
299  assert(type == k_RixSCFloat && detail == k_RixSCUniform);
300  return data.f1[0];
301  }
302 
303  float GetValueF(int i) const
304  {
305  if(detail == k_RixSCUniform)
306  {
307  return data.f1[0];
308  }
309  else if(detail == k_RixSCVarying)
310  {
311  return data.f1[i];
312  }
313  else
314  {
315  return 0.f;
316  }
317  }
318 
319  RtFloat3 const&GetValueV() const
320  {
321  assert(type == k_RixSCFloat3 && detail == k_RixSCUniform);
322  return data.f3[0];
323  }
324 
325  RtFloat3 const &GetValueV(int i) const
326  {
327  if(detail == k_RixSCUniform)
328  {
329  return data.f3[0];
330  }
331  else if(detail == k_RixSCVarying)
332  {
333  return data.f3[i];
334  }
335  else
336  {
337  return RixConstants::k_ZeroF3;
338  }
339  }
340 
341  RtColorRGB const &GetValueC() const
342  {
343  assert(type == k_RixSCColor && detail == k_RixSCUniform);
344  return data.rgb[0];
345  }
346 
347  RtColorRGB const &GetValueC(int i) const
348  {
349  if(detail == k_RixSCUniform)
350  {
351  return data.rgb[0];
352  }
353  else if(detail == k_RixSCVarying)
354  {
355  return data.rgb[i];
356  }
357  else
358  {
359  return RixConstants::k_ZeroRGB;
360  }
361  }
362 
363  // SetValue
364  void SetValue(int i, float v)
365  {
366  if(detail == k_RixSCUniform)
367  {
368  if(i==0) { data.f1[0] = v; }
369  }
370  else if(detail == k_RixSCVarying)
371  {
372  data.f1[i] = v;
373  }
374  }
375 
376  // Bind:
377  // exposes our internal state as standard RixPattern output
378  void Bind(RixPattern::OutputSpec &o, int paramId)
379  {
380  o.paramId = paramId;
381  o.detail = detail;
382  o.value = (void const*) data.f1; // f1 works for all types
383  }
384 
385  // PromoteToVarying: convert uniform to varying. NB: this assumes that
386  // data has been allocated for worse-case (via InitReserve). Tread carefully.
387  void PromoteToVarying(int numPts)
388  {
389  if(this->detail != k_RixSCUniform)
390  {
391  return; // can't do it
392  }
393  else
394  {
395  // "smear" our uniform result into all elements.
396  switch(type)
397  {
398  case k_RixSCFloat:
399  this->detail = k_RixSCVarying;
400  for(int i = 1; i < numPts; i++)
401  {
402  data.f1[i] = data.f1[0];
403  }
404  break;
405  case k_RixSCColor:
406  this->detail = k_RixSCVarying;
407  for(int i = 1; i < numPts; i++)
408  {
409  data.rgb[i] = data.rgb[0];
410  }
411  break;
412  case k_RixSCNormal:
413  case k_RixSCPoint:
414  case k_RixSCVector:
415  case k_RixSCFloat3:
416  for(int i = 1; i < numPts; i++)
417  {
418  data.f3[i] = data.f3[0];
419  }
420  break;
421  case k_RixSCInteger:
422  default:
423  assert(0); // unexpected
424  break;
425  }
426  }
427  }
428 
429 }; // end of RixShadingParam definition
430 
431 #endif
432 
int GetValueI() const
Definition: RixShadingParam.h:275
RixShadingContext is analogous to a RenderMan grid - it is a group of 1 or more points that may be sh...
Definition: RixShading.h:663
Definition: RixShadingParam.h:83
void InitEvalC(RixShadingContext const *sCtx, int paramId, RtColorRGB *dflt, bool promoteToVarying=false)
Definition: RixShadingParam.h:136
RixShadingParam()
Definition: RixShadingParam.h:86
RixSCType type
Definition: RixShadingParam.h:102
void InitEvalV(RixShadingContext const *sCtx, int paramId, RtFloat3 *dflt, bool promoteToVarying=false)
Definition: RixShadingParam.h:123
void InitEmptyF(float const *dflt=NULL)
Definition: RixShadingParam.h:166
Definition: RixShading.h:356
float GetValueF() const
Definition: RixShadingParam.h:297
pxrcore::ColorRGB RtColorRGB
Definition: RiTypesHelper.h:520
void const * value
output values
Definition: RixPattern.h:66
void Bind(RixPattern::OutputSpec &o, int paramId)
Definition: RixShadingParam.h:378
int * i1
Definition: RixShadingParam.h:96
RtColorRGB const & GetValueC(int i) const
Definition: RixShadingParam.h:347
Definition: RixShading.h:436
RixSCDetail detail
output detail may be determined by inputs
Definition: RixPattern.h:65
MemCategory
Definition: RixShading.h:1223
Definition: RixShading.h:358
float RtFloat
Definition: ri.h:54
RixSCDetail
RixSCDetail enumerates the valid sizing qualifiers for SC parameters.
Definition: RixShading.h:432
void InitReserveV(RixShadingContext::Allocator &pool, int numPts)
Definition: RixShadingParam.h:264
Definition: RixShading.h:435
bool IsValid() const
Definition: RixShadingParam.h:104
RtFloat3 const & GetValueV(int i) const
Definition: RixShadingParam.h:325
Definition: RixShading.h:359
void InitEmptyV(RtFloat3 *dflt=NULL)
Definition: RixShadingParam.h:194
Definition: RixShading.h:434
void InitEmptyI(int const *dflt=NULL)
Definition: RixShadingParam.h:180
pxrcore::Float3 RtFloat3
Definition: RiTypesHelper.h:69
int paramId
into param table
Definition: RixPattern.h:64
Definition: RixShading.h:360
RtFloat3 * f3
Definition: RixShadingParam.h:98
RixSCType
RixSCType enumerates the valid atomic datatypes.
Definition: RixShading.h:349
void InitEmptyC(RtColorRGB const *dflt=NULL)
Definition: RixShadingParam.h:208
float * f1
Definition: RixShadingParam.h:97
RtColorRGB const & GetValueC() const
Definition: RixShadingParam.h:341
Definition: RixShading.h:354
float GetValueF(int i) const
Definition: RixShadingParam.h:303
Definition: RixShading.h:353
virtual RixSCDetail EvalParam(int paramId, int arrayIndex, int const **result, int const *dflt=NULL, bool promoteToVarying=false) const =0
EvalParam.
void InitReserveF(RixShadingContext::Allocator &pool, int numPts, float dflt)
Definition: RixShadingParam.h:225
Definition: RixShading.h:357
Allocator is a utility (fully inlined) class to assist with tedious memory pool allocations.
Definition: RixShading.h:1363
void InitEvalI(RixShadingContext const *sCtx, int paramId, int *dflt, bool promoteToVarying=false)
Definition: RixShadingParam.h:150
union RixShadingParam::@5 data
void SetValue(int i, float v)
Definition: RixShadingParam.h:364
void InitReserveI(RixShadingContext::Allocator &pool, int numPts, int dflt)
Definition: RixShadingParam.h:238
void InitEvalF(RixShadingContext const *sCtx, int paramId, float *dflt, bool promoteToVarying=false)
Definition: RixShadingParam.h:109
void InitReserveC(RixShadingContext::Allocator &pool, int numPts, RtColorRGB const &dflt)
Definition: RixShadingParam.h:251
T * AllocFor(MemCategory memcat, int num)
Definition: RixShading.h:1375
Definition: RixPattern.h:62
int GetValueI(int i) const
Definition: RixShadingParam.h:281
RtFloat3 const & GetValueV() const
Definition: RixShadingParam.h:319
void PromoteToVarying(int numPts)
Definition: RixShadingParam.h:387
Definition: RixShading.h:351
RixSCDetail detail
Definition: RixShadingParam.h:101
RtColorRGB * rgb
Definition: RixShadingParam.h:99