"L. Spiro Engine"

F:/My Projects/LSEngine/Modules/LSGraphicsLib/Src/DirectX/LSGDirectX9VertexBuffer.h

00001 
00017 #ifndef __LSG_DIRECTX9VERTEXBUFFER_H__
00018 #define __LSG_DIRECTX9VERTEXBUFFER_H__
00019 
00020 #include "../LSGGraphicsLib.h"
00021 
00022 #ifdef LSG_DX9
00023 
00024 #include "../DirectX/LSGDirectX9LosableResource.h"
00025 #include "../VertexBuffer/LSGVertexBufferBase.h"
00026 #include "CriticalSection/LSHCriticalSection.h"
00027 #include "Vector/LSTLVectorPoD.h"
00028 
00029 namespace lsg {
00030 
00038         class CDirectX9VertexBuffer : public CVertexBufferBase, public CDirectX9LosableResource {
00039         public :
00040                 // == Various constructors.
00041                 LSE_CALLCTOR                                                                    CDirectX9VertexBuffer();
00042                 LSE_CALLCTOR                                                                    ~CDirectX9VertexBuffer();
00043 
00044 
00045                 // == Functions.
00051                 LSE_INLINE D3DPRIMITIVETYPE LSE_CALL                    GetDirectX9PrimitiveType() const;
00052 
00056                 virtual LSVOID LSE_CALL                                                 OnDeviceLost();
00057 
00063                 virtual LSBOOL LSE_CALL                                                 OnDeviceReset();
00064 
00065         protected :
00066                 // == Types.
00070                 typedef struct LSG_DIRECTX9_PREFINED_VERT_ATT {
00074                         LSUINT32                                                                        ui32Mask;
00075 
00079                         LSUINT32                                                                        ui32Size;
00080                 } * LPLSG_DIRECTX9_PREFINED_VERT_ATT, * const LPCLSG_DIRECTX9_PREFINED_VERT_ATT;
00081 
00085                 typedef struct LSG_DIRECTX9_LAST_VERT_DECLS {
00089                         LSUINT32                                                                        ui32StreamIds[LSG_VBM_MAX_STREAMS];
00090 
00094                         IDirect3DVertexDeclaration9 *                           pvdVertexDeclaration;
00095 
00096 
00097                         // == Various constructors.
00098                         LSE_CALLCTOR                                                            LSG_DIRECTX9_LAST_VERT_DECLS();
00099                         LSE_CALLCTOR                                                            ~LSG_DIRECTX9_LAST_VERT_DECLS();
00100                 } * LPLSG_DIRECTX9_LAST_VERT_DECLS, * const LPCLSG_DIRECTX9_LAST_VERT_DECLS;
00101 
00102 
00103                 // == Members.
00107                 IDirect3DVertexBuffer9 *                                                m_pvbVertexBuffer;
00108 
00112                 D3DVERTEXELEMENT9 *                                                             m_pvdVertexDeclaration;
00113 
00117                 CVectorPoD<LSG_DIRECTX9_LAST_VERT_DECLS, LSUINT8, 1UL>
00118                                                                                                                 m_vVertDeclRecord;
00119 
00123                 LSUINT32                                                                                m_ui32VertexDeclStream;
00124 
00128                 LSUINT32                                                                                m_ui32VertexDeclLength;
00129 
00133                 D3DPRIMITIVETYPE                                                                m_ptPrimitiveType;
00134 
00138                 LSBOOL                                                                                  m_bFakeAlpha[LSG_VBM_MAX_USAGES];
00139 
00143                 static LSG_DIRECTX9_PREFINED_VERT_ATT                   m_dpvaVertices[];
00144 
00149                 static CCriticalSection                                                 m_csCrit;
00150 
00151 
00152                 // == Functions.
00158                 LSBOOL LSE_CALL                                                                 CreateApiVertexBuffer();
00159 
00166                 LSVOID LSE_CALL                                                                 ResetApi( LSBOOL _bFull = true );
00167 
00171                 LSVOID LSE_CALL                                                                 UnLockApi();
00172                 
00182                 LSVOID LSE_CALL                                                                 AdjustInputsToApi( LSG_VB_CONTENTS &_vcContents, LSUINT16 &_ui16UsageAndAccess );
00183 
00190                 LSUINT32 LSE_CALL                                                               GetVertexAttributeSizeApi( const LSG_VB_CONTENTS &_vcContents );
00191 
00200                 LSBOOL LSE_CALL                                                                 SetStreamApi( LSUINT32 _ui32Stream, LSUINT32 _ui32Offset, LSUINT32 _ui32ElementsSize );
00201 
00210                 LSBOOL LSE_CALL                                                                 CanBufferBeDestroyedApi( LSUINT32 _ui32Elements, LSUINT32 _ui32ElementsSize, LSUINT32 _ui32UsageAndAccess );
00211 
00221                 LSUINT32 LSE_CALL                                                               RawColorToApiColor( LSREAL _fR, LSREAL _fG, LSREAL _fB, LSVOID * _pvBuffer );
00222 
00233                 LSUINT32 LSE_CALL                                                               RawColorToApiColor( LSREAL _fR, LSREAL _fG, LSREAL _fB, LSREAL _fA, LSVOID * _pvBuffer );
00234 
00243                 LSVOID LSE_CALL                                                                 SetColorApi( LSREAL _fR, LSREAL _fG, LSREAL _fB, LSVOID * _pvBuffer );
00244 
00251                 LSVOID LSE_CALL                                                                 SetAlphaApi( LSREAL _fA, LSVOID * _pvBuffer );
00252 
00256                 LSVOID LSE_CALL                                                                 PrepareToRenderApi();
00257 
00261                 LSVOID LSE_CALL                                                                 EndRenderApi();
00262 
00269                 LSVOID LSE_CALL                                                                 RenderApi( LSUINT32 _ui32TotalPrimitives );
00270 
00277                 LSUINT32 LSE_CALL                                                               BestStrideApi( LSUINT32 _ui32Size );
00278 
00279         private :
00286                 LSBOOL LSE_CALL                                                                 GenerateVertexDeclaration( const LSG_VB_CONTENTS &_vcContents );
00287         };
00288 
00289 
00290         // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00291         // DEFINITIONS
00292         // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00293         // == Functions.
00299         LSE_INLINE D3DPRIMITIVETYPE LSE_CALL CDirectX9VertexBuffer::GetDirectX9PrimitiveType() const {
00300                 return m_ptPrimitiveType;
00301         }
00302 
00312         LSE_INLINE LSUINT32 LSE_CALL CDirectX9VertexBuffer::RawColorToApiColor( LSREAL _fR, LSREAL _fG, LSREAL _fB, LSVOID * _pvBuffer ) {
00313                 LSUINT32 * pui32Dest = static_cast<LSUINT32 *>(_pvBuffer);
00314                 // 0xAARRGGBB.
00315                 (*pui32Dest) = 0xFF |                                                                                                                                                                                   // Alpha.
00316                         (static_cast<LSUINT32>((CMathLib::Clamp( _fR, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 16) |       // Red.
00317                         (static_cast<LSUINT32>((CMathLib::Clamp( _fG, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 8) |        // Green.
00318                         static_cast<LSUINT32>((CMathLib::Clamp( _fB, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0)));                        // Blue.
00319                 return sizeof( LSUINT32 );
00320         }
00321 
00332         LSE_INLINE LSUINT32 LSE_CALL CDirectX9VertexBuffer::RawColorToApiColor( LSREAL _fR, LSREAL _fG, LSREAL _fB, LSREAL _fA, LSVOID * _pvBuffer ) {
00333                 LSUINT32 * pui32Dest = static_cast<LSUINT32 *>(_pvBuffer);
00334                 // 0xAARRGGBB.
00335                 (*pui32Dest) =
00336                         (static_cast<LSUINT32>((CMathLib::Clamp( _fA, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 24) |       // Alpha.
00337                         (static_cast<LSUINT32>((CMathLib::Clamp( _fR, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 16) |       // Red.
00338                         (static_cast<LSUINT32>((CMathLib::Clamp( _fG, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 8) |        // Green.
00339                         static_cast<LSUINT32>((CMathLib::Clamp( _fB, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0)));                        // Blue.
00340                 return sizeof( LSUINT32 );
00341         }
00342 
00351         LSE_INLINE LSVOID LSE_CALL CDirectX9VertexBuffer::SetColorApi( LSREAL _fR, LSREAL _fG, LSREAL _fB, LSVOID * _pvBuffer ) {
00352                 LSUINT32 * pui32Dest = static_cast<LSUINT32 *>(_pvBuffer);
00353                 LSUINT32 ui32Temp = 
00354                         (static_cast<LSUINT32>((CMathLib::Clamp( _fR, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 16) |       // Red.
00355                         (static_cast<LSUINT32>((CMathLib::Clamp( _fG, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 8) |        // Green.
00356                         static_cast<LSUINT32>((CMathLib::Clamp( _fB, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0)));                        // Blue.
00357                 ui32Temp |= (*pui32Dest) & 0xFF000000;  // Keep the existing alpha.
00358                 (*pui32Dest) = ui32Temp;
00359         }
00360 
00367         LSE_INLINE LSVOID LSE_CALL CDirectX9VertexBuffer::SetAlphaApi( LSREAL _fA, LSVOID * _pvBuffer ) {
00368                 LSUINT32 * pui32Dest = static_cast<LSUINT32 *>(_pvBuffer);
00369                 LSUINT32 ui32Temp = 
00370                         (static_cast<LSUINT32>((CMathLib::Clamp( _fA, LSM_ZERO, LSM_ONE ) * static_cast<LSREAL>(255.0))) << 24);        // Alpha.
00371                 ui32Temp |= (*pui32Dest) & 0x00FFFFFF;  // Keep the existing color.
00372                 (*pui32Dest) = ui32Temp;
00373         }
00374 
00375 }       // namespace lsg
00376 
00377 #endif  // #ifdef LSG_DX9
00378 
00379 #endif  // __LSG_DIRECTX9VERTEXBUFFER_H__
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator