BufferObject.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. idCVar r_showBuffers( "r_showBuffers", "0", CVAR_INTEGER, "" );
  24. //static const GLenum bufferUsage = GL_STATIC_DRAW_ARB;
  25. static const GLenum bufferUsage = GL_DYNAMIC_DRAW_ARB;
  26. /*
  27. ==================
  28. IsWriteCombined
  29. ==================
  30. */
  31. bool IsWriteCombined( void * base ) {
  32. MEMORY_BASIC_INFORMATION info;
  33. SIZE_T size = VirtualQueryEx( GetCurrentProcess(), base, &info, sizeof( info ) );
  34. if ( size == 0 ) {
  35. DWORD error = GetLastError();
  36. error = error;
  37. return false;
  38. }
  39. bool isWriteCombined = ( ( info.AllocationProtect & PAGE_WRITECOMBINE ) != 0 );
  40. return isWriteCombined;
  41. }
  42. /*
  43. ================================================================================================
  44. Buffer Objects
  45. ================================================================================================
  46. */
  47. /*
  48. ========================
  49. UnbindBufferObjects
  50. ========================
  51. */
  52. void UnbindBufferObjects() {
  53. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
  54. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
  55. }
  56. #ifdef ID_WIN_X86_SSE2_INTRIN
  57. void CopyBuffer( byte * dst, const byte * src, int numBytes ) {
  58. assert_16_byte_aligned( dst );
  59. assert_16_byte_aligned( src );
  60. int i = 0;
  61. for ( ; i + 128 <= numBytes; i += 128 ) {
  62. __m128i d0 = _mm_load_si128( (__m128i *)&src[i + 0*16] );
  63. __m128i d1 = _mm_load_si128( (__m128i *)&src[i + 1*16] );
  64. __m128i d2 = _mm_load_si128( (__m128i *)&src[i + 2*16] );
  65. __m128i d3 = _mm_load_si128( (__m128i *)&src[i + 3*16] );
  66. __m128i d4 = _mm_load_si128( (__m128i *)&src[i + 4*16] );
  67. __m128i d5 = _mm_load_si128( (__m128i *)&src[i + 5*16] );
  68. __m128i d6 = _mm_load_si128( (__m128i *)&src[i + 6*16] );
  69. __m128i d7 = _mm_load_si128( (__m128i *)&src[i + 7*16] );
  70. _mm_stream_si128( (__m128i *)&dst[i + 0*16], d0 );
  71. _mm_stream_si128( (__m128i *)&dst[i + 1*16], d1 );
  72. _mm_stream_si128( (__m128i *)&dst[i + 2*16], d2 );
  73. _mm_stream_si128( (__m128i *)&dst[i + 3*16], d3 );
  74. _mm_stream_si128( (__m128i *)&dst[i + 4*16], d4 );
  75. _mm_stream_si128( (__m128i *)&dst[i + 5*16], d5 );
  76. _mm_stream_si128( (__m128i *)&dst[i + 6*16], d6 );
  77. _mm_stream_si128( (__m128i *)&dst[i + 7*16], d7 );
  78. }
  79. for ( ; i + 16 <= numBytes; i += 16 ) {
  80. __m128i d = _mm_load_si128( (__m128i *)&src[i] );
  81. _mm_stream_si128( (__m128i *)&dst[i], d );
  82. }
  83. for ( ; i + 4 <= numBytes; i += 4 ) {
  84. *(uint32 *)&dst[i] = *(const uint32 *)&src[i];
  85. }
  86. for ( ; i < numBytes; i++ ) {
  87. dst[i] = src[i];
  88. }
  89. _mm_sfence();
  90. }
  91. #else
  92. void CopyBuffer( byte * dst, const byte * src, int numBytes ) {
  93. assert_16_byte_aligned( dst );
  94. assert_16_byte_aligned( src );
  95. memcpy( dst, src, numBytes );
  96. }
  97. #endif
  98. /*
  99. ================================================================================================
  100. idVertexBuffer
  101. ================================================================================================
  102. */
  103. /*
  104. ========================
  105. idVertexBuffer::idVertexBuffer
  106. ========================
  107. */
  108. idVertexBuffer::idVertexBuffer() {
  109. size = 0;
  110. offsetInOtherBuffer = OWNS_BUFFER_FLAG;
  111. apiObject = NULL;
  112. SetUnmapped();
  113. }
  114. /*
  115. ========================
  116. idVertexBuffer::~idVertexBuffer
  117. ========================
  118. */
  119. idVertexBuffer::~idVertexBuffer() {
  120. FreeBufferObject();
  121. }
  122. /*
  123. ========================
  124. idVertexBuffer::AllocBufferObject
  125. ========================
  126. */
  127. bool idVertexBuffer::AllocBufferObject( const void * data, int allocSize ) {
  128. assert( apiObject == NULL );
  129. assert_16_byte_aligned( data );
  130. if ( allocSize <= 0 ) {
  131. idLib::Error( "idVertexBuffer::AllocBufferObject: allocSize = %i", allocSize );
  132. }
  133. size = allocSize;
  134. bool allocationFailed = false;
  135. int numBytes = GetAllocedSize();
  136. // clear out any previous error
  137. qglGetError();
  138. GLuint bufferObject = 0xFFFF;
  139. qglGenBuffersARB( 1, & bufferObject );
  140. if ( bufferObject == 0xFFFF ) {
  141. idLib::FatalError( "idVertexBuffer::AllocBufferObject: failed" );
  142. }
  143. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
  144. // these are rewritten every frame
  145. qglBufferDataARB( GL_ARRAY_BUFFER_ARB, numBytes, NULL, bufferUsage );
  146. apiObject = reinterpret_cast< void * >( bufferObject );
  147. GLenum err = qglGetError();
  148. if ( err == GL_OUT_OF_MEMORY ) {
  149. idLib::Warning( "idVertexBuffer::AllocBufferObject: allocation failed" );
  150. allocationFailed = true;
  151. }
  152. if ( r_showBuffers.GetBool() ) {
  153. idLib::Printf( "vertex buffer alloc %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
  154. }
  155. // copy the data
  156. if ( data != NULL ) {
  157. Update( data, allocSize );
  158. }
  159. return !allocationFailed;
  160. }
  161. /*
  162. ========================
  163. idVertexBuffer::FreeBufferObject
  164. ========================
  165. */
  166. void idVertexBuffer::FreeBufferObject() {
  167. if ( IsMapped() ) {
  168. UnmapBuffer();
  169. }
  170. // if this is a sub-allocation inside a larger buffer, don't actually free anything.
  171. if ( OwnsBuffer() == false ) {
  172. ClearWithoutFreeing();
  173. return;
  174. }
  175. if ( apiObject == NULL ) {
  176. return;
  177. }
  178. if ( r_showBuffers.GetBool() ) {
  179. idLib::Printf( "vertex buffer free %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
  180. }
  181. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  182. qglDeleteBuffersARB( 1, & bufferObject );
  183. ClearWithoutFreeing();
  184. }
  185. /*
  186. ========================
  187. idVertexBuffer::Reference
  188. ========================
  189. */
  190. void idVertexBuffer::Reference( const idVertexBuffer & other ) {
  191. assert( IsMapped() == false );
  192. //assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up idDrawVerts
  193. assert( other.GetAPIObject() != NULL );
  194. assert( other.GetSize() > 0 );
  195. FreeBufferObject();
  196. size = other.GetSize(); // this strips the MAPPED_FLAG
  197. offsetInOtherBuffer = other.GetOffset(); // this strips the OWNS_BUFFER_FLAG
  198. apiObject = other.apiObject;
  199. assert( OwnsBuffer() == false );
  200. }
  201. /*
  202. ========================
  203. idVertexBuffer::Reference
  204. ========================
  205. */
  206. void idVertexBuffer::Reference( const idVertexBuffer & other, int refOffset, int refSize ) {
  207. assert( IsMapped() == false );
  208. //assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up idDrawVerts
  209. assert( other.GetAPIObject() != NULL );
  210. assert( refOffset >= 0 );
  211. assert( refSize >= 0 );
  212. assert( refOffset + refSize <= other.GetSize() );
  213. FreeBufferObject();
  214. size = refSize;
  215. offsetInOtherBuffer = other.GetOffset() + refOffset;
  216. apiObject = other.apiObject;
  217. assert( OwnsBuffer() == false );
  218. }
  219. /*
  220. ========================
  221. idVertexBuffer::Update
  222. ========================
  223. */
  224. void idVertexBuffer::Update( const void * data, int updateSize ) const {
  225. assert( apiObject != NULL );
  226. assert( IsMapped() == false );
  227. assert_16_byte_aligned( data );
  228. assert( ( GetOffset() & 15 ) == 0 );
  229. if ( updateSize > size ) {
  230. idLib::FatalError( "idVertexBuffer::Update: size overrun, %i > %i\n", updateSize, GetSize() );
  231. }
  232. int numBytes = ( updateSize + 15 ) & ~15;
  233. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  234. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
  235. qglBufferSubDataARB( GL_ARRAY_BUFFER_ARB, GetOffset(), (GLsizeiptrARB)numBytes, data );
  236. /*
  237. void * buffer = MapBuffer( BM_WRITE );
  238. CopyBuffer( (byte *)buffer + GetOffset(), (byte *)data, numBytes );
  239. UnmapBuffer();
  240. */
  241. }
  242. /*
  243. ========================
  244. idVertexBuffer::MapBuffer
  245. ========================
  246. */
  247. void * idVertexBuffer::MapBuffer( bufferMapType_t mapType ) const {
  248. assert( apiObject != NULL );
  249. assert( IsMapped() == false );
  250. void * buffer = NULL;
  251. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  252. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
  253. if ( mapType == BM_READ ) {
  254. //buffer = qglMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_READ_ONLY_ARB );
  255. buffer = qglMapBufferRange( GL_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_READ_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
  256. if ( buffer != NULL ) {
  257. buffer = (byte *)buffer + GetOffset();
  258. }
  259. } else if ( mapType == BM_WRITE ) {
  260. //buffer = qglMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
  261. buffer = qglMapBufferRange( GL_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
  262. if ( buffer != NULL ) {
  263. buffer = (byte *)buffer + GetOffset();
  264. }
  265. assert( IsWriteCombined( buffer ) );
  266. } else {
  267. assert( false );
  268. }
  269. SetMapped();
  270. if ( buffer == NULL ) {
  271. idLib::FatalError( "idVertexBuffer::MapBuffer: failed" );
  272. }
  273. return buffer;
  274. }
  275. /*
  276. ========================
  277. idVertexBuffer::UnmapBuffer
  278. ========================
  279. */
  280. void idVertexBuffer::UnmapBuffer() const {
  281. assert( apiObject != NULL );
  282. assert( IsMapped() );
  283. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  284. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
  285. if ( !qglUnmapBufferARB( GL_ARRAY_BUFFER_ARB ) ) {
  286. idLib::Printf( "idVertexBuffer::UnmapBuffer failed\n" );
  287. }
  288. SetUnmapped();
  289. }
  290. /*
  291. ========================
  292. idVertexBuffer::ClearWithoutFreeing
  293. ========================
  294. */
  295. void idVertexBuffer::ClearWithoutFreeing() {
  296. size = 0;
  297. offsetInOtherBuffer = OWNS_BUFFER_FLAG;
  298. apiObject = NULL;
  299. }
  300. /*
  301. ================================================================================================
  302. idIndexBuffer
  303. ================================================================================================
  304. */
  305. /*
  306. ========================
  307. idIndexBuffer::idIndexBuffer
  308. ========================
  309. */
  310. idIndexBuffer::idIndexBuffer() {
  311. size = 0;
  312. offsetInOtherBuffer = OWNS_BUFFER_FLAG;
  313. apiObject = NULL;
  314. SetUnmapped();
  315. }
  316. /*
  317. ========================
  318. idIndexBuffer::~idIndexBuffer
  319. ========================
  320. */
  321. idIndexBuffer::~idIndexBuffer() {
  322. FreeBufferObject();
  323. }
  324. /*
  325. ========================
  326. idIndexBuffer::AllocBufferObject
  327. ========================
  328. */
  329. bool idIndexBuffer::AllocBufferObject( const void * data, int allocSize ) {
  330. assert( apiObject == NULL );
  331. assert_16_byte_aligned( data );
  332. if ( allocSize <= 0 ) {
  333. idLib::Error( "idIndexBuffer::AllocBufferObject: allocSize = %i", allocSize );
  334. }
  335. size = allocSize;
  336. bool allocationFailed = false;
  337. int numBytes = GetAllocedSize();
  338. // clear out any previous error
  339. qglGetError();
  340. GLuint bufferObject = 0xFFFF;
  341. qglGenBuffersARB( 1, & bufferObject );
  342. if ( bufferObject == 0xFFFF ) {
  343. GLenum error = qglGetError();
  344. idLib::FatalError( "idIndexBuffer::AllocBufferObject: failed - GL_Error %d", error );
  345. }
  346. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
  347. // these are rewritten every frame
  348. qglBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, numBytes, NULL, bufferUsage );
  349. apiObject = reinterpret_cast< void * >( bufferObject );
  350. GLenum err = qglGetError();
  351. if ( err == GL_OUT_OF_MEMORY ) {
  352. idLib::Warning( "idIndexBuffer:AllocBufferObject: allocation failed" );
  353. allocationFailed = true;
  354. }
  355. if ( r_showBuffers.GetBool() ) {
  356. idLib::Printf( "index buffer alloc %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
  357. }
  358. // copy the data
  359. if ( data != NULL ) {
  360. Update( data, allocSize );
  361. }
  362. return !allocationFailed;
  363. }
  364. /*
  365. ========================
  366. idIndexBuffer::FreeBufferObject
  367. ========================
  368. */
  369. void idIndexBuffer::FreeBufferObject() {
  370. if ( IsMapped() ) {
  371. UnmapBuffer();
  372. }
  373. // if this is a sub-allocation inside a larger buffer, don't actually free anything.
  374. if ( OwnsBuffer() == false ) {
  375. ClearWithoutFreeing();
  376. return;
  377. }
  378. if ( apiObject == NULL ) {
  379. return;
  380. }
  381. if ( r_showBuffers.GetBool() ) {
  382. idLib::Printf( "index buffer free %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
  383. }
  384. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  385. qglDeleteBuffersARB( 1, & bufferObject );
  386. ClearWithoutFreeing();
  387. }
  388. /*
  389. ========================
  390. idIndexBuffer::Reference
  391. ========================
  392. */
  393. void idIndexBuffer::Reference( const idIndexBuffer & other ) {
  394. assert( IsMapped() == false );
  395. //assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up triIndex_t
  396. assert( other.GetAPIObject() != NULL );
  397. assert( other.GetSize() > 0 );
  398. FreeBufferObject();
  399. size = other.GetSize(); // this strips the MAPPED_FLAG
  400. offsetInOtherBuffer = other.GetOffset(); // this strips the OWNS_BUFFER_FLAG
  401. apiObject = other.apiObject;
  402. assert( OwnsBuffer() == false );
  403. }
  404. /*
  405. ========================
  406. idIndexBuffer::Reference
  407. ========================
  408. */
  409. void idIndexBuffer::Reference( const idIndexBuffer & other, int refOffset, int refSize ) {
  410. assert( IsMapped() == false );
  411. //assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up triIndex_t
  412. assert( other.GetAPIObject() != NULL );
  413. assert( refOffset >= 0 );
  414. assert( refSize >= 0 );
  415. assert( refOffset + refSize <= other.GetSize() );
  416. FreeBufferObject();
  417. size = refSize;
  418. offsetInOtherBuffer = other.GetOffset() + refOffset;
  419. apiObject = other.apiObject;
  420. assert( OwnsBuffer() == false );
  421. }
  422. /*
  423. ========================
  424. idIndexBuffer::Update
  425. ========================
  426. */
  427. void idIndexBuffer::Update( const void * data, int updateSize ) const {
  428. assert( apiObject != NULL );
  429. assert( IsMapped() == false );
  430. assert_16_byte_aligned( data );
  431. assert( ( GetOffset() & 15 ) == 0 );
  432. if ( updateSize > size ) {
  433. idLib::FatalError( "idIndexBuffer::Update: size overrun, %i > %i\n", updateSize, GetSize() );
  434. }
  435. int numBytes = ( updateSize + 15 ) & ~15;
  436. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  437. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
  438. qglBufferSubDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, GetOffset(), (GLsizeiptrARB)numBytes, data );
  439. /*
  440. void * buffer = MapBuffer( BM_WRITE );
  441. CopyBuffer( (byte *)buffer + GetOffset(), (byte *)data, numBytes );
  442. UnmapBuffer();
  443. */
  444. }
  445. /*
  446. ========================
  447. idIndexBuffer::MapBuffer
  448. ========================
  449. */
  450. void * idIndexBuffer::MapBuffer( bufferMapType_t mapType ) const {
  451. assert( apiObject != NULL );
  452. assert( IsMapped() == false );
  453. void * buffer = NULL;
  454. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  455. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
  456. if ( mapType == BM_READ ) {
  457. //buffer = qglMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, GL_READ_ONLY_ARB );
  458. buffer = qglMapBufferRange( GL_ELEMENT_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_READ_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
  459. if ( buffer != NULL ) {
  460. buffer = (byte *)buffer + GetOffset();
  461. }
  462. } else if ( mapType == BM_WRITE ) {
  463. //buffer = qglMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
  464. buffer = qglMapBufferRange( GL_ELEMENT_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
  465. if ( buffer != NULL ) {
  466. buffer = (byte *)buffer + GetOffset();
  467. }
  468. assert( IsWriteCombined( buffer ) );
  469. } else {
  470. assert( false );
  471. }
  472. SetMapped();
  473. if ( buffer == NULL ) {
  474. idLib::FatalError( "idIndexBuffer::MapBuffer: failed" );
  475. }
  476. return buffer;
  477. }
  478. /*
  479. ========================
  480. idIndexBuffer::UnmapBuffer
  481. ========================
  482. */
  483. void idIndexBuffer::UnmapBuffer() const {
  484. assert( apiObject != NULL );
  485. assert( IsMapped() );
  486. GLuint bufferObject = reinterpret_cast< GLuint >( apiObject );
  487. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
  488. if ( !qglUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB ) ) {
  489. idLib::Printf( "idIndexBuffer::UnmapBuffer failed\n" );
  490. }
  491. SetUnmapped();
  492. }
  493. /*
  494. ========================
  495. idIndexBuffer::ClearWithoutFreeing
  496. ========================
  497. */
  498. void idIndexBuffer::ClearWithoutFreeing() {
  499. size = 0;
  500. offsetInOtherBuffer = OWNS_BUFFER_FLAG;
  501. apiObject = NULL;
  502. }
  503. /*
  504. ================================================================================================
  505. idJointBuffer
  506. ================================================================================================
  507. */
  508. /*
  509. ========================
  510. idJointBuffer::idJointBuffer
  511. ========================
  512. */
  513. idJointBuffer::idJointBuffer() {
  514. numJoints = 0;
  515. offsetInOtherBuffer = OWNS_BUFFER_FLAG;
  516. apiObject = NULL;
  517. SetUnmapped();
  518. }
  519. /*
  520. ========================
  521. idJointBuffer::~idJointBuffer
  522. ========================
  523. */
  524. idJointBuffer::~idJointBuffer() {
  525. FreeBufferObject();
  526. }
  527. /*
  528. ========================
  529. idJointBuffer::AllocBufferObject
  530. ========================
  531. */
  532. bool idJointBuffer::AllocBufferObject( const float * joints, int numAllocJoints ) {
  533. assert( apiObject == NULL );
  534. assert_16_byte_aligned( joints );
  535. if ( numAllocJoints <= 0 ) {
  536. idLib::Error( "idJointBuffer::AllocBufferObject: joints = %i", numAllocJoints );
  537. }
  538. numJoints = numAllocJoints;
  539. bool allocationFailed = false;
  540. const int numBytes = GetAllocedSize();
  541. GLuint buffer = 0;
  542. qglGenBuffersARB( 1, &buffer );
  543. qglBindBufferARB( GL_UNIFORM_BUFFER, buffer );
  544. qglBufferDataARB( GL_UNIFORM_BUFFER, numBytes, NULL, GL_STREAM_DRAW_ARB );
  545. qglBindBufferARB( GL_UNIFORM_BUFFER, 0);
  546. apiObject = reinterpret_cast< void * >( buffer );
  547. if ( r_showBuffers.GetBool() ) {
  548. idLib::Printf( "joint buffer alloc %p, api %p (%i joints)\n", this, GetAPIObject(), GetNumJoints() );
  549. }
  550. // copy the data
  551. if ( joints != NULL ) {
  552. Update( joints, numAllocJoints );
  553. }
  554. return !allocationFailed;
  555. }
  556. /*
  557. ========================
  558. idJointBuffer::FreeBufferObject
  559. ========================
  560. */
  561. void idJointBuffer::FreeBufferObject() {
  562. if ( IsMapped() ) {
  563. UnmapBuffer();
  564. }
  565. // if this is a sub-allocation inside a larger buffer, don't actually free anything.
  566. if ( OwnsBuffer() == false ) {
  567. ClearWithoutFreeing();
  568. return;
  569. }
  570. if ( apiObject == NULL ) {
  571. return;
  572. }
  573. if ( r_showBuffers.GetBool() ) {
  574. idLib::Printf( "joint buffer free %p, api %p (%i joints)\n", this, GetAPIObject(), GetNumJoints() );
  575. }
  576. GLuint buffer = reinterpret_cast< GLuint > ( apiObject );
  577. qglBindBufferARB( GL_UNIFORM_BUFFER, 0 );
  578. qglDeleteBuffersARB( 1, & buffer );
  579. ClearWithoutFreeing();
  580. }
  581. /*
  582. ========================
  583. idJointBuffer::Reference
  584. ========================
  585. */
  586. void idJointBuffer::Reference( const idJointBuffer & other ) {
  587. assert( IsMapped() == false );
  588. assert( other.IsMapped() == false );
  589. assert( other.GetAPIObject() != NULL );
  590. assert( other.GetNumJoints() > 0 );
  591. FreeBufferObject();
  592. numJoints = other.GetNumJoints(); // this strips the MAPPED_FLAG
  593. offsetInOtherBuffer = other.GetOffset(); // this strips the OWNS_BUFFER_FLAG
  594. apiObject = other.apiObject;
  595. assert( OwnsBuffer() == false );
  596. }
  597. /*
  598. ========================
  599. idJointBuffer::Reference
  600. ========================
  601. */
  602. void idJointBuffer::Reference( const idJointBuffer & other, int jointRefOffset, int numRefJoints ) {
  603. assert( IsMapped() == false );
  604. assert( other.IsMapped() == false );
  605. assert( other.GetAPIObject() != NULL );
  606. assert( jointRefOffset >= 0 );
  607. assert( numRefJoints >= 0 );
  608. assert( jointRefOffset + numRefJoints * sizeof( idJointMat ) <= other.GetNumJoints() * sizeof( idJointMat ) );
  609. assert_16_byte_aligned( numRefJoints * 3 * 4 * sizeof( float ) );
  610. FreeBufferObject();
  611. numJoints = numRefJoints;
  612. offsetInOtherBuffer = other.GetOffset() + jointRefOffset;
  613. apiObject = other.apiObject;
  614. assert( OwnsBuffer() == false );
  615. }
  616. /*
  617. ========================
  618. idJointBuffer::Update
  619. ========================
  620. */
  621. void idJointBuffer::Update( const float * joints, int numUpdateJoints ) const {
  622. assert( apiObject != NULL );
  623. assert( IsMapped() == false );
  624. assert_16_byte_aligned( joints );
  625. assert( ( GetOffset() & 15 ) == 0 );
  626. if ( numUpdateJoints > numJoints ) {
  627. idLib::FatalError( "idJointBuffer::Update: size overrun, %i > %i\n", numUpdateJoints, numJoints );
  628. }
  629. const int numBytes = numUpdateJoints * 3 * 4 * sizeof( float );
  630. qglBindBufferARB( GL_UNIFORM_BUFFER, reinterpret_cast< GLuint >( apiObject ) );
  631. qglBufferSubDataARB( GL_UNIFORM_BUFFER, GetOffset(), (GLsizeiptrARB)numBytes, joints );
  632. }
  633. /*
  634. ========================
  635. idJointBuffer::MapBuffer
  636. ========================
  637. */
  638. float * idJointBuffer::MapBuffer( bufferMapType_t mapType ) const {
  639. assert( IsMapped() == false );
  640. assert( mapType == BM_WRITE );
  641. assert( apiObject != NULL );
  642. int numBytes = GetAllocedSize();
  643. void * buffer = NULL;
  644. qglBindBufferARB( GL_UNIFORM_BUFFER, reinterpret_cast< GLuint >( apiObject ) );
  645. numBytes = numBytes;
  646. assert( GetOffset() == 0 );
  647. //buffer = qglMapBufferARB( GL_UNIFORM_BUFFER, GL_WRITE_ONLY_ARB );
  648. buffer = qglMapBufferRange( GL_UNIFORM_BUFFER, 0, GetAllocedSize(), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
  649. if ( buffer != NULL ) {
  650. buffer = (byte *)buffer + GetOffset();
  651. }
  652. SetMapped();
  653. if ( buffer == NULL ) {
  654. idLib::FatalError( "idJointBuffer::MapBuffer: failed" );
  655. }
  656. return (float *) buffer;
  657. }
  658. /*
  659. ========================
  660. idJointBuffer::UnmapBuffer
  661. ========================
  662. */
  663. void idJointBuffer::UnmapBuffer() const {
  664. assert( apiObject != NULL );
  665. assert( IsMapped() );
  666. qglBindBufferARB( GL_UNIFORM_BUFFER, reinterpret_cast< GLuint >( apiObject ) );
  667. if ( !qglUnmapBufferARB( GL_UNIFORM_BUFFER ) ) {
  668. idLib::Printf( "idJointBuffer::UnmapBuffer failed\n" );
  669. }
  670. SetUnmapped();
  671. }
  672. /*
  673. ========================
  674. idJointBuffer::ClearWithoutFreeing
  675. ========================
  676. */
  677. void idJointBuffer::ClearWithoutFreeing() {
  678. numJoints = 0;
  679. offsetInOtherBuffer = OWNS_BUFFER_FLAG;
  680. apiObject = NULL;
  681. }
  682. /*
  683. ========================
  684. idJointBuffer::Swap
  685. ========================
  686. */
  687. void idJointBuffer::Swap( idJointBuffer & other ) {
  688. // Make sure the ownership of the buffer is not transferred to an unintended place.
  689. assert( other.OwnsBuffer() == OwnsBuffer() );
  690. SwapValues( other.numJoints, numJoints );
  691. SwapValues( other.offsetInOtherBuffer, offsetInOtherBuffer );
  692. SwapValues( other.apiObject, apiObject );
  693. }