wrap_Shader.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. luaL_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. luaL_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. luaL_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 luaL_typerror(L, 3, "number, boolean, or table");
  114. if (!values)
  115. return luaL_error(L, "Error in arguments.");
  116. try
  117. {
  118. shader->sendInt(name, dimension, values, count);
  119. }
  120. catch (love::Exception &e)
  121. {
  122. delete[] values;
  123. return luaL_error(L, "%s", e.what());
  124. }
  125. delete[] values;
  126. return 0;
  127. }
  128. int w_Shader_sendFloat(lua_State *L)
  129. {
  130. Shader *shader = luax_checkshader(L, 1);
  131. const char *name = luaL_checkstring(L, 2);
  132. int count = lua_gettop(L) - 2;
  133. if (count < 1)
  134. return luaL_error(L, "No variable to send.");
  135. float *values = 0;
  136. size_t dimension = 1;
  137. if (lua_isnumber(L, 3) || lua_isboolean(L, 3))
  138. values = _getScalars<float>(L, count, dimension);
  139. else if (lua_istable(L, 3))
  140. values = _getVectors<float>(L, count, dimension);
  141. else
  142. return luaL_typerror(L, 3, "number, boolean, or table");
  143. if (!values)
  144. return luaL_error(L, "Error in arguments.");
  145. try
  146. {
  147. shader->sendFloat(name, dimension, values, count);
  148. }
  149. catch (love::Exception &e)
  150. {
  151. delete[] values;
  152. return luaL_error(L, "%s", e.what());
  153. }
  154. delete[] values;
  155. return 0;
  156. }
  157. int w_Shader_sendMatrix(lua_State *L)
  158. {
  159. int count = lua_gettop(L) - 2;
  160. Shader *shader = luax_checkshader(L, 1);
  161. const char *name = luaL_checkstring(L, 2);
  162. if (!lua_istable(L, 3))
  163. return luaL_typerror(L, 3, "matrix table");
  164. lua_getfield(L, 3, "dimension");
  165. int dimension = lua_tointeger(L, -1);
  166. lua_pop(L, 1);
  167. if (dimension < 2 || dimension > 4)
  168. return luaL_error(L, "Invalid matrix size: %dx%d (only 2x2, 3x3 and 4x4 matrices are supported).",
  169. count, count);
  170. float *values = new float[dimension * dimension * count];
  171. for (int i = 0; i < count; ++i)
  172. {
  173. lua_getfield(L, 3+i, "dimension");
  174. if (lua_tointeger(L, -1) != dimension)
  175. {
  176. // You unlock this door with the key of imagination. Beyond it is
  177. // another dimension: a dimension of sound, a dimension of sight,
  178. // a dimension of mind. You're moving into a land of both shadow
  179. // and substance, of things and ideas. You've just crossed over
  180. // into... the Twilight Zone.
  181. int other_dimension = lua_tointeger(L, -1);
  182. delete[] values;
  183. return luaL_error(L, "Invalid matrix size at argument %d: Expected size %dx%d, got %dx%d.",
  184. 3+i, dimension, dimension, other_dimension, other_dimension);
  185. }
  186. for (int k = 1; k <= dimension*dimension; ++k)
  187. {
  188. lua_rawgeti(L, 3+i, k);
  189. values[i * dimension * dimension + k - 1] = (float)lua_tonumber(L, -1);
  190. }
  191. lua_pop(L, 1 + dimension);
  192. }
  193. try
  194. {
  195. shader->sendMatrix(name, dimension, values, count);
  196. }
  197. catch(love::Exception &e)
  198. {
  199. delete[] values;
  200. return luaL_error(L, "%s", e.what());
  201. }
  202. delete[] values;
  203. return 0;
  204. }
  205. int w_Shader_sendImage(lua_State *L)
  206. {
  207. Shader *shader = luax_checkshader(L, 1);
  208. const char *name = luaL_checkstring(L, 2);
  209. Image *img = luax_checkimage(L, 3);
  210. try
  211. {
  212. shader->sendImage(name, *img);
  213. }
  214. catch(love::Exception &e)
  215. {
  216. luaL_error(L, "%s", e.what());
  217. }
  218. return 0;
  219. }
  220. int w_Shader_sendCanvas(lua_State *L)
  221. {
  222. Shader *shader = luax_checkshader(L, 1);
  223. const char *name = luaL_checkstring(L, 2);
  224. Canvas *canvas = luax_checkcanvas(L, 3);
  225. try
  226. {
  227. shader->sendCanvas(name, *canvas);
  228. }
  229. catch(love::Exception &e)
  230. {
  231. luaL_error(L, "%s", e.what());
  232. }
  233. return 0;
  234. }
  235. static const luaL_Reg functions[] =
  236. {
  237. { "getWarnings", w_Shader_getWarnings },
  238. { "sendInt", w_Shader_sendInt },
  239. { "sendFloat", w_Shader_sendFloat },
  240. { "sendMatrix", w_Shader_sendMatrix },
  241. { "sendImage", w_Shader_sendImage },
  242. { "sendCanvas", w_Shader_sendCanvas },
  243. { 0, 0 }
  244. };
  245. extern "C" int luaopen_shader(lua_State *L)
  246. {
  247. return luax_register_type(L, "Shader", functions);
  248. }
  249. } // opengl
  250. } // graphics
  251. } // love