summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/DynamicVertexBuffer.cpp
blob: cb59a8ba796b7c1a007b7fa99824fa20bf0a8401 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "DynamicVertexBuffer.h"
#include "CustomVertexLayout.h"
#include "../Profiling/FrameStats.h"

DynamicVertexBuffer::DynamicVertexBuffer()
{
}

DynamicVertexBuffer::~DynamicVertexBuffer()
{
}

//------------------------------------------------------------------------------------------------------------
// Defualt Vertex Layout

//GetChunk
//-> ReleaseChunk
//-> DrawChunk

void DynamicVertexBuffer::GetChunk(uint attrsMask, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib)
{
	Assert(out_vb && out_ib);

	uint stride = VertexLayout::GetDynamicChunkStride(attrsMask); // data size of single vertex
	GetChunk(stride, maxVerts, VertexLayout::GetDefaultIndexSize(), maxIndices, primitive, out_vb, out_ib);

	// default layout
	m_CurAttrMask = attrsMask;
	m_CurStride = stride;
}

void DynamicVertexBuffer::DrawChunk()
{
	DefaultVertexLayout vertexArray;
	FillDefaultVertexLayout(vertexArray);

	// bind vertex attributes data 
	VertexLayout::SetupDefaultVertexLayout(vertexArray);

	const void* indexPtr = m_CurIB ? 0 : (m_CurIBData.empty() ? 0 : &m_CurIBData[0]);

	if (m_CurIB)
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CurIB->GetHandle());

	GLenum indexFormat = VertexLayout::GetDefaultIndexFormat();

	switch (m_CurPrimitive)
	{
	case Primitive_Triangle:
		glDrawElements(GL_TRIANGLES, m_CurIndexCount, indexFormat, indexPtr);
		g_FrameStats.AddDrawCall();
		g_FrameStats.AddTrianglesCount(m_CurIndexCount / 3);
		break;
	case Primitive_Line:
		glDrawElements(GL_LINE, m_CurIndexCount, indexFormat, indexPtr);
		g_FrameStats.AddDrawCall();
		break;
	case Primitive_Point:
		glDrawElements(GL_POINT, m_CurIndexCount, indexFormat, indexPtr);
		g_FrameStats.AddDrawCall();
		break;
	}

	if (m_CurIB)
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	// End draw
	Clean();
}

void DynamicVertexBuffer::FillDefaultVertexLayout(DefaultVertexLayout& dst)
{
	const byte* basepointer = m_CurVB ? 0 : &m_CurVBData[0];
	const GLuint buffer = m_CurVB ? m_CurVB->GetHandle() : 0;

	int attrOffsets[VertexAttr_Count] = { 0 };
	{
		uint32 curOffset = 0;
		for (uint idx = 0; idx < VertexAttr_Count; ++idx)
		{
			if (m_CurAttrMask & Mask(idx))
			{
				attrOffsets[idx] = curOffset;
				curOffset += VertexLayout::GetDefaultVertexAttrSize(idx);
			}
		}
	}

	dst.buffer = buffer;

	for (uint32 attrIdx = 0; attrIdx < VertexAttr_Count; ++attrIdx)
	{
		if (m_CurAttrMask & Mask(attrIdx))
		{
			dst.attributes[attrIdx].pointer = basepointer + attrOffsets[attrIdx];
			dst.attributes[attrIdx].componentFormat = VertexLayout::GetDefaultShaderChannelFormat(attrIdx);
			dst.attributes[attrIdx].componentNum = VertexLayout::GetDefaultShaderChannelDimension(attrIdx);
			dst.attributes[attrIdx].stride = m_CurStride;

			dst.enableMask |= Mask(attrIdx);
		}
	}
}

//------------------------------------------------------------------------------------------------------------
// Custom Vertex Layout

// 用buffersize为依据决定用vbo或者pinned memory
void DynamicVertexBuffer::GetChunk(uint sizePerVert, uint sizePerIndex, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib)
{
	Assert(out_vb && out_ib);

    m_CurStride = sizePerVert;
	uint vbufferSize = sizePerVert * maxVerts;
	uint ibufferSize = sizePerIndex * maxIndices;

	const bool mapVertexBuffer = vbufferSize >= kDataBufferThreshold;
	const bool mapIndexBuffer = ibufferSize >= kDataBufferThreshold;

	GLenum usage = GL_STREAM_DRAW;

	GPU::DataBuffer* vertexBuffer = mapVertexBuffer ? GPU::ClaimBuffer(vbufferSize, usage) : 0;
	GPU::DataBuffer* indexBuffer = mapIndexBuffer ? GPU::ClaimBuffer(ibufferSize, usage) : 0;

	if (vertexBuffer && vertexBuffer->GetSize() < vbufferSize)
		vertexBuffer->Restore(vbufferSize, usage);

	if (indexBuffer && indexBuffer->GetSize() < ibufferSize)
		indexBuffer->Restore(ibufferSize, usage);

	if (!mapVertexBuffer && m_CurVBData.size() < vbufferSize)
		m_CurVBData.resize(vbufferSize);

	if (!mapIndexBuffer && m_CurIBData.size() < ibufferSize)
		m_CurIBData.resize(ibufferSize);

	const GLenum access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;

	if (vertexBuffer)
		*out_vb = vertexBuffer->MapRange(0, vbufferSize, access);
	else
		*out_vb = &m_CurVBData[0];

	if (indexBuffer)
		*out_ib = indexBuffer->MapRange(0, ibufferSize, access);
	else
		*out_ib = &m_CurIBData[0];

	m_CurVB = vertexBuffer;
	m_CurIB = indexBuffer;

	m_CurPrimitive = primitive;
}

