Main Page   Compound List   File List   Compound Members   File Members  

cgbinding.h

Go to the documentation of this file.
00001 #ifndef CGBINDING_H
00002 
00003 #define CGBINDING_H
00004 
00005 /********************************************************************************
00006 ** Include this header file into your application and inherit the base class
00007 ** CgBinding to provide a single common interface mechanism for loading Cg
00008 ** programs and performing run time binding of data assets.
00009 **
00010 ** Written by John W. Ratcliff
00011 ** July 18, 2002
00012 ** Uses Doxygen style documentation header blocks.
00013 *********************************************************************************/
00014 
00015 #pragma warning( disable:4786 )  // warning C4503: '  ' : decorated name length exceeded, name was truncated
00016 
00017 #include "cgbindingtype.h"
00018 
00019 #include <vector>
00020 #include <map>
00021 
00022 class CgBinding;
00023 class CgVertexShader;
00024 class CgPixelShader;
00025 
00045 class CgVertexBind
00046 {
00047 public:
00048   CgVertexBind(void) { };
00049 
00050   CgVertexBind(VertexBindingType b,unsigned int handle)
00051   {
00052     mBind = b;
00053     mHandle = handle;
00054   };
00055 
00056   bool Activate(CgBinding *bindings,unsigned int program_handle,void *context1,void *context2);
00057 
00058 private:
00059   VertexBindingType mBind;    /* Type of vertex binding operation */
00060   unsigned int      mHandle;  /* Handle to the Cg binding point iterator */
00061 };
00062 
00063 // STL vector of binding points
00064 typedef std::vector< CgVertexBind > CgVertexBindVector;
00065 
00084 class CgVertexShader
00085 {
00086 public:
00087 
00088   CgVertexShader(int guid,VertexBindingFormat format,unsigned int handle)
00089   {
00090     mGuid   = guid;
00091     mFormat = format;
00092     mHandle = handle;
00093   }
00094 
00095   int  GetGuid(void) const { return mGuid; };
00096 
00097   void AddBinding(VertexBindingType type,unsigned int bind_handle)
00098   {
00099     CgVertexBind b(type,bind_handle);
00100     mBindings.push_back(b);
00101   }
00102 
00103   bool Activate(void);
00104 
00105   bool ActivateBindings(CgBinding *bindings,void *context1,void *context2);
00106 
00107 private:
00108   int                 mGuid;      /* Our Guid Id number */
00109   unsigned int        mHandle;    /* Handle to our CgShader program. */
00110   VertexBindingFormat mFormat;    /* The format our vertex data is in. */
00111   CgVertexBindVector  mBindings;  /* STL vector of all of our vertex binding points. */
00112 };
00113 
00114 typedef std::map< int , CgVertexShader * > CgVertexShaderMap;
00115 
00116 
00136 class CgPixelBind
00137 {
00138 public:
00139   CgPixelBind(void) { };
00140 
00141   CgPixelBind(PixelBindingType b,unsigned int handle)
00142   {
00143     mBind = b;
00144     mHandle = handle;
00145   };
00146 
00147   bool Activate(CgBinding *bindings,unsigned int program_handle,void *context1,void *context2);
00148 
00149 private:
00150   PixelBindingType mBind;     /* Type of bind this is. */
00151   unsigned int      mHandle;  /* Cg bind iteration handle. */
00152 };
00153 
00154 // STL vector of binding points
00155 typedef std::vector< CgPixelBind > CgPixelBindVector;
00156 
00157 
00172 class CgPixelShader
00173 {
00174 public:
00175 
00176   CgPixelShader(int guid,unsigned int handle)
00177   {
00178     mGuid   = guid;
00179     mHandle = handle;
00180   }
00181 
00182   int  GetGuid(void) const { return mGuid; }; /* Return our assigned Guid */
00183 
00184   void AddBinding(PixelBindingType type,unsigned int bind_handle) /* Add a pixel binding point */
00185   {
00186     CgPixelBind b(type,bind_handle);
00187     mBindings.push_back(b);
00188   }
00189 
00190   bool Activate(void);
00191 
00192   bool ActivateBindings(CgBinding *bindings,void *context1,void *context2);
00193 
00194 private:
00195   int                 mGuid;    /* Our Id numer. */
00196   unsigned int        mHandle;  /* Handle to our CgShader program. */
00197   CgPixelBindVector   mBindings; /* STL vector of our binding points */
00198 };
00199 
00200 typedef std::map< int , CgPixelShader * > CgPixelShaderMap;
00201 
00221 class CgBinding
00222 {
00223 public:
00224 
00225   CgBinding(void);
00226   ~CgBinding(void);
00227 
00228 
00229   // validate that our application knows how to handle arguments of this type.
00230   // Pure virtual function, application must provided this service
00231   virtual bool ValidateVertexBindingType(VertexBindingType type) =0;
00232 
00233   virtual void * GetVertexBindingType(VertexBindingType type,       // keyword we need the results for
00234                                       void *context1,               // up to 2 contexts for this shader
00235                                       void *context2) = 0;          //
00236 
00237   // validate that our application knows how to handle arguments of this type.
00238   // Pure virtual function, application must provided this service
00239   virtual bool ValidatePixelBindingType(VertexBindingType type) =0;
00240 
00241   virtual void * GetPixelBindingType(PixelBindingType type,       // keyword we need the results for
00242                                      void *context1,               // up to 2 contexts for this shader
00243                                      void *context2) = 0;          //
00244 
00245 
00246   // convenience function, converts bind name, including all possible aliases to logical vertexbindingtype keyword.
00247   VertexBindingType AsciiToVertexBindingType(const char *bind_name);
00248   PixelBindingType  AsciiToPixelBindingType(const char *bind_name);
00249 
00250   CgVertexShader * CreateVertexShader(const char *program,VertexBindingFormat type,unsigned int device);
00251   CgPixelShader  * CreatePixelShader(const char *program,unsigned int device);
00252 
00253   bool       ReleaseVertexShader(CgVertexShader *shader); // this shader is toast.
00254   bool       ReleasePixelShader(CgPixelShader *shader); // this shader is toast.
00255 
00256   void * GetVertexAttributes(VertexBindingFormat type);
00257 
00258   const char * GetLastListing(void) const { return mLastListing; }; /* Return last compilation output */
00259   const char * GetLastError(void)   const { return mLastError; };   /* Return reason for last error. */
00260 
00261 private:
00262   int                 mGuid;                  /* Just a guid we keep around to keep track of shaders we allocate. */
00263   void               *mContextContainer;      /* Cg container class for all shaders loaded. */
00264   CgVertexShaderMap   mVertexShaders;         /* STL map, indexed by Guid, of all loaded vertex shaders */
00265   CgPixelShaderMap    mPixelShaders;          /* STL map, indexed by Guid, of all loaded pixel shaders */
00266   const char         *mLastListing;           /* Pointer to last compilation output. */
00267   char                mLastError[1024];       /* Our reason we failed when attempting to load a shader */
00268 };
00269 
00270 
00271 #endif

Generated on Sun Jul 21 13:35:16 2002 for CgBinding by doxygen1.2.17