OgreInstanceManager.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __InstanceManager_H__
29 #define __InstanceManager_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreMesh.h"
33 #include "OgreRenderOperation.h"
34 #include "OgreHeaderPrefix.h"
35 
36 namespace Ogre
37 {
64  {
65  public:
67  {
72  InstancingTechniquesCount
73  };
74 
77  {
79  CAST_SHADOWS = 0,
82 
83  NUM_SETTINGS
84  };
85 
86  private:
88  {
89  //These are all per material
90  bool setting[NUM_SETTINGS];
91 
93  {
94  setting[CAST_SHADOWS] = true;
95  setting[SHOW_BOUNDINGBOX] = false;
96  }
97  };
98 
99  typedef vector<InstanceBatch*>::type InstanceBatchVec; //vec[batchN] = Batch
100  typedef map<String, InstanceBatchVec>::type InstanceBatchMap; //map[materialName] = Vec
101 
103 
104  const String mName; //Not the name of the mesh
107  size_t mIdCount;
108 
110 
112 
116  unsigned short mSubMeshIdx;
117 
120 
122  unsigned char mNumCustomParams; //Number of custom params per instance.
123 
127  inline InstanceBatch* getFreeBatch( const String &materialName );
128 
138  InstanceBatch* buildNewBatch( const String &materialName, bool firstTime );
139 
142  void defragmentBatches( bool optimizeCull, vector<InstancedEntity*>::type &entities,
143  vector<Ogre::Vector4>::type &usedParams,
144  InstanceBatchVec &fragmentedBatches );
145 
149  void applySettingToBatches( BatchSettingId id, bool value, const InstanceBatchVec &container );
150 
154  void unshareVertices(const Ogre::MeshPtr &mesh);
155 
156  public:
157  InstanceManager( const String &customName, SceneManager *sceneManager,
158  const String &meshName, const String &groupName,
159  InstancingTechnique instancingTechnique, uint16 instancingFlags,
160  size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup = false);
161  virtual ~InstanceManager();
162 
163  const String& getName() const { return mName; }
164 
165  SceneManager* getSceneManager() const { return mSceneManager; }
166 
172  void setInstancesPerBatch( size_t instancesPerBatch );
173 
182  void setMaxLookupTableInstances( size_t maxLookupTableInstances );
183 
202  void setNumCustomParams( unsigned char numCustomParams );
203 
204  unsigned char getNumCustomParams() const
205  { return mNumCustomParams; }
206 
209  { return mInstancingTechnique; }
210 
222  size_t getMaxOrBestNumInstancesPerBatch( String materialName, size_t suggestedSize, uint16 flags );
223 
226 
234 
258  void defragmentBatches( bool optimizeCulling );
259 
275  void setSetting( BatchSettingId id, bool enabled, const String &materialName = StringUtil::BLANK );
276 
278  bool getSetting( BatchSettingId id, const String &materialName ) const;
279 
283  bool hasSettings( const String &materialName ) const
284  { return mBatchSettings.find( materialName ) != mBatchSettings.end(); }
285 
287  void setBatchesAsStaticAndUpdate( bool bStatic );
288 
292  void _addDirtyBatch( InstanceBatch *dirtyBatch );
293 
296 
299 
302  { return InstanceBatchMapIterator( mInstanceBatches.begin(), mInstanceBatches.end() ); }
303 
311  {
312  InstanceBatchMap::const_iterator it = mInstanceBatches.find( materialName );
313  if(it != mInstanceBatches.end())
314  return InstanceBatchIterator( it->second.begin(), it->second.end() );
315  else
316  OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Cannot create instance batch iterator. "
317  "Material " + materialName + " cannot be found.", "InstanceManager::getInstanceBatchIterator");
318  }
319  };
320 } // namespace Ogre
321 
322 #include "OgreHeaderSuffix.h"
323 
324 #endif // __InstanceManager_H__
OgreHeaderSuffix.h
Ogre::InstanceManager::getFreeBatch
InstanceBatch * getFreeBatch(const String &materialName)
Finds a batch with at least one free instanced entity we can use.
Ogre::InstanceManager::BatchSettings
Definition: OgreInstanceManager.h:88
Ogre::Exception::ERR_INVALID_STATE
@ ERR_INVALID_STATE
Definition: OgreException.h:102
Ogre::InstanceManager::getInstanceBatchIterator
InstanceBatchIterator getInstanceBatchIterator(const String &materialName) const
Get non-updateable iterator over instance batches for given material.
Definition: OgreInstanceManager.h:310
Ogre::InstanceManager::~InstanceManager
virtual ~InstanceManager()
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:59
Ogre
Definition: OgreAndroidLogListener.h:35
Ogre::InstanceManager::mMaxLookupTableInstances
size_t mMaxLookupTableInstances
Definition: OgreInstanceManager.h:121
Ogre::map
Definition: OgrePrerequisites.h:534
OgreMesh.h
Ogre::InstanceManager::InstanceManager
InstanceManager(const String &customName, SceneManager *sceneManager, const String &meshName, const String &groupName, InstancingTechnique instancingTechnique, uint16 instancingFlags, size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup=false)
Ogre::InstanceManager::getSceneManager
SceneManager * getSceneManager() const
Definition: OgreInstanceManager.h:165
Ogre::InstanceManager::mInstancingTechnique
InstancingTechnique mInstancingTechnique
Definition: OgreInstanceManager.h:114
Ogre::InstanceManager::getSetting
bool getSetting(BatchSettingId id, const String &materialName) const
If settings for the given material didn't exist, default value is returned.
Ogre::InstanceManager::mName
const String mName
Definition: OgreInstanceManager.h:104
Ogre::InstanceManager::mSubMeshIdx
unsigned short mSubMeshIdx
Definition: OgreInstanceManager.h:116
Ogre::InstanceManager
This is the main starting point for the new instancing system.
Definition: OgreInstanceManager.h:64
Ogre::InstanceManager::getMaxOrBestNumInstancesPerBatch
size_t getMaxOrBestNumInstancesPerBatch(String materialName, size_t suggestedSize, uint16 flags)
Calculates the maximum (or the best amount, depending on flags) of instances per batch given the sugg...
Ogre::InstanceManager::mMeshReference
MeshPtr mMeshReference
Definition: OgreInstanceManager.h:105
Ogre::uint16
unsigned short uint16
Definition: OgrePlatform.h:360
Ogre::InstanceManager::HWInstancingVTF
@ HWInstancingVTF
Needs SM 3.0+, HW instancing support & VTF.
Definition: OgreInstanceManager.h:71
Ogre::InstanceManager::defragmentBatches
void defragmentBatches(bool optimizeCulling)
After creating many entities (which turns in many batches) and then removing entities that are in the...
Ogre::InstanceManager::setMaxLookupTableInstances
void setMaxLookupTableInstances(size_t maxLookupTableInstances)
Sets the size of the lookup table for techniques supporting bone lookup table.
Ogre::InstanceManager::InstancingTechnique
InstancingTechnique
Definition: OgreInstanceManager.h:67
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::InstanceManager::InstanceBatchMapIterator
ConstMapIterator< InstanceBatchMap > InstanceBatchMapIterator
Definition: OgreInstanceManager.h:297
Ogre::InstanceManager::_updateDirtyBatches
void _updateDirtyBatches(void)
Called by SceneManager when we told it we have at least one dirty batch.
Ogre::InstanceManager::mSceneManager
SceneManager * mSceneManager
Definition: OgreInstanceManager.h:119
Ogre::InstanceManager::getNumCustomParams
unsigned char getNumCustomParams() const
Definition: OgreInstanceManager.h:204
Ogre::InstanceManager::BatchSettingsMap
map< String, BatchSettings >::type BatchSettingsMap
Definition: OgreInstanceManager.h:102
Ogre::InstanceManager::setBatchesAsStaticAndUpdate
void setBatchesAsStaticAndUpdate(bool bStatic)
Tells this batch to stop updating animations, positions, rotations, and display all it's active insta...
Ogre::InstanceManager::createInstancedEntity
InstancedEntity * createInstancedEntity(const String &materialName)
Creates an InstancedEntity based on an existing InstanceManager (.
Ogre::InstanceManager::unshareVertices
void unshareVertices(const Ogre::MeshPtr &mesh)
Called when we you use a mesh which has shared vertices, the function creates separate vertex/index b...
Ogre::StringUtil::BLANK
static const String BLANK
Constant blank string, useful for returning by ref where local does not exist.
Definition: OgreString.h:196
Ogre::vector::type
std::vector< T, A > type
Definition: OgrePrerequisites.h:494
OgreRenderOperation.h
OgreHeaderPrefix.h
Ogre::InstanceManager::InstanceBatchVec
vector< InstanceBatch * >::type InstanceBatchVec
Definition: OgreInstanceManager.h:99
Ogre::InstanceManager::mNumCustomParams
unsigned char mNumCustomParams
Definition: OgreInstanceManager.h:122
Ogre::InstanceManager::getInstancingTechnique
InstancingTechnique getInstancingTechnique() const
Definition: OgreInstanceManager.h:208
Ogre::SceneManager
Manages the organisation and rendering of a 'scene' i.e.
Definition: OgreSceneManager.h:144
Ogre::InstanceManager::mSharedRenderOperation
RenderOperation mSharedRenderOperation
Definition: OgreInstanceManager.h:111
Ogre::InstanceManager::HWInstancingBasic
@ HWInstancingBasic
Needs SM 3.0+ and HW instancing support.
Definition: OgreInstanceManager.h:70
Ogre::InstanceManager::_addDirtyBatch
void _addDirtyBatch(InstanceBatch *dirtyBatch)
Called by an InstanceBatch when it requests their bounds to be updated for proper culling.
OgrePrerequisites.h
Ogre::InstanceManager::setSetting
void setSetting(BatchSettingId id, bool enabled, const String &materialName=StringUtil::BLANK)
Applies a setting for all batches using the same material_ existing ones and those that will be creat...
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::InstanceManager::ShaderBased
@ ShaderBased
Any SM 2.0+.
Definition: OgreInstanceManager.h:68
Ogre::InstanceManager::getName
const String & getName() const
Definition: OgreInstanceManager.h:163
Ogre::InstanceManager::mDirtyBatches
InstanceBatchVec mDirtyBatches
Definition: OgreInstanceManager.h:109
Ogre::InstanceManager::TextureVTF
@ TextureVTF
Needs Vertex Texture Fetch & SM 3.0+.
Definition: OgreInstanceManager.h:69
Ogre::InstanceManager::mInstancingFlags
uint16 mInstancingFlags
Definition: OgreInstanceManager.h:115
Ogre::SharedPtr< Mesh >
Ogre::InstanceManager::cleanupEmptyBatches
void cleanupEmptyBatches(void)
This function can be useful to improve CPU speed after having too many instances created,...
Ogre::InstanceManager::applySettingToBatches
void applySettingToBatches(BatchSettingId id, bool value, const InstanceBatchVec &container)
Ogre::InstanceManager::buildNewBatch
InstanceBatch * buildNewBatch(const String &materialName, bool firstTime)
Called when batches are fully exhausted (can't return more instances) so a new batch is created.
Ogre::InstanceManager::mInstanceBatches
InstanceBatchMap mInstanceBatches
Definition: OgreInstanceManager.h:106
Ogre::InstanceManager::InstanceBatchMap
map< String, InstanceBatchVec >::type InstanceBatchMap
Definition: OgreInstanceManager.h:100
Ogre::InstanceManager::SHOW_BOUNDINGBOX
@ SHOW_BOUNDINGBOX
Makes each batch to display it's bounding box. Useful for debugging or profiling.
Definition: OgreInstanceManager.h:81
Ogre::InstanceManager::setNumCustomParams
void setNumCustomParams(unsigned char numCustomParams)
Sets the number of custom parameters per instance.
Ogre::InstanceManager::mInstancesPerBatch
size_t mInstancesPerBatch
Definition: OgreInstanceManager.h:113
Ogre::InstanceManager::BatchSettingId
BatchSettingId
Values to be used in setSetting() & BatchSettings::setting.
Definition: OgreInstanceManager.h:77
Ogre::InstanceBatch
InstanceBatch forms part of the new Instancing system This is an abstract class that must be derived ...
Definition: OgreInstanceBatch.h:89
Ogre::ConstMapIterator
Concrete IteratorWrapper for const access to the underlying key-value container.
Definition: OgreIteratorWrapper.h:352
Ogre::InstanceManager::setInstancesPerBatch
void setInstancesPerBatch(size_t instancesPerBatch)
Raises an exception if trying to change it after creating the first InstancedEntity.
Ogre::InstanceManager::defragmentBatches
void defragmentBatches(bool optimizeCull, vector< InstancedEntity * >::type &entities, vector< Ogre::Vector4 >::type &usedParams, InstanceBatchVec &fragmentedBatches)
Ogre::InstanceManager::getInstanceBatchMapIterator
InstanceBatchMapIterator getInstanceBatchMapIterator(void) const
Get non-updateable iterator over instance batches per material.
Definition: OgreInstanceManager.h:301
Ogre::InstanceManager::hasSettings
bool hasSettings(const String &materialName) const
Returns true if settings were already created for the given material name.
Definition: OgreInstanceManager.h:283
OGRE_EXCEPT
#define OGRE_EXCEPT(num, desc, src)
Definition: OgreException.h:334
Ogre::vector
Definition: OgrePrerequisites.h:492
Ogre::InstanceManager::InstanceBatchIterator
ConstVectorIterator< InstanceBatchVec > InstanceBatchIterator
Definition: OgreInstanceManager.h:298
Ogre::InstanceManager::mIdCount
size_t mIdCount
Definition: OgreInstanceManager.h:107
Ogre::InstancedEntity
Definition: OgreInstancedEntity.h:78
Ogre::InstanceManager::mBatchSettings
BatchSettingsMap mBatchSettings
Definition: OgreInstanceManager.h:118
Ogre::InstanceManager::BatchSettings::BatchSettings
BatchSettings()
Definition: OgreInstanceManager.h:92
Ogre::RenderOperation
'New' rendering operation using vertex buffers.
Definition: OgreRenderOperation.h:45
Ogre::ConstVectorIterator
Concrete IteratorWrapper for const access to the underlying container.
Definition: OgreIteratorWrapper.h:217

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.