CmGLHardwareBufferManager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 CM_RSGL_EXPORT GLHardwareBufferManagerBase : public HardwareBufferManagerBase
  33. {
  34. protected:
  35. char* mScratchBufferPool;
  36. CM_MUTEX(mScratchMutex)
  37. UINT32 mMapBufferThreshold;
  38. public:
  39. GLHardwareBufferManagerBase();
  40. ~GLHardwareBufferManagerBase();
  41. /**
  42. * @copydoc HardwareBufferManagerBase::createVertexBuffer
  43. */
  44. HardwareVertexBufferPtr createVertexBuffer(UINT32 vertexSize,
  45. UINT32 numVerts, HardwareBuffer::Usage usage, bool streamOut = false);
  46. /**
  47. * @copydoc HardwareBufferManagerBase::createIndexBuffer
  48. */
  49. HardwareIndexBufferPtr createIndexBuffer(
  50. HardwareIndexBuffer::IndexType itype, UINT32 numIndexes,
  51. HardwareBuffer::Usage usage);
  52. /**
  53. * @copydoc HardwareBufferManagerBase::createConstantBuffer
  54. */
  55. HardwareConstantBufferPtr createConstantBuffer(UINT32 sizeBytes, HardwareBuffer::Usage usage);
  56. /// Utility function to get the correct GL usage based on HBU's
  57. static GLenum getGLUsage(unsigned int usage);
  58. /// Utility function to get the correct GL type based on VET's
  59. static GLenum getGLType(unsigned int type);
  60. /** Allocator method to allow us to use a pool of memory as a scratch
  61. area for hardware buffers. This is because glMapBuffer is incredibly
  62. inefficient, seemingly no matter what options we give it. So for the
  63. period of lock/unlock, we will instead allocate a section of a local
  64. memory pool, and use glBufferSubDataARB / glGetBufferSubDataARB
  65. instead.
  66. */
  67. void* allocateScratch(UINT32 size);
  68. /// @see allocateScratch
  69. void deallocateScratch(void* ptr);
  70. /** Threshold after which glMapBuffer is used and not glBufferSubData
  71. */
  72. const UINT32 getGLMapBufferThreshold() const;
  73. void setGLMapBufferThreshold( const UINT32 value );
  74. };
  75. /// GLHardwareBufferManagerBase as a Singleton
  76. class CM_RSGL_EXPORT GLHardwareBufferManager : public HardwareBufferManager
  77. {
  78. public:
  79. GLHardwareBufferManager()
  80. : HardwareBufferManager(new GLHardwareBufferManagerBase())
  81. {
  82. }
  83. ~GLHardwareBufferManager()
  84. {
  85. delete mImpl;
  86. }
  87. /// Utility function to get the correct GL usage based on HBU's
  88. static GLenum getGLUsage(unsigned int usage)
  89. { return GLHardwareBufferManagerBase::getGLUsage(usage); }
  90. /// Utility function to get the correct GL type based on VET's
  91. static GLenum getGLType(unsigned int type)
  92. { return GLHardwareBufferManagerBase::getGLType(type); }
  93. /** Allocator method to allow us to use a pool of memory as a scratch
  94. area for hardware buffers. This is because glMapBuffer is incredibly
  95. inefficient, seemingly no matter what options we give it. So for the
  96. period of lock/unlock, we will instead allocate a section of a local
  97. memory pool, and use glBufferSubDataARB / glGetBufferSubDataARB
  98. instead.
  99. */
  100. void* allocateScratch(UINT32 size)
  101. {
  102. return static_cast<GLHardwareBufferManagerBase*>(mImpl)->allocateScratch(size);
  103. }
  104. /// @see allocateScratch
  105. void deallocateScratch(void* ptr)
  106. {
  107. static_cast<GLHardwareBufferManagerBase*>(mImpl)->deallocateScratch(ptr);
  108. }
  109. /** Threshold after which glMapBuffer is used and not glBufferSubData
  110. */
  111. const UINT32 getGLMapBufferThreshold() const
  112. {
  113. return static_cast<GLHardwareBufferManagerBase*>(mImpl)->getGLMapBufferThreshold();
  114. }
  115. void setGLMapBufferThreshold( const UINT32 value )
  116. {
  117. static_cast<GLHardwareBufferManagerBase*>(mImpl)->setGLMapBufferThreshold(value);
  118. }
  119. };
  120. }
  121. #endif // __GLHARWAREBUFFERMANAGER_H__