Buffer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "Buffer.h"
  21. #include "Graphics.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. love::Type Buffer::type("GraphicsBuffer", &Object::type);
  27. Buffer::Buffer(Graphics *gfx, const Settings &settings, const std::vector<DataDeclaration> &bufferformat, size_t size, size_t arraylength)
  28. : arrayLength(0)
  29. , arrayStride(0)
  30. , size(size)
  31. , typeFlags(settings.typeFlags)
  32. , usage(settings.usage)
  33. , mapFlags(settings.mapFlags)
  34. , mapped(false)
  35. {
  36. if (size == 0 && arraylength == 0)
  37. throw love::Exception("Size or array length must be specified.");
  38. if (bufferformat.size() == 0)
  39. throw love::Exception("Data format must contain values.");
  40. bool supportsGLSL3 = gfx->getCapabilities().features[Graphics::FEATURE_GLSL3];
  41. bool indexbuffer = settings.typeFlags & TYPEFLAG_INDEX;
  42. bool vertexbuffer = settings.typeFlags & TYPEFLAG_VERTEX;
  43. size_t offset = 0;
  44. size_t stride = 0;
  45. for (const DataDeclaration &decl : bufferformat)
  46. {
  47. DataMember member(decl);
  48. DataFormat format = member.decl.format;
  49. const DataFormatInfo &info = member.info;
  50. if (indexbuffer)
  51. {
  52. if (format != DATAFORMAT_UINT16 && format != DATAFORMAT_UINT32)
  53. throw love::Exception("Index buffers only support uint16 and uint32 data types.");
  54. if (bufferformat.size() > 1)
  55. throw love::Exception("Index buffers only support a single value per element.");
  56. }
  57. if (vertexbuffer)
  58. {
  59. if (decl.arrayLength > 0)
  60. throw love::Exception("Arrays are not supported in vertex buffers.");
  61. if (info.isMatrix)
  62. throw love::Exception("Matrix types are not supported in vertex buffers.");
  63. if (info.baseType == DATA_BASETYPE_BOOL)
  64. throw love::Exception("Bool types are not supported in vertex buffers.");
  65. if ((info.baseType == DATA_BASETYPE_INT || info.baseType == DATA_BASETYPE_UINT) && !supportsGLSL3)
  66. throw love::Exception("Integer vertex attribute data types require GLSL 3 support.");
  67. }
  68. // TODO: alignment
  69. member.offset = offset;
  70. member.size = member.info.size;
  71. offset += member.size;
  72. dataMembers.push_back(member);
  73. }
  74. stride = offset;
  75. if (size != 0)
  76. {
  77. size_t remainder = size % stride;
  78. if (remainder > 0)
  79. size += stride - remainder;
  80. arraylength = size / stride;
  81. }
  82. else
  83. {
  84. size = arraylength * stride;
  85. }
  86. this->arrayStride = stride;
  87. this->arrayLength = arraylength;
  88. this->size = size;
  89. }
  90. Buffer::~Buffer()
  91. {
  92. }
  93. int Buffer::getDataMemberIndex(const std::string &name) const
  94. {
  95. for (size_t i = 0; i < dataMembers.size(); i++)
  96. {
  97. if (dataMembers[i].decl.name == name)
  98. return (int) i;
  99. }
  100. return -1;
  101. }
  102. std::vector<Buffer::DataDeclaration> Buffer::getCommonFormatDeclaration(CommonFormat format)
  103. {
  104. switch (format)
  105. {
  106. case CommonFormat::NONE:
  107. return {};
  108. case CommonFormat::XYf:
  109. return {
  110. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 }
  111. };
  112. case CommonFormat::XYZf:
  113. return {
  114. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC3 }
  115. };
  116. case CommonFormat::RGBAub:
  117. return {
  118. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 }
  119. };
  120. case CommonFormat::STf_RGBAub:
  121. return {
  122. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  123. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  124. };
  125. case CommonFormat::STPf_RGBAub:
  126. return {
  127. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC3 },
  128. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  129. };
  130. case CommonFormat::XYf_STf:
  131. return {
  132. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  133. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  134. };
  135. case CommonFormat::XYf_STPf:
  136. return {
  137. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  138. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC3 },
  139. };
  140. case CommonFormat::XYf_STf_RGBAub:
  141. return {
  142. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  143. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  144. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  145. };
  146. case CommonFormat::XYf_STus_RGBAub:
  147. return {
  148. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  149. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_UNORM16_VEC2 },
  150. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  151. };
  152. case CommonFormat::XYf_STPf_RGBAub:
  153. return {
  154. { getConstant(ATTRIB_POS), DATAFORMAT_FLOAT_VEC2 },
  155. { getConstant(ATTRIB_TEXCOORD), DATAFORMAT_FLOAT_VEC2 },
  156. { getConstant(ATTRIB_COLOR), DATAFORMAT_UNORM8_VEC4 },
  157. };
  158. }
  159. return {};
  160. }
  161. } // graphics
  162. } // love