Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
** GLIM - OpenGL Immediate Mode
** Copyright Jan Krassnigg (Jan@Krassnigg.de)
** For more details, see the included Readme.txt.
*/

#include "glimBatchData.h"
#include "glimInterface.h"

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

namespace NS_GLIM
{
	GlimArrayData::GlimArrayData ()
	{
		Reset ();	
	}

	void GlimArrayData::Reset (void)
	{
		m_DataType = GLIM_NODATA;
		m_ArrayData.clear ();
		m_ArrayData.reserve (256);

		m_CurrentValue[0].Int = 0;
		m_CurrentValue[1].Int = 0;
		m_CurrentValue[2].Int = 0;
		m_CurrentValue[3].Int = 0;
	}

	void GlimArrayData::PushElement (void)
	{
		switch (m_DataType)
		{
		// 4 byte data
		case GLIM_1I:
		case GLIM_1F:
		case GLIM_4UB:
			m_ArrayData.push_back (m_CurrentValue[0]);
			return;

		// 8 byte data
		case GLIM_2I:
		case GLIM_2F:
			m_ArrayData.push_back (m_CurrentValue[0]);
			m_ArrayData.push_back (m_CurrentValue[1]);
			return;

		// 12 byte data
		case GLIM_3I:
		case GLIM_3F:
			m_ArrayData.push_back (m_CurrentValue[0]);
			m_ArrayData.push_back (m_CurrentValue[1]);
			m_ArrayData.push_back (m_CurrentValue[2]);
			return;

		// 16 byte data
		case GLIM_4I:
		case GLIM_4F:
			m_ArrayData.push_back (m_CurrentValue[0]);
			m_ArrayData.push_back (m_CurrentValue[1]);
			m_ArrayData.push_back (m_CurrentValue[2]);
			m_ArrayData.push_back (m_CurrentValue[3]);
			return;

		default:
			// throws an exception
			GLIM_CHECK (false, "GlimArrayData::PushElement: Data-Type is invalid.");
			return;
		}
	}

	void glimBatchData::Upload (void)
	{
#ifdef AE_RENDERAPI_OPENGL
		UploadOGL ();
#else
		UploadD3D11 ();
#endif
	}

	void glimBatchData::Bind (unsigned int uiCurrentProgram)
	{
#ifdef AE_RENDERAPI_OPENGL
		BindOGL (uiCurrentProgram);
#else
		BindD3D11 (uiCurrentProgram);
#endif
	}

	void glimBatchData::Unbind (void)
	{
#ifdef AE_RENDERAPI_OPENGL
		UnbindOGL ();
#else
		UnbindD3D11 ();
#endif
	}

	glimBatchData::glimBatchData ()
	{
		m_bUploadedToGPU = false;
		m_bCreatedVBOs = false;

#ifdef AE_RENDERAPI_D3D11
		m_pVertexBuffer = NULL;
		m_pIndexBuffer_Points = NULL;
		m_pIndexBuffer_Lines = NULL;
		m_pIndexBuffer_Triangles = NULL;
		m_pIndexBuffer_Wireframe = NULL;
#endif

		Reset ();
	}

	glimBatchData::~glimBatchData ()
	{
		Reset ();

#ifdef AE_RENDERAPI_OPENGL
		if (m_bCreatedVBOs)
		{
			m_bCreatedVBOs = false;

			glDeleteBuffers (1, &m_uiVertexBufferID);
			glDeleteBuffers (1, &m_uiElementBufferID_Points);
			glDeleteBuffers (1, &m_uiElementBufferID_Lines);
			glDeleteBuffers (1, &m_uiElementBufferID_Triangles);
			glDeleteBuffers (1, &m_uiElementBufferID_Wireframe);
		}
#endif
	}

	void glimBatchData::Reset (void)
	{
		m_State = STATE_EMPTY;

		m_Attributes.clear ();
		m_PositionData.clear ();
		m_PositionData.reserve (64 * 3);

		m_IndexBuffer_Points.clear ();
		m_IndexBuffer_Points.reserve (16);
		m_IndexBuffer_Lines.clear ();
		m_IndexBuffer_Lines.reserve (32);
		m_IndexBuffer_Triangles.clear ();
		m_IndexBuffer_Triangles.reserve (128);
		m_IndexBuffer_Wireframe.clear ();
		m_IndexBuffer_Wireframe.reserve (128);

		m_bUploadedToGPU = false;

		m_fMinX = 99999999.0f;
		m_fMaxX =-99999999.0f;
		m_fMinY = 99999999.0f;
		m_fMaxY =-99999999.0f;
		m_fMinZ = 99999999.0f;
		m_fMaxZ =-99999999.0f;

#ifdef AE_RENDERAPI_D3D11
		if (m_pVertexBuffer)
		{
			GLIM_Interface::s_ReleaseResourceCallback (m_pVertexBuffer);
			//m_pVertexBuffer->Release ();
			m_pVertexBuffer = NULL;
		}
		if (m_pIndexBuffer_Points)
		{
			GLIM_Interface::s_ReleaseResourceCallback (m_pIndexBuffer_Points);
			//m_pIndexBuffer_Points->Release ();
			m_pIndexBuffer_Points = NULL;
		}
		if (m_pIndexBuffer_Lines)
		{
			GLIM_Interface::s_ReleaseResourceCallback (m_pIndexBuffer_Lines);
			//m_pIndexBuffer_Lines->Release ();
			m_pIndexBuffer_Lines = NULL;
		}
		if (m_pIndexBuffer_Triangles)
		{
			GLIM_Interface::s_ReleaseResourceCallback (m_pIndexBuffer_Triangles);
			//m_pIndexBuffer_Triangles->Release ();
			m_pIndexBuffer_Triangles = NULL;
		}
		if (m_pIndexBuffer_Wireframe)
		{
			GLIM_Interface::s_ReleaseResourceCallback (m_pIndexBuffer_Wireframe);
			//m_pIndexBuffer_Wireframe->Release ();
			m_pIndexBuffer_Wireframe = NULL;
		}

		m_Signature.clear ();
#endif
	}

	unsigned int glimBatchData::AddVertex (float x, float y, float z)
	{
		if (x < m_fMinX)
			m_fMinX = x;
		if (x > m_fMaxX)
			m_fMaxX = x;

		if (y < m_fMinY)
			m_fMinY = y;
		if (y > m_fMaxY)
			m_fMaxY = y;

		if (z < m_fMinZ)
			m_fMinZ = z;
		if (z > m_fMaxZ)
			m_fMaxZ = z;

		m_PositionData.push_back (x);
		m_PositionData.push_back (y);
		m_PositionData.push_back (z);

		std::map<std::string, GlimArrayData>::iterator it, itend;
		itend = m_Attributes.end ();

		for (it = m_Attributes.begin (); it != itend; ++it)
			it->second.PushElement ();

		return ((unsigned int) (m_PositionData.size () / 3) - 1);
	}

#ifdef AE_RENDERAPI_D3D11
	void glimBatchData::GenerateSignature (void)
	{
		m_Signature.clear ();
		m_Signature.reserve (1 + m_Attributes.size ());

		D3D11_INPUT_ELEMENT_DESC sig;
		sig.InputSlot = 0;
		sig.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
		sig.InstanceDataStepRate = 0;
		sig.SemanticIndex = 0;
		sig.SemanticName = GLIM_Interface::s_VertexPosSemanticName;
		sig.AlignedByteOffset = 0;
		sig.Format = DXGI_FORMAT_R32G32B32_FLOAT;

		m_Signature.push_back (sig);

		std::map<std::string, GlimArrayData>::const_iterator it, itend;
		itend = m_Attributes.end ();

		unsigned int uiOffset = sizeof (float) * 3;

		int iSlot = 1;
		for (it = m_Attributes.begin (); it != itend; ++it, ++iSlot)
		{
			D3D11_INPUT_ELEMENT_DESC sig;
			sig.InputSlot = iSlot;
			sig.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
			sig.InstanceDataStepRate = 0;
			sig.SemanticIndex = 0;
			sig.SemanticName = it->first.c_str ();
			sig.AlignedByteOffset = 0;//uiOffset;

			switch (it->second.m_DataType)
			{
			// 4 byte data
			case GLIM_1I:
				uiOffset += sizeof (int) * 1;
				sig.Format = DXGI_FORMAT_R32_SINT;
				break;
			case GLIM_1F:
				uiOffset += sizeof (float) * 1;
				sig.Format = DXGI_FORMAT_R32_FLOAT;
				break;
			case GLIM_4UB:
				uiOffset += sizeof (int) * 1;
				sig.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
				break;

			// 8 byte data
			case GLIM_2I:
				uiOffset += sizeof (int) * 2;
				sig.Format = DXGI_FORMAT_R32G32_SINT;
				break;
			case GLIM_2F:
				uiOffset += sizeof (float) * 2;
				sig.Format = DXGI_FORMAT_R32G32_FLOAT;
				break;

			// 12 byte data
			case GLIM_3I:
				uiOffset += sizeof (int) * 3;
				sig.Format = DXGI_FORMAT_R32G32B32_SINT;
				break;
			case GLIM_3F:
				uiOffset += sizeof (float) * 3;
				sig.Format = DXGI_FORMAT_R32G32B32_FLOAT;
				break;

			// 16 byte data
			case GLIM_4I:
				uiOffset += sizeof (int) * 4;
				sig.Format = DXGI_FORMAT_R32G32B32A32_SINT;
				break;
			case GLIM_4F:
				uiOffset += sizeof (float) * 4;
				sig.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
				break;

			default:
				// throws an exception
				GLIM_CHECK (false, "glimBatchData::GenerateSignature: Data-Type is invalid.");
				break;
			}

			m_Signature.push_back (sig);
		}
	}
#endif

	unsigned int glimBatchData::getVertexDataSize (void) const
	{
		unsigned int uiVertexDataSize = sizeof (float) * 3;

		std::map<std::string, GlimArrayData>::const_iterator it, itend;
		itend = m_Attributes.end ();

		for (it = m_Attributes.begin (); it != itend; ++it)
		{
			switch (it->second.m_DataType)
			{
			// 4 byte data
			case GLIM_1I:
			case GLIM_1F:
			case GLIM_4UB:
				uiVertexDataSize += sizeof (int) * 1;
				break;

			// 8 byte data
			case GLIM_2I:
			case GLIM_2F:
				uiVertexDataSize += sizeof (int) * 2;
				break;

			// 12 byte data
			case GLIM_3I:
			case GLIM_3F:
				uiVertexDataSize += sizeof (int) * 3;
				break;

			// 16 byte data
			case GLIM_4I:
			case GLIM_4F:
				uiVertexDataSize += sizeof (int) * 4;
				break;

			default:
				// throws an exception
				GLIM_CHECK (false, "glimBatchData::Upload: Data-Type is invalid.");
				break;
			}
		}

		return (uiVertexDataSize);
	}

#ifdef AE_RENDERAPI_OPENGL

	int glimBatchData::getVertexAttribBindPoint (unsigned int uiCurrentProgram, const char* szAttribName) const
	{
		int iLocation = glGetAttribLocation (uiCurrentProgram, szAttribName);

		return (iLocation);
	}

	void glimBatchData::UnbindOGL (void)
	{
		// make sure no VAO is bound, such that GLIM does not accidently change it's state

		// if the pointer is != NULL, the extension should be available
		if (glBindVertexArray)
		{
			glBindVertexArray (0);
		}

		glBindBuffer (GL_ARRAY_BUFFER, 0);
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);

		for (int i = 0; i < 16; ++i)
			glDisableVertexAttribArray (i);
	}

	void glimBatchData::BindOGL (unsigned int uiCurrentProgram)
	{
		if (!m_bUploadedToGPU)
			return;

		// make sure no VAO is bound, such that GLIM does not accidently change it's state

		// if the pointer is != NULL, the extension should be available
		if (glBindVertexArray)
		{
			glBindVertexArray (0);
		}

		if (GLEW_NV_vertex_buffer_unified_memory)
		{
			//glDisableClientState (GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
			//glDisableClientState (GL_ELEMENT_ARRAY_UNIFIED_NV);
		}

		glBindBuffer (GL_ARRAY_BUFFER, m_uiVertexBufferID);

		std::map<std::string, GlimArrayData>::iterator it, itend;
		itend = m_Attributes.end ();

		for (it = m_Attributes.begin (); it != itend; ++it)
		{
			const int iAttributeArray = getVertexAttribBindPoint (uiCurrentProgram, it->first.c_str ());

			if (iAttributeArray < 0)
				continue;

			glEnableVertexAttribArray (iAttributeArray);

			switch (it->second.m_DataType)
			{
			case GLIM_1F:
				glVertexAttribPointer (iAttributeArray, 1, GL_FLOAT, false, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_2F:
				glVertexAttribPointer (iAttributeArray, 2, GL_FLOAT, false, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_3F:
				glVertexAttribPointer (iAttributeArray, 3, GL_FLOAT, false, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_4F:
				glVertexAttribPointer (iAttributeArray, 4, GL_FLOAT, false, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_1I:
				glVertexAttribIPointer (iAttributeArray, 1, GL_INT, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_2I:
				glVertexAttribIPointer (iAttributeArray, 2, GL_INT, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_3I:
				glVertexAttribIPointer (iAttributeArray, 3, GL_INT, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_4I:
				glVertexAttribIPointer (iAttributeArray, 4, GL_INT, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			case GLIM_4UB:
				glVertexAttribPointer (iAttributeArray, 4, GL_UNSIGNED_BYTE, true, 0, BUFFER_OFFSET (it->second.m_uiBufferOffset));
				break;
			}
		}

		// set the pointer for position last
		glEnableVertexAttribArray (m_VertAttribLocation);
		glVertexAttribPointer (m_VertAttribLocation, 3, GL_FLOAT, false, 0, BUFFER_OFFSET (0));
	}

	void glimBatchData::UploadOGL (void)
	{
		if (m_bUploadedToGPU)
			return;

		if (m_PositionData.empty ())
			return;

		m_bUploadedToGPU = true;


		// make sure no VAO is bound, such that GLIM does not accidently change it's state

		// if the pointer is != NULL, the extension should be available
		if (glBindVertexArray)
		{
			glBindVertexArray (0);
		}

		if (GLEW_NV_vertex_buffer_unified_memory)
		{
			//glDisableClientState (GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
			//glDisableClientState (GL_ELEMENT_ARRAY_UNIFIED_NV);
		}

		const unsigned int uiVertexDataSize = (unsigned int) getVertexDataSize ();
		const unsigned int uiVertices = (unsigned int) (m_PositionData.size ()) / 3;

		if (!m_bCreatedVBOs)
		{
			m_bCreatedVBOs = true;

			glGenBuffers (1, &m_uiVertexBufferID);
			glGenBuffers (1, &m_uiElementBufferID_Points);
			glGenBuffers (1, &m_uiElementBufferID_Lines);
			glGenBuffers (1, &m_uiElementBufferID_Triangles);
			glGenBuffers (1, &m_uiElementBufferID_Wireframe);
		}

		
		glBindBuffer (GL_ARRAY_BUFFER, m_uiVertexBufferID);
		glBufferData (GL_ARRAY_BUFFER, uiVertices * uiVertexDataSize, NULL, GL_STATIC_DRAW);

		// first upload the position data
		unsigned int uiOffset = uiVertices * sizeof (float) * 3;

		// offset is zero, but can use uiOffset as size
		glBufferSubData (GL_ARRAY_BUFFER, 0, uiOffset, &m_PositionData[0]);
		// the buffer in RAM can be cleared now
		m_PositionData.clear ();


		// now upload each attribute array one after another

		std::map<std::string, GlimArrayData>::iterator it, itend;
		itend = m_Attributes.end ();

		for (it = m_Attributes.begin (); it != itend; ++it)
		{
			const unsigned int uiAttributeSize = (unsigned int) (it->second.m_ArrayData.size ()) * 4; // already includes the number of vertices

			// upload the data
			glBufferSubData (GL_ARRAY_BUFFER, uiOffset, uiAttributeSize, &it->second.m_ArrayData[0].Float);

			// free the temporary buffer in RAM, the data is now in a better place (the GPU)
			it->second.m_ArrayData.clear ();

			it->second.m_uiBufferStride = uiAttributeSize / uiVertices;

			// store the buffer offset for later use
			it->second.m_uiBufferOffset = uiOffset;

			// increase the buffer offset
			uiOffset += uiAttributeSize;
		}

		m_uiPointElements = 0;
		m_uiLineElements = 0;
		m_uiTriangleElements = 0;
		m_uiWireframeElements = 0;

		// upload the index buffer for the points
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, m_uiElementBufferID_Points);

		if (!m_IndexBuffer_Points.empty ())
		{
			m_uiPointElements = (unsigned int) m_IndexBuffer_Points.size ();
			glBufferData (GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer_Points.size () * 4, &m_IndexBuffer_Points[0], GL_STATIC_DRAW);
			m_IndexBuffer_Points.clear ();
		}
			
		// upload the index buffer for the lines
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, m_uiElementBufferID_Lines);

		if (!m_IndexBuffer_Lines.empty ())
		{
			m_uiLineElements = (unsigned int) m_IndexBuffer_Lines.size ();
			glBufferData (GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer_Lines.size () * 4, &m_IndexBuffer_Lines[0], GL_STATIC_DRAW);
			m_IndexBuffer_Lines.clear ();
		}


		// upload the index buffer for the triangles
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, m_uiElementBufferID_Triangles);

		if (!m_IndexBuffer_Triangles.empty ())
		{
			m_uiTriangleElements = (unsigned int) m_IndexBuffer_Triangles.size ();
			glBufferData (GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer_Triangles.size () * 4, &m_IndexBuffer_Triangles[0], GL_STATIC_DRAW);
			m_IndexBuffer_Triangles.clear ();
		}


		// upload the index buffer for wireframe
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, m_uiElementBufferID_Wireframe);

		if (!m_IndexBuffer_Wireframe.empty ())
		{
			m_uiWireframeElements = (unsigned int) m_IndexBuffer_Wireframe.size ();
			glBufferData (GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer_Wireframe.size () * 4, &m_IndexBuffer_Wireframe[0], GL_STATIC_DRAW);
			m_IndexBuffer_Wireframe.clear ();
		}
	}
#endif
}


Commits for Divide-Framework/trunk/Source Code/Libs/src/GLIM/glimBatchData.cpp

Diff revisions: vs.
Revision Author Commited Message
221 Diff Diff k1ngp1n picture k1ngp1n Wed 08 Jan, 2014 18:50:02 +0000

[Ionut] [[BR]]
- Merged branch “Shadows And Animations” back into trunk [[BR]]
- Added DoxygenGUI project file [[BR]]

  • Buggy *
219 k1ngp1n picture k1ngp1n Wed 08 Jan, 2014 15:47:57 +0000

[Ionut] [[BR]]
- Made GLIM part of the main solution [[BR]]
- glLineWidth fixes [[BR]]
- IMEmulation fixes [[BR]]