vertex.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * Copyright (c) 2006-2020 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. #pragma once
  21. // LOVE
  22. #include "common/int.h"
  23. #include "common/Color.h"
  24. #include "common/StringMap.h"
  25. // C
  26. #include <stddef.h>
  27. #include <vector>
  28. #include <string>
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. class Resource;
  34. // Vertex attribute indices used in shaders by LOVE. The values map to GPU
  35. // generic vertex attribute indices.
  36. enum BuiltinVertexAttribute
  37. {
  38. ATTRIB_POS = 0,
  39. ATTRIB_TEXCOORD,
  40. ATTRIB_COLOR,
  41. ATTRIB_MAX_ENUM
  42. };
  43. enum BuiltinVertexAttributeFlags
  44. {
  45. ATTRIBFLAG_POS = 1 << ATTRIB_POS,
  46. ATTRIBFLAG_TEXCOORD = 1 << ATTRIB_TEXCOORD,
  47. ATTRIBFLAG_COLOR = 1 << ATTRIB_COLOR,
  48. };
  49. enum BufferType
  50. {
  51. BUFFERTYPE_VERTEX = 0,
  52. BUFFERTYPE_INDEX,
  53. BUFFERTYPE_MAX_ENUM
  54. };
  55. enum IndexDataType
  56. {
  57. INDEX_UINT16,
  58. INDEX_UINT32,
  59. INDEX_MAX_ENUM
  60. };
  61. // http://escience.anu.edu.au/lecture/cg/surfaceModeling/image/surfaceModeling015.png
  62. enum PrimitiveType
  63. {
  64. PRIMITIVE_TRIANGLES,
  65. PRIMITIVE_TRIANGLE_STRIP,
  66. PRIMITIVE_TRIANGLE_FAN,
  67. PRIMITIVE_POINTS,
  68. PRIMITIVE_MAX_ENUM
  69. };
  70. enum AttributeStep
  71. {
  72. STEP_PER_VERTEX,
  73. STEP_PER_INSTANCE,
  74. STEP_MAX_ENUM
  75. };
  76. enum CullMode
  77. {
  78. CULL_NONE,
  79. CULL_BACK,
  80. CULL_FRONT,
  81. CULL_MAX_ENUM
  82. };
  83. // The expected usage pattern of buffer data.
  84. enum BufferUsage
  85. {
  86. BUFFERUSAGE_STREAM,
  87. BUFFERUSAGE_DYNAMIC,
  88. BUFFERUSAGE_STATIC,
  89. BUFFERUSAGE_MAX_ENUM
  90. };
  91. enum DataTypeDeprecated
  92. {
  93. DATADEPRECATED_UNORM8,
  94. DATADEPRECATED_UNORM16,
  95. DATADEPRECATED_FLOAT,
  96. DATADEPRECATED_MAX_ENUM
  97. };
  98. // Value types used when interfacing with the GPU (vertex and shader data).
  99. // The order of this enum affects the dataFormatInfo array.
  100. enum DataFormat
  101. {
  102. DATAFORMAT_FLOAT,
  103. DATAFORMAT_FLOAT_VEC2,
  104. DATAFORMAT_FLOAT_VEC3,
  105. DATAFORMAT_FLOAT_VEC4,
  106. DATAFORMAT_FLOAT_MAT2X2,
  107. DATAFORMAT_FLOAT_MAT2X3,
  108. DATAFORMAT_FLOAT_MAT2X4,
  109. DATAFORMAT_FLOAT_MAT3X2,
  110. DATAFORMAT_FLOAT_MAT3X3,
  111. DATAFORMAT_FLOAT_MAT3X4,
  112. DATAFORMAT_FLOAT_MAT4X2,
  113. DATAFORMAT_FLOAT_MAT4X3,
  114. DATAFORMAT_FLOAT_MAT4X4,
  115. DATAFORMAT_INT32,
  116. DATAFORMAT_INT32_VEC2,
  117. DATAFORMAT_INT32_VEC3,
  118. DATAFORMAT_INT32_VEC4,
  119. DATAFORMAT_UINT32,
  120. DATAFORMAT_UINT32_VEC2,
  121. DATAFORMAT_UINT32_VEC3,
  122. DATAFORMAT_UINT32_VEC4,
  123. DATAFORMAT_SNORM8_VEC4,
  124. DATAFORMAT_UNORM8_VEC4,
  125. DATAFORMAT_INT8_VEC4,
  126. DATAFORMAT_UINT8_VEC4,
  127. DATAFORMAT_SNORM16_VEC2,
  128. DATAFORMAT_SNORM16_VEC4,
  129. DATAFORMAT_UNORM16_VEC2,
  130. DATAFORMAT_UNORM16_VEC4,
  131. DATAFORMAT_INT16_VEC2,
  132. DATAFORMAT_INT16_VEC4,
  133. DATAFORMAT_UINT16,
  134. DATAFORMAT_UINT16_VEC2,
  135. DATAFORMAT_UINT16_VEC4,
  136. DATAFORMAT_BOOL,
  137. DATAFORMAT_BOOL_VEC2,
  138. DATAFORMAT_BOOL_VEC3,
  139. DATAFORMAT_BOOL_VEC4,
  140. DATAFORMAT_MAX_ENUM
  141. };
  142. enum DataBaseType
  143. {
  144. DATA_BASETYPE_FLOAT,
  145. DATA_BASETYPE_INT,
  146. DATA_BASETYPE_UINT,
  147. DATA_BASETYPE_SNORM,
  148. DATA_BASETYPE_UNORM,
  149. DATA_BASETYPE_BOOL,
  150. DATA_BASETYPE_MAX_ENUM
  151. };
  152. enum Winding
  153. {
  154. WINDING_CW,
  155. WINDING_CCW,
  156. WINDING_MAX_ENUM
  157. };
  158. enum TriangleIndexMode
  159. {
  160. TRIANGLEINDEX_NONE,
  161. TRIANGLEINDEX_STRIP,
  162. TRIANGLEINDEX_FAN,
  163. TRIANGLEINDEX_QUADS,
  164. };
  165. enum class CommonFormat
  166. {
  167. NONE,
  168. XYf,
  169. XYZf,
  170. RGBAub,
  171. STf_RGBAub,
  172. STPf_RGBAub,
  173. XYf_STf,
  174. XYf_STPf,
  175. XYf_STf_RGBAub,
  176. XYf_STus_RGBAub,
  177. XYf_STPf_RGBAub,
  178. };
  179. struct DataFormatInfo
  180. {
  181. DataBaseType baseType;
  182. bool isMatrix;
  183. int components;
  184. int matrixRows;
  185. int matrixColumns;
  186. size_t componentSize;
  187. size_t size;
  188. };
  189. struct STf_RGBAub
  190. {
  191. float s, t;
  192. Color32 color;
  193. };
  194. struct STPf_RGBAub
  195. {
  196. float s, t, p;
  197. Color32 color;
  198. };
  199. struct XYf_STf
  200. {
  201. float x, y;
  202. float s, t;
  203. };
  204. struct XYf_STPf
  205. {
  206. float x, y;
  207. float s, t, p;
  208. };
  209. struct XYf_STf_RGBAub
  210. {
  211. float x, y;
  212. float s, t;
  213. Color32 color;
  214. };
  215. typedef XYf_STf_RGBAub Vertex;
  216. struct XYf_STus_RGBAub
  217. {
  218. float x, y;
  219. uint16 s, t;
  220. Color32 color;
  221. };
  222. struct XYf_STPf_RGBAub
  223. {
  224. float x, y;
  225. float s, t, p;
  226. Color32 color;
  227. };
  228. struct BufferBindings
  229. {
  230. static const uint32 MAX = 32;
  231. uint32 useBits = 0;
  232. struct
  233. {
  234. Resource *buffer;
  235. size_t offset;
  236. } info[MAX];
  237. void set(uint32 index, Resource *r, size_t offset)
  238. {
  239. useBits |= (1u << index);
  240. info[index] = {r, offset};
  241. }
  242. void disable(uint32 index) { useBits &= (1u << index); }
  243. void clear() { useBits = 0; }
  244. };
  245. struct VertexAttributeInfo
  246. {
  247. uint8 bufferIndex;
  248. DataFormat format : 8;
  249. uint16 offsetFromVertex;
  250. };
  251. struct VertexBufferLayout
  252. {
  253. uint16 stride;
  254. };
  255. struct VertexAttributes
  256. {
  257. static const uint32 MAX = 32;
  258. uint32 enableBits = 0; // indexed by attribute
  259. uint32 instanceBits = 0; // indexed by buffer
  260. VertexAttributeInfo attribs[MAX];
  261. VertexBufferLayout bufferLayouts[BufferBindings::MAX];
  262. VertexAttributes() {}
  263. VertexAttributes(CommonFormat format, uint8 bufferindex)
  264. {
  265. setCommonFormat(format, bufferindex);
  266. }
  267. void set(uint32 index, DataFormat format, uint16 offsetfromvertex, uint8 bufferindex)
  268. {
  269. enableBits |= (1u << index);
  270. attribs[index].bufferIndex = bufferindex;
  271. attribs[index].format = format;
  272. attribs[index].offsetFromVertex = offsetfromvertex;
  273. }
  274. void setBufferLayout(uint32 bufferindex, uint16 stride, AttributeStep step = STEP_PER_VERTEX)
  275. {
  276. uint32 bufferbit = (1u << bufferindex);
  277. if (step == STEP_PER_INSTANCE)
  278. instanceBits |= bufferbit;
  279. else
  280. instanceBits &= ~bufferbit;
  281. bufferLayouts[bufferindex].stride = stride;
  282. }
  283. void disable(uint32 index)
  284. {
  285. enableBits &= ~(1u << index);
  286. }
  287. void clear()
  288. {
  289. enableBits = 0;
  290. }
  291. bool isEnabled(uint32 index) const
  292. {
  293. return (enableBits & (1u << index)) != 0;
  294. }
  295. AttributeStep getBufferStep(uint32 index) const
  296. {
  297. return (instanceBits & (1u << index)) != 0 ? STEP_PER_INSTANCE : STEP_PER_VERTEX;
  298. }
  299. void setCommonFormat(CommonFormat format, uint8 bufferindex);
  300. };
  301. size_t getFormatStride(CommonFormat format);
  302. uint32 getFormatFlags(CommonFormat format);
  303. int getFormatPositionComponents(CommonFormat format);
  304. inline CommonFormat getSinglePositionFormat(bool is2D)
  305. {
  306. return is2D ? CommonFormat::XYf : CommonFormat::XYZf;
  307. }
  308. const DataFormatInfo &getDataFormatInfo(DataFormat format);
  309. size_t getIndexDataSize(IndexDataType type);
  310. IndexDataType getIndexDataTypeFromMax(size_t maxvalue);
  311. DataFormat getIndexDataFormat(IndexDataType type);
  312. int getIndexCount(TriangleIndexMode mode, int vertexCount);
  313. void fillIndices(TriangleIndexMode mode, uint16 vertexStart, uint16 vertexCount, uint16 *indices);
  314. void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount, uint32 *indices);
  315. DECLARE_STRINGMAP(BuiltinVertexAttribute);
  316. DECLARE_STRINGMAP(IndexDataType);
  317. DECLARE_STRINGMAP(BufferUsage);
  318. DECLARE_STRINGMAP(PrimitiveType);
  319. DECLARE_STRINGMAP(AttributeStep);
  320. DECLARE_STRINGMAP(DataTypeDeprecated);
  321. DECLARE_STRINGMAP(DataFormat);
  322. DECLARE_STRINGMAP(DataBaseType);
  323. DECLARE_STRINGMAP(CullMode);
  324. DECLARE_STRINGMAP(Winding);
  325. const char *getConstant(BuiltinVertexAttribute attrib);
  326. } // graphics
  327. } // love