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