Caelum
FastGpuParamRef.h
Go to the documentation of this file.
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2009 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef CAELUM__FAST_GPU_PARAM_REF_H
22 #define CAELUM__FAST_GPU_PARAM_REF_H
23 
24 #include "CaelumPrerequisites.h"
25 
26 namespace Caelum
27 {
33  #define CAELUM_DEBUG_PARAM_REF OGRE_DEBUG_MODE
34 
49  class CAELUM_EXPORT FastGpuParamRef
50  {
51  public:
53  FastGpuParamRef(): mPhysicalIndex(InvalidPhysicalIndex)
54  {
55  // mParams needs no initialization; SharedPtrs start as 0.
56  }
57 
59  FastGpuParamRef(Ogre::GpuProgramParametersSharedPtr paramsPtr, const Ogre::String& name);
60 
71  void bind(
72  Ogre::GpuProgramParametersSharedPtr paramsPtr,
73  const Ogre::String& name,
74  bool throwIfNotFound = false);
75 
81  void unbind();
82 
84  inline bool isBound() const { return mPhysicalIndex != InvalidPhysicalIndex; }
85 
87  inline size_t getPhysicalIndex () const { return mPhysicalIndex; }
88 
89  protected:
98  template<typename ArgumentT>
99  inline void doSet(const Ogre::GpuProgramParametersSharedPtr& params, ArgumentT arg) const {
100  #if CAELUM_DEBUG_PARAM_REF
101  assert(params.getPointer() == mParams.getPointer());
102  #endif
103  assert(!params.isNull());
104  if (mPhysicalIndex != InvalidPhysicalIndex) {
105  params->_writeRawConstant(mPhysicalIndex, arg);
106  }
107  }
108 
109  template<typename ArgumentT>
110  inline void doSet(const Ogre::GpuProgramParametersSharedPtr& params, ArgumentT arg, size_t count) const {
111  #if CAELUM_DEBUG_PARAM_REF
112  assert(params.getPointer() == mParams.getPointer());
113  #endif
114  assert(!params.isNull());
115  if (mPhysicalIndex != InvalidPhysicalIndex) {
116  params->_writeRawConstant(mPhysicalIndex, arg, count);
117  }
118  }
119 
120  public:
122  inline void set(const Ogre::GpuProgramParametersSharedPtr& params, int val) const { doSet<int>(params, val); }
124  inline void set(const Ogre::GpuProgramParametersSharedPtr& params, Ogre::Real val) const { doSet<Ogre::Real>(params, val); }
126  inline void set(const Ogre::GpuProgramParametersSharedPtr& params, const Ogre::Vector3& val) const { doSet<const Ogre::Vector3&>(params, val); }
128  inline void set(const Ogre::GpuProgramParametersSharedPtr& params, const Ogre::Vector4& val) const { doSet<const Ogre::Vector4&>(params, val); }
130  inline void set(const Ogre::GpuProgramParametersSharedPtr& params, const Ogre::ColourValue& val) const { doSet<const Ogre::ColourValue&>(params, val); }
132  inline void set(const Ogre::GpuProgramParametersSharedPtr& params, const Ogre::Matrix4& val) const { doSet<const Ogre::Matrix4*>(params, &val, 1); }
133 
134  private:
135  #if CAELUM_DEBUG_PARAM_REF
136  Ogre::GpuProgramParametersSharedPtr mParams;
137  #endif
138  static const size_t InvalidPhysicalIndex = 0xFFFFFFFF;
139  size_t mPhysicalIndex;
140  };
141 }
142 
143 #endif /* CAELUM__FAST_GPU_PARAM_REF_H */
size_t getPhysicalIndex() const
Return the physical index. Only valid if this->isBound().
void doSet(const Ogre::GpuProgramParametersSharedPtr &params, ArgumentT arg) const
Set the value.
bool isBound() const
Return if this param ref is bound to an actual param.
FastGpuParamRef()
Default constructor. Starts as unbound.
An optimized reference to a gpu shared parameter.
Caelum namespace.
Definition: Astronomy.cpp:24