RslPlugin.h Source File

RslPlugin.h
Go to the documentation of this file.
1 /* $Revision: #1 $ (Pixar - RenderMan Division) $Date: 2015/04/23 $ */
2 /*
3 # ------------------------------------------------------------------------------
4 #
5 # Copyright (c) 2006-2010 Pixar Animation Studios. All rights reserved.
6 #
7 # The information in this file (the "Software") is provided for the
8 # exclusive use of the software licensees of Pixar. Licensees have
9 # the right to incorporate the Software into other products for use
10 # by other authorized software licensees of Pixar, without fee.
11 # Except as expressly permitted herein, the Software may not be
12 # disclosed to third parties, copied or duplicated in any form, in
13 # whole or in part, without the prior written permission of
14 # Pixar Animation Studios.
15 #
16 # The copyright notices in the Software and this entire statement,
17 # including the above license grant, this restriction and the
18 # following disclaimer, must be included in all copies of the
19 # Software, in whole or in part, and all permitted derivative works of
20 # the Software, unless such copies or derivative works are solely
21 # in the form of machine-executable object code generated by a
22 # source language processor.
23 #
24 # PIXAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
26 # SHALL PIXAR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
27 # OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
28 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
29 # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30 # SOFTWARE.
31 #
32 # Pixar
33 # 1200 Park Ave
34 # Emeryville CA 94608
35 #
36 # ------------------------------------------------------------------------------
37 
38 */
39 
40 #ifndef RSLPLUGIN_H
41 #define RSLPLUGIN_H
42 
43 // Use of typedefs like RtColor is recommended for new grid-based shadeops.
44 // For backwards compatibility, they are not automatically included for older
45 // pointwise shadeops.
46 #include "ri.h"
47 #include "RixInterfaces.h" // RslContext inherits from RixContext
48 #include <stdarg.h>
49 #include <assert.h>
50 #include <string>
51 
53 #define RSL_PLUGIN_VERSION 7
54 
125 typedef unsigned int RslRunFlag;
128 
130 typedef unsigned int RslIncrType;
131 
134 typedef int (*RslEntryFunc)(class RslContext* ctx,
135  int argc, const class RslArg** argv);
136 
151 class RslContext_v1 : public RixContext {
152 public:
154  virtual ~RslContext_v1() {}
155 
158  virtual const RslRunFlag* GetRunFlags(unsigned int* length) const = 0;
159 
164  virtual RixInterface* GetRixInterface(RixInterfaceId id) const = 0;
165 
188  RixStorage* GetGlobalStorage() const
189  {
190  return (RixStorage*) GetRixInterface(k_RixGlobalData);
191  }
192 
211  RixStorage* GetThreadStorage() const
212  {
213  return (RixStorage*) GetRixInterface(k_RixThreadData);
214  }
215 
222  RixStorage* GetLocalStorage() const
223  {
224  return (RixStorage*) GetRixInterface(k_RixLocalData);
225  }
226 
230  void SetThreadData(void* data, RixCleanupFunc cleanup = 0L)
231  {
232  RixStorage* storage = (RixStorage*) GetRixInterface(k_RixThreadData);
233  storage->Set(GetPluginName(), data, cleanup);
234  }
235 
237  void* GetThreadData() const
238  {
239  RixStorage* storage = (RixStorage*) GetRixInterface(k_RixThreadData);
240  return storage->Get(GetPluginName());
241  }
242 
247  void SetLocalData(void* data, RixCleanupFunc cleanup = 0L)
248  {
249  RixStorage* storage = (RixStorage*) GetRixInterface(k_RixLocalData);
250  storage->Set(GetPluginName(), data, cleanup);
251  }
252 
254  void* GetLocalData() const
255  {
256  RixStorage* storage = (RixStorage*) GetRixInterface(k_RixLocalData);
257  return storage->Get(GetPluginName());
258  }
259 
261  virtual const char* GetPluginName() const = 0;
262 
263 private:
264  // Need to call private methods from iterator constructors.
265  template<typename T> friend class RslIter;
266  template<typename T> friend class RslArrayIter;
267 
268  // Get acceleration info for an iterator with "packed" user data.
269  virtual RslIncrType* getIncrList(unsigned int stride) const = 0;
270 };
271 
272 class RslContext_v2 : public RslContext_v1 {
273 public:
275  virtual ~RslContext_v2() {};
276 
280  virtual bool HasAuxiliaryPoints() const = 0;
281 };
282 
283 class RslContext : public RslContext_v2 {
284 public:
286  virtual ~RslContext() {};
287 };
288 
291 class RslArg_v2 {
292 public:
294  virtual ~RslArg_v2() {};
295 
297  virtual bool IsFloat() const = 0;
298 
300  virtual bool IsPoint() const = 0;
301 
303  virtual bool IsVector() const = 0;
304 
306  virtual bool IsColor() const = 0;
307 
309  virtual bool IsString() const = 0;
310 
312  virtual bool IsMatrix() const = 0;
313 
315  virtual bool IsArray() const = 0;
316 
318  virtual bool IsVarying() const = 0;
319 
322  virtual int GetArrayLength() const = 0;
323 
327  virtual unsigned int NumValues() const = 0;
328 
332  static unsigned int NumValues(int argc, const class RslArg** argv);
333 
341  virtual void GetData(float** data, int* stride) const = 0;
342 
343 private:
344  // Need to call private methods from iterator constructors.
345  template<typename T> friend class RslIter;
346  template<typename T> friend class RslArrayIter;
347 
348  // Returns (in result parameters) the data pointer and increment list.
349  virtual void getInfo(float** data, RslIncrType** incrList,
350  bool* isVarying) const = 0;
351 
352  // Returns (in result parameters) the data pointer, increment list, and
353  // array length.
354  virtual void getArrayInfo(float** data,
355  RslIncrType** incrList, int* arrayLength,
356  bool* isVarying) const = 0;
357 };
358 
359 
362 class RslArg_v3 : public RslArg_v2
363 {
364 public:
366  virtual bool IsNormal() const = 0;
367 
372  virtual bool IsWriteable() const = 0;
373 
375  virtual ~RslArg_v3() {};
376 };
377 
378 
381 class RslArg_v5 : public RslArg_v3
382 {
383 public:
385  virtual bool IsStruct() const = 0;
386 
389  virtual const char* GetName() const = 0;
390 
392  virtual bool IsResizable() const = 0;
393 
395  virtual class RslResizer* GetResizer() const = 0;
396 
398  virtual ~RslArg_v5() {};
399 
400 private:
401  // Need to call private methods from various constructors.
402  friend class RslStruct;
403  friend class RslStructArray;
404  template<typename T> friend class RslArrayIter;
405 
406 
407  // Get struct data pointer, along with array length and stride.
408  virtual float* getStructData(int* length,
409  unsigned int* stride) const = 0;
410 
411  // Get struct info. Returns RslArgs for struct members.
412  virtual const RslArg** getStructInfo(float* data,
413  const char** name,
414  unsigned int* numMembers) const = 0;
415 
416  // OBSOLETE: The layout of array data has changed in version 5.
417  // Returns (in result parameters) the data pointer, increment list, and
418  // array length/capacity.
419  virtual void getArrayInfo(float** data,
420  RslIncrType** incrList, int* arrayLength,
421  int* arrayCapacity, bool* isVarying) const = 0;
422 
423  // Obsolete version of getArrayInfo, which doesn't return capacity.
424  virtual void getArrayInfo(float** data,
425  RslIncrType** incrList, int* arrayLength,
426  bool* isVarying) const = 0;
427 };
428 
429 // NOTE:
430 // This is a shim class to provide compatabiliy with
431 // v5 of the RslArg interface. Due to a cut/paste
432 // error in a previous release, some private methods
433 // from RslArg_v5 were aliased against the actual
434 // RslArg interface.
435 class RslArg_v6 : public RslArg_v5
436 {
437 public:
439  virtual ~RslArg_v6() {};
440 
441  // Note these are the actual methods that were resolved
442  // instead of the ones defined in RslArg_v5
443 private:
444  // Need to call private methods from various constructors.
445  friend class RslStruct;
446  friend class RslStructArray;
447  template<typename T> friend class RslArrayIter;
448 
449  // Returns (in result parameters) the data pointer, increment list, and
450  // array length/capacity and element stride.
451  virtual void getArrayInfo(float **data,
452  RslIncrType ** incrList, int* arrayLength,
453  int* arrayCapacity, int* elemStride,
454  bool* isVarying) const = 0;
455 
456  // OBSOLETE: The layout of array data has changed in version 5.
457  virtual void getArrayInfo(float** data,
458  RslIncrType** incrList, int* arrayLength,
459  int* arrayCapacity, bool* isVarying) const = 0;
460 
461  // Obsolete version of getArrayInfo, which doesn't return capacity.
462  virtual void getArrayInfo(float** data,
463  RslIncrType** incrList, int* arrayLength,
464  bool* isVarying) const = 0;
465 };
466 
469 class RslArg_v7 : public RslArg_v6
470 {
471 public:
472 
474  virtual bool IsFilterRegion() const = 0;
475 
477  virtual ~RslArg_v7() {};
478 };
479 
524 class RslArg : public RslArg_v7
525 {
526 public:
528  virtual ~RslArg() {};
529 
530 };
531 
532 
533 // Implementation of RslArg_v2::NumValues must follow definition of RslArg.
534 inline unsigned int
535 RslArg_v2::NumValues(int argc, const RslArg** argv)
536 {
537  int m = 1;
538  for (int i = 0; i < argc; ++i) {
539  int n = argv[i]->NumValues();
540  if (n > m)
541  m = n;
542  }
543  return m;
544 }
545 
546 
574 template<typename T>
575 class RslIter {
576 public:
578  RslIter(const RslArg* arg)
579  {
580  arg->getInfo(&m_data, &m_incrList, &m_isVarying);
581  m_dataStart = m_data;
582  m_incrListStart = m_incrList;
583  }
584 
587  RslIter(const T* data, const RslContext* ctx) :
588  m_data((float*) data),
589  m_dataStart((float*) data),
590  m_incrList(ctx->getIncrList(0)),
591  m_incrListStart(ctx->getIncrList(0)),
592  m_isVarying(false)
593  {
594  }
595 
599  T& operator*() { return *((T*) m_data); }
600 
604  const T& operator*() const { return *((T*) m_data); }
605 
610  {
611  m_data += *m_incrList;
612  ++m_incrList;
613  return *this;
614  };
615 
621  {
622  RslIter<T> temp = *this; // Copy before increment.
623  ++*this; // Increment.
624  return temp; // Return old value.
625  };
626 
628  void Reset()
629  {
630  m_data = m_dataStart;
631  m_incrList = m_incrListStart;
632  }
633 
636  bool IsVarying() const { return m_isVarying; }
637 
638 private:
639  // Current data pointer.
640  float* m_data;
641 
642  // The reset to the start data pointer.
643  float* m_dataStart;
644 
645  // Current increment list, which gives fast access to next active point.
646  RslIncrType* m_incrList;
647 
648  // The reset to the start increment list pointer.
649  RslIncrType* m_incrListStart;
650 
651  // True if the iterator is varying.
652  bool m_isVarying;
653 };
654 
655 
657 template<>
658 inline RslIter<RtMatrix>&
660 {
661  m_data += *m_incrList * 16;
662  ++m_incrList;
663  return *this;
664 }
665 
666 
714 template<typename T>
716 public:
718  RslArrayIter(const RslArg* arg)
719  {
720  arg->getArrayInfo(&m_data, &m_incrList, &m_length, &m_capacity,
721  &m_stride, &m_isVarying);
722 
723  m_dataStart = m_data;
724  m_incrListStart = m_incrList;
725  }
726 
729  RslArrayIter(const T* data, int length, const RslContext* ctx) :
730  m_data((float*) data),
731  m_dataStart((float*) data),
732  m_incrList(ctx->getIncrList(0)),
733  m_incrListStart(ctx->getIncrList(0)),
734  m_length(length),
735  m_capacity(length),
736  m_stride(1),
737  m_isVarying(false)
738  {
739  }
740 
741  // Default constructor
742  RslArrayIter() :
743  m_data(NULL),
744  m_dataStart(NULL),
745  m_incrList(NULL),
746  m_incrListStart(NULL),
747  m_length(0),
748  m_capacity(0),
749  m_stride(0),
750  m_isVarying(false)
751  {
752  }
753 
756  T& operator[](int x)
757  {
758  assert(x >= 0 && x < m_length);
759  return ((T*)m_data)[x * m_stride];
760  }
761 
766  {
767  m_data += *m_incrList;
768  ++m_incrList;
769  return *this;
770  };
771 
777  {
778  RslArrayIter<T> temp = *this; // Copy before increment.
779  ++*this; // Increment.
780  return temp; // Return old value.
781  };
782 
786  void Reset(int index=0)
787  {
788  m_data = (float *)&((T*)m_dataStart)[index * m_stride];
789  m_incrList = m_incrListStart;
790  }
791 
794  bool IsVarying() const { return m_isVarying; }
795 
797  int GetLength() const { return m_length; }
798 
800  int GetCapacity() const { return m_capacity; }
801 
803  int GetStride() const { return m_stride; }
804 
805 #ifndef RSL_ALLOW_ARRAY_DEREF
806 private:
807 #endif
808  T* operator*() { return (T*) m_data; }
811 
812 private:
813  // Current data pointer.
814  float* m_data;
815 
816  // The reset to the start data pointer.
817  float* m_dataStart;
818 
819  // Current increment list, which gives fast access to next active point.
820  RslIncrType* m_incrList;
821 
822  // The reset to the start increment list pointer.
823  RslIncrType* m_incrListStart;
824 
825  // Array length.
826  int m_length;
827 
828  // Array capacity, which is the same as the length unless it's resizable.
829  int m_capacity;
830 
831  // The stride to get to the next array element
832  int m_stride;
833 
834  // True if the iterator is varying.
835  bool m_isVarying;
836 };
837 
839 template<>
842 {
843  m_data += *m_incrList * 16;
844  ++m_incrList;
845  return *this;
846 }
847 
856  // data layout as RtMatrix
857 
865 
913 class RslResizer {
914 public:
916  virtual unsigned int GetLength() const = 0;
917 
919  virtual unsigned int GetCapacity() const = 0;
920 
926  virtual void Resize(unsigned int n) = 0;
927 
933  virtual void Reserve(unsigned int n) = 0;
934 
935 protected:
937  virtual ~RslResizer() { }
938 };
939 
940 
982 class RslStruct {
983 public:
985  RslStruct(const RslArg* arg)
986  {
987  assert(arg->IsStruct() && !arg->IsArray());
988  int length;
989  unsigned int stride;
990  float* data = arg->getStructData(&length, &stride);
991  m_members = arg->getStructInfo(data, &m_name, &m_numMembers);
992  }
993 
995  const char* GetName() const { return m_name; }
996 
998  unsigned int GetNumMembers() const { return m_numMembers; }
999 
1001  const RslArg* operator[](int i) const
1002  {
1003  assert(i >= 0 && (unsigned int) i < m_numMembers);
1004  return m_members[i];
1005  }
1006 
1007 private:
1008  // Struct members
1009  const RslArg** m_members;
1010 
1011  // Struct type name.
1012  const char* m_name;
1013 
1014  // Number of struct members.
1015  unsigned int m_numMembers;
1016 
1017  // RslStructArray calls a private constructor.
1018  friend class RslStructArray;
1019 
1020  // Private constructor
1021  RslStruct(const RslArg* arg, float* data)
1022  {
1023  m_members = arg->getStructInfo(data, &m_name, &m_numMembers);
1024  }
1025 };
1026 
1027 
1046 public:
1049  {
1050  assert(arg->IsArray() && arg->IsStruct());
1051  m_array = arg;
1052  m_data = arg->getStructData(&m_length, &m_stride);
1053  }
1054 
1056  RslStruct operator[](int i) const
1057  {
1058  assert(i >= 0 && i < m_length);
1059  return RslStruct(m_array, m_data + i * m_stride);
1060  }
1061 
1063  int GetLength() const { return m_length; }
1064 
1065 private:
1066  // RslArg that represents the array.
1067  const RslArg* m_array;
1068 
1069  // Data pointer.
1070  float* m_data;
1071 
1072  // Array length.
1073  int m_length;
1074 
1075  // Stride for array index calculations (in words)
1076  unsigned int m_stride;
1077 };
1078 
1079 
1084 typedef void (*RslVoidFunc)(RixContext* context);
1085 
1126 struct RslFunction {
1128  const char *m_prototype;
1129 
1132 
1135 
1138 
1141 
1144 };
1145 
1171 
1173  const char m_version;
1174 
1177 
1180 
1182  RslFunctionTable(const RslFunction* functions,
1183  RslVoidFunc init = NULL, RslVoidFunc cleanup = NULL) :
1184  m_functions(functions),
1186  m_initFunc(init),
1187  m_cleanupFunc(cleanup)
1188  {
1189  }
1190 };
1191 
1192 #endif /* defined RSLPLUGIN_H */