Buffer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "Buffer.h"
  21. #include "Graphics.h"
  22. #include "common/memory.h"
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. love::Type Buffer::type("GraphicsBuffer", &Object::type);
  28. Buffer::Buffer(Graphics *gfx, const Settings &settings, const std::vector<DataDeclaration> &bufferformat, size_t size, size_t arraylength)
  29. : arrayLength(0)
  30. , arrayStride(0)
  31. , size(size)
  32. , usageFlags(settings.usageFlags)
  33. , dataUsage(settings.dataUsage)
  34. , debugName(settings.debugName)
  35. , mapped(false)
  36. , mappedType(MAP_WRITE_INVALIDATE)
  37. , immutable(false)
  38. {
  39. if (size == 0 && arraylength == 0)
  40. throw love::Exception("Size or array length must be specified.");
  41. if (bufferformat.size() == 0)
  42. throw love::Exception("Data format must contain values.");
  43. const auto &caps = gfx->getCapabilities();
  44. bool supportsGLSL3 = caps.features[Graphics::FEATURE_GLSL3];
  45. bool indexbuffer = usageFlags & BUFFERUSAGEFLAG_INDEX;
  46. bool vertexbuffer = usageFlags & BUFFERUSAGEFLAG_VERTEX;
  47. bool texelbuffer = usageFlags & BUFFERUSAGEFLAG_TEXEL;
  48. bool storagebuffer = usageFlags & BUFFERUSAGEFLAG_SHADER_STORAGE;
  49. bool indirectbuffer = usageFlags & BUFFERUSAGEFLAG_INDIRECT_ARGUMENTS;
  50. if (texelbuffer && !caps.features[Graphics::FEATURE_TEXEL_BUFFER])
  51. throw love::Exception("Texel buffers are not supported on this system.");
  52. if (storagebuffer && !caps.features[Graphics::FEATURE_GLSL4])
  53. throw love::Exception("Shader Storage buffers are not supported on this system (GLSL 4 support is necessary.)");
  54. if (storagebuffer && dataUsage == BUFFERDATAUSAGE_STREAM)
  55. throw love::Exception("Buffers created with 'stream' data usage cannot be used as a shader storage buffer.");
  56. if (indirectbuffer && !caps.features[Graphics::FEATURE_INDIRECT_DRAW])
  57. throw love::Exception("Indirect argument buffers are not supported on this system.");
  58. if (dataUsage == BUFFERDATAUSAGE_READBACK && (indexbuffer || vertexbuffer || texelbuffer || storagebuffer || indirectbuffer))
  59. throw love::Exception("Buffers created with 'readback' data usage cannot be index, vertex, texel, shaderstorage, or indirectarguments buffer types.");
  60. size_t offset = 0;
  61. size_t stride = 0;
  62. size_t structurealignment = 1;
  63. for (const DataDeclaration &decl : bufferformat)
  64. {
  65. DataMember member(decl);
  66. DataFormat format = member.decl.format;
  67. const DataFormatInfo &info = member.info;
  68. if (indexbuffer)
  69. {
  70. if (!caps.features[Graphics::FEATURE_INDEX_BUFFER_32BIT] && format == DATAFORMAT_UINT32)
  71. throw love::Exception("32 bit index buffer formats are not supported on this system.");
  72. if (format != DATAFORMAT_UINT16 && format != DATAFORMAT_UINT32)
  73. throw love::Exception("Index buffers only support uint16 and uint32 data types.");
  74. if (bufferformat.size() > 1)
  75. throw love::Exception("Index buffers only support a single value per element.");
  76. if (decl.arrayLength > 0)
  77. throw love::Exception("Arrays are not supported in index buffers.");
  78. }
  79. if (vertexbuffer)
  80. {
  81. if (decl.arrayLength > 0)
  82. throw love::Exception("Arrays are not supported in vertex buffers.");
  83. if (info.isMatrix)
  84. throw love::Exception("Matrix types are not supported in vertex buffers.");
  85. if (info.baseType == DATA_BASETYPE_BOOL)
  86. throw love::Exception("Bool types are not supported in vertex buffers.");
  87. if ((info.baseType == DATA_BASETYPE_INT || info.baseType == DATA_BASETYPE_UINT) && !supportsGLSL3)
  88. throw love::Exception("Integer vertex attribute data types require GLSL 3 support.");
  89. if (decl.name.empty())
  90. throw love::Exception("Vertex buffer attributes must have a name.");
  91. }
  92. if (texelbuffer)
  93. {
  94. if (format != bufferformat[0].format)
  95. throw love::Exception("All values in a texel buffer must have the same format.");
  96. if (decl.arrayLength > 0)
  97. throw love::Exception("Arrays are not supported in texel buffers.");
  98. if (info.isMatrix)
  99. throw love::Exception("Matrix types are not supported in texel buffers.");
  100. if (info.baseType == DATA_BASETYPE_BOOL)
  101. throw love::Exception("Bool types are not supported in texel buffers.");
  102. if (info.components == 3)
  103. throw love::Exception("3-component formats are not supported in texel buffers.");
  104. if (info.baseType == DATA_BASETYPE_SNORM)
  105. throw love::Exception("Signed normalized formats are not supported in texel buffers.");
  106. }
  107. size_t memberoffset = offset;
  108. size_t membersize = member.info.size;
  109. // Storage buffers are always treated as being an array of a structure.
  110. // The structure's contents are the buffer format declaration.
  111. if (storagebuffer)
  112. {
  113. // TODO: We can support these.
  114. if (decl.arrayLength > 0)
  115. throw love::Exception("Arrays are not currently supported in shader storage buffers.");
  116. if (info.baseType == DATA_BASETYPE_BOOL)
  117. throw love::Exception("Bool types are not supported in shader storage buffers.");
  118. if (info.baseType == DATA_BASETYPE_UNORM || info.baseType == DATA_BASETYPE_SNORM)
  119. throw love::Exception("Normalized formats are not supported in shader storage buffers.");
  120. size_t alignment = 1;
  121. // GLSL's std430 packing rules. We also assume all matrices are
  122. // column-major.
  123. // https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf
  124. // "If the member is a column-major matrix with C columns and R rows,
  125. // the matrix is stored identically to an array of C column vectors
  126. // with R components each".
  127. // "If the member is a three-component vector with components
  128. // consuming N basic machine units, the base alignment is 4N."
  129. int c = info.isMatrix ? info.matrixRows : info.components;
  130. alignment = c == 3 ? 4 * info.componentSize : c * info.componentSize;
  131. // std430 will effectively turn a floatmat3x3 into a floatmat4x3
  132. // because of its vec3 padding rules. For now we'd rather not
  133. // support those formats at all, because it's not easy for users to
  134. // deal with.
  135. if (alignment != c * info.componentSize && (decl.arrayLength > 0 || info.isMatrix))
  136. {
  137. const char *fstr = "unknown";
  138. getConstant(decl.format, fstr);
  139. throw love::Exception("Data format %s%s is not currently supported in shader storage buffers.", fstr, decl.arrayLength > 0 ? " array" : "");
  140. }
  141. // "If the member is a structure, the base alignment of the structure
  142. // is N, where N is the largest base alignment value of any of its
  143. // members"
  144. structurealignment = std::max(structurealignment, alignment);
  145. memberoffset = alignUp(memberoffset, alignment);
  146. if (memberoffset != offset && (indexbuffer || vertexbuffer || texelbuffer))
  147. throw love::Exception("Cannot create Buffer:\nInternal alignment of member '%s' is preventing Buffer from being created as both a shader storage buffer and other buffer types\nMember byte offset needed for shader storage buffer: %d\nMember byte offset needed for other buffer types: %d",
  148. member.decl.name.c_str(), memberoffset, offset);
  149. }
  150. if (indirectbuffer)
  151. {
  152. if (info.isMatrix || info.components != 1
  153. || (info.baseType != DATA_BASETYPE_UINT && info.baseType != DATA_BASETYPE_INT))
  154. {
  155. throw love::Exception("Indirect argument buffers must use single-component int or uint types.");
  156. }
  157. if (bufferformat.size() > 5)
  158. throw love::Exception("Indirect argument buffers only support up to 5 values per array element.");
  159. }
  160. member.offset = memberoffset;
  161. member.size = membersize;
  162. offset = member.offset + member.size;
  163. dataMembers.push_back(member);
  164. }
  165. stride = alignUp(offset, structurealignment);
  166. if (storagebuffer && (indexbuffer || vertexbuffer || texelbuffer))
  167. {
  168. if (stride != offset)
  169. throw love::Exception("Cannot create Buffer:\nBuffer used as a shader storage buffer would have a different number of bytes per array element (%d) than when used as other buffer types (%d)",
  170. stride, offset);
  171. }
  172. if (storagebuffer && stride > SHADER_STORAGE_BUFFER_MAX_STRIDE)
  173. throw love::Exception("Shader storage buffers cannot have more than %d bytes within each array element.", SHADER_STORAGE_BUFFER_MAX_STRIDE);
  174. if (size != 0)
  175. {
  176. size_t remainder = size % stride;
  177. if (remainder > 0)
  178. size += stride - remainder;
  179. arraylength = size / stride;
  180. }
  181. else
  182. {
  183. size = arraylength * stride;
  184. }
  185. this->arrayStride = stride;
  186. this->arrayLength = arraylength;
  187. this->size = size;
  188. if (texelbuffer && arraylength * dataMembers.size() > caps.limits[Graphics::LIMIT_TEXEL_BUFFER_SIZE])
  189. throw love::Exception("Cannot create texel buffer: total number of values in the buffer (%d * %d) is too large for this system (maximum %d).",
  190. (int) dataMembers.size(), (int) arraylength, caps.limits[Graphics::LIMIT_TEXEL_BUFFER_SIZE]);
  191. }
  192. Buffer::~Buffer()
  193. {
  194. }
  195. int Buffer::getDataMemberIndex(const std::string &name) const
  196. {
  197. for (size_t i = 0; i < dataMembers.size(); i++)
  198. {
  199. if (dataMembers[i].decl.name == name)
  200. return (int) i;
  201. }
  202. return -1;
  203. }
  204. std::vector<Buffer::DataDeclaration> Buffer::getCommonFormatDeclaration(CommonFormat format)
  205. {
  206. switch (format)
  207. {
  208. case CommonFormat::NONE:
  209. return {};
  210. case CommonFormat::XYf:
  211. return {
  212. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 }
  213. };
  214. case CommonFormat::XYZf:
  215. return {
  216. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC3 }
  217. };
  218. case CommonFormat::RGBAub:
  219. return {
  220. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 }
  221. };
  222. case CommonFormat::STf_RGBAub:
  223. return {
  224. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  225. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  226. };
  227. case CommonFormat::STPf_RGBAub:
  228. return {
  229. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC3 },
  230. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  231. };
  232. case CommonFormat::XYf_STf:
  233. return {
  234. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  235. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  236. };
  237. case CommonFormat::XYf_STPf:
  238. return {
  239. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  240. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC3 },
  241. };
  242. case CommonFormat::XYf_STf_RGBAub:
  243. return {
  244. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  245. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  246. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  247. };
  248. case CommonFormat::XYf_STus_RGBAub:
  249. return {
  250. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  251. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_UNORM16_VEC2 },
  252. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  253. };
  254. case CommonFormat::XYf_STPf_RGBAub:
  255. return {
  256. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  257. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  258. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  259. };
  260. }
  261. return {};
  262. }
  263. } // graphics
  264. } // love