|
@@ -18,17 +18,12 @@
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
**/
|
|
|
|
|
|
-
|
|
|
-#ifndef LOVE_GRAPHICS_OPENGL_GL_BUFFER_H
|
|
|
-#define LOVE_GRAPHICS_OPENGL_GL_BUFFER_H
|
|
|
+#pragma once
|
|
|
|
|
|
// LOVE
|
|
|
#include "common/config.h"
|
|
|
-#include "graphics/Volatile.h"
|
|
|
-#include "graphics/vertex.h"
|
|
|
-
|
|
|
-// OpenGL
|
|
|
-#include "OpenGL.h"
|
|
|
+#include "common/int.h"
|
|
|
+#include "vertex.h"
|
|
|
|
|
|
// C
|
|
|
#include <stddef.h>
|
|
@@ -37,57 +32,35 @@ namespace love
|
|
|
{
|
|
|
namespace graphics
|
|
|
{
|
|
|
-namespace opengl
|
|
|
-{
|
|
|
+
|
|
|
+class Graphics;
|
|
|
|
|
|
/**
|
|
|
- * GLBuffer is a thin abstraction over OpenGL's Buffer Objects.
|
|
|
- * The class is meant for internal use.
|
|
|
- */
|
|
|
-class GLBuffer : public Volatile
|
|
|
+ * A block of GPU-owned memory. Currently meant for internal use.
|
|
|
+ **/
|
|
|
+class Buffer
|
|
|
{
|
|
|
public:
|
|
|
|
|
|
enum MapFlags
|
|
|
{
|
|
|
- MAP_EXPLICIT_RANGE_MODIFY = 0x01, // see setMappedRangeModified.
|
|
|
+ MAP_EXPLICIT_RANGE_MODIFY = (1 << 0), // see setMappedRangeModified.
|
|
|
+ MAP_READ = (1 << 1),
|
|
|
};
|
|
|
|
|
|
- /**
|
|
|
- * Constructor.
|
|
|
- *
|
|
|
- * @param size The size of the GLBuffer in bytes.
|
|
|
- * @param type The type of the buffer object.
|
|
|
- * @param usage Usage hint.
|
|
|
- */
|
|
|
- GLBuffer(size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags = 0);
|
|
|
-
|
|
|
- /**
|
|
|
- * Destructor.
|
|
|
- */
|
|
|
- virtual ~GLBuffer();
|
|
|
+ Buffer(size_t size, BufferType type, vertex::Usage usage, uint32 mapflags);
|
|
|
+ virtual ~Buffer();
|
|
|
|
|
|
- /**
|
|
|
- * Get the size of the GLBuffer, in bytes.
|
|
|
- *
|
|
|
- * @return The size of the GLBuffer.
|
|
|
- */
|
|
|
size_t getSize() const
|
|
|
{
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Get the type of the buffer object.
|
|
|
- */
|
|
|
BufferType getType() const
|
|
|
{
|
|
|
return type;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Get the usage hint for this GLBuffer.
|
|
|
- */
|
|
|
vertex::Usage getUsage() const
|
|
|
{
|
|
|
return usage;
|
|
@@ -99,32 +72,29 @@ public:
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Map the GLBuffer to client memory.
|
|
|
+ * Map the Buffer to client memory.
|
|
|
*
|
|
|
* This can be faster for large changes to the buffer. For smaller
|
|
|
* changes, see fill().
|
|
|
- *
|
|
|
- * @return A pointer to memory which represents the buffer.
|
|
|
*/
|
|
|
- void *map();
|
|
|
+ virtual void *map() = 0;
|
|
|
|
|
|
/**
|
|
|
- * Unmap a previously mapped GLBuffer. The buffer must be unmapped
|
|
|
- * when used to draw elements.
|
|
|
+ * Unmap a previously mapped Buffer. The buffer must be unmapped when used
|
|
|
+ * to draw.
|
|
|
*/
|
|
|
- void unmap();
|
|
|
+ virtual void unmap() = 0;
|
|
|
|
|
|
/**
|
|
|
* Marks a range of mapped data as modified.
|
|
|
- * NOTE: GLBuffer::fill calls this internally for you.
|
|
|
+ * NOTE: Buffer::fill calls this internally for you.
|
|
|
**/
|
|
|
- void setMappedRangeModified(size_t offset, size_t size);
|
|
|
+ virtual void setMappedRangeModified(size_t offset, size_t size) = 0;
|
|
|
|
|
|
/**
|
|
|
- * Bind the GLBuffer to its specified target.
|
|
|
- * (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, etc).
|
|
|
- */
|
|
|
- void bind();
|
|
|
+ * Gets the backend-specific handle for this Buffer.
|
|
|
+ **/
|
|
|
+ virtual ptrdiff_t getHandle() const = 0;
|
|
|
|
|
|
/**
|
|
|
* Fill a portion of the buffer with data and marks the range as modified.
|
|
@@ -133,34 +103,27 @@ public:
|
|
|
* @param size The size of the incoming data.
|
|
|
* @param data Pointer to memory to copy data from.
|
|
|
*/
|
|
|
- void fill(size_t offset, size_t size, const void *data);
|
|
|
+ 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.
|
|
|
- */
|
|
|
- const void *getPointer(size_t offset) const;
|
|
|
+ * Copy the contents of this Buffer to another Buffer object.
|
|
|
+ **/
|
|
|
+ virtual void copyTo(size_t offset, size_t size, Buffer *other, size_t otheroffset) = 0;
|
|
|
|
|
|
uint32 getMapFlags() const
|
|
|
{
|
|
|
return map_flags;
|
|
|
}
|
|
|
|
|
|
- // Implements Volatile.
|
|
|
- bool loadVolatile() override;
|
|
|
- void unloadVolatile() override;
|
|
|
-
|
|
|
class Mapper
|
|
|
{
|
|
|
public:
|
|
|
|
|
|
/**
|
|
|
- * Memory-maps a GLBuffer.
|
|
|
+ * Memory-maps a Buffer.
|
|
|
*/
|
|
|
- Mapper(GLBuffer &buffer)
|
|
|
- : buf(buffer)
|
|
|
+ Mapper(Buffer &buffer)
|
|
|
+ : buf(buffer)
|
|
|
{
|
|
|
elems = buf.map();
|
|
|
}
|
|
@@ -183,57 +146,33 @@ public:
|
|
|
|
|
|
private:
|
|
|
|
|
|
- GLBuffer &buf;
|
|
|
+ Buffer &buf;
|
|
|
void *elems;
|
|
|
|
|
|
}; // Mapper
|
|
|
|
|
|
-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);
|
|
|
- void unload();
|
|
|
-
|
|
|
- void unmapStatic(size_t offset, size_t size);
|
|
|
- void unmapStream();
|
|
|
-
|
|
|
- // Whether the buffer is currently mapped to main memory.
|
|
|
- bool is_mapped;
|
|
|
+protected:
|
|
|
|
|
|
// The size of the buffer, in bytes.
|
|
|
size_t size;
|
|
|
|
|
|
// The type of the buffer object.
|
|
|
BufferType type;
|
|
|
- GLenum target;
|
|
|
|
|
|
// Usage hint. GL_[DYNAMIC, STATIC, STREAM]_DRAW.
|
|
|
vertex::Usage usage;
|
|
|
-
|
|
|
- // The VBO identifier. Assigned by OpenGL.
|
|
|
- GLuint vbo;
|
|
|
-
|
|
|
- // A pointer to mapped memory.
|
|
|
- char *memory_map;
|
|
|
-
|
|
|
- size_t modified_offset;
|
|
|
- size_t modified_size;
|
|
|
-
|
|
|
+
|
|
|
uint32 map_flags;
|
|
|
|
|
|
-}; // GLBuffer
|
|
|
-
|
|
|
+ bool is_mapped;
|
|
|
+
|
|
|
+}; // Buffer
|
|
|
|
|
|
/**
|
|
|
- * QuadIndices manages one shared GLBuffer 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.
|
|
|
+ * QuadIndices manages one shared Buffer 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 indexed draw
|
|
|
+ * call.
|
|
|
*
|
|
|
* indices[i*6 + 0] = i*4 + 0;
|
|
|
* indices[i*6 + 1] = i*4 + 1;
|
|
@@ -243,25 +182,23 @@ private:
|
|
|
* indices[i*6 + 4] = i*4 + 1;
|
|
|
* indices[i*6 + 5] = i*4 + 3;
|
|
|
*
|
|
|
- * There will always be a large enough GLBuffer around until all
|
|
|
- * QuadIndices instances have been deleted.
|
|
|
+ * There will always be a large enough Buffer around until all QuadIndices
|
|
|
+ * instances have been deleted.
|
|
|
*
|
|
|
* Q: Why have something like QuadIndices?
|
|
|
- * A: The indices for the SpriteBatch do not change, only the array size
|
|
|
- * varies. Using one GLBuffer for all element arrays removes this
|
|
|
- * duplicated data and saves some memory.
|
|
|
+ * A: The indices for the SpriteBatch do not change, only the array size varies.
|
|
|
+ * Using one Buffer for all element arrays removes this duplicated data and
|
|
|
+ * saves some memory.
|
|
|
*/
|
|
|
class QuadIndices
|
|
|
{
|
|
|
public:
|
|
|
/**
|
|
|
- * Adds an entry to the list of sizes and resizes the GLBuffer
|
|
|
+ * Adds an entry to the list of sizes and resizes the Buffer
|
|
|
* 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.
|
|
|
*/
|
|
|
- QuadIndices(size_t size);
|
|
|
+ QuadIndices(Graphics *gfx, size_t size);
|
|
|
|
|
|
QuadIndices(const QuadIndices &other);
|
|
|
QuadIndices &operator = (const QuadIndices &other);
|
|
@@ -275,8 +212,6 @@ public:
|
|
|
/**
|
|
|
* 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;
|
|
|
|
|
@@ -284,9 +219,6 @@ public:
|
|
|
* Returns the number of indices that the passed element count will have.
|
|
|
* Use QuadIndices::getSize to get the full index count for that
|
|
|
* QuadIndices instance.
|
|
|
- *
|
|
|
- * @param elements The number of elements to calculate the index count for.
|
|
|
- * @return The index count.
|
|
|
*/
|
|
|
size_t getIndexCount(size_t elements) const;
|
|
|
|
|
@@ -294,12 +226,9 @@ public:
|
|
|
* 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
|
|
|
+ IndexDataType getType(size_t s) const;
|
|
|
+ inline IndexDataType getType() const
|
|
|
{
|
|
|
return getType(maxSize);
|
|
|
}
|
|
@@ -308,28 +237,16 @@ public:
|
|
|
* Returns the size in bytes of an element in the element array.
|
|
|
* Can be used with getPointer to calculate an offset into the array based
|
|
|
* on a number of elements.
|
|
|
- *
|
|
|
- * @return The size of an element in bytes.
|
|
|
**/
|
|
|
size_t getElementSize();
|
|
|
|
|
|
/**
|
|
|
- * Returns the pointer to the GLBuffer.
|
|
|
- * The pointer will change if a new size request or removal causes
|
|
|
- * a GLBuffer resize. It is recommended to retrieve the pointer
|
|
|
- * value directly before the drawing call.
|
|
|
- *
|
|
|
- * @return The pointer to the GLBuffer.
|
|
|
+ * Returns the pointer to the Buffer.
|
|
|
+ * The pointer will change if a new size request or removal causes a Buffer
|
|
|
+ * resize. It is recommended to retrieve the pointer value directly before
|
|
|
+ * the drawing call.
|
|
|
*/
|
|
|
- GLBuffer *getBuffer() 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;
|
|
|
+ Buffer *getBuffer() const;
|
|
|
|
|
|
/**
|
|
|
* Returns a direct pointer to the index data.
|
|
@@ -339,9 +256,6 @@ public:
|
|
|
* the index data came from an Index Buffer.
|
|
|
* So the direct pointer to the index buffer should be used instead of the
|
|
|
* index buffer when rendering using client-side vertex arrays.
|
|
|
- *
|
|
|
- * @param offset An offset in bytes into the index data.
|
|
|
- * @return A direct pointer to the index data at the specified offset.
|
|
|
**/
|
|
|
const void *getIndices(size_t offset) const;
|
|
|
|
|
@@ -360,20 +274,18 @@ private:
|
|
|
// The size in bytes of an element in the element array.
|
|
|
static size_t elementSize;
|
|
|
|
|
|
- // The current GLBuffer size. 0 means no GLBuffer.
|
|
|
+ // The current GLBuffer size. 0 means no Buffer.
|
|
|
static size_t maxSize;
|
|
|
|
|
|
static size_t objectCount;
|
|
|
|
|
|
- // The GLBuffer for the element array. Can be null.
|
|
|
- static GLBuffer *indexBuffer;
|
|
|
-
|
|
|
+ // The Buffer for the element array. Can be null.
|
|
|
+ static Buffer *indexBuffer;
|
|
|
+
|
|
|
// The array of indices that will also be stored in the index buffer.
|
|
|
static char *indices;
|
|
|
-};
|
|
|
|
|
|
-} // opengl
|
|
|
+}; // QuadIndices
|
|
|
+
|
|
|
} // graphics
|
|
|
} // love
|
|
|
-
|
|
|
-#endif // LOVE_GRAPHICS_OPENGL_GL_BUFFER_H
|