Graphics.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /**
  2. * Copyright (c) 2006-2019 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 "Graphics.h"
  22. #include "Buffer.h"
  23. #include "math/MathModule.h"
  24. #include "data/DataModule.h"
  25. #include "Polyline.h"
  26. #include "font/Font.h"
  27. #include "window/Window.h"
  28. #include "SpriteBatch.h"
  29. #include "ParticleSystem.h"
  30. #include "Font.h"
  31. #include "Video.h"
  32. #include "Text.h"
  33. #include "common/deprecation.h"
  34. // C++
  35. #include <algorithm>
  36. #include <stdlib.h>
  37. namespace love
  38. {
  39. namespace graphics
  40. {
  41. static bool gammaCorrect = false;
  42. static bool debugMode = false;
  43. static bool debugModeQueried = false;
  44. void setGammaCorrect(bool gammacorrect)
  45. {
  46. gammaCorrect = gammacorrect;
  47. }
  48. bool isGammaCorrect()
  49. {
  50. return gammaCorrect;
  51. }
  52. void gammaCorrectColor(Colorf &c)
  53. {
  54. if (isGammaCorrect())
  55. {
  56. c.r = math::gammaToLinear(c.r);
  57. c.g = math::gammaToLinear(c.g);
  58. c.b = math::gammaToLinear(c.b);
  59. }
  60. }
  61. Colorf gammaCorrectColor(const Colorf &c)
  62. {
  63. Colorf r = c;
  64. gammaCorrectColor(r);
  65. return r;
  66. }
  67. void unGammaCorrectColor(Colorf &c)
  68. {
  69. if (isGammaCorrect())
  70. {
  71. c.r = math::linearToGamma(c.r);
  72. c.g = math::linearToGamma(c.g);
  73. c.b = math::linearToGamma(c.b);
  74. }
  75. }
  76. Colorf unGammaCorrectColor(const Colorf &c)
  77. {
  78. Colorf r = c;
  79. unGammaCorrectColor(r);
  80. return r;
  81. }
  82. bool isDebugEnabled()
  83. {
  84. if (!debugModeQueried)
  85. {
  86. const char *debugenv = getenv("LOVE_GRAPHICS_DEBUG");
  87. debugMode = debugenv != nullptr && debugenv[0] != '0';
  88. debugModeQueried = true;
  89. }
  90. return debugMode;
  91. }
  92. love::Type Graphics::type("graphics", &Module::type);
  93. Graphics::DefaultShaderCode Graphics::defaultShaderCode[Shader::STANDARD_MAX_ENUM][Shader::LANGUAGE_MAX_ENUM][2];
  94. Graphics::Graphics()
  95. : width(0)
  96. , height(0)
  97. , pixelWidth(0)
  98. , pixelHeight(0)
  99. , created(false)
  100. , active(true)
  101. , writingToStencil(false)
  102. , streamBufferState()
  103. , projectionMatrix()
  104. , canvasSwitchCount(0)
  105. , drawCalls(0)
  106. , drawCallsBatched(0)
  107. , quadIndexBuffer(nullptr)
  108. , capabilities()
  109. , cachedShaderStages()
  110. {
  111. transformStack.reserve(16);
  112. transformStack.push_back(Matrix4());
  113. pixelScaleStack.reserve(16);
  114. pixelScaleStack.push_back(1);
  115. states.reserve(10);
  116. states.push_back(DisplayState());
  117. if (!Shader::initialize())
  118. throw love::Exception("Shader support failed to initialize!");
  119. }
  120. Graphics::~Graphics()
  121. {
  122. delete quadIndexBuffer;
  123. // Clean up standard shaders before the active shader. If we do it after,
  124. // the active shader may try to activate a standard shader when deactivating
  125. // itself, which will cause problems since it calls Graphics methods in the
  126. // Graphics destructor.
  127. for (int i = 0; i < Shader::STANDARD_MAX_ENUM; i++)
  128. {
  129. if (Shader::standardShaders[i])
  130. {
  131. Shader::standardShaders[i]->release();
  132. Shader::standardShaders[i] = nullptr;
  133. }
  134. }
  135. states.clear();
  136. defaultFont.set(nullptr);
  137. delete streamBufferState.vb[0];
  138. delete streamBufferState.vb[1];
  139. delete streamBufferState.indexBuffer;
  140. for (int i = 0; i < (int) ShaderStage::STAGE_MAX_ENUM; i++)
  141. cachedShaderStages[i].clear();
  142. Shader::deinitialize();
  143. }
  144. void Graphics::createQuadIndexBuffer()
  145. {
  146. if (quadIndexBuffer != nullptr)
  147. return;
  148. size_t size = sizeof(uint16) * (LOVE_UINT16_MAX / 4) * 6;
  149. quadIndexBuffer = newBuffer(size, nullptr, BUFFER_INDEX, vertex::USAGE_STATIC, 0);
  150. Buffer::Mapper map(*quadIndexBuffer);
  151. vertex::fillIndices(vertex::TriangleIndexMode::QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.get());
  152. }
  153. Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
  154. {
  155. return new Quad(v, sw, sh);
  156. }
  157. Font *Graphics::newFont(love::font::Rasterizer *data, const Texture::Filter &filter)
  158. {
  159. return new Font(data, filter);
  160. }
  161. Font *Graphics::newDefaultFont(int size, font::TrueTypeRasterizer::Hinting hinting, const Texture::Filter &filter)
  162. {
  163. auto fontmodule = Module::getInstance<font::Font>(M_FONT);
  164. if (!fontmodule)
  165. throw love::Exception("Font module has not been loaded.");
  166. StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(size, hinting), Acquire::NORETAIN);
  167. return newFont(r.get(), filter);
  168. }
  169. Video *Graphics::newVideo(love::video::VideoStream *stream, float dpiscale)
  170. {
  171. return new Video(this, stream, dpiscale);
  172. }
  173. love::graphics::SpriteBatch *Graphics::newSpriteBatch(Texture *texture, int size, vertex::Usage usage)
  174. {
  175. return new SpriteBatch(this, texture, size, usage);
  176. }
  177. love::graphics::ParticleSystem *Graphics::newParticleSystem(Texture *texture, int size)
  178. {
  179. return new ParticleSystem(texture, size);
  180. }
  181. ShaderStage *Graphics::newShaderStage(ShaderStage::StageType stage, const std::string &optsource)
  182. {
  183. if (stage == ShaderStage::STAGE_MAX_ENUM)
  184. throw love::Exception("Invalid shader stage.");
  185. const std::string &source = optsource.empty() ? getCurrentDefaultShaderCode().source[stage] : optsource;
  186. ShaderStage *s = nullptr;
  187. std::string cachekey;
  188. if (!source.empty())
  189. {
  190. data::HashFunction::Value hashvalue;
  191. data::hash(data::HashFunction::FUNCTION_SHA1, source.c_str(), source.size(), hashvalue);
  192. cachekey = std::string(hashvalue.data, hashvalue.size);
  193. auto it = cachedShaderStages[stage].find(cachekey);
  194. if (it != cachedShaderStages[stage].end())
  195. {
  196. s = it->second;
  197. s->retain();
  198. }
  199. }
  200. if (s == nullptr)
  201. {
  202. s = newShaderStageInternal(stage, cachekey, source, getRenderer() == RENDERER_OPENGLES);
  203. if (!cachekey.empty())
  204. cachedShaderStages[stage][cachekey] = s;
  205. }
  206. return s;
  207. }
  208. Shader *Graphics::newShader(const std::string &vertex, const std::string &pixel)
  209. {
  210. if (vertex.empty() && pixel.empty())
  211. throw love::Exception("Error creating shader: no source code!");
  212. StrongRef<ShaderStage> vertexstage(newShaderStage(ShaderStage::STAGE_VERTEX, vertex), Acquire::NORETAIN);
  213. StrongRef<ShaderStage> pixelstage(newShaderStage(ShaderStage::STAGE_PIXEL, pixel), Acquire::NORETAIN);
  214. return newShaderInternal(vertexstage.get(), pixelstage.get());
  215. }
  216. Mesh *Graphics::newMesh(const std::vector<Vertex> &vertices, PrimitiveType drawmode, vertex::Usage usage)
  217. {
  218. return newMesh(Mesh::getDefaultVertexFormat(), &vertices[0], vertices.size() * sizeof(Vertex), drawmode, usage);
  219. }
  220. Mesh *Graphics::newMesh(int vertexcount, PrimitiveType drawmode, vertex::Usage usage)
  221. {
  222. return newMesh(Mesh::getDefaultVertexFormat(), vertexcount, drawmode, usage);
  223. }
  224. love::graphics::Mesh *Graphics::newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, int vertexcount, PrimitiveType drawmode, vertex::Usage usage)
  225. {
  226. return new Mesh(this, vertexformat, vertexcount, drawmode, usage);
  227. }
  228. love::graphics::Mesh *Graphics::newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, const void *data, size_t datasize, PrimitiveType drawmode, vertex::Usage usage)
  229. {
  230. return new Mesh(this, vertexformat, data, datasize, drawmode, usage);
  231. }
  232. love::graphics::Text *Graphics::newText(graphics::Font *font, const std::vector<Font::ColoredString> &text)
  233. {
  234. return new Text(font, text);
  235. }
  236. void Graphics::cleanupCachedShaderStage(ShaderStage::StageType type, const std::string &hashkey)
  237. {
  238. cachedShaderStages[type].erase(hashkey);
  239. }
  240. bool Graphics::validateShader(bool gles, const std::string &vertex, const std::string &pixel, std::string &err)
  241. {
  242. if (vertex.empty() && pixel.empty())
  243. {
  244. err = "Error validating shader: no source code!";
  245. return false;
  246. }
  247. StrongRef<ShaderStage> vertexstage;
  248. StrongRef<ShaderStage> pixelstage;
  249. // Don't use cached shader stages, since the gles flag may not match the
  250. // current renderer.
  251. if (!vertex.empty())
  252. vertexstage.set(new ShaderStageForValidation(this, ShaderStage::STAGE_VERTEX, vertex, gles), Acquire::NORETAIN);
  253. if (!pixel.empty())
  254. pixelstage.set(new ShaderStageForValidation(this, ShaderStage::STAGE_PIXEL, pixel, gles), Acquire::NORETAIN);
  255. return Shader::validate(vertexstage.get(), pixelstage.get(), err);
  256. }
  257. int Graphics::getWidth() const
  258. {
  259. return width;
  260. }
  261. int Graphics::getHeight() const
  262. {
  263. return height;
  264. }
  265. int Graphics::getPixelWidth() const
  266. {
  267. return pixelWidth;
  268. }
  269. int Graphics::getPixelHeight() const
  270. {
  271. return pixelHeight;
  272. }
  273. double Graphics::getCurrentDPIScale() const
  274. {
  275. const auto &rt = states.back().renderTargets.getFirstTarget();
  276. if (rt.canvas.get())
  277. return rt.canvas->getDPIScale();
  278. return getScreenDPIScale();
  279. }
  280. double Graphics::getScreenDPIScale() const
  281. {
  282. return (double) getPixelHeight() / (double) getHeight();
  283. }
  284. bool Graphics::isCreated() const
  285. {
  286. return created;
  287. }
  288. bool Graphics::isActive() const
  289. {
  290. // The graphics module is only completely 'active' if there's a window, a
  291. // context, and the active variable is set.
  292. auto window = getInstance<love::window::Window>(M_WINDOW);
  293. return active && isCreated() && window != nullptr && window->isOpen();
  294. }
  295. void Graphics::reset()
  296. {
  297. DisplayState s;
  298. stopDrawToStencilBuffer();
  299. restoreState(s);
  300. origin();
  301. }
  302. /**
  303. * State functions.
  304. **/
  305. void Graphics::restoreState(const DisplayState &s)
  306. {
  307. setColor(s.color);
  308. setBackgroundColor(s.backgroundColor);
  309. setBlendMode(s.blendMode, s.blendAlphaMode);
  310. setLineWidth(s.lineWidth);
  311. setLineStyle(s.lineStyle);
  312. setLineJoin(s.lineJoin);
  313. setPointSize(s.pointSize);
  314. if (s.scissor)
  315. setScissor(s.scissorRect);
  316. else
  317. setScissor();
  318. setStencilTest(s.stencilCompare, s.stencilTestValue);
  319. setDepthMode(s.depthTest, s.depthWrite);
  320. setMeshCullMode(s.meshCullMode);
  321. setFrontFaceWinding(s.winding);
  322. setFont(s.font.get());
  323. setShader(s.shader.get());
  324. setCanvas(s.renderTargets);
  325. setColorMask(s.colorMask);
  326. setWireframe(s.wireframe);
  327. setDefaultFilter(s.defaultFilter);
  328. setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
  329. }
  330. void Graphics::restoreStateChecked(const DisplayState &s)
  331. {
  332. const DisplayState &cur = states.back();
  333. if (s.color != cur.color)
  334. setColor(s.color);
  335. setBackgroundColor(s.backgroundColor);
  336. if (s.blendMode != cur.blendMode || s.blendAlphaMode != cur.blendAlphaMode)
  337. setBlendMode(s.blendMode, s.blendAlphaMode);
  338. // These are just simple assignments.
  339. setLineWidth(s.lineWidth);
  340. setLineStyle(s.lineStyle);
  341. setLineJoin(s.lineJoin);
  342. if (s.pointSize != cur.pointSize)
  343. setPointSize(s.pointSize);
  344. if (s.scissor != cur.scissor || (s.scissor && !(s.scissorRect == cur.scissorRect)))
  345. {
  346. if (s.scissor)
  347. setScissor(s.scissorRect);
  348. else
  349. setScissor();
  350. }
  351. if (s.stencilCompare != cur.stencilCompare || s.stencilTestValue != cur.stencilTestValue)
  352. setStencilTest(s.stencilCompare, s.stencilTestValue);
  353. if (s.depthTest != cur.depthTest || s.depthWrite != cur.depthWrite)
  354. setDepthMode(s.depthTest, s.depthWrite);
  355. setMeshCullMode(s.meshCullMode);
  356. if (s.winding != cur.winding)
  357. setFrontFaceWinding(s.winding);
  358. setFont(s.font.get());
  359. setShader(s.shader.get());
  360. const auto &sRTs = s.renderTargets;
  361. const auto &curRTs = cur.renderTargets;
  362. bool canvaseschanged = sRTs.colors.size() != curRTs.colors.size();
  363. if (!canvaseschanged)
  364. {
  365. for (size_t i = 0; i < sRTs.colors.size() && i < curRTs.colors.size(); i++)
  366. {
  367. if (sRTs.colors[i] != curRTs.colors[i])
  368. {
  369. canvaseschanged = true;
  370. break;
  371. }
  372. }
  373. if (!canvaseschanged && sRTs.depthStencil != curRTs.depthStencil)
  374. canvaseschanged = true;
  375. if (sRTs.temporaryRTFlags != curRTs.temporaryRTFlags)
  376. canvaseschanged = true;
  377. }
  378. if (canvaseschanged)
  379. setCanvas(s.renderTargets);
  380. if (s.colorMask != cur.colorMask)
  381. setColorMask(s.colorMask);
  382. if (s.wireframe != cur.wireframe)
  383. setWireframe(s.wireframe);
  384. setDefaultFilter(s.defaultFilter);
  385. setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
  386. }
  387. Colorf Graphics::getColor() const
  388. {
  389. return states.back().color;
  390. }
  391. void Graphics::setBackgroundColor(Colorf c)
  392. {
  393. states.back().backgroundColor = c;
  394. }
  395. Colorf Graphics::getBackgroundColor() const
  396. {
  397. return states.back().backgroundColor;
  398. }
  399. void Graphics::checkSetDefaultFont()
  400. {
  401. // We don't create or set the default Font if an existing font is in use.
  402. if (states.back().font.get() != nullptr)
  403. return;
  404. // Create a new default font if we don't have one yet.
  405. if (!defaultFont.get())
  406. defaultFont.set(newDefaultFont(12, font::TrueTypeRasterizer::HINTING_NORMAL), Acquire::NORETAIN);
  407. states.back().font.set(defaultFont.get());
  408. }
  409. void Graphics::setFont(love::graphics::Font *font)
  410. {
  411. // We don't need to set a default font here if null is passed in, since we
  412. // only care about the default font in getFont and print.
  413. DisplayState &state = states.back();
  414. state.font.set(font);
  415. }
  416. love::graphics::Font *Graphics::getFont()
  417. {
  418. checkSetDefaultFont();
  419. return states.back().font.get();
  420. }
  421. void Graphics::setShader(love::graphics::Shader *shader)
  422. {
  423. if (shader == nullptr)
  424. return setShader();
  425. shader->attach();
  426. states.back().shader.set(shader);
  427. }
  428. void Graphics::setShader()
  429. {
  430. Shader::attachDefault(Shader::STANDARD_DEFAULT);
  431. states.back().shader.set(nullptr);
  432. }
  433. love::graphics::Shader *Graphics::getShader() const
  434. {
  435. return states.back().shader.get();
  436. }
  437. void Graphics::setCanvas(RenderTarget rt, uint32 temporaryRTFlags)
  438. {
  439. if (rt.canvas == nullptr)
  440. return setCanvas();
  441. RenderTargets rts;
  442. rts.colors.push_back(rt);
  443. rts.temporaryRTFlags = temporaryRTFlags;
  444. setCanvas(rts);
  445. }
  446. void Graphics::setCanvas(const RenderTargetsStrongRef &rts)
  447. {
  448. RenderTargets targets;
  449. targets.colors.reserve(rts.colors.size());
  450. for (const auto &rt : rts.colors)
  451. targets.colors.emplace_back(rt.canvas.get(), rt.slice, rt.mipmap);
  452. targets.depthStencil = RenderTarget(rts.depthStencil.canvas, rts.depthStencil.slice, rts.depthStencil.mipmap);
  453. targets.temporaryRTFlags = rts.temporaryRTFlags;
  454. return setCanvas(targets);
  455. }
  456. void Graphics::setCanvas(const RenderTargets &rts)
  457. {
  458. DisplayState &state = states.back();
  459. int ncanvases = (int) rts.colors.size();
  460. RenderTarget firsttarget = rts.getFirstTarget();
  461. love::graphics::Canvas *firstcanvas = firsttarget.canvas;
  462. if (firstcanvas == nullptr)
  463. return setCanvas();
  464. const auto &prevRTs = state.renderTargets;
  465. if (ncanvases == (int) prevRTs.colors.size())
  466. {
  467. bool modified = false;
  468. for (int i = 0; i < ncanvases; i++)
  469. {
  470. if (rts.colors[i] != prevRTs.colors[i])
  471. {
  472. modified = true;
  473. break;
  474. }
  475. }
  476. if (!modified && rts.depthStencil != prevRTs.depthStencil)
  477. modified = true;
  478. if (rts.temporaryRTFlags != prevRTs.temporaryRTFlags)
  479. modified = true;
  480. if (!modified)
  481. return;
  482. }
  483. if (ncanvases > capabilities.limits[LIMIT_MULTI_CANVAS])
  484. throw love::Exception("This system can't simultaneously render to %d canvases.", ncanvases);
  485. bool multiformatsupported = capabilities.features[FEATURE_MULTI_CANVAS_FORMATS];
  486. PixelFormat firstcolorformat = PIXELFORMAT_UNKNOWN;
  487. if (!rts.colors.empty())
  488. firstcolorformat = rts.colors[0].canvas->getPixelFormat();
  489. if (isPixelFormatDepthStencil(firstcolorformat))
  490. throw love::Exception("Depth/stencil format Canvases must be used with the 'depthstencil' field of the table passed into setCanvas.");
  491. if (firsttarget.mipmap < 0 || firsttarget.mipmap >= firstcanvas->getMipmapCount())
  492. throw love::Exception("Invalid mipmap level %d.", firsttarget.mipmap + 1);
  493. if (!firstcanvas->isValidSlice(firsttarget.slice))
  494. throw love::Exception("Invalid slice index: %d.", firsttarget.slice + 1);
  495. bool hasSRGBcanvas = firstcolorformat == PIXELFORMAT_sRGBA8;
  496. int pixelw = firstcanvas->getPixelWidth(firsttarget.mipmap);
  497. int pixelh = firstcanvas->getPixelHeight(firsttarget.mipmap);
  498. int reqmsaa = firstcanvas->getRequestedMSAA();
  499. for (int i = 1; i < ncanvases; i++)
  500. {
  501. love::graphics::Canvas *c = rts.colors[i].canvas;
  502. PixelFormat format = c->getPixelFormat();
  503. int mip = rts.colors[i].mipmap;
  504. int slice = rts.colors[i].slice;
  505. if (mip < 0 || mip >= c->getMipmapCount())
  506. throw love::Exception("Invalid mipmap level %d.", mip + 1);
  507. if (!c->isValidSlice(slice))
  508. throw love::Exception("Invalid slice index: %d.", slice + 1);
  509. if (c->getPixelWidth(mip) != pixelw || c->getPixelHeight(mip) != pixelh)
  510. throw love::Exception("All canvases must have the same pixel dimensions.");
  511. if (!multiformatsupported && format != firstcolorformat)
  512. throw love::Exception("This system doesn't support multi-canvas rendering with different canvas formats.");
  513. if (c->getRequestedMSAA() != reqmsaa)
  514. throw love::Exception("All Canvases must have the same MSAA value.");
  515. if (isPixelFormatDepthStencil(format))
  516. throw love::Exception("Depth/stencil format Canvases must be used with the 'depthstencil' field of the table passed into setCanvas.");
  517. if (format == PIXELFORMAT_sRGBA8)
  518. hasSRGBcanvas = true;
  519. }
  520. if (rts.depthStencil.canvas != nullptr)
  521. {
  522. love::graphics::Canvas *c = rts.depthStencil.canvas;
  523. int mip = rts.depthStencil.mipmap;
  524. int slice = rts.depthStencil.slice;
  525. if (!isPixelFormatDepthStencil(c->getPixelFormat()))
  526. throw love::Exception("Only depth/stencil format Canvases can be used with the 'depthstencil' field of the table passed into setCanvas.");
  527. if (c->getPixelWidth(mip) != pixelw || c->getPixelHeight(mip) != pixelh)
  528. throw love::Exception("All canvases must have the same pixel dimensions.");
  529. if (c->getRequestedMSAA() != firstcanvas->getRequestedMSAA())
  530. throw love::Exception("All Canvases must have the same MSAA value.");
  531. if (mip < 0 || mip >= c->getMipmapCount())
  532. throw love::Exception("Invalid mipmap level %d.", mip + 1);
  533. if (!c->isValidSlice(slice))
  534. throw love::Exception("Invalid slice index: %d.", slice + 1);
  535. }
  536. int w = firstcanvas->getWidth(firsttarget.mipmap);
  537. int h = firstcanvas->getHeight(firsttarget.mipmap);
  538. flushStreamDraws();
  539. if (rts.depthStencil.canvas == nullptr && rts.temporaryRTFlags != 0)
  540. {
  541. bool wantsdepth = (rts.temporaryRTFlags & TEMPORARY_RT_DEPTH) != 0;
  542. bool wantsstencil = (rts.temporaryRTFlags & TEMPORARY_RT_STENCIL) != 0;
  543. PixelFormat dsformat = PIXELFORMAT_STENCIL8;
  544. if (wantsdepth && wantsstencil)
  545. dsformat = PIXELFORMAT_DEPTH24_STENCIL8;
  546. else if (wantsdepth && isCanvasFormatSupported(PIXELFORMAT_DEPTH24, false))
  547. dsformat = PIXELFORMAT_DEPTH24;
  548. else if (wantsdepth)
  549. dsformat = PIXELFORMAT_DEPTH16;
  550. else if (wantsstencil)
  551. dsformat = PIXELFORMAT_STENCIL8;
  552. // We want setCanvasInternal to have a pointer to the temporary RT, but
  553. // we don't want to directly store it in the main graphics state.
  554. RenderTargets realRTs = rts;
  555. realRTs.depthStencil.canvas = getTemporaryCanvas(dsformat, pixelw, pixelh, reqmsaa);
  556. realRTs.depthStencil.slice = 0;
  557. setCanvasInternal(realRTs, w, h, pixelw, pixelh, hasSRGBcanvas);
  558. }
  559. else
  560. setCanvasInternal(rts, w, h, pixelw, pixelh, hasSRGBcanvas);
  561. RenderTargetsStrongRef refs;
  562. refs.colors.reserve(rts.colors.size());
  563. for (auto c : rts.colors)
  564. refs.colors.emplace_back(c.canvas, c.slice, c.mipmap);
  565. refs.depthStencil = RenderTargetStrongRef(rts.depthStencil.canvas, rts.depthStencil.slice);
  566. refs.temporaryRTFlags = rts.temporaryRTFlags;
  567. std::swap(state.renderTargets, refs);
  568. canvasSwitchCount++;
  569. }
  570. void Graphics::setCanvas()
  571. {
  572. DisplayState &state = states.back();
  573. if (state.renderTargets.colors.empty() && state.renderTargets.depthStencil.canvas == nullptr)
  574. return;
  575. flushStreamDraws();
  576. setCanvasInternal(RenderTargets(), width, height, pixelWidth, pixelHeight, isGammaCorrect());
  577. state.renderTargets = RenderTargetsStrongRef();
  578. canvasSwitchCount++;
  579. }
  580. Graphics::RenderTargets Graphics::getCanvas() const
  581. {
  582. const auto &curRTs = states.back().renderTargets;
  583. RenderTargets rts;
  584. rts.colors.reserve(curRTs.colors.size());
  585. for (const auto &rt : curRTs.colors)
  586. rts.colors.emplace_back(rt.canvas.get(), rt.slice, rt.mipmap);
  587. rts.depthStencil = RenderTarget(curRTs.depthStencil.canvas, curRTs.depthStencil.slice, curRTs.depthStencil.mipmap);
  588. rts.temporaryRTFlags = curRTs.temporaryRTFlags;
  589. return rts;
  590. }
  591. bool Graphics::isCanvasActive() const
  592. {
  593. const auto &rts = states.back().renderTargets;
  594. return !rts.colors.empty() || rts.depthStencil.canvas != nullptr;
  595. }
  596. bool Graphics::isCanvasActive(love::graphics::Canvas *canvas) const
  597. {
  598. const auto &rts = states.back().renderTargets;
  599. for (const auto &rt : rts.colors)
  600. {
  601. if (rt.canvas.get() == canvas)
  602. return true;
  603. }
  604. if (rts.depthStencil.canvas.get() == canvas)
  605. return true;
  606. return false;
  607. }
  608. bool Graphics::isCanvasActive(Canvas *canvas, int slice) const
  609. {
  610. const auto &rts = states.back().renderTargets;
  611. for (const auto &rt : rts.colors)
  612. {
  613. if (rt.canvas.get() == canvas && rt.slice == slice)
  614. return true;
  615. }
  616. if (rts.depthStencil.canvas.get() == canvas && rts.depthStencil.slice == slice)
  617. return true;
  618. return false;
  619. }
  620. Canvas *Graphics::getTemporaryCanvas(PixelFormat format, int w, int h, int samples)
  621. {
  622. love::graphics::Canvas *canvas = nullptr;
  623. for (TemporaryCanvas &temp : temporaryCanvases)
  624. {
  625. Canvas *c = temp.canvas;
  626. if (c->getPixelFormat() == format && c->getPixelWidth() == w
  627. && c->getPixelHeight() == h && c->getRequestedMSAA() == samples)
  628. {
  629. canvas = c;
  630. temp.framesSinceUse = 0;
  631. break;
  632. }
  633. }
  634. if (canvas == nullptr)
  635. {
  636. Canvas::Settings settings;
  637. settings.format = format;
  638. settings.width = w;
  639. settings.height = h;
  640. settings.msaa = samples;
  641. canvas = newCanvas(settings);
  642. temporaryCanvases.emplace_back(canvas);
  643. }
  644. return canvas;
  645. }
  646. void Graphics::intersectScissor(const Rect &rect)
  647. {
  648. Rect currect = states.back().scissorRect;
  649. if (!states.back().scissor)
  650. {
  651. currect.x = 0;
  652. currect.y = 0;
  653. currect.w = std::numeric_limits<int>::max();
  654. currect.h = std::numeric_limits<int>::max();
  655. }
  656. int x1 = std::max(currect.x, rect.x);
  657. int y1 = std::max(currect.y, rect.y);
  658. int x2 = std::min(currect.x + currect.w, rect.x + rect.w);
  659. int y2 = std::min(currect.y + currect.h, rect.y + rect.h);
  660. Rect newrect = {x1, y1, std::max(0, x2 - x1), std::max(0, y2 - y1)};
  661. setScissor(newrect);
  662. }
  663. bool Graphics::getScissor(Rect &rect) const
  664. {
  665. const DisplayState &state = states.back();
  666. rect = state.scissorRect;
  667. return state.scissor;
  668. }
  669. void Graphics::setStencilTest()
  670. {
  671. setStencilTest(COMPARE_ALWAYS, 0);
  672. }
  673. void Graphics::getStencilTest(CompareMode &compare, int &value) const
  674. {
  675. const DisplayState &state = states.back();
  676. compare = state.stencilCompare;
  677. value = state.stencilTestValue;
  678. }
  679. void Graphics::setDepthMode()
  680. {
  681. setDepthMode(COMPARE_ALWAYS, false);
  682. }
  683. void Graphics::getDepthMode(CompareMode &compare, bool &write) const
  684. {
  685. const DisplayState &state = states.back();
  686. compare = state.depthTest;
  687. write = state.depthWrite;
  688. }
  689. void Graphics::setMeshCullMode(CullMode cull)
  690. {
  691. // Handled inside the draw() graphics API implementations.
  692. states.back().meshCullMode = cull;
  693. }
  694. CullMode Graphics::getMeshCullMode() const
  695. {
  696. return states.back().meshCullMode;
  697. }
  698. vertex::Winding Graphics::getFrontFaceWinding() const
  699. {
  700. return states.back().winding;
  701. }
  702. Graphics::ColorMask Graphics::getColorMask() const
  703. {
  704. return states.back().colorMask;
  705. }
  706. Graphics::BlendMode Graphics::getBlendMode(BlendAlpha &alphamode) const
  707. {
  708. alphamode = states.back().blendAlphaMode;
  709. return states.back().blendMode;
  710. }
  711. void Graphics::setDefaultFilter(const Texture::Filter &f)
  712. {
  713. Texture::defaultFilter = f;
  714. states.back().defaultFilter = f;
  715. }
  716. const Texture::Filter &Graphics::getDefaultFilter() const
  717. {
  718. return Texture::defaultFilter;
  719. }
  720. void Graphics::setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness)
  721. {
  722. Texture::defaultMipmapFilter = filter;
  723. Texture::defaultMipmapSharpness = sharpness;
  724. states.back().defaultMipmapFilter = filter;
  725. states.back().defaultMipmapSharpness = sharpness;
  726. }
  727. void Graphics::getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const
  728. {
  729. *filter = Texture::defaultMipmapFilter;
  730. *sharpness = Texture::defaultMipmapSharpness;
  731. }
  732. void Graphics::setLineWidth(float width)
  733. {
  734. states.back().lineWidth = width;
  735. }
  736. void Graphics::setLineStyle(Graphics::LineStyle style)
  737. {
  738. states.back().lineStyle = style;
  739. }
  740. void Graphics::setLineJoin(Graphics::LineJoin join)
  741. {
  742. states.back().lineJoin = join;
  743. }
  744. float Graphics::getLineWidth() const
  745. {
  746. return states.back().lineWidth;
  747. }
  748. Graphics::LineStyle Graphics::getLineStyle() const
  749. {
  750. return states.back().lineStyle;
  751. }
  752. Graphics::LineJoin Graphics::getLineJoin() const
  753. {
  754. return states.back().lineJoin;
  755. }
  756. float Graphics::getPointSize() const
  757. {
  758. return states.back().pointSize;
  759. }
  760. bool Graphics::isWireframe() const
  761. {
  762. return states.back().wireframe;
  763. }
  764. void Graphics::captureScreenshot(const ScreenshotInfo &info)
  765. {
  766. pendingScreenshotCallbacks.push_back(info);
  767. }
  768. Graphics::StreamVertexData Graphics::requestStreamDraw(const StreamDrawCommand &cmd)
  769. {
  770. using namespace vertex;
  771. StreamBufferState &state = streamBufferState;
  772. bool shouldflush = false;
  773. bool shouldresize = false;
  774. if (cmd.primitiveMode != state.primitiveMode
  775. || cmd.formats[0] != state.formats[0] || cmd.formats[1] != state.formats[1]
  776. || ((cmd.indexMode != TriangleIndexMode::NONE) != (state.indexCount > 0))
  777. || cmd.texture != state.texture
  778. || cmd.standardShaderType != state.standardShaderType)
  779. {
  780. shouldflush = true;
  781. }
  782. int totalvertices = state.vertexCount + cmd.vertexCount;
  783. // We only support uint16 index buffers for now.
  784. if (totalvertices > LOVE_UINT16_MAX && cmd.indexMode != TriangleIndexMode::NONE)
  785. shouldflush = true;
  786. int reqIndexCount = getIndexCount(cmd.indexMode, cmd.vertexCount);
  787. size_t reqIndexSize = reqIndexCount * sizeof(uint16);
  788. size_t newdatasizes[2] = {0, 0};
  789. size_t buffersizes[3] = {0, 0, 0};
  790. for (int i = 0; i < 2; i++)
  791. {
  792. if (cmd.formats[i] == CommonFormat::NONE)
  793. continue;
  794. size_t stride = getFormatStride(cmd.formats[i]);
  795. size_t datasize = stride * totalvertices;
  796. if (state.vbMap[i].data != nullptr && datasize > state.vbMap[i].size)
  797. shouldflush = true;
  798. if (datasize > state.vb[i]->getUsableSize())
  799. {
  800. buffersizes[i] = std::max(datasize, state.vb[i]->getSize() * 2);
  801. shouldresize = true;
  802. }
  803. newdatasizes[i] = stride * cmd.vertexCount;
  804. }
  805. if (cmd.indexMode != TriangleIndexMode::NONE)
  806. {
  807. size_t datasize = (state.indexCount + reqIndexCount) * sizeof(uint16);
  808. if (state.indexBufferMap.data != nullptr && datasize > state.indexBufferMap.size)
  809. shouldflush = true;
  810. if (datasize > state.indexBuffer->getUsableSize())
  811. {
  812. buffersizes[2] = std::max(datasize, state.indexBuffer->getSize() * 2);
  813. shouldresize = true;
  814. }
  815. }
  816. if (shouldflush || shouldresize)
  817. {
  818. flushStreamDraws();
  819. state.primitiveMode = cmd.primitiveMode;
  820. state.formats[0] = cmd.formats[0];
  821. state.formats[1] = cmd.formats[1];
  822. state.texture = cmd.texture;
  823. state.standardShaderType = cmd.standardShaderType;
  824. }
  825. if (state.vertexCount == 0 && Shader::isDefaultActive())
  826. Shader::attachDefault(state.standardShaderType);
  827. if (state.vertexCount == 0 && Shader::current != nullptr && cmd.texture != nullptr)
  828. Shader::current->checkMainTexture(cmd.texture);
  829. if (shouldresize)
  830. {
  831. for (int i = 0; i < 2; i++)
  832. {
  833. if (state.vb[i]->getSize() < buffersizes[i])
  834. {
  835. delete state.vb[i];
  836. state.vb[i] = newStreamBuffer(BUFFER_VERTEX, buffersizes[i]);
  837. }
  838. }
  839. if (state.indexBuffer->getSize() < buffersizes[2])
  840. {
  841. delete state.indexBuffer;
  842. state.indexBuffer = newStreamBuffer(BUFFER_INDEX, buffersizes[2]);
  843. }
  844. }
  845. if (cmd.indexMode != TriangleIndexMode::NONE)
  846. {
  847. if (state.indexBufferMap.data == nullptr)
  848. state.indexBufferMap = state.indexBuffer->map(reqIndexSize);
  849. uint16 *indices = (uint16 *) state.indexBufferMap.data;
  850. fillIndices(cmd.indexMode, state.vertexCount, cmd.vertexCount, indices);
  851. state.indexBufferMap.data += reqIndexSize;
  852. }
  853. StreamVertexData d;
  854. for (int i = 0; i < 2; i++)
  855. {
  856. if (newdatasizes[i] > 0)
  857. {
  858. if (state.vbMap[i].data == nullptr)
  859. state.vbMap[i] = state.vb[i]->map(newdatasizes[i]);
  860. d.stream[i] = state.vbMap[i].data;
  861. state.vbMap[i].data += newdatasizes[i];
  862. }
  863. }
  864. if (state.vertexCount > 0)
  865. drawCallsBatched++;
  866. state.vertexCount += cmd.vertexCount;
  867. state.indexCount += reqIndexCount;
  868. return d;
  869. }
  870. void Graphics::flushStreamDraws()
  871. {
  872. using namespace vertex;
  873. auto &sbstate = streamBufferState;
  874. if (sbstate.vertexCount == 0 && sbstate.indexCount == 0)
  875. return;
  876. Attributes attributes;
  877. BufferBindings buffers;
  878. size_t usedsizes[3] = {0, 0, 0};
  879. for (int i = 0; i < 2; i++)
  880. {
  881. if (sbstate.formats[i] == CommonFormat::NONE)
  882. continue;
  883. attributes.setCommonFormat(sbstate.formats[i], (uint8) i);
  884. usedsizes[i] = getFormatStride(sbstate.formats[i]) * sbstate.vertexCount;
  885. size_t offset = sbstate.vb[i]->unmap(usedsizes[i]);
  886. buffers.set(i, sbstate.vb[i], offset);
  887. sbstate.vbMap[i] = StreamBuffer::MapInfo();
  888. }
  889. if (attributes.enableBits == 0)
  890. return;
  891. Colorf nc = getColor();
  892. if (attributes.isEnabled(ATTRIB_COLOR))
  893. setColor(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
  894. pushIdentityTransform();
  895. if (sbstate.indexCount > 0)
  896. {
  897. usedsizes[2] = sizeof(uint16) * sbstate.indexCount;
  898. DrawIndexedCommand cmd(&attributes, &buffers, sbstate.indexBuffer);
  899. cmd.primitiveType = sbstate.primitiveMode;
  900. cmd.indexCount = sbstate.indexCount;
  901. cmd.indexType = INDEX_UINT16;
  902. cmd.indexBufferOffset = sbstate.indexBuffer->unmap(usedsizes[2]);
  903. cmd.texture = sbstate.texture;
  904. draw(cmd);
  905. sbstate.indexBufferMap = StreamBuffer::MapInfo();
  906. }
  907. else
  908. {
  909. DrawCommand cmd(&attributes, &buffers);
  910. cmd.primitiveType = sbstate.primitiveMode;
  911. cmd.vertexStart = 0;
  912. cmd.vertexCount = sbstate.vertexCount;
  913. cmd.texture = sbstate.texture;
  914. draw(cmd);
  915. }
  916. for (int i = 0; i < 2; i++)
  917. {
  918. if (usedsizes[i] > 0)
  919. sbstate.vb[i]->markUsed(usedsizes[i]);
  920. }
  921. if (usedsizes[2] > 0)
  922. sbstate.indexBuffer->markUsed(usedsizes[2]);
  923. popTransform();
  924. if (attributes.isEnabled(ATTRIB_COLOR))
  925. setColor(nc);
  926. streamBufferState.vertexCount = 0;
  927. streamBufferState.indexCount = 0;
  928. }
  929. void Graphics::flushStreamDrawsGlobal()
  930. {
  931. Graphics *instance = getInstance<Graphics>(M_GRAPHICS);
  932. if (instance != nullptr)
  933. instance->flushStreamDraws();
  934. }
  935. /**
  936. * Drawing
  937. **/
  938. void Graphics::draw(Drawable *drawable, const Matrix4 &m)
  939. {
  940. drawable->draw(this, m);
  941. }
  942. void Graphics::draw(Texture *texture, Quad *quad, const Matrix4 &m)
  943. {
  944. texture->draw(this, quad, m);
  945. }
  946. void Graphics::drawLayer(Texture *texture, int layer, const Matrix4 &m)
  947. {
  948. texture->drawLayer(this, layer, m);
  949. }
  950. void Graphics::drawLayer(Texture *texture, int layer, Quad *quad, const Matrix4 &m)
  951. {
  952. texture->drawLayer(this, layer, quad, m);
  953. }
  954. void Graphics::drawInstanced(Mesh *mesh, const Matrix4 &m, int instancecount)
  955. {
  956. mesh->drawInstanced(this, m, instancecount);
  957. }
  958. void Graphics::print(const std::vector<Font::ColoredString> &str, const Matrix4 &m)
  959. {
  960. checkSetDefaultFont();
  961. if (states.back().font.get() != nullptr)
  962. print(str, states.back().font.get(), m);
  963. }
  964. void Graphics::print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m)
  965. {
  966. font->print(this, str, m, states.back().color);
  967. }
  968. void Graphics::printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m)
  969. {
  970. checkSetDefaultFont();
  971. if (states.back().font.get() != nullptr)
  972. printf(str, states.back().font.get(), wrap, align, m);
  973. }
  974. void Graphics::printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m)
  975. {
  976. font->printf(this, str, wrap, align, m, states.back().color);
  977. }
  978. /**
  979. * Primitives (points, shapes, lines).
  980. **/
  981. void Graphics::points(const Vector2 *positions, const Colorf *colors, size_t numpoints)
  982. {
  983. const Matrix4 &t = getTransform();
  984. bool is2D = t.isAffine2DTransform();
  985. StreamDrawCommand cmd;
  986. cmd.primitiveMode = PRIMITIVE_POINTS;
  987. cmd.formats[0] = vertex::getSinglePositionFormat(is2D);
  988. cmd.formats[1] = vertex::CommonFormat::RGBAub;
  989. cmd.vertexCount = (int) numpoints;
  990. StreamVertexData data = requestStreamDraw(cmd);
  991. if (is2D)
  992. t.transformXY((Vector2 *) data.stream[0], positions, cmd.vertexCount);
  993. else
  994. t.transformXY0((Vector3 *) data.stream[0], positions, cmd.vertexCount);
  995. Color32 *colordata = (Color32 *) data.stream[1];
  996. if (colors)
  997. {
  998. Colorf nc = getColor();
  999. gammaCorrectColor(nc);
  1000. if (isGammaCorrect())
  1001. {
  1002. for (int i = 0; i < cmd.vertexCount; i++)
  1003. {
  1004. Colorf ci = colors[i];
  1005. gammaCorrectColor(ci);
  1006. ci *= nc;
  1007. unGammaCorrectColor(ci);
  1008. colordata[i] = toColor32(ci);
  1009. }
  1010. }
  1011. else
  1012. {
  1013. for (int i = 0; i < cmd.vertexCount; i++)
  1014. colordata[i] = toColor32(nc * colors[i]);
  1015. }
  1016. }
  1017. else
  1018. {
  1019. Color32 c = toColor32(getColor());
  1020. for (int i = 0; i < cmd.vertexCount; i++)
  1021. colordata[i] = c;
  1022. }
  1023. }
  1024. int Graphics::calculateEllipsePoints(float rx, float ry) const
  1025. {
  1026. int points = (int) sqrtf(((rx + ry) / 2.0f) * 20.0f * (float) pixelScaleStack.back());
  1027. return std::max(points, 8);
  1028. }
  1029. void Graphics::polyline(const Vector2 *vertices, size_t count)
  1030. {
  1031. float halfwidth = getLineWidth() * 0.5f;
  1032. LineJoin linejoin = getLineJoin();
  1033. LineStyle linestyle = getLineStyle();
  1034. float pixelsize = 1.0f / std::max((float) pixelScaleStack.back(), 0.000001f);
  1035. if (linejoin == LINE_JOIN_NONE)
  1036. {
  1037. NoneJoinPolyline line;
  1038. line.render(vertices, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
  1039. line.draw(this);
  1040. }
  1041. else if (linejoin == LINE_JOIN_BEVEL)
  1042. {
  1043. BevelJoinPolyline line;
  1044. line.render(vertices, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
  1045. line.draw(this);
  1046. }
  1047. else if (linejoin == LINE_JOIN_MITER)
  1048. {
  1049. MiterJoinPolyline line;
  1050. line.render(vertices, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
  1051. line.draw(this);
  1052. }
  1053. }
  1054. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
  1055. {
  1056. Vector2 coords[] = {Vector2(x,y), Vector2(x,y+h), Vector2(x+w,y+h), Vector2(x+w,y), Vector2(x,y)};
  1057. polygon(mode, coords, 5);
  1058. }
  1059. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points)
  1060. {
  1061. if (rx == 0 || ry == 0)
  1062. {
  1063. rectangle(mode, x, y, w, h);
  1064. return;
  1065. }
  1066. // Radius values that are more than half the rectangle's size aren't handled
  1067. // correctly (for now)...
  1068. if (w >= 0.02f)
  1069. rx = std::min(rx, w / 2.0f - 0.01f);
  1070. if (h >= 0.02f)
  1071. ry = std::min(ry, h / 2.0f - 0.01f);
  1072. points = std::max(points / 4, 1);
  1073. const float half_pi = static_cast<float>(LOVE_M_PI / 2);
  1074. float angle_shift = half_pi / ((float) points + 1.0f);
  1075. int num_coords = (points + 2) * 4;
  1076. Vector2 *coords = getScratchBuffer<Vector2>(num_coords + 1);
  1077. float phi = .0f;
  1078. for (int i = 0; i <= points + 2; ++i, phi += angle_shift)
  1079. {
  1080. coords[i].x = x + rx * (1 - cosf(phi));
  1081. coords[i].y = y + ry * (1 - sinf(phi));
  1082. }
  1083. phi = half_pi;
  1084. for (int i = points + 2; i <= 2 * (points + 2); ++i, phi += angle_shift)
  1085. {
  1086. coords[i].x = x + w - rx * (1 + cosf(phi));
  1087. coords[i].y = y + ry * (1 - sinf(phi));
  1088. }
  1089. phi = 2 * half_pi;
  1090. for (int i = 2 * (points + 2); i <= 3 * (points + 2); ++i, phi += angle_shift)
  1091. {
  1092. coords[i].x = x + w - rx * (1 + cosf(phi));
  1093. coords[i].y = y + h - ry * (1 + sinf(phi));
  1094. }
  1095. phi = 3 * half_pi;
  1096. for (int i = 3 * (points + 2); i <= 4 * (points + 2); ++i, phi += angle_shift)
  1097. {
  1098. coords[i].x = x + rx * (1 - cosf(phi));
  1099. coords[i].y = y + h - ry * (1 + sinf(phi));
  1100. }
  1101. coords[num_coords] = coords[0];
  1102. polygon(mode, coords, num_coords + 1);
  1103. }
  1104. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry)
  1105. {
  1106. rectangle(mode, x, y, w, h, rx, ry, calculateEllipsePoints(rx, ry));
  1107. }
  1108. void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
  1109. {
  1110. ellipse(mode, x, y, radius, radius, points);
  1111. }
  1112. void Graphics::circle(DrawMode mode, float x, float y, float radius)
  1113. {
  1114. ellipse(mode, x, y, radius, radius);
  1115. }
  1116. void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
  1117. {
  1118. float two_pi = (float) (LOVE_M_PI * 2);
  1119. if (points <= 0) points = 1;
  1120. float angle_shift = (two_pi / points);
  1121. float phi = .0f;
  1122. // 1 extra point at the end for a closed loop, and 1 extra point at the
  1123. // start in filled mode for the vertex in the center of the ellipse.
  1124. int extrapoints = 1 + (mode == DRAW_FILL ? 1 : 0);
  1125. Vector2 *polygoncoords = getScratchBuffer<Vector2>(points + extrapoints);
  1126. Vector2 *coords = polygoncoords;
  1127. if (mode == DRAW_FILL)
  1128. {
  1129. coords[0].x = x;
  1130. coords[0].y = y;
  1131. coords++;
  1132. }
  1133. for (int i = 0; i < points; ++i, phi += angle_shift)
  1134. {
  1135. coords[i].x = x + a * cosf(phi);
  1136. coords[i].y = y + b * sinf(phi);
  1137. }
  1138. coords[points] = coords[0];
  1139. // Last argument to polygon(): don't skip the last vertex in fill mode.
  1140. polygon(mode, polygoncoords, points + extrapoints, false);
  1141. }
  1142. void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
  1143. {
  1144. ellipse(mode, x, y, a, b, calculateEllipsePoints(a, b));
  1145. }
  1146. void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
  1147. {
  1148. // Nothing to display with no points or equal angles. (Or is there with line mode?)
  1149. if (points <= 0 || angle1 == angle2)
  1150. return;
  1151. // Oh, you want to draw a circle?
  1152. if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
  1153. {
  1154. circle(drawmode, x, y, radius, points);
  1155. return;
  1156. }
  1157. float angle_shift = (angle2 - angle1) / points;
  1158. // Bail on precision issues.
  1159. if (angle_shift == 0.0)
  1160. return;
  1161. // Prevent the connecting line from being drawn if a closed line arc has a
  1162. // small angle. Avoids some visual issues when connected lines are at sharp
  1163. // angles, due to the miter line join drawing code.
  1164. if (drawmode == DRAW_LINE && arcmode == ARC_CLOSED && fabsf(angle1 - angle2) < LOVE_TORAD(4))
  1165. arcmode = ARC_OPEN;
  1166. // Quick fix for the last part of a filled open arc not being drawn (because
  1167. // polygon(DRAW_FILL, ...) doesn't work without a closed loop of vertices.)
  1168. if (drawmode == DRAW_FILL && arcmode == ARC_OPEN)
  1169. arcmode = ARC_CLOSED;
  1170. float phi = angle1;
  1171. Vector2 *coords = nullptr;
  1172. int num_coords = 0;
  1173. const auto createPoints = [&](Vector2 *coordinates)
  1174. {
  1175. for (int i = 0; i <= points; ++i, phi += angle_shift)
  1176. {
  1177. coordinates[i].x = x + radius * cosf(phi);
  1178. coordinates[i].y = y + radius * sinf(phi);
  1179. }
  1180. };
  1181. if (arcmode == ARC_PIE)
  1182. {
  1183. num_coords = points + 3;
  1184. coords = getScratchBuffer<Vector2>(num_coords);
  1185. coords[0] = coords[num_coords - 1] = Vector2(x, y);
  1186. createPoints(coords + 1);
  1187. }
  1188. else if (arcmode == ARC_OPEN)
  1189. {
  1190. num_coords = points + 1;
  1191. coords = getScratchBuffer<Vector2>(num_coords);
  1192. createPoints(coords);
  1193. }
  1194. else // ARC_CLOSED
  1195. {
  1196. num_coords = points + 2;
  1197. coords = getScratchBuffer<Vector2>(num_coords);
  1198. createPoints(coords);
  1199. // Connect the ends of the arc.
  1200. coords[num_coords - 1] = coords[0];
  1201. }
  1202. polygon(drawmode, coords, num_coords);
  1203. }
  1204. void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2)
  1205. {
  1206. float points = (float) calculateEllipsePoints(radius, radius);
  1207. // The amount of points is based on the fraction of the circle created by the arc.
  1208. float angle = fabsf(angle1 - angle2);
  1209. if (angle < 2.0f * (float) LOVE_M_PI)
  1210. points *= angle / (2.0f * (float) LOVE_M_PI);
  1211. arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
  1212. }
  1213. void Graphics::polygon(DrawMode mode, const Vector2 *coords, size_t count, bool skipLastFilledVertex)
  1214. {
  1215. // coords is an array of a closed loop of vertices, i.e.
  1216. // coords[count-1] == coords[0]
  1217. if (mode == DRAW_LINE)
  1218. {
  1219. polyline(coords, count);
  1220. }
  1221. else
  1222. {
  1223. const Matrix4 &t = getTransform();
  1224. bool is2D = t.isAffine2DTransform();
  1225. StreamDrawCommand cmd;
  1226. cmd.formats[0] = vertex::getSinglePositionFormat(is2D);
  1227. cmd.formats[1] = vertex::CommonFormat::RGBAub;
  1228. cmd.indexMode = vertex::TriangleIndexMode::FAN;
  1229. cmd.vertexCount = (int)count - (skipLastFilledVertex ? 1 : 0);
  1230. StreamVertexData data = requestStreamDraw(cmd);
  1231. if (is2D)
  1232. t.transformXY((Vector2 *) data.stream[0], coords, cmd.vertexCount);
  1233. else
  1234. t.transformXY0((Vector3 *) data.stream[0], coords, cmd.vertexCount);
  1235. Color32 c = toColor32(getColor());
  1236. Color32 *colordata = (Color32 *) data.stream[1];
  1237. for (int i = 0; i < cmd.vertexCount; i++)
  1238. colordata[i] = c;
  1239. }
  1240. }
  1241. const Graphics::Capabilities &Graphics::getCapabilities() const
  1242. {
  1243. return capabilities;
  1244. }
  1245. Graphics::Stats Graphics::getStats() const
  1246. {
  1247. Stats stats;
  1248. getAPIStats(stats.shaderSwitches);
  1249. stats.drawCalls = drawCalls;
  1250. if (streamBufferState.vertexCount > 0)
  1251. stats.drawCalls++;
  1252. stats.canvasSwitches = canvasSwitchCount;
  1253. stats.drawCallsBatched = drawCallsBatched;
  1254. stats.canvases = Canvas::canvasCount;
  1255. stats.images = Image::imageCount;
  1256. stats.fonts = Font::fontCount;
  1257. stats.textureMemory = Texture::totalGraphicsMemory;
  1258. return stats;
  1259. }
  1260. size_t Graphics::getStackDepth() const
  1261. {
  1262. return stackTypeStack.size();
  1263. }
  1264. void Graphics::push(StackType type)
  1265. {
  1266. if (stackTypeStack.size() == MAX_USER_STACK_DEPTH)
  1267. throw Exception("Maximum stack depth reached (more pushes than pops?)");
  1268. pushTransform();
  1269. pixelScaleStack.push_back(pixelScaleStack.back());
  1270. if (type == STACK_ALL)
  1271. states.push_back(states.back());
  1272. stackTypeStack.push_back(type);
  1273. }
  1274. void Graphics::pop()
  1275. {
  1276. if (stackTypeStack.size() < 1)
  1277. throw Exception("Minimum stack depth reached (more pops than pushes?)");
  1278. popTransform();
  1279. pixelScaleStack.pop_back();
  1280. if (stackTypeStack.back() == STACK_ALL)
  1281. {
  1282. DisplayState &newstate = states[states.size() - 2];
  1283. restoreStateChecked(newstate);
  1284. // The last two states in the stack should be equal now.
  1285. states.pop_back();
  1286. }
  1287. stackTypeStack.pop_back();
  1288. }
  1289. /**
  1290. * Transform and stack functions.
  1291. **/
  1292. const Matrix4 &Graphics::getTransform() const
  1293. {
  1294. return transformStack.back();
  1295. }
  1296. const Matrix4 &Graphics::getProjection() const
  1297. {
  1298. return projectionMatrix;
  1299. }
  1300. void Graphics::pushTransform()
  1301. {
  1302. transformStack.push_back(transformStack.back());
  1303. }
  1304. void Graphics::pushIdentityTransform()
  1305. {
  1306. transformStack.push_back(Matrix4());
  1307. }
  1308. void Graphics::popTransform()
  1309. {
  1310. transformStack.pop_back();
  1311. }
  1312. void Graphics::rotate(float r)
  1313. {
  1314. transformStack.back().rotate(r);
  1315. }
  1316. void Graphics::scale(float x, float y)
  1317. {
  1318. transformStack.back().scale(x, y);
  1319. pixelScaleStack.back() *= (fabs(x) + fabs(y)) / 2.0;
  1320. }
  1321. void Graphics::translate(float x, float y)
  1322. {
  1323. transformStack.back().translate(x, y);
  1324. }
  1325. void Graphics::shear(float kx, float ky)
  1326. {
  1327. transformStack.back().shear(kx, ky);
  1328. }
  1329. void Graphics::origin()
  1330. {
  1331. transformStack.back().setIdentity();
  1332. pixelScaleStack.back() = 1;
  1333. }
  1334. void Graphics::applyTransform(love::math::Transform *transform)
  1335. {
  1336. Matrix4 &m = transformStack.back();
  1337. m *= transform->getMatrix();
  1338. float sx, sy;
  1339. m.getApproximateScale(sx, sy);
  1340. pixelScaleStack.back() = (sx + sy) / 2.0;
  1341. }
  1342. void Graphics::replaceTransform(love::math::Transform *transform)
  1343. {
  1344. const Matrix4 &m = transform->getMatrix();
  1345. transformStack.back() = m;
  1346. float sx, sy;
  1347. m.getApproximateScale(sx, sy);
  1348. pixelScaleStack.back() = (sx + sy) / 2.0;
  1349. }
  1350. Vector2 Graphics::transformPoint(Vector2 point)
  1351. {
  1352. Vector2 p;
  1353. transformStack.back().transformXY(&p, &point, 1);
  1354. return p;
  1355. }
  1356. Vector2 Graphics::inverseTransformPoint(Vector2 point)
  1357. {
  1358. Vector2 p;
  1359. // TODO: We should probably cache the inverse transform so we don't have to
  1360. // re-calculate it every time this is called.
  1361. transformStack.back().inverse().transformXY(&p, &point, 1);
  1362. return p;
  1363. }
  1364. const Graphics::DefaultShaderCode &Graphics::getCurrentDefaultShaderCode() const
  1365. {
  1366. int languageindex = (int) getShaderLanguageTarget();
  1367. int gammaindex = isGammaCorrect() ? 1 : 0;
  1368. return defaultShaderCode[Shader::STANDARD_DEFAULT][languageindex][gammaindex];
  1369. }
  1370. /**
  1371. * Constants.
  1372. **/
  1373. bool Graphics::getConstant(const char *in, DrawMode &out)
  1374. {
  1375. return drawModes.find(in, out);
  1376. }
  1377. bool Graphics::getConstant(DrawMode in, const char *&out)
  1378. {
  1379. return drawModes.find(in, out);
  1380. }
  1381. std::vector<std::string> Graphics::getConstants(DrawMode)
  1382. {
  1383. return drawModes.getNames();
  1384. }
  1385. bool Graphics::getConstant(const char *in, ArcMode &out)
  1386. {
  1387. return arcModes.find(in, out);
  1388. }
  1389. bool Graphics::getConstant(ArcMode in, const char *&out)
  1390. {
  1391. return arcModes.find(in, out);
  1392. }
  1393. std::vector<std::string> Graphics::getConstants(ArcMode)
  1394. {
  1395. return arcModes.getNames();
  1396. }
  1397. bool Graphics::getConstant(const char *in, BlendMode &out)
  1398. {
  1399. return blendModes.find(in, out);
  1400. }
  1401. bool Graphics::getConstant(BlendMode in, const char *&out)
  1402. {
  1403. return blendModes.find(in, out);
  1404. }
  1405. std::vector<std::string> Graphics::getConstants(BlendMode)
  1406. {
  1407. return blendModes.getNames();
  1408. }
  1409. bool Graphics::getConstant(const char *in, BlendAlpha &out)
  1410. {
  1411. return blendAlphaModes.find(in, out);
  1412. }
  1413. bool Graphics::getConstant(BlendAlpha in, const char *&out)
  1414. {
  1415. return blendAlphaModes.find(in, out);
  1416. }
  1417. std::vector<std::string> Graphics::getConstants(BlendAlpha)
  1418. {
  1419. return blendAlphaModes.getNames();
  1420. }
  1421. bool Graphics::getConstant(const char *in, LineStyle &out)
  1422. {
  1423. return lineStyles.find(in, out);
  1424. }
  1425. bool Graphics::getConstant(LineStyle in, const char *&out)
  1426. {
  1427. return lineStyles.find(in, out);
  1428. }
  1429. std::vector<std::string> Graphics::getConstants(LineStyle)
  1430. {
  1431. return lineStyles.getNames();
  1432. }
  1433. bool Graphics::getConstant(const char *in, LineJoin &out)
  1434. {
  1435. return lineJoins.find(in, out);
  1436. }
  1437. bool Graphics::getConstant(LineJoin in, const char *&out)
  1438. {
  1439. return lineJoins.find(in, out);
  1440. }
  1441. std::vector<std::string> Graphics::getConstants(LineJoin)
  1442. {
  1443. return lineJoins.getNames();
  1444. }
  1445. bool Graphics::getConstant(const char *in, Feature &out)
  1446. {
  1447. return features.find(in, out);
  1448. }
  1449. bool Graphics::getConstant(Feature in, const char *&out)
  1450. {
  1451. return features.find(in, out);
  1452. }
  1453. bool Graphics::getConstant(const char *in, SystemLimit &out)
  1454. {
  1455. return systemLimits.find(in, out);
  1456. }
  1457. bool Graphics::getConstant(SystemLimit in, const char *&out)
  1458. {
  1459. return systemLimits.find(in, out);
  1460. }
  1461. bool Graphics::getConstant(const char *in, StackType &out)
  1462. {
  1463. return stackTypes.find(in, out);
  1464. }
  1465. bool Graphics::getConstant(StackType in, const char *&out)
  1466. {
  1467. return stackTypes.find(in, out);
  1468. }
  1469. std::vector<std::string> Graphics::getConstants(StackType)
  1470. {
  1471. return stackTypes.getNames();
  1472. }
  1473. StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM>::Entry Graphics::drawModeEntries[] =
  1474. {
  1475. { "line", DRAW_LINE },
  1476. { "fill", DRAW_FILL },
  1477. };
  1478. StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM> Graphics::drawModes(Graphics::drawModeEntries, sizeof(Graphics::drawModeEntries));
  1479. StringMap<Graphics::ArcMode, Graphics::ARC_MAX_ENUM>::Entry Graphics::arcModeEntries[] =
  1480. {
  1481. { "open", ARC_OPEN },
  1482. { "closed", ARC_CLOSED },
  1483. { "pie", ARC_PIE },
  1484. };
  1485. StringMap<Graphics::ArcMode, Graphics::ARC_MAX_ENUM> Graphics::arcModes(Graphics::arcModeEntries, sizeof(Graphics::arcModeEntries));
  1486. StringMap<Graphics::BlendMode, Graphics::BLEND_MAX_ENUM>::Entry Graphics::blendModeEntries[] =
  1487. {
  1488. { "alpha", BLEND_ALPHA },
  1489. { "add", BLEND_ADD },
  1490. { "subtract", BLEND_SUBTRACT },
  1491. { "multiply", BLEND_MULTIPLY },
  1492. { "lighten", BLEND_LIGHTEN },
  1493. { "darken", BLEND_DARKEN },
  1494. { "screen", BLEND_SCREEN },
  1495. { "replace", BLEND_REPLACE },
  1496. { "none", BLEND_NONE },
  1497. };
  1498. StringMap<Graphics::BlendMode, Graphics::BLEND_MAX_ENUM> Graphics::blendModes(Graphics::blendModeEntries, sizeof(Graphics::blendModeEntries));
  1499. StringMap<Graphics::BlendAlpha, Graphics::BLENDALPHA_MAX_ENUM>::Entry Graphics::blendAlphaEntries[] =
  1500. {
  1501. { "alphamultiply", BLENDALPHA_MULTIPLY },
  1502. { "premultiplied", BLENDALPHA_PREMULTIPLIED },
  1503. };
  1504. StringMap<Graphics::BlendAlpha, Graphics::BLENDALPHA_MAX_ENUM> Graphics::blendAlphaModes(Graphics::blendAlphaEntries, sizeof(Graphics::blendAlphaEntries));
  1505. StringMap<Graphics::LineStyle, Graphics::LINE_MAX_ENUM>::Entry Graphics::lineStyleEntries[] =
  1506. {
  1507. { "smooth", LINE_SMOOTH },
  1508. { "rough", LINE_ROUGH }
  1509. };
  1510. StringMap<Graphics::LineStyle, Graphics::LINE_MAX_ENUM> Graphics::lineStyles(Graphics::lineStyleEntries, sizeof(Graphics::lineStyleEntries));
  1511. StringMap<Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM>::Entry Graphics::lineJoinEntries[] =
  1512. {
  1513. { "none", LINE_JOIN_NONE },
  1514. { "miter", LINE_JOIN_MITER },
  1515. { "bevel", LINE_JOIN_BEVEL }
  1516. };
  1517. StringMap<Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM> Graphics::lineJoins(Graphics::lineJoinEntries, sizeof(Graphics::lineJoinEntries));
  1518. StringMap<Graphics::Feature, Graphics::FEATURE_MAX_ENUM>::Entry Graphics::featureEntries[] =
  1519. {
  1520. { "multicanvasformats", FEATURE_MULTI_CANVAS_FORMATS },
  1521. { "clampzero", FEATURE_CLAMP_ZERO },
  1522. { "lighten", FEATURE_LIGHTEN },
  1523. { "fullnpot", FEATURE_FULL_NPOT },
  1524. { "pixelshaderhighp", FEATURE_PIXEL_SHADER_HIGHP },
  1525. { "shaderderivatives", FEATURE_SHADER_DERIVATIVES },
  1526. { "glsl3", FEATURE_GLSL3 },
  1527. { "glsl4", FEATURE_GLSL4 },
  1528. { "instancing", FEATURE_INSTANCING },
  1529. };
  1530. StringMap<Graphics::Feature, Graphics::FEATURE_MAX_ENUM> Graphics::features(Graphics::featureEntries, sizeof(Graphics::featureEntries));
  1531. StringMap<Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM>::Entry Graphics::systemLimitEntries[] =
  1532. {
  1533. { "pointsize", LIMIT_POINT_SIZE },
  1534. { "texturesize", LIMIT_TEXTURE_SIZE },
  1535. { "texturelayers", LIMIT_TEXTURE_LAYERS },
  1536. { "volumetexturesize", LIMIT_VOLUME_TEXTURE_SIZE },
  1537. { "cubetexturesize", LIMIT_CUBE_TEXTURE_SIZE },
  1538. { "multicanvas", LIMIT_MULTI_CANVAS },
  1539. { "canvasmsaa", LIMIT_CANVAS_MSAA },
  1540. { "anisotropy", LIMIT_ANISOTROPY },
  1541. };
  1542. StringMap<Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM> Graphics::systemLimits(Graphics::systemLimitEntries, sizeof(Graphics::systemLimitEntries));
  1543. StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM>::Entry Graphics::stackTypeEntries[] =
  1544. {
  1545. { "all", STACK_ALL },
  1546. { "transform", STACK_TRANSFORM },
  1547. };
  1548. StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM> Graphics::stackTypes(Graphics::stackTypeEntries, sizeof(Graphics::stackTypeEntries));
  1549. } // graphics
  1550. } // love