Graphics.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. #ifndef LOVE_GRAPHICS_GRAPHICS_H
  21. #define LOVE_GRAPHICS_GRAPHICS_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "common/Module.h"
  25. #include "common/StringMap.h"
  26. #include "common/Vector.h"
  27. #include "common/Optional.h"
  28. #include "common/int.h"
  29. #include "common/Color.h"
  30. #include "StreamBuffer.h"
  31. #include "vertex.h"
  32. #include "Texture.h"
  33. #include "Canvas.h"
  34. #include "Font.h"
  35. #include "ShaderStage.h"
  36. #include "Shader.h"
  37. #include "Quad.h"
  38. #include "Mesh.h"
  39. #include "Image.h"
  40. #include "Deprecations.h"
  41. #include "renderstate.h"
  42. #include "math/Transform.h"
  43. #include "font/Rasterizer.h"
  44. #include "font/Font.h"
  45. #include "video/VideoStream.h"
  46. #include "data/HashFunction.h"
  47. // C++
  48. #include <string>
  49. #include <vector>
  50. namespace love
  51. {
  52. namespace graphics
  53. {
  54. class SpriteBatch;
  55. class ParticleSystem;
  56. class Text;
  57. class Video;
  58. class Buffer;
  59. typedef Optional<Colorf> OptionalColorf;
  60. const int MAX_COLOR_RENDER_TARGETS = 8;
  61. /**
  62. * Globally sets whether gamma correction is enabled. Ideally this should be set
  63. * prior to using any Graphics module function.
  64. **/
  65. void setGammaCorrect(bool gammacorrect);
  66. /**
  67. * Gets whether global gamma correction is enabled.
  68. **/
  69. bool isGammaCorrect();
  70. /**
  71. * Gamma-corrects a color (converts it from sRGB to linear RGB, if
  72. * gamma correction is enabled.)
  73. * The color's components are expected to be in the range of [0, 1].
  74. **/
  75. void gammaCorrectColor(Colorf &c);
  76. /**
  77. * Un-gamma-corrects a color (converts it from linear RGB to sRGB, if
  78. * gamma correction is enabled.)
  79. * The color's components are expected to be in the range of [0, 1].
  80. **/
  81. void unGammaCorrectColor(Colorf &c);
  82. Colorf gammaCorrectColor(const Colorf &c);
  83. Colorf unGammaCorrectColor(const Colorf &c);
  84. bool isDebugEnabled();
  85. class Graphics : public Module
  86. {
  87. public:
  88. static love::Type type;
  89. enum DrawMode
  90. {
  91. DRAW_LINE,
  92. DRAW_FILL,
  93. DRAW_MAX_ENUM
  94. };
  95. enum ArcMode
  96. {
  97. ARC_OPEN,
  98. ARC_CLOSED,
  99. ARC_PIE,
  100. ARC_MAX_ENUM
  101. };
  102. enum LineStyle
  103. {
  104. LINE_ROUGH,
  105. LINE_SMOOTH,
  106. LINE_MAX_ENUM
  107. };
  108. enum LineJoin
  109. {
  110. LINE_JOIN_NONE,
  111. LINE_JOIN_MITER,
  112. LINE_JOIN_BEVEL,
  113. LINE_JOIN_MAX_ENUM
  114. };
  115. enum Feature
  116. {
  117. FEATURE_MULTI_CANVAS_FORMATS,
  118. FEATURE_CLAMP_ZERO,
  119. FEATURE_BLENDMINMAX,
  120. FEATURE_LIGHTEN, // Deprecated
  121. FEATURE_FULL_NPOT,
  122. FEATURE_PIXEL_SHADER_HIGHP,
  123. FEATURE_SHADER_DERIVATIVES,
  124. FEATURE_GLSL3,
  125. FEATURE_GLSL4,
  126. FEATURE_INSTANCING,
  127. FEATURE_MAX_ENUM
  128. };
  129. enum Renderer
  130. {
  131. RENDERER_OPENGL = 0,
  132. RENDERER_OPENGLES,
  133. RENDERER_MAX_ENUM
  134. };
  135. enum SystemLimit
  136. {
  137. LIMIT_POINT_SIZE,
  138. LIMIT_TEXTURE_SIZE,
  139. LIMIT_VOLUME_TEXTURE_SIZE,
  140. LIMIT_CUBE_TEXTURE_SIZE,
  141. LIMIT_TEXTURE_LAYERS,
  142. LIMIT_MULTI_CANVAS,
  143. LIMIT_CANVAS_MSAA,
  144. LIMIT_ANISOTROPY,
  145. LIMIT_MAX_ENUM
  146. };
  147. enum StackType
  148. {
  149. STACK_ALL,
  150. STACK_TRANSFORM,
  151. STACK_MAX_ENUM
  152. };
  153. enum TemporaryRenderTargetFlags
  154. {
  155. TEMPORARY_RT_DEPTH = (1 << 0),
  156. TEMPORARY_RT_STENCIL = (1 << 1),
  157. };
  158. struct Capabilities
  159. {
  160. double limits[LIMIT_MAX_ENUM];
  161. bool features[FEATURE_MAX_ENUM];
  162. bool textureTypes[TEXTURE_MAX_ENUM];
  163. };
  164. struct RendererInfo
  165. {
  166. std::string name;
  167. std::string version;
  168. std::string vendor;
  169. std::string device;
  170. };
  171. struct Stats
  172. {
  173. int drawCalls;
  174. int drawCallsBatched;
  175. int canvasSwitches;
  176. int shaderSwitches;
  177. int canvases;
  178. int images;
  179. int fonts;
  180. int64 textureMemory;
  181. };
  182. struct DrawCommand
  183. {
  184. PrimitiveType primitiveType = PRIMITIVE_TRIANGLES;
  185. const vertex::Attributes *attributes;
  186. const vertex::BufferBindings *buffers;
  187. int vertexStart = 0;
  188. int vertexCount = 0;
  189. int instanceCount = 1;
  190. Texture *texture = nullptr;
  191. // TODO: This should be moved out to a state transition API?
  192. CullMode cullMode = CULL_NONE;
  193. DrawCommand(const vertex::Attributes *attribs, const vertex::BufferBindings *buffers)
  194. : attributes(attribs)
  195. , buffers(buffers)
  196. {}
  197. };
  198. struct DrawIndexedCommand
  199. {
  200. PrimitiveType primitiveType = PRIMITIVE_TRIANGLES;
  201. const vertex::Attributes *attributes;
  202. const vertex::BufferBindings *buffers;
  203. int indexCount = 0;
  204. int instanceCount = 1;
  205. IndexDataType indexType = INDEX_UINT16;
  206. Resource *indexBuffer;
  207. size_t indexBufferOffset = 0;
  208. Texture *texture = nullptr;
  209. // TODO: This should be moved out to a state transition API?
  210. CullMode cullMode = CULL_NONE;
  211. DrawIndexedCommand(const vertex::Attributes *attribs, const vertex::BufferBindings *buffers, Resource *indexbuffer)
  212. : attributes(attribs)
  213. , buffers(buffers)
  214. , indexBuffer(indexbuffer)
  215. {}
  216. };
  217. struct StreamDrawCommand
  218. {
  219. PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES;
  220. vertex::CommonFormat formats[2];
  221. vertex::TriangleIndexMode indexMode = vertex::TriangleIndexMode::NONE;
  222. int vertexCount = 0;
  223. Texture *texture = nullptr;
  224. Shader::StandardShader standardShaderType = Shader::STANDARD_DEFAULT;
  225. StreamDrawCommand()
  226. {
  227. // VS2013 can't initialize arrays in the above manner...
  228. formats[1] = formats[0] = vertex::CommonFormat::NONE;
  229. }
  230. };
  231. struct StreamVertexData
  232. {
  233. void *stream[2];
  234. };
  235. class TempTransform
  236. {
  237. public:
  238. TempTransform(Graphics *gfx)
  239. : gfx(gfx)
  240. {
  241. gfx->pushTransform();
  242. }
  243. TempTransform(Graphics *gfx, const Matrix4 &t)
  244. : gfx(gfx)
  245. {
  246. gfx->pushTransform();
  247. gfx->transformStack.back() *= t;
  248. }
  249. ~TempTransform()
  250. {
  251. gfx->popTransform();
  252. }
  253. private:
  254. Graphics *gfx;
  255. };
  256. struct ScreenshotInfo;
  257. typedef void (*ScreenshotCallback)(const ScreenshotInfo *info, love::image::ImageData *i, void *ud);
  258. struct ScreenshotInfo
  259. {
  260. ScreenshotCallback callback = nullptr;
  261. void *data = nullptr;
  262. };
  263. struct RenderTargetStrongRef;
  264. struct RenderTarget
  265. {
  266. Canvas *canvas;
  267. int slice;
  268. int mipmap;
  269. RenderTarget(Canvas *canvas, int slice = 0, int mipmap = 0)
  270. : canvas(canvas)
  271. , slice(slice)
  272. , mipmap(mipmap)
  273. {}
  274. RenderTarget()
  275. : canvas(nullptr)
  276. , slice(0)
  277. , mipmap(0)
  278. {}
  279. bool operator != (const RenderTarget &other) const
  280. {
  281. return canvas != other.canvas || slice != other.slice || mipmap != other.mipmap;
  282. }
  283. bool operator != (const RenderTargetStrongRef &other) const
  284. {
  285. return canvas != other.canvas.get() || slice != other.slice || mipmap != other.mipmap;
  286. }
  287. };
  288. struct RenderTargetStrongRef
  289. {
  290. StrongRef<Canvas> canvas;
  291. int slice = 0;
  292. int mipmap = 0;
  293. RenderTargetStrongRef(Canvas *canvas, int slice = 0, int mipmap = 0)
  294. : canvas(canvas)
  295. , slice(slice)
  296. , mipmap(mipmap)
  297. {}
  298. bool operator != (const RenderTargetStrongRef &other) const
  299. {
  300. return canvas.get() != other.canvas.get() || slice != other.slice || mipmap != other.mipmap;
  301. }
  302. bool operator != (const RenderTarget &other) const
  303. {
  304. return canvas.get() != other.canvas || slice != other.slice || mipmap != other.mipmap;
  305. }
  306. };
  307. struct RenderTargets
  308. {
  309. std::vector<RenderTarget> colors;
  310. RenderTarget depthStencil;
  311. uint32 temporaryRTFlags;
  312. RenderTargets()
  313. : depthStencil(nullptr)
  314. , temporaryRTFlags(0)
  315. {}
  316. const RenderTarget &getFirstTarget() const
  317. {
  318. return colors.empty() ? depthStencil : colors[0];
  319. }
  320. bool operator == (const RenderTargets &other) const
  321. {
  322. size_t ncolors = colors.size();
  323. if (ncolors != other.colors.size())
  324. return false;
  325. for (size_t i = 0; i < ncolors; i++)
  326. {
  327. if (colors[i] != other.colors[i])
  328. return false;
  329. }
  330. if (depthStencil != other.depthStencil || temporaryRTFlags != other.temporaryRTFlags)
  331. return false;
  332. return true;
  333. }
  334. };
  335. struct RenderTargetsStrongRef
  336. {
  337. std::vector<RenderTargetStrongRef> colors;
  338. RenderTargetStrongRef depthStencil;
  339. uint32 temporaryRTFlags;
  340. RenderTargetsStrongRef()
  341. : depthStencil(nullptr)
  342. , temporaryRTFlags(0)
  343. {}
  344. const RenderTargetStrongRef &getFirstTarget() const
  345. {
  346. return colors.empty() ? depthStencil : colors[0];
  347. }
  348. };
  349. struct DefaultShaderCode
  350. {
  351. std::string source[ShaderStage::STAGE_MAX_ENUM];
  352. };
  353. Graphics();
  354. virtual ~Graphics();
  355. // Implements Module.
  356. virtual ModuleType getModuleType() const { return M_GRAPHICS; }
  357. virtual Image *newImage(const Image::Slices &data, const Image::Settings &settings) = 0;
  358. virtual Image *newImage(TextureType textype, PixelFormat format, int width, int height, int slices, const Image::Settings &settings) = 0;
  359. Quad *newQuad(Quad::Viewport v, double sw, double sh);
  360. Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::defaultFilter);
  361. Font *newDefaultFont(int size, font::TrueTypeRasterizer::Hinting hinting, const Texture::Filter &filter = Texture::defaultFilter);
  362. Video *newVideo(love::video::VideoStream *stream, float dpiscale);
  363. SpriteBatch *newSpriteBatch(Texture *texture, int size, vertex::Usage usage);
  364. ParticleSystem *newParticleSystem(Texture *texture, int size);
  365. virtual Canvas *newCanvas(const Canvas::Settings &settings) = 0;
  366. ShaderStage *newShaderStage(ShaderStage::StageType stage, const std::string &source);
  367. Shader *newShader(const std::string &vertex, const std::string &pixel);
  368. virtual Buffer *newBuffer(size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags) = 0;
  369. Mesh *newMesh(const std::vector<Vertex> &vertices, PrimitiveType drawmode, vertex::Usage usage);
  370. Mesh *newMesh(int vertexcount, PrimitiveType drawmode, vertex::Usage usage);
  371. Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, int vertexcount, PrimitiveType drawmode, vertex::Usage usage);
  372. Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, const void *data, size_t datasize, PrimitiveType drawmode, vertex::Usage usage);
  373. Text *newText(Font *font, const std::vector<Font::ColoredString> &text = {});
  374. bool validateShader(bool gles, const std::string &vertex, const std::string &pixel, std::string &err);
  375. /**
  376. * Resets the current color, background color, line style, and so forth.
  377. **/
  378. void reset();
  379. virtual void clear(OptionalColorf color, OptionalInt stencil, OptionalDouble depth) = 0;
  380. virtual void clear(const std::vector<OptionalColorf> &colors, OptionalInt stencil, OptionalDouble depth) = 0;
  381. virtual void discard(const std::vector<bool> &colorbuffers, bool depthstencil) = 0;
  382. /**
  383. * Flips buffers. (Rendered geometry is presented on screen).
  384. **/
  385. virtual void present(void *screenshotCallbackData) = 0;
  386. /**
  387. * Sets the current graphics display viewport dimensions.
  388. **/
  389. virtual void setViewportSize(int width, int height, int pixelwidth, int pixelheight) = 0;
  390. /**
  391. * Sets the current graphics display viewport and initializes the renderer.
  392. * @param width The viewport width.
  393. * @param height The viewport height.
  394. **/
  395. virtual bool setMode(int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil) = 0;
  396. /**
  397. * Un-sets the current graphics display mode (uninitializing objects if
  398. * necessary.)
  399. **/
  400. virtual void unSetMode() = 0;
  401. /**
  402. * Sets whether the module is active (internal use only.)
  403. **/
  404. virtual void setActive(bool active) = 0;
  405. /**
  406. * Gets whether the module is active. Graphics module methods are only
  407. * guaranteed to work when it is active. Calling them otherwise may cause
  408. * the program to crash (or worse.)
  409. * Normally the module will always be active as long as a window exists, it
  410. * may be different on some platforms (especially mobile ones.)
  411. **/
  412. bool isActive() const;
  413. /**
  414. * True if a graphics viewport is set.
  415. **/
  416. bool isCreated() const;
  417. int getWidth() const;
  418. int getHeight() const;
  419. int getPixelWidth() const;
  420. int getPixelHeight() const;
  421. double getCurrentDPIScale() const;
  422. double getScreenDPIScale() const;
  423. /**
  424. * Sets the current constant color.
  425. **/
  426. virtual void setColor(Colorf c) = 0;
  427. /**
  428. * Gets current color.
  429. **/
  430. Colorf getColor() const;
  431. /**
  432. * Sets the background Color.
  433. **/
  434. void setBackgroundColor(Colorf c);
  435. /**
  436. * Gets the current background color.
  437. **/
  438. Colorf getBackgroundColor() const;
  439. void setFont(Font *font);
  440. Font *getFont();
  441. void setShader(Shader *shader);
  442. void setShader();
  443. Shader *getShader() const;
  444. void setCanvas(RenderTarget rt, uint32 temporaryRTFlags);
  445. void setCanvas(const RenderTargets &rts);
  446. void setCanvas(const RenderTargetsStrongRef &rts);
  447. void setCanvas();
  448. RenderTargets getCanvas() const;
  449. bool isCanvasActive() const;
  450. bool isCanvasActive(Canvas *canvas) const;
  451. bool isCanvasActive(Canvas *canvas, int slice) const;
  452. /**
  453. * Scissor defines a box such that everything outside that box is discarded
  454. * and not drawn. Scissoring is automatically enabled.
  455. * @param rect The rectangle defining the scissor area.
  456. **/
  457. virtual void setScissor(const Rect &rect) = 0;
  458. void intersectScissor(const Rect &rect);
  459. /**
  460. * Clears any scissor that has been created.
  461. **/
  462. virtual void setScissor() = 0;
  463. /**
  464. * Gets the current scissor box.
  465. * @return Whether the scissor is enabled.
  466. */
  467. bool getScissor(Rect &rect) const;
  468. /**
  469. * Enables or disables drawing to the stencil buffer. When enabled, the
  470. * color buffer is disabled.
  471. **/
  472. virtual void drawToStencilBuffer(StencilAction action, int value) = 0;
  473. virtual void stopDrawToStencilBuffer() = 0;
  474. /**
  475. * Sets whether stencil testing is enabled.
  476. **/
  477. virtual void setStencilTest(CompareMode compare, int value) = 0;
  478. void setStencilTest();
  479. void getStencilTest(CompareMode &compare, int &value) const;
  480. virtual void setDepthMode(CompareMode compare, bool write) = 0;
  481. void setDepthMode();
  482. void getDepthMode(CompareMode &compare, bool &write) const;
  483. void setMeshCullMode(CullMode cull);
  484. CullMode getMeshCullMode() const;
  485. virtual void setFrontFaceWinding(vertex::Winding winding) = 0;
  486. vertex::Winding getFrontFaceWinding() const;
  487. /**
  488. * Sets the enabled color components when rendering.
  489. **/
  490. virtual void setColorMask(ColorChannelMask mask) = 0;
  491. /**
  492. * Gets the current color mask.
  493. **/
  494. ColorChannelMask getColorMask() const;
  495. /**
  496. * High-level blend mode.
  497. **/
  498. void setBlendMode(BlendMode mode, BlendAlpha alphamode);
  499. BlendMode getBlendMode(BlendAlpha &alphamode) const;
  500. /**
  501. * Low-level blend state.
  502. **/
  503. virtual void setBlendState(const BlendState &blend) = 0;
  504. const BlendState &getBlendState() const;
  505. /**
  506. * Sets the default filter for images, canvases, and fonts.
  507. **/
  508. void setDefaultFilter(const Texture::Filter &f);
  509. /**
  510. * Gets the default filter for images, canvases, and fonts.
  511. **/
  512. const Texture::Filter &getDefaultFilter() const;
  513. /**
  514. * Default Image mipmap filter mode and sharpness values.
  515. **/
  516. void setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness);
  517. void getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const;
  518. /**
  519. * Sets the line width.
  520. * @param width The new width of the line.
  521. **/
  522. void setLineWidth(float width);
  523. float getLineWidth() const;
  524. /**
  525. * Sets the line style.
  526. * @param style LINE_ROUGH or LINE_SMOOTH.
  527. **/
  528. void setLineStyle(LineStyle style);
  529. LineStyle getLineStyle() const;
  530. /**
  531. * Sets the line join mode.
  532. **/
  533. void setLineJoin(LineJoin style);
  534. LineJoin getLineJoin() const;
  535. /**
  536. * Sets the size of points.
  537. **/
  538. virtual void setPointSize(float size) = 0;
  539. /**
  540. * Gets the point size.
  541. **/
  542. float getPointSize() const;
  543. /**
  544. * Sets whether graphics will be drawn as wireframe lines instead of filled
  545. * triangles (has no effect for drawn points.)
  546. * This should only be used as a debugging tool. The wireframe lines do not
  547. * behave the same as regular love.graphics lines.
  548. **/
  549. virtual void setWireframe(bool enable) = 0;
  550. /**
  551. * Gets whether wireframe drawing mode is enabled.
  552. **/
  553. bool isWireframe() const;
  554. void captureScreenshot(const ScreenshotInfo &info);
  555. void draw(Drawable *drawable, const Matrix4 &m);
  556. void draw(Texture *texture, Quad *quad, const Matrix4 &m);
  557. void drawLayer(Texture *texture, int layer, const Matrix4 &m);
  558. void drawLayer(Texture *texture, int layer, Quad *quad, const Matrix4 &m);
  559. void drawInstanced(Mesh *mesh, const Matrix4 &m, int instancecount);
  560. /**
  561. * Draws text at the specified coordinates
  562. **/
  563. void print(const std::vector<Font::ColoredString> &str, const Matrix4 &m);
  564. void print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m);
  565. /**
  566. * Draws formatted text on screen at the specified coordinates.
  567. **/
  568. void printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m);
  569. void printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m);
  570. /**
  571. * Draws a series of points at the specified positions.
  572. **/
  573. void points(const Vector2 *positions, const Colorf *colors, size_t numpoints);
  574. /**
  575. * Draws a series of lines connecting the given vertices.
  576. * @param coords Vertex positions (v1, ..., vn). If v1 == vn the line will be drawn closed.
  577. * @param count Number of vertices.
  578. **/
  579. void polyline(const Vector2 *vertices, size_t count);
  580. /**
  581. * Draws a rectangle.
  582. * @param x Position along x-axis for top-left corner.
  583. * @param y Position along y-axis for top-left corner.
  584. * @param w The width of the rectangle.
  585. * @param h The height of the rectangle.
  586. **/
  587. void rectangle(DrawMode mode, float x, float y, float w, float h);
  588. /**
  589. * Variant of rectangle that draws a rounded rectangle.
  590. * @param mode The mode of drawing (line/filled).
  591. * @param x X-coordinate of top-left corner
  592. * @param y Y-coordinate of top-left corner
  593. * @param w The width of the rectangle.
  594. * @param h The height of the rectangle.
  595. * @param rx The radius of the corners on the x axis
  596. * @param ry The radius of the corners on the y axis
  597. * @param points The number of points to use per corner
  598. **/
  599. void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points);
  600. void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry);
  601. /**
  602. * Draws a circle using the specified arguments.
  603. * @param mode The mode of drawing (line/filled).
  604. * @param x X-coordinate.
  605. * @param y Y-coordinate.
  606. * @param radius Radius of the circle.
  607. * @param points Number of points to use to draw the circle.
  608. **/
  609. void circle(DrawMode mode, float x, float y, float radius, int points);
  610. void circle(DrawMode mode, float x, float y, float radius);
  611. /**
  612. * Draws an ellipse using the specified arguments.
  613. * @param mode The mode of drawing (line/filled).
  614. * @param x X-coordinate of center
  615. * @param y Y-coordinate of center
  616. * @param a Radius in x-direction
  617. * @param b Radius in y-direction
  618. * @param points Number of points to use to draw the circle.
  619. **/
  620. void ellipse(DrawMode mode, float x, float y, float a, float b, int points);
  621. void ellipse(DrawMode mode, float x, float y, float a, float b);
  622. /**
  623. * Draws an arc using the specified arguments.
  624. * @param drawmode The mode of drawing (line/filled).
  625. * @param arcmode The type of arc.
  626. * @param x X-coordinate.
  627. * @param y Y-coordinate.
  628. * @param radius Radius of the arc.
  629. * @param angle1 The angle at which the arc begins.
  630. * @param angle2 The angle at which the arc terminates.
  631. * @param points Number of points to use to draw the arc.
  632. **/
  633. void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points);
  634. void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2);
  635. /**
  636. * Draws a polygon with an arbitrary number of vertices.
  637. * @param mode The type of drawing (line/filled).
  638. * @param coords Vertex positions.
  639. * @param count Vertex array size.
  640. **/
  641. void polygon(DrawMode mode, const Vector2 *vertices, size_t count, bool skipLastFilledVertex = true);
  642. /**
  643. * Gets the graphics capabilities (feature support, limit values, and
  644. * supported texture types) of this system.
  645. **/
  646. const Capabilities &getCapabilities() const;
  647. /**
  648. * Converts PIXELFORMAT_NORMAL and PIXELFORMAT_HDR into a real format.
  649. **/
  650. virtual PixelFormat getSizedFormat(PixelFormat format) const = 0;
  651. /**
  652. * Gets whether the specified pixel format is supported.
  653. **/
  654. virtual bool isPixelFormatSupported(PixelFormat format, bool rendertarget, bool readable, bool sRGB = false) = 0;
  655. /**
  656. * Gets the renderer used by love.graphics.
  657. **/
  658. virtual Renderer getRenderer() const = 0;
  659. /**
  660. * Returns system-dependent renderer information.
  661. * Returned strings can vary greatly between systems! Do not rely on it for
  662. * anything!
  663. **/
  664. virtual RendererInfo getRendererInfo() const = 0;
  665. /**
  666. * Returns performance-related statistics.
  667. **/
  668. Stats getStats() const;
  669. size_t getStackDepth() const;
  670. void push(StackType type = STACK_TRANSFORM);
  671. void pop();
  672. const Matrix4 &getTransform() const;
  673. const Matrix4 &getProjection() const;
  674. void rotate(float r);
  675. void scale(float x, float y = 1.0f);
  676. void translate(float x, float y);
  677. void shear(float kx, float ky);
  678. void origin();
  679. void applyTransform(love::math::Transform *transform);
  680. void replaceTransform(love::math::Transform *transform);
  681. Vector2 transformPoint(Vector2 point);
  682. Vector2 inverseTransformPoint(Vector2 point);
  683. virtual void draw(const DrawCommand &cmd) = 0;
  684. virtual void draw(const DrawIndexedCommand &cmd) = 0;
  685. virtual void drawQuads(int start, int count, const vertex::Attributes &attributes, const vertex::BufferBindings &buffers, Texture *texture) = 0;
  686. void flushStreamDraws();
  687. StreamVertexData requestStreamDraw(const StreamDrawCommand &command);
  688. static void flushStreamDrawsGlobal();
  689. virtual Shader::Language getShaderLanguageTarget() const = 0;
  690. const DefaultShaderCode &getCurrentDefaultShaderCode() const;
  691. void cleanupCachedShaderStage(ShaderStage::StageType type, const std::string &cachekey);
  692. template <typename T>
  693. T *getScratchBuffer(size_t count)
  694. {
  695. size_t bytes = sizeof(T) * count;
  696. if (scratchBuffer.size() < bytes)
  697. scratchBuffer.resize(bytes);
  698. return (T *) scratchBuffer.data();
  699. }
  700. static bool getConstant(const char *in, DrawMode &out);
  701. static bool getConstant(DrawMode in, const char *&out);
  702. static std::vector<std::string> getConstants(DrawMode);
  703. static bool getConstant(const char *in, ArcMode &out);
  704. static bool getConstant(ArcMode in, const char *&out);
  705. static std::vector<std::string> getConstants(ArcMode);
  706. static bool getConstant(const char *in, LineStyle &out);
  707. static bool getConstant(LineStyle in, const char *&out);
  708. static std::vector<std::string> getConstants(LineStyle);
  709. static bool getConstant(const char *in, LineJoin &out);
  710. static bool getConstant(LineJoin in, const char *&out);
  711. static std::vector<std::string> getConstants(LineJoin);
  712. static bool getConstant(const char *in, Feature &out);
  713. static bool getConstant(Feature in, const char *&out);
  714. static bool getConstant(const char *in, SystemLimit &out);
  715. static bool getConstant(SystemLimit in, const char *&out);
  716. static bool getConstant(const char *in, StackType &out);
  717. static bool getConstant(StackType in, const char *&out);
  718. static std::vector<std::string> getConstants(StackType);
  719. // Default shader code (a shader is always required internally.)
  720. static DefaultShaderCode defaultShaderCode[Shader::STANDARD_MAX_ENUM][Shader::LANGUAGE_MAX_ENUM][2];
  721. protected:
  722. struct DisplayState
  723. {
  724. Colorf color = Colorf(1.0, 1.0, 1.0, 1.0);
  725. Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 1.0);
  726. BlendState blend = computeBlendState(BLEND_ALPHA, BLENDALPHA_MULTIPLY);
  727. float lineWidth = 1.0f;
  728. LineStyle lineStyle = LINE_SMOOTH;
  729. LineJoin lineJoin = LINE_JOIN_MITER;
  730. float pointSize = 1.0f;
  731. bool scissor = false;
  732. Rect scissorRect = Rect();
  733. CompareMode stencilCompare = COMPARE_ALWAYS;
  734. int stencilTestValue = 0;
  735. CompareMode depthTest = COMPARE_ALWAYS;
  736. bool depthWrite = false;
  737. CullMode meshCullMode = CULL_NONE;
  738. vertex::Winding winding = vertex::WINDING_CCW;
  739. StrongRef<Font> font;
  740. StrongRef<Shader> shader;
  741. RenderTargetsStrongRef renderTargets;
  742. ColorChannelMask colorMask;
  743. bool wireframe = false;
  744. Texture::Filter defaultFilter = Texture::Filter();
  745. Texture::FilterMode defaultMipmapFilter = Texture::FILTER_LINEAR;
  746. float defaultMipmapSharpness = 0.0f;
  747. };
  748. struct StreamBufferState
  749. {
  750. StreamBuffer *vb[2];
  751. StreamBuffer *indexBuffer = nullptr;
  752. PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES;
  753. vertex::CommonFormat formats[2];
  754. StrongRef<Texture> texture;
  755. Shader::StandardShader standardShaderType = Shader::STANDARD_DEFAULT;
  756. int vertexCount = 0;
  757. int indexCount = 0;
  758. StreamBuffer::MapInfo vbMap[2];
  759. StreamBuffer::MapInfo indexBufferMap = StreamBuffer::MapInfo();
  760. StreamBufferState()
  761. {
  762. vb[0] = vb[1] = nullptr;
  763. formats[0] = formats[1] = vertex::CommonFormat::NONE;
  764. vbMap[0] = vbMap[1] = StreamBuffer::MapInfo();
  765. }
  766. };
  767. struct TemporaryCanvas
  768. {
  769. Canvas *canvas;
  770. int framesSinceUse;
  771. TemporaryCanvas(Canvas *c)
  772. : canvas(c)
  773. , framesSinceUse(0)
  774. {}
  775. };
  776. virtual ShaderStage *newShaderStageInternal(ShaderStage::StageType stage, const std::string &cachekey, const std::string &source, bool gles) = 0;
  777. virtual Shader *newShaderInternal(ShaderStage *vertex, ShaderStage *pixel) = 0;
  778. virtual StreamBuffer *newStreamBuffer(BufferType type, size_t size) = 0;
  779. virtual void setCanvasInternal(const RenderTargets &rts, int w, int h, int pixelw, int pixelh, bool hasSRGBcanvas) = 0;
  780. virtual void initCapabilities() = 0;
  781. virtual void getAPIStats(int &shaderswitches) const = 0;
  782. void createQuadIndexBuffer();
  783. Canvas *getTemporaryCanvas(PixelFormat format, int w, int h, int samples);
  784. void restoreState(const DisplayState &s);
  785. void restoreStateChecked(const DisplayState &s);
  786. void pushTransform();
  787. void pushIdentityTransform();
  788. void popTransform();
  789. int width;
  790. int height;
  791. int pixelWidth;
  792. int pixelHeight;
  793. bool created;
  794. bool active;
  795. bool writingToStencil;
  796. StrongRef<love::graphics::Font> defaultFont;
  797. std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
  798. StreamBufferState streamBufferState;
  799. std::vector<Matrix4> transformStack;
  800. Matrix4 projectionMatrix;
  801. std::vector<double> pixelScaleStack;
  802. std::vector<DisplayState> states;
  803. std::vector<StackType> stackTypeStack;
  804. std::vector<TemporaryCanvas> temporaryCanvases;
  805. int canvasSwitchCount;
  806. int drawCalls;
  807. int drawCallsBatched;
  808. Buffer *quadIndexBuffer;
  809. Capabilities capabilities;
  810. Deprecations deprecations;
  811. static const size_t MAX_USER_STACK_DEPTH = 128;
  812. static const int MAX_TEMPORARY_CANVAS_UNUSED_FRAMES = 16;
  813. private:
  814. void checkSetDefaultFont();
  815. int calculateEllipsePoints(float rx, float ry) const;
  816. std::vector<uint8> scratchBuffer;
  817. std::unordered_map<std::string, ShaderStage *> cachedShaderStages[ShaderStage::STAGE_MAX_ENUM];
  818. static StringMap<DrawMode, DRAW_MAX_ENUM>::Entry drawModeEntries[];
  819. static StringMap<DrawMode, DRAW_MAX_ENUM> drawModes;
  820. static StringMap<ArcMode, ARC_MAX_ENUM>::Entry arcModeEntries[];
  821. static StringMap<ArcMode, ARC_MAX_ENUM> arcModes;
  822. static StringMap<LineStyle, LINE_MAX_ENUM>::Entry lineStyleEntries[];
  823. static StringMap<LineStyle, LINE_MAX_ENUM> lineStyles;
  824. static StringMap<LineJoin, LINE_JOIN_MAX_ENUM>::Entry lineJoinEntries[];
  825. static StringMap<LineJoin, LINE_JOIN_MAX_ENUM> lineJoins;
  826. static StringMap<Feature, FEATURE_MAX_ENUM>::Entry featureEntries[];
  827. static StringMap<Feature, FEATURE_MAX_ENUM> features;
  828. static StringMap<SystemLimit, LIMIT_MAX_ENUM>::Entry systemLimitEntries[];
  829. static StringMap<SystemLimit, LIMIT_MAX_ENUM> systemLimits;
  830. static StringMap<StackType, STACK_MAX_ENUM>::Entry stackTypeEntries[];
  831. static StringMap<StackType, STACK_MAX_ENUM> stackTypes;
  832. }; // Graphics
  833. } // graphics
  834. } // love
  835. #endif // LOVE_GRAPHICS_GRAPHICS_H