RslPlugin.h

Go to the documentation of this file.
00001 /* $Revision: #67 $  (Pixar - RenderMan Division)       $Date: 2008/12/08 $ */
00002 /*
00003 ** Copyright (c) 2006 PIXAR.  All rights reserved.  This program or
00004 ** documentation contains proprietary confidential information and trade
00005 ** secrets of PIXAR.  Reverse engineering of object code is prohibited.
00006 ** Use of copyright notice is precautionary and does not imply
00007 ** publication.
00008 **
00009 **                      RESTRICTED RIGHTS NOTICE
00010 **
00011 ** Use, duplication, or disclosure by the Government is subject to the
00012 ** following restrictions:  For civilian agencies, subparagraphs (a) through
00013 ** (d) of the Commercial Computer Software--Restricted Rights clause at
00014 ** 52.227-19 of the FAR; and, for units of the Department of Defense, DoD
00015 ** Supplement to the FAR, clause 52.227-7013 (c)(1)(ii), Rights in
00016 ** Technical Data and Computer Software.
00017 **
00018 ** Pixar
00019 ** 1200 Park Ave.
00020 ** Emeryville, CA  94608
00021 */
00022 
00023 #ifndef RSLPLUGIN_H
00024 #define RSLPLUGIN_H
00025 
00026 // Use of typedefs like RtColor is recommended for new grid-based shadeops.
00027 // For backwards compatibility, they are not automatically included for older
00028 // pointwise shadeops.
00029 #include "ri.h"
00030 #include "RixInterfaces.h"      // RslContext inherits from RixContext
00031 #include <stdarg.h>
00032 #include <assert.h>
00033 #include <string>
00034 
00036 #define RSL_PLUGIN_VERSION 5
00037 
00108 
00109 
00110 typedef unsigned int RslRunFlag;
00111 
00113 typedef unsigned int RslIncrType;
00114 
00117 typedef int (*RslEntryFunc)(class RslContext* ctx,
00118                             int argc, const class RslArg** argv);
00119 
00134 class RslContext : public RixContext {
00135 public:
00137     virtual ~RslContext() {}
00138 
00141     virtual const RslRunFlag* GetRunFlags(unsigned int* length) const = 0;
00142 
00147     virtual RixInterface* GetRixInterface(RixInterfaceId id) const = 0;
00148 
00171     RixStorage* GetGlobalStorage() const
00172     {
00173         return (RixStorage*) GetRixInterface(k_RixGlobalData);
00174     }
00175 
00194     RixStorage* GetThreadStorage() const
00195     {
00196         return (RixStorage*) GetRixInterface(k_RixThreadData);
00197     }
00198 
00205     RixStorage* GetLocalStorage() const
00206     {
00207         return (RixStorage*) GetRixInterface(k_RixLocalData);
00208     }
00209 
00213     void SetThreadData(void* data, RixCleanupFunc cleanup = 0L)
00214     {
00215         RixStorage* storage = (RixStorage*) GetRixInterface(k_RixThreadData);
00216         storage->Set(GetPluginName(), data, cleanup);
00217     }
00218 
00220     void* GetThreadData() const
00221     {
00222         RixStorage* storage = (RixStorage*) GetRixInterface(k_RixThreadData);
00223         return storage->Get(GetPluginName());
00224     }
00225 
00230     void SetLocalData(void* data, RixCleanupFunc cleanup = 0L)
00231     {
00232         RixStorage* storage = (RixStorage*) GetRixInterface(k_RixLocalData);
00233         storage->Set(GetPluginName(), data, cleanup);
00234     }
00235 
00237     void* GetLocalData() const
00238     {
00239         RixStorage* storage = (RixStorage*) GetRixInterface(k_RixLocalData);
00240         return storage->Get(GetPluginName());
00241     }
00242 
00244     virtual const char* GetPluginName() const = 0;
00245 
00246 private:
00247     // Need to call private methods from iterator constructors.
00248     template<typename T> friend class RslIter;
00249     template<typename T> friend class RslArrayIter;
00250 
00251     // Get acceleration info for an iterator with "packed" user data.
00252     virtual RslIncrType* getIncrList(unsigned int stride) const = 0;
00253 };
00254 
00257 class RslArg_v2 {
00258 public:
00260     virtual ~RslArg_v2() {};
00261 
00263     virtual bool IsFloat() const = 0;
00264 
00266     virtual bool IsPoint() const = 0;
00267 
00269     virtual bool IsVector() const = 0;
00270 
00272     virtual bool IsColor() const = 0;
00273 
00275     virtual bool IsString() const = 0;
00276 
00278     virtual bool IsMatrix() const = 0;
00279 
00281     virtual bool IsArray() const = 0;
00282 
00284     virtual bool IsVarying() const = 0;
00285 
00288     virtual int GetArrayLength() const = 0;
00289 
00293     virtual unsigned int NumValues() const = 0;
00294 
00298     static unsigned int NumValues(int argc, const class RslArg** argv);
00299 
00306     virtual void GetData(float** data, int* stride) const = 0;
00307 
00308 private:
00309     // Need to call private methods from iterator constructors.
00310     template<typename T> friend class RslIter;
00311     template<typename T> friend class RslArrayIter;
00312 
00313     // Returns (in result parameters) the data pointer and increment list.
00314     virtual void getInfo(float** data, RslIncrType** incrList,
00315                  bool* isVarying) const = 0;
00316 
00317     // Returns (in result parameters) the data pointer, increment list, and
00318     // array length.
00319     virtual void getArrayInfo(float** data,
00320                       RslIncrType** incrList, int* arrayLength,
00321                       bool* isVarying) const = 0;
00322 };
00323 
00324 
00327 class RslArg_v3 : public RslArg_v2
00328 {
00329 public:
00331     virtual bool IsNormal() const = 0;
00332 
00337     virtual bool IsWriteable() const = 0;
00338 
00340     virtual ~RslArg_v3() {};
00341 };
00342 
00343 
00392 class RslArg : public RslArg_v3
00393 {
00394 public:
00396     virtual bool IsStruct() const = 0;
00397 
00400     virtual const char* GetName() const = 0;
00401 
00403     virtual bool IsResizable() const = 0;
00404 
00406     virtual class RslResizer* GetResizer() const = 0;
00407 
00409     virtual ~RslArg() {};
00410 
00411 private:
00412     // Need to call private methods from various constructors.
00413     friend class RslStruct;
00414     friend class RslStructArray;
00415     template<typename T> friend class RslArrayIter;
00416 
00417 
00418     // Get struct data pointer, along with array length and stride.
00419     virtual float* getStructData(int* length,
00420                                  unsigned int* stride) const = 0;
00421 
00422     // Get struct info.  Returns RslArgs for struct members.
00423     virtual const RslArg** getStructInfo(float* data,
00424                                          const char** name, 
00425                                          unsigned int* numMembers) const = 0;
00426 
00427     // Returns (in result parameters) the data pointer, increment list, and
00428     // array length/capacity.
00429     virtual void getArrayInfo(float** data,
00430                               RslIncrType** incrList, int* arrayLength,
00431                               int* arrayCapacity, bool* isVarying) const = 0;
00432 
00433     // Obsolete version of getArrayInfo, which doesn't return capacity.
00434     virtual void getArrayInfo(float** data,
00435                               RslIncrType** incrList, int* arrayLength,
00436                               bool* isVarying) const = 0;
00437 };
00438 
00439 
00440 // Implementation of RslArg_v2::NumValues must follow definition of RslArg.
00441 inline unsigned int 
00442 RslArg_v2::NumValues(int argc, const RslArg** argv) 
00443 {
00444     int m = 1;
00445     for (int i = 0; i < argc; ++i) {
00446         int n = argv[i]->NumValues();
00447         if (n > m)
00448             m = n;
00449     }
00450     return m;
00451 }
00452 
00453 
00481 template<typename T>
00482 class RslIter {
00483 public:
00485     RslIter(const RslArg* arg)
00486     {
00487         arg->getInfo(&m_data, &m_incrList, &m_isVarying);
00488     }
00489 
00492     RslIter(const T* data, const RslContext* ctx) :
00493         m_data((float*) data),
00494         m_incrList(ctx->getIncrList(0)),
00495         m_isVarying(false)
00496     {
00497     }
00498 
00502     T& operator*() { return *((T*) m_data); }
00503 
00507     const T& operator*() const { return *((T*) m_data); }
00508 
00512     RslIter<T>& operator++() 
00513     {
00514         m_data += *m_incrList;
00515         ++m_incrList;
00516         return *this;
00517     };
00518 
00523     RslIter<T> operator++(int) 
00524     {
00525         RslIter<T> temp = *this;                // Copy before increment.
00526         ++*this;                        // Increment.
00527         return temp;                    // Return old value.
00528     };
00529 
00532     bool IsVarying() const { return m_isVarying; }
00533 
00534 private:
00535     // Current data pointer.
00536     float* m_data;
00537 
00538     // Current increment list, which gives fast access to next active point.
00539     RslIncrType* m_incrList;
00540 
00541     // True if the iterator is varying.
00542     bool m_isVarying;
00543 };
00544     
00545 
00547 template<>
00548 inline RslIter<RtMatrix>&
00549 RslIter<RtMatrix>::operator++() 
00550 {
00551     m_data += *m_incrList * 16;
00552     ++m_incrList;
00553     return *this;
00554 }
00555 
00556 
00600 template<typename T>
00601 class RslArrayIter {
00602 public:
00604     RslArrayIter(const RslArg* arg)
00605     {
00606         arg->getArrayInfo(&m_data, &m_incrList, &m_length, &m_capacity, 
00607                           &m_isVarying);
00608     }
00609 
00612     RslArrayIter(const T* data, int length, const RslContext* ctx) :
00613         m_data((float*) data),
00614         m_incrList(ctx->getIncrList(0)),
00615         m_length(length),
00616         m_capacity(length),
00617         m_isVarying(false)
00618     {
00619     }
00620 
00623     T* operator*() { return (T*) m_data; }
00624 
00627     T&  operator[](int x) 
00628     {
00629         assert(x >= 0 && x < m_length);
00630         return ((T*)m_data)[x]; 
00631     }
00632 
00636     RslArrayIter<T>& operator++() 
00637     {
00638         m_data += *m_incrList * m_capacity;
00639         ++m_incrList;
00640         return *this;
00641     };
00642 
00647     RslArrayIter<T> operator++(int) 
00648     {
00649         RslArrayIter<T> temp = *this;   // Copy before increment.
00650         ++*this;                        // Increment.
00651         return temp;                    // Return old value.
00652     };
00653         
00656     bool IsVarying() const { return m_isVarying; }
00657 
00659     int GetLength() const { return m_length; }
00660         
00662     int GetCapacity() const { return m_capacity; }
00663         
00664 private:
00665     // Current data pointer.
00666     float* m_data;
00667 
00668     // Current increment list, which gives fast access to next active point.
00669     RslIncrType* m_incrList;
00670 
00671     // Array length.
00672     int m_length;
00673 
00674     // Array capacity, which is the same as the length unless it's resizable.
00675     int m_capacity;
00676 
00677     // True if the iterator is varying.
00678     bool m_isVarying;
00679 };
00680 
00681 
00683 template<>
00684 inline RslArrayIter<RtMatrix>&
00685 RslArrayIter<RtMatrix>::operator++() 
00686 {
00687     m_data += *m_incrList * 16 * m_length;
00688     ++m_incrList;
00689     return *this;
00690 }
00691 
00692 typedef RslIter<RtFloat>        RslFloatIter;        
00693 typedef RslIter<RtString>       RslStringIter;       
00694 typedef RslIter<RtColor>        RslColorIter;        
00695 typedef RslIter<RtVector>       RslVectorIter;       
00696 typedef RslIter<RtNormal>       RslNormalIter;       
00697 typedef RslIter<RtPoint>        RslPointIter;        
00698 typedef RslIter<RtMatrix>       RslMatrixIter;       
00699 
00700 typedef RslArrayIter<RtFloat>   RslFloatArrayIter;   
00701 typedef RslArrayIter<RtString>  RslStringArrayIter;  
00702 typedef RslArrayIter<RtColor>   RslColorArrayIter;   
00703 typedef RslArrayIter<RtVector>  RslVectorArrayIter;  
00704 typedef RslArrayIter<RtNormal>  RslNormalArrayIter;  
00705 typedef RslArrayIter<RtPoint>   RslPointArrayIter;    
00706 typedef RslArrayIter<RtMatrix>  RslMatrixArrayIter;   
00707 
00755 class RslResizer {
00756 public:
00758     virtual unsigned int GetLength() const = 0;
00759 
00761     virtual unsigned int GetCapacity() const = 0;
00762 
00768     virtual void Resize(unsigned int n) = 0;
00769 
00775     virtual void Reserve(unsigned int n) = 0;
00776 
00777 protected:
00779     virtual ~RslResizer() { }
00780 };
00781 
00782 
00824 class RslStruct {
00825 public:
00827     RslStruct(const RslArg* arg)
00828     {
00829         assert(arg->IsStruct() && !arg->IsArray());
00830         int length;
00831         unsigned int stride;
00832         float* data = arg->getStructData(&length, &stride);
00833         m_members = arg->getStructInfo(data, &m_name, &m_numMembers);
00834     }
00835 
00837     const char* GetName() const { return m_name; }
00838 
00840     unsigned int GetNumMembers() const { return m_numMembers; }
00841 
00843     const RslArg* operator[](int i) const
00844     {
00845         assert(i >= 0 && (unsigned int) i < m_numMembers); 
00846         return m_members[i];
00847     }
00848 
00849 private:
00850     // Struct members
00851     const RslArg** m_members;
00852 
00853     // Struct type name.
00854     const char* m_name;
00855 
00856     // Number of struct members.
00857     unsigned int m_numMembers;
00858 
00859     // RslStructArray calls a private constructor.
00860     friend class RslStructArray;
00861 
00862     // Private constructor
00863     RslStruct(const RslArg* arg, float* data)
00864     {
00865         m_members = arg->getStructInfo(data, &m_name, &m_numMembers);
00866     }
00867 };
00868 
00869 
00887 class RslStructArray {
00888 public:
00890     RslStructArray(const RslArg* arg)
00891     {
00892         assert(arg->IsArray() && arg->IsStruct());
00893         m_array = arg;
00894         m_data = arg->getStructData(&m_length, &m_stride);
00895     }
00896 
00898     RslStruct operator[](int i) const
00899     {
00900         assert(i >= 0 && i < m_length);
00901         return RslStruct(m_array, m_data + i * m_stride);
00902     }
00903 
00905     int GetLength() const { return m_length; }
00906 
00907 private:
00908     // RslArg that represents the array.
00909     const RslArg* m_array;
00910 
00911     // Data pointer.
00912     float* m_data;
00913 
00914     // Array length.
00915     int m_length;
00916 
00917     // Stride for array index calculations (in words)
00918     unsigned int m_stride;
00919 };
00920 
00921 
00926 typedef void (*RslVoidFunc)(RixContext* context);
00927 
00968 struct RslFunction {
00970     const char *m_prototype;
00971 
00973     RslEntryFunc m_entry;
00974 
00976     RslVoidFunc m_initFunc;
00977 
00979     RslVoidFunc m_cleanupFunc;
00980     
00982     RslVoidFunc m_renderBeginFunc;
00983     
00985     RslVoidFunc m_renderEndFunc;
00986 };
00987 
01010 struct RslFunctionTable {
01012     const RslFunction* m_functions;
01013 
01015     const char m_version;
01016 
01018     RslVoidFunc m_initFunc;
01019 
01021     RslVoidFunc m_cleanupFunc;
01022 
01024     RslFunctionTable(const RslFunction* functions,
01025                      RslVoidFunc init = NULL, RslVoidFunc cleanup = NULL) :
01026         m_functions(functions),
01027         m_version(RSL_PLUGIN_VERSION),
01028         m_initFunc(init),
01029         m_cleanupFunc(cleanup)
01030     {
01031     }
01032 };
01033 
01034 #endif /* defined RSLPLUGIN_H */