Texture.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. love::image::ImageData *id = dynamic_cast<love::image::ImageData *>(d);
  356. love::thread::EmptyLock lock;
  357. if (id != nullptr)
  358. lock.setLock(id->getMutex());
  359. Rect rect = {x, y, d->getWidth(), d->getHeight()};
  360. uploadByteData(format, d->getData(), d->getSize(), level, slice, rect);
  361. }
  362. void Texture::replacePixels(love::image::ImageDataBase *d, int slice, int mipmap, int x, int y, bool reloadmipmaps)
  363. {
  364. if (!isReadable())
  365. throw love::Exception("replacePixels can only be called on readable Textures.");
  366. if (getMSAA() > 1)
  367. throw love::Exception("replacePixels cannot be called on a MSAA Texture.");
  368. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  369. if (gfx != nullptr && gfx->isRenderTargetActive(this))
  370. throw love::Exception("replacePixels cannot be called on this Texture while it's an active render target.");
  371. // No effect if the texture hasn't been created yet.
  372. if (getHandle() == 0)
  373. return;
  374. if (d->getFormat() != getPixelFormat())
  375. throw love::Exception("Pixel formats must match.");
  376. if (mipmap < 0 || mipmap >= getMipmapCount())
  377. throw love::Exception("Invalid texture mipmap index %d.", mipmap + 1);
  378. if (slice < 0 || (texType == TEXTURE_CUBE && slice >= 6)
  379. || (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap))
  380. || (texType == TEXTURE_2D_ARRAY && slice >= getLayerCount()))
  381. {
  382. throw love::Exception("Invalid texture slice index %d.", slice + 1);
  383. }
  384. Rect rect = {x, y, d->getWidth(), d->getHeight()};
  385. int mipw = getPixelWidth(mipmap);
  386. int miph = getPixelHeight(mipmap);
  387. if (rect.x < 0 || rect.y < 0 || rect.w <= 0 || rect.h <= 0
  388. || (rect.x + rect.w) > mipw || (rect.y + rect.h) > miph)
  389. {
  390. 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);
  391. }
  392. if (isPixelFormatCompressed(d->getFormat()) && (rect.x != 0 || rect.y != 0 || rect.w != mipw || rect.h != miph))
  393. {
  394. const PixelFormatInfo &info = getPixelFormatInfo(d->getFormat());
  395. int bw = (int) info.blockWidth;
  396. int bh = (int) info.blockHeight;
  397. if (rect.x % bw != 0 || rect.y % bh != 0 || rect.w % bw != 0 || rect.h % bh != 0)
  398. {
  399. const char *name = nullptr;
  400. love::getConstant(d->getFormat(), name);
  401. 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);
  402. }
  403. }
  404. Graphics::flushBatchedDrawsGlobal();
  405. uploadImageData(d, mipmap, slice, x, y);
  406. if (reloadmipmaps && mipmap == 0 && getMipmapCount() > 1)
  407. generateMipmaps();
  408. }
  409. void Texture::replacePixels(const void *data, size_t size, int slice, int mipmap, const Rect &rect, bool reloadmipmaps)
  410. {
  411. if (!isReadable() || getMSAA() > 1)
  412. return;
  413. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  414. if (gfx != nullptr && gfx->isRenderTargetActive(this))
  415. return;
  416. Graphics::flushBatchedDrawsGlobal();
  417. uploadByteData(format, data, size, mipmap, slice, rect);
  418. if (reloadmipmaps && mipmap == 0 && getMipmapCount() > 1)
  419. generateMipmaps();
  420. }
  421. bool Texture::supportsGenerateMipmaps(const char *&outReason) const
  422. {
  423. if (getMipmapsMode() == MIPMAPS_NONE)
  424. {
  425. outReason = "generateMipmaps can only be called on a Texture which was created with mipmaps enabled.";
  426. return false;
  427. }
  428. if (isPixelFormatCompressed(format))
  429. {
  430. outReason = "generateMipmaps cannot be called on a compressed Texture.";
  431. return false;
  432. }
  433. if (isPixelFormatDepthStencil(format))
  434. {
  435. outReason = "generateMipmaps cannot be called on a depth/stencil Texture.";
  436. return false;
  437. }
  438. if (isPixelFormatInteger(format))
  439. {
  440. outReason = "generateMipmaps cannot be called on an integer Texture.";
  441. return false;
  442. }
  443. // This should be linear | rt because that's what metal needs, but the above
  444. // code handles textures can't be used as RTs in metal.
  445. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  446. if (gfx != nullptr && !gfx->isPixelFormatSupported(format, PIXELFORMATUSAGEFLAGS_LINEAR))
  447. {
  448. outReason = "generateMipmaps cannot be called on textures with formats that don't support linear filtering on this system.";
  449. return false;
  450. }
  451. return true;
  452. }
  453. void Texture::generateMipmaps()
  454. {
  455. const char *err = nullptr;
  456. if (!supportsGenerateMipmaps(err))
  457. throw love::Exception("%s", err);
  458. generateMipmapsInternal();
  459. }
  460. TextureType Texture::getTextureType() const
  461. {
  462. return texType;
  463. }
  464. PixelFormat Texture::getPixelFormat() const
  465. {
  466. return format;
  467. }
  468. Texture::MipmapsMode Texture::getMipmapsMode() const
  469. {
  470. return mipmapsMode;
  471. }
  472. bool Texture::isRenderTarget() const
  473. {
  474. return renderTarget;
  475. }
  476. bool Texture::isComputeWritable() const
  477. {
  478. return computeWrite;
  479. }
  480. bool Texture::isReadable() const
  481. {
  482. return readable;
  483. }
  484. bool Texture::isCompressed() const
  485. {
  486. return isPixelFormatCompressed(format);
  487. }
  488. bool Texture::isFormatLinear() const
  489. {
  490. return isGammaCorrect() && !isPixelFormatSRGB(format);
  491. }
  492. bool Texture::isValidSlice(int slice, int mip) const
  493. {
  494. return slice >= 0 && slice < getSliceCount(mip);
  495. }
  496. int Texture::getSliceCount(int mip) const
  497. {
  498. if (texType == TEXTURE_2D)
  499. return 1;
  500. else if (texType == TEXTURE_CUBE)
  501. return 6;
  502. else if (texType == TEXTURE_2D_ARRAY)
  503. return layers;
  504. else if (texType == TEXTURE_VOLUME)
  505. return getDepth(mip);
  506. return 1;
  507. }
  508. int Texture::getWidth(int mip) const
  509. {
  510. return std::max(width >> mip, 1);
  511. }
  512. int Texture::getHeight(int mip) const
  513. {
  514. return std::max(height >> mip, 1);
  515. }
  516. int Texture::getDepth(int mip) const
  517. {
  518. return std::max(depth >> mip, 1);
  519. }
  520. int Texture::getLayerCount() const
  521. {
  522. return layers;
  523. }
  524. int Texture::getMipmapCount() const
  525. {
  526. return mipmapCount;
  527. }
  528. int Texture::getPixelWidth(int mip) const
  529. {
  530. return std::max(pixelWidth >> mip, 1);
  531. }
  532. int Texture::getPixelHeight(int mip) const
  533. {
  534. return std::max(pixelHeight >> mip, 1);
  535. }
  536. float Texture::getDPIScale() const
  537. {
  538. return (float) pixelHeight / (float) height;
  539. }
  540. int Texture::getRequestedMSAA() const
  541. {
  542. return requestedMSAA;
  543. }
  544. void Texture::setSamplerState(const SamplerState &s)
  545. {
  546. if (!readable)
  547. return;
  548. if (s.depthSampleMode.hasValue && !isPixelFormatDepth(format))
  549. throw love::Exception("Only depth textures can have a depth sample compare mode.");
  550. Graphics::flushBatchedDrawsGlobal();
  551. samplerState = s;
  552. if (samplerState.mipmapFilter != SamplerState::MIPMAP_FILTER_NONE && getMipmapCount() == 1)
  553. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  554. if (texType == TEXTURE_CUBE)
  555. samplerState.wrapU = samplerState.wrapV = samplerState.wrapW = SamplerState::WRAP_CLAMP;
  556. }
  557. const SamplerState &Texture::getSamplerState() const
  558. {
  559. return samplerState;
  560. }
  561. Quad *Texture::getQuad() const
  562. {
  563. return quad;
  564. }
  565. int Texture::getTotalMipmapCount(int w, int h)
  566. {
  567. return (int) log2(std::max(w, h)) + 1;
  568. }
  569. int Texture::getTotalMipmapCount(int w, int h, int d)
  570. {
  571. return (int) log2(std::max(std::max(w, h), d)) + 1;
  572. }
  573. bool Texture::validateDimensions(bool throwException) const
  574. {
  575. bool success = true;
  576. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  577. if (gfx == nullptr)
  578. return false;
  579. const Graphics::Capabilities &caps = gfx->getCapabilities();
  580. int max2Dsize = (int) caps.limits[Graphics::LIMIT_TEXTURE_SIZE];
  581. int max3Dsize = (int) caps.limits[Graphics::LIMIT_VOLUME_TEXTURE_SIZE];
  582. int maxcubesize = (int) caps.limits[Graphics::LIMIT_CUBE_TEXTURE_SIZE];
  583. int maxlayers = (int) caps.limits[Graphics::LIMIT_TEXTURE_LAYERS];
  584. int largestdim = 0;
  585. const char *largestname = nullptr;
  586. if ((texType == TEXTURE_2D || texType == TEXTURE_2D_ARRAY) && (pixelWidth > max2Dsize || pixelHeight > max2Dsize))
  587. {
  588. success = false;
  589. largestdim = std::max(pixelWidth, pixelHeight);
  590. largestname = pixelWidth > pixelHeight ? "pixel width" : "pixel height";
  591. }
  592. else if (texType == TEXTURE_2D_ARRAY && layers > maxlayers)
  593. {
  594. success = false;
  595. largestdim = layers;
  596. largestname = "array layer count";
  597. }
  598. else if (texType == TEXTURE_CUBE && (pixelWidth > maxcubesize || pixelWidth != pixelHeight))
  599. {
  600. success = false;
  601. largestdim = std::max(pixelWidth, pixelHeight);
  602. largestname = pixelWidth > pixelHeight ? "pixel width" : "pixel height";
  603. if (throwException && pixelWidth != pixelHeight)
  604. throw love::Exception("Cubemap textures must have equal width and height.");
  605. }
  606. else if (texType == TEXTURE_VOLUME && (pixelWidth > max3Dsize || pixelHeight > max3Dsize || depth > max3Dsize))
  607. {
  608. success = false;
  609. largestdim = std::max(std::max(pixelWidth, pixelHeight), depth);
  610. if (largestdim == pixelWidth)
  611. largestname = "pixel width";
  612. else if (largestdim == pixelHeight)
  613. largestname = "pixel height";
  614. else
  615. largestname = "pixel depth";
  616. }
  617. if (throwException && largestname != nullptr)
  618. throw love::Exception("Cannot create texture: %s of %d is too large for this system.", largestname, largestdim);
  619. return success;
  620. }
  621. Texture::Slices::Slices(TextureType textype)
  622. : textureType(textype)
  623. {
  624. }
  625. void Texture::Slices::clear()
  626. {
  627. data.clear();
  628. }
  629. void Texture::Slices::set(int slice, int mipmap, love::image::ImageDataBase *d)
  630. {
  631. if (textureType == TEXTURE_VOLUME)
  632. {
  633. if (mipmap >= (int) data.size())
  634. data.resize(mipmap + 1);
  635. if (slice >= (int) data[mipmap].size())
  636. data[mipmap].resize(slice + 1);
  637. data[mipmap][slice].set(d);
  638. }
  639. else
  640. {
  641. if (slice >= (int) data.size())
  642. data.resize(slice + 1);
  643. if (mipmap >= (int) data[slice].size())
  644. data[slice].resize(mipmap + 1);
  645. data[slice][mipmap].set(d);
  646. }
  647. }
  648. love::image::ImageDataBase *Texture::Slices::get(int slice, int mipmap) const
  649. {
  650. if (slice < 0 || slice >= getSliceCount(mipmap))
  651. return nullptr;
  652. if (mipmap < 0 || mipmap >= getMipmapCount(slice))
  653. return nullptr;
  654. if (textureType == TEXTURE_VOLUME)
  655. return data[mipmap][slice].get();
  656. else
  657. return data[slice][mipmap].get();
  658. }
  659. void Texture::Slices::add(love::image::CompressedImageData *cdata, int startslice, int startmip, bool addallslices, bool addallmips)
  660. {
  661. int slicecount = addallslices ? cdata->getSliceCount() : 1;
  662. int mipcount = addallmips ? cdata->getMipmapCount() : 1;
  663. for (int mip = 0; mip < mipcount; mip++)
  664. {
  665. for (int slice = 0; slice < slicecount; slice++)
  666. set(startslice + slice, startmip + mip, cdata->getSlice(slice, mip));
  667. }
  668. }
  669. int Texture::Slices::getSliceCount(int mip) const
  670. {
  671. if (textureType == TEXTURE_VOLUME)
  672. {
  673. if (mip < 0 || mip >= (int) data.size())
  674. return 0;
  675. return (int) data[mip].size();
  676. }
  677. else
  678. return (int) data.size();
  679. }
  680. int Texture::Slices::getMipmapCount(int slice) const
  681. {
  682. if (textureType == TEXTURE_VOLUME)
  683. return (int) data.size();
  684. else
  685. {
  686. if (slice < 0 || slice >= (int) data.size())
  687. return 0;
  688. return data[slice].size();
  689. }
  690. }
  691. bool Texture::Slices::validate() const
  692. {
  693. int slicecount = getSliceCount();
  694. int mipcount = getMipmapCount(0);
  695. if (slicecount == 0 || mipcount == 0)
  696. throw love::Exception("At least one ImageData or CompressedImageData is required.");
  697. if (textureType == TEXTURE_CUBE && slicecount != 6)
  698. throw love::Exception("Cube textures must have exactly 6 sides.");
  699. image::ImageDataBase *firstdata = get(0, 0);
  700. int w = firstdata->getWidth();
  701. int h = firstdata->getHeight();
  702. PixelFormat format = firstdata->getFormat();
  703. bool linear = firstdata->isLinear();
  704. if (textureType == TEXTURE_CUBE && w != h)
  705. throw love::Exception("Cube textures must have equal widths and heights for each cube face.");
  706. int mipw = w;
  707. int miph = h;
  708. int mipslices = slicecount;
  709. for (int mip = 0; mip < mipcount; mip++)
  710. {
  711. if (textureType == TEXTURE_VOLUME)
  712. {
  713. slicecount = getSliceCount(mip);
  714. if (slicecount != mipslices)
  715. throw love::Exception("Invalid number of image data layers in mipmap level %d (expected %d, got %d)", mip+1, mipslices, slicecount);
  716. }
  717. for (int slice = 0; slice < slicecount; slice++)
  718. {
  719. auto slicedata = get(slice, mip);
  720. if (slicedata == nullptr)
  721. throw love::Exception("Missing image data (slice %d, mipmap level %d)", slice+1, mip+1);
  722. int realw = slicedata->getWidth();
  723. int realh = slicedata->getHeight();
  724. if (getMipmapCount(slice) != mipcount)
  725. throw love::Exception("All texture layers must have the same mipmap count.");
  726. if (mipw != realw)
  727. throw love::Exception("Width of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, mipw, realw);
  728. if (miph != realh)
  729. throw love::Exception("Height of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, miph, realh);
  730. if (format != slicedata->getFormat())
  731. throw love::Exception("All texture slices and mipmaps must have the same pixel format.");
  732. if (linear != slicedata->isLinear())
  733. throw love::Exception("All texture slices and mipmaps must have the same linear setting.");
  734. }
  735. mipw = std::max(mipw / 2, 1);
  736. miph = std::max(miph / 2, 1);
  737. if (textureType == TEXTURE_VOLUME)
  738. mipslices = std::max(mipslices / 2, 1);
  739. }
  740. return true;
  741. }
  742. static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[] =
  743. {
  744. { "2d", TEXTURE_2D },
  745. { "volume", TEXTURE_VOLUME },
  746. { "array", TEXTURE_2D_ARRAY },
  747. { "cube", TEXTURE_CUBE },
  748. };
  749. static StringMap<TextureType, TEXTURE_MAX_ENUM> texTypes(texTypeEntries, sizeof(texTypeEntries));
  750. static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM>::Entry mipmapEntries[] =
  751. {
  752. { "none", Texture::MIPMAPS_NONE },
  753. { "manual", Texture::MIPMAPS_MANUAL },
  754. { "auto", Texture::MIPMAPS_AUTO },
  755. };
  756. static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM> mipmapModes(mipmapEntries, sizeof(mipmapEntries));
  757. static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM>::Entry settingTypeEntries[] =
  758. {
  759. { "width", Texture::SETTING_WIDTH },
  760. { "height", Texture::SETTING_HEIGHT },
  761. { "layers", Texture::SETTING_LAYERS },
  762. { "mipmaps", Texture::SETTING_MIPMAPS },
  763. { "mipmapcount", Texture::SETTING_MIPMAP_COUNT },
  764. { "format", Texture::SETTING_FORMAT },
  765. { "linear", Texture::SETTING_LINEAR },
  766. { "type", Texture::SETTING_TYPE },
  767. { "dpiscale", Texture::SETTING_DPI_SCALE },
  768. { "msaa", Texture::SETTING_MSAA },
  769. { "canvas", Texture::SETTING_RENDER_TARGET },
  770. { "computewrite", Texture::SETTING_COMPUTE_WRITE },
  771. { "readable", Texture::SETTING_READABLE },
  772. { "debugname", Texture::SETTING_DEBUGNAME },
  773. };
  774. static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM> settingTypes(settingTypeEntries, sizeof(settingTypeEntries));
  775. bool Texture::getConstant(const char *in, TextureType &out)
  776. {
  777. return texTypes.find(in, out);
  778. }
  779. bool Texture::getConstant(TextureType in, const char *&out)
  780. {
  781. return texTypes.find(in, out);
  782. }
  783. std::vector<std::string> Texture::getConstants(TextureType)
  784. {
  785. return texTypes.getNames();
  786. }
  787. bool Texture::getConstant(const char *in, MipmapsMode &out)
  788. {
  789. return mipmapModes.find(in, out);
  790. }
  791. bool Texture::getConstant(MipmapsMode in, const char *&out)
  792. {
  793. return mipmapModes.find(in, out);
  794. }
  795. std::vector<std::string> Texture::getConstants(MipmapsMode)
  796. {
  797. return mipmapModes.getNames();
  798. }
  799. bool Texture::getConstant(const char *in, SettingType &out)
  800. {
  801. return settingTypes.find(in, out);
  802. }
  803. bool Texture::getConstant(SettingType in, const char *&out)
  804. {
  805. return settingTypes.find(in, out);
  806. }
  807. const char *Texture::getConstant(SettingType in)
  808. {
  809. const char *name = nullptr;
  810. getConstant(in, name);
  811. return name;
  812. }
  813. std::vector<std::string> Texture::getConstants(SettingType)
  814. {
  815. return settingTypes.getNames();
  816. }
  817. } // graphics
  818. } // love