File: buffer.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (703 lines) | stat: -rw-r--r-- 16,754 bytes parent folder | download | duplicates (6)
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#ifndef GLW_BUFFER_H
#define GLW_BUFFER_H

#include "./object.h"

namespace glw
{

class BufferArguments : public ObjectArguments
{
	public:

		typedef ObjectArguments BaseType;
		typedef BufferArguments ThisType;

		GLsizeiptr   size;
		GLenum       usage;
		const void * data;

		BufferArguments(void)
			: BaseType ()
			, size     (0)
			, usage    (GL_STATIC_DRAW)
			, data     (0)
		{
			this->clear();
		}

		BufferArguments(GLsizeiptr aSize, GLenum aUsage = GL_STATIC_DRAW, const void * aData = 0)
			: BaseType ()
			, size     (aSize)
			, usage    (aUsage)
			, data     (aData)
		{
			;
		}

		void clear(void)
		{
			BaseType::clear();
			this->size  = 0;
			this->usage = GL_STATIC_DRAW;
			this->data  = 0;
		}
};

class Buffer : public Object
{
	friend class Context;

	public:

		typedef Object BaseType;
		typedef Buffer ThisType;

		virtual ~Buffer(void)
		{
			this->destroy();
		}

		virtual Type type(void) const
		{
			return BufferType;
		}

		GLsizeiptr size(void) const
		{
			return this->m_size;
		}

		GLenum usage(void) const
		{
			return this->m_usage;
		}

		void setData(GLenum target, GLint unit, const GLsizeiptr size, GLenum usage, const GLvoid * data)
		{
			(void)unit;
			GLW_ASSERT(this->isValid());
			glBufferData(target, size, data, usage);
			this->m_size  = size;
			this->m_usage = usage;
		}

		void setSubData(GLenum target, GLint unit, GLintptr offset, GLsizeiptr size, const GLvoid * data)
		{
			(void)unit;
			GLW_ASSERT(this->isValid());
			glBufferSubData(target, offset, size, data);
		}

		void getSubData(GLenum target, GLint unit, GLintptr offset, GLsizeiptr size, GLvoid * data)
		{
			(void)unit;
			GLW_ASSERT(this->isValid());
			glGetBufferSubData(target, offset, size, data);
		}

		void * map(GLenum target, GLint unit, GLenum access)
		{
			(void)unit;
			GLW_ASSERT(this->isValid());
			GLW_ASSERT(!this->isMapped(target, unit));
			void * ptr = glMapBuffer(target, access);
			if (ptr == 0) return 0;
			this->m_mapAccess  = access;
			this->m_mapPointer = ptr;
			return ptr;
		}

		void unmap(GLenum target, GLint unit)
		{
			(void)unit;
			GLW_ASSERT(this->isValid());
			GLW_ASSERT(this->isMapped(target, unit));
			glUnmapBuffer(target);
			this->m_mapAccess  = GL_NONE;
			this->m_mapPointer = 0;
		}

		GLenum mapAccess(GLenum target, GLint unit) const
		{
			(void)target;
			(void)unit;
			return this->m_mapAccess;
		}

		bool isMapped(GLenum target, GLint unit) const
		{
			(void)target;
			(void)unit;
			return (this->m_mapAccess != GL_NONE);
		}

		void * mapPointer(GLenum target, GLint unit) const
		{
			(void)target;
			(void)unit;
			return this->m_mapPointer;
		}

		void vertexAttribPointer(GLenum target, GLint unit, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * offset)
		{
			(void)target;
			(void)unit;
			GLW_ASSERT(this->isValid());
			glVertexAttribPointer(index, size, type, normalized, stride, offset);
		}

		void drawElements(GLenum target, GLint unit, GLenum mode, GLsizei count, GLenum type, const GLvoid * indices)
		{
			(void)target;
			(void)unit;
			GLW_ASSERT(this->isValid());
			glDrawElements(mode, count, type, indices);
		}

	protected:

		GLsizeiptr m_size;
		GLenum     m_usage;
		GLenum     m_mapAccess;
		void *     m_mapPointer;

		Buffer(Context * ctx)
			: BaseType     (ctx)
			, m_size       (0)
			, m_usage      (GL_NONE)
			, m_mapAccess  (GL_NONE)
			, m_mapPointer (0)
		{
			;
		}

		bool create(const BufferArguments & args)
		{
			this->destroy();
			GLint boundName = 0;
			glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &boundName);
			glGenBuffers(1, &(this->m_name));
			glBindBuffer(GL_ARRAY_BUFFER, this->m_name);
			glBufferData(GL_ARRAY_BUFFER, args.size, args.data, args.usage);
			glBindBuffer(GL_ARRAY_BUFFER, boundName);
			this->m_size  = args.size;
			this->m_usage = args.usage;
			return true;
		}

		virtual void doDestroy(void)
		{
			glDeleteBuffers(1, &(this->m_name));
			this->m_size       = 0;
			this->m_usage      = GL_NONE;
			this->m_mapAccess  = GL_NONE;
			this->m_mapPointer = 0;
		}

		virtual bool doIsValid(void) const
		{
			return (this->m_size > 0);
		}
};

namespace detail { template <> struct BaseOf <Buffer> { typedef Object Type; }; };
typedef   detail::ObjectSharedPointerTraits  <Buffer> ::Type BufferPtr;

class SafeBuffer : public SafeObject
{
	friend class Context;
	friend class BoundBuffer;

	public:

		typedef SafeObject BaseType;
		typedef SafeBuffer ThisType;

		SafeBuffer(void)
			: BaseType()
		{
			;
		}

		GLsizeiptr size(void) const
		{
			return this->object()->size();
		}

		GLenum usage(void) const
		{
			return this->object()->usage();
		}

	protected:

		SafeBuffer(const BufferPtr & buffer)
			: BaseType(buffer)
		{
			;
		}

		const BufferPtr & object(void) const
		{
			return static_cast<const BufferPtr &>(BaseType::object());
		}

		BufferPtr & object(void)
		{
			return static_cast<BufferPtr &>(BaseType::object());
		}
};

namespace detail { template <> struct BaseOf     <SafeBuffer> { typedef SafeObject Type; }; };
namespace detail { template <> struct ObjectBase <SafeBuffer> { typedef Buffer     Type; }; };
namespace detail { template <> struct ObjectSafe <Buffer    > { typedef SafeBuffer Type; }; };
typedef   detail::ObjectSharedPointerTraits      <SafeBuffer> ::Type BufferHandle;

class BufferBindingParams : public ObjectBindingParams
{
	public:

		typedef ObjectBindingParams BaseType;
		typedef BufferBindingParams ThisType;

		BufferBindingParams(void)
			: BaseType()
		{
			;
		}

		BufferBindingParams(GLenum aTarget, GLenum aUnit)
			: BaseType(aTarget, aUnit)
		{
			;
		}
};

class BoundBuffer : public BoundObject
{
	friend class Context;

	public:

		typedef BoundObject BaseType;
		typedef BoundBuffer ThisType;

		BoundBuffer(void)
			: BaseType()
		{
			;
		}

		const BufferHandle & handle(void) const
		{
			return static_cast<const BufferHandle &>(BaseType::handle());
		}

		BufferHandle & handle(void)
		{
			return static_cast<BufferHandle &>(BaseType::handle());
		}

		void setData(const GLsizeiptr size, GLenum usage, const GLvoid * data)
		{
			this->object()->setData(this->m_target, this->m_unit, size, usage, data);
		}

		void setSubData(GLintptr offset, GLsizeiptr size, const GLvoid * data)
		{
			this->object()->setSubData(this->m_target, this->m_unit, offset, size, data);
		}

		void getSubData(GLintptr offset, GLsizeiptr size, GLvoid * data)
		{
			this->object()->getSubData(this->m_target, this->m_unit, offset, size, data);
		}

		void * map(GLenum access)
		{
			return this->object()->map(this->m_target, this->m_unit, access);
		}

		void unmap(void)
		{
			this->object()->unmap(this->m_target, this->m_unit);
		}

		GLenum mapAccess(void) const
		{
			return this->object()->mapAccess(this->m_target, this->m_unit);
		}

		bool isMapped(void) const
		{
			return this->object()->isMapped(this->m_target, this->m_unit);
		}

		void * mapPointer(void) const
		{
			return this->object()->mapPointer(this->m_target, this->m_unit);
		}

	protected:

		BoundBuffer(const BufferHandle & handle, const BufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}

		const BufferPtr & object(void) const
		{
			return this->handle()->object();
		}

		BufferPtr & object(void)
		{
			return this->handle()->object();
		}

		virtual void bind(void)
		{
			glBindBuffer(this->m_target, this->object()->name());
		}

		virtual void unbind(void)
		{
			glBindBuffer(this->m_target, 0);
		}
};

namespace detail { template <> struct ParamsOf    <BoundBuffer> { typedef BufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf      <BoundBuffer> { typedef BoundObject Type; }; };
namespace detail { template <> struct ObjectBase  <BoundBuffer> { typedef Buffer      Type; }; };
namespace detail { template <> struct ObjectBound <Buffer     > { typedef BoundBuffer Type; }; };
typedef   detail::ObjectSharedPointerTraits       <BoundBuffer> ::Type  BoundBufferHandle;

class VertexBufferBindingParams : public BufferBindingParams
{
	public:

		typedef BufferBindingParams       BaseType;
		typedef VertexBufferBindingParams ThisType;

		VertexBufferBindingParams(void)
			: BaseType(GL_ARRAY_BUFFER, 0)
		{
			;
		}
};

class BoundVertexBuffer : public BoundBuffer
{
	friend class Context;

	public:

		typedef BoundBuffer       BaseType;
		typedef BoundVertexBuffer ThisType;

		BoundVertexBuffer(void)
			: BaseType()
		{
			;
		}

		void vertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * offset)
		{
			this->object()->vertexAttribPointer(this->m_target, this->m_unit, index, size, type, normalized, stride, offset);
		}

	protected:

		BoundVertexBuffer(const BufferHandle & handle, const VertexBufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}
};

namespace detail { template <> struct ParamsOf   <BoundVertexBuffer> { typedef VertexBufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf     <BoundVertexBuffer> { typedef BoundBuffer Type; }; };
namespace detail { template <> struct ObjectBase <BoundVertexBuffer> { typedef Buffer      Type; }; };
typedef   detail::ObjectSharedPointerTraits      <BoundVertexBuffer> ::Type BoundVertexBufferHandle;

class IndexBufferBindingParams : public BufferBindingParams
{
	public:

		typedef BufferBindingParams      BaseType;
		typedef IndexBufferBindingParams ThisType;

		IndexBufferBindingParams(void)
			: BaseType(GL_ELEMENT_ARRAY_BUFFER, 0)
		{
			;
		}
};

class BoundIndexBuffer : public BoundBuffer
{
	friend class Context;

	public:

		typedef BoundBuffer       BaseType;
		typedef BoundIndexBuffer ThisType;

		BoundIndexBuffer(void)
			: BaseType()
		{
			;
		}

		void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid * indices)
		{
			this->object()->drawElements(this->m_target, this->m_unit, mode, count, type, indices);
		}

	protected:

		BoundIndexBuffer(const BufferHandle & handle, const IndexBufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}
};

namespace detail { template <> struct ParamsOf   <BoundIndexBuffer> { typedef IndexBufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf     <BoundIndexBuffer> { typedef BoundBuffer Type; }; };
namespace detail { template <> struct ObjectBase <BoundIndexBuffer> { typedef Buffer      Type; }; };
typedef   detail::ObjectSharedPointerTraits      <BoundIndexBuffer> ::Type BoundIndexBufferHandle;

class PixelPackBufferBindingParams : public BufferBindingParams
{
	public:

		typedef BufferBindingParams      BaseType;
		typedef PixelPackBufferBindingParams ThisType;

		PixelPackBufferBindingParams(void)
			: BaseType(GL_PIXEL_PACK_BUFFER, 0)
		{
			;
		}
};

class BoundPixelPackBuffer : public BoundBuffer
{
	friend class Context;

	public:

		typedef BoundBuffer          BaseType;
		typedef BoundPixelPackBuffer ThisType;

		BoundPixelPackBuffer(void)
			: BaseType()
		{
			;
		}

	protected:

		BoundPixelPackBuffer(const BufferHandle & handle, const PixelPackBufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}
};

namespace detail { template <> struct ParamsOf   <BoundPixelPackBuffer> { typedef PixelPackBufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf     <BoundPixelPackBuffer> { typedef BoundBuffer Type; }; };
namespace detail { template <> struct ObjectBase <BoundPixelPackBuffer> { typedef Buffer      Type; }; };
typedef   detail::ObjectSharedPointerTraits      <BoundPixelPackBuffer> ::Type BoundPixelPackBufferHandle;

class PixelUnpackBufferBindingParams : public BufferBindingParams
{
	public:

		typedef BufferBindingParams      BaseType;
		typedef PixelUnpackBufferBindingParams ThisType;

		PixelUnpackBufferBindingParams(void)
			: BaseType(GL_PIXEL_UNPACK_BUFFER, 0)
		{
			;
		}
};

class BoundPixelUnpackBuffer : public BoundBuffer
{
	friend class Context;

	public:

		typedef BoundBuffer          BaseType;
		typedef BoundPixelUnpackBuffer ThisType;

		BoundPixelUnpackBuffer(void)
			: BaseType()
		{
			;
		}

	protected:

		BoundPixelUnpackBuffer(const BufferHandle & handle, const PixelUnpackBufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}
};

namespace detail { template <> struct ParamsOf   <BoundPixelUnpackBuffer> { typedef PixelUnpackBufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf     <BoundPixelUnpackBuffer> { typedef BoundBuffer Type; }; };
namespace detail { template <> struct ObjectBase <BoundPixelUnpackBuffer> { typedef Buffer      Type; }; };
typedef   detail::ObjectSharedPointerTraits      <BoundPixelUnpackBuffer> ::Type BoundPixelUnpackBufferHandle;

class UniformBufferBindingParams : public BufferBindingParams
{
	public:

		typedef BufferBindingParams      BaseType;
		typedef UniformBufferBindingParams ThisType;

		GLintptr   offset;
		GLsizeiptr size;

		UniformBufferBindingParams(void)
			: BaseType (GL_UNIFORM_BUFFER, 0)
			, offset   (0)
			, size     (0)
		{
			;
		}

		UniformBufferBindingParams(GLuint aIndex, GLintptr aOffset, GLsizeiptr aSize)
			: BaseType (GL_UNIFORM_BUFFER, GLint(aIndex))
			, offset   (aOffset)
			, size     (aSize)
		{
			;
		}
};

class BoundUniformBuffer : public BoundBuffer
{
	friend class Context;

	public:

		typedef BoundBuffer          BaseType;
		typedef BoundUniformBuffer ThisType;

		BoundUniformBuffer(void)
			: BaseType()
		{
			;
		}

	protected:

		BoundUniformBuffer(const BufferHandle & handle, const UniformBufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}

		virtual void bind(void)
		{
			glBindBufferRange(this->m_target, GLuint(this->m_unit), this->object()->name(), this->m_offset, this->m_size);
		}

		virtual void unbind(void)
		{
			glBindBufferRange(this->m_target, GLuint(this->m_unit), 0, 0, 0);
		}

	private:

		GLintptr   m_offset;
		GLsizeiptr m_size;
};

namespace detail { template <> struct ParamsOf   <BoundUniformBuffer> { typedef UniformBufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf     <BoundUniformBuffer> { typedef BoundBuffer Type; }; };
namespace detail { template <> struct ObjectBase <BoundUniformBuffer> { typedef Buffer      Type; }; };
typedef   detail::ObjectSharedPointerTraits      <BoundUniformBuffer> ::Type BoundUniformBufferHandle;

class FeedbackBufferBindingParams : public BufferBindingParams
{
	public:

		typedef BufferBindingParams      BaseType;
		typedef FeedbackBufferBindingParams ThisType;

		GLintptr   offset;
		GLsizeiptr size;

		FeedbackBufferBindingParams(void)
			: BaseType (GL_TRANSFORM_FEEDBACK_BUFFER, 0)
			, offset   (0)
			, size     (0)
		{
			;
		}

		FeedbackBufferBindingParams(GLuint aIndex, GLintptr aOffset, GLsizeiptr aSize)
			: BaseType (GL_TRANSFORM_FEEDBACK_BUFFER, GLint(aIndex))
			, offset   (aOffset)
			, size     (aSize)
		{
			;
		}
};

class BoundFeedbackBuffer : public BoundBuffer
{
	friend class Context;

	public:

		typedef BoundBuffer          BaseType;
		typedef BoundFeedbackBuffer ThisType;

		BoundFeedbackBuffer(void)
			: BaseType()
		{
			;
		}

	protected:

		BoundFeedbackBuffer(const BufferHandle & handle, const FeedbackBufferBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}

		virtual void bind(void)
		{
			glBindBufferRange(this->m_target, GLuint(this->m_unit), this->object()->name(), this->m_offset, this->m_size);
		}

		virtual void unbind(void)
		{
			glBindBufferRange(this->m_target, GLuint(this->m_unit), 0, 0, 0);
		}

	private:

		GLintptr   m_offset;
		GLsizeiptr m_size;
};

namespace detail { template <> struct ParamsOf   <BoundFeedbackBuffer> { typedef FeedbackBufferBindingParams Type; }; };
namespace detail { template <> struct BaseOf     <BoundFeedbackBuffer> { typedef BoundBuffer Type; }; };
namespace detail { template <> struct ObjectBase <BoundFeedbackBuffer> { typedef Buffer      Type; }; };
typedef   detail::ObjectSharedPointerTraits      <BoundFeedbackBuffer> ::Type BoundFeedbackBufferHandle;

};

#endif // GLW_BUFFER_H