wrap_Texture.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /**
  2. * Copyright (c) 2006-2024 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_Texture.h"
  21. #include "Graphics.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. Texture *luax_checktexture(lua_State *L, int idx)
  27. {
  28. return luax_checktype<Texture>(L, idx);
  29. }
  30. int w_Texture_getTextureType(lua_State *L)
  31. {
  32. Texture *t = luax_checktexture(L, 1);
  33. const char *tstr;
  34. if (!Texture::getConstant(t->getTextureType(), tstr))
  35. return luax_enumerror(L, "texture type", Texture::getConstants(TEXTURE_MAX_ENUM), tstr);
  36. lua_pushstring(L, tstr);
  37. return 1;
  38. }
  39. static int w__optMipmap(lua_State *L, Texture *t, int idx)
  40. {
  41. int mipmap = 0;
  42. if (!lua_isnoneornil(L, idx))
  43. {
  44. mipmap = (int) luaL_checkinteger(L, idx) - 1;
  45. if (mipmap < 0 || mipmap >= t->getMipmapCount())
  46. luaL_error(L, "Invalid mipmap index: %d", mipmap + 1);
  47. }
  48. return mipmap;
  49. }
  50. int w_Texture_getWidth(lua_State *L)
  51. {
  52. Texture *t = luax_checktexture(L, 1);
  53. lua_pushnumber(L, t->getWidth(w__optMipmap(L, t, 2)));
  54. return 1;
  55. }
  56. int w_Texture_getHeight(lua_State *L)
  57. {
  58. Texture *t = luax_checktexture(L, 1);
  59. lua_pushnumber(L, t->getHeight(w__optMipmap(L, t, 2)));
  60. return 1;
  61. }
  62. int w_Texture_getDimensions(lua_State *L)
  63. {
  64. Texture *t = luax_checktexture(L, 1);
  65. int mipmap = w__optMipmap(L, t, 2);
  66. lua_pushnumber(L, t->getWidth(mipmap));
  67. lua_pushnumber(L, t->getHeight(mipmap));
  68. return 2;
  69. }
  70. int w_Texture_getDepth(lua_State *L)
  71. {
  72. Texture *t = luax_checktexture(L, 1);
  73. lua_pushnumber(L, t->getDepth(w__optMipmap(L, t, 2)));
  74. return 1;
  75. }
  76. int w_Texture_getLayerCount(lua_State *L)
  77. {
  78. Texture *t = luax_checktexture(L, 1);
  79. lua_pushnumber(L, t->getLayerCount());
  80. return 1;
  81. }
  82. int w_Texture_getMipmapCount(lua_State *L)
  83. {
  84. Texture *t = luax_checktexture(L, 1);
  85. lua_pushnumber(L, t->getMipmapCount());
  86. return 1;
  87. }
  88. int w_Texture_getPixelWidth(lua_State *L)
  89. {
  90. Texture *t = luax_checktexture(L, 1);
  91. lua_pushnumber(L, t->getPixelWidth(w__optMipmap(L, t, 2)));
  92. return 1;
  93. }
  94. int w_Texture_getPixelHeight(lua_State *L)
  95. {
  96. Texture *t = luax_checktexture(L, 1);
  97. lua_pushnumber(L, t->getPixelHeight(w__optMipmap(L, t, 2)));
  98. return 1;
  99. }
  100. int w_Texture_getPixelDimensions(lua_State *L)
  101. {
  102. Texture *t = luax_checktexture(L, 1);
  103. int mipmap = w__optMipmap(L, t, 2);
  104. lua_pushnumber(L, t->getPixelWidth(mipmap));
  105. lua_pushnumber(L, t->getPixelHeight(mipmap));
  106. return 2;
  107. }
  108. int w_Texture_getDPIScale(lua_State *L)
  109. {
  110. Texture *t = luax_checktexture(L, 1);
  111. lua_pushnumber(L, t->getDPIScale());
  112. return 1;
  113. }
  114. int w_Texture_isFormatLinear(lua_State *L)
  115. {
  116. Texture *t = luax_checktexture(L, 1);
  117. luax_pushboolean(L, t->isFormatLinear());
  118. return 1;
  119. }
  120. int w_Texture_isCompressed(lua_State *L)
  121. {
  122. Texture *t = luax_checktexture(L, 1);
  123. luax_pushboolean(L, t->isCompressed());
  124. return 1;
  125. }
  126. int w_Texture_getMSAA(lua_State *L)
  127. {
  128. Texture *t = luax_checktexture(L, 1);
  129. lua_pushinteger(L, t->getMSAA());
  130. return 1;
  131. }
  132. int w_Texture_setFilter(lua_State *L)
  133. {
  134. Texture *t = luax_checktexture(L, 1);
  135. SamplerState s = t->getSamplerState();
  136. const char *minstr = luaL_checkstring(L, 2);
  137. const char *magstr = luaL_optstring(L, 3, minstr);
  138. if (!SamplerState::getConstant(minstr, s.minFilter))
  139. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.minFilter), minstr);
  140. if (!SamplerState::getConstant(magstr, s.magFilter))
  141. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.magFilter), magstr);
  142. s.maxAnisotropy = std::min(std::max(1, (int) luaL_optnumber(L, 4, 1.0)), LOVE_UINT8_MAX);
  143. luax_catchexcept(L, [&](){ t->setSamplerState(s); });
  144. return 0;
  145. }
  146. int w_Texture_getFilter(lua_State *L)
  147. {
  148. Texture *t = luax_checktexture(L, 1);
  149. const SamplerState &s = t->getSamplerState();
  150. const char *minstr = nullptr;
  151. const char *magstr = nullptr;
  152. if (!SamplerState::getConstant(s.minFilter, minstr))
  153. return luaL_error(L, "Unknown filter mode.");
  154. if (!SamplerState::getConstant(s.magFilter, magstr))
  155. return luaL_error(L, "Unknown filter mode.");
  156. lua_pushstring(L, minstr);
  157. lua_pushstring(L, magstr);
  158. lua_pushnumber(L, s.maxAnisotropy);
  159. return 3;
  160. }
  161. int w_Texture_setMipmapFilter(lua_State *L)
  162. {
  163. Texture *t = luax_checktexture(L, 1);
  164. SamplerState s = t->getSamplerState();
  165. // Mipmapping is disabled if no argument is given.
  166. if (lua_isnoneornil(L, 2))
  167. s.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  168. else
  169. {
  170. const char *mipmapstr = luaL_checkstring(L, 2);
  171. if (!SamplerState::getConstant(mipmapstr, s.mipmapFilter))
  172. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.mipmapFilter), mipmapstr);
  173. }
  174. s.lodBias = -((float) luaL_optnumber(L, 3, 0.0));
  175. luax_catchexcept(L, [&](){ t->setSamplerState(s); });
  176. return 0;
  177. }
  178. int w_Texture_getMipmapFilter(lua_State *L)
  179. {
  180. Texture *t = luax_checktexture(L, 1);
  181. const SamplerState &s = t->getSamplerState();
  182. const char *mipmapstr;
  183. if (SamplerState::getConstant(s.mipmapFilter, mipmapstr))
  184. lua_pushstring(L, mipmapstr);
  185. else
  186. lua_pushnil(L); // only return a mipmap filter if mipmapping is enabled
  187. lua_pushnumber(L, -s.lodBias);
  188. return 2;
  189. }
  190. int w_Texture_setWrap(lua_State *L)
  191. {
  192. Texture *t = luax_checktexture(L, 1);
  193. SamplerState s = t->getSamplerState();
  194. const char *sstr = luaL_checkstring(L, 2);
  195. const char *tstr = luaL_optstring(L, 3, sstr);
  196. const char *rstr = luaL_optstring(L, 4, sstr);
  197. if (!SamplerState::getConstant(sstr, s.wrapU))
  198. return luax_enumerror(L, "wrap mode", SamplerState::getConstants(s.wrapU), sstr);
  199. if (!SamplerState::getConstant(tstr, s.wrapV))
  200. return luax_enumerror(L, "wrap mode", SamplerState::getConstants(s.wrapV), tstr);
  201. if (!SamplerState::getConstant(rstr, s.wrapW))
  202. return luax_enumerror(L, "wrap mode", SamplerState::getConstants(s.wrapW), rstr);
  203. luax_catchexcept(L, [&](){ t->setSamplerState(s); });
  204. return 0;
  205. }
  206. int w_Texture_getWrap(lua_State *L)
  207. {
  208. Texture *t = luax_checktexture(L, 1);
  209. const SamplerState &s = t->getSamplerState();
  210. const char *sstr = nullptr;
  211. const char *tstr = nullptr;
  212. const char *rstr = nullptr;
  213. if (!SamplerState::getConstant(s.wrapU, sstr))
  214. return luaL_error(L, "Unknown wrap mode.");
  215. if (!SamplerState::getConstant(s.wrapV, tstr))
  216. return luaL_error(L, "Unknown wrap mode.");
  217. if (!SamplerState::getConstant(s.wrapW, rstr))
  218. return luaL_error(L, "Unknown wrap mode.");
  219. lua_pushstring(L, sstr);
  220. lua_pushstring(L, tstr);
  221. lua_pushstring(L, rstr);
  222. return 3;
  223. }
  224. int w_Texture_getFormat(lua_State *L)
  225. {
  226. Texture *t = luax_checktexture(L, 1);
  227. PixelFormat format = t->getPixelFormat();
  228. const char *str;
  229. if (!getConstant(format, str))
  230. return luaL_error(L, "Unknown pixel format.");
  231. lua_pushstring(L, str);
  232. return 1;
  233. }
  234. int w_Texture_isCanvas(lua_State *L)
  235. {
  236. Texture *t = luax_checktexture(L, 1);
  237. luax_pushboolean(L, t->isRenderTarget());
  238. return 1;
  239. }
  240. int w_Texture_isComputeWritable(lua_State *L)
  241. {
  242. Texture *t = luax_checktexture(L, 1);
  243. luax_pushboolean(L, t->isComputeWritable());
  244. return 1;
  245. }
  246. int w_Texture_isReadable(lua_State *L)
  247. {
  248. Texture *t = luax_checktexture(L, 1);
  249. luax_pushboolean(L, t->isReadable());
  250. return 1;
  251. }
  252. int w_Texture_getViewFormats(lua_State *L)
  253. {
  254. Texture *t = luax_checktexture(L, 1);
  255. const std::vector<PixelFormat> &viewformats = t->getViewFormats();
  256. lua_createtable(L, (int) viewformats.size(), 0);
  257. for (int i = 0; i < (int) viewformats.size(); i++)
  258. {
  259. const char *str = nullptr;
  260. if (!getConstant(viewformats[i], str))
  261. return luaL_error(L, "Unknown pixel format.");
  262. lua_pushstring(L, str);
  263. lua_rawseti(L, -2, i + 1);
  264. }
  265. return 1;
  266. }
  267. int w_Texture_setDepthSampleMode(lua_State *L)
  268. {
  269. Texture *t = luax_checktexture(L, 1);
  270. SamplerState s = t->getSamplerState();
  271. s.depthSampleMode.hasValue = false;
  272. if (!lua_isnoneornil(L, 2))
  273. {
  274. const char *str = luaL_checkstring(L, 2);
  275. s.depthSampleMode.hasValue = true;
  276. if (!getConstant(str, s.depthSampleMode.value))
  277. return luax_enumerror(L, "compare mode", getConstants(s.depthSampleMode.value), str);
  278. }
  279. luax_catchexcept(L, [&](){ t->setSamplerState(s); });
  280. return 0;
  281. }
  282. int w_Texture_getDepthSampleMode(lua_State *L)
  283. {
  284. Texture *t = luax_checktexture(L, 1);
  285. const SamplerState &s = t->getSamplerState();
  286. if (s.depthSampleMode.hasValue)
  287. {
  288. const char *str = nullptr;
  289. if (!getConstant(s.depthSampleMode.value, str))
  290. return luaL_error(L, "Unknown compare mode.");
  291. lua_pushstring(L, str);
  292. }
  293. else
  294. lua_pushnil(L);
  295. return 1;
  296. }
  297. int w_Texture_getMipmapMode(lua_State *L)
  298. {
  299. Texture *t = luax_checktexture(L, 1);
  300. const char *str;
  301. if (!Texture::getConstant(t->getMipmapsMode(), str))
  302. return luax_enumerror(L, "mipmap mode", Texture::getConstants(Texture::MIPMAPS_MAX_ENUM), str);
  303. lua_pushstring(L, str);
  304. return 1;
  305. }
  306. int w_Texture_generateMipmaps(lua_State *L)
  307. {
  308. Texture *t = luax_checktexture(L, 1);
  309. luax_catchexcept(L, [&]() { t->generateMipmaps(); });
  310. return 0;
  311. }
  312. int w_Texture_replacePixels(lua_State *L)
  313. {
  314. Texture *t = luax_checktexture(L, 1);
  315. love::image::ImageData *id = nullptr;
  316. love::image::CompressedImageData *cid = nullptr;
  317. if (luax_istype(L, 2, love::image::CompressedImageData::type))
  318. cid = luax_checktype<love::image::CompressedImageData>(L, 2);
  319. else
  320. id = luax_checktype<love::image::ImageData>(L, 2);
  321. int slice = 0;
  322. int dstmip = 0;
  323. int x = 0;
  324. int y = 0;
  325. bool reloadmipmaps = t->getMipmapsMode() == Texture::MIPMAPS_AUTO;
  326. if (t->getTextureType() != TEXTURE_2D)
  327. slice = (int) luaL_checkinteger(L, 3) - 1;
  328. dstmip = (int) luaL_optinteger(L, 4, 1) - 1;
  329. if (!lua_isnoneornil(L, 5))
  330. {
  331. x = (int) luaL_checkinteger(L, 5);
  332. y = (int) luaL_checkinteger(L, 6);
  333. if (reloadmipmaps)
  334. reloadmipmaps = luax_optboolean(L, 7, reloadmipmaps);
  335. }
  336. if (cid != nullptr)
  337. {
  338. int srcmip = 0;
  339. if (cid->getMipmapCount() > 1)
  340. srcmip = (int) luaL_checkinteger(L, 8) - 1;
  341. if (srcmip < 0 || srcmip >= cid->getMipmapCount())
  342. return luaL_error(L, "Invalid source mipmap level.");
  343. luax_catchexcept(L, [&](){ t->replacePixels(cid->getSlice(0, srcmip), slice, dstmip, x, y, reloadmipmaps); });
  344. }
  345. else
  346. {
  347. luax_catchexcept(L, [&](){ t->replacePixels(id, slice, dstmip, x, y, reloadmipmaps); });
  348. }
  349. return 0;
  350. }
  351. int w_Texture_newImageData(lua_State *L)
  352. {
  353. luax_markdeprecated(L, 1, "Texture:newImageData", API_METHOD, DEPRECATED_RENAMED, "love.graphics.readbackTexture");
  354. Texture *t = luax_checktexture(L, 1);
  355. int slice = 0;
  356. if (t->getTextureType() != TEXTURE_2D)
  357. slice = (int) luaL_checkinteger(L, 2) - 1;
  358. int mipmap = (int) luaL_optinteger(L, 3, 1) - 1;
  359. Rect rect = {0, 0, t->getPixelWidth(mipmap), t->getPixelHeight(mipmap)};
  360. if (!lua_isnoneornil(L, 4))
  361. {
  362. rect.x = (int) luaL_checkinteger(L, 4);
  363. rect.y = (int) luaL_checkinteger(L, 5);
  364. rect.w = (int) luaL_checkinteger(L, 6);
  365. rect.h = (int) luaL_checkinteger(L, 7);
  366. }
  367. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  368. if (gfx == nullptr)
  369. return luaL_error(L, "Cannot find Graphics module.");
  370. love::image::ImageData *img = nullptr;
  371. luax_catchexcept(L, [&](){ img = gfx->readbackTexture(t, slice, mipmap, rect, nullptr, 0, 0); });
  372. luax_pushtype(L, img);
  373. img->release();
  374. return 1;
  375. }
  376. int w_Texture_renderTo(lua_State *L)
  377. {
  378. Graphics::RenderTarget rt(luax_checktexture(L, 1));
  379. int args = lua_gettop(L);
  380. int startidx = 2;
  381. if (rt.texture->getTextureType() != TEXTURE_2D)
  382. {
  383. rt.slice = (int) luaL_checkinteger(L, 2) - 1;
  384. startidx++;
  385. }
  386. luaL_checktype(L, startidx, LUA_TFUNCTION);
  387. auto graphics = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  388. if (graphics)
  389. {
  390. // Save the current render targets so we can restore them when we're done.
  391. Graphics::RenderTargets oldtargets = graphics->getRenderTargets();
  392. for (auto c : oldtargets.colors)
  393. c.texture->retain();
  394. if (oldtargets.depthStencil.texture != nullptr)
  395. oldtargets.depthStencil.texture->retain();
  396. luax_catchexcept(L,
  397. [&]() { graphics->setRenderTarget(rt, 0); },
  398. [&](bool err)
  399. {
  400. if (err)
  401. {
  402. for (auto c : oldtargets.colors)
  403. c.texture->release();
  404. }
  405. }
  406. );
  407. int status = lua_pcall(L, args - startidx, 0, 0);
  408. graphics->setRenderTargets(oldtargets);
  409. for (auto c : oldtargets.colors)
  410. c.texture->release();
  411. if (oldtargets.depthStencil.texture != nullptr)
  412. oldtargets.depthStencil.texture->release();
  413. if (status != 0)
  414. return lua_error(L);
  415. }
  416. return 0;
  417. }
  418. static int w_Texture_getDebugName(lua_State *L)
  419. {
  420. Texture *t = luax_checktexture(L, 1);
  421. const std::string &debugName = t->getDebugName();
  422. if (debugName.empty())
  423. lua_pushnil(L);
  424. else
  425. luax_pushstring(L, debugName);
  426. return 1;
  427. }
  428. const luaL_Reg w_Texture_functions[] =
  429. {
  430. { "getTextureType", w_Texture_getTextureType },
  431. { "getWidth", w_Texture_getWidth },
  432. { "getHeight", w_Texture_getHeight },
  433. { "getDimensions", w_Texture_getDimensions },
  434. { "getDepth", w_Texture_getDepth },
  435. { "getLayerCount", w_Texture_getLayerCount },
  436. { "getMipmapCount", w_Texture_getMipmapCount },
  437. { "getPixelWidth", w_Texture_getPixelWidth },
  438. { "getPixelHeight", w_Texture_getPixelHeight },
  439. { "getPixelDimensions", w_Texture_getPixelDimensions },
  440. { "getDPIScale", w_Texture_getDPIScale },
  441. { "isFormatLinear", w_Texture_isFormatLinear },
  442. { "isCompressed", w_Texture_isCompressed },
  443. { "getMSAA", w_Texture_getMSAA },
  444. { "setFilter", w_Texture_setFilter },
  445. { "getFilter", w_Texture_getFilter },
  446. { "setMipmapFilter", w_Texture_setMipmapFilter },
  447. { "getMipmapFilter", w_Texture_getMipmapFilter },
  448. { "setWrap", w_Texture_setWrap },
  449. { "getWrap", w_Texture_getWrap },
  450. { "getFormat", w_Texture_getFormat },
  451. { "isCanvas", w_Texture_isCanvas },
  452. { "isComputeWritable", w_Texture_isComputeWritable },
  453. { "isReadable", w_Texture_isReadable },
  454. { "getViewFormats", w_Texture_getViewFormats },
  455. { "getMipmapMode", w_Texture_getMipmapMode },
  456. { "getDepthSampleMode", w_Texture_getDepthSampleMode },
  457. { "setDepthSampleMode", w_Texture_setDepthSampleMode },
  458. { "generateMipmaps", w_Texture_generateMipmaps },
  459. { "replacePixels", w_Texture_replacePixels },
  460. { "renderTo", w_Texture_renderTo },
  461. { "getDebugName", w_Texture_getDebugName },
  462. // Deprecated
  463. { "newImageData", w_Texture_newImageData },
  464. { 0, 0 }
  465. };
  466. extern "C" int luaopen_texture(lua_State *L)
  467. {
  468. return luax_register_type(L, &Texture::type, w_Texture_functions, nullptr);
  469. }
  470. } // graphics
  471. } // love