Texture.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. // 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. , width(settings.width)
  138. , height(settings.height)
  139. , depth(settings.type == TEXTURE_VOLUME ? settings.layers : 1)
  140. , layers(settings.type == TEXTURE_2D_ARRAY ? settings.layers : 1)
  141. , mipmapCount(1)
  142. , pixelWidth(0)
  143. , pixelHeight(0)
  144. , requestedMSAA(settings.msaa > 1 ? settings.msaa : 0)
  145. , samplerState()
  146. , graphicsMemorySize(0)
  147. , debugName(settings.debugName)
  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 (isGammaCorrect() && !slice->isLinear())
  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);
  192. if (!isGammaCorrect() || settings.linear)
  193. format = getLinearPixelFormat(format);
  194. if (mipmapsMode == MIPMAPS_AUTO && isCompressed())
  195. mipmapsMode = MIPMAPS_MANUAL;
  196. if (mipmapsMode != MIPMAPS_NONE)
  197. {
  198. int totalMipmapCount = getTotalMipmapCount(pixelWidth, pixelHeight, depth);
  199. if (requestedMipmapCount > 0)
  200. mipmapCount = std::min(totalMipmapCount, requestedMipmapCount);
  201. else
  202. mipmapCount = totalMipmapCount;
  203. if (mipmapCount != totalMipmapCount && !caps.features[Graphics::FEATURE_MIPMAP_RANGE])
  204. 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);
  205. }
  206. const char *miperr = nullptr;
  207. if (mipmapsMode == MIPMAPS_AUTO && !supportsGenerateMipmaps(miperr))
  208. {
  209. const char *fstr = "unknown";
  210. love::getConstant(format, fstr);
  211. throw love::Exception("Automatic mipmap generation is not supported for textures with the %s pixel format.", fstr);
  212. }
  213. if (pixelWidth <= 0 || pixelHeight <= 0 || layers <= 0 || depth <= 0)
  214. throw love::Exception("Texture dimensions must be greater than 0.");
  215. if (texType != TEXTURE_2D && requestedMSAA > 1)
  216. throw love::Exception("MSAA is only supported for textures with the 2D texture type.");
  217. if (!renderTarget && requestedMSAA > 1)
  218. throw love::Exception("MSAA is only supported with render target textures.");
  219. if (readable && isPixelFormatDepthStencil(format) && settings.msaa > 1)
  220. throw love::Exception("Readable depth/stencil textures with MSAA are not currently supported.");
  221. if ((!readable || settings.msaa > 1) && mipmapsMode != MIPMAPS_NONE)
  222. throw love::Exception("Non-readable and MSAA textures cannot have mipmaps.");
  223. if (!readable && texType != TEXTURE_2D)
  224. throw love::Exception("Non-readable pixel formats are only supported for 2D texture types.");
  225. if (isCompressed() && renderTarget)
  226. throw love::Exception("Compressed textures cannot be render targets.");
  227. uint32 usage = PIXELFORMATUSAGEFLAGS_NONE;
  228. if (renderTarget)
  229. usage |= PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  230. if (readable)
  231. usage |= PIXELFORMATUSAGEFLAGS_SAMPLE;
  232. if (computeWrite)
  233. usage |= PIXELFORMATUSAGEFLAGS_COMPUTEWRITE;
  234. if (!gfx->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage))
  235. {
  236. const char *fstr = "unknown";
  237. love::getConstant(format, fstr);
  238. const char *readablestr = "";
  239. if (readable != !isPixelFormatDepthStencil(format))
  240. readablestr = readable ? " readable" : " non-readable";
  241. const char *rtstr = "";
  242. if (computeWrite)
  243. rtstr = " as a compute shader-writable texture";
  244. else if (renderTarget)
  245. rtstr = " as a render target";
  246. throw love::Exception("The %s%s pixel format is not supported%s on this system.", fstr, readablestr, rtstr);
  247. }
  248. if (!caps.textureTypes[texType])
  249. {
  250. const char *textypestr = "unknown";
  251. Texture::getConstant(texType, textypestr);
  252. throw love::Exception("%s textures are not supported on this system.", textypestr);
  253. }
  254. validateDimensions(true);
  255. samplerState = gfx->getDefaultSamplerState();
  256. if (getMipmapCount() == 1)
  257. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  258. Quad::Viewport v = {0, 0, (double) width, (double) height};
  259. quad.set(new Quad(v, width, height), Acquire::NORETAIN);
  260. ++textureCount;
  261. }
  262. Texture::~Texture()
  263. {
  264. --textureCount;
  265. setGraphicsMemorySize(0);
  266. }
  267. void Texture::setGraphicsMemorySize(int64 bytes)
  268. {
  269. totalGraphicsMemory = std::max(totalGraphicsMemory - graphicsMemorySize, (int64) 0);
  270. bytes = std::max(bytes, (int64) 0);
  271. graphicsMemorySize = bytes;
  272. totalGraphicsMemory += bytes;
  273. }
  274. void Texture::draw(Graphics *gfx, const Matrix4 &m)
  275. {
  276. draw(gfx, quad, m);
  277. }
  278. void Texture::draw(Graphics *gfx, Quad *q, const Matrix4 &localTransform)
  279. {
  280. if (!readable)
  281. throw love::Exception("Textures with non-readable formats cannot be drawn.");
  282. if (renderTarget && gfx->isRenderTargetActive(this))
  283. throw love::Exception("Cannot render a Texture to itself.");
  284. if (texType == TEXTURE_2D_ARRAY)
  285. {
  286. drawLayer(gfx, q->getLayer(), q, localTransform);
  287. return;
  288. }
  289. const Matrix4 &tm = gfx->getTransform();
  290. bool is2D = tm.isAffine2DTransform();
  291. Graphics::BatchedDrawCommand cmd;
  292. cmd.formats[0] = getSinglePositionFormat(is2D);
  293. cmd.formats[1] = CommonFormat::STf_RGBAub;
  294. cmd.indexMode = TRIANGLEINDEX_QUADS;
  295. cmd.vertexCount = 4;
  296. cmd.texture = this;
  297. Graphics::BatchedVertexData data = gfx->requestBatchedDraw(cmd);
  298. Matrix4 t(tm, localTransform);
  299. if (is2D)
  300. t.transformXY((Vector2 *) data.stream[0], q->getVertexPositions(), 4);
  301. else
  302. t.transformXY0((Vector3 *) data.stream[0], q->getVertexPositions(), 4);
  303. const Vector2 *texcoords = q->getVertexTexCoords();
  304. STf_RGBAub *vertexdata = (STf_RGBAub *) data.stream[1];
  305. Color32 c = toColor32(gfx->getColor());
  306. for (int i = 0; i < 4; i++)
  307. {
  308. vertexdata[i].s = texcoords[i].x;
  309. vertexdata[i].t = texcoords[i].y;
  310. vertexdata[i].color = c;
  311. }
  312. }
  313. void Texture::drawLayer(Graphics *gfx, int layer, const Matrix4 &m)
  314. {
  315. drawLayer(gfx, layer, quad, m);
  316. }
  317. void Texture::drawLayer(Graphics *gfx, int layer, Quad *q, const Matrix4 &m)
  318. {
  319. if (!readable)
  320. throw love::Exception("Textures with non-readable formats cannot be drawn.");
  321. if (renderTarget && gfx->isRenderTargetActive(this, layer))
  322. throw love::Exception("Cannot render a Texture to itself.");
  323. if (texType != TEXTURE_2D_ARRAY)
  324. throw love::Exception("drawLayer can only be used with Array Textures.");
  325. if (layer < 0 || layer >= layers)
  326. throw love::Exception("Invalid layer: %d (Texture has %d layers)", layer + 1, layers);
  327. Color32 c = toColor32(gfx->getColor());
  328. const Matrix4 &tm = gfx->getTransform();
  329. bool is2D = tm.isAffine2DTransform();
  330. Matrix4 t(tm, m);
  331. Graphics::BatchedDrawCommand cmd;
  332. cmd.formats[0] = getSinglePositionFormat(is2D);
  333. cmd.formats[1] = CommonFormat::STPf_RGBAub;
  334. cmd.indexMode = TRIANGLEINDEX_QUADS;
  335. cmd.vertexCount = 4;
  336. cmd.texture = this;
  337. cmd.standardShaderType = Shader::STANDARD_ARRAY;
  338. Graphics::BatchedVertexData data = gfx->requestBatchedDraw(cmd);
  339. if (is2D)
  340. t.transformXY((Vector2 *) data.stream[0], q->getVertexPositions(), 4);
  341. else
  342. t.transformXY0((Vector3 *) data.stream[0], q->getVertexPositions(), 4);
  343. const Vector2 *texcoords = q->getVertexTexCoords();
  344. STPf_RGBAub *vertexdata = (STPf_RGBAub *) data.stream[1];
  345. for (int i = 0; i < 4; i++)
  346. {
  347. vertexdata[i].s = texcoords[i].x;
  348. vertexdata[i].t = texcoords[i].y;
  349. vertexdata[i].p = (float) layer;
  350. vertexdata[i].color = c;
  351. }
  352. }
  353. void Texture::uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y)
  354. {
  355. Rect rect = {x, y, d->getWidth(), d->getHeight()};
  356. uploadByteData(d->getData(), d->getSize(), level, slice, rect);
  357. }
  358. void Texture::replacePixels(love::image::ImageDataBase *d, int slice, int mipmap, int x, int y, bool reloadmipmaps)
  359. {
  360. if (!isReadable())
  361. throw love::Exception("replacePixels can only be called on readable Textures.");
  362. if (getMSAA() > 1)
  363. throw love::Exception("replacePixels cannot be called on a MSAA Texture.");
  364. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  365. if (gfx != nullptr && gfx->isRenderTargetActive(this))
  366. throw love::Exception("replacePixels cannot be called on this Texture while it's an active render target.");
  367. // No effect if the texture hasn't been created yet.
  368. if (getHandle() == 0)
  369. return;
  370. // ImageData format might be linear but intended to be used as sRGB, so we
  371. // don't error if only the sRGBness is different.
  372. if (getLinearPixelFormat(d->getFormat()) != getLinearPixelFormat(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(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() && !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. bool linear = firstdata->isLinear();
  702. if (textureType == TEXTURE_CUBE && w != h)
  703. throw love::Exception("Cube textures must have equal widths and heights for each cube face.");
  704. int mipw = w;
  705. int miph = h;
  706. int mipslices = slicecount;
  707. for (int mip = 0; mip < mipcount; mip++)
  708. {
  709. if (textureType == TEXTURE_VOLUME)
  710. {
  711. slicecount = getSliceCount(mip);
  712. if (slicecount != mipslices)
  713. throw love::Exception("Invalid number of image data layers in mipmap level %d (expected %d, got %d)", mip+1, mipslices, slicecount);
  714. }
  715. for (int slice = 0; slice < slicecount; slice++)
  716. {
  717. auto slicedata = get(slice, mip);
  718. if (slicedata == nullptr)
  719. throw love::Exception("Missing image data (slice %d, mipmap level %d)", slice+1, mip+1);
  720. int realw = slicedata->getWidth();
  721. int realh = slicedata->getHeight();
  722. if (getMipmapCount(slice) != mipcount)
  723. throw love::Exception("All texture layers must have the same mipmap count.");
  724. if (mipw != realw)
  725. throw love::Exception("Width of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, mipw, realw);
  726. if (miph != realh)
  727. throw love::Exception("Height of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, miph, realh);
  728. if (format != slicedata->getFormat())
  729. throw love::Exception("All texture slices and mipmaps must have the same pixel format.");
  730. if (linear != slicedata->isLinear())
  731. throw love::Exception("All texture slices and mipmaps must have the same linear setting.");
  732. }
  733. mipw = std::max(mipw / 2, 1);
  734. miph = std::max(miph / 2, 1);
  735. if (textureType == TEXTURE_VOLUME)
  736. mipslices = std::max(mipslices / 2, 1);
  737. }
  738. return true;
  739. }
  740. static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[] =
  741. {
  742. { "2d", TEXTURE_2D },
  743. { "volume", TEXTURE_VOLUME },
  744. { "array", TEXTURE_2D_ARRAY },
  745. { "cube", TEXTURE_CUBE },
  746. };
  747. static StringMap<TextureType, TEXTURE_MAX_ENUM> texTypes(texTypeEntries, sizeof(texTypeEntries));
  748. static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM>::Entry mipmapEntries[] =
  749. {
  750. { "none", Texture::MIPMAPS_NONE },
  751. { "manual", Texture::MIPMAPS_MANUAL },
  752. { "auto", Texture::MIPMAPS_AUTO },
  753. };
  754. static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM> mipmapModes(mipmapEntries, sizeof(mipmapEntries));
  755. static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM>::Entry settingTypeEntries[] =
  756. {
  757. { "width", Texture::SETTING_WIDTH },
  758. { "height", Texture::SETTING_HEIGHT },
  759. { "layers", Texture::SETTING_LAYERS },
  760. { "mipmaps", Texture::SETTING_MIPMAPS },
  761. { "mipmapcount", Texture::SETTING_MIPMAP_COUNT },
  762. { "format", Texture::SETTING_FORMAT },
  763. { "linear", Texture::SETTING_LINEAR },
  764. { "type", Texture::SETTING_TYPE },
  765. { "dpiscale", Texture::SETTING_DPI_SCALE },
  766. { "msaa", Texture::SETTING_MSAA },
  767. { "canvas", Texture::SETTING_RENDER_TARGET },
  768. { "computewrite", Texture::SETTING_COMPUTE_WRITE },
  769. { "readable", Texture::SETTING_READABLE },
  770. { "debugname", Texture::SETTING_DEBUGNAME },
  771. };
  772. static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM> settingTypes(settingTypeEntries, sizeof(settingTypeEntries));
  773. bool Texture::getConstant(const char *in, TextureType &out)
  774. {
  775. return texTypes.find(in, out);
  776. }
  777. bool Texture::getConstant(TextureType in, const char *&out)
  778. {
  779. return texTypes.find(in, out);
  780. }
  781. std::vector<std::string> Texture::getConstants(TextureType)
  782. {
  783. return texTypes.getNames();
  784. }
  785. bool Texture::getConstant(const char *in, MipmapsMode &out)
  786. {
  787. return mipmapModes.find(in, out);
  788. }
  789. bool Texture::getConstant(MipmapsMode in, const char *&out)
  790. {
  791. return mipmapModes.find(in, out);
  792. }
  793. std::vector<std::string> Texture::getConstants(MipmapsMode)
  794. {
  795. return mipmapModes.getNames();
  796. }
  797. bool Texture::getConstant(const char *in, SettingType &out)
  798. {
  799. return settingTypes.find(in, out);
  800. }
  801. bool Texture::getConstant(SettingType in, const char *&out)
  802. {
  803. return settingTypes.find(in, out);
  804. }
  805. const char *Texture::getConstant(SettingType in)
  806. {
  807. const char *name = nullptr;
  808. getConstant(in, name);
  809. return name;
  810. }
  811. std::vector<std::string> Texture::getConstants(SettingType)
  812. {
  813. return settingTypes.getNames();
  814. }
  815. } // graphics
  816. } // love