Browse Source

Add love.graphics.getQuadIndexBuffer.

love's pre-generated quad index buffer is useful when combined with drawShaderVertices. Note that it uses 16 bit indices so it can only draw up to 16k quads in a single call.
Alex Szpakowski 3 years ago
parent
commit
ecbc5ca7f6

+ 1 - 0
src/modules/graphics/Buffer.cpp

@@ -36,6 +36,7 @@ Buffer::Buffer(Graphics *gfx, const Settings &settings, const std::vector<DataDe
 	, usageFlags(settings.usageFlags)
 	, usageFlags(settings.usageFlags)
 	, dataUsage(settings.dataUsage)
 	, dataUsage(settings.dataUsage)
 	, mapped(false)
 	, mapped(false)
+	, immutable(false)
 {
 {
 	if (size == 0 && arraylength == 0)
 	if (size == 0 && arraylength == 0)
 		throw love::Exception("Size or array length must be specified.");
 		throw love::Exception("Size or array length must be specified.");

+ 5 - 0
src/modules/graphics/Buffer.h

@@ -111,6 +111,9 @@ public:
 	size_t getMemberOffset(int index) const { return dataMembers[index].offset; }
 	size_t getMemberOffset(int index) const { return dataMembers[index].offset; }
 	int getDataMemberIndex(const std::string &name) const;
 	int getDataMemberIndex(const std::string &name) const;
 
 
+	void setImmutable(bool immutable) { this->immutable = immutable; };
+	bool isImmutable() const { return immutable; }
+
 	/**
 	/**
 	 * Map a portion of the Buffer to client memory.
 	 * Map a portion of the Buffer to client memory.
 	 */
 	 */
@@ -176,6 +179,8 @@ protected:
 	BufferDataUsage dataUsage;
 	BufferDataUsage dataUsage;
 
 
 	bool mapped;
 	bool mapped;
+
+	bool immutable;
 	
 	
 }; // Buffer
 }; // Buffer
 
 

+ 8 - 0
src/modules/graphics/Graphics.cpp

@@ -182,6 +182,8 @@ void Graphics::createQuadIndexBuffer()
 
 
 	Buffer::Mapper map(*quadIndexBuffer);
 	Buffer::Mapper map(*quadIndexBuffer);
 	fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.data);
 	fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.data);
+
+	quadIndexBuffer->setImmutable(true);
 }
 }
 
 
 Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
 Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
@@ -1073,6 +1075,9 @@ void Graphics::copyBuffer(Buffer *source, Buffer *dest, size_t sourceoffset, siz
 	if (source == dest && sourcerange.intersects(destrange))
 	if (source == dest && sourcerange.intersects(destrange))
 		throw love::Exception("Copying a portion of a buffer to the same buffer requires non-overlapping source and destination offsets.");
 		throw love::Exception("Copying a portion of a buffer to the same buffer requires non-overlapping source and destination offsets.");
 
 
+	if (dest->isImmutable())
+		throw love::Exception("Cannot copy to an immutable buffer.");
+
 	source->copyTo(dest, sourceoffset, destoffset, size);
 	source->copyTo(dest, sourceoffset, destoffset, size);
 }
 }
 
 
@@ -1098,6 +1103,9 @@ void Graphics::copyTextureToBuffer(Texture *source, Buffer *dest, int slice, int
 	if (dest->getDataUsage() == BUFFERDATAUSAGE_STREAM)
 	if (dest->getDataUsage() == BUFFERDATAUSAGE_STREAM)
 		throw love::Exception("Buffers created with 'stream' data usage cannot be used as a copy destination.");
 		throw love::Exception("Buffers created with 'stream' data usage cannot be used as a copy destination.");
 
 
+	if (dest->isImmutable())
+		throw love::Exception("Cannot copy to an immutable buffer.");
+
 	if (isRenderTargetActive(source))
 	if (isRenderTargetActive(source))
 		throw love::Exception("copyTextureToBuffer cannot be called while the Texture is an active render target.");
 		throw love::Exception("copyTextureToBuffer cannot be called while the Texture is an active render target.");
 
 

+ 2 - 0
src/modules/graphics/Graphics.h

@@ -519,6 +519,8 @@ public:
 	virtual int getRequestedBackbufferMSAA() const = 0;
 	virtual int getRequestedBackbufferMSAA() const = 0;
 	virtual int getBackbufferMSAA() const = 0;
 	virtual int getBackbufferMSAA() const = 0;
 
 
+	Buffer *getQuadIndexBuffer() const { return quadIndexBuffer; }
+
 	/**
 	/**
 	 * Sets the current constant color.
 	 * Sets the current constant color.
 	 **/
 	 **/

+ 2 - 2
src/modules/graphics/opengl/Buffer.cpp

@@ -166,7 +166,7 @@ bool Buffer::supportsOrphan() const
 
 
 void *Buffer::map(MapType /*map*/, size_t offset, size_t size)
 void *Buffer::map(MapType /*map*/, size_t offset, size_t size)
 {
 {
-	if (size == 0)
+	if (size == 0 || isImmutable())
 		return nullptr;
 		return nullptr;
 
 
 	Range r(offset, size);
 	Range r(offset, size);
@@ -229,7 +229,7 @@ void Buffer::unmap(size_t usedoffset, size_t usedsize)
 
 
 void Buffer::fill(size_t offset, size_t size, const void *data)
 void Buffer::fill(size_t offset, size_t size, const void *data)
 {
 {
-	if (size == 0)
+	if (size == 0 || isImmutable())
 		return;
 		return;
 
 
 	size_t buffersize = getSize();
 	size_t buffersize = getSize();

+ 7 - 0
src/modules/graphics/wrap_Graphics.cpp

@@ -242,6 +242,12 @@ int w_getDPIScale(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_getQuadIndexBuffer(lua_State *L)
+{
+	luax_pushtype(L, instance()->getQuadIndexBuffer());
+	return 1;
+}
+
 static Graphics::RenderTarget checkRenderTarget(lua_State *L, int idx)
 static Graphics::RenderTarget checkRenderTarget(lua_State *L, int idx)
 {
 {
 	lua_rawgeti(L, idx, 1);
 	lua_rawgeti(L, idx, 1);
@@ -3671,6 +3677,7 @@ static const luaL_Reg functions[] =
 	{ "getPixelHeight", w_getPixelHeight },
 	{ "getPixelHeight", w_getPixelHeight },
 	{ "getPixelDimensions", w_getPixelDimensions },
 	{ "getPixelDimensions", w_getPixelDimensions },
 	{ "getDPIScale", w_getDPIScale },
 	{ "getDPIScale", w_getDPIScale },
+	{ "getQuadIndexBuffer", w_getQuadIndexBuffer },
 
 
 	{ "setScissor", w_setScissor },
 	{ "setScissor", w_setScissor },
 	{ "intersectScissor", w_intersectScissor },
 	{ "intersectScissor", w_intersectScissor },