# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

vertex_buffer_heap.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000-2002 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/vertex_buffer_heap.h"
00029 #include "nel/misc/common.h"
00030 
00031 using namespace NLMISC;
00032 
00033 
00034 namespace NL3D
00035 {
00036 
00037 // ***************************************************************************
00038 CVertexBufferHeap::CVertexBufferHeap()
00039 {
00040         _Enabled= false;
00041         _HardMode= false;
00042 
00043         _HeapStart= NULL;
00044         _VertexFormat= 0;
00045         _VertexSize= 0;
00046         _MaxVertices= 0;
00047 }
00048 
00049 // ***************************************************************************
00050 CVertexBufferHeap::~CVertexBufferHeap()
00051 {
00052         release();
00053 }
00054 
00055 // ***************************************************************************
00056 void                    CVertexBufferHeap::init(IDriver *driver, uint vertexFormat, uint maxVertices)
00057 {
00058         nlassert(driver);
00059         // clean before.
00060         release();
00061 
00062         // setup
00063         _Driver= driver;
00064 
00065         // setup the vertexBuffer soft with queried info.
00066         _VBSoft.setVertexFormat(vertexFormat);
00067         // VertexSize must be a multitple of 4 (Heap alignement ...)
00068         nlassert( (_VBSoft.getVertexSize()&3) == 0)
00069 
00070         // create the VBHard, if possible
00071         _VBHard= driver->createVertexBufferHard(_VBSoft.getVertexFormat(), _VBSoft.getValueTypePointer(), maxVertices, IDriver::VBHardAGP);
00072 
00073         // Ok
00074         _Enabled= true;
00075         _VertexFormat= _VBSoft.getVertexFormat();
00076         _VertexSize= _VBSoft.getVertexSize();
00077         _MaxVertices= maxVertices;
00078 
00079         // Use hard or soft ???
00080         if(_VBHard)
00081         {
00082                 _HardMode= true;
00083                 // setup heap start with good AGP ptr.
00084                 _HeapStart= (uint8*)_VBHard->lock();
00085                 // just a gestion lock, no vertices have changed.
00086                 _VBHard->unlock(0,0);
00087         }
00088         else
00089         {
00090                 _HardMode= false;
00091                 // => allocate soft one.
00092                 _VBSoft.setNumVertices(maxVertices);
00093                 // setup heap start with good ptr.
00094                 _HeapStart= (uint8*)_VBSoft.getVertexCoordPointer();
00095         }
00096 
00097         // setup heap Manager with good ptr.
00098         _HeapManager.initHeap(_HeapStart, _MaxVertices*_VertexSize);
00099 }
00100 // ***************************************************************************
00101 void                    CVertexBufferHeap::release()
00102 {
00103         if(_VBHard)
00104         {
00105                 nlassert(_Driver);
00106                 _Driver->deleteVertexBufferHard(_VBHard);
00107         }
00108         _VBHard= NULL;
00109         _Driver= NULL;
00110         _HeapStart= NULL;
00111         // release all memory
00112         contReset(_VBSoft);
00113         contReset(_HeapManager);
00114 
00115         _Enabled= false;
00116         _HardMode= false;
00117         _VertexFormat= 0;
00118         _VertexSize= 0;
00119         _MaxVertices= 0;
00120 }
00121 
00122 // ***************************************************************************
00123 bool                    CVertexBufferHeap::allocate(uint numVertices, uint &indexStart)
00124 {
00125         nlassert(enabled());
00126 
00127         // allocate into the heap ?
00128         uint8   *ptr= (uint8*)_HeapManager.allocate(numVertices*getVertexSize());
00129         if(!ptr)
00130                 return false;
00131         else
00132         {
00133                 // compute vertex index
00134                 indexStart= (uint)(ptr-_HeapStart);
00135                 indexStart/= _VertexSize;
00136                 return true;
00137         }
00138 }
00139 // ***************************************************************************
00140 void                    CVertexBufferHeap::free(uint indexStart)
00141 {
00142         nlassert(enabled());
00143 
00144         // compute ptr to free
00145         uint8   *ptr= _HeapStart + indexStart*_VertexSize;
00146         // free it.
00147         _HeapManager.free(ptr);
00148 }
00149 
00150 // ***************************************************************************
00151 uint8                   *CVertexBufferHeap::lock(uint indexStart)
00152 {
00153         nlassert(enabled());
00154 
00155         uint8   *ptr;
00156         if(_HardMode)
00157         {
00158                 // lock the vbHard
00159                 ptr= (uint8*)_VBHard->lock();
00160                 nlassert(ptr==_HeapStart);
00161         }
00162         else
00163                 ptr= _HeapStart;
00164 
00165         // index it with index
00166         return ptr + indexStart*_VertexSize;
00167 }
00168 // ***************************************************************************
00169 void                    CVertexBufferHeap::unlock(uint startVert, uint endVert)
00170 {
00171         nlassert(enabled());
00172 
00173         if(_HardMode)
00174                 _VBHard->unlock(startVert, endVert);
00175 }
00176 // ***************************************************************************
00177 void                    CVertexBufferHeap::activate()
00178 {
00179         nlassert(enabled());
00180 
00181         if(_HardMode)
00182                 _Driver->activeVertexBufferHard(_VBHard);
00183         else
00184                 _Driver->activeVertexBuffer(_VBSoft);
00185 }
00186 
00187 
00188 } // NL3D