wrap_Shader.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * Copyright (c) 2006-2013 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_Shader.h"
  21. #include "wrap_Image.h"
  22. #include "wrap_Canvas.h"
  23. #include <string>
  24. #include <iostream>
  25. namespace love
  26. {
  27. namespace graphics
  28. {
  29. namespace opengl
  30. {
  31. Shader *luax_checkshader(lua_State *L, int idx)
  32. {
  33. return luax_checktype<Shader>(L, idx, "Shader", GRAPHICS_SHADER_T);
  34. }
  35. int w_Shader_getWarnings(lua_State *L)
  36. {
  37. Shader *shader = luax_checkshader(L, 1);
  38. lua_pushstring(L, shader->getWarnings().c_str());
  39. return 1;
  40. }
  41. template <typename T>
  42. static T *_getScalars(lua_State *L, int count, size_t &dimension)
  43. {
  44. dimension = 1;
  45. T *values = new T[count];
  46. for (int i = 0; i < count; ++i)
  47. {
  48. if (lua_isnumber(L, 3 + i))
  49. values[i] = static_cast<T>(lua_tonumber(L, 3 + i));
  50. else if (lua_isboolean(L, 3 + i))
  51. values[i] = static_cast<T>(lua_toboolean(L, 3 + i));
  52. else
  53. {
  54. delete[] values;
  55. luax_typerror(L, 3 + i, "number or boolean");
  56. return 0;
  57. }
  58. }
  59. return values;
  60. }
  61. template <typename T>
  62. static T *_getVectors(lua_State *L, int count, size_t &dimension)
  63. {
  64. dimension = lua_objlen(L, 3);
  65. T *values = new T[count * dimension];
  66. for (int i = 0; i < count; ++i)
  67. {
  68. if (!lua_istable(L, 3 + i))
  69. {
  70. delete[] values;
  71. luax_typerror(L, 3 + i, "table");
  72. return 0;
  73. }
  74. if (lua_objlen(L, 3 + i) != dimension)
  75. {
  76. delete[] values;
  77. luaL_error(L, "Error in argument %d: Expected table size %d, got %d.",
  78. 3+i, dimension, lua_objlen(L, 3+i));
  79. return 0;
  80. }
  81. for (size_t k = 1; k <= dimension; ++k)
  82. {
  83. lua_rawgeti(L, 3 + i, k);
  84. if (lua_isnumber(L, -1))
  85. values[i * dimension + k - 1] = static_cast<T>(lua_tonumber(L, -1));
  86. else if (lua_isboolean(L, -1))
  87. values[i * dimension + k - 1] = static_cast<T>(lua_toboolean(L, -1));
  88. else
  89. {
  90. delete[] values;
  91. luax_typerror(L, -1, "number or boolean");
  92. return 0;
  93. }
  94. }
  95. lua_pop(L, int(dimension));
  96. }
  97. return values;
  98. }
  99. int w_Shader_sendInt(lua_State *L)
  100. {
  101. Shader *shader = luax_checkshader(L, 1);
  102. const char *name = luaL_checkstring(L, 2);
  103. int count = lua_gettop(L) - 2;
  104. if (count < 1)
  105. return luaL_error(L, "No variable to send.");
  106. int *values = 0;
  107. size_t dimension = 1;
  108. if (lua_isnumber(L, 3) || lua_isboolean(L, 3))
  109. values = _getScalars<int>(L, count, dimension);
  110. else if (lua_istable(L, 3))
  111. values = _getVectors<int>(L, count, dimension);
  112. else
  113. return luax_typerror(L, 3, "number, boolean, or table");
  114. if (!values)
  115. return luaL_error(L, "Error in arguments.");
  116. bool should_error = false;
  117. try
  118. {
  119. shader->sendInt(name, dimension, values, count);
  120. }
  121. catch (love::Exception &e)
  122. {
  123. should_error = true;
  124. lua_pushstring(L, e.what());
  125. }
  126. delete[] values;
  127. if (should_error)
  128. return lua_error(L);
  129. return 0;
  130. }
  131. int w_Shader_sendFloat(lua_State *L)
  132. {
  133. Shader *shader = luax_checkshader(L, 1);
  134. const char *name = luaL_checkstring(L, 2);
  135. int count = lua_gettop(L) - 2;
  136. if (count < 1)
  137. return luaL_error(L, "No variable to send.");
  138. float *values = 0;
  139. size_t dimension = 1;
  140. if (lua_isnumber(L, 3) || lua_isboolean(L, 3))
  141. values = _getScalars<float>(L, count, dimension);
  142. else if (lua_istable(L, 3))
  143. values = _getVectors<float>(L, count, dimension);
  144. else
  145. return luax_typerror(L, 3, "number, boolean, or table");
  146. if (!values)
  147. return luaL_error(L, "Error in arguments.");
  148. bool should_error = false;
  149. try
  150. {
  151. shader->sendFloat(name, dimension, values, count);
  152. }
  153. catch (love::Exception &e)
  154. {
  155. should_error = true;
  156. lua_pushstring(L, e.what());
  157. }
  158. delete[] values;
  159. if (should_error)
  160. return lua_error(L);
  161. return 0;
  162. }
  163. int w_Shader_sendMatrix(lua_State *L)
  164. {
  165. int count = lua_gettop(L) - 2;
  166. Shader *shader = luax_checkshader(L, 1);
  167. const char *name = luaL_checkstring(L, 2);
  168. if (!lua_istable(L, 3))
  169. return luax_typerror(L, 3, "matrix table");
  170. lua_getfield(L, 3, "dimension");
  171. int dimension = lua_tointeger(L, -1);
  172. lua_pop(L, 1);
  173. if (dimension < 2 || dimension > 4)
  174. return luaL_error(L, "Invalid matrix size: %dx%d (only 2x2, 3x3 and 4x4 matrices are supported).",
  175. dimension, dimension);
  176. float *values = new float[dimension * dimension * count];
  177. for (int i = 0; i < count; ++i)
  178. {
  179. lua_getfield(L, 3+i, "dimension");
  180. if (lua_tointeger(L, -1) != dimension)
  181. {
  182. // You unlock this door with the key of imagination. Beyond it is
  183. // another dimension: a dimension of sound, a dimension of sight,
  184. // a dimension of mind. You're moving into a land of both shadow
  185. // and substance, of things and ideas. You've just crossed over
  186. // into... the Twilight Zone.
  187. int other_dimension = lua_tointeger(L, -1);
  188. delete[] values;
  189. return luaL_error(L, "Invalid matrix size at argument %d: Expected size %dx%d, got %dx%d.",
  190. 3+i, dimension, dimension, other_dimension, other_dimension);
  191. }
  192. for (int k = 1; k <= dimension*dimension; ++k)
  193. {
  194. lua_rawgeti(L, 3+i, k);
  195. values[i * dimension * dimension + k - 1] = (float)lua_tonumber(L, -1);
  196. }
  197. lua_pop(L, 1 + dimension);
  198. }
  199. bool should_error = false;
  200. try
  201. {
  202. shader->sendMatrix(name, dimension, values, count);
  203. }
  204. catch(love::Exception &e)
  205. {
  206. should_error = true;
  207. lua_pushstring(L, e.what());
  208. }
  209. delete[] values;
  210. if (should_error)
  211. return lua_error(L);
  212. return 0;
  213. }
  214. int w_Shader_sendImage(lua_State *L)
  215. {
  216. Shader *shader = luax_checkshader(L, 1);
  217. const char *name = luaL_checkstring(L, 2);
  218. Image *img = luax_checkimage(L, 3);
  219. EXCEPT_GUARD(shader->sendImage(name, *img);)
  220. return 0;
  221. }
  222. int w_Shader_sendCanvas(lua_State *L)
  223. {
  224. Shader *shader = luax_checkshader(L, 1);
  225. const char *name = luaL_checkstring(L, 2);
  226. Canvas *canvas = luax_checkcanvas(L, 3);
  227. EXCEPT_GUARD(shader->sendCanvas(name, *canvas);)
  228. return 0;
  229. }
  230. // Convert matrices on the stack for use with sendMatrix.
  231. static void w_convertMatrices(lua_State *L, int idx)
  232. {
  233. int matrixcount = lua_gettop(L) - (idx - 1);
  234. for (int matrix = idx; matrix < idx + matrixcount; matrix++)
  235. {
  236. luaL_checktype(L, matrix, LUA_TTABLE);
  237. int dimension = lua_objlen(L, matrix);
  238. int newi = 1;
  239. lua_createtable(L, dimension * dimension, 0);
  240. // Collapse {{a,b,c}, {d,e,f}, ...} to {a,b,c, d,e,f, ...}
  241. for (size_t i = 1; i <= lua_objlen(L, matrix); i++)
  242. {
  243. // Push args[matrix][i] onto the stack.
  244. lua_rawgeti(L, matrix, i);
  245. luaL_checktype(L, -1, LUA_TTABLE);
  246. for (size_t j = 1; j <= lua_objlen(L, -1); j++)
  247. {
  248. // Push args[matrix[i][j] onto the stack.
  249. lua_rawgeti(L, -1, j);
  250. luaL_checktype(L, -1, LUA_TNUMBER);
  251. // newtable[newi] = args[matrix][i][j]
  252. lua_rawseti(L, -3, newi++);
  253. }
  254. lua_pop(L, 1);
  255. }
  256. // newtable.dimension = #args[matrix]
  257. lua_pushinteger(L, dimension);
  258. lua_setfield(L, -2, "dimension");
  259. // Replace args[i] with the new table
  260. lua_replace(L, matrix);
  261. }
  262. }
  263. int w_Shader_send(lua_State *L)
  264. {
  265. int ttype = lua_type(L, 3);
  266. Proxy *p = 0;
  267. switch (ttype)
  268. {
  269. case LUA_TNUMBER:
  270. case LUA_TBOOLEAN:
  271. // Scalar float/boolean.
  272. return w_Shader_sendFloat(L);
  273. break;
  274. case LUA_TUSERDATA:
  275. // Image or Canvas.
  276. p = (Proxy *) lua_touserdata(L, 3);
  277. if (p->flags[GRAPHICS_IMAGE_ID])
  278. return w_Shader_sendImage(L);
  279. else if (p->flags[GRAPHICS_CANVAS_ID])
  280. return w_Shader_sendCanvas(L);
  281. break;
  282. case LUA_TTABLE:
  283. // Vector or Matrix.
  284. lua_rawgeti(L, 3, 1);
  285. ttype = lua_type(L, -1);
  286. lua_pop(L, 1);
  287. if (ttype == LUA_TNUMBER || ttype == LUA_TBOOLEAN)
  288. return w_Shader_sendFloat(L);
  289. else if (ttype == LUA_TTABLE)
  290. {
  291. w_convertMatrices(L, 3);
  292. return w_Shader_sendMatrix(L);
  293. }
  294. break;
  295. default:
  296. break;
  297. }
  298. return luaL_argerror(L, 3, "number, boolean, table, image, or canvas expected");
  299. }
  300. static const luaL_Reg functions[] =
  301. {
  302. { "getWarnings", w_Shader_getWarnings },
  303. { "sendInt", w_Shader_sendInt },
  304. { "sendBoolean", w_Shader_sendInt },
  305. { "sendFloat", w_Shader_sendFloat },
  306. { "sendMatrix", w_Shader_sendMatrix },
  307. { "sendImage", w_Shader_sendImage },
  308. { "sendCanvas", w_Shader_sendCanvas },
  309. { "send", w_Shader_send },
  310. { 0, 0 }
  311. };
  312. extern "C" int luaopen_shader(lua_State *L)
  313. {
  314. return luax_register_type(L, "Shader", functions);
  315. }
  316. } // opengl
  317. } // graphics
  318. } // love