wrap_Buffer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 "wrap_Buffer.h"
  21. #include "Buffer.h"
  22. #include "common/Data.h"
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. static const double defaultComponents[] = {0.0, 0.0, 0.0, 1.0};
  28. template <typename T>
  29. static inline size_t writeData(lua_State *L, int startidx, int components, char *data)
  30. {
  31. auto componentdata = (T *) data;
  32. for (int i = 0; i < components; i++)
  33. componentdata[i] = (T) (luaL_optnumber(L, startidx + i, defaultComponents[i]));
  34. return sizeof(T) * components;
  35. }
  36. template <typename T>
  37. static inline size_t writeSNormData(lua_State *L, int startidx, int components, char *data)
  38. {
  39. auto componentdata = (T *) data;
  40. const auto maxval = std::numeric_limits<T>::max();
  41. for (int i = 0; i < components; i++)
  42. componentdata[i] = (T) (luax_optnumberclamped(L, startidx + i, -1.0, 1.0, defaultComponents[i]) * maxval);
  43. return sizeof(T) * components;
  44. }
  45. template <typename T>
  46. static inline size_t writeUNormData(lua_State *L, int startidx, int components, char *data)
  47. {
  48. auto componentdata = (T *) data;
  49. const auto maxval = std::numeric_limits<T>::max();
  50. for (int i = 0; i < components; i++)
  51. componentdata[i] = (T) (luax_optnumberclamped01(L, startidx + i, 1.0) * maxval);
  52. return sizeof(T) * components;
  53. }
  54. void luax_writebufferdata(lua_State *L, int startidx, DataFormat format, char *data)
  55. {
  56. switch (format)
  57. {
  58. case DATAFORMAT_FLOAT: writeData<float>(L, startidx, 1, data); break;
  59. case DATAFORMAT_FLOAT_VEC2: writeData<float>(L, startidx, 2, data); break;
  60. case DATAFORMAT_FLOAT_VEC3: writeData<float>(L, startidx, 3, data); break;
  61. case DATAFORMAT_FLOAT_VEC4: writeData<float>(L, startidx, 4, data); break;
  62. case DATAFORMAT_INT32: writeData<int32>(L, startidx, 1, data); break;
  63. case DATAFORMAT_INT32_VEC2: writeData<int32>(L, startidx, 2, data); break;
  64. case DATAFORMAT_INT32_VEC3: writeData<int32>(L, startidx, 3, data); break;
  65. case DATAFORMAT_INT32_VEC4: writeData<int32>(L, startidx, 4, data); break;
  66. case DATAFORMAT_UINT32: writeData<uint32>(L, startidx, 1, data); break;
  67. case DATAFORMAT_UINT32_VEC2: writeData<uint32>(L, startidx, 2, data); break;
  68. case DATAFORMAT_UINT32_VEC3: writeData<uint32>(L, startidx, 3, data); break;
  69. case DATAFORMAT_UINT32_VEC4: writeData<uint32>(L, startidx, 4, data); break;
  70. case DATAFORMAT_SNORM8_VEC4: writeSNormData<int8>(L, startidx, 4, data); break;
  71. case DATAFORMAT_UNORM8_VEC4: writeUNormData<uint8>(L, startidx, 4, data); break;
  72. case DATAFORMAT_INT8_VEC4: writeData<int8>(L, startidx, 4, data); break;
  73. case DATAFORMAT_UINT8_VEC4: writeData<uint8>(L, startidx, 4, data); break;
  74. case DATAFORMAT_SNORM16_VEC2: writeSNormData<int16>(L, startidx, 2, data); break;
  75. case DATAFORMAT_SNORM16_VEC4: writeSNormData<int16>(L, startidx, 4, data); break;
  76. case DATAFORMAT_UNORM16_VEC2: writeUNormData<uint16>(L, startidx, 2, data); break;
  77. case DATAFORMAT_UNORM16_VEC4: writeUNormData<uint16>(L, startidx, 4, data); break;
  78. case DATAFORMAT_INT16_VEC2: writeData<int16>(L, startidx, 2, data); break;
  79. case DATAFORMAT_INT16_VEC4: writeData<int16>(L, startidx, 4, data); break;
  80. case DATAFORMAT_UINT16: writeData<uint16>(L, startidx, 1, data); break;
  81. case DATAFORMAT_UINT16_VEC2: writeData<uint16>(L, startidx, 2, data); break;
  82. case DATAFORMAT_UINT16_VEC4: writeData<uint16>(L, startidx, 4, data); break;
  83. default: break;
  84. }
  85. }
  86. template <typename T>
  87. static inline size_t readData(lua_State *L, int components, const char *data)
  88. {
  89. const auto componentdata = (const T *) data;
  90. for (int i = 0; i < components; i++)
  91. lua_pushnumber(L, (lua_Number) componentdata[i]);
  92. return sizeof(T) * components;
  93. }
  94. template <typename T>
  95. static inline size_t readSNormData(lua_State *L, int components, const char *data)
  96. {
  97. const auto componentdata = (const T *) data;
  98. const auto maxval = std::numeric_limits<T>::max();
  99. for (int i = 0; i < components; i++)
  100. lua_pushnumber(L, std::max(-1.0, (lua_Number) componentdata[i] / (lua_Number)maxval));
  101. return sizeof(T) * components;
  102. }
  103. template <typename T>
  104. static inline size_t readUNormData(lua_State *L, int components, const char *data)
  105. {
  106. const auto componentdata = (const T *) data;
  107. const auto maxval = std::numeric_limits<T>::max();
  108. for (int i = 0; i < components; i++)
  109. lua_pushnumber(L, (lua_Number) componentdata[i] / (lua_Number)maxval);
  110. return sizeof(T) * components;
  111. }
  112. void luax_readbufferdata(lua_State *L, DataFormat format, const char *data)
  113. {
  114. switch (format)
  115. {
  116. case DATAFORMAT_FLOAT: readData<float>(L, 1, data); break;
  117. case DATAFORMAT_FLOAT_VEC2: readData<float>(L, 2, data); break;
  118. case DATAFORMAT_FLOAT_VEC3: readData<float>(L, 3, data); break;
  119. case DATAFORMAT_FLOAT_VEC4: readData<float>(L, 4, data); break;
  120. case DATAFORMAT_INT32: readData<int32>(L, 1, data); break;
  121. case DATAFORMAT_INT32_VEC2: readData<int32>(L, 2, data); break;
  122. case DATAFORMAT_INT32_VEC3: readData<int32>(L, 3, data); break;
  123. case DATAFORMAT_INT32_VEC4: readData<int32>(L, 4, data); break;
  124. case DATAFORMAT_UINT32: readData<uint32>(L, 1, data); break;
  125. case DATAFORMAT_UINT32_VEC2: readData<uint32>(L, 2, data); break;
  126. case DATAFORMAT_UINT32_VEC3: readData<uint32>(L, 3, data); break;
  127. case DATAFORMAT_UINT32_VEC4: readData<uint32>(L, 4, data); break;
  128. case DATAFORMAT_SNORM8_VEC4: readSNormData<int8>(L, 4, data); break;
  129. case DATAFORMAT_UNORM8_VEC4: readUNormData<uint8>(L, 4, data); break;
  130. case DATAFORMAT_INT8_VEC4: readData<int8>(L, 4, data); break;
  131. case DATAFORMAT_UINT8_VEC4: readData<uint8>(L, 4, data); break;
  132. case DATAFORMAT_SNORM16_VEC2: readSNormData<int16>(L, 2, data); break;
  133. case DATAFORMAT_SNORM16_VEC4: readSNormData<int16>(L, 4, data); break;
  134. case DATAFORMAT_UNORM16_VEC2: readUNormData<uint16>(L, 2, data); break;
  135. case DATAFORMAT_UNORM16_VEC4: readUNormData<uint16>(L, 4, data); break;
  136. case DATAFORMAT_INT16_VEC2: readData<int16>(L, 2, data); break;
  137. case DATAFORMAT_INT16_VEC4: readData<int16>(L, 4, data); break;
  138. case DATAFORMAT_UINT16: readData<uint16>(L, 1, data); break;
  139. case DATAFORMAT_UINT16_VEC2: readData<uint16>(L, 2, data); break;
  140. case DATAFORMAT_UINT16_VEC4: readData<uint16>(L, 4, data); break;
  141. default: break;
  142. }
  143. }
  144. Buffer *luax_checkbuffer(lua_State *L, int idx)
  145. {
  146. return luax_checktype<Buffer>(L, idx);
  147. }
  148. static int w_Buffer_flush(lua_State *L)
  149. {
  150. Buffer *t = luax_checkbuffer(L, 1);
  151. t->unmap();
  152. return 0;
  153. }
  154. static int w_Buffer_setArrayData(lua_State *L)
  155. {
  156. Buffer *t = luax_checkbuffer(L, 1);
  157. int startindex = (int) luaL_optnumber(L, 3, 1) - 1;
  158. int count = -1;
  159. if (!lua_isnoneornil(L, 4))
  160. {
  161. count = (int) luaL_checknumber(L, 4);
  162. if (count <= 0)
  163. return luaL_error(L, "Element count must be greater than 0.");
  164. }
  165. size_t stride = t->getArrayStride();
  166. size_t offset = startindex * stride;
  167. int arraylength = (int) t->getArrayLength();
  168. if (startindex >= arraylength || startindex < 0)
  169. return luaL_error(L, "Invalid vertex start index (must be between 1 and %d)", arraylength);
  170. if (luax_istype(L, 2, Data::type))
  171. {
  172. Data *d = luax_checktype<Data>(L, 2);
  173. count = count >= 0 ? count : (arraylength - startindex);
  174. if (startindex + count > arraylength)
  175. return luaL_error(L, "Too many array elements (expected at most %d, got %d)", arraylength - startindex, count);
  176. size_t datasize = std::min(d->getSize(), count * stride);
  177. char *bytedata = (char *) t->map() + offset;
  178. memcpy(bytedata, d->getData(), datasize);
  179. t->setMappedRangeModified(offset, datasize);
  180. t->unmap();
  181. return 0;
  182. }
  183. const std::vector<Buffer::DataMember> &members = t->getDataMembers();
  184. int ncomponents = 0;
  185. for (const Buffer::DataMember &member : members)
  186. ncomponents += member.info.components;
  187. luaL_checktype(L, 2, LUA_TTABLE);
  188. int tablelen = (int) luax_objlen(L, 2);
  189. lua_rawgeti(L, 2, 1);
  190. bool tableoftables = lua_istable(L, -1);
  191. lua_pop(L, 1);
  192. if (!tableoftables)
  193. {
  194. if (tablelen % ncomponents != 0)
  195. return luaL_error(L, "Array length in flat array variant of Buffer:setArrayData must be a multiple of the total number of components (%d)", ncomponents);
  196. tablelen /= ncomponents;
  197. }
  198. count = count >= 0 ? std::min(count, tablelen) : tablelen;
  199. if (startindex + count > arraylength)
  200. return luaL_error(L, "Too many array elements (expected at most %d, got %d)", arraylength - startindex, count);
  201. char *data = (char *) t->map() + offset;
  202. if (tableoftables)
  203. {
  204. for (int i = 0; i < count; i++)
  205. {
  206. // get arraydata[index]
  207. lua_rawgeti(L, 2, i + 1);
  208. luaL_checktype(L, -1, LUA_TTABLE);
  209. // get arraydata[index][j]
  210. for (int j = 1; j <= ncomponents; j++)
  211. lua_rawgeti(L, -j, j);
  212. int idx = -ncomponents;
  213. for (const Buffer::DataMember &member : members)
  214. {
  215. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  216. idx += member.info.components;
  217. }
  218. lua_pop(L, ncomponents + 1);
  219. data += stride;
  220. }
  221. }
  222. else // Flat array
  223. {
  224. for (int i = 0; i < count; i++)
  225. {
  226. // get arraydata[arrayindex * ncomponents + componentindex]
  227. for (int componentindex = 1; componentindex <= ncomponents; componentindex++)
  228. lua_rawgeti(L, 2, i * ncomponents + componentindex);
  229. int idx = -ncomponents;
  230. for (const Buffer::DataMember &member : members)
  231. {
  232. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  233. idx += member.info.components;
  234. }
  235. lua_pop(L, ncomponents);
  236. data += stride;
  237. }
  238. }
  239. t->setMappedRangeModified(offset, count * stride);
  240. t->unmap();
  241. return 0;
  242. }
  243. static int w_Buffer_setElement(lua_State *L)
  244. {
  245. Buffer *t = luax_checkbuffer(L, 1);
  246. size_t index = (size_t) (luaL_checkinteger(L, 2) - 1);
  247. if (index >= t->getArrayLength())
  248. return luaL_error(L, "Invalid Buffer element index: %d", (int) index + 1);
  249. size_t stride = t->getArrayStride();
  250. size_t offset = index * stride;
  251. char *data = (char *) t->map() + offset;
  252. const auto &members = t->getDataMembers();
  253. bool istable = lua_istable(L, 3);
  254. int idx = istable ? 1 : 3;
  255. if (istable)
  256. {
  257. for (const Buffer::DataMember &member : members)
  258. {
  259. int components = member.info.components;
  260. for (int i = idx; i < idx + components; i++)
  261. lua_rawgeti(L, 3, i);
  262. luax_writebufferdata(L, -components, member.decl.format, data + member.offset);
  263. idx += components;
  264. lua_pop(L, components);
  265. }
  266. }
  267. else
  268. {
  269. for (const Buffer::DataMember &member : members)
  270. {
  271. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  272. idx += member.info.components;
  273. }
  274. }
  275. t->setMappedRangeModified(offset, stride);
  276. return 0;
  277. }
  278. static int w_Buffer_getElement(lua_State *L)
  279. {
  280. Buffer *t = luax_checkbuffer(L, 1);
  281. if ((t->getMapFlags() & Buffer::MAP_READ) == 0)
  282. return luaL_error(L, "Buffer:getElement requires the buffer to be created with the 'cpureadable' setting set to true.");
  283. size_t index = (size_t) (luaL_checkinteger(L, 2) - 1);
  284. if (index >= t->getArrayLength())
  285. return luaL_error(L, "Invalid Buffer element index: %d", (int) index + 1);
  286. size_t offset = index * t->getArrayStride();
  287. const char *data = (const char *) t->map() + offset;
  288. const auto &members = t->getDataMembers();
  289. int n = 0;
  290. for (const Buffer::DataMember &member : members)
  291. {
  292. luax_readbufferdata(L, member.decl.format, data + member.offset);
  293. n += member.info.components;
  294. }
  295. return n;
  296. }
  297. static int w_Buffer_getElementCount(lua_State *L)
  298. {
  299. Buffer *t = luax_checkbuffer(L, 1);
  300. lua_pushinteger(L, t->getArrayLength());
  301. return 1;
  302. }
  303. static int w_Buffer_getElementStride(lua_State *L)
  304. {
  305. Buffer *t = luax_checkbuffer(L, 1);
  306. lua_pushinteger(L, t->getArrayStride());
  307. return 1;
  308. }
  309. static int w_Buffer_getSize(lua_State *L)
  310. {
  311. Buffer *t = luax_checkbuffer(L, 1);
  312. lua_pushinteger(L, t->getSize());
  313. return 1;
  314. }
  315. static int w_Buffer_getFormat(lua_State *L)
  316. {
  317. Buffer *t = luax_checkbuffer(L, 1);
  318. const auto &members = t->getDataMembers();
  319. lua_createtable(L, (int) members.size(), 0);
  320. for (size_t i = 0; i < members.size(); i++)
  321. {
  322. const Buffer::DataMember &member = members[i];
  323. lua_createtable(L, 0, 4);
  324. lua_pushstring(L, member.decl.name.c_str());
  325. lua_setfield(L, -2, "name");
  326. const char *formatstr = "unknown";
  327. getConstant(member.decl.format, formatstr);
  328. lua_pushstring(L, formatstr);
  329. lua_setfield(L, -2, "format");
  330. lua_pushinteger(L, member.decl.arrayLength);
  331. lua_setfield(L, -2, "arraylength");
  332. lua_pushinteger(L, member.offset);
  333. lua_setfield(L, -2, "offset");
  334. lua_rawseti(L, -2, i + 1);
  335. }
  336. return 1;
  337. }
  338. static int w_Buffer_isBufferType(lua_State *L)
  339. {
  340. Buffer *t = luax_checkbuffer(L, 1);
  341. BufferType buffertype = BUFFERTYPE_MAX_ENUM;
  342. const char *typestr = luaL_checkstring(L, 2);
  343. if (!getConstant(typestr, buffertype))
  344. return luax_enumerror(L, "buffer type", getConstants(buffertype), typestr);
  345. luax_pushboolean(L, (t->getTypeFlags() & (1 << buffertype)) != 0);
  346. return 1;
  347. }
  348. static int w_Buffer_isCPUReadable(lua_State *L)
  349. {
  350. Buffer *t = luax_checkbuffer(L, 1);
  351. luax_pushboolean(L, (t->getMapFlags() & Buffer::MAP_READ) != 0);
  352. return 1;
  353. }
  354. static const luaL_Reg w_Buffer_functions[] =
  355. {
  356. { "flush", w_Buffer_flush },
  357. { "setArrayData", w_Buffer_setArrayData },
  358. { "setElement", w_Buffer_setElement },
  359. { "getElement", w_Buffer_getElement },
  360. { "getElementCount", w_Buffer_getElementCount },
  361. { "getElementStride", w_Buffer_getElementStride },
  362. { "getSize", w_Buffer_getSize },
  363. { "getFormat", w_Buffer_getFormat },
  364. { "isBufferType", w_Buffer_isBufferType },
  365. { "isCPUReadable", w_Buffer_isCPUReadable },
  366. { 0, 0 }
  367. };
  368. extern "C" int luaopen_graphicsbuffer(lua_State *L)
  369. {
  370. return luax_register_type(L, &Buffer::type, w_Buffer_functions, nullptr);
  371. }
  372. } // graphics
  373. } // love