Buffer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. static const Buffer::DataTypeInfo dataTypeInfo[]
  27. {
  28. // baseType, isMatrix, components, rows, columns, componentSize, packedAlign, packedSize
  29. { Buffer::DATA_BASE_FLOAT, false, 1, 0, 0, sizeof(float), 4, 4 }, // DATA_FLOAT
  30. };
  31. love::Type Buffer::type("GraphicsBuffer", &Object::type);
  32. Buffer::Buffer(size_t size, BufferTypeFlags typeflags, BufferUsage usage, uint32 mapflags)
  33. : size(size)
  34. , typeFlags(typeflags)
  35. , usage(usage)
  36. , mapFlags(mapflags)
  37. , mapped(false)
  38. {
  39. }
  40. Buffer::Buffer(Graphics *gfx, const Settings &settings, const std::vector<DataMember> &format, size_t arraylength)
  41. : Buffer(0, settings.typeFlags, settings.usage, settings.mapFlags)
  42. {
  43. if (format.size() == 0)
  44. throw love::Exception("Data format must contain values.");
  45. bool supportsGLSL3 = gfx->getCapabilities().features[Graphics::FEATURE_GLSL3];
  46. bool uniformbuffer = settings.typeFlags & BUFFERFLAG_UNIFORM;
  47. bool indexbuffer = settings.typeFlags & BUFFERFLAG_INDEX;
  48. bool vertexbuffer = settings.typeFlags & BUFFERFLAG_VERTEX;
  49. bool ssbuffer = settings.typeFlags & BUFFERFLAG_SHADER_STORAGE;
  50. if (indexbuffer && format.size() > 1)
  51. throw love::Exception("test");
  52. size_t offset = 0;
  53. size_t stride = 0;
  54. for (const auto &member : format)
  55. {
  56. DataType type = member.type;
  57. const DataTypeInfo &info = getDataTypeInfo(type);
  58. if (indexbuffer)
  59. {
  60. if (type != DATA_UINT16 && type != DATA_UINT32)
  61. throw love::Exception("Index buffers only support uint16 and uint32 data types.");
  62. }
  63. if (vertexbuffer)
  64. {
  65. if (info.isMatrix)
  66. throw love::Exception("matrix types are not supported in vertex buffers.");
  67. if (info.baseType == DATA_BASE_BOOL)
  68. throw love::Exception("bool types are not supported in vertex buffers.");
  69. if ((info.baseType == DATA_BASE_INT || info.baseType == DATA_BASE_UINT) && !supportsGLSL3)
  70. throw love::Exception("Integer vertex attribute data types require GLSL 3 support.");
  71. }
  72. if (uniformbuffer)
  73. {
  74. if (info.componentSize != 4)
  75. throw love::Exception("");
  76. }
  77. }
  78. }
  79. Buffer::~Buffer()
  80. {
  81. }
  82. } // graphics
  83. } // love