OpenBarnyard
 
Loading...
Searching...
No Matches
TVertexBlockResource_DX8.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
6#include "Toshi/T2Pair.h"
7
8//-----------------------------------------------------------------------------
9// Enables memory debugging.
10// Note: Should be the last include!
11//-----------------------------------------------------------------------------
12#include "Core/TMemoryDebugOn.h"
13
15
18
19// $Barnyard: FUNCTION 006c0730
21{
22 TVALIDPTR( a_pPool );
23
24 m_uiVerticesUsed += a_pPool->GetNumVertices();
25 a_pPool->SetParent( this );
26
27 if ( m_uiFlags & 1 )
28 {
29 Invalidate();
30 }
31
32 return TTRUE;
33}
34
35// $Barnyard: FUNCTION 006c0670
37{
38 if ( m_uiFlags & 1 && a_pPoolResource->GetFlags() & 1 )
39 {
40 return m_uiMaxVertices >= a_pPoolResource->GetNumVertices() + m_uiVerticesUsed;
41 }
42
43 return TFALSE;
44}
45
46// $Barnyard: FUNCTION 006c02f0
48{
50 a_pPool->SetParent( Parent() == Tree()->GetRoot() ? TNULL : Parent() );
51
52 m_uiVerticesUsed -= a_pPool->GetNumVertices();
53
54 if ( m_uiVerticesUsed == 0 && Child() == TNULL )
55 {
57 return TTRUE;
58 }
59
60 return TFALSE;
61}
62
63// $Barnyard: FUNCTION 006c0520
65{
66 m_pFactory = a_pFactory;
67 m_uiMaxVertices = a_uiMaxVertices;
68 m_uiFlags = a_uiFlags;
69 return TResource::Create();
70}
71
72// $Barnyard: FUNCTION 006c0970
74{
75 DestroyHAL();
76
77 auto pRenderer = TRenderD3DInterface::Interface();
78 auto& vertexFormat = m_pFactory->GetVertexFormat();
79 m_HALBuffer.uiNumStreams = vertexFormat.m_uiNumStreams;
80
81 for ( TUINT i = 0; i < m_HALBuffer.uiNumStreams; i++ )
82 {
83 UINT length = vertexFormat.m_aStreamFormats[ i ].m_uiVertexSize * m_uiMaxVertices;
84 DWORD usage = D3DUSAGE_WRITEONLY;
85
86 if ( ISZERO( m_uiFlags & 1 ) )
87 {
88 usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
89 m_uiOffset = 0;
90 }
91
92 if ( !pRenderer->GetCurrentDevice()->SupportsHardwareTransfomations() )
93 {
94 usage |= D3DUSAGE_SOFTWAREPROCESSING;
95 }
96
97 HRESULT hRes = pRenderer->GetDirect3DDevice()->CreateVertexBuffer(
98 length,
99 usage,
100 0,
101 ( m_uiFlags & 1 ) ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT,
102 &m_HALBuffer.apVertexBuffers[ i ]
103 );
104
105 if ( FAILED( hRes ) )
106 {
107 TRenderD3DInterface::PrintError( hRes, "Unable to create a new vertex buffer!" );
108 return TFALSE;
109 }
110
111 TMemory::HALMemInfo memInfoHAL;
112 TMemory::GetHALMemInfo( memInfoHAL );
113
114 s_iHALMemoryUsage += memInfoHAL.m_uiMemUsage;
115 s_iCurrentNumHALCreated += 1;
116 s_iTotalNumHALCreated += 1;
117 s_iTotalVertexBufferBytesAllocated += length;
118 s_iCurrentVertexBufferBytesAllocated += length;
119
120 if ( s_iTotalVertexBufferBytesAllocated < s_iHALMemoryUsage )
121 {
122 s_iWastedVertexBufferBytesAllocated = s_iHALMemoryUsage - s_iTotalVertexBufferBytesAllocated;
123 }
124 }
125
126 return TTRUE;
127}
128
129// $Barnyard: FUNCTION 006c0760
131{
132 TMemory::HALMemInfo memInfoHAL;
133 TMemory::GetHALMemInfo( memInfoHAL );
134
135 for ( TUINT i = 0; i < m_HALBuffer.uiNumStreams; i++ )
136 {
137 if ( m_HALBuffer.apVertexBuffers[ i ] )
138 {
139 m_HALBuffer.apVertexBuffers[ i ]->Release();
140 m_HALBuffer.apVertexBuffers[ i ] = TNULL;
141 s_iCurrentNumHALCreated -= 1;
142 s_iTotalNumHALDestroyed += 1;
143 s_iCurrentVertexBufferBytesAllocated -= m_pFactory->GetVertexFormat().m_aStreamFormats[ i ].m_uiVertexSize * m_uiMaxVertices;
144 }
145 }
146
147 m_HALBuffer.uiNumStreams = 0;
148
149 TMemory::GetHALMemInfo( memInfoHAL );
150 s_iHALMemoryUsage = s_iHALMemoryUsage - memInfoHAL.m_uiMemUsage;
151
152 if ( s_iTotalVertexBufferBytesAllocated < s_iHALMemoryUsage )
153 {
154 s_iWastedVertexBufferBytesAllocated = s_iHALMemoryUsage - s_iTotalVertexBufferBytesAllocated;
155 }
156}
157
158// $Barnyard: FUNCTION 006c06b0
160{
161 if ( a_iChange < 0 )
162 {
163 TASSERT( m_uiVerticesUsed >= TUINT( -a_iChange ) );
164 }
165
166 m_uiVerticesUsed += a_iChange;
167}
168
169// $Barnyard: FUNCTION 006c0190
171{
172 if ( m_uiFlags & 1 )
173 {
174 Invalidate();
175 }
176
177 return TTRUE;
178}
179
180// $Barnyard: FUNCTION 006c01a0
182{
183 if ( m_uiFlags & 1 )
184 {
185 Validate();
186 }
187
188 return TTRUE;
189}
190
191// $Barnyard: FUNCTION 006c0ac0
193{
194 if ( IsValid() && IsCreated() )
195 {
196 return TTRUE;
197 }
198
199 if ( !CreateHAL() )
200 {
201 return TFALSE;
202 }
203
204 if ( m_uiFlags & 1 )
205 {
207 Pair pair;
208
209 if ( Lock( &pair.second, 0 ) )
210 {
211 pair.first = this;
212
214 []( TResource* a_pResource, void* a_pUserData ) {
215 if ( a_pResource->IsExactly( &TGetClass( TVertexPoolResource ) ) )
216 {
217 auto pPool = TSTATICCAST( TVertexPoolResource, a_pResource );
218 auto pPair = TSTATICCAST( Pair, a_pUserData );
219
220 auto pFactory = pPool->GetFactory();
221 auto vertexFormat = pFactory->GetVertexFormat();
222
223 if ( pPool->m_uiFlags & 1 )
224 {
225 pPool->m_uiVertexOffset = pPair->second.uiOffset;
226 pPair->second.uiOffset += pPool->GetNumVertices();
227
228 for ( TUINT i = 0; i < vertexFormat.GetNumStreams(); i++ )
229 {
230 auto uiVertexSize = vertexFormat.m_aStreamFormats[ i ].m_uiVertexSize;
231
233 pPair->second.apStreams[ i ] + pPool->m_uiVertexOffset * uiVertexSize,
234 pPool->GetManagedStream( i ),
235 pPool->GetNumVertices() * uiVertexSize
236 );
237 }
238 }
239 }
240
241 return TTRUE;
242 },
243 this,
244 &pair
245 );
246
247 Unlock();
248 }
249 }
250
251 return TResource::Validate();
252}
253
254// $Barnyard: FUNCTION 006c0950
256{
257 if ( IsValid() && IsCreated() )
258 {
259 TASSERT( m_uiLockCount == 0 );
260
261 DestroyHAL();
263 }
264}
265
266// $Barnyard: FUNCTION 006c0810
268{
269 TVALIDPTR( a_pLockBuffer );
270
271 auto& vertexFormat = m_pFactory->GetVertexFormat();
272 a_pLockBuffer->uiNumStreams = vertexFormat.m_uiNumStreams;
273
274 DWORD uiFlags;
275 TUINT uiNumVertices = 0;
276 TUINT uiUnk1 = m_uiFlags & 7;
277
278 if ( uiUnk1 == 1 )
279 {
280 uiFlags = D3DLOCK_NOSYSLOCK;
281 a_pLockBuffer->uiOffset = 0;
282 }
283 else
284 {
285 if ( uiUnk1 != 2 )
286 {
287 if ( uiUnk1 != 4 )
288 {
289 return TFALSE;
290 }
291
292 Validate();
293 uiNumVertices = a_uiNumVertices;
294
295 if ( m_uiMaxVertices < m_uiOffset + uiNumVertices )
296 {
297 uiFlags = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
298 a_pLockBuffer->uiOffset = 0;
299 m_uiOffset = uiNumVertices;
300 }
301 else
302 {
303 uiFlags = D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK;
304 a_pLockBuffer->uiOffset = m_uiOffset;
305 m_uiOffset += uiNumVertices;
306 }
307 }
308 else
309 {
310 Validate();
311 uiFlags = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
312 a_pLockBuffer->uiOffset = 0;
313 }
314 }
315
316 for ( TUINT i = 0; i < a_pLockBuffer->uiNumStreams; i++ )
317 {
318 HRESULT hRes = m_HALBuffer.apVertexBuffers[ i ]->Lock(
319 a_pLockBuffer->uiOffset * vertexFormat.m_aStreamFormats[ i ].m_uiVertexSize,
320 uiNumVertices * vertexFormat.m_aStreamFormats[ i ].m_uiVertexSize,
321 &a_pLockBuffer->apStreams[ i ],
322 uiFlags
323 );
324
325 if ( FAILED( hRes ) )
326 {
327 TRenderD3DInterface::PrintError( hRes, "Couldn\'t lock stream vertex buffer" );
328 }
329 }
330
331 m_uiLockCount += 1;
332 return TTRUE;
333}
334
335// $Barnyard: FUNCTION 006c0620
337{
338 TASSERT( 0 != m_uiLockCount );
339
340 if ( m_uiLockCount > 0 )
341 {
342 for ( TUINT i = 0; i < m_pFactory->GetVertexFormat().m_uiNumStreams; i++ )
343 {
344 HRESULT hRes = m_HALBuffer.apVertexBuffers[ i ]->Unlock();
345
346 if ( FAILED( hRes ) )
347 {
348 TRenderD3DInterface::PrintError( hRes, "Couldn\'t unlock stream vertex buffer!" );
349 }
350 }
351
352 m_uiLockCount -= 1;
353 }
354}
355
356// $Barnyard: FUNCTION 006c04d0
367
369{
370 TVALIDPTR( a_pHALBuffer );
371
372 if ( a_pHALBuffer )
373 {
374 *a_pHALBuffer = m_HALBuffer;
375 return TTRUE;
376 }
377
378 return TFALSE;
379}
380
394
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define ISZERO(X)
Definition Defines.h:3
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TVALIDPTR(PTR)
Definition Defines.h:139
#define TDEFINE_FREELIST_ALLOCATOR(CLASS_NAME)
Definition TFreeList.h:19
#define TGetClass(CLASS)
Definition TObject.h:13
#define TDEFINE_CLASS(...)
Definition TObject.h:120
uint16_t TUINT16
Definition Typedefs.h:15
unsigned int TUINT
Definition Typedefs.h:8
#define TNULL
Definition Typedefs.h:23
uint32_t TUINT32
Definition Typedefs.h:13
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
static void GetHALMemInfo(HALMemInfo &a_rHALMemInfo)
Definition TMemory.cpp:956
Hardware abstraction layer memory information.
Definition TMemory.h:161
TSIZE m_uiMemUsage
Total memory usage.
Definition TMemory.h:165
static void PrintError(TINT32 a_eError, const TCHAR *a_szInfo)
Prints error text caused by some directx call.
static TFORCEINLINE TRenderD3DInterface * Interface()
Gets the render interface singleton.
virtual TBOOL Validate() override
virtual TBOOL TryInvalidate() override
TBOOL GetHALBuffer(HALBuffer *a_pHALBuffer) const
TVertexFactoryResourceInterface * m_pFactory
TBOOL Lock(TVertexPoolResourceInterface::LockBuffer *a_pLockBuffer, TUINT16 a_uiNumVertices)
TBOOL AttachPool(TVertexPoolResource *a_pPool)
void ChildVertexUsedChanged(TINT a_iChange)
virtual void Invalidate() override
TBOOL DettachPool(TVertexPoolResource *a_pPool)
TBOOL CanFit(TVertexPoolResource *a_pPoolResource) const
virtual TBOOL TryValidate() override
IDirect3DVertexBuffer8 * apVertexBuffers[TVertexFactoryFormat::MAX_NUM_STREAMS]
void DestroyResource(TResource *resource)
TBOOL IsCreated() const
Definition TResource.h:46
virtual void Invalidate()
Definition TResource.cpp:47
virtual TBOOL Validate()
Definition TResource.cpp:36
TBOOL IsValid() const
Definition TResource.h:44
virtual TBOOL Create()
Definition TResource.cpp:28
void SetParent(TResource *a_pParent)
Definition TResource.cpp:75
TBOOL RecurseSimple(t_RecurseCb a_pCallback, TResource *a_pResource, void *a_pUserData)
TRenderInterface * GetRenderer() const
TBYTE * apStreams[TVertexFactoryFormat::MAX_NUM_STREAMS]
Definition T2Pair.h:8
T * Parent() const
Definition TNodeTree.h:33
T * Child() const
Definition TNodeTree.h:37
TNodeTree< T > * Tree() const
Definition TNodeTree.h:36
TBOOL IsExactly(TClass *a_pClass)
Definition TObject.h:191
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90