OpenBarnyard
 
Loading...
Searching...
No Matches
TIndexBlockResource_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 006c01b0
20// $Barnyard: FUNCTION 006c0200
21TIndexBlockResource::TIndexBlockResource()
22{
24 m_uiFlags = 0;
26 m_uiOffset = 0;
28 m_uiLockCount = 0;
29 m_Unk1 = 0;
30}
31
32// $Barnyard: FUNCTION 006c0250
33TIndexBlockResource::~TIndexBlockResource()
34{
35}
36
38{
39 TVALIDPTR( a_pPool );
40
41 m_uiIndicesUsed += a_pPool->GetNumIndices();
42 a_pPool->SetParent( this );
43
44 if ( m_uiFlags & 1 )
45 {
46 Invalidate();
47 }
48
49 return TTRUE;
50}
51
53{
55 a_pPool->SetParent( Parent() == Tree()->GetRoot() ? TNULL : Parent() );
56
57 m_uiIndicesUsed -= a_pPool->GetNumIndices();
58
59 if ( m_uiIndicesUsed == 0 && Child() == TNULL )
60 {
62 return TTRUE;
63 }
64
65 return TFALSE;
66}
67
68// $Barnyard: FUNCTION 006c0440
70{
71 if ( IsValid() && IsCreated() )
72 {
73 return TTRUE;
74 }
75
76 if ( !CreateHAL() )
77 {
78 return TFALSE;
79 }
80
81 if ( m_uiFlags & 1 )
82 {
84 Pair pair;
85
86 if ( Lock( &pair.second, 0 ) )
87 {
88 pair.first = this;
89
91 []( TResource* a_pResource, void* a_pUserData ) {
92 if ( a_pResource->IsExactly( &TGetClass( TIndexPoolResource ) ) )
93 {
94 auto pPool = TSTATICCAST( TIndexPoolResource, a_pResource );
95 auto pPair = TSTATICCAST( Pair, a_pUserData );
96
97 if ( pPool->m_uiFlags & 1 )
98 {
99 pPool->m_uiIndexOffset = pPair->second.uiOffset;
100 pPair->second.uiOffset += pPool->GetNumIndices();
101
103 pPair->second.pBuffer + pPool->m_uiIndexOffset,
104 pPool->GetIndices(),
105 pPool->GetNumIndices() * sizeof( TIndexType )
106 );
107 }
108 }
109
110 return TTRUE;
111 },
112 this,
113 &pair
114 );
115
116 Unlock();
117 }
118 }
119
120 return TResource::Validate();
121}
122
123// $Barnyard: FUNCTION 006c02d0
125{
126 if ( IsValid() && IsCreated() )
127 {
128 TASSERT( m_uiLockCount == 0 );
129
130 DestroyHAL();
132 }
133}
134
136{
137 if ( m_uiFlags & 1 )
138 {
139 Invalidate();
140 }
141
142 return TTRUE;
143}
144
146{
147 if ( m_uiFlags & 1 )
148 {
149 Validate();
150 }
151
152 return TTRUE;
153}
154
155// $Barnyard: FUNCTION 006c0350
157{
158 DestroyHAL();
159
160 TMemory::HALMemInfo memInfoHAL;
161 TMemory::GetHALMemInfo( memInfoHAL );
162
163 auto pRenderer = TRenderD3DInterface::Interface();
164 DWORD usage = D3DUSAGE_WRITEONLY;
165
166 if ( ISZERO( m_uiFlags & 1 ) )
167 {
168 usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
169 m_uiOffset = 0;
170 }
171
172 if ( !pRenderer->GetCurrentDevice()->SupportsHardwareTransfomations() )
173 {
174 usage |= D3DUSAGE_SOFTWAREPROCESSING;
175 }
176
177 HRESULT hRes = pRenderer->GetDirect3DDevice()->CreateIndexBuffer(
178 m_uiMaxIndices * sizeof( TIndexType ),
179 usage,
180 D3DFMT_INDEX16,
181 ( m_uiFlags & 1 ) ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT,
182 &m_HALBuffer.pIndexBuffer
183 );
184
185 if ( FAILED( hRes ) )
186 {
187 TRenderD3DInterface::PrintError( hRes, "Unable to create a new index buffer!" );
188 return TFALSE;
189 }
190
191 TMemory::GetHALMemInfo( memInfoHAL );
192 s_iHALMemoryUsage += memInfoHAL.m_uiMemUsage;
193
194 return TTRUE;
195}
196
197// $Barnyard: FUNCTION 006c0280
199{
200 TMemory::HALMemInfo memInfoHAL;
201 TMemory::GetHALMemInfo( memInfoHAL );
202
203 if ( m_HALBuffer.pIndexBuffer )
204 {
205 m_HALBuffer.pIndexBuffer->Release();
206 m_HALBuffer.pIndexBuffer = TNULL;
207 }
208
209 TMemory::GetHALMemInfo( memInfoHAL );
210 s_iHALMemoryUsage = s_iHALMemoryUsage - memInfoHAL.m_uiMemUsage;
211}
212
213// $Barnyard: FUNCTION 006c0060
215{
216 TVALIDPTR( a_pLockBuffer );
217
218 DWORD uiFlags;
219 TUINT uiNumIndices = 0;
220 TUINT uiUnk1 = m_uiFlags & 7;
221
222 if ( uiUnk1 == 1 )
223 {
224 uiFlags = D3DLOCK_NOSYSLOCK;
225 a_pLockBuffer->uiOffset = 0;
226 }
227 else
228 {
229 if ( uiUnk1 != 2 )
230 {
231 if ( uiUnk1 != 4 )
232 {
233 return TFALSE;
234 }
235
236 Validate();
237 uiNumIndices = a_uiNumIndices;
238
239 if ( m_uiMaxIndices < m_uiOffset + uiNumIndices )
240 {
241 uiFlags = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
242 a_pLockBuffer->uiOffset = 0;
243 m_uiOffset = uiNumIndices;
244 }
245 else
246 {
247 uiFlags = D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK;
248 a_pLockBuffer->uiOffset = m_uiOffset;
249 m_uiOffset += uiNumIndices;
250 }
251 }
252 else
253 {
254 Validate();
255 uiFlags = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
256 a_pLockBuffer->uiOffset = 0;
257 }
258 }
259
260 HRESULT hRes = m_HALBuffer.pIndexBuffer->Lock(
261 a_pLockBuffer->uiOffset * sizeof( TIndexType ),
262 uiNumIndices * sizeof( TIndexType ),
263 (BYTE**)&a_pLockBuffer->pBuffer,
264 uiFlags
265 );
266
267 if ( FAILED( hRes ) )
268 {
269 TRenderD3DInterface::PrintError( hRes, "Couldn\'t lock stream index buffer" );
270 }
271
272 m_uiLockCount += 1;
273 return TTRUE;
274}
275
276// $Barnyard: FUNCTION 006c0120
278{
279 TASSERT( 0 != m_uiLockCount );
280
281 if ( m_uiLockCount > 0 )
282 {
283 HRESULT hRes = m_HALBuffer.pIndexBuffer->Unlock();
284
285 if ( FAILED( hRes ) )
286 {
287 TRenderD3DInterface::PrintError( hRes, "Couldn\'t unlock stream index buffer!" );
288 }
289
290 m_uiLockCount -= 1;
291 }
292}
293
295{
296 m_pFactory = a_pFactory;
297 m_uiMaxIndices = a_uiMaxIndices;
298 m_uiFlags = a_uiFlags;
299 return TResource::Create();
300}
301
302// $Barnyard: FUNCTION 006c0150
304{
305 if ( !HASANYFLAG( m_uiFlags, 1 ) )
306 {
307 if ( HASANYFLAG( m_uiFlags, 4 ) && HASANYFLAG( a_pPoolResource->m_uiFlags, 4 ) )
308 {
309 return TTRUE;
310 }
311 }
312 else if ( HASANYFLAG( a_pPoolResource->m_uiFlags, 1 ) )
313 {
314 return m_uiMaxIndices > a_pPoolResource->GetNumIndices() + m_uiIndicesUsed;
315 }
316
317 return TFALSE;
318}
319
321{
322 if ( a_iChange < 0 )
323 {
324 TASSERT( m_uiIndicesUsed >= TUINT( -a_iChange ) );
325 }
326
327 m_uiIndicesUsed += a_iChange;
328}
329
331{
332 TVALIDPTR( a_pHALBuffer );
333
334 if ( a_pHALBuffer )
335 {
336 *a_pHALBuffer = m_HALBuffer;
337 return TTRUE;
338 }
339
340 return TFALSE;
341}
342
TUINT16 TIndexType
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define HASANYFLAG(STATE, FLAG)
Definition Defines.h:5
#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
virtual TBOOL Validate() override
TBOOL DettachPool(TIndexPoolResource *a_pPool)
TBOOL Lock(TIndexPoolResourceInterface::LockBuffer *a_pLockBuffer, TUINT16 a_uiNumIndices)
virtual TBOOL TryValidate() override
TIndexFactoryResourceInterface * m_pFactory
TBOOL AttachPool(TIndexPoolResource *a_pPool)
TBOOL CanFit(TIndexPoolResource *a_pPoolResource) const
virtual void Invalidate() override
void ChildIndexUsedChanged(TINT a_iChange)
TBOOL GetHALBuffer(HALBuffer *a_pHALBuffer) const
virtual TBOOL TryInvalidate() override
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.
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
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