Graphics.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /**
  2. * Copyright (c) 2006-2017 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 "Polyline.h"
  25. #include "font/Font.h"
  26. #include "window/Window.h"
  27. #include "Font.h"
  28. #include "Video.h"
  29. // C++
  30. #include <algorithm>
  31. #include <stdlib.h>
  32. namespace love
  33. {
  34. namespace graphics
  35. {
  36. static bool gammaCorrect = false;
  37. static bool debugMode = false;
  38. static bool debugModeQueried = false;
  39. void setGammaCorrect(bool gammacorrect)
  40. {
  41. gammaCorrect = gammacorrect;
  42. }
  43. bool isGammaCorrect()
  44. {
  45. return gammaCorrect;
  46. }
  47. void gammaCorrectColor(Colorf &c)
  48. {
  49. if (isGammaCorrect())
  50. {
  51. c.r = math::gammaToLinear(c.r);
  52. c.g = math::gammaToLinear(c.g);
  53. c.b = math::gammaToLinear(c.b);
  54. }
  55. }
  56. Colorf gammaCorrectColor(const Colorf &c)
  57. {
  58. Colorf r = c;
  59. gammaCorrectColor(r);
  60. return r;
  61. }
  62. void unGammaCorrectColor(Colorf &c)
  63. {
  64. if (isGammaCorrect())
  65. {
  66. c.r = math::linearToGamma(c.r);
  67. c.g = math::linearToGamma(c.g);
  68. c.b = math::linearToGamma(c.b);
  69. }
  70. }
  71. Colorf unGammaCorrectColor(const Colorf &c)
  72. {
  73. Colorf r = c;
  74. unGammaCorrectColor(r);
  75. return r;
  76. }
  77. bool isDebugEnabled()
  78. {
  79. if (!debugModeQueried)
  80. {
  81. const char *debugenv = getenv("LOVE_GRAPHICS_DEBUG");
  82. debugMode = debugenv != nullptr && debugenv[0] != '0';
  83. debugModeQueried = true;
  84. }
  85. return debugMode;
  86. }
  87. love::Type Graphics::type("graphics", &Module::type);
  88. Shader::ShaderSource Graphics::defaultShaderCode[Shader::STANDARD_MAX_ENUM][Shader::LANGUAGE_MAX_ENUM][2];
  89. Graphics::Graphics()
  90. : width(0)
  91. , height(0)
  92. , pixelWidth(0)
  93. , pixelHeight(0)
  94. , created(false)
  95. , active(true)
  96. , writingToStencil(false)
  97. , streamBufferState()
  98. , projectionMatrix()
  99. , canvasSwitchCount(0)
  100. {
  101. transformStack.reserve(16);
  102. transformStack.push_back(Matrix4());
  103. pixelScaleStack.reserve(16);
  104. pixelScaleStack.push_back(1);
  105. if (!Shader::initialize())
  106. throw love::Exception("Shader support failed to initialize!");
  107. }
  108. Graphics::~Graphics()
  109. {
  110. states.clear();
  111. defaultFont.set(nullptr);
  112. for (int i = 0; i < Shader::STANDARD_MAX_ENUM; i++)
  113. {
  114. if (Shader::standardShaders[i])
  115. {
  116. Shader::standardShaders[i]->release();
  117. Shader::standardShaders[i] = nullptr;
  118. }
  119. }
  120. delete streamBufferState.vb[0];
  121. delete streamBufferState.vb[1];
  122. delete streamBufferState.indexBuffer;
  123. Shader::deinitialize();
  124. }
  125. Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
  126. {
  127. return new Quad(v, sw, sh);
  128. }
  129. Font *Graphics::newFont(love::font::Rasterizer *data, const Texture::Filter &filter)
  130. {
  131. return new Font(data, filter);
  132. }
  133. Video *Graphics::newVideo(love::video::VideoStream *stream, float pixeldensity)
  134. {
  135. return new Video(this, stream, pixeldensity);
  136. }
  137. bool Graphics::validateShader(bool gles, const Shader::ShaderSource &source, std::string &err)
  138. {
  139. return Shader::validate(this, gles, source, true, err);
  140. }
  141. int Graphics::getWidth() const
  142. {
  143. return width;
  144. }
  145. int Graphics::getHeight() const
  146. {
  147. return height;
  148. }
  149. int Graphics::getPixelWidth() const
  150. {
  151. return pixelWidth;
  152. }
  153. int Graphics::getPixelHeight() const
  154. {
  155. return pixelHeight;
  156. }
  157. double Graphics::getCurrentPixelDensity() const
  158. {
  159. if (states.back().renderTargets.colors.size() > 0)
  160. {
  161. Canvas *c = states.back().renderTargets.colors[0].canvas;
  162. return (double) c->getPixelHeight() / (double) c->getHeight();
  163. }
  164. return getScreenPixelDensity();
  165. }
  166. double Graphics::getScreenPixelDensity() const
  167. {
  168. return (double) getPixelHeight() / (double) getHeight();
  169. }
  170. bool Graphics::isCreated() const
  171. {
  172. return created;
  173. }
  174. bool Graphics::isActive() const
  175. {
  176. // The graphics module is only completely 'active' if there's a window, a
  177. // context, and the active variable is set.
  178. auto window = getInstance<love::window::Window>(M_WINDOW);
  179. return active && isCreated() && window != nullptr && window->isOpen();
  180. }
  181. void Graphics::reset()
  182. {
  183. DisplayState s;
  184. stopDrawToStencilBuffer();
  185. restoreState(s);
  186. origin();
  187. }
  188. /**
  189. * State functions.
  190. **/
  191. void Graphics::restoreState(const DisplayState &s)
  192. {
  193. setColor(s.color);
  194. setBackgroundColor(s.backgroundColor);
  195. setBlendMode(s.blendMode, s.blendAlphaMode);
  196. setLineWidth(s.lineWidth);
  197. setLineStyle(s.lineStyle);
  198. setLineJoin(s.lineJoin);
  199. setPointSize(s.pointSize);
  200. if (s.scissor)
  201. setScissor(s.scissorRect);
  202. else
  203. setScissor();
  204. setStencilTest(s.stencilCompare, s.stencilTestValue);
  205. setFont(s.font.get());
  206. setShader(s.shader.get());
  207. setCanvas(s.renderTargets);
  208. setColorMask(s.colorMask);
  209. setWireframe(s.wireframe);
  210. setDefaultFilter(s.defaultFilter);
  211. setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
  212. }
  213. void Graphics::restoreStateChecked(const DisplayState &s)
  214. {
  215. const DisplayState &cur = states.back();
  216. if (s.color != cur.color)
  217. setColor(s.color);
  218. setBackgroundColor(s.backgroundColor);
  219. if (s.blendMode != cur.blendMode || s.blendAlphaMode != cur.blendAlphaMode)
  220. setBlendMode(s.blendMode, s.blendAlphaMode);
  221. // These are just simple assignments.
  222. setLineWidth(s.lineWidth);
  223. setLineStyle(s.lineStyle);
  224. setLineJoin(s.lineJoin);
  225. if (s.pointSize != cur.pointSize)
  226. setPointSize(s.pointSize);
  227. if (s.scissor != cur.scissor || (s.scissor && !(s.scissorRect == cur.scissorRect)))
  228. {
  229. if (s.scissor)
  230. setScissor(s.scissorRect);
  231. else
  232. setScissor();
  233. }
  234. if (s.stencilCompare != cur.stencilCompare || s.stencilTestValue != cur.stencilTestValue)
  235. setStencilTest(s.stencilCompare, s.stencilTestValue);
  236. setFont(s.font.get());
  237. setShader(s.shader.get());
  238. const auto &sRTs = s.renderTargets;
  239. const auto &curRTs = cur.renderTargets;
  240. bool canvaseschanged = sRTs.colors.size() != curRTs.colors.size();
  241. if (!canvaseschanged)
  242. {
  243. for (size_t i = 0; i < sRTs.colors.size() && i < curRTs.colors.size(); i++)
  244. {
  245. const auto &rt1 = sRTs.colors[i];
  246. const auto &rt2 = curRTs.colors[i];
  247. if (rt1.canvas.get() != rt2.canvas.get() || rt1.slice != rt2.slice || rt1.mipmap != rt2.mipmap)
  248. {
  249. canvaseschanged = true;
  250. break;
  251. }
  252. }
  253. if (!canvaseschanged && (sRTs.depthStencil.canvas.get() != curRTs.depthStencil.canvas.get()
  254. || sRTs.depthStencil.slice != curRTs.depthStencil.slice
  255. || sRTs.depthStencil.mipmap != curRTs.depthStencil.mipmap))
  256. {
  257. canvaseschanged = true;
  258. }
  259. if (sRTs.temporaryRTFlags != curRTs.temporaryRTFlags)
  260. canvaseschanged = true;
  261. }
  262. if (canvaseschanged)
  263. setCanvas(s.renderTargets);
  264. if (s.colorMask != cur.colorMask)
  265. setColorMask(s.colorMask);
  266. if (s.wireframe != cur.wireframe)
  267. setWireframe(s.wireframe);
  268. setDefaultFilter(s.defaultFilter);
  269. setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
  270. }
  271. Colorf Graphics::getColor() const
  272. {
  273. return states.back().color;
  274. }
  275. void Graphics::setBackgroundColor(Colorf c)
  276. {
  277. states.back().backgroundColor = c;
  278. }
  279. Colorf Graphics::getBackgroundColor() const
  280. {
  281. return states.back().backgroundColor;
  282. }
  283. void Graphics::checkSetDefaultFont()
  284. {
  285. // We don't create or set the default Font if an existing font is in use.
  286. if (states.back().font.get() != nullptr)
  287. return;
  288. // Create a new default font if we don't have one yet.
  289. if (!defaultFont.get())
  290. {
  291. auto fontmodule = Module::getInstance<font::Font>(M_FONT);
  292. if (!fontmodule)
  293. throw love::Exception("Font module has not been loaded.");
  294. auto hinting = font::TrueTypeRasterizer::HINTING_NORMAL;
  295. StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(12, hinting), Acquire::NORETAIN);
  296. defaultFont.set(newFont(r.get()), Acquire::NORETAIN);
  297. }
  298. states.back().font.set(defaultFont.get());
  299. }
  300. void Graphics::setFont(love::graphics::Font *font)
  301. {
  302. // We don't need to set a default font here if null is passed in, since we
  303. // only care about the default font in getFont and print.
  304. DisplayState &state = states.back();
  305. state.font.set(font);
  306. }
  307. love::graphics::Font *Graphics::getFont()
  308. {
  309. checkSetDefaultFont();
  310. return states.back().font.get();
  311. }
  312. void Graphics::setShader(love::graphics::Shader *shader)
  313. {
  314. if (shader == nullptr)
  315. return setShader();
  316. shader->attach();
  317. states.back().shader.set(shader);
  318. }
  319. void Graphics::setShader()
  320. {
  321. Shader::attachDefault(Shader::STANDARD_DEFAULT);
  322. states.back().shader.set(nullptr);
  323. }
  324. love::graphics::Shader *Graphics::getShader() const
  325. {
  326. return states.back().shader.get();
  327. }
  328. void Graphics::setCanvas(RenderTarget rt, uint32 temporaryRTFlags)
  329. {
  330. if (rt.canvas == nullptr)
  331. return setCanvas();
  332. RenderTargets rts;
  333. rts.colors.push_back(rt);
  334. rts.temporaryRTFlags = temporaryRTFlags;
  335. setCanvas(rts);
  336. }
  337. void Graphics::setCanvas(const RenderTargetsStrongRef &rts)
  338. {
  339. RenderTargets targets;
  340. targets.colors.reserve(rts.colors.size());
  341. for (const auto &rt : rts.colors)
  342. targets.colors.emplace_back(rt.canvas.get(), rt.slice, rt.mipmap);
  343. targets.depthStencil = RenderTarget(rts.depthStencil.canvas, rts.depthStencil.slice, rts.depthStencil.mipmap);
  344. targets.temporaryRTFlags = rts.temporaryRTFlags;
  345. return setCanvas(targets);
  346. }
  347. Graphics::RenderTargets Graphics::getCanvas() const
  348. {
  349. const auto &curRTs = states.back().renderTargets;
  350. RenderTargets rts;
  351. rts.colors.reserve(curRTs.colors.size());
  352. for (const auto &rt : curRTs.colors)
  353. rts.colors.emplace_back(rt.canvas.get(), rt.slice, rt.mipmap);
  354. rts.depthStencil = RenderTarget(curRTs.depthStencil.canvas, curRTs.depthStencil.slice, curRTs.depthStencil.mipmap);
  355. rts.temporaryRTFlags = curRTs.temporaryRTFlags;
  356. return rts;
  357. }
  358. bool Graphics::isCanvasActive() const
  359. {
  360. const auto &rts = states.back().renderTargets;
  361. return !rts.colors.empty() || rts.depthStencil.canvas != nullptr;
  362. }
  363. bool Graphics::isCanvasActive(love::graphics::Canvas *canvas) const
  364. {
  365. const auto &rts = states.back().renderTargets;
  366. for (const auto &rt : rts.colors)
  367. {
  368. if (rt.canvas.get() == canvas)
  369. return true;
  370. }
  371. if (rts.depthStencil.canvas.get() == canvas)
  372. return true;
  373. return false;
  374. }
  375. bool Graphics::isCanvasActive(Canvas *canvas, int slice) const
  376. {
  377. const auto &rts = states.back().renderTargets;
  378. for (const auto &rt : rts.colors)
  379. {
  380. if (rt.canvas.get() == canvas && rt.slice == slice)
  381. return true;
  382. }
  383. if (rts.depthStencil.canvas.get() == canvas && rts.depthStencil.slice == slice)
  384. return true;
  385. return false;
  386. }
  387. Canvas *Graphics::getTemporaryCanvas(PixelFormat format, int w, int h, int samples)
  388. {
  389. love::graphics::Canvas *canvas = nullptr;
  390. for (Canvas *c : temporaryCanvases)
  391. {
  392. if (c->getPixelFormat() == format && c->getPixelWidth() == w
  393. && c->getPixelHeight() == h && c->getRequestedMSAA() == samples)
  394. {
  395. canvas = c;
  396. break;
  397. }
  398. }
  399. if (canvas == nullptr)
  400. {
  401. Canvas::Settings settings;
  402. settings.format = format;
  403. settings.width = w;
  404. settings.height = h;
  405. settings.msaa = samples;
  406. canvas = newCanvas(settings);
  407. temporaryCanvases.push_back(canvas);
  408. }
  409. return canvas;
  410. }
  411. void Graphics::intersectScissor(const Rect &rect)
  412. {
  413. Rect currect = states.back().scissorRect;
  414. if (!states.back().scissor)
  415. {
  416. currect.x = 0;
  417. currect.y = 0;
  418. currect.w = std::numeric_limits<int>::max();
  419. currect.h = std::numeric_limits<int>::max();
  420. }
  421. int x1 = std::max(currect.x, rect.x);
  422. int y1 = std::max(currect.y, rect.y);
  423. int x2 = std::min(currect.x + currect.w, rect.x + rect.w);
  424. int y2 = std::min(currect.y + currect.h, rect.y + rect.h);
  425. Rect newrect = {x1, y1, std::max(0, x2 - x1), std::max(0, y2 - y1)};
  426. setScissor(newrect);
  427. }
  428. bool Graphics::getScissor(Rect &rect) const
  429. {
  430. const DisplayState &state = states.back();
  431. rect = state.scissorRect;
  432. return state.scissor;
  433. }
  434. void Graphics::getStencilTest(CompareMode &compare, int &value)
  435. {
  436. const DisplayState &state = states.back();
  437. compare = state.stencilCompare;
  438. value = state.stencilTestValue;
  439. }
  440. Graphics::ColorMask Graphics::getColorMask() const
  441. {
  442. return states.back().colorMask;
  443. }
  444. Graphics::BlendMode Graphics::getBlendMode(BlendAlpha &alphamode) const
  445. {
  446. alphamode = states.back().blendAlphaMode;
  447. return states.back().blendMode;
  448. }
  449. void Graphics::setDefaultFilter(const Texture::Filter &f)
  450. {
  451. Texture::defaultFilter = f;
  452. states.back().defaultFilter = f;
  453. }
  454. const Texture::Filter &Graphics::getDefaultFilter() const
  455. {
  456. return Texture::defaultFilter;
  457. }
  458. void Graphics::setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness)
  459. {
  460. Texture::defaultMipmapFilter = filter;
  461. Texture::defaultMipmapSharpness = sharpness;
  462. states.back().defaultMipmapFilter = filter;
  463. states.back().defaultMipmapSharpness = sharpness;
  464. }
  465. void Graphics::getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const
  466. {
  467. *filter = Texture::defaultMipmapFilter;
  468. *sharpness = Texture::defaultMipmapSharpness;
  469. }
  470. void Graphics::setLineWidth(float width)
  471. {
  472. states.back().lineWidth = width;
  473. }
  474. void Graphics::setLineStyle(Graphics::LineStyle style)
  475. {
  476. states.back().lineStyle = style;
  477. }
  478. void Graphics::setLineJoin(Graphics::LineJoin join)
  479. {
  480. states.back().lineJoin = join;
  481. }
  482. float Graphics::getLineWidth() const
  483. {
  484. return states.back().lineWidth;
  485. }
  486. Graphics::LineStyle Graphics::getLineStyle() const
  487. {
  488. return states.back().lineStyle;
  489. }
  490. Graphics::LineJoin Graphics::getLineJoin() const
  491. {
  492. return states.back().lineJoin;
  493. }
  494. float Graphics::getPointSize() const
  495. {
  496. return states.back().pointSize;
  497. }
  498. bool Graphics::isWireframe() const
  499. {
  500. return states.back().wireframe;
  501. }
  502. void Graphics::captureScreenshot(const ScreenshotInfo &info)
  503. {
  504. pendingScreenshotCallbacks.push_back(info);
  505. }
  506. Graphics::StreamVertexData Graphics::requestStreamDraw(const StreamDrawRequest &req)
  507. {
  508. using namespace vertex;
  509. StreamBufferState &state = streamBufferState;
  510. bool shouldflush = false;
  511. bool shouldresize = false;
  512. if (req.primitiveMode != state.primitiveMode
  513. || req.formats[0] != state.formats[0] || req.formats[1] != state.formats[1]
  514. || ((req.indexMode != TriangleIndexMode::NONE) != (state.indexCount > 0))
  515. || req.texture != state.texture
  516. || req.standardShaderType != state.standardShaderType)
  517. {
  518. shouldflush = true;
  519. }
  520. int totalvertices = state.vertexCount + req.vertexCount;
  521. // We only support uint16 index buffers for now.
  522. if (totalvertices > LOVE_UINT16_MAX && req.indexMode != TriangleIndexMode::NONE)
  523. shouldflush = true;
  524. int reqIndexCount = getIndexCount(req.indexMode, req.vertexCount);
  525. size_t reqIndexSize = reqIndexCount * sizeof(uint16);
  526. size_t newdatasizes[2] = {0, 0};
  527. size_t buffersizes[3] = {0, 0, 0};
  528. for (int i = 0; i < 2; i++)
  529. {
  530. if (req.formats[i] == CommonFormat::NONE)
  531. continue;
  532. size_t stride = getFormatStride(req.formats[i]);
  533. size_t datasize = stride * totalvertices;
  534. if (state.vbMap[i].data != nullptr && datasize > state.vbMap[i].size)
  535. shouldflush = true;
  536. if (datasize > state.vb[i]->getSize())
  537. {
  538. buffersizes[i] = std::max(datasize, state.vb[i]->getSize() * 2);
  539. shouldresize = true;
  540. }
  541. newdatasizes[i] = stride * req.vertexCount;
  542. }
  543. if (req.indexMode != TriangleIndexMode::NONE)
  544. {
  545. size_t datasize = (state.indexCount + reqIndexCount) * sizeof(uint16);
  546. if (state.indexBufferMap.data != nullptr && datasize > state.indexBufferMap.size)
  547. shouldflush = true;
  548. if (datasize > state.indexBuffer->getSize())
  549. {
  550. buffersizes[2] = std::max(datasize, state.indexBuffer->getSize() * 2);
  551. shouldresize = true;
  552. }
  553. }
  554. if (shouldflush || shouldresize)
  555. {
  556. flushStreamDraws();
  557. state.primitiveMode = req.primitiveMode;
  558. state.formats[0] = req.formats[0];
  559. state.formats[1] = req.formats[1];
  560. state.texture = req.texture;
  561. state.standardShaderType = req.standardShaderType;
  562. }
  563. if (state.vertexCount == 0 && Shader::isDefaultActive())
  564. Shader::attachDefault(state.standardShaderType);
  565. if (state.vertexCount == 0 && Shader::current != nullptr && req.texture != nullptr)
  566. Shader::current->checkMainTexture(req.texture);
  567. if (shouldresize)
  568. {
  569. for (int i = 0; i < 2; i++)
  570. {
  571. if (state.vb[i]->getSize() < buffersizes[i])
  572. {
  573. delete state.vb[i];
  574. state.vb[i] = newStreamBuffer(BUFFER_VERTEX, buffersizes[i]);
  575. }
  576. }
  577. if (state.indexBuffer->getSize() < buffersizes[2])
  578. {
  579. delete state.indexBuffer;
  580. state.indexBuffer = newStreamBuffer(BUFFER_INDEX, buffersizes[2]);
  581. }
  582. }
  583. if (req.indexMode != TriangleIndexMode::NONE)
  584. {
  585. if (state.indexBufferMap.data == nullptr)
  586. state.indexBufferMap = state.indexBuffer->map(reqIndexSize);
  587. uint16 *indices = (uint16 *) state.indexBufferMap.data;
  588. fillIndices(req.indexMode, state.vertexCount, req.vertexCount, indices);
  589. state.indexBufferMap.data += reqIndexSize;
  590. }
  591. StreamVertexData d;
  592. for (int i = 0; i < 2; i++)
  593. {
  594. if (newdatasizes[i] > 0)
  595. {
  596. if (state.vbMap[i].data == nullptr)
  597. state.vbMap[i] = state.vb[i]->map(newdatasizes[i]);
  598. d.stream[i] = state.vbMap[i].data;
  599. state.vbMap[i].data += newdatasizes[i];
  600. }
  601. }
  602. state.vertexCount += req.vertexCount;
  603. state.indexCount += reqIndexCount;
  604. return d;
  605. }
  606. void Graphics::flushStreamDrawsGlobal()
  607. {
  608. Graphics *instance = getInstance<Graphics>(M_GRAPHICS);
  609. if (instance != nullptr)
  610. instance->flushStreamDraws();
  611. }
  612. /**
  613. * Drawing
  614. **/
  615. void Graphics::draw(Drawable *drawable, const Matrix4 &m)
  616. {
  617. drawable->draw(this, m);
  618. }
  619. void Graphics::draw(Texture *texture, Quad *quad, const Matrix4 &m)
  620. {
  621. texture->draw(this, quad, m);
  622. }
  623. void Graphics::drawLayer(Texture *texture, int layer, const Matrix4 &m)
  624. {
  625. texture->drawLayer(this, layer, m);
  626. }
  627. void Graphics::drawLayer(Texture *texture, int layer, Quad *quad, const Matrix4 &m)
  628. {
  629. texture->drawLayer(this, layer, quad, m);
  630. }
  631. void Graphics::drawInstanced(Mesh *mesh, const Matrix4 &m, int instancecount)
  632. {
  633. mesh->drawInstanced(this, m, instancecount);
  634. }
  635. void Graphics::print(const std::vector<Font::ColoredString> &str, const Matrix4 &m)
  636. {
  637. checkSetDefaultFont();
  638. if (states.back().font.get() != nullptr)
  639. print(str, states.back().font.get(), m);
  640. }
  641. void Graphics::print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m)
  642. {
  643. font->print(this, str, m, states.back().color);
  644. }
  645. void Graphics::printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m)
  646. {
  647. checkSetDefaultFont();
  648. if (states.back().font.get() != nullptr)
  649. printf(str, states.back().font.get(), wrap, align, m);
  650. }
  651. void Graphics::printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m)
  652. {
  653. font->printf(this, str, wrap, align, m, states.back().color);
  654. }
  655. /**
  656. * Primitives (points, shapes, lines).
  657. **/
  658. void Graphics::points(const float *coords, const Colorf *colors, size_t numpoints)
  659. {
  660. const Matrix4 &t = getTransform();
  661. bool is2D = t.isAffine2DTransform();
  662. StreamDrawRequest req;
  663. req.primitiveMode = vertex::PrimitiveMode::POINTS;
  664. req.formats[0] = vertex::getSinglePositionFormat(is2D);
  665. req.formats[1] = vertex::CommonFormat::RGBAub;
  666. req.vertexCount = (int) numpoints;
  667. StreamVertexData data = requestStreamDraw(req);
  668. if (is2D)
  669. t.transformXY((Vector2 *) data.stream[0], (const Vector2 *) coords, req.vertexCount);
  670. else
  671. t.transformXY0((Vector3 *) data.stream[0], (const Vector2 *) coords, req.vertexCount);
  672. Color *colordata = (Color *) data.stream[1];
  673. if (colors)
  674. {
  675. Colorf nc = getColor();
  676. gammaCorrectColor(nc);
  677. if (isGammaCorrect())
  678. {
  679. for (int i = 0; i < req.vertexCount; i++)
  680. {
  681. Colorf ci = colors[i];
  682. gammaCorrectColor(ci);
  683. ci *= nc;
  684. unGammaCorrectColor(ci);
  685. colordata[i] = toColor(ci);
  686. }
  687. }
  688. else
  689. {
  690. for (int i = 0; i < req.vertexCount; i++)
  691. colordata[i] = toColor(nc * colors[i]);
  692. }
  693. }
  694. else
  695. {
  696. Color c = toColor(getColor());
  697. for (int i = 0; i < req.vertexCount; i++)
  698. colordata[i] = c;
  699. }
  700. }
  701. int Graphics::calculateEllipsePoints(float rx, float ry) const
  702. {
  703. int points = (int) sqrtf(((rx + ry) / 2.0f) * 20.0f * (float) pixelScaleStack.back());
  704. return std::max(points, 8);
  705. }
  706. void Graphics::polyline(const float *coords, size_t count)
  707. {
  708. float halfwidth = getLineWidth() * 0.5f;
  709. LineJoin linejoin = getLineJoin();
  710. LineStyle linestyle = getLineStyle();
  711. float pixelsize = 1.0f / std::max((float) pixelScaleStack.back(), 0.000001f);
  712. if (linejoin == LINE_JOIN_NONE)
  713. {
  714. NoneJoinPolyline line;
  715. line.render(coords, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
  716. line.draw(this);
  717. }
  718. else if (linejoin == LINE_JOIN_BEVEL)
  719. {
  720. BevelJoinPolyline line;
  721. line.render(coords, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
  722. line.draw(this);
  723. }
  724. else if (linejoin == LINE_JOIN_MITER)
  725. {
  726. MiterJoinPolyline line;
  727. line.render(coords, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
  728. line.draw(this);
  729. }
  730. }
  731. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
  732. {
  733. float coords[] = {x,y, x,y+h, x+w,y+h, x+w,y, x,y};
  734. polygon(mode, coords, 5 * 2);
  735. }
  736. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points)
  737. {
  738. if (rx == 0 || ry == 0)
  739. {
  740. rectangle(mode, x, y, w, h);
  741. return;
  742. }
  743. // Radius values that are more than half the rectangle's size aren't handled
  744. // correctly (for now)...
  745. if (w >= 0.02f)
  746. rx = std::min(rx, w / 2.0f - 0.01f);
  747. if (h >= 0.02f)
  748. ry = std::min(ry, h / 2.0f - 0.01f);
  749. points = std::max(points / 4, 1);
  750. const float half_pi = static_cast<float>(LOVE_M_PI / 2);
  751. float angle_shift = half_pi / ((float) points + 1.0f);
  752. int num_coords = (points + 2) * 8;
  753. float *coords = getScratchBuffer<float>(num_coords + 2);
  754. float phi = .0f;
  755. for (int i = 0; i <= points + 2; ++i, phi += angle_shift)
  756. {
  757. coords[2 * i + 0] = x + rx * (1 - cosf(phi));
  758. coords[2 * i + 1] = y + ry * (1 - sinf(phi));
  759. }
  760. phi = half_pi;
  761. for (int i = points + 2; i <= 2 * (points + 2); ++i, phi += angle_shift)
  762. {
  763. coords[2 * i + 0] = x + w - rx * (1 + cosf(phi));
  764. coords[2 * i + 1] = y + ry * (1 - sinf(phi));
  765. }
  766. phi = 2 * half_pi;
  767. for (int i = 2 * (points + 2); i <= 3 * (points + 2); ++i, phi += angle_shift)
  768. {
  769. coords[2 * i + 0] = x + w - rx * (1 + cosf(phi));
  770. coords[2 * i + 1] = y + h - ry * (1 + sinf(phi));
  771. }
  772. phi = 3 * half_pi;
  773. for (int i = 3 * (points + 2); i <= 4 * (points + 2); ++i, phi += angle_shift)
  774. {
  775. coords[2 * i + 0] = x + rx * (1 - cosf(phi));
  776. coords[2 * i + 1] = y + h - ry * (1 + sinf(phi));
  777. }
  778. coords[num_coords + 0] = coords[0];
  779. coords[num_coords + 1] = coords[1];
  780. polygon(mode, coords, num_coords + 2);
  781. }
  782. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry)
  783. {
  784. rectangle(mode, x, y, w, h, rx, ry, calculateEllipsePoints(rx, ry));
  785. }
  786. void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
  787. {
  788. ellipse(mode, x, y, radius, radius, points);
  789. }
  790. void Graphics::circle(DrawMode mode, float x, float y, float radius)
  791. {
  792. ellipse(mode, x, y, radius, radius);
  793. }
  794. void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
  795. {
  796. float two_pi = (float) (LOVE_M_PI * 2);
  797. if (points <= 0) points = 1;
  798. float angle_shift = (two_pi / points);
  799. float phi = .0f;
  800. float *coords = getScratchBuffer<float>(2 * (points + 1));
  801. for (int i = 0; i < points; ++i, phi += angle_shift)
  802. {
  803. coords[2*i+0] = x + a * cosf(phi);
  804. coords[2*i+1] = y + b * sinf(phi);
  805. }
  806. coords[2*points+0] = coords[0];
  807. coords[2*points+1] = coords[1];
  808. polygon(mode, coords, (points + 1) * 2);
  809. }
  810. void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
  811. {
  812. ellipse(mode, x, y, a, b, calculateEllipsePoints(a, b));
  813. }
  814. void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
  815. {
  816. // Nothing to display with no points or equal angles. (Or is there with line mode?)
  817. if (points <= 0 || angle1 == angle2)
  818. return;
  819. // Oh, you want to draw a circle?
  820. if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
  821. {
  822. circle(drawmode, x, y, radius, points);
  823. return;
  824. }
  825. float angle_shift = (angle2 - angle1) / points;
  826. // Bail on precision issues.
  827. if (angle_shift == 0.0)
  828. return;
  829. // Prevent the connecting line from being drawn if a closed line arc has a
  830. // small angle. Avoids some visual issues when connected lines are at sharp
  831. // angles, due to the miter line join drawing code.
  832. if (drawmode == DRAW_LINE && arcmode == ARC_CLOSED && fabsf(angle1 - angle2) < LOVE_TORAD(4))
  833. arcmode = ARC_OPEN;
  834. // Quick fix for the last part of a filled open arc not being drawn (because
  835. // polygon(DRAW_FILL, ...) doesn't work without a closed loop of vertices.)
  836. if (drawmode == DRAW_FILL && arcmode == ARC_OPEN)
  837. arcmode = ARC_CLOSED;
  838. float phi = angle1;
  839. float *coords = nullptr;
  840. int num_coords = 0;
  841. const auto createPoints = [&](float *coordinates)
  842. {
  843. for (int i = 0; i <= points; ++i, phi += angle_shift)
  844. {
  845. coordinates[2 * i + 0] = x + radius * cosf(phi);
  846. coordinates[2 * i + 1] = y + radius * sinf(phi);
  847. }
  848. };
  849. if (arcmode == ARC_PIE)
  850. {
  851. num_coords = (points + 3) * 2;
  852. coords = getScratchBuffer<float>(num_coords);
  853. coords[0] = coords[num_coords - 2] = x;
  854. coords[1] = coords[num_coords - 1] = y;
  855. createPoints(coords + 2);
  856. }
  857. else if (arcmode == ARC_OPEN)
  858. {
  859. num_coords = (points + 1) * 2;
  860. coords = getScratchBuffer<float>(num_coords);
  861. createPoints(coords);
  862. }
  863. else // ARC_CLOSED
  864. {
  865. num_coords = (points + 2) * 2;
  866. coords = getScratchBuffer<float>(num_coords);
  867. createPoints(coords);
  868. // Connect the ends of the arc.
  869. coords[num_coords - 2] = coords[0];
  870. coords[num_coords - 1] = coords[1];
  871. }
  872. polygon(drawmode, coords, num_coords);
  873. }
  874. void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2)
  875. {
  876. float points = (float) calculateEllipsePoints(radius, radius);
  877. // The amount of points is based on the fraction of the circle created by the arc.
  878. float angle = fabsf(angle1 - angle2);
  879. if (angle < 2.0f * (float) LOVE_M_PI)
  880. points *= angle / (2.0f * (float) LOVE_M_PI);
  881. arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
  882. }
  883. /// @param mode the draw mode
  884. /// @param coords the coordinate array
  885. /// @param count the number of coordinates/size of the array
  886. void Graphics::polygon(DrawMode mode, const float *coords, size_t count)
  887. {
  888. // coords is an array of a closed loop of vertices, i.e.
  889. // coords[count-2] = coords[0], coords[count-1] = coords[1]
  890. if (mode == DRAW_LINE)
  891. {
  892. polyline(coords, count);
  893. }
  894. else
  895. {
  896. const Matrix4 &t = getTransform();
  897. bool is2D = t.isAffine2DTransform();
  898. StreamDrawRequest req;
  899. req.formats[0] = vertex::getSinglePositionFormat(is2D);
  900. req.formats[1] = vertex::CommonFormat::RGBAub;
  901. req.indexMode = vertex::TriangleIndexMode::FAN;
  902. req.vertexCount = (int)count/2 - 1;
  903. StreamVertexData data = requestStreamDraw(req);
  904. if (is2D)
  905. t.transformXY((Vector2 *) data.stream[0], (const Vector2 *) coords, req.vertexCount);
  906. else
  907. t.transformXY0((Vector3 *) data.stream[0], (const Vector2 *) coords, req.vertexCount);
  908. Color c = toColor(getColor());
  909. Color *colordata = (Color *) data.stream[1];
  910. for (int i = 0; i < req.vertexCount; i++)
  911. colordata[i] = c;
  912. }
  913. }
  914. Graphics::Stats Graphics::getStats() const
  915. {
  916. Stats stats;
  917. getAPIStats(stats.drawCalls, stats.shaderSwitches);
  918. if (streamBufferState.vertexCount > 0)
  919. stats.drawCalls++;
  920. stats.canvasSwitches = canvasSwitchCount;
  921. stats.canvases = Canvas::canvasCount;
  922. stats.images = Image::imageCount;
  923. stats.fonts = Font::fontCount;
  924. stats.textureMemory = Texture::totalGraphicsMemory;
  925. return stats;
  926. }
  927. void Graphics::push(StackType type)
  928. {
  929. if (stackTypeStack.size() == MAX_USER_STACK_DEPTH)
  930. throw Exception("Maximum stack depth reached (more pushes than pops?)");
  931. pushTransform();
  932. pixelScaleStack.push_back(pixelScaleStack.back());
  933. if (type == STACK_ALL)
  934. states.push_back(states.back());
  935. stackTypeStack.push_back(type);
  936. }
  937. void Graphics::pop()
  938. {
  939. if (stackTypeStack.size() < 1)
  940. throw Exception("Minimum stack depth reached (more pops than pushes?)");
  941. popTransform();
  942. pixelScaleStack.pop_back();
  943. if (stackTypeStack.back() == STACK_ALL)
  944. {
  945. DisplayState &newstate = states[states.size() - 2];
  946. restoreStateChecked(newstate);
  947. // The last two states in the stack should be equal now.
  948. states.pop_back();
  949. }
  950. stackTypeStack.pop_back();
  951. }
  952. /**
  953. * Transform and stack functions.
  954. **/
  955. const Matrix4 &Graphics::getTransform() const
  956. {
  957. return transformStack.back();
  958. }
  959. const Matrix4 &Graphics::getProjection() const
  960. {
  961. return projectionMatrix;
  962. }
  963. void Graphics::pushTransform()
  964. {
  965. transformStack.push_back(transformStack.back());
  966. }
  967. void Graphics::pushIdentityTransform()
  968. {
  969. transformStack.push_back(Matrix4());
  970. }
  971. void Graphics::popTransform()
  972. {
  973. transformStack.pop_back();
  974. }
  975. void Graphics::rotate(float r)
  976. {
  977. transformStack.back().rotate(r);
  978. }
  979. void Graphics::scale(float x, float y)
  980. {
  981. transformStack.back().scale(x, y);
  982. pixelScaleStack.back() *= (fabs(x) + fabs(y)) / 2.0;
  983. }
  984. void Graphics::translate(float x, float y)
  985. {
  986. transformStack.back().translate(x, y);
  987. }
  988. void Graphics::shear(float kx, float ky)
  989. {
  990. transformStack.back().shear(kx, ky);
  991. }
  992. void Graphics::origin()
  993. {
  994. transformStack.back().setIdentity();
  995. pixelScaleStack.back() = 1;
  996. }
  997. void Graphics::applyTransform(love::math::Transform *transform)
  998. {
  999. Matrix4 &m = transformStack.back();
  1000. m *= transform->getMatrix();
  1001. float sx, sy;
  1002. m.getApproximateScale(sx, sy);
  1003. pixelScaleStack.back() = (sx + sy) / 2.0;
  1004. }
  1005. void Graphics::replaceTransform(love::math::Transform *transform)
  1006. {
  1007. const Matrix4 &m = transform->getMatrix();
  1008. transformStack.back() = m;
  1009. float sx, sy;
  1010. m.getApproximateScale(sx, sy);
  1011. pixelScaleStack.back() = (sx + sy) / 2.0;
  1012. }
  1013. Vector2 Graphics::transformPoint(Vector2 point)
  1014. {
  1015. Vector2 p;
  1016. transformStack.back().transformXY(&p, &point, 1);
  1017. return p;
  1018. }
  1019. Vector2 Graphics::inverseTransformPoint(Vector2 point)
  1020. {
  1021. Vector2 p;
  1022. // TODO: We should probably cache the inverse transform so we don't have to
  1023. // re-calculate it every time this is called.
  1024. transformStack.back().inverse().transformXY(&p, &point, 1);
  1025. return p;
  1026. }
  1027. const Shader::ShaderSource &Graphics::getCurrentDefaultShaderCode() const
  1028. {
  1029. return defaultShaderCode[Shader::STANDARD_DEFAULT][getShaderLanguageTarget()][isGammaCorrect() ? 1 : 0];
  1030. }
  1031. /**
  1032. * Constants.
  1033. **/
  1034. bool Graphics::getConstant(const char *in, DrawMode &out)
  1035. {
  1036. return drawModes.find(in, out);
  1037. }
  1038. bool Graphics::getConstant(DrawMode in, const char *&out)
  1039. {
  1040. return drawModes.find(in, out);
  1041. }
  1042. bool Graphics::getConstant(const char *in, ArcMode &out)
  1043. {
  1044. return arcModes.find(in, out);
  1045. }
  1046. bool Graphics::getConstant(ArcMode in, const char *&out)
  1047. {
  1048. return arcModes.find(in, out);
  1049. }
  1050. bool Graphics::getConstant(const char *in, BlendMode &out)
  1051. {
  1052. return blendModes.find(in, out);
  1053. }
  1054. bool Graphics::getConstant(BlendMode in, const char *&out)
  1055. {
  1056. return blendModes.find(in, out);
  1057. }
  1058. bool Graphics::getConstant(const char *in, BlendAlpha &out)
  1059. {
  1060. return blendAlphaModes.find(in, out);
  1061. }
  1062. bool Graphics::getConstant(BlendAlpha in, const char *&out)
  1063. {
  1064. return blendAlphaModes.find(in, out);
  1065. }
  1066. bool Graphics::getConstant(const char *in, LineStyle &out)
  1067. {
  1068. return lineStyles.find(in, out);
  1069. }
  1070. bool Graphics::getConstant(LineStyle in, const char *&out)
  1071. {
  1072. return lineStyles.find(in, out);
  1073. }
  1074. bool Graphics::getConstant(const char *in, LineJoin &out)
  1075. {
  1076. return lineJoins.find(in, out);
  1077. }
  1078. bool Graphics::getConstant(LineJoin in, const char *&out)
  1079. {
  1080. return lineJoins.find(in, out);
  1081. }
  1082. bool Graphics::getConstant(const char *in, Feature &out)
  1083. {
  1084. return features.find(in, out);
  1085. }
  1086. bool Graphics::getConstant(Feature in, const char *&out)
  1087. {
  1088. return features.find(in, out);
  1089. }
  1090. bool Graphics::getConstant(const char *in, SystemLimit &out)
  1091. {
  1092. return systemLimits.find(in, out);
  1093. }
  1094. bool Graphics::getConstant(SystemLimit in, const char *&out)
  1095. {
  1096. return systemLimits.find(in, out);
  1097. }
  1098. bool Graphics::getConstant(const char *in, StackType &out)
  1099. {
  1100. return stackTypes.find(in, out);
  1101. }
  1102. bool Graphics::getConstant(StackType in, const char *&out)
  1103. {
  1104. return stackTypes.find(in, out);
  1105. }
  1106. StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM>::Entry Graphics::drawModeEntries[] =
  1107. {
  1108. { "line", DRAW_LINE },
  1109. { "fill", DRAW_FILL },
  1110. };
  1111. StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM> Graphics::drawModes(Graphics::drawModeEntries, sizeof(Graphics::drawModeEntries));
  1112. StringMap<Graphics::ArcMode, Graphics::ARC_MAX_ENUM>::Entry Graphics::arcModeEntries[] =
  1113. {
  1114. { "open", ARC_OPEN },
  1115. { "closed", ARC_CLOSED },
  1116. { "pie", ARC_PIE },
  1117. };
  1118. StringMap<Graphics::ArcMode, Graphics::ARC_MAX_ENUM> Graphics::arcModes(Graphics::arcModeEntries, sizeof(Graphics::arcModeEntries));
  1119. StringMap<Graphics::BlendMode, Graphics::BLEND_MAX_ENUM>::Entry Graphics::blendModeEntries[] =
  1120. {
  1121. { "alpha", BLEND_ALPHA },
  1122. { "add", BLEND_ADD },
  1123. { "subtract", BLEND_SUBTRACT },
  1124. { "multiply", BLEND_MULTIPLY },
  1125. { "lighten", BLEND_LIGHTEN },
  1126. { "darken", BLEND_DARKEN },
  1127. { "screen", BLEND_SCREEN },
  1128. { "replace", BLEND_REPLACE },
  1129. { "none", BLEND_NONE },
  1130. };
  1131. StringMap<Graphics::BlendMode, Graphics::BLEND_MAX_ENUM> Graphics::blendModes(Graphics::blendModeEntries, sizeof(Graphics::blendModeEntries));
  1132. StringMap<Graphics::BlendAlpha, Graphics::BLENDALPHA_MAX_ENUM>::Entry Graphics::blendAlphaEntries[] =
  1133. {
  1134. { "alphamultiply", BLENDALPHA_MULTIPLY },
  1135. { "premultiplied", BLENDALPHA_PREMULTIPLIED },
  1136. };
  1137. StringMap<Graphics::BlendAlpha, Graphics::BLENDALPHA_MAX_ENUM> Graphics::blendAlphaModes(Graphics::blendAlphaEntries, sizeof(Graphics::blendAlphaEntries));
  1138. StringMap<Graphics::LineStyle, Graphics::LINE_MAX_ENUM>::Entry Graphics::lineStyleEntries[] =
  1139. {
  1140. { "smooth", LINE_SMOOTH },
  1141. { "rough", LINE_ROUGH }
  1142. };
  1143. StringMap<Graphics::LineStyle, Graphics::LINE_MAX_ENUM> Graphics::lineStyles(Graphics::lineStyleEntries, sizeof(Graphics::lineStyleEntries));
  1144. StringMap<Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM>::Entry Graphics::lineJoinEntries[] =
  1145. {
  1146. { "none", LINE_JOIN_NONE },
  1147. { "miter", LINE_JOIN_MITER },
  1148. { "bevel", LINE_JOIN_BEVEL }
  1149. };
  1150. StringMap<Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM> Graphics::lineJoins(Graphics::lineJoinEntries, sizeof(Graphics::lineJoinEntries));
  1151. StringMap<Graphics::Feature, Graphics::FEATURE_MAX_ENUM>::Entry Graphics::featureEntries[] =
  1152. {
  1153. { "multicanvasformats", FEATURE_MULTI_CANVAS_FORMATS },
  1154. { "clampzero", FEATURE_CLAMP_ZERO },
  1155. { "lighten", FEATURE_LIGHTEN },
  1156. { "fullnpot", FEATURE_FULL_NPOT },
  1157. { "pixelshaderhighp", FEATURE_PIXEL_SHADER_HIGHP },
  1158. { "arraytexture", FEATURE_ARRAY_TEXTURE },
  1159. { "volumetexture", FEATURE_VOLUME_TEXTURE },
  1160. { "glsl3", FEATURE_GLSL3 },
  1161. { "instancing", FEATURE_INSTANCING },
  1162. };
  1163. StringMap<Graphics::Feature, Graphics::FEATURE_MAX_ENUM> Graphics::features(Graphics::featureEntries, sizeof(Graphics::featureEntries));
  1164. StringMap<Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM>::Entry Graphics::systemLimitEntries[] =
  1165. {
  1166. { "pointsize", LIMIT_POINT_SIZE },
  1167. { "texturesize", LIMIT_TEXTURE_SIZE },
  1168. { "texturelayers", LIMIT_TEXTURE_LAYERS },
  1169. { "volumetexturesize", LIMIT_VOLUME_TEXTURE_SIZE },
  1170. { "cubetexturesize", LIMIT_CUBE_TEXTURE_SIZE },
  1171. { "multicanvas", LIMIT_MULTI_CANVAS },
  1172. { "canvasmsaa", LIMIT_CANVAS_MSAA },
  1173. { "anisotropy", LIMIT_ANISOTROPY },
  1174. };
  1175. StringMap<Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM> Graphics::systemLimits(Graphics::systemLimitEntries, sizeof(Graphics::systemLimitEntries));
  1176. StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM>::Entry Graphics::stackTypeEntries[] =
  1177. {
  1178. { "all", STACK_ALL },
  1179. { "transform", STACK_TRANSFORM },
  1180. };
  1181. StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM> Graphics::stackTypes(Graphics::stackTypeEntries, sizeof(Graphics::stackTypeEntries));
  1182. } // graphics
  1183. } // love