void DynamicVertexBuffer::FillCustomVertexLayout(CustomVertexLayout& dst)
{
	const byte* basepointer = m_CurVB ? 0 : &m_CurVBData[0];
	const GLuint buffer = m_CurVB ? m_CurVB->GetHandle() : 0;

	dst.buffer = buffer;

	for (int i = 0; i < dst.attributes.size(); ++i)
	{
		int offset = dst.attributes[i].startOffset;
		dst.attributes[i].pointer = basepointer + offset;
	}
}

void DynamicVertexBuffer::DrawChunk(CustomVertexLayout& layout)
{
	const byte* basepointer = m_CurVB ? 0 : &m_CurVBData[0];
	const GLuint buffer = m_CurVB ? m_CurVB->GetHandle() : 0;

	FillCustomVertexLayout(layout);

	VertexLayout::SetupCustomVertexLayout(layout);

	layout.RestorePointer();

	const void* indexPtr = m_CurIB ? 0 : (m_CurIBData.empty() ? 0 : &m_CurIBData[0]);

	if (m_CurIB)
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CurIB->GetHandle());

	GLenum indexFormat = VertexLayout::GetDefaultIndexFormat();

	switch (m_CurPrimitive)
	{
	case Primitive_Triangle:
		glDrawElements(GL_TRIANGLES, m_CurIndexCount, indexFormat, indexPtr);
		g_FrameStats.AddDrawCall();
		g_FrameStats.AddTrianglesCount(m_CurIndexCount / 3);
		break;
	case Primitive_Line:
		glDrawElements(GL_LINE, m_CurIndexCount, indexFormat, indexPtr);
		g_FrameStats.AddDrawCall();
		break;
	case Primitive_Point:
		glDrawElements(GL_POINT, m_CurIndexCount, indexFormat, indexPtr);
		g_FrameStats.AddDrawCall();
		break;
	}

	if (m_CurIB)
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	// End draw
	Clean();
}

//------------------------------------------------------------------------------------------------------------
// Both Default and Custom Vertex Layout

void DynamicVertexBuffer::ReleaseChunk(int actualVerts, int actualIndices)
{
	int actualVBufferSize = m_CurStride * actualVerts;
	int actualIBufferSize = VertexLayout::GetDefaultIndexSize() * actualIndices;

	const GLenum usage = GL_STREAM_DRAW;

	if (m_CurVB)
	{
		m_CurVB->FlushMapedRange(0, actualVBufferSize);
		m_CurVB->UnMap();
	}
	else if (actualVBufferSize >= kDataBufferThreshold)
	{
		m_CurVB = GPU::ClaimBuffer(actualVBufferSize, usage);
		m_CurVB->RestoreWithData(actualVBufferSize, usage, &m_CurVBData[0]);
	}

	if (m_CurIB)
	{
		m_CurIB->FlushMapedRange(0, actualIBufferSize);
		m_CurIB->UnMap();
	}
	else if (actualIBufferSize >= kDataBufferThreshold)
	{
		m_CurIB = GPU::ClaimBuffer(0, usage);
		m_CurIB->RestoreWithData(0, usage, &m_CurIBData[0]);
	}

	m_CurVertexCount = actualVerts;
	m_CurIndexCount = actualIndices;
}

void DynamicVertexBuffer::Clean()
{
	if (m_CurVB)
	{
		GPU::ReleaseBuffer(m_CurVB);
		m_CurVB = 0;
	}

	if (m_CurIB)
	{
		GPU::ReleaseBuffer(m_CurIB);
		m_CurIB = 0;
	}

	m_CurPrimitive = Primitive_Triangle;
	m_CurAttrMask = 0;
	m_CurStride = 0;
	m_CurVertexCount = 0;
	m_CurIndexCount = 0;

	m_CurVBData.clear();
	m_CurIBData.clear();
}