Skip to content

Commit f878ebc

Browse files
committed
More TVertexBlockResource progress
1 parent 6739763 commit f878ebc

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

Toshi/Include/TKernel/TMemory.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ TOSHI_NAMESPACE_BEGIN
1111

1212
class TKERNELINTERFACE_EXPORTS TMemory
1313
{
14-
14+
public:
1515
struct HALMemInfo
1616
{
17+
TINT m_iMemUsage; // 0x40
1718
// sizeof 0x54
1819
};
1920

@@ -57,6 +58,7 @@ class TKERNELINTERFACE_EXPORTS TMemory
5758
static void TOSHI_API DebugPrintHALMemInfo(TCHAR const *a_pPrint){};
5859
static TMemory &TOSHI_API GetMemMangager();
5960
static MemBlock *TOSHI_API GetGlobalBlock();
61+
static void GetHALMemInfo(HALMemInfo &a_rInfo);
6062
static MemNode *TOSHI_API GetMemNodeFromAddress(TPVOID a_pAddr);
6163
static void TOSHI_API ExtendNodeSize(MemNode *a_pMemNode, TUINT a_iuSize);
6264

Toshi/Include/TRenderD3D/TD3DVertexBlockResource.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,17 @@ class TRENDERINTERFACED3D_EXPORTS TVertexBlockResource : public TResource
4343
m_Unk1 = 0;
4444
}
4545

46+
virtual TBOOL Validate();
47+
4648
TBOOL Create(TVertexFactoryResource *a_pFactory, TUSHORT a_uiMaxVertices, TUINT a_uiFlags);
4749

50+
protected:
51+
52+
TBOOL CreateHAL();
53+
void DestroyHAL();
54+
55+
public:
56+
4857
TBOOL AttachPool(TVertexPoolResource *a_pPool);
4958
TBOOL CanFit(TVertexPoolResource *a_pPoolResource);
5059

@@ -65,6 +74,14 @@ class TRENDERINTERFACED3D_EXPORTS TVertexBlockResource : public TResource
6574
// $TRenderD3DInterface: FUNCTION 100094e0
6675
TUINT GetFlags() { return m_uiFlags; }
6776

77+
inline static TINT s_iCurrentNumHALCreated;
78+
inline static TINT s_iTotalNumHALCreated;
79+
inline static TINT s_iTotalNumHALDestroyed;
80+
inline static TINT s_iCurrentVertexBufferBytesAllocated;
81+
inline static TINT s_iTotalVertexBufferBytesAllocated;
82+
inline static TINT s_iHALMemoryUsage;
83+
inline static TINT s_iWastedVertexBufferBytesAllocated;
84+
6885
private:
6986
TVertexFactoryResourceInterface *m_pFactory; // 0x30
7087
TUINT m_uiFlags; // 0x34

Toshi/Source/TKernel/TMemory.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "TMemory.h"
2+
#include "TSystemTools.h"
23
#include <new>
34

45
#include "TMemoryDebug.h"
@@ -78,6 +79,12 @@ TMemory::MemBlock *TOSHI_API TMemory::GetGlobalBlock()
7879
return g_oMemManager.m_pMemBlock;
7980
}
8081

82+
// $TKernelInterface: FUNCTION 10025d70
83+
void TMemory::GetHALMemInfo(HALMemInfo &a_rInfo)
84+
{
85+
TSystem::MemSet(&a_rInfo, 0, sizeof(HALMemInfo));
86+
}
87+
8188
// $TKernelInterface: FUNCTION 100245a0
8289
TMemory::MemNode *TOSHI_API TMemory::GetMemNodeFromAddress(TPVOID a_pAddr)
8390
{

Toshi/Source/TRenderD3D/TD3DVertexBlockResource.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ TBOOL TVertexBlockResource::AttachPool(TVertexPoolResource *a_pPool)
9393
return TTRUE;
9494
}
9595

96+
// $TRenderD3DInterface: FUNCTION 10008960
97+
TBOOL TVertexBlockResource::Validate()
98+
{
99+
if (IsValid()) {
100+
return TTRUE;
101+
}
102+
TASSERT(TTRUE==Parent()->GetClass().IsExactly(TGetClass(TVertexFactoryResource)));
103+
if (!CreateHAL()) {
104+
return TFALSE;
105+
}
106+
if (GetFlags() & 1) {
107+
108+
}
109+
TIMPLEMENT();
110+
}
111+
96112
// $TRenderD3DInterface: FUNCTION 100088c0
97113
TBOOL TVertexBlockResource::Create(TVertexFactoryResource *a_pFactory, TUSHORT a_uiMaxVertices, TUINT a_uiFlags)
98114
{
@@ -103,3 +119,66 @@ TBOOL TVertexBlockResource::Create(TVertexFactoryResource *a_pFactory, TUSHORT a
103119
m_uiFlags = a_uiFlags;
104120
return TResource::Create();
105121
}
122+
123+
// $TRenderD3DInterface: FUNCTION 100091a0
124+
TBOOL TVertexBlockResource::CreateHAL()
125+
{
126+
DestroyHAL();
127+
TRenderD3DInterface *pRenderer = static_cast<TRenderD3DInterface *>(GetRenderer());
128+
TVertexFactoryFormat *vertexFormat = GetFactory()->GetVertexFormat();
129+
m_HALBuffer.uiNumStreams = vertexFormat->m_uiNumStreams;
130+
for (TUINT i = 0; i < m_HALBuffer.uiNumStreams; i++) {
131+
UINT length = vertexFormat->m_aStreamFormats[i].m_uiVertexSize * m_uiMaxVertices;
132+
DWORD usage = D3DUSAGE_WRITEONLY;
133+
if (GetFlags() & 1) {
134+
usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
135+
m_uiOffset = 0;
136+
}
137+
if (!pRenderer->GetCurrentDevice()->SupportsHardwareTransfomations()) {
138+
usage |= D3DUSAGE_SOFTWAREPROCESSING;
139+
}
140+
HRESULT hRes = pRenderer->GetD3DDevice()->CreateVertexBuffer(
141+
length,
142+
usage,
143+
0,
144+
D3DPOOL_DEFAULT,
145+
&m_HALBuffer.apVertexBuffers[i]);
146+
TRenderD3DInterface::TD3DAssert(hRes, "Unable to create a new vertex buffer!");
147+
148+
TMemory::HALMemInfo memInfoHAL;
149+
TMemory::GetHALMemInfo(memInfoHAL);
150+
s_iHALMemoryUsage += memInfoHAL.m_iMemUsage;
151+
s_iCurrentNumHALCreated += 1;
152+
s_iTotalNumHALCreated += 1;
153+
s_iTotalVertexBufferBytesAllocated += length;
154+
s_iCurrentVertexBufferBytesAllocated += length;
155+
156+
if (s_iTotalVertexBufferBytesAllocated < s_iHALMemoryUsage) {
157+
s_iWastedVertexBufferBytesAllocated = s_iHALMemoryUsage - s_iTotalVertexBufferBytesAllocated;
158+
}
159+
}
160+
161+
return TTRUE;
162+
}
163+
164+
// $TRenderD3DInterface: FUNCTION 10009300
165+
void TVertexBlockResource::DestroyHAL()
166+
{
167+
TMemory::HALMemInfo memInfoHAL;
168+
TMemory::GetHALMemInfo(memInfoHAL);
169+
for (TUINT i = 0; i < m_HALBuffer.uiNumStreams; i++) {
170+
if (m_HALBuffer.apVertexBuffers[i]) {
171+
TD3DRELEASE(m_HALBuffer.apVertexBuffers[i]);
172+
m_HALBuffer.apVertexBuffers[i] = TNULL;
173+
s_iCurrentNumHALCreated--;
174+
s_iTotalNumHALDestroyed++;
175+
s_iCurrentVertexBufferBytesAllocated -= GetFactory()->GetVertexFormat()->m_aStreamFormats[i].m_uiVertexSize * m_uiMaxVertices;
176+
}
177+
}
178+
m_HALBuffer.uiNumStreams = 0;
179+
TMemory::GetHALMemInfo(memInfoHAL);
180+
s_iHALMemoryUsage = s_iHALMemoryUsage - memInfoHAL.m_iMemUsage;
181+
if (s_iTotalVertexBufferBytesAllocated < s_iHALMemoryUsage) {
182+
s_iWastedVertexBufferBytesAllocated = s_iHALMemoryUsage - s_iTotalVertexBufferBytesAllocated;
183+
}
184+
}

0 commit comments

Comments
 (0)