BufferObject.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __BUFFEROBJECT_H__
  21. #define __BUFFEROBJECT_H__
  22. /*
  23. ================================================================================================
  24. Buffer Objects
  25. ================================================================================================
  26. */
  27. class idIndexBuffer;
  28. enum bufferMapType_t {
  29. BM_READ, // map for reading
  30. BM_WRITE // map for writing
  31. };
  32. // Returns all targets to virtual memory use instead of buffer object use.
  33. // Call this before doing any conventional buffer reads, like screenshots.
  34. void UnbindBufferObjects();
  35. /*
  36. ================================================
  37. idVertexBuffer
  38. ================================================
  39. */
  40. class idVertexBuffer {
  41. public:
  42. idVertexBuffer();
  43. ~idVertexBuffer();
  44. // Allocate or free the buffer.
  45. bool AllocBufferObject( const void * data, int allocSize );
  46. void FreeBufferObject();
  47. // Make this buffer a reference to another buffer.
  48. void Reference( const idVertexBuffer & other );
  49. void Reference( const idVertexBuffer & other, int refOffset, int refSize );
  50. // Copies data to the buffer. 'size' may be less than the originally allocated size.
  51. void Update( const void * data, int updateSize ) const;
  52. void * MapBuffer( bufferMapType_t mapType ) const;
  53. idDrawVert * MapVertexBuffer( bufferMapType_t mapType ) const { return static_cast< idDrawVert * >( MapBuffer( mapType ) ); }
  54. void UnmapBuffer() const;
  55. bool IsMapped() const { return ( size & MAPPED_FLAG ) != 0; }
  56. int GetSize() const { return ( size & ~MAPPED_FLAG ); }
  57. int GetAllocedSize() const { return ( ( size & ~MAPPED_FLAG ) + 15 ) & ~15; }
  58. void * GetAPIObject() const { return apiObject; }
  59. int GetOffset() const { return ( offsetInOtherBuffer & ~OWNS_BUFFER_FLAG ); }
  60. private:
  61. int size; // size in bytes
  62. int offsetInOtherBuffer; // offset in bytes
  63. void * apiObject;
  64. // sizeof() confuses typeinfo...
  65. static const int MAPPED_FLAG = 1 << ( 4 /* sizeof( int ) */ * 8 - 1 );
  66. static const int OWNS_BUFFER_FLAG = 1 << ( 4 /* sizeof( int ) */ * 8 - 1 );
  67. private:
  68. void ClearWithoutFreeing();
  69. void SetMapped() const { const_cast< int & >( size ) |= MAPPED_FLAG; }
  70. void SetUnmapped() const { const_cast< int & >( size ) &= ~MAPPED_FLAG; }
  71. bool OwnsBuffer() const { return ( ( offsetInOtherBuffer & OWNS_BUFFER_FLAG ) != 0 ); }
  72. DISALLOW_COPY_AND_ASSIGN( idVertexBuffer );
  73. };
  74. /*
  75. ================================================
  76. idIndexBuffer
  77. ================================================
  78. */
  79. class idIndexBuffer {
  80. public:
  81. idIndexBuffer();
  82. ~idIndexBuffer();
  83. // Allocate or free the buffer.
  84. bool AllocBufferObject( const void * data, int allocSize );
  85. void FreeBufferObject();
  86. // Make this buffer a reference to another buffer.
  87. void Reference( const idIndexBuffer & other );
  88. void Reference( const idIndexBuffer & other, int refOffset, int refSize );
  89. // Copies data to the buffer. 'size' may be less than the originally allocated size.
  90. void Update( const void * data, int updateSize ) const;
  91. void * MapBuffer( bufferMapType_t mapType ) const;
  92. triIndex_t * MapIndexBuffer( bufferMapType_t mapType ) const { return static_cast< triIndex_t * >( MapBuffer( mapType ) ); }
  93. void UnmapBuffer() const;
  94. bool IsMapped() const { return ( size & MAPPED_FLAG ) != 0; }
  95. int GetSize() const { return ( size & ~MAPPED_FLAG ); }
  96. int GetAllocedSize() const { return ( ( size & ~MAPPED_FLAG ) + 15 ) & ~15; }
  97. void * GetAPIObject() const { return apiObject; }
  98. int GetOffset() const { return ( offsetInOtherBuffer & ~OWNS_BUFFER_FLAG ); }
  99. private:
  100. int size; // size in bytes
  101. int offsetInOtherBuffer; // offset in bytes
  102. void * apiObject;
  103. // sizeof() confuses typeinfo...
  104. static const int MAPPED_FLAG = 1 << ( 4 /* sizeof( int ) */ * 8 - 1 );
  105. static const int OWNS_BUFFER_FLAG = 1 << ( 4 /* sizeof( int ) */ * 8 - 1 );
  106. private:
  107. void ClearWithoutFreeing();
  108. void SetMapped() const { const_cast< int & >( size ) |= MAPPED_FLAG; }
  109. void SetUnmapped() const { const_cast< int & >( size ) &= ~MAPPED_FLAG; }
  110. bool OwnsBuffer() const { return ( ( offsetInOtherBuffer & OWNS_BUFFER_FLAG ) != 0 ); }
  111. DISALLOW_COPY_AND_ASSIGN( idIndexBuffer );
  112. };
  113. /*
  114. ================================================
  115. idJointBuffer
  116. IMPORTANT NOTICE: on the PC, binding to an offset in uniform buffer objects
  117. is limited to GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, which is 256 on current nvidia cards,
  118. so joint offsets, which are multiples of 48 bytes, must be in multiples of 16 = 768 bytes.
  119. ================================================
  120. */
  121. class idJointBuffer {
  122. public:
  123. idJointBuffer();
  124. ~idJointBuffer();
  125. // Allocate or free the buffer.
  126. bool AllocBufferObject( const float * joints, int numAllocJoints );
  127. void FreeBufferObject();
  128. // Make this buffer a reference to another buffer.
  129. void Reference( const idJointBuffer & other );
  130. void Reference( const idJointBuffer & other, int jointRefOffset, int numRefJoints );
  131. // Copies data to the buffer. 'numJoints' may be less than the originally allocated size.
  132. void Update( const float * joints, int numUpdateJoints ) const;
  133. float * MapBuffer( bufferMapType_t mapType ) const;
  134. void UnmapBuffer() const;
  135. bool IsMapped() const { return ( numJoints & MAPPED_FLAG ) != 0; }
  136. int GetNumJoints() const { return ( numJoints & ~MAPPED_FLAG ); }
  137. int GetAllocedSize() const { return ( numJoints & ~MAPPED_FLAG ) * 3 * 4 * sizeof( float ); }
  138. void * GetAPIObject() const { return apiObject; }
  139. int GetOffset() const { return ( offsetInOtherBuffer & ~OWNS_BUFFER_FLAG ); }
  140. void Swap( idJointBuffer & other );
  141. private:
  142. int numJoints;
  143. int offsetInOtherBuffer; // offset in bytes
  144. void * apiObject;
  145. // sizeof() confuses typeinfo...
  146. static const int MAPPED_FLAG = 1 << ( 4 /* sizeof( int ) */ * 8 - 1 );
  147. static const int OWNS_BUFFER_FLAG = 1 << ( 4 /* sizeof( int ) */ * 8 - 1 );
  148. private:
  149. void ClearWithoutFreeing();
  150. void SetMapped() const { const_cast< int & >( numJoints ) |= MAPPED_FLAG; }
  151. void SetUnmapped() const { const_cast< int & >( numJoints ) &= ~MAPPED_FLAG; }
  152. bool OwnsBuffer() const { return ( ( offsetInOtherBuffer & OWNS_BUFFER_FLAG ) != 0 ); }
  153. DISALLOW_COPY_AND_ASSIGN( idJointBuffer );
  154. };
  155. #endif // !__BUFFEROBJECT_H__