2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-12-07 16:06:44 +00:00
|
|
|
Copyright (C) 2012 Robert Beckebans
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
#pragma hdrstop
|
|
|
|
#include "../idlib/precompiled.h"
|
|
|
|
#include "tr_local.h"
|
|
|
|
|
|
|
|
idCVar r_showBuffers( "r_showBuffers", "0", CVAR_INTEGER, "" );
|
|
|
|
|
|
|
|
|
|
|
|
//static const GLenum bufferUsage = GL_STATIC_DRAW_ARB;
|
|
|
|
static const GLenum bufferUsage = GL_DYNAMIC_DRAW_ARB;
|
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB begin
|
|
|
|
#if defined(_WIN32)
|
2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
IsWriteCombined
|
|
|
|
==================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool IsWriteCombined( void* base )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
MEMORY_BASIC_INFORMATION info;
|
|
|
|
SIZE_T size = VirtualQueryEx( GetCurrentProcess(), base, &info, sizeof( info ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( size == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
DWORD error = GetLastError();
|
|
|
|
error = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool isWriteCombined = ( ( info.AllocationProtect & PAGE_WRITECOMBINE ) != 0 );
|
|
|
|
return isWriteCombined;
|
|
|
|
}
|
2012-12-07 16:06:44 +00:00
|
|
|
#endif
|
|
|
|
// RB end
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
================================================================================================
|
|
|
|
|
|
|
|
Buffer Objects
|
|
|
|
|
|
|
|
================================================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
UnbindBufferObjects
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void UnbindBufferObjects()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
|
|
|
|
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void CopyBuffer( byte* dst, const byte* src, int numBytes )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert_16_byte_aligned( dst );
|
|
|
|
assert_16_byte_aligned( src );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int i = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i + 128 <= numBytes; i += 128 )
|
|
|
|
{
|
|
|
|
__m128i d0 = _mm_load_si128( ( __m128i* )&src[i + 0 * 16] );
|
|
|
|
__m128i d1 = _mm_load_si128( ( __m128i* )&src[i + 1 * 16] );
|
|
|
|
__m128i d2 = _mm_load_si128( ( __m128i* )&src[i + 2 * 16] );
|
|
|
|
__m128i d3 = _mm_load_si128( ( __m128i* )&src[i + 3 * 16] );
|
|
|
|
__m128i d4 = _mm_load_si128( ( __m128i* )&src[i + 4 * 16] );
|
|
|
|
__m128i d5 = _mm_load_si128( ( __m128i* )&src[i + 5 * 16] );
|
|
|
|
__m128i d6 = _mm_load_si128( ( __m128i* )&src[i + 6 * 16] );
|
|
|
|
__m128i d7 = _mm_load_si128( ( __m128i* )&src[i + 7 * 16] );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 0 * 16], d0 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 1 * 16], d1 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 2 * 16], d2 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 3 * 16], d3 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 4 * 16], d4 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 5 * 16], d5 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 6 * 16], d6 );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i + 7 * 16], d7 );
|
|
|
|
}
|
|
|
|
for( ; i + 16 <= numBytes; i += 16 )
|
|
|
|
{
|
|
|
|
__m128i d = _mm_load_si128( ( __m128i* )&src[i] );
|
|
|
|
_mm_stream_si128( ( __m128i* )&dst[i], d );
|
|
|
|
}
|
|
|
|
for( ; i + 4 <= numBytes; i += 4 )
|
|
|
|
{
|
|
|
|
*( uint32* )&dst[i] = *( const uint32* )&src[i];
|
|
|
|
}
|
|
|
|
for( ; i < numBytes; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
dst[i] = src[i];
|
|
|
|
}
|
|
|
|
_mm_sfence();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
================================================================================================
|
|
|
|
|
|
|
|
idVertexBuffer
|
|
|
|
|
|
|
|
================================================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::idVertexBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVertexBuffer::idVertexBuffer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size = 0;
|
|
|
|
offsetInOtherBuffer = OWNS_BUFFER_FLAG;
|
|
|
|
apiObject = NULL;
|
|
|
|
SetUnmapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::~idVertexBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVertexBuffer::~idVertexBuffer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::AllocBufferObject
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idVertexBuffer::AllocBufferObject( const void* data, int allocSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject == NULL );
|
|
|
|
assert_16_byte_aligned( data );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( allocSize <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Error( "idVertexBuffer::AllocBufferObject: allocSize = %i", allocSize );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
size = allocSize;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
bool allocationFailed = false;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numBytes = GetAllocedSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// clear out any previous error
|
|
|
|
qglGetError();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
GLuint bufferObject = 0xFFFF;
|
|
|
|
qglGenBuffersARB( 1, & bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( bufferObject == 0xFFFF )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idVertexBuffer::AllocBufferObject: failed" );
|
|
|
|
}
|
|
|
|
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// these are rewritten every frame
|
|
|
|
qglBufferDataARB( GL_ARRAY_BUFFER_ARB, numBytes, NULL, bufferUsage );
|
2012-11-28 15:47:07 +00:00
|
|
|
apiObject = reinterpret_cast< void* >( bufferObject );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
GLenum err = qglGetError();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( err == GL_OUT_OF_MEMORY )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Warning( "idVertexBuffer::AllocBufferObject: allocation failed" );
|
|
|
|
allocationFailed = true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( r_showBuffers.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "vertex buffer alloc %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// copy the data
|
2012-11-28 15:47:07 +00:00
|
|
|
if( data != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Update( data, allocSize );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return !allocationFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::FreeBufferObject
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVertexBuffer::FreeBufferObject()
|
|
|
|
{
|
|
|
|
if( IsMapped() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnmapBuffer();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if this is a sub-allocation inside a larger buffer, don't actually free anything.
|
2012-11-28 15:47:07 +00:00
|
|
|
if( OwnsBuffer() == false )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ClearWithoutFreeing();
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( apiObject == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( r_showBuffers.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "vertex buffer free %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
qglDeleteBuffersARB( 1, (const unsigned int*) & bufferObject );
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
ClearWithoutFreeing();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::Reference
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVertexBuffer::Reference( const idVertexBuffer& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
//assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up idDrawVerts
|
|
|
|
assert( other.GetAPIObject() != NULL );
|
|
|
|
assert( other.GetSize() > 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
size = other.GetSize(); // this strips the MAPPED_FLAG
|
|
|
|
offsetInOtherBuffer = other.GetOffset(); // this strips the OWNS_BUFFER_FLAG
|
|
|
|
apiObject = other.apiObject;
|
|
|
|
assert( OwnsBuffer() == false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::Reference
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVertexBuffer::Reference( const idVertexBuffer& other, int refOffset, int refSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
//assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up idDrawVerts
|
|
|
|
assert( other.GetAPIObject() != NULL );
|
|
|
|
assert( refOffset >= 0 );
|
|
|
|
assert( refSize >= 0 );
|
|
|
|
assert( refOffset + refSize <= other.GetSize() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
size = refSize;
|
|
|
|
offsetInOtherBuffer = other.GetOffset() + refOffset;
|
|
|
|
apiObject = other.apiObject;
|
|
|
|
assert( OwnsBuffer() == false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::Update
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVertexBuffer::Update( const void* data, int updateSize ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() == false );
|
|
|
|
assert_16_byte_aligned( data );
|
|
|
|
assert( ( GetOffset() & 15 ) == 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( updateSize > size )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idVertexBuffer::Update: size overrun, %i > %i\n", updateSize, GetSize() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numBytes = ( updateSize + 15 ) & ~15;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
qglBufferSubDataARB( GL_ARRAY_BUFFER_ARB, GetOffset(), ( GLsizeiptrARB )numBytes, data );
|
|
|
|
/*
|
|
|
|
void * buffer = MapBuffer( BM_WRITE );
|
|
|
|
CopyBuffer( (byte *)buffer + GetOffset(), (byte *)data, numBytes );
|
|
|
|
UnmapBuffer();
|
|
|
|
*/
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::MapBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void* idVertexBuffer::MapBuffer( bufferMapType_t mapType ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() == false );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
void* buffer = NULL;
|
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mapType == BM_READ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//buffer = qglMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_READ_ONLY_ARB );
|
|
|
|
buffer = qglMapBufferRange( GL_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_READ_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( buffer != NULL )
|
|
|
|
{
|
|
|
|
buffer = ( byte* )buffer + GetOffset();
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( mapType == BM_WRITE )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//buffer = qglMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
|
|
|
|
buffer = qglMapBufferRange( GL_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( buffer != NULL )
|
|
|
|
{
|
|
|
|
buffer = ( byte* )buffer + GetOffset();
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 20:48:52 +00:00
|
|
|
// assert( IsWriteCombined( buffer ) ); // commented out because it spams the console
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( false );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SetMapped();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( buffer == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idVertexBuffer::MapBuffer: failed" );
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::UnmapBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVertexBuffer::UnmapBuffer() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !qglUnmapBufferARB( GL_ARRAY_BUFFER_ARB ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "idVertexBuffer::UnmapBuffer failed\n" );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SetUnmapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idVertexBuffer::ClearWithoutFreeing
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVertexBuffer::ClearWithoutFreeing()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size = 0;
|
|
|
|
offsetInOtherBuffer = OWNS_BUFFER_FLAG;
|
|
|
|
apiObject = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================================================================================================
|
|
|
|
|
|
|
|
idIndexBuffer
|
|
|
|
|
|
|
|
================================================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::idIndexBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idIndexBuffer::idIndexBuffer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size = 0;
|
|
|
|
offsetInOtherBuffer = OWNS_BUFFER_FLAG;
|
|
|
|
apiObject = NULL;
|
|
|
|
SetUnmapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::~idIndexBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idIndexBuffer::~idIndexBuffer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::AllocBufferObject
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idIndexBuffer::AllocBufferObject( const void* data, int allocSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject == NULL );
|
|
|
|
assert_16_byte_aligned( data );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( allocSize <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Error( "idIndexBuffer::AllocBufferObject: allocSize = %i", allocSize );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
size = allocSize;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
bool allocationFailed = false;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numBytes = GetAllocedSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// clear out any previous error
|
|
|
|
qglGetError();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
GLuint bufferObject = 0xFFFF;
|
|
|
|
qglGenBuffersARB( 1, & bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( bufferObject == 0xFFFF )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
GLenum error = qglGetError();
|
|
|
|
idLib::FatalError( "idIndexBuffer::AllocBufferObject: failed - GL_Error %d", error );
|
|
|
|
}
|
|
|
|
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// these are rewritten every frame
|
|
|
|
qglBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, numBytes, NULL, bufferUsage );
|
2012-11-28 15:47:07 +00:00
|
|
|
apiObject = reinterpret_cast< void* >( bufferObject );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
GLenum err = qglGetError();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( err == GL_OUT_OF_MEMORY )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Warning( "idIndexBuffer:AllocBufferObject: allocation failed" );
|
|
|
|
allocationFailed = true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( r_showBuffers.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "index buffer alloc %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// copy the data
|
2012-11-28 15:47:07 +00:00
|
|
|
if( data != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Update( data, allocSize );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return !allocationFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::FreeBufferObject
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idIndexBuffer::FreeBufferObject()
|
|
|
|
{
|
|
|
|
if( IsMapped() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnmapBuffer();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if this is a sub-allocation inside a larger buffer, don't actually free anything.
|
2012-11-28 15:47:07 +00:00
|
|
|
if( OwnsBuffer() == false )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ClearWithoutFreeing();
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( apiObject == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( r_showBuffers.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "index buffer free %p, api %p (%i bytes)\n", this, GetAPIObject(), GetSize() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
qglDeleteBuffersARB( 1, ( const unsigned int* )& bufferObject );
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
ClearWithoutFreeing();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::Reference
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idIndexBuffer::Reference( const idIndexBuffer& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
//assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up triIndex_t
|
|
|
|
assert( other.GetAPIObject() != NULL );
|
|
|
|
assert( other.GetSize() > 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
size = other.GetSize(); // this strips the MAPPED_FLAG
|
|
|
|
offsetInOtherBuffer = other.GetOffset(); // this strips the OWNS_BUFFER_FLAG
|
|
|
|
apiObject = other.apiObject;
|
|
|
|
assert( OwnsBuffer() == false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::Reference
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idIndexBuffer::Reference( const idIndexBuffer& other, int refOffset, int refSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
//assert( other.IsMapped() == false ); // this happens when building idTriangles while at the same time setting up triIndex_t
|
|
|
|
assert( other.GetAPIObject() != NULL );
|
|
|
|
assert( refOffset >= 0 );
|
|
|
|
assert( refSize >= 0 );
|
|
|
|
assert( refOffset + refSize <= other.GetSize() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
size = refSize;
|
|
|
|
offsetInOtherBuffer = other.GetOffset() + refOffset;
|
|
|
|
apiObject = other.apiObject;
|
|
|
|
assert( OwnsBuffer() == false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::Update
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idIndexBuffer::Update( const void* data, int updateSize ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() == false );
|
|
|
|
assert_16_byte_aligned( data );
|
|
|
|
assert( ( GetOffset() & 15 ) == 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( updateSize > size )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idIndexBuffer::Update: size overrun, %i > %i\n", updateSize, GetSize() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numBytes = ( updateSize + 15 ) & ~15;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
qglBufferSubDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, GetOffset(), ( GLsizeiptrARB )numBytes, data );
|
|
|
|
/*
|
|
|
|
void * buffer = MapBuffer( BM_WRITE );
|
|
|
|
CopyBuffer( (byte *)buffer + GetOffset(), (byte *)data, numBytes );
|
|
|
|
UnmapBuffer();
|
|
|
|
*/
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::MapBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void* idIndexBuffer::MapBuffer( bufferMapType_t mapType ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() == false );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
void* buffer = NULL;
|
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( mapType == BM_READ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//buffer = qglMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, GL_READ_ONLY_ARB );
|
|
|
|
buffer = qglMapBufferRange( GL_ELEMENT_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_READ_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( buffer != NULL )
|
|
|
|
{
|
|
|
|
buffer = ( byte* )buffer + GetOffset();
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( mapType == BM_WRITE )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//buffer = qglMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
|
|
|
|
buffer = qglMapBufferRange( GL_ELEMENT_ARRAY_BUFFER_ARB, 0, GetAllocedSize(), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( buffer != NULL )
|
|
|
|
{
|
|
|
|
buffer = ( byte* )buffer + GetOffset();
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 20:48:52 +00:00
|
|
|
// assert( IsWriteCombined( buffer ) ); // commented out because it spams the console
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( false );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SetMapped();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( buffer == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idIndexBuffer::MapBuffer: failed" );
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::UnmapBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idIndexBuffer::UnmapBuffer() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB bufferObject = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferObject );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !qglUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "idIndexBuffer::UnmapBuffer failed\n" );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SetUnmapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idIndexBuffer::ClearWithoutFreeing
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idIndexBuffer::ClearWithoutFreeing()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size = 0;
|
|
|
|
offsetInOtherBuffer = OWNS_BUFFER_FLAG;
|
|
|
|
apiObject = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================================================================================================
|
|
|
|
|
|
|
|
idJointBuffer
|
|
|
|
|
|
|
|
================================================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::idJointBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idJointBuffer::idJointBuffer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
numJoints = 0;
|
|
|
|
offsetInOtherBuffer = OWNS_BUFFER_FLAG;
|
|
|
|
apiObject = NULL;
|
|
|
|
SetUnmapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::~idJointBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idJointBuffer::~idJointBuffer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::AllocBufferObject
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idJointBuffer::AllocBufferObject( const float* joints, int numAllocJoints )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject == NULL );
|
|
|
|
assert_16_byte_aligned( joints );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( numAllocJoints <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Error( "idJointBuffer::AllocBufferObject: joints = %i", numAllocJoints );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numJoints = numAllocJoints;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
bool allocationFailed = false;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
const int numBytes = GetAllocedSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
GLuint buffer = 0;
|
|
|
|
qglGenBuffersARB( 1, &buffer );
|
|
|
|
qglBindBufferARB( GL_UNIFORM_BUFFER, buffer );
|
|
|
|
qglBufferDataARB( GL_UNIFORM_BUFFER, numBytes, NULL, GL_STREAM_DRAW_ARB );
|
2012-11-28 15:47:07 +00:00
|
|
|
qglBindBufferARB( GL_UNIFORM_BUFFER, 0 );
|
|
|
|
apiObject = reinterpret_cast< void* >( buffer );
|
|
|
|
|
|
|
|
if( r_showBuffers.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "joint buffer alloc %p, api %p (%i joints)\n", this, GetAPIObject(), GetNumJoints() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// copy the data
|
2012-11-28 15:47:07 +00:00
|
|
|
if( joints != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Update( joints, numAllocJoints );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return !allocationFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::FreeBufferObject
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::FreeBufferObject()
|
|
|
|
{
|
|
|
|
if( IsMapped() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnmapBuffer();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if this is a sub-allocation inside a larger buffer, don't actually free anything.
|
2012-11-28 15:47:07 +00:00
|
|
|
if( OwnsBuffer() == false )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ClearWithoutFreeing();
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( apiObject == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( r_showBuffers.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "joint buffer free %p, api %p (%i joints)\n", this, GetAPIObject(), GetNumJoints() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
GLintptrARB buffer = reinterpret_cast< GLintptrARB >( apiObject );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
qglBindBufferARB( GL_UNIFORM_BUFFER, 0 );
|
2012-12-07 16:06:44 +00:00
|
|
|
qglDeleteBuffersARB( 1, (const GLuint*)& buffer );
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
ClearWithoutFreeing();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::Reference
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::Reference( const idJointBuffer& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
assert( other.IsMapped() == false );
|
|
|
|
assert( other.GetAPIObject() != NULL );
|
|
|
|
assert( other.GetNumJoints() > 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
numJoints = other.GetNumJoints(); // this strips the MAPPED_FLAG
|
|
|
|
offsetInOtherBuffer = other.GetOffset(); // this strips the OWNS_BUFFER_FLAG
|
|
|
|
apiObject = other.apiObject;
|
|
|
|
assert( OwnsBuffer() == false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::Reference
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::Reference( const idJointBuffer& other, int jointRefOffset, int numRefJoints )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
assert( other.IsMapped() == false );
|
|
|
|
assert( other.GetAPIObject() != NULL );
|
|
|
|
assert( jointRefOffset >= 0 );
|
|
|
|
assert( numRefJoints >= 0 );
|
|
|
|
assert( jointRefOffset + numRefJoints * sizeof( idJointMat ) <= other.GetNumJoints() * sizeof( idJointMat ) );
|
|
|
|
assert_16_byte_aligned( numRefJoints * 3 * 4 * sizeof( float ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeBufferObject();
|
|
|
|
numJoints = numRefJoints;
|
|
|
|
offsetInOtherBuffer = other.GetOffset() + jointRefOffset;
|
|
|
|
apiObject = other.apiObject;
|
|
|
|
assert( OwnsBuffer() == false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::Update
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::Update( const float* joints, int numUpdateJoints ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() == false );
|
|
|
|
assert_16_byte_aligned( joints );
|
|
|
|
assert( ( GetOffset() & 15 ) == 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( numUpdateJoints > numJoints )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idJointBuffer::Update: size overrun, %i > %i\n", numUpdateJoints, numJoints );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
const int numBytes = numUpdateJoints * 3 * 4 * sizeof( float );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
qglBindBufferARB( GL_UNIFORM_BUFFER, reinterpret_cast< GLintptrARB >( apiObject ) );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
qglBufferSubDataARB( GL_UNIFORM_BUFFER, GetOffset(), ( GLsizeiptrARB )numBytes, joints );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::MapBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
float* idJointBuffer::MapBuffer( bufferMapType_t mapType ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( IsMapped() == false );
|
|
|
|
assert( mapType == BM_WRITE );
|
|
|
|
assert( apiObject != NULL );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numBytes = GetAllocedSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
void* buffer = NULL;
|
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
qglBindBufferARB( GL_UNIFORM_BUFFER, reinterpret_cast< GLintptrARB >( apiObject ) );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numBytes = numBytes;
|
|
|
|
assert( GetOffset() == 0 );
|
|
|
|
//buffer = qglMapBufferARB( GL_UNIFORM_BUFFER, GL_WRITE_ONLY_ARB );
|
|
|
|
buffer = qglMapBufferRange( GL_UNIFORM_BUFFER, 0, GetAllocedSize(), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( buffer != NULL )
|
|
|
|
{
|
|
|
|
buffer = ( byte* )buffer + GetOffset();
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SetMapped();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( buffer == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::FatalError( "idJointBuffer::MapBuffer: failed" );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
return ( float* ) buffer;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::UnmapBuffer
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::UnmapBuffer() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( apiObject != NULL );
|
|
|
|
assert( IsMapped() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 16:06:44 +00:00
|
|
|
// RB: 64 bit fixes, changed GLuint to GLintptrARB
|
|
|
|
qglBindBufferARB( GL_UNIFORM_BUFFER, reinterpret_cast< GLintptrARB >( apiObject ) );
|
|
|
|
// RB end
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !qglUnmapBufferARB( GL_UNIFORM_BUFFER ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::Printf( "idJointBuffer::UnmapBuffer failed\n" );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SetUnmapped();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::ClearWithoutFreeing
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::ClearWithoutFreeing()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
numJoints = 0;
|
|
|
|
offsetInOtherBuffer = OWNS_BUFFER_FLAG;
|
|
|
|
apiObject = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idJointBuffer::Swap
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idJointBuffer::Swap( idJointBuffer& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// Make sure the ownership of the buffer is not transferred to an unintended place.
|
|
|
|
assert( other.OwnsBuffer() == OwnsBuffer() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
SwapValues( other.numJoints, numJoints );
|
|
|
|
SwapValues( other.offsetInOtherBuffer, offsetInOtherBuffer );
|
|
|
|
SwapValues( other.apiObject, apiObject );
|
|
|
|
}
|