1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#pragma once
22
23// LOVE
24#include "common/config.h"
25#include "common/int.h"
26#include "vertex.h"
27#include "Resource.h"
28
29// C
30#include <stddef.h>
31
32namespace love
33{
34namespace graphics
35{
36
37/**
38 * A block of GPU-owned memory. Currently meant for internal use.
39 **/
40class Buffer : public Resource
41{
42public:
43
44 enum MapFlags
45 {
46 MAP_EXPLICIT_RANGE_MODIFY = (1 << 0), // see setMappedRangeModified.
47 MAP_READ = (1 << 1),
48 };
49
50 Buffer(size_t size, BufferType type, vertex::Usage usage, uint32 mapflags);
51 virtual ~Buffer();
52
53 size_t getSize() const { return size; }
54
55 BufferType getType() const { return type; }
56
57 vertex::Usage getUsage() const { return usage; }
58
59 bool isMapped() const { return is_mapped; }
60
61 /**
62 * Map the Buffer to client memory.
63 *
64 * This can be faster for large changes to the buffer. For smaller
65 * changes, see fill().
66 */
67 virtual void *map() = 0;
68
69 /**
70 * Unmap a previously mapped Buffer. The buffer must be unmapped when used
71 * to draw.
72 */
73 virtual void unmap() = 0;
74
75 /**
76 * Marks a range of mapped data as modified.
77 * NOTE: Buffer::fill calls this internally for you.
78 **/
79 virtual void setMappedRangeModified(size_t offset, size_t size) = 0;
80
81 /**
82 * Fill a portion of the buffer with data and marks the range as modified.
83 *
84 * @param offset The offset in the GLBuffer to store the data.
85 * @param size The size of the incoming data.
86 * @param data Pointer to memory to copy data from.
87 */
88 virtual void fill(size_t offset, size_t size, const void *data) = 0;
89
90 /**
91 * Copy the contents of this Buffer to another Buffer object.
92 **/
93 virtual void copyTo(size_t offset, size_t size, Buffer *other, size_t otheroffset) = 0;
94
95 uint32 getMapFlags() const { return map_flags; }
96
97 class Mapper
98 {
99 public:
100
101 /**
102 * Memory-maps a Buffer.
103 */
104 Mapper(Buffer &buffer)
105 : buf(buffer)
106 {
107 elems = buf.map();
108 }
109
110 /**
111 * unmaps the buffer
112 */
113 ~Mapper()
114 {
115 buf.unmap();
116 }
117
118 /**
119 * Get pointer to memory mapped region
120 */
121 void *get()
122 {
123 return elems;
124 }
125
126 private:
127
128 Buffer &buf;
129 void *elems;
130
131 }; // Mapper
132
133protected:
134
135 // The size of the buffer, in bytes.
136 size_t size;
137
138 // The type of the buffer object.
139 BufferType type;
140
141 // Usage hint. GL_[DYNAMIC, STATIC, STREAM]_DRAW.
142 vertex::Usage usage;
143
144 uint32 map_flags;
145
146 bool is_mapped;
147
148}; // Buffer
149
150} // graphics
151} // love
152