Debian Codegraph @

include/igeometrystore.h

raw
1 #pragma once
2
3 #include <cstdint>
4 #include <vector>
5 #include "math/AABB.h"
6 #include "render/RenderVertex.h"
7
8 namespace render
9 {
10
11 // A sync object guarding a single frame buffer (glFenceSync)
12 class ISyncObject
13 {
14 public:
15 using Ptr = std::shared_ptr<ISyncObject>;
16
17 virtual ~ISyncObject() {}
18
19 // Blocks until the guarded buffer is no longer in use
20 virtual void wait() = 0;
21 };
22
23 // Creates sync objects to safely switch between frame buffers
24 // An implementation of this interface may be required to construct an IGeometryStore
25 class ISyncObjectProvider
26 {
27 public:
28 virtual ~ISyncObjectProvider() {}
29
30 // Create a sync object (fence)
31 virtual ISyncObject::Ptr createSyncObject() = 0;
32 };
33
34 // Represents a buffer object as provided by the openGL implementation
35 // Operates on byte (unsigned char) data types.
36 class IBufferObject
37 {
38 public:
39 virtual ~IBufferObject() {}
40
41 enum class Type
42 {
43 Vertex, // vertex buffer
44 Index // index buffer
45 };
46
47 using Ptr = std::shared_ptr<IBufferObject>;
48
49 // Binds this object (glBindBuffer)
50 virtual void bind() = 0;
51
52 // Release the binding, aka glBindBuffer(0)
53 virtual void unbind() = 0;
54
55 // Uploads the given data to the buffer, starting at the given offset
56 virtual void setData(std::size_t offset, const unsigned char* firstElement, std::size_t numBytes) = 0;
57
58 // Downloads the specified data chunk
59 virtual std::vector<unsigned char> getData(std::size_t offset, std::size_t numBytes) = 0;
60
61 // Re-allocates the memory of this buffer, does not transfer the data
62 // from the old internal buffer to the new one.
63 virtual void resize(std::size_t newSize) = 0;
64 };
65
66 // Factory class creating new buffer objects
67 class IBufferObjectProvider
68 {
69 public:
70 virtual ~IBufferObjectProvider() {}
71
72 // Creates a new, empty buffer object of 0 size. Has to be resized before use.
73 virtual IBufferObject::Ptr createBufferObject(IBufferObject::Type type) = 0;
74 };
75
76 /**
77 * Storage container for indexed vertex data.
78 *
79 * Client code will allocate fixed-size blocks of continuous
80 * memory for vertex and index data.
81 *
82 * The Block handle will remain valid until relasing it,
83 * though the underlying memory location is subject to change.
84 *
85 * Blocks cannot be resized after allocation.
86 *
87 * All the vertex data will is guaranteed to belong to the same continuous
88 * large block of memory, making it suitable for openGL multi draw calls.
89 */
90 class IGeometryStore
91 {
92 public:
93 virtual ~IGeometryStore() {}
94
95 // Slot ID handed out to client code
96 using Slot = std::uint64_t;
97
98 /// List of vertices
99 using Vertices = std::vector<RenderVertex>;
100
101 /// List of indices
102 using Indices = std::vector<unsigned int>;
103
104 /**
105 * Allocate memory blocks, one for vertices and one for indices, of the given size.
106 * The block can be populated using updateData(), where it's possible to
107 * fill the entire block or just a portion of it.
108 * Returns a handle as reference to the block for use in later calls.
109 * The allocated block cannot be resized later.
110 */
111 virtual Slot allocateSlot(std::size_t numVertices, std::size_t numIndices) = 0;
112
113 /**
114 * Allocate memory to store an alternative set of indices referencing
115 * an existing set of vertices. When rendering this re-mapped geometry, it will
116 * re-use the vertices of that other slot with the indices defined in this slot.
117 *
118 * With the returned handle, user code can invoke the update[Sub]Data() methods
119 * to upload the indices, but the passed vertex set has to be empty.
120 *
121 * Use the regular deallocate() method to release this slot.
122 *
123 * The index remap slot is depending on the one containing the vertex data, if the latter
124 * is removed, this slot becomes invalid and behaviour is undefined.
125 */
126 virtual Slot allocateIndexSlot(Slot slotContainingVertexData, std::size_t numIndices) = 0;
127
128 /**
129 * Load vertex and index data into the specified block. The given vertex and
130 * index arrays must not be larger than what has been allocated earlier,
131 * but they're allowed to be smaller.
132 */
133 virtual void updateData(Slot slot, const Vertices& vertices, const Indices& indices) = 0;
134
135 /**
136 * Updates the data of an index slot. Equivalent to calling updateData() with
137 * an empty set of vertices.
138 */
139 virtual void updateIndexData(Slot slot, const Indices& indices)
140 {
141 updateData(slot, {}, indices);
142 }
143
144 /**
145 * Load a chunk of vertex and index data into the specified range, starting
146 * from vertexOffset/indexOffset respectively. The affected range must not be out of bounds
147 * of the allocated slot.
148 */
149 virtual void updateSubData(
150 Slot slot, std::size_t vertexOffset, const Vertices& vertices, std::size_t indexOffset,
151 const Indices& indices
152 ) = 0;
153
154 /**
155 * Updates a portion of index data in an index slot. Equivalent to calling updateSubData() with
156 * an empty set of vertices.
157 */
158 virtual void updateIndexSubData(Slot slot, std::size_t indexOffset, const Indices& indices)
159 {
160 updateSubData(slot, 0, {}, indexOffset, indices);
161 }
162
163 /**
164 * Called in case the stored data in the given slot should just be cut off at the end.
165 */
166 virtual void resizeData(Slot slot, std::size_t vertexSize, std::size_t indexSize) = 0;
167
168 /**
169 * Resize the index data in an index slot. Equivalent to calling resizeData() with vertexSize == 0.
170 */
171 virtual void resizeIndexData(Slot slot, std::size_t indexSize)
172 {
173 resizeData(slot, 0, indexSize);
174 }
175
176 /**
177 * Releases the memory allocated by the given slot.
178 * The Slot ID is invalidated by this operation and should no longer be used.
179 */
180 virtual void deallocateSlot(Slot slot) = 0;
181
182 /// The memory addresses required for rendering surfaces using gl(Multi)DrawElements
183 struct BufferAddresses
184 {
185 const RenderVertex* bufferStart; // start of buffer (to pass to gl*Pointer, usually nullptr)
186 const RenderVertex* clientBufferStart; // start of buffer in client memory
187 const unsigned int* firstIndex; // first index location of the given geometry (to pass to glDraw*)
188 const unsigned int* clientFirstIndex; // first index location of the given geometry in client memory
189 std::size_t indexCount; // index count of the given geometry
190 std::size_t firstVertex; // offset to the first vertex of this surface
191 };
192
193 /**
194 * @brief Get the memory layout information necessary to render the given slot with
195 * OpenGL functions, e.g. glDrawElements().
196 *
197 * Don't store these pointers on the client side, they will be only be valid for a
198 * certain amount of time, at the latest until allocateSlot or deallocateSlot are
199 * invoked.
200 */
201 virtual BufferAddresses getBufferAddresses(Slot slot) const = 0;
202
203 /**
204 * Returns the bounds of the geometry stored in the given slot.
205 * Note that this will only take those vertices into account
206 * that are actually referenced by any index in the slot.
207 */
208 virtual AABB getBounds(Slot slot) const = 0;
209
210 // Return the buffer objects of the current frame
211 virtual std::pair<IBufferObject::Ptr, IBufferObject::Ptr> getBufferObjects() = 0;
212
213 // Synchronises the data in the currently active framebuffer to the attached IBufferObjects
214 virtual void syncToBufferObjects() = 0;
215 };
216
217 }