Texture.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /**
  2. * Copyright (c) 2006-2023 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. // LOVE
  21. #include "common/config.h"
  22. #include "Texture.h"
  23. #include "Graphics.h"
  24. // C
  25. #include <cmath>
  26. #include <algorithm>
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. uint64 SamplerState::toKey() const
  32. {
  33. union { float f; uint32 i; } conv;
  34. conv.f = lodBias;
  35. const uint32 BITS_4 = 0xF;
  36. return (minFilter << 0) | (magFilter << 1) | (mipmapFilter << 2)
  37. | (wrapU << 4) | (wrapV << 7) | (wrapW << 10)
  38. | (maxAnisotropy << 13) | ((BITS_4 & minLod) << 17) | ((BITS_4 & maxLod) << 21)
  39. | (depthSampleMode.hasValue << 25) | (depthSampleMode.value << 26)
  40. | ((uint64)conv.i << 32);
  41. }
  42. SamplerState SamplerState::fromKey(uint64 key)
  43. {
  44. const uint32 BITS_1 = 0x1;
  45. const uint32 BITS_2 = 0x3;
  46. const uint32 BITS_3 = 0x7;
  47. const uint32 BITS_4 = 0xF;
  48. SamplerState s;
  49. s.minFilter = (FilterMode) ((key >> 0) & BITS_1);
  50. s.magFilter = (FilterMode) ((key >> 1) & BITS_1);
  51. s.mipmapFilter = (MipmapFilterMode) ((key >> 2) & BITS_2);
  52. s.wrapU = (WrapMode) ((key >> 4 ) & BITS_3);
  53. s.wrapV = (WrapMode) ((key >> 7 ) & BITS_3);
  54. s.wrapW = (WrapMode) ((key >> 10) & BITS_3);
  55. s.maxAnisotropy = (key >> 13) & BITS_4;
  56. s.minLod = (key >> 17) & BITS_4;
  57. s.maxLod = (key >> 21) & BITS_4;
  58. s.depthSampleMode.hasValue = ((key >> 25) & BITS_1) != 0;
  59. s.depthSampleMode.value = (CompareMode) ((key >> 26) & BITS_4);
  60. union { float f; uint32 i; } conv;
  61. conv.i = (uint32) (key >> 32);
  62. s.lodBias = conv.f;
  63. return s;
  64. }
  65. bool SamplerState::isClampZeroOrOne(WrapMode w)
  66. {
  67. return w == WRAP_CLAMP_ONE || w == WRAP_CLAMP_ZERO;
  68. }
  69. static StringMap<SamplerState::FilterMode, SamplerState::FILTER_MAX_ENUM>::Entry filterModeEntries[] =
  70. {
  71. { "linear", SamplerState::FILTER_LINEAR },
  72. { "nearest", SamplerState::FILTER_NEAREST },
  73. };
  74. static StringMap<SamplerState::FilterMode, SamplerState::FILTER_MAX_ENUM> filterModes(filterModeEntries, sizeof(filterModeEntries));
  75. static StringMap<SamplerState::MipmapFilterMode, SamplerState::MIPMAP_FILTER_MAX_ENUM>::Entry mipmapFilterModeEntries[] =
  76. {
  77. { "none", SamplerState::MIPMAP_FILTER_NONE },
  78. { "linear", SamplerState::MIPMAP_FILTER_LINEAR },
  79. { "nearest", SamplerState::MIPMAP_FILTER_NEAREST },
  80. };
  81. static StringMap<SamplerState::MipmapFilterMode, SamplerState::MIPMAP_FILTER_MAX_ENUM> mipmapFilterModes(mipmapFilterModeEntries, sizeof(mipmapFilterModeEntries));
  82. static StringMap<SamplerState::WrapMode, SamplerState::WRAP_MAX_ENUM>::Entry wrapModeEntries[] =
  83. {
  84. { "clamp", SamplerState::WRAP_CLAMP },
  85. { "clampzero", SamplerState::WRAP_CLAMP_ZERO },
  86. { "clampone", SamplerState::WRAP_CLAMP_ONE },
  87. { "repeat", SamplerState::WRAP_REPEAT },
  88. { "mirroredrepeat", SamplerState::WRAP_MIRRORED_REPEAT },
  89. };
  90. static StringMap<SamplerState::WrapMode, SamplerState::WRAP_MAX_ENUM> wrapModes(wrapModeEntries, sizeof(wrapModeEntries));
  91. bool SamplerState::getConstant(const char *in, FilterMode &out)
  92. {
  93. return filterModes.find(in, out);
  94. }
  95. bool SamplerState::getConstant(FilterMode in, const char *&out)
  96. {
  97. return filterModes.find(in, out);
  98. }
  99. std::vector<std::string> SamplerState::getConstants(FilterMode)
  100. {
  101. return filterModes.getNames();
  102. }
  103. bool SamplerState::getConstant(const char *in, MipmapFilterMode &out)
  104. {
  105. return mipmapFilterModes.find(in, out);
  106. }
  107. bool SamplerState::getConstant(MipmapFilterMode in, const char *&out)
  108. {
  109. return mipmapFilterModes.find(in, out);
  110. }
  111. std::vector<std::string> SamplerState::getConstants(MipmapFilterMode)
  112. {
  113. return mipmapFilterModes.getNames();
  114. }
  115. bool SamplerState::getConstant(const char *in, WrapMode &out)
  116. {
  117. return wrapModes.find(in, out);
  118. }
  119. bool SamplerState::getConstant(WrapMode in, const char *&out)
  120. {
  121. return wrapModes.find(in, out);
  122. }
  123. std::vector<std::string> SamplerState::getConstants(WrapMode)
  124. {
  125. return wrapModes.getNames();
  126. }
  127. love::Type Texture::type("Texture", &Drawable::type);
  128. int Texture::textureCount = 0;
  129. int64 Texture::totalGraphicsMemory = 0;
  130. Texture::Texture(Graphics *gfx, const Settings &settings, const Slices *slices)
  131. : texType(settings.type)
  132. , format(settings.format)
  133. , renderTarget(settings.renderTarget)
  134. , computeWrite(settings.computeWrite)
  135. , readable(true)
  136. , mipmapsMode(settings.mipmaps)
  137. , sRGB(isGammaCorrect() && !settings.linear)
  138. , width(settings.width)
  139. , height(settings.height)
  140. , depth(settings.type == TEXTURE_VOLUME ? settings.layers : 1)
  141. , layers(settings.type == TEXTURE_2D_ARRAY ? settings.layers : 1)
  142. , mipmapCount(1)
  143. , pixelWidth(0)
  144. , pixelHeight(0)
  145. , requestedMSAA(settings.msaa > 1 ? settings.msaa : 0)
  146. , samplerState()
  147. , graphicsMemorySize(0)
  148. {
  149. const auto &caps = gfx->getCapabilities();
  150. int requestedMipmapCount = settings.mipmapCount;
  151. if (slices != nullptr && slices->getMipmapCount() > 0 && slices->getSliceCount() > 0)
  152. {
  153. texType = slices->getTextureType();
  154. if (requestedMSAA > 1)
  155. throw love::Exception("MSAA textures cannot be created from image data.");
  156. int dataMipmaps = 1;
  157. if (slices->validate() && slices->getMipmapCount() > 1)
  158. {
  159. dataMipmaps = slices->getMipmapCount();
  160. if (requestedMipmapCount > 0)
  161. requestedMipmapCount = std::min(requestedMipmapCount, dataMipmaps);
  162. else
  163. requestedMipmapCount = dataMipmaps;
  164. }
  165. love::image::ImageDataBase *slice = slices->get(0, 0);
  166. format = slice->getFormat();
  167. if (sRGB)
  168. format = getSRGBPixelFormat(format);
  169. pixelWidth = slice->getWidth();
  170. pixelHeight = slice->getHeight();
  171. if (texType == TEXTURE_2D_ARRAY)
  172. layers = slices->getSliceCount();
  173. else if (texType == TEXTURE_VOLUME)
  174. depth = slices->getSliceCount();
  175. width = (int) (pixelWidth / settings.dpiScale + 0.5);
  176. height = (int) (pixelHeight / settings.dpiScale + 0.5);
  177. if (isCompressed() && dataMipmaps <= 1)
  178. mipmapsMode = MIPMAPS_NONE;
  179. }
  180. else
  181. {
  182. if (isCompressed())
  183. throw love::Exception("Compressed textures must be created with initial data.");
  184. pixelWidth = (int) ((width * settings.dpiScale) + 0.5);
  185. pixelHeight = (int) ((height * settings.dpiScale) + 0.5);
  186. }
  187. if (settings.readable.hasValue)
  188. readable = settings.readable.value;
  189. else
  190. readable = !renderTarget || !isPixelFormatDepthStencil(format);
  191. format = gfx->getSizedFormat(format, renderTarget, readable);
  192. if (mipmapsMode == MIPMAPS_AUTO && isCompressed())
  193. mipmapsMode = MIPMAPS_MANUAL;
  194. if (mipmapsMode != MIPMAPS_NONE)
  195. {
  196. int totalMipmapCount = getTotalMipmapCount(pixelWidth, pixelHeight, depth);
  197. if (requestedMipmapCount > 0)
  198. mipmapCount = std::min(totalMipmapCount, requestedMipmapCount);
  199. else
  200. mipmapCount = totalMipmapCount;
  201. if (mipmapCount != totalMipmapCount && !caps.features[Graphics::FEATURE_MIPMAP_RANGE])
  202. throw love::Exception("Custom mipmap ranges for a texture are not supported on this system (%d mipmap levels are required but only %d levels were provided.)", totalMipmapCount, mipmapCount);
  203. }
  204. const char *miperr = nullptr;
  205. if (mipmapsMode == MIPMAPS_AUTO && !supportsGenerateMipmaps(miperr))
  206. {
  207. const char *fstr = "unknown";
  208. love::getConstant(format, fstr);
  209. throw love::Exception("Automatic mipmap generation is not supported for textures with the %s pixel format.", fstr);
  210. }
  211. if (pixelWidth <= 0 || pixelHeight <= 0 || layers <= 0 || depth <= 0)
  212. throw love::Exception("Texture dimensions must be greater than 0.");
  213. if (texType != TEXTURE_2D && requestedMSAA > 1)
  214. throw love::Exception("MSAA is only supported for textures with the 2D texture type.");
  215. if (!renderTarget && requestedMSAA > 1)
  216. throw love::Exception("MSAA is only supported with render target textures.");
  217. if (readable && isPixelFormatDepthStencil(format) && settings.msaa > 1)
  218. throw love::Exception("Readable depth/stencil textures with MSAA are not currently supported.");
  219. if ((!readable || settings.msaa > 1) && mipmapsMode != MIPMAPS_NONE)
  220. throw love::Exception("Non-readable and MSAA textures cannot have mipmaps.");
  221. if (!readable && texType != TEXTURE_2D)
  222. throw love::Exception("Non-readable pixel formats are only supported for 2D texture types.");
  223. if (isCompressed() && renderTarget)
  224. throw love::Exception("Compressed textures cannot be render targets.");
  225. uint32 usage = PIXELFORMATUSAGEFLAGS_NONE;
  226. if (renderTarget)
  227. usage |= PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  228. if (readable)
  229. usage |= PIXELFORMATUSAGEFLAGS_SAMPLE;
  230. if (computeWrite)
  231. usage |= PIXELFORMATUSAGEFLAGS_COMPUTEWRITE;
  232. if (!gfx->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage, sRGB))
  233. {
  234. const char *fstr = "unknown";
  235. love::getConstant(format, fstr);
  236. const char *readablestr = "";
  237. if (readable != !isPixelFormatDepthStencil(format))
  238. readablestr = readable ? " readable" : " non-readable";
  239. const char *rtstr = "";
  240. if (computeWrite)
  241. rtstr = " as a compute shader-writable texture";
  242. else if (renderTarget)
  243. rtstr = " as a render target";
  244. throw love::Exception("The %s%s pixel format is not supported%s on this system.", fstr, readablestr, rtstr);
  245. }
  246. if (!caps.textureTypes[texType])
  247. {
  248. const char *textypestr = "unknown";
  249. Texture::getConstant(texType, textypestr);
  250. throw love::Exception("%s textures are not supported on this system.", textypestr);
  251. }
  252. validateDimensions(true);
  253. samplerState = gfx->getDefaultSamplerState();
  254. if (getMipmapCount() == 1)
  255. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  256. Quad::Viewport v = {0, 0, (double) width, (double) height};
  257. quad.set(new Quad(v, width, height), Acquire::NORETAIN);
  258. ++textureCount;
  259. }
  260. Texture::~Texture()
  261. {
  262. --textureCount;
  263. setGraphicsMemorySize(0);
  264. }
  265. void Texture::setGraphicsMemorySize(int64 bytes)
  266. {
  267. totalGraphicsMemory = std::max(totalGraphicsMemory - graphicsMemorySize, (int64) 0);
  268. bytes = std::max(bytes, (int64) 0);
  269. graphicsMemorySize = bytes;
  270. totalGraphicsMemory += bytes;
  271. }
  272. void Texture::draw(Graphics *gfx, const Matrix4 &m)
  273. {
  274. draw(gfx, quad, m);
  275. }
  276. void Texture::draw(Graphics *gfx, Quad *q, const Matrix4 &localTransform)
  277. {
  278. if (!readable)
  279. throw love::Exception("Textures with non-readable formats cannot be drawn.");
  280. if (renderTarget && gfx->isRenderTargetActive(this))
  281. throw love::Exception("Cannot render a Texture to itself.");
  282. if (texType == TEXTURE_2D_ARRAY)
  283. {
  284. drawLayer(gfx, q->getLayer(), q, localTransform);
  285. return;
  286. }
  287. const Matrix4 &tm = gfx->getTransform();
  288. bool is2D = tm.isAffine2DTransform();
  289. Graphics::BatchedDrawCommand cmd;
  290. cmd.formats[0] = getSinglePositionFormat(is2D);
  291. cmd.formats[1] = CommonFormat::STf_RGBAub;
  292. cmd.indexMode = TRIANGLEINDEX_QUADS;
  293. cmd.vertexCount = 4;
  294. cmd.texture = this;
  295. Graphics::BatchedVertexData data = gfx->requestBatchedDraw(cmd);
  296. Matrix4 t(tm, localTransform);
  297. if (is2D)
  298. t.transformXY((Vector2 *) data.stream[0], q->getVertexPositions(), 4);
  299. else
  300. t.transformXY0((Vector3 *) data.stream[0], q->getVertexPositions(), 4);
  301. const Vector2 *texcoords = q->getVertexTexCoords();
  302. STf_RGBAub *vertexdata = (STf_RGBAub *) data.stream[1];
  303. Color32 c = toColor32(gfx->getColor());
  304. for (int i = 0; i < 4; i++)
  305. {
  306. vertexdata[i].s = texcoords[i].x;
  307. vertexdata[i].t = texcoords[i].y;
  308. vertexdata[i].color = c;
  309. }
  310. }
  311. void Texture::drawLayer(Graphics *gfx, int layer, const Matrix4 &m)
  312. {
  313. drawLayer(gfx, layer, quad, m);
  314. }
  315. void Texture::drawLayer(Graphics *gfx, int layer, Quad *q, const Matrix4 &m)
  316. {
  317. if (!readable)
  318. throw love::Exception("Textures with non-readable formats cannot be drawn.");
  319. if (renderTarget && gfx->isRenderTargetActive(this, layer))
  320. throw love::Exception("Cannot render a Texture to itself.");
  321. if (texType != TEXTURE_2D_ARRAY)
  322. throw love::Exception("drawLayer can only be used with Array Textures.");
  323. if (layer < 0 || layer >= layers)
  324. throw love::Exception("Invalid layer: %d (Texture has %d layers)", layer + 1, layers);
  325. Color32 c = toColor32(gfx->getColor());
  326. const Matrix4 &tm = gfx->getTransform();
  327. bool is2D = tm.isAffine2DTransform();
  328. Matrix4 t(tm, m);
  329. Graphics::BatchedDrawCommand cmd;
  330. cmd.formats[0] = getSinglePositionFormat(is2D);
  331. cmd.formats[1] = CommonFormat::STPf_RGBAub;
  332. cmd.indexMode = TRIANGLEINDEX_QUADS;
  333. cmd.vertexCount = 4;
  334. cmd.texture = this;
  335. cmd.standardShaderType = Shader::STANDARD_ARRAY;
  336. Graphics::BatchedVertexData data = gfx->requestBatchedDraw(cmd);
  337. if (is2D)
  338. t.transformXY((Vector2 *) data.stream[0], q->getVertexPositions(), 4);
  339. else
  340. t.transformXY0((Vector3 *) data.stream[0], q->getVertexPositions(), 4);
  341. const Vector2 *texcoords = q->getVertexTexCoords();
  342. STPf_RGBAub *vertexdata = (STPf_RGBAub *) data.stream[1];
  343. for (int i = 0; i < 4; i++)
  344. {
  345. vertexdata[i].s = texcoords[i].x;
  346. vertexdata[i].t = texcoords[i].y;
  347. vertexdata[i].p = (float) layer;
  348. vertexdata[i].color = c;
  349. }
  350. }
  351. void Texture::uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y)
  352. {
  353. love::image::ImageData *id = dynamic_cast<love::image::ImageData *>(d);
  354. love::thread::EmptyLock lock;
  355. if (id != nullptr)
  356. lock.setLock(id->getMutex());
  357. Rect rect = {x, y, d->getWidth(), d->getHeight()};
  358. uploadByteData(d->getFormat(), d->getData(), d->getSize(), level, slice, rect);
  359. }
  360. void Texture::replacePixels(love::image::ImageDataBase *d, int slice, int mipmap, int x, int y, bool reloadmipmaps)
  361. {
  362. if (!isReadable())
  363. throw love::Exception("replacePixels can only be called on readable Textures.");
  364. if (getMSAA() > 1)
  365. throw love::Exception("replacePixels cannot be called on a MSAA Texture.");
  366. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  367. if (gfx != nullptr && gfx->isRenderTargetActive(this))
  368. throw love::Exception("replacePixels cannot be called on this Texture while it's an active render target.");
  369. // No effect if the texture hasn't been created yet.
  370. if (getHandle() == 0)
  371. return;
  372. if (d->getFormat() != getPixelFormat())
  373. throw love::Exception("Pixel formats must match.");
  374. if (mipmap < 0 || mipmap >= getMipmapCount())
  375. throw love::Exception("Invalid texture mipmap index %d.", mipmap + 1);
  376. if (slice < 0 || (texType == TEXTURE_CUBE && slice >= 6)
  377. || (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap))
  378. || (texType == TEXTURE_2D_ARRAY && slice >= getLayerCount()))
  379. {
  380. throw love::Exception("Invalid texture slice index %d.", slice + 1);
  381. }
  382. Rect rect = {x, y, d->getWidth(), d->getHeight()};
  383. int mipw = getPixelWidth(mipmap);
  384. int miph = getPixelHeight(mipmap);
  385. if (rect.x < 0 || rect.y < 0 || rect.w <= 0 || rect.h <= 0
  386. || (rect.x + rect.w) > mipw || (rect.y + rect.h) > miph)
  387. {
  388. throw love::Exception("Invalid rectangle dimensions (x=%d, y=%d, w=%d, h=%d) for %dx%d Texture.", rect.x, rect.y, rect.w, rect.h, mipw, miph);
  389. }
  390. if (isPixelFormatCompressed(d->getFormat()) && (rect.x != 0 || rect.y != 0 || rect.w != mipw || rect.h != miph))
  391. {
  392. const PixelFormatInfo &info = getPixelFormatInfo(d->getFormat());
  393. int bw = (int) info.blockWidth;
  394. int bh = (int) info.blockHeight;
  395. if (rect.x % bw != 0 || rect.y % bh != 0 || rect.w % bw != 0 || rect.h % bh != 0)
  396. {
  397. const char *name = nullptr;
  398. love::getConstant(d->getFormat(), name);
  399. throw love::Exception("Compressed texture format %s only supports replacing a sub-rectangle with offset and dimensions that are a multiple of %d x %d.", name, bw, bh);
  400. }
  401. }
  402. Graphics::flushBatchedDrawsGlobal();
  403. uploadImageData(d, mipmap, slice, x, y);
  404. if (reloadmipmaps && mipmap == 0 && getMipmapCount() > 1)
  405. generateMipmaps();
  406. }
  407. void Texture::replacePixels(const void *data, size_t size, int slice, int mipmap, const Rect &rect, bool reloadmipmaps)
  408. {
  409. if (!isReadable() || getMSAA() > 1)
  410. return;
  411. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  412. if (gfx != nullptr && gfx->isRenderTargetActive(this))
  413. return;
  414. Graphics::flushBatchedDrawsGlobal();
  415. uploadByteData(format, data, size, mipmap, slice, rect);
  416. if (reloadmipmaps && mipmap == 0 && getMipmapCount() > 1)
  417. generateMipmaps();
  418. }
  419. bool Texture::supportsGenerateMipmaps(const char *&outReason) const
  420. {
  421. if (getMipmapsMode() == MIPMAPS_NONE)
  422. {
  423. outReason = "generateMipmaps can only be called on a Texture which was created with mipmaps enabled.";
  424. return false;
  425. }
  426. if (isPixelFormatCompressed(format))
  427. {
  428. outReason = "generateMipmaps cannot be called on a compressed Texture.";
  429. return false;
  430. }
  431. if (isPixelFormatDepthStencil(format))
  432. {
  433. outReason = "generateMipmaps cannot be called on a depth/stencil Texture.";
  434. return false;
  435. }
  436. if (isPixelFormatInteger(format))
  437. {
  438. outReason = "generateMipmaps cannot be called on an integer Texture.";
  439. return false;
  440. }
  441. // This should be linear | rt because that's what metal needs, but the above
  442. // code handles textures can't be used as RTs in metal.
  443. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  444. if (gfx != nullptr && !gfx->isPixelFormatSupported(format, PIXELFORMATUSAGEFLAGS_LINEAR))
  445. {
  446. outReason = "generateMipmaps cannot be called on textures with formats that don't support linear filtering on this system.";
  447. return false;
  448. }
  449. return true;
  450. }
  451. void Texture::generateMipmaps()
  452. {
  453. const char *err = nullptr;
  454. if (!supportsGenerateMipmaps(err))
  455. throw love::Exception("%s", err);
  456. generateMipmapsInternal();
  457. }
  458. TextureType Texture::getTextureType() const
  459. {
  460. return texType;
  461. }
  462. PixelFormat Texture::getPixelFormat() const
  463. {
  464. return format;
  465. }
  466. Texture::MipmapsMode Texture::getMipmapsMode() const
  467. {
  468. return mipmapsMode;
  469. }
  470. bool Texture::isRenderTarget() const
  471. {
  472. return renderTarget;
  473. }
  474. bool Texture::isComputeWritable() const
  475. {
  476. return computeWrite;
  477. }
  478. bool Texture::isReadable() const
  479. {
  480. return readable;
  481. }
  482. bool Texture::isCompressed() const
  483. {
  484. return isPixelFormatCompressed(format);
  485. }
  486. bool Texture::isFormatLinear() const
  487. {
  488. return isGammaCorrect() && !sRGB && !isPixelFormatSRGB(format);
  489. }
  490. bool Texture::isValidSlice(int slice, int mip) const
  491. {
  492. return slice >= 0 && slice < getSliceCount(mip);
  493. }
  494. int Texture::getSliceCount(int mip) const
  495. {
  496. if (texType == TEXTURE_2D)
  497. return 1;
  498. else if (texType == TEXTURE_CUBE)
  499. return 6;
  500. else if (texType == TEXTURE_2D_ARRAY)
  501. return layers;
  502. else if (texType == TEXTURE_VOLUME)
  503. return getDepth(mip);
  504. return 1;
  505. }
  506. int Texture::getWidth(int mip) const
  507. {
  508. return std::max(width >> mip, 1);
  509. }
  510. int Texture::getHeight(int mip) const
  511. {
  512. return std::max(height >> mip, 1);
  513. }
  514. int Texture::getDepth(int mip) const
  515. {
  516. return std::max(depth >> mip, 1);
  517. }
  518. int Texture::getLayerCount() const
  519. {
  520. return layers;
  521. }
  522. int Texture::getMipmapCount() const
  523. {
  524. return mipmapCount;
  525. }
  526. int Texture::getPixelWidth(int mip) const
  527. {
  528. return std::max(pixelWidth >> mip, 1);
  529. }
  530. int Texture::getPixelHeight(int mip) const
  531. {
  532. return std::max(pixelHeight >> mip, 1);
  533. }
  534. float Texture::getDPIScale() const
  535. {
  536. return (float) pixelHeight / (float) height;
  537. }
  538. int Texture::getRequestedMSAA() const
  539. {
  540. return requestedMSAA;
  541. }
  542. void Texture::setSamplerState(const SamplerState &s)
  543. {
  544. if (!readable)
  545. return;
  546. if (s.depthSampleMode.hasValue && !isPixelFormatDepth(format))
  547. throw love::Exception("Only depth textures can have a depth sample compare mode.");
  548. Graphics::flushBatchedDrawsGlobal();
  549. samplerState = s;
  550. if (samplerState.mipmapFilter != SamplerState::MIPMAP_FILTER_NONE && getMipmapCount() == 1)
  551. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  552. if (texType == TEXTURE_CUBE)
  553. samplerState.wrapU = samplerState.wrapV = samplerState.wrapW = SamplerState::WRAP_CLAMP;
  554. }
  555. const SamplerState &Texture::getSamplerState() const
  556. {
  557. return samplerState;
  558. }
  559. Quad *Texture::getQuad() const
  560. {
  561. return quad;
  562. }
  563. int Texture::getTotalMipmapCount(int w, int h)
  564. {
  565. return (int) log2(std::max(w, h)) + 1;
  566. }
  567. int Texture::getTotalMipmapCount(int w, int h, int d)
  568. {
  569. return (int) log2(std::max(std::max(w, h), d)) + 1;
  570. }
  571. bool Texture::validateDimensions(bool throwException) const
  572. {
  573. bool success = true;
  574. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  575. if (gfx == nullptr)
  576. return false;
  577. const Graphics::Capabilities &caps = gfx->getCapabilities();
  578. int max2Dsize = (int) caps.limits[Graphics::LIMIT_TEXTURE_SIZE];
  579. int max3Dsize = (int) caps.limits[Graphics::LIMIT_VOLUME_TEXTURE_SIZE];
  580. int maxcubesize = (int) caps.limits[Graphics::LIMIT_CUBE_TEXTURE_SIZE];
  581. int maxlayers = (int) caps.limits[Graphics::LIMIT_TEXTURE_LAYERS];
  582. int largestdim = 0;
  583. const char *largestname = nullptr;
  584. if ((texType == TEXTURE_2D || texType == TEXTURE_2D_ARRAY) && (pixelWidth > max2Dsize || pixelHeight > max2Dsize))
  585. {
  586. success = false;
  587. largestdim = std::max(pixelWidth, pixelHeight);
  588. largestname = pixelWidth > pixelHeight ? "pixel width" : "pixel height";
  589. }
  590. else if (texType == TEXTURE_2D_ARRAY && layers > maxlayers)
  591. {
  592. success = false;
  593. largestdim = layers;
  594. largestname = "array layer count";
  595. }
  596. else if (texType == TEXTURE_CUBE && (pixelWidth > maxcubesize || pixelWidth != pixelHeight))
  597. {
  598. success = false;
  599. largestdim = std::max(pixelWidth, pixelHeight);
  600. largestname = pixelWidth > pixelHeight ? "pixel width" : "pixel height";
  601. if (throwException && pixelWidth != pixelHeight)
  602. throw love::Exception("Cubemap textures must have equal width and height.");
  603. }
  604. else if (texType == TEXTURE_VOLUME && (pixelWidth > max3Dsize || pixelHeight > max3Dsize || depth > max3Dsize))
  605. {
  606. success = false;
  607. largestdim = std::max(std::max(pixelWidth, pixelHeight), depth);
  608. if (largestdim == pixelWidth)
  609. largestname = "pixel width";
  610. else if (largestdim == pixelHeight)
  611. largestname = "pixel height";
  612. else
  613. largestname = "pixel depth";
  614. }
  615. if (throwException && largestname != nullptr)
  616. throw love::Exception("Cannot create texture: %s of %d is too large for this system.", largestname, largestdim);
  617. return success;
  618. }
  619. Texture::Slices::Slices(TextureType textype)
  620. : textureType(textype)
  621. {
  622. }
  623. void Texture::Slices::clear()
  624. {
  625. data.clear();
  626. }
  627. void Texture::Slices::set(int slice, int mipmap, love::image::ImageDataBase *d)
  628. {
  629. if (textureType == TEXTURE_VOLUME)
  630. {
  631. if (mipmap >= (int) data.size())
  632. data.resize(mipmap + 1);
  633. if (slice >= (int) data[mipmap].size())
  634. data[mipmap].resize(slice + 1);
  635. data[mipmap][slice].set(d);
  636. }
  637. else
  638. {
  639. if (slice >= (int) data.size())
  640. data.resize(slice + 1);
  641. if (mipmap >= (int) data[slice].size())
  642. data[slice].resize(mipmap + 1);
  643. data[slice][mipmap].set(d);
  644. }
  645. }
  646. love::image::ImageDataBase *Texture::Slices::get(int slice, int mipmap) const
  647. {
  648. if (slice < 0 || slice >= getSliceCount(mipmap))
  649. return nullptr;
  650. if (mipmap < 0 || mipmap >= getMipmapCount(slice))
  651. return nullptr;
  652. if (textureType == TEXTURE_VOLUME)
  653. return data[mipmap][slice].get();
  654. else
  655. return data[slice][mipmap].get();
  656. }
  657. void Texture::Slices::add(love::image::CompressedImageData *cdata, int startslice, int startmip, bool addallslices, bool addallmips)
  658. {
  659. int slicecount = addallslices ? cdata->getSliceCount() : 1;
  660. int mipcount = addallmips ? cdata->getMipmapCount() : 1;
  661. for (int mip = 0; mip < mipcount; mip++)
  662. {
  663. for (int slice = 0; slice < slicecount; slice++)
  664. set(startslice + slice, startmip + mip, cdata->getSlice(slice, mip));
  665. }
  666. }
  667. int Texture::Slices::getSliceCount(int mip) const
  668. {
  669. if (textureType == TEXTURE_VOLUME)
  670. {
  671. if (mip < 0 || mip >= (int) data.size())
  672. return 0;
  673. return (int) data[mip].size();
  674. }
  675. else
  676. return (int) data.size();
  677. }
  678. int Texture::Slices::getMipmapCount(int slice) const
  679. {
  680. if (textureType == TEXTURE_VOLUME)
  681. return (int) data.size();
  682. else
  683. {
  684. if (slice < 0 || slice >= (int) data.size())
  685. return 0;
  686. return data[slice].size();
  687. }
  688. }
  689. bool Texture::Slices::validate() const
  690. {
  691. int slicecount = getSliceCount();
  692. int mipcount = getMipmapCount(0);
  693. if (slicecount == 0 || mipcount == 0)
  694. throw love::Exception("At least one ImageData or CompressedImageData is required.");
  695. if (textureType == TEXTURE_CUBE && slicecount != 6)
  696. throw love::Exception("Cube textures must have exactly 6 sides.");
  697. image::ImageDataBase *firstdata = get(0, 0);
  698. int w = firstdata->getWidth();
  699. int h = firstdata->getHeight();
  700. PixelFormat format = firstdata->getFormat();
  701. if (textureType == TEXTURE_CUBE && w != h)
  702. throw love::Exception("Cube textures must have equal widths and heights for each cube face.");
  703. int mipw = w;
  704. int miph = h;
  705. int mipslices = slicecount;
  706. for (int mip = 0; mip < mipcount; mip++)
  707. {
  708. if (textureType == TEXTURE_VOLUME)
  709. {
  710. slicecount = getSliceCount(mip);
  711. if (slicecount != mipslices)
  712. throw love::Exception("Invalid number of image data layers in mipmap level %d (expected %d, got %d)", mip+1, mipslices, slicecount);
  713. }
  714. for (int slice = 0; slice < slicecount; slice++)
  715. {
  716. auto slicedata = get(slice, mip);
  717. if (slicedata == nullptr)
  718. throw love::Exception("Missing image data (slice %d, mipmap level %d)", slice+1, mip+1);
  719. int realw = slicedata->getWidth();
  720. int realh = slicedata->getHeight();
  721. if (getMipmapCount(slice) != mipcount)
  722. throw love::Exception("All texture layers must have the same mipmap count.");
  723. if (mipw != realw)
  724. throw love::Exception("Width of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, mipw, realw);
  725. if (miph != realh)
  726. throw love::Exception("Height of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, miph, realh);
  727. if (format != slicedata->getFormat())
  728. throw love::Exception("All texture slices and mipmaps must have the same pixel format.");
  729. }
  730. mipw = std::max(mipw / 2, 1);
  731. miph = std::max(miph / 2, 1);
  732. if (textureType == TEXTURE_VOLUME)
  733. mipslices = std::max(mipslices / 2, 1);
  734. }
  735. return true;
  736. }
  737. static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[] =
  738. {
  739. { "2d", TEXTURE_2D },
  740. { "volume", TEXTURE_VOLUME },
  741. { "array", TEXTURE_2D_ARRAY },
  742. { "cube", TEXTURE_CUBE },
  743. };
  744. static StringMap<TextureType, TEXTURE_MAX_ENUM> texTypes(texTypeEntries, sizeof(texTypeEntries));
  745. static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM>::Entry mipmapEntries[] =
  746. {
  747. { "none", Texture::MIPMAPS_NONE },
  748. { "manual", Texture::MIPMAPS_MANUAL },
  749. { "auto", Texture::MIPMAPS_AUTO },
  750. };
  751. static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM> mipmapModes(mipmapEntries, sizeof(mipmapEntries));
  752. static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM>::Entry settingTypeEntries[] =
  753. {
  754. { "width", Texture::SETTING_WIDTH },
  755. { "height", Texture::SETTING_HEIGHT },
  756. { "layers", Texture::SETTING_LAYERS },
  757. { "mipmaps", Texture::SETTING_MIPMAPS },
  758. { "mipmapcount", Texture::SETTING_MIPMAP_COUNT },
  759. { "format", Texture::SETTING_FORMAT },
  760. { "linear", Texture::SETTING_LINEAR },
  761. { "type", Texture::SETTING_TYPE },
  762. { "dpiscale", Texture::SETTING_DPI_SCALE },
  763. { "msaa", Texture::SETTING_MSAA },
  764. { "canvas", Texture::SETTING_RENDER_TARGET },
  765. { "computewrite", Texture::SETTING_COMPUTE_WRITE },
  766. { "readable", Texture::SETTING_READABLE },
  767. };
  768. static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM> settingTypes(settingTypeEntries, sizeof(settingTypeEntries));
  769. bool Texture::getConstant(const char *in, TextureType &out)
  770. {
  771. return texTypes.find(in, out);
  772. }
  773. bool Texture::getConstant(TextureType in, const char *&out)
  774. {
  775. return texTypes.find(in, out);
  776. }
  777. std::vector<std::string> Texture::getConstants(TextureType)
  778. {
  779. return texTypes.getNames();
  780. }
  781. bool Texture::getConstant(const char *in, MipmapsMode &out)
  782. {
  783. return mipmapModes.find(in, out);
  784. }
  785. bool Texture::getConstant(MipmapsMode in, const char *&out)
  786. {
  787. return mipmapModes.find(in, out);
  788. }
  789. std::vector<std::string> Texture::getConstants(MipmapsMode)
  790. {
  791. return mipmapModes.getNames();
  792. }
  793. bool Texture::getConstant(const char *in, SettingType &out)
  794. {
  795. return settingTypes.find(in, out);
  796. }
  797. bool Texture::getConstant(SettingType in, const char *&out)
  798. {
  799. return settingTypes.find(in, out);
  800. }
  801. const char *Texture::getConstant(SettingType in)
  802. {
  803. const char *name = nullptr;
  804. getConstant(in, name);
  805. return name;
  806. }
  807. std::vector<std::string> Texture::getConstants(SettingType)
  808. {
  809. return settingTypes.getNames();
  810. }
  811. } // graphics
  812. } // love