-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexBuffer.cpp
93 lines (77 loc) · 2.44 KB
/
IndexBuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "IndexBuffer.h"
#include "D3DResourceSystem.h"
#include "DynamicIndexBuffer.h"
namespace DeferredRenderingEngine
{
IndexBuffer::IndexBuffer(uint32_t size, bool dynamic)
{
mIndexBuffer = nullptr;
mDynamic = dynamic;
mSize = size;
}
IndexBuffer::~IndexBuffer()
{
SAFE_RELEASE(mIndexBuffer);
}
void IndexBuffer::Initialize()
{
if (mIndexBuffer)
{
SAFE_RELEASE(mIndexBuffer);
Log::Warn("RwIndexBuffer::Initialize - buffer not released property");
}
try
{
HRESULT result;
if (mSize == 0)
{
Log::Error("RwIndexBuffer::Initialize - invalid size");
throw std::runtime_error("invalid size");
}
if (mDynamic)
result = RwD3DDevice->CreateIndexBuffer(mSize * sizeof(uint16_t), D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,
D3DFMT_INDEX16, D3DPOOL_DEFAULT, &mIndexBuffer, 0);
else
result = RwD3DDevice->CreateIndexBuffer(mSize * sizeof(uint16_t), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIndexBuffer, 0);
if (FAILED(result) || mIndexBuffer == nullptr)
{
Log::Fatal("RwIndexBuffer::Initialize - failed to create index buffer");
throw std::runtime_error("failed to create index buffer");
}
}
catch (const std::runtime_error &e)
{
SAFE_RELEASE(mIndexBuffer);
throw std::runtime_error(std::string("RwIndexBuffer::Initialize - ") + e.what());
}
Log::Debug("IndexBuffer::Initialize - base size %i", mSize);
}
void IndexBuffer::Deinitialize()
{
SAFE_RELEASE(mIndexBuffer);
Log::Debug("IndexBuffer::Deinitialize");
}
void IndexBuffer::Map(uint32_t size, void** data)
{
if (size == 0 || mIndexBuffer == nullptr || data == nullptr)
return;
mIndexBuffer->Lock(0, size, data, mDynamic ? D3DLOCK_DISCARD : 0);
}
void IndexBuffer::Unmap()
{
if (mIndexBuffer == nullptr)
return;
mIndexBuffer->Unlock();
}
void IndexBuffer::Apply()
{
if (mIndexBuffer == nullptr)
return;
_rwD3D9SetIndices(mIndexBuffer);
}
IDirect3DIndexBuffer9* IndexBuffer::GetObject()
{
return mIndexBuffer;
}
}