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 | #include "common/config.h" |
22 | #include "SpriteBatch.h" |
23 | |
24 | // LOVE |
25 | #include "Texture.h" |
26 | #include "Quad.h" |
27 | #include "Graphics.h" |
28 | #include "Buffer.h" |
29 | |
30 | // C++ |
31 | #include <algorithm> |
32 | |
33 | // C |
34 | #include <stddef.h> |
35 | |
36 | namespace love |
37 | { |
38 | namespace graphics |
39 | { |
40 | |
41 | love::Type SpriteBatch::type("SpriteBatch" , &Drawable::type); |
42 | |
43 | SpriteBatch::SpriteBatch(Graphics *gfx, Texture *texture, int size, vertex::Usage usage) |
44 | : texture(texture) |
45 | , size(size) |
46 | , next(0) |
47 | , color(255, 255, 255, 255) |
48 | , color_active(false) |
49 | , array_buf(nullptr) |
50 | , range_start(-1) |
51 | , range_count(-1) |
52 | { |
53 | if (size <= 0) |
54 | throw love::Exception("Invalid SpriteBatch size." ); |
55 | |
56 | if (texture == nullptr) |
57 | throw love::Exception("A texture must be used when creating a SpriteBatch." ); |
58 | |
59 | if (texture->getTextureType() == TEXTURE_2D_ARRAY) |
60 | vertex_format = vertex::CommonFormat::XYf_STPf_RGBAub; |
61 | else |
62 | vertex_format = vertex::CommonFormat::XYf_STf_RGBAub; |
63 | |
64 | vertex_stride = vertex::getFormatStride(vertex_format); |
65 | |
66 | size_t vertex_size = vertex_stride * 4 * size; |
67 | array_buf = gfx->newBuffer(vertex_size, nullptr, BUFFER_VERTEX, usage, Buffer::MAP_EXPLICIT_RANGE_MODIFY); |
68 | } |
69 | |
70 | SpriteBatch::~SpriteBatch() |
71 | { |
72 | delete array_buf; |
73 | } |
74 | |
75 | int SpriteBatch::add(const Matrix4 &m, int index /*= -1*/) |
76 | { |
77 | return add(texture->getQuad(), m, index); |
78 | } |
79 | |
80 | int SpriteBatch::add(Quad *quad, const Matrix4 &m, int index /*= -1*/) |
81 | { |
82 | using namespace vertex; |
83 | |
84 | if (vertex_format == CommonFormat::XYf_STPf_RGBAub) |
85 | return addLayer(quad->getLayer(), quad, m, index); |
86 | |
87 | if (index < -1 || index >= size) |
88 | throw love::Exception("Invalid sprite index: %d" , index + 1); |
89 | |
90 | if (index == -1 && next >= size) |
91 | setBufferSize(size * 2); |
92 | |
93 | const Vector2 *quadpositions = quad->getVertexPositions(); |
94 | const Vector2 *quadtexcoords = quad->getVertexTexCoords(); |
95 | |
96 | // Always keep the buffer mapped when adding data (it'll be unmapped on draw.) |
97 | size_t offset = (index == -1 ? next : index) * vertex_stride * 4; |
98 | auto verts = (XYf_STf_RGBAub *) ((uint8 *) array_buf->map() + offset); |
99 | |
100 | m.transformXY(verts, quadpositions, 4); |
101 | |
102 | for (int i = 0; i < 4; i++) |
103 | { |
104 | verts[i].s = quadtexcoords[i].x; |
105 | verts[i].t = quadtexcoords[i].y; |
106 | verts[i].color = color; |
107 | } |
108 | |
109 | array_buf->setMappedRangeModified(offset, vertex_stride * 4); |
110 | |
111 | // Increment counter. |
112 | if (index == -1) |
113 | return next++; |
114 | |
115 | return index; |
116 | } |
117 | |
118 | int SpriteBatch::addLayer(int layer, const Matrix4 &m, int index) |
119 | { |
120 | return addLayer(layer, texture->getQuad(), m, index); |
121 | } |
122 | |
123 | int SpriteBatch::addLayer(int layer, Quad *quad, const Matrix4 &m, int index) |
124 | { |
125 | using namespace vertex; |
126 | |
127 | if (vertex_format != CommonFormat::XYf_STPf_RGBAub) |
128 | throw love::Exception("addLayer can only be called on a SpriteBatch that uses an Array Texture!" ); |
129 | |
130 | if (index < -1 || index >= size) |
131 | throw love::Exception("Invalid sprite index: %d" , index + 1); |
132 | |
133 | if (layer < 0 || layer >= texture->getLayerCount()) |
134 | throw love::Exception("Invalid layer: %d (Texture has %d layers)" , layer + 1, texture->getLayerCount()); |
135 | |
136 | if (index == -1 && next >= size) |
137 | setBufferSize(size * 2); |
138 | |
139 | const Vector2 *quadpositions = quad->getVertexPositions(); |
140 | const Vector2 *quadtexcoords = quad->getVertexTexCoords(); |
141 | |
142 | // Always keep the buffer mapped when adding data (it'll be unmapped on draw.) |
143 | size_t offset = (index == -1 ? next : index) * vertex_stride * 4; |
144 | auto verts = (XYf_STPf_RGBAub *) ((uint8 *) array_buf->map() + offset); |
145 | |
146 | m.transformXY(verts, quadpositions, 4); |
147 | |
148 | for (int i = 0; i < 4; i++) |
149 | { |
150 | verts[i].s = quadtexcoords[i].x; |
151 | verts[i].t = quadtexcoords[i].y; |
152 | verts[i].p = (float) layer; |
153 | verts[i].color = color; |
154 | } |
155 | |
156 | array_buf->setMappedRangeModified(offset, vertex_stride * 4); |
157 | |
158 | // Increment counter. |
159 | if (index == -1) |
160 | return next++; |
161 | |
162 | return index; |
163 | } |
164 | |
165 | void SpriteBatch::clear() |
166 | { |
167 | // Reset the position of the next index. |
168 | next = 0; |
169 | } |
170 | |
171 | void SpriteBatch::flush() |
172 | { |
173 | array_buf->unmap(); |
174 | } |
175 | |
176 | void SpriteBatch::setTexture(Texture *newtexture) |
177 | { |
178 | if (texture->getTextureType() != newtexture->getTextureType()) |
179 | throw love::Exception("Texture must have the same texture type as the SpriteBatch's previous texture." ); |
180 | |
181 | texture.set(newtexture); |
182 | } |
183 | |
184 | Texture *SpriteBatch::getTexture() const |
185 | { |
186 | return texture.get(); |
187 | } |
188 | |
189 | void SpriteBatch::setColor(const Colorf &c) |
190 | { |
191 | color_active = true; |
192 | |
193 | Colorf cclamped; |
194 | cclamped.r = std::min(std::max(c.r, 0.0f), 1.0f); |
195 | cclamped.g = std::min(std::max(c.g, 0.0f), 1.0f); |
196 | cclamped.b = std::min(std::max(c.b, 0.0f), 1.0f); |
197 | cclamped.a = std::min(std::max(c.a, 0.0f), 1.0f); |
198 | |
199 | this->color = toColor32(cclamped); |
200 | } |
201 | |
202 | void SpriteBatch::setColor() |
203 | { |
204 | color_active = false; |
205 | color = Color32(255, 255, 255, 255); |
206 | } |
207 | |
208 | Colorf SpriteBatch::getColor(bool &active) const |
209 | { |
210 | active = color_active; |
211 | return toColorf(color); |
212 | } |
213 | |
214 | int SpriteBatch::getCount() const |
215 | { |
216 | return next; |
217 | } |
218 | |
219 | void SpriteBatch::setBufferSize(int newsize) |
220 | { |
221 | if (newsize <= 0) |
222 | throw love::Exception("Invalid SpriteBatch size." ); |
223 | |
224 | if (newsize == size) |
225 | return; |
226 | |
227 | size_t vertex_size = vertex_stride * 4 * newsize; |
228 | love::graphics::Buffer *new_array_buf = nullptr; |
229 | |
230 | int new_next = std::min(next, newsize); |
231 | |
232 | try |
233 | { |
234 | auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS); |
235 | new_array_buf = gfx->newBuffer(vertex_size, nullptr, array_buf->getType(), array_buf->getUsage(), array_buf->getMapFlags()); |
236 | |
237 | // Copy as much of the old data into the new GLBuffer as can fit. |
238 | size_t copy_size = vertex_stride * 4 * new_next; |
239 | array_buf->copyTo(0, copy_size, new_array_buf, 0); |
240 | } |
241 | catch (love::Exception &) |
242 | { |
243 | delete new_array_buf; |
244 | throw; |
245 | } |
246 | |
247 | // We don't need to unmap the old GLBuffer since we're deleting it. |
248 | delete array_buf; |
249 | |
250 | array_buf = new_array_buf; |
251 | size = newsize; |
252 | |
253 | next = new_next; |
254 | } |
255 | |
256 | int SpriteBatch::getBufferSize() const |
257 | { |
258 | return size; |
259 | } |
260 | |
261 | void SpriteBatch::attachAttribute(const std::string &name, Mesh *mesh) |
262 | { |
263 | AttachedAttribute oldattrib = {}; |
264 | AttachedAttribute newattrib = {}; |
265 | |
266 | if (mesh->getVertexCount() < (size_t) next * 4) |
267 | throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)" , next*4); |
268 | |
269 | auto it = attached_attributes.find(name); |
270 | if (it != attached_attributes.end()) |
271 | oldattrib = it->second; |
272 | |
273 | newattrib.index = mesh->getAttributeIndex(name); |
274 | |
275 | if (newattrib.index < 0) |
276 | throw love::Exception("The specified mesh does not have a vertex attribute named '%s'" , name.c_str()); |
277 | |
278 | newattrib.mesh = mesh; |
279 | |
280 | attached_attributes[name] = newattrib; |
281 | } |
282 | |
283 | void SpriteBatch::setDrawRange(int start, int count) |
284 | { |
285 | if (start < 0 || count <= 0) |
286 | throw love::Exception("Invalid draw range." ); |
287 | |
288 | range_start = start; |
289 | range_count = count; |
290 | } |
291 | |
292 | void SpriteBatch::setDrawRange() |
293 | { |
294 | range_start = range_count = -1; |
295 | } |
296 | |
297 | bool SpriteBatch::getDrawRange(int &start, int &count) const |
298 | { |
299 | if (range_start < 0 || range_count <= 0) |
300 | return false; |
301 | |
302 | start = range_start; |
303 | count = range_count; |
304 | return true; |
305 | } |
306 | |
307 | void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m) |
308 | { |
309 | using namespace vertex; |
310 | |
311 | if (next == 0) |
312 | return; |
313 | |
314 | gfx->flushStreamDraws(); |
315 | |
316 | if (texture.get()) |
317 | { |
318 | if (Shader::isDefaultActive()) |
319 | { |
320 | Shader::StandardShader defaultshader = Shader::STANDARD_DEFAULT; |
321 | if (texture->getTextureType() == TEXTURE_2D_ARRAY) |
322 | defaultshader = Shader::STANDARD_ARRAY; |
323 | |
324 | Shader::attachDefault(defaultshader); |
325 | } |
326 | |
327 | if (Shader::current) |
328 | Shader::current->checkMainTexture(texture); |
329 | } |
330 | |
331 | // Make sure the buffer isn't mapped when we draw (sends data to GPU if needed.) |
332 | array_buf->unmap(); |
333 | |
334 | Attributes attributes; |
335 | BufferBindings buffers; |
336 | |
337 | { |
338 | buffers.set(0, array_buf, 0); |
339 | attributes.setCommonFormat(vertex_format, 0); |
340 | |
341 | if (!color_active) |
342 | attributes.disable(ATTRIB_COLOR); |
343 | } |
344 | |
345 | int activebuffers = 1; |
346 | |
347 | for (const auto &it : attached_attributes) |
348 | { |
349 | Mesh *mesh = it.second.mesh.get(); |
350 | |
351 | // We have to do this check here as wll because setBufferSize can be |
352 | // called after attachAttribute. |
353 | if (mesh->getVertexCount() < (size_t) next * 4) |
354 | throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices" , it.first.c_str()); |
355 | |
356 | int attributeindex = -1; |
357 | |
358 | // If the attribute is one of the LOVE-defined ones, use the constant |
359 | // attribute index for it, otherwise query the index from the shader. |
360 | BuiltinVertexAttribute builtinattrib; |
361 | if (vertex::getConstant(it.first.c_str(), builtinattrib)) |
362 | attributeindex = (int) builtinattrib; |
363 | else if (Shader::current) |
364 | attributeindex = Shader::current->getVertexAttributeIndex(it.first); |
365 | |
366 | if (attributeindex >= 0) |
367 | { |
368 | // Make sure the buffer isn't mapped (sends data to GPU if needed.) |
369 | mesh->vertexBuffer->unmap(); |
370 | |
371 | const auto &formats = mesh->getVertexFormat(); |
372 | const auto &format = formats[it.second.index]; |
373 | |
374 | uint16 offset = (uint16) mesh->getAttributeOffset(it.second.index); |
375 | uint16 stride = (uint16) mesh->getVertexStride(); |
376 | |
377 | attributes.set(attributeindex, format.type, (uint8) format.components, offset, activebuffers); |
378 | attributes.setBufferLayout(activebuffers, stride); |
379 | |
380 | // TODO: We should reuse buffer bindings with the same buffer+stride+step. |
381 | buffers.set(activebuffers, mesh->vertexBuffer, 0); |
382 | activebuffers++; |
383 | } |
384 | } |
385 | |
386 | Graphics::TempTransform transform(gfx, m); |
387 | |
388 | int start = std::min(std::max(0, range_start), next - 1); |
389 | |
390 | int count = next; |
391 | if (range_count > 0) |
392 | count = std::min(count, range_count); |
393 | |
394 | count = std::min(count, next - start); |
395 | |
396 | if (count > 0) |
397 | gfx->drawQuads(start, count, attributes, buffers, texture); |
398 | } |
399 | |
400 | } // graphics |
401 | } // love |
402 | |