00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __NEBUFFER_H
00028 #define __NEBUFFER_H
00029
00030
00037 #include "base.h"
00038 #include "pointer.h"
00039 #include "core.h"
00040 #include "logstream.h"
00041
00042 #include <string>
00043 #include <assert.h>
00044
00045
00046 namespace NeoEngine
00047 {
00048
00049
00094 class NEOENGINE_API Buffer : public RefCounter
00095 {
00096 public:
00097
00102 enum BUFFERTYPE
00103 {
00105 DYNAMIC = 0x00000000,
00106
00108 STATIC = 0x00000001,
00109
00111 READPRIORITIZED = 0x00000002,
00112
00114 WRITEPRIORITIZED = 0x00000004,
00115
00117 NOREADWRITE = 0x00000008,
00118
00120 NORENDER = 0x00000010,
00121
00123 NORMAL = ( DYNAMIC | READPRIORITIZED | WRITEPRIORITIZED )
00124 };
00125
00130 enum BUFFERUPLOADPOLICY
00131 {
00133 ONUNLOCK,
00134
00136 ONRENDER,
00137
00139 ONFLUSH
00140 };
00141
00142
00147 enum BUFFERLOCK
00148 {
00150 READ = 1,
00151
00153 WRITE = 2,
00154
00156 NOUPLOAD = 4,
00157
00159 FORCEUPLOAD = 8
00160 };
00161
00162
00163 protected:
00164
00166 unsigned int m_uiType;
00167
00169 unsigned int m_uiLock;
00170
00172 bool m_bDirty;
00173
00175 unsigned int m_uiNumAllocated;
00176
00178 unsigned int m_uiNumCurrent;
00179
00184 virtual bool AcquireLock() = 0;
00185
00189 virtual void ReleaseLock() = 0;
00190
00191
00192 public:
00193
00195 static BUFFERUPLOADPOLICY s_eUploadPolicy;
00196
00197
00200 Buffer( unsigned int uiType ) : RefCounter(), m_uiType( uiType ), m_uiLock( 0 ), m_bDirty( false ), m_uiNumAllocated( 0 ), m_uiNumCurrent( 0 ) {}
00201
00204 virtual ~Buffer()
00205 {
00206 if( m_uiLock )
00207 {
00208 neolog << LogLevel( WARNING ) << "*** Active lock present when destroying buffer" << std::endl;
00209 assert( false );
00210 }
00211 }
00212
00218 inline bool Lock( unsigned int uiLockType )
00219 {
00220 #ifdef _DEBUG
00221 if( m_uiLock )
00222 {
00223 neolog << LogLevel( WARNING ) << "*** Unable to lock buffer: active lock present" << std::endl;
00224 return false;
00225 }
00226
00227 if( m_uiType & Buffer::NOREADWRITE )
00228 {
00229 neolog << LogLevel( ERROR ) << "*** Unable to lock buffer: NOREADWRITE buffer" << std::endl;
00230 return false;
00231 }
00232 #endif
00233
00234 m_uiLock = uiLockType;
00235 m_bDirty |= ( ( m_uiLock & WRITE ) != 0 );
00236
00237 if( !AcquireLock() )
00238 {
00239 m_uiLock = 0;
00240 return false;
00241 }
00242
00243 return true;
00244 }
00245
00249 inline void Unlock()
00250 {
00251 if( m_uiLock )
00252 {
00253 bool bUpload = ( m_bDirty && ( ( s_eUploadPolicy == ONUNLOCK ) || ( m_uiLock & FORCEUPLOAD ) ) && !( m_uiLock & NOUPLOAD ) );
00254
00255 ReleaseLock();
00256
00257 if( bUpload )
00258 Upload();
00259
00260 m_uiLock = 0;
00261 }
00262 }
00263
00267 virtual void Upload() { m_bDirty = false; }
00268
00272 inline bool IsLocked() const { return( m_uiLock != 0 ); }
00273
00277 inline bool IsDirty() const { return m_bDirty; }
00278
00282 inline unsigned int GetType() const { return m_uiType; }
00283
00287 inline unsigned int GetNumAllocated() const { return m_uiNumAllocated; }
00288
00292 inline unsigned int GetNumElements() const { return m_uiNumCurrent; }
00293
00298 inline void SetNumElements( unsigned int uiNumElements ) { m_uiNumCurrent = ( ( uiNumElements > m_uiNumAllocated ) ? m_uiNumAllocated : uiNumElements ); }
00299
00305 static std::string GetTypeAsString( unsigned int uiType );
00306
00312 static unsigned int GetTypeFromString( std::string const &strType );
00313 };
00314
00315
00316 };
00317
00318
00319 #endif
00320
00321