OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreHardwareBufferManager.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-2014 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 __HardwareBufferManager__
29 #define __HardwareBufferManager__
30 
31 // Precompiler options
32 #include "OgrePrerequisites.h"
33 
34 #include "OgreSingleton.h"
40 #include "OgreHeaderPrefix.h"
41 
42 namespace Ogre {
61  {
62  public:
67  virtual void licenseExpired(HardwareBuffer* buffer) = 0;
68  };
69 
72  {
73  private:
74  // Pre-blended
77  // Post-blended
82  unsigned short posBindIndex;
83  unsigned short normBindIndex;
86 
87  public:
88  ~TempBlendedBufferInfo(void);
90  void extractFrom(const VertexData* sourceData);
92  void checkoutTempCopies(bool positions = true, bool normals = true);
94  void bindTempCopies(VertexData* targetData, bool suppressHardwareUpload);
96  void licenseExpired(HardwareBuffer* buffer);
98  bool buffersCheckedOut(bool positions = true, bool normals = true) const;
99  };
100 
101 
114  {
117  protected:
131 
132 
137 
138  // Mutexes
139  OGRE_MUTEX(mVertexBuffersMutex);
140  OGRE_MUTEX(mIndexBuffersMutex);
141  OGRE_MUTEX(mUniformBuffersMutex);
142  OGRE_MUTEX(mCounterBuffersMutex);
143  OGRE_MUTEX(mVertexDeclarationsMutex);
144  OGRE_MUTEX(mVertexBufferBindingsMutex);
145 
147  virtual void destroyAllDeclarations(void);
149  virtual void destroyAllBindings(void);
150 
152  virtual VertexDeclaration* createVertexDeclarationImpl(void);
154  virtual void destroyVertexDeclarationImpl(VertexDeclaration* decl);
155 
157  virtual VertexBufferBinding* createVertexBufferBindingImpl(void);
159  virtual void destroyVertexBufferBindingImpl(VertexBufferBinding* binding);
160 
161  public:
162 
164  {
168  BLT_AUTOMATIC_RELEASE
169  };
170 
171  protected:
174  {
175  public:
178  size_t expiredDelay;
182  HardwareVertexBuffer* orig,
183  BufferLicenseType ltype,
184  size_t delay,
187  : originalBufferPtr(orig)
188  , licenseType(ltype)
189  , expiredDelay(delay)
190  , buffer(buf)
191  , licensee(lic)
192  {}
193 
194  };
195 
207  static const size_t UNDER_USED_FRAME_THRESHOLD;
209  static const size_t EXPIRED_DELAY_FRAME_THRESHOLD;
210  // Mutexes
211  OGRE_MUTEX(mTempBuffersMutex);
212 
213 
215  virtual HardwareVertexBufferSharedPtr makeBufferCopy(
216  const HardwareVertexBufferSharedPtr& source,
217  HardwareBuffer::Usage usage, bool useShadowBuffer);
218 
219  public:
221  virtual ~HardwareBufferManagerBase();
255  createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
256  bool useShadowBuffer = false) = 0;
278  createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
279  HardwareBuffer::Usage usage, bool useShadowBuffer = false) = 0;
280 
285  virtual RenderToVertexBufferSharedPtr createRenderToVertexBuffer() = 0;
286 
292  virtual HardwareUniformBufferSharedPtr createUniformBuffer(size_t sizeBytes,
294  bool useShadowBuffer = false, const String& name = "") = 0;
295 
300  virtual HardwareCounterBufferSharedPtr createCounterBuffer(size_t sizeBytes,
302  bool useShadowBuffer = false, const String& name = "") = 0;
303 
305  virtual VertexDeclaration* createVertexDeclaration(void);
307  virtual void destroyVertexDeclaration(VertexDeclaration* decl);
308 
310  virtual VertexBufferBinding* createVertexBufferBinding(void);
312  virtual void destroyVertexBufferBinding(VertexBufferBinding* binding);
313 
319  virtual void registerVertexBufferSourceAndCopy(
320  const HardwareVertexBufferSharedPtr& sourceBuffer,
321  const HardwareVertexBufferSharedPtr& copy);
322 
343  virtual HardwareVertexBufferSharedPtr allocateVertexBufferCopy(
344  const HardwareVertexBufferSharedPtr& sourceBuffer,
345  BufferLicenseType licenseType,
346  HardwareBufferLicensee* licensee,
347  bool copyData = false);
348 
358  virtual void releaseVertexBufferCopy(
359  const HardwareVertexBufferSharedPtr& bufferCopy);
360 
372  virtual void touchVertexBufferCopy(const HardwareVertexBufferSharedPtr& bufferCopy);
373 
383  virtual void _freeUnusedBufferCopies(void);
384 
392  virtual void _releaseBufferCopies(bool forceFreeUnused = false);
393 
404  virtual void _forceReleaseBufferCopies(const HardwareVertexBufferSharedPtr& sourceBuffer);
405 
416  virtual void _forceReleaseBufferCopies(HardwareVertexBuffer* sourceBuffer);
417 
419  void _notifyVertexBufferDestroyed(HardwareVertexBuffer* buf);
421  void _notifyIndexBufferDestroyed(HardwareIndexBuffer* buf);
423  void _notifyUniformBufferDestroyed(HardwareUniformBuffer* buf);
425  void _notifyCounterBufferDestroyed(HardwareCounterBuffer* buf);
426  };
427 
429  class _OgreExport HardwareBufferManager : public HardwareBufferManagerBase, public Singleton<HardwareBufferManager>
430  {
433  protected:
435  public:
438 
441  createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
442  bool useShadowBuffer = false)
443  {
444  return mImpl->createVertexBuffer(vertexSize, numVerts, usage, useShadowBuffer);
445  }
449  HardwareBuffer::Usage usage, bool useShadowBuffer = false)
450  {
451  return mImpl->createIndexBuffer(itype, numIndexes, usage, useShadowBuffer);
452  }
453 
456  {
457  return mImpl->createRenderToVertexBuffer();
458  }
459 
462  createUniformBuffer(size_t sizeBytes, HardwareBuffer::Usage usage, bool useShadowBuffer, const String& name = "")
463  {
464  return mImpl->createUniformBuffer(sizeBytes, usage, useShadowBuffer, name);
465  }
466 
469  createCounterBuffer(size_t sizeBytes, HardwareBuffer::Usage usage, bool useShadowBuffer, const String& name = "")
470  {
471  return mImpl->createCounterBuffer(sizeBytes, usage, useShadowBuffer, name);
472  }
473 
476  {
477  return mImpl->createVertexDeclaration();
478  }
481  {
482  mImpl->destroyVertexDeclaration(decl);
483  }
484 
487  {
488  return mImpl->createVertexBufferBinding();
489  }
492  {
493  mImpl->destroyVertexBufferBinding(binding);
494  }
497  const HardwareVertexBufferSharedPtr& sourceBuffer,
498  const HardwareVertexBufferSharedPtr& copy)
499  {
500  mImpl->registerVertexBufferSourceAndCopy(sourceBuffer, copy);
501  }
504  const HardwareVertexBufferSharedPtr& sourceBuffer,
505  BufferLicenseType licenseType,
506  HardwareBufferLicensee* licensee,
507  bool copyData = false)
508  {
509  return mImpl->allocateVertexBufferCopy(sourceBuffer, licenseType, licensee, copyData);
510  }
513  const HardwareVertexBufferSharedPtr& bufferCopy)
514  {
515  mImpl->releaseVertexBufferCopy(bufferCopy);
516  }
517 
519  virtual void touchVertexBufferCopy(
520  const HardwareVertexBufferSharedPtr& bufferCopy)
521  {
522  mImpl->touchVertexBufferCopy(bufferCopy);
523  }
524 
526  virtual void _freeUnusedBufferCopies(void)
527  {
528  mImpl->_freeUnusedBufferCopies();
529  }
531  virtual void _releaseBufferCopies(bool forceFreeUnused = false)
532  {
533  mImpl->_releaseBufferCopies(forceFreeUnused);
534  }
537  const HardwareVertexBufferSharedPtr& sourceBuffer)
538  {
539  mImpl->_forceReleaseBufferCopies(sourceBuffer);
540  }
543  {
544  mImpl->_forceReleaseBufferCopies(sourceBuffer);
545  }
548  {
549  mImpl->_notifyVertexBufferDestroyed(buf);
550  }
553  {
554  mImpl->_notifyIndexBufferDestroyed(buf);
555  }
558  {
559  mImpl->_notifyUniformBufferDestroyed(buf);
560  }
563  {
564  mImpl->_notifyCounterBufferDestroyed(buf);
565  }
566 
582  static HardwareBufferManager& getSingleton(void);
598  static HardwareBufferManager* getSingletonPtr(void);
599 
600  };
601 
604 } // namespace Ogre
605 
606 #include "OgreHeaderSuffix.h"
607 
608 #endif // __HardwareBufferManager__
609 
HardwareVertexBufferSharedPtr createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage, bool useShadowBuffer=false)
Create a hardware vertex buffer.
virtual void destroyVertexBufferBinding(VertexBufferBinding *binding)
Destroys a VertexBufferBinding.
HardwareVertexBufferSharedPtr srcNormalBuffer
size_t mUnderUsedFrameCount
Number of frames elapsed since temporary buffers utilization was above half the available.
#define _OgreExport
Definition: OgrePlatform.h:255
Singleton wrapper for hardware buffer manager.
Records the state of all the vertex buffer bindings required to provide a vertex declaration with the...
virtual void registerVertexBufferSourceAndCopy(const HardwareVertexBufferSharedPtr &sourceBuffer, const HardwareVertexBufferSharedPtr &copy)
Registers a vertex buffer as a copy of another.
Shared pointer implementation used to share uniform buffers.
#define OGRE_MUTEX(name)
Structure for recording the use of temporary blend buffers.
Template class for creating single-instance global classes.
Definition: OgreSingleton.h:64
virtual void _forceReleaseBufferCopies(const HardwareVertexBufferSharedPtr &sourceBuffer)
Internal method that forces the release of copies of a given buffer.
HardwareBufferManagerBase * mImpl
VertexBufferLicense(HardwareVertexBuffer *orig, BufferLicenseType ltype, size_t delay, HardwareVertexBufferSharedPtr buf, HardwareBufferLicensee *lic)
virtual void destroyVertexDeclaration(VertexDeclaration *decl)
Destroys a vertex declaration.
void _notifyUniformBufferDestroyed(HardwareUniformBuffer *buf)
Specialisation of HardwareBuffer for vertex index buffers, still abstract.
virtual void releaseVertexBufferCopy(const HardwareVertexBufferSharedPtr &bufferCopy)
Manually release a vertex buffer copy for others to subsequently use.
Specialisation of HardwareBuffer for a counter buffer.
virtual HardwareVertexBufferSharedPtr allocateVertexBufferCopy(const HardwareVertexBufferSharedPtr &sourceBuffer, BufferLicenseType licenseType, HardwareBufferLicensee *licensee, bool copyData=false)
Allocates a copy of a given vertex buffer.
Shared pointer implementation used to share vertex buffers.
void _notifyIndexBufferDestroyed(HardwareIndexBuffer *buf)
Notification that a hardware index buffer has been destroyed.
Combination of HBU_DYNAMIC, HBU_WRITE_ONLY and HBU_DISCARDABLE.
void _notifyConterBufferDestroyed(HardwareCounterBuffer *buf)
Shared pointer implementation used to share index buffers.
bool posNormalShareBuffer
Both positions and normals are contained in the same buffer.
static const size_t UNDER_USED_FRAME_THRESHOLD
Number of frames to wait before free unused temporary buffers.
void _notifyVertexBufferDestroyed(HardwareVertexBuffer *buf)
Notification that a hardware vertex buffer has been destroyed.
HardwareUniformBufferSharedPtr createUniformBuffer(size_t sizeBytes, HardwareBuffer::Usage usage, bool useShadowBuffer, const String &name="")
Create uniform buffer.
HardwareVertexBufferSharedPtr destNormalBuffer
Licensee will only release buffer when it says so.
Abstract interface representing a 'licensee' of a hardware buffer copy.
#define _OgrePrivate
Definition: OgrePlatform.h:256
TemporaryVertexBufferLicenseMap mTempVertexBufferLicenses
Map of currently licensed temporary buffers.
HardwareVertexBufferSharedPtr destPositionBuffer
HardwareIndexBufferSharedPtr createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes, HardwareBuffer::Usage usage, bool useShadowBuffer=false)
Create a hardware index buffer.
RenderToVertexBufferSharedPtr createRenderToVertexBuffer()
Create a render to vertex buffer.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Base definition of a hardware buffer manager.
Specialisation of HardwareBuffer for a uniform buffer.
map< HardwareVertexBuffer *, VertexBufferLicense >::type TemporaryVertexBufferLicenseMap
Map from temporary buffer to details of a license.
set< VertexDeclaration * >::type VertexDeclarationList
Usage
Enums describing buffer usage; not mutually exclusive.
virtual void _releaseBufferCopies(bool forceFreeUnused=false)
Internal method for releasing all temporary buffers which have been allocated using BLT_AUTOMATIC_REL...
multimap< HardwareVertexBuffer *, HardwareVertexBufferSharedPtr >::type FreeTemporaryVertexBufferMap
Map from original buffer to temporary buffers.
set< HardwareUniformBuffer * >::type UniformBufferList
set< HardwareVertexBuffer * >::type VertexBufferList
WARNING: The following two members should place before all other members.
HardwareVertexBufferSharedPtr srcPositionBuffer
Summary class collecting together vertex source information.
This class declares the format of a set of vertex inputs, which can be issued to the rendering API th...
virtual VertexBufferBinding * createVertexBufferBinding(void)
Creates a new VertexBufferBinding.
FreeTemporaryVertexBufferMap mFreeTempVertexBufferMap
Map of current available temp buffers.
Struct holding details of a license to use a temporary shared buffer.
Abstract class defining common features of hardware buffers.
set< VertexBufferBinding * >::type VertexBufferBindingList
HardwareCounterBufferSharedPtr createCounterBuffer(size_t sizeBytes, HardwareBuffer::Usage usage, bool useShadowBuffer, const String &name="")
Create counter buffer.
Shared pointer implementation used to share counter buffers.
Specialisation of HardwareBuffer for a vertex buffer.
set< HardwareCounterBuffer * >::type CounterBufferList
Reference-counted shared pointer, used for objects where implicit destruction is required.
_StringBase String
Definition: OgreCommon.h:53
virtual VertexDeclaration * createVertexDeclaration(void)
virtual void _forceReleaseBufferCopies(HardwareVertexBuffer *sourceBuffer)
Internal method that forces the release of copies of a given buffer.
static const size_t EXPIRED_DELAY_FRAME_THRESHOLD
Frame delay for BLT_AUTOMATIC_RELEASE temporary buffers.
set< HardwareIndexBuffer * >::type IndexBufferList
virtual void touchVertexBufferCopy(const HardwareVertexBufferSharedPtr &bufferCopy)
Tell engine that the vertex buffer copy intent to reuse.
virtual void _freeUnusedBufferCopies(void)
Free all unused vertex buffer copies.
VertexBufferBindingList mVertexBufferBindings