Graphics.cpp 50 KB

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