gfxGLShader.h

Engine/source/gfx/gl/gfxGLShader.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GFXGLSHADER_H_
 25#define _GFXGLSHADER_H_
 26
 27#include "core/util/refBase.h"
 28#include "gfx/gfxShader.h"
 29#include "gfx/gl/tGL/tGL.h"
 30#include "core/util/tSignal.h"
 31#include "core/util/tDictionary.h"
 32
 33class GFXGLShaderConstHandle;
 34class FileStream;
 35class GFXGLShaderConstBuffer;
 36
 37class GFXGLShader : public GFXShader
 38{
 39   typedef Map<String, GFXGLShaderConstHandle*> HandleMap;
 40public:
 41   GFXGLShader();
 42   virtual ~GFXGLShader();
 43   
 44   /// @name GFXShader interface
 45   /// @{
 46   virtual GFXShaderConstHandle* getShaderConstHandle(const String& name);
 47   virtual GFXShaderConstHandle* findShaderConstHandle(const String& name);
 48
 49   /// Returns our list of shader constants, the material can get this and just set the constants it knows about
 50   virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
 51
 52   /// Returns the alignment value for constType
 53   virtual U32 getAlignmentValue(const GFXShaderConstType constType) const; 
 54
 55   virtual GFXShaderConstBufferRef allocConstBuffer();
 56
 57   /// @}
 58   
 59   /// @name GFXResource interface
 60   /// @{
 61   virtual void zombify();
 62   virtual void resurrect() { reload(); }
 63   virtual const String describeSelf() const;
 64   /// @}      
 65
 66   /// Activates this shader in the GL context.
 67   void useProgram();
 68   
 69protected:
 70
 71   friend class GFXGLShaderConstBuffer;
 72   friend class GFXGLShaderConstHandle;
 73   
 74   virtual bool _init();   
 75
 76   bool initShader(  const Torque::Path &file, 
 77                     bool isVertex, 
 78                     const Vector<GFXShaderMacro> &macros );
 79
 80   void clearShaders();
 81   void initConstantDescs();
 82   void initHandles();
 83   void setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer);
 84   
 85   static char* _handleIncludes( const Torque::Path &path, FileStream *s );
 86
 87   static bool _loadShaderFromStream(  GLuint shader, 
 88                                       const Torque::Path& path, 
 89                                       FileStream* s, 
 90                                       const Vector<GFXShaderMacro>& macros );
 91
 92   /// @name Internal GL handles
 93   /// @{
 94   GLuint mVertexShader;
 95   GLuint mPixelShader;
 96   GLuint mProgram;
 97   /// @}
 98    
 99   Vector<GFXShaderConstDesc> mConstants;
100   U32 mConstBufferSize;
101   U8* mConstBuffer;
102   HandleMap mHandles;
103   Vector<GFXGLShaderConstHandle*> mValidHandles;
104};
105
106class GFXGLShaderConstBuffer : public GFXShaderConstBuffer
107{
108public:
109   GFXGLShaderConstBuffer(GFXGLShader* shader, U32 bufSize, U8* existingConstants);
110   ~GFXGLShaderConstBuffer();
111   
112   /// Called by GFXGLDevice to activate this buffer.
113   void activate();
114
115   /// Called when the shader this buffer references is reloaded.
116   void onShaderReload( GFXGLShader *shader );
117
118   // GFXShaderConstBuffer
119   virtual GFXShader* getShader() { return mShader; }
120   virtual void set(GFXShaderConstHandle* handle, const F32 fv);
121   virtual void set(GFXShaderConstHandle* handle, const Point2F& fv);
122   virtual void set(GFXShaderConstHandle* handle, const Point3F& fv);
123   virtual void set(GFXShaderConstHandle* handle, const Point4F& fv);
124   virtual void set(GFXShaderConstHandle* handle, const PlaneF& fv);
125   virtual void set(GFXShaderConstHandle* handle, const LinearColorF& fv);   
126   virtual void set(GFXShaderConstHandle* handle, const S32 f);
127   virtual void set(GFXShaderConstHandle* handle, const Point2I& fv);
128   virtual void set(GFXShaderConstHandle* handle, const Point3I& fv);
129   virtual void set(GFXShaderConstHandle* handle, const Point4I& fv);
130   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv);
131   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv);
132   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv);
133   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv);   
134   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv);
135   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv);
136   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv);
137   virtual void set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv);
138   virtual void set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType = GFXSCT_Float4x4);
139   virtual void set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);   
140
141   // GFXResource
142   virtual const String describeSelf() const;
143   virtual void zombify() {}
144   virtual void resurrect() {}
145
146private:
147
148   friend class GFXGLShader;
149   U8* mBuffer;
150   WeakRefPtr<GFXGLShader> mShader;
151   
152   template<typename ConstType>
153   void internalSet(GFXShaderConstHandle* handle, const ConstType& param);
154   
155   template<typename ConstType>
156   void internalSet(GFXShaderConstHandle* handle, const AlignedArray<ConstType>& fv);
157};
158
159#endif // _GFXGLSHADER_H_
160