Browse Source

Add Mesh:set/getIndexBuffer.

Alex Szpakowski 5 years ago
parent
commit
e60736f5a6

+ 3 - 3
src/common/StringMap.h

@@ -182,15 +182,15 @@ private:
 
 
 }; // StringMap
 }; // StringMap
 
 
-#define DECLARE_STRINGMAP(type) \
+#define STRINGMAP_DECLARE(type) \
 bool getConstant(const char *in, type &out); \
 bool getConstant(const char *in, type &out); \
 bool getConstant(type in, const char *&out); \
 bool getConstant(type in, const char *&out); \
 std::vector<std::string> getConstants(type); \
 std::vector<std::string> getConstants(type); \
 
 
-#define DEFINE_STRINGMAP_BEGIN(type, count, name) \
+#define STRINGMAP_BEGIN(type, count, name) \
 static StringMap<type, count>::Entry name##Entries[] =
 static StringMap<type, count>::Entry name##Entries[] =
 
 
-#define DEFINE_STRINGMAP_END(type, count, name) \
+#define STRINGMAP_END(type, count, name) \
 ; \
 ; \
 static StringMap<type, count> name##s(name##Entries, sizeof(name##Entries)); \
 static StringMap<type, count> name##s(name##Entries, sizeof(name##Entries)); \
 bool getConstant(const char *in, type &out) { return name##s.find(in, out); } \
 bool getConstant(const char *in, type &out) { return name##s.find(in, out); } \

+ 24 - 1
src/modules/graphics/Mesh.cpp

@@ -472,6 +472,9 @@ bool Mesh::getVertexMap(std::vector<uint32> &map) const
 	if (!indexBuffer || indexCount == 0)
 	if (!indexBuffer || indexCount == 0)
 		return true;
 		return true;
 
 
+	if ((indexBuffer->getMapFlags() & Buffer::MAP_READ) == 0)
+		return false;
+
 	// We unmap the buffer in Mesh::draw, Mesh::setVertexMap, and Mesh::flush.
 	// We unmap the buffer in Mesh::draw, Mesh::setVertexMap, and Mesh::flush.
 	void *buffer = indexBuffer->map();
 	void *buffer = indexBuffer->map();
 
 
@@ -490,7 +493,27 @@ bool Mesh::getVertexMap(std::vector<uint32> &map) const
 	return true;
 	return true;
 }
 }
 
 
-size_t Mesh::getVertexMapCount() const
+void Mesh::setIndexBuffer(Buffer *buffer)
+{
+	// Buffer constructor does the rest of the validation for index buffers
+	// (data member formats, etc.)
+	if (buffer != nullptr && (buffer->getTypeFlags() & Buffer::TYPEFLAG_INDEX) == 0)
+		throw love::Exception("setIndexBuffer requires a Buffer created as an index buffer.");
+
+	indexBuffer.set(buffer);
+	useIndexBuffer = buffer != nullptr;
+	indexCount = buffer != nullptr ? buffer->getArrayLength() : 0;
+
+	if (buffer != nullptr)
+		indexDataType = getIndexDataType(buffer->getDataMember(0).decl.format);
+}
+
+Buffer *Mesh::getIndexBuffer() const
+{
+	return indexBuffer;
+}
+
+size_t Mesh::getIndexCount() const
 {
 {
 	return indexCount;
 	return indexCount;
 }
 }

+ 7 - 1
src/modules/graphics/Mesh.h

@@ -95,6 +95,9 @@ public:
 	 **/
 	 **/
 	size_t getVertexStride() const;
 	size_t getVertexStride() const;
 
 
+	/**
+	 * Gets the Buffer that holds the Mesh's vertices.
+	 **/
 	Buffer *getVertexBuffer() const;
 	Buffer *getVertexBuffer() const;
 
 
 	/**
 	/**
@@ -140,10 +143,13 @@ public:
 	 **/
 	 **/
 	bool getVertexMap(std::vector<uint32> &map) const;
 	bool getVertexMap(std::vector<uint32> &map) const;
 
 
+	void setIndexBuffer(Buffer *buffer);
+	Buffer *getIndexBuffer() const;
+
 	/**
 	/**
 	 * Gets the total number of elements in the vertex map array.
 	 * Gets the total number of elements in the vertex map array.
 	 **/
 	 **/
-	size_t getVertexMapCount() const;
+	size_t getIndexCount() const;
 
 
 	/**
 	/**
 	 * Sets the texture used when drawing the Mesh.
 	 * Sets the texture used when drawing the Mesh.

+ 28 - 18
src/modules/graphics/vertex.cpp

@@ -183,6 +183,16 @@ DataFormat getIndexDataFormat(IndexDataType type)
 	return type == INDEX_UINT32 ? DATAFORMAT_UINT32 : DATAFORMAT_UINT16;
 	return type == INDEX_UINT32 ? DATAFORMAT_UINT32 : DATAFORMAT_UINT16;
 }
 }
 
 
+IndexDataType getIndexDataType(DataFormat format)
+{
+	switch (format)
+	{
+		case DATAFORMAT_UINT16: return INDEX_UINT16;
+		case DATAFORMAT_UINT32: return INDEX_UINT32;
+		default: return INDEX_MAX_ENUM;
+	}
+}
+
 int getIndexCount(TriangleIndexMode mode, int vertexCount)
 int getIndexCount(TriangleIndexMode mode, int vertexCount)
 {
 {
 	switch (mode)
 	switch (mode)
@@ -312,13 +322,13 @@ void VertexAttributes::setCommonFormat(CommonFormat format, uint8 bufferindex)
 	}
 	}
 }
 }
 
 
-DEFINE_STRINGMAP_BEGIN(BuiltinVertexAttribute, ATTRIB_MAX_ENUM, attribName)
+STRINGMAP_BEGIN(BuiltinVertexAttribute, ATTRIB_MAX_ENUM, attribName)
 {
 {
 	{ "VertexPosition", ATTRIB_POS           },
 	{ "VertexPosition", ATTRIB_POS           },
 	{ "VertexTexCoord", ATTRIB_TEXCOORD      },
 	{ "VertexTexCoord", ATTRIB_TEXCOORD      },
 	{ "VertexColor",    ATTRIB_COLOR         },
 	{ "VertexColor",    ATTRIB_COLOR         },
 }
 }
-DEFINE_STRINGMAP_END(BuiltinVertexAttribute, ATTRIB_MAX_ENUM, attribName)
+STRINGMAP_END(BuiltinVertexAttribute, ATTRIB_MAX_ENUM, attribName)
 
 
 const char *getConstant(BuiltinVertexAttribute attrib)
 const char *getConstant(BuiltinVertexAttribute attrib)
 {
 {
@@ -327,38 +337,38 @@ const char *getConstant(BuiltinVertexAttribute attrib)
 	return name;
 	return name;
 }
 }
 
 
-DEFINE_STRINGMAP_BEGIN(IndexDataType, INDEX_MAX_ENUM, indexType)
+STRINGMAP_BEGIN(IndexDataType, INDEX_MAX_ENUM, indexType)
 {
 {
 	{ "uint16", INDEX_UINT16 },
 	{ "uint16", INDEX_UINT16 },
 	{ "uint32", INDEX_UINT32 },
 	{ "uint32", INDEX_UINT32 },
 }
 }
-DEFINE_STRINGMAP_END(IndexDataType, INDEX_MAX_ENUM, indexType)
+STRINGMAP_END(IndexDataType, INDEX_MAX_ENUM, indexType)
 
 
-DEFINE_STRINGMAP_BEGIN(BufferUsage, BUFFERUSAGE_MAX_ENUM, bufferUsage)
+STRINGMAP_BEGIN(BufferUsage, BUFFERUSAGE_MAX_ENUM, bufferUsage)
 {
 {
 	{ "stream",  BUFFERUSAGE_STREAM  },
 	{ "stream",  BUFFERUSAGE_STREAM  },
 	{ "dynamic", BUFFERUSAGE_DYNAMIC },
 	{ "dynamic", BUFFERUSAGE_DYNAMIC },
 	{ "static",  BUFFERUSAGE_STATIC  },
 	{ "static",  BUFFERUSAGE_STATIC  },
 }
 }
-DEFINE_STRINGMAP_END(BufferUsage, BUFFERUSAGE_MAX_ENUM, bufferUsage)
+STRINGMAP_END(BufferUsage, BUFFERUSAGE_MAX_ENUM, bufferUsage)
 
 
-DEFINE_STRINGMAP_BEGIN(PrimitiveType, PRIMITIVE_MAX_ENUM, primitiveType)
+STRINGMAP_BEGIN(PrimitiveType, PRIMITIVE_MAX_ENUM, primitiveType)
 {
 {
 	{ "fan",       PRIMITIVE_TRIANGLE_FAN   },
 	{ "fan",       PRIMITIVE_TRIANGLE_FAN   },
 	{ "strip",     PRIMITIVE_TRIANGLE_STRIP },
 	{ "strip",     PRIMITIVE_TRIANGLE_STRIP },
 	{ "triangles", PRIMITIVE_TRIANGLES      },
 	{ "triangles", PRIMITIVE_TRIANGLES      },
 	{ "points",    PRIMITIVE_POINTS         },
 	{ "points",    PRIMITIVE_POINTS         },
 }
 }
-DEFINE_STRINGMAP_END(PrimitiveType, PRIMITIVE_MAX_ENUM, primitiveType)
+STRINGMAP_END(PrimitiveType, PRIMITIVE_MAX_ENUM, primitiveType)
 
 
-DEFINE_STRINGMAP_BEGIN(AttributeStep, STEP_MAX_ENUM, attributeStep)
+STRINGMAP_BEGIN(AttributeStep, STEP_MAX_ENUM, attributeStep)
 {
 {
 	{ "pervertex",   STEP_PER_VERTEX   },
 	{ "pervertex",   STEP_PER_VERTEX   },
 	{ "perinstance", STEP_PER_INSTANCE },
 	{ "perinstance", STEP_PER_INSTANCE },
 }
 }
-DEFINE_STRINGMAP_END(AttributeStep, STEP_MAX_ENUM, attributeStep)
+STRINGMAP_END(AttributeStep, STEP_MAX_ENUM, attributeStep)
 
 
-DEFINE_STRINGMAP_BEGIN(DataFormat, DATAFORMAT_MAX_ENUM, dataFormat)
+STRINGMAP_BEGIN(DataFormat, DATAFORMAT_MAX_ENUM, dataFormat)
 {
 {
 	{ "float",     DATAFORMAT_FLOAT      },
 	{ "float",     DATAFORMAT_FLOAT      },
 	{ "floatvec2", DATAFORMAT_FLOAT_VEC2 },
 	{ "floatvec2", DATAFORMAT_FLOAT_VEC2 },
@@ -410,9 +420,9 @@ DEFINE_STRINGMAP_BEGIN(DataFormat, DATAFORMAT_MAX_ENUM, dataFormat)
 	{ "boolvec3", DATAFORMAT_BOOL_VEC3 },
 	{ "boolvec3", DATAFORMAT_BOOL_VEC3 },
 	{ "boolvec4", DATAFORMAT_BOOL_VEC4 },
 	{ "boolvec4", DATAFORMAT_BOOL_VEC4 },
 }
 }
-DEFINE_STRINGMAP_END(DataFormat, DATAFORMAT_MAX_ENUM, dataFormat)
+STRINGMAP_END(DataFormat, DATAFORMAT_MAX_ENUM, dataFormat)
 
 
-DEFINE_STRINGMAP_BEGIN(DataBaseType, DATA_BASETYPE_MAX_ENUM, dataBaseType)
+STRINGMAP_BEGIN(DataBaseType, DATA_BASETYPE_MAX_ENUM, dataBaseType)
 {
 {
 	{ "float", DATA_BASETYPE_FLOAT },
 	{ "float", DATA_BASETYPE_FLOAT },
 	{ "int",   DATA_BASETYPE_INT   },
 	{ "int",   DATA_BASETYPE_INT   },
@@ -421,22 +431,22 @@ DEFINE_STRINGMAP_BEGIN(DataBaseType, DATA_BASETYPE_MAX_ENUM, dataBaseType)
 	{ "unorm", DATA_BASETYPE_UNORM },
 	{ "unorm", DATA_BASETYPE_UNORM },
 	{ "bool",  DATA_BASETYPE_BOOL  },
 	{ "bool",  DATA_BASETYPE_BOOL  },
 }
 }
-DEFINE_STRINGMAP_END(DataBaseType, DATA_BASETYPE_MAX_ENUM, dataBaseType)
+STRINGMAP_END(DataBaseType, DATA_BASETYPE_MAX_ENUM, dataBaseType)
 
 
-DEFINE_STRINGMAP_BEGIN(CullMode, CULL_MAX_ENUM, cullMode)
+STRINGMAP_BEGIN(CullMode, CULL_MAX_ENUM, cullMode)
 {
 {
 	{ "none",  CULL_NONE  },
 	{ "none",  CULL_NONE  },
 	{ "back",  CULL_BACK  },
 	{ "back",  CULL_BACK  },
 	{ "front", CULL_FRONT },
 	{ "front", CULL_FRONT },
 }
 }
-DEFINE_STRINGMAP_END(CullMode, CULL_MAX_ENUM, cullMode)
+STRINGMAP_END(CullMode, CULL_MAX_ENUM, cullMode)
 
 
-DEFINE_STRINGMAP_BEGIN(Winding, WINDING_MAX_ENUM, winding)
+STRINGMAP_BEGIN(Winding, WINDING_MAX_ENUM, winding)
 {
 {
 	{ "cw",  WINDING_CW  },
 	{ "cw",  WINDING_CW  },
 	{ "ccw", WINDING_CCW },
 	{ "ccw", WINDING_CCW },
 }
 }
-DEFINE_STRINGMAP_END(Winding, WINDING_MAX_ENUM, winding)
+STRINGMAP_END(Winding, WINDING_MAX_ENUM, winding)
 
 
 } // graphics
 } // graphics
 } // love
 } // love

+ 11 - 9
src/modules/graphics/vertex.h

@@ -289,6 +289,7 @@ struct VertexAttributeInfo
 
 
 struct VertexBufferLayout
 struct VertexBufferLayout
 {
 {
+	// Attribute step rate is stored outside this struct as a bitmask.
 	uint16 stride;
 	uint16 stride;
 };
 };
 
 
@@ -368,21 +369,22 @@ const DataFormatInfo &getDataFormatInfo(DataFormat format);
 size_t getIndexDataSize(IndexDataType type);
 size_t getIndexDataSize(IndexDataType type);
 IndexDataType getIndexDataTypeFromMax(size_t maxvalue);
 IndexDataType getIndexDataTypeFromMax(size_t maxvalue);
 DataFormat getIndexDataFormat(IndexDataType type);
 DataFormat getIndexDataFormat(IndexDataType type);
+IndexDataType getIndexDataType(DataFormat format);
 
 
 int getIndexCount(TriangleIndexMode mode, int vertexCount);
 int getIndexCount(TriangleIndexMode mode, int vertexCount);
 
 
 void fillIndices(TriangleIndexMode mode, uint16 vertexStart, uint16 vertexCount, uint16 *indices);
 void fillIndices(TriangleIndexMode mode, uint16 vertexStart, uint16 vertexCount, uint16 *indices);
 void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount, uint32 *indices);
 void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount, uint32 *indices);
 
 
-DECLARE_STRINGMAP(BuiltinVertexAttribute);
-DECLARE_STRINGMAP(IndexDataType);
-DECLARE_STRINGMAP(BufferUsage);
-DECLARE_STRINGMAP(PrimitiveType);
-DECLARE_STRINGMAP(AttributeStep);
-DECLARE_STRINGMAP(DataFormat);
-DECLARE_STRINGMAP(DataBaseType);
-DECLARE_STRINGMAP(CullMode);
-DECLARE_STRINGMAP(Winding);
+STRINGMAP_DECLARE(BuiltinVertexAttribute);
+STRINGMAP_DECLARE(IndexDataType);
+STRINGMAP_DECLARE(BufferUsage);
+STRINGMAP_DECLARE(PrimitiveType);
+STRINGMAP_DECLARE(AttributeStep);
+STRINGMAP_DECLARE(DataFormat);
+STRINGMAP_DECLARE(DataBaseType);
+STRINGMAP_DECLARE(CullMode);
+STRINGMAP_DECLARE(Winding);
 
 
 const char *getConstant(BuiltinVertexAttribute attrib);
 const char *getConstant(BuiltinVertexAttribute attrib);
 
 

+ 19 - 0
src/modules/graphics/wrap_Mesh.cpp

@@ -448,6 +448,23 @@ int w_Mesh_getVertexMap(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_Mesh_setIndexBuffer(lua_State *L)
+{
+	Mesh *t = luax_checkmesh(L, 1);
+	Buffer *b = nullptr;
+	if (!lua_isnoneornil(L, 2))
+		b = luax_checkbuffer(L, 2);
+	luax_catchexcept(L, [&]() { t->setIndexBuffer(b); });
+	return 0;
+}
+
+int w_Mesh_getIndexBuffer(lua_State *L)
+{
+	Mesh *t = luax_checkmesh(L, 1);
+	luax_pushtype(L, t->getIndexBuffer());
+	return 1;
+}
+
 int w_Mesh_setTexture(lua_State *L)
 int w_Mesh_setTexture(lua_State *L)
 {
 {
 	Mesh *t = luax_checkmesh(L, 1);
 	Mesh *t = luax_checkmesh(L, 1);
@@ -548,6 +565,8 @@ static const luaL_Reg w_Mesh_functions[] =
 	{ "flush", w_Mesh_flush },
 	{ "flush", w_Mesh_flush },
 	{ "setVertexMap", w_Mesh_setVertexMap },
 	{ "setVertexMap", w_Mesh_setVertexMap },
 	{ "getVertexMap", w_Mesh_getVertexMap },
 	{ "getVertexMap", w_Mesh_getVertexMap },
+	{ "setIndexBuffer", w_Mesh_setIndexBuffer },
+	{ "getIndexBuffer", w_Mesh_getIndexBuffer },
 	{ "setTexture", w_Mesh_setTexture },
 	{ "setTexture", w_Mesh_setTexture },
 	{ "getTexture", w_Mesh_getTexture },
 	{ "getTexture", w_Mesh_getTexture },
 	{ "setDrawMode", w_Mesh_setDrawMode },
 	{ "setDrawMode", w_Mesh_setDrawMode },