1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "nel/3d/vertex_buffer_heap.h"
23 #include "nel/misc/common.h"
25 using namespace NLMISC
;
31 void vertex_buffer_heap_dummy_cpp() { }
33 // This code is not used actually and doesn't compile
34 // just preproc comment it
40 // ***************************************************************************
41 CVertexBufferHeap::CVertexBufferHeap()
52 // ***************************************************************************
53 CVertexBufferHeap::~CVertexBufferHeap()
58 // ***************************************************************************
59 void CVertexBufferHeap::init(IDriver
*driver
, uint vertexFormat
, uint maxVertices
)
68 // setup the vertexBuffer soft with queried info.
69 _VBSoft
.setVertexFormat(vertexFormat
);
70 // VertexSize must be a multitple of 4 (Heap alignement ...)
71 nlassert( (_VBSoft
.getVertexSize()&3) == 0);
73 // create the VBHard, if possible
74 _VBHard
= driver
->createVertexBufferHard(_VBSoft
.getVertexFormat(), _VBSoft
.getValueTypePointer(), maxVertices
, IDriver::VBHardAGP
, _VBSoft
.getUVRouting());
78 _VertexFormat
= _VBSoft
.getVertexFormat();
79 _VertexSize
= _VBSoft
.getVertexSize();
80 _MaxVertices
= maxVertices
;
82 // Use hard or soft ???
86 // setup heap start with good AGP ptr.
87 _HeapStart
= (uint8
*)_VBHard
->lock();
88 // just a gestion lock, no vertices have changed.
90 // In essence, the VBHeap is rarely modified (but on mesh loading). => set it static
91 _VBHard
->lockHintStatic(true);
96 // => allocate soft one.
97 _VBSoft
.setNumVertices(maxVertices
);
98 // setup heap start with good ptr.
99 _HeapStart
= (uint8
*)_VBSoft
.getVertexCoordPointer();
102 // setup heap Manager with good ptr.
103 _HeapManager
.initHeap(_HeapStart
, _MaxVertices
*_VertexSize
);
105 // ***************************************************************************
106 void CVertexBufferHeap::release()
111 _Driver
->deleteVertexBufferHard(_VBHard
);
116 // release all memory
118 contReset(_HeapManager
);
127 // ***************************************************************************
128 bool CVertexBufferHeap::allocate(uint numVertices
, uint
&indexStart
)
132 // allocate into the heap ?
133 uint8
*ptr
= (uint8
*)_HeapManager
.allocate(numVertices
*getVertexSize());
138 // compute vertex index
139 indexStart
= (uint
)(ptr
-_HeapStart
);
140 indexStart
/= _VertexSize
;
144 // ***************************************************************************
145 void CVertexBufferHeap::free(uint indexStart
)
149 // compute ptr to free
150 uint8
*ptr
= _HeapStart
+ indexStart
*_VertexSize
;
152 _HeapManager
.free(ptr
);
155 // ***************************************************************************
156 uint8
*CVertexBufferHeap::lock(uint indexStart
)
164 ptr
= (uint8
*)_VBHard
->lock();
165 nlassert(ptr
==_HeapStart
);
170 // index it with index
171 return ptr
+ indexStart
*_VertexSize
;
173 // ***************************************************************************
174 void CVertexBufferHeap::unlock(uint startVert
, uint endVert
)
179 _VBHard
->unlock(startVert
, endVert
);
181 // ***************************************************************************
182 void CVertexBufferHeap::activate()
187 _Driver
->activeVertexBufferHard(_VBHard
);
189 _Driver
->activeVertexBuffer(_VBSoft
);