CmGLHardwareBufferManager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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 FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __GLHARWAREBUFFERMANAGER_H__
  25. #define __GLHARWAREBUFFERMANAGER_H__
  26. #include "CmGLPrerequisites.h"
  27. #include "CmHardwareBufferManager.h"
  28. namespace CamelotEngine {
  29. // Default threshold at which glMapBuffer becomes more efficient than glBufferSubData (32k?)
  30. # define OGRE_GL_DEFAULT_MAP_BUFFER_THRESHOLD (1024 * 32)
  31. /** Implementation of HardwareBufferManager for OpenGL. */
  32. class _OgreGLExport GLHardwareBufferManagerBase : public HardwareBufferManagerBase
  33. {
  34. protected:
  35. char* mScratchBufferPool;
  36. CM_MUTEX(mScratchMutex)
  37. size_t mMapBufferThreshold;
  38. public:
  39. GLHardwareBufferManagerBase();
  40. ~GLHardwareBufferManagerBase();
  41. /// Creates a vertex buffer
  42. HardwareVertexBufferPtr createVertexBuffer(size_t vertexSize,
  43. size_t numVerts, HardwareBuffer::Usage usage, bool useShadowBuffer = false);
  44. /// Create a hardware vertex buffer
  45. HardwareIndexBufferPtr createIndexBuffer(
  46. HardwareIndexBuffer::IndexType itype, size_t numIndexes,
  47. HardwareBuffer::Usage usage, bool useShadowBuffer = false);
  48. /// Utility function to get the correct GL usage based on HBU's
  49. static GLenum getGLUsage(unsigned int usage);
  50. /// Utility function to get the correct GL type based on VET's
  51. static GLenum getGLType(unsigned int type);
  52. /** Allocator method to allow us to use a pool of memory as a scratch
  53. area for hardware buffers. This is because glMapBuffer is incredibly
  54. inefficient, seemingly no matter what options we give it. So for the
  55. period of lock/unlock, we will instead allocate a section of a local
  56. memory pool, and use glBufferSubDataARB / glGetBufferSubDataARB
  57. instead.
  58. */
  59. void* allocateScratch(UINT32 size);
  60. /// @see allocateScratch
  61. void deallocateScratch(void* ptr);
  62. /** Threshold after which glMapBuffer is used and not glBufferSubData
  63. */
  64. const size_t getGLMapBufferThreshold() const;
  65. void setGLMapBufferThreshold( const size_t value );
  66. };
  67. /// GLHardwareBufferManagerBase as a Singleton
  68. class _OgreGLExport GLHardwareBufferManager : public HardwareBufferManager
  69. {
  70. public:
  71. GLHardwareBufferManager()
  72. : HardwareBufferManager(new GLHardwareBufferManagerBase())
  73. {
  74. }
  75. ~GLHardwareBufferManager()
  76. {
  77. delete mImpl;
  78. }
  79. /// Utility function to get the correct GL usage based on HBU's
  80. static GLenum getGLUsage(unsigned int usage)
  81. { return GLHardwareBufferManagerBase::getGLUsage(usage); }
  82. /// Utility function to get the correct GL type based on VET's
  83. static GLenum getGLType(unsigned int type)
  84. { return GLHardwareBufferManagerBase::getGLType(type); }
  85. /** Allocator method to allow us to use a pool of memory as a scratch
  86. area for hardware buffers. This is because glMapBuffer is incredibly
  87. inefficient, seemingly no matter what options we give it. So for the
  88. period of lock/unlock, we will instead allocate a section of a local
  89. memory pool, and use glBufferSubDataARB / glGetBufferSubDataARB
  90. instead.
  91. */
  92. void* allocateScratch(UINT32 size)
  93. {
  94. return static_cast<GLHardwareBufferManagerBase*>(mImpl)->allocateScratch(size);
  95. }
  96. /// @see allocateScratch
  97. void deallocateScratch(void* ptr)
  98. {
  99. static_cast<GLHardwareBufferManagerBase*>(mImpl)->deallocateScratch(ptr);
  100. }
  101. /** Threshold after which glMapBuffer is used and not glBufferSubData
  102. */
  103. const size_t getGLMapBufferThreshold() const
  104. {
  105. return static_cast<GLHardwareBufferManagerBase*>(mImpl)->getGLMapBufferThreshold();
  106. }
  107. void setGLMapBufferThreshold( const size_t value )
  108. {
  109. static_cast<GLHardwareBufferManagerBase*>(mImpl)->setGLMapBufferThreshold(value);
  110. }
  111. };
  112. }
  113. #endif // __GLHARWAREBUFFERMANAGER_H__