Source

love / src / modules / graphics / opengl / VertexBuffer.h

Bart van Strien 86a44f2 





































Marc Lepage 7428fd5 
Bart van Strien 86a44f2 


























































































































































































































































































vrld 7f3b0b4 


Bart van Strien 86a44f2 
vrld 7f3b0b4 






Bart van Strien 86a44f2 





vrld 9ab0ca0 





































































































































Bart van Strien 86a44f2 



  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
/**
 * Copyright (c) 2006-2012 LOVE Development Team
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 **/


#ifndef LOVE_GRAPHICS_OPENGL_VERTEX_BUFFER_H
#define LOVE_GRAPHICS_OPENGL_VERTEX_BUFFER_H

// LOVE
#include "graphics/Volatile.h"

// OpenGL
#include "GLee.h"

namespace love
{
namespace graphics
{
namespace opengl
{

/**
 * VertexBuffer is an abstraction over VBOs (Vertex Buffer Objects), which
 * falls back to regular vertex arrays if VBOs are not supported.
 *
 * This allows code to take advantage of VBOs where available, but still
 * work on older systems where it's *not* available. Everyone's happy.
 *
 * The class is (for now) meant for internal use.
 */
class VertexBuffer
{
public:

	/**
	 * Create a new VertexBuffer (either a plain vertex array, or a VBO),
	 * based on what's supported on the system.
	 *
	 * If VBOs are not supported, a plain vertex array will automatically
	 * be created and returned instead.
	 *
	 * @param size The size of the VertexBuffer (in bytes).
	 * @param target GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER.
	 * @param usage GL_DYNAMIC_DRAW, etc.
	 * @return A new VertexBuffer.
	 */
	static VertexBuffer *Create(size_t size, GLenum target, GLenum usage);

	/**
	 * Constructor.
	 *
	 * @param size The size of the VertexBuffer in bytes.
	 * @param target The target VertexBuffer object, e.g. GL_ARRAY_BUFFER.
	 * @param usage Usage hint, e.g. GL_DYNAMIC_DRAW.
	 */
	VertexBuffer(size_t size, GLenum target, GLenum usage);

	/**
	 * Destructor. Does nothing, but must be declared virtual.
	 */
	virtual ~VertexBuffer();

	/**
	 * Get the size of the VertexBuffer, in bytes.
	 *
	 * @return The size of the VertexBuffer.
	 */
	size_t getSize() const
	{
		return size;
	}

	/**
	 * Get the target buffer object.
	 *
	 * @return The target buffer object, e.g. GL_ARRAY_BUFFER.
	 */
	GLenum getTarget() const
	{
		return target;
	}

	/**
	 * Get the usage hint for this VertexBuffer.
	 *
	 * @return The usage hint, e.g. GL_DYNAMIC_DRAW.
	 */
	GLenum getUsage() const
	{
		return usage;
	}

	/**
	 * Map the VertexBuffer to client memory.
	 *
	 * This can be faster for large changes to the buffer. For smaller
	 * changes, see fill().
	 *
	 * The VertexBuffer must be bound to use this function.
	 *
	 * @return A pointer to memory which represents the buffer.
	 */
	virtual void *map() = 0;

	/**
	 * Unmap a previously mapped VertexBuffer. The buffer must be unmapped
	 * when used to draw elements.
	 *
	 * The VertexBuffer must be bound to use this function.
	 */
	virtual void unmap() = 0;

	/**
	 * Bind the VertexBuffer to the specified target.
	 * (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER).
	 *
	 * @param target GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER.
	 */
	virtual void bind() = 0;

	/**
	 * Unbind a prevously bound VertexBuffer.
	 */
	virtual void unbind() = 0;

	/**
	 * Fill a portion of the buffer with data.
	 *
	 * The VertexBuffer must be bound to use this function.
	 *
	 * @param offset The offset in the VertexBuffer to store the data.
	 * @param size The size of the incoming data.
	 * @param data Pointer to memory to copy data from.
	 */
	virtual void fill(size_t offset, size_t size, const void *data) = 0;

	/**
	 * Get a pointer which represents the specified byte offset.
	 *
	 * @param offset The byte offset. (0 is first byte).
	 * @return A pointer which represents the offset.
	 */
	virtual const void *getPointer(size_t offset) const = 0;

	/**
	 * This helper class can bind a VertexArray temporarily, and
	 * automatically un-bind when it's destroyed.
	 */
	class Bind
	{
	public:

		/**
		 * Bind a VertexBuffer.
		 */
		Bind(VertexBuffer &buf)
			: buf(buf)
		{
			buf.bind();
		}

		/**
		 * Unbinds a VertexBuffer.
		 */
		~Bind()
		{
			buf.unbind();
		}

	private:

		// VertexBuffer to work on.
		VertexBuffer &buf;
	};

	class Mapper
	{
	public:
		/**
		 * Memory-maps a VertexBuffer.
		 */
		Mapper(VertexBuffer &buffer)
			: buf(buffer)
		{
			elems = buf.map();
		}

		/**
		 * unmaps the buffer
		 */
		~Mapper()
		{
			buf.unmap();
		}

		/**
		 * Get pointer to memory mapped region
		 */
		void *get()
		{
			return elems;
		}

	private:
		VertexBuffer &buf;
		void *elems;
	};

private:

	// The size of the buffer, in bytes.
	size_t size;

	// The target buffer object. (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER).
	GLenum target;

	// Usage hint. GL_[DYNAMIC, STATIC, STREAM]_DRAW.
	GLenum usage;
};

/**
 * Implementation of VertexBuffer which uses plain arrays to store the data.
 *
 * This implementation should be supported everywhere, and acts as a fallback
 * on systems which do not support VBOs.
 */
class VertexArray : public VertexBuffer
{
public:

	/**
	 * @copydoc VertexBuffer(int, GLenum, GLenum)
	 */
	VertexArray(size_t size, GLenum target, GLenum usage);

	/**
	 * Frees the data we've allocated.
	 */
	virtual ~VertexArray();

	// Implements VertexBuffer.
	virtual void *map();
	virtual void unmap();
	virtual void bind();
	virtual void unbind();
	virtual void fill(size_t offset, size_t size, const void *data);
	virtual const void *getPointer(size_t offset) const ;

private:
	// Holds the data.
	char *buf;
};

/**
 * Vertex Buffer Object (VBO) implementation of VertexBuffer.
 *
 * This will be used on all systems that support it. It's in general
 * faster than vertex arrays, but especially in use-cases where there
 * is no need to update the data every frame.
 */
class VBO : public VertexBuffer, public Volatile
{
public:

	/**
	 * @copydoc VertexBuffer(size_t, GLenum, GLenum)
	 */
	VBO(size_t size, GLenum target, GLenum usage);

	/**
	 * Deletes the VBOs from OpenGL.
	 */
	virtual ~VBO();

	// Implements VertexBuffer.
	virtual void *map();
	virtual void unmap();
	virtual void bind();
	virtual void unbind();
	virtual void fill(size_t offset, size_t size, const void *data);
	virtual const void *getPointer(size_t offset) const ;

	// Implements Volatile.
	bool loadVolatile();
	void unloadVolatile();

private:

	/**
	 * Creates the VBO, and optionally restores data we saved earlier.
	 *
	 * @param restore True to restore data previously saved with 'unload'.
	 * @return True on success, false otherwise.
	 */
	bool load(bool restore);

	/**
	 * Optionally save the data in the VBO, then delete it.
	 *
	 * @param save True to save the data before deleting.
	 */
	void unload(bool save);

	// The VBO identifier. Assigned by OpenGL.
	GLuint vbo;

	// A pointer to mapped memory. Will be inialized on the first
	// call to map().
	void *memory_map;

	// Set if the vbo currently operates on main instead of gpu
	// memory.
	bool is_mapped;

	// Set if the buffer was modified while operating on gpu memory
	// and needs to be synchronized.
	bool is_dirty;

	// Usage hint for map()/unmap() pair. Same as `access' parameter in
	// glBufferData or 0 if not mapped.
	GLenum mapped_access;
};

/**
 * VertexIndex manages one shared VertexBuffer that stores the indices for an
 * element array. Vertex arrays using the vertex structure (or anything else
 * that can use the pattern below) can request a size and use it for the
 * drawElements call.
 *
 *  indices[i*6 + 0] = i*4 + 0;
 *  indices[i*6 + 1] = i*4 + 1;
 *  indices[i*6 + 2] = i*4 + 2;
 *
 *  indices[i*6 + 3] = i*4 + 0;
 *  indices[i*6 + 4] = i*4 + 2;
 *  indices[i*6 + 5] = i*4 + 3;
 *
 * There will always be a large enough VertexBuffer around until all
 * VertexIndex instances have been deleted.
 *
 * Q: Why have something like VertexIndex?
 * A: The indices for the SpriteBatch do not change, only the array size
 * varies. Using one VertexBuffer for all element arrays removes this
 * duplicated data and saves some memory.
 */
class VertexIndex
{
public:
	/**
	 * Adds an entry to the list of sizes and resizes the VertexBuffer
	 * if needed. A size of 1 allocates a group of 6 indices for 4 vertices
	 * creating 1 face.
	 *
	 * @param size The requested size in groups of 6 indices.
	 */
	VertexIndex(size_t size);

	/**
	 * Removes an entry from the list of sizes and resizes the VertexBuffer
	 * if needed.
	 */
	~VertexIndex();

	/**
	 * Returns the number of index groups.
	 * This can be used for getIndexCount to get the full count of indices.
	 *
	 * @return The number of index groups.
	 */
	size_t getSize() const;

	/**
	 * Returns the number of indices that the passed element count will have.
	 * Use VertexIndex::getSize to get the full index count for that
	 * VertexIndex instance.
	 *
	 * @param elements The number of elements to calculate the index count for.
	 * @return The index count.
	 */
	size_t getIndexCount(size_t elements) const;

	/**
	 * Returns the integer type of the element array.
	 * If an optional nonzero size argument is passed, the function returns
	 * the integer type of the element array of that size.
	 *
	 * @param s The size of the array to calculated the integer type of.
	 * @return The element array integer type.
	 */
	GLenum getType(size_t s) const;
	inline GLenum getType() const
	{
		return getType(maxSize);
	}

	/**
	 * Returns the pointer to the VertexBuffer.
	 * The pointer will change if a new size request or removal causes
	 * a VertexBuffer resize. It is recommended to retrieve the pointer
	 * value directly before the drawing call.
	 *
	 * @return The pointer to the VertexBuffer.
	 */
	VertexBuffer *getVertexBuffer() const;

	/**
	 * Returns a pointer which represents the specified byte offset.
	 *
	 * @param offset The offset in bytes.
	 * @return A pointer which represents the offset.
	 */
	const void *getPointer(size_t offset) const;

private:

	/**
	 * Adds a new size to the size list, then sorts and resizes it if needed.
	 *
	 * @param newSize The new size to be added.
	 */
	void addSize(size_t newSize);

	/**
	 * Removes a size from the size list, then sorts and resizes it if needed.
	 *
	 * @param oldSize The old size to be removed.
	 */
	void removeSize(size_t oldSize);

	/**
	 * Resizes the VertexBuffer to the requested size.
	 * This function takes care of choosing the correct integer type and
	 * allocating and deleting the VertexBuffer instance. It also has some
	 * fallback logic in case the memory ran out.
	 *
	 * @param size The requested VertexBuffer size. Passing 0 deletes the VertexBuffer without allocating a new one.
	 */
	void resize(size_t size);

	/**
	 * Adds all indices to the array with the type T.
	 * There are no checks for the correct types or overflows. The calling
	 * function should check for that.
	 */
	template <typename T> void fill();

	// The size of the array requested by this instance.
	size_t size;

	// The current VertexBuffer size. 0 means no VertexBuffer.
	static size_t maxSize;
	// The list of sizes. Needs to be kept sorted in ascending order.
	static std::list<size_t> sizeRefs;
	// The VertexBuffer for the element array. Can be NULL.
	static VertexBuffer *element_array;
};

} // opengl
} // graphics
} // love

#endif // LOVE_GRAPHICS_OPENGL_SPRITE_BATCH_H