Graphics.h 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /**
  2. * Copyright (c) 2006-2021 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 "Font.h"
  34. #include "ShaderStage.h"
  35. #include "Shader.h"
  36. #include "Quad.h"
  37. #include "Mesh.h"
  38. #include "Deprecations.h"
  39. #include "renderstate.h"
  40. #include "math/Transform.h"
  41. #include "font/Rasterizer.h"
  42. #include "font/Font.h"
  43. #include "video/VideoStream.h"
  44. #include "data/HashFunction.h"
  45. // C++
  46. #include <string>
  47. #include <vector>
  48. namespace love
  49. {
  50. namespace graphics
  51. {
  52. class SpriteBatch;
  53. class ParticleSystem;
  54. class Text;
  55. class Video;
  56. class Buffer;
  57. typedef Optional<Colorf> OptionalColorf;
  58. const int MAX_COLOR_RENDER_TARGETS = 8;
  59. /**
  60. * Globally sets whether gamma correction is enabled. Ideally this should be set
  61. * prior to using any Graphics module function.
  62. **/
  63. void setGammaCorrect(bool gammacorrect);
  64. /**
  65. * Gets whether global gamma correction is enabled.
  66. **/
  67. bool isGammaCorrect();
  68. /**
  69. * Gamma-corrects a color (converts it from sRGB to linear RGB, if
  70. * gamma correction is enabled.)
  71. * The color's components are expected to be in the range of [0, 1].
  72. **/
  73. void gammaCorrectColor(Colorf &c);
  74. /**
  75. * Un-gamma-corrects a color (converts it from linear RGB to sRGB, if
  76. * gamma correction is enabled.)
  77. * The color's components are expected to be in the range of [0, 1].
  78. **/
  79. void unGammaCorrectColor(Colorf &c);
  80. Colorf gammaCorrectColor(const Colorf &c);
  81. Colorf unGammaCorrectColor(const Colorf &c);
  82. bool isDebugEnabled();
  83. class Graphics : public Module
  84. {
  85. public:
  86. static love::Type type;
  87. enum DrawMode
  88. {
  89. DRAW_LINE,
  90. DRAW_FILL,
  91. DRAW_MAX_ENUM
  92. };
  93. enum ArcMode
  94. {
  95. ARC_OPEN,
  96. ARC_CLOSED,
  97. ARC_PIE,
  98. ARC_MAX_ENUM
  99. };
  100. enum LineStyle
  101. {
  102. LINE_ROUGH,
  103. LINE_SMOOTH,
  104. LINE_MAX_ENUM
  105. };
  106. enum LineJoin
  107. {
  108. LINE_JOIN_NONE,
  109. LINE_JOIN_MITER,
  110. LINE_JOIN_BEVEL,
  111. LINE_JOIN_MAX_ENUM
  112. };
  113. enum Feature
  114. {
  115. FEATURE_MULTI_RENDER_TARGET_FORMATS,
  116. FEATURE_CLAMP_ZERO,
  117. FEATURE_BLEND_MINMAX,
  118. FEATURE_LIGHTEN, // Deprecated
  119. FEATURE_FULL_NPOT,
  120. FEATURE_PIXEL_SHADER_HIGHP,
  121. FEATURE_SHADER_DERIVATIVES,
  122. FEATURE_GLSL3,
  123. FEATURE_GLSL4,
  124. FEATURE_INSTANCING,
  125. FEATURE_TEXEL_BUFFER,
  126. FEATURE_COPY_BUFFER,
  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_TEXEL_BUFFER_SIZE,
  143. LIMIT_SHADER_STORAGE_BUFFER_SIZE,
  144. LIMIT_THREADGROUPS_X,
  145. LIMIT_THREADGROUPS_Y,
  146. LIMIT_THREADGROUPS_Z,
  147. LIMIT_RENDER_TARGETS,
  148. LIMIT_TEXTURE_MSAA,
  149. LIMIT_ANISOTROPY,
  150. LIMIT_MAX_ENUM
  151. };
  152. enum StackType
  153. {
  154. STACK_ALL,
  155. STACK_TRANSFORM,
  156. STACK_MAX_ENUM
  157. };
  158. enum TemporaryRenderTargetFlags
  159. {
  160. TEMPORARY_RT_DEPTH = (1 << 0),
  161. TEMPORARY_RT_STENCIL = (1 << 1),
  162. };
  163. struct Capabilities
  164. {
  165. double limits[LIMIT_MAX_ENUM];
  166. bool features[FEATURE_MAX_ENUM];
  167. bool textureTypes[TEXTURE_MAX_ENUM];
  168. };
  169. struct RendererInfo
  170. {
  171. std::string name;
  172. std::string version;
  173. std::string vendor;
  174. std::string device;
  175. };
  176. struct Stats
  177. {
  178. int drawCalls;
  179. int drawCallsBatched;
  180. int renderTargetSwitches;
  181. int shaderSwitches;
  182. int textures;
  183. int fonts;
  184. int64 textureMemory;
  185. };
  186. struct DrawCommand
  187. {
  188. PrimitiveType primitiveType = PRIMITIVE_TRIANGLES;
  189. const VertexAttributes *attributes;
  190. const BufferBindings *buffers;
  191. int vertexStart = 0;
  192. int vertexCount = 0;
  193. int instanceCount = 1;
  194. Texture *texture = nullptr;
  195. // TODO: This should be moved out to a state transition API?
  196. CullMode cullMode = CULL_NONE;
  197. DrawCommand(const VertexAttributes *attribs, const BufferBindings *buffers)
  198. : attributes(attribs)
  199. , buffers(buffers)
  200. {}
  201. };
  202. struct DrawIndexedCommand
  203. {
  204. PrimitiveType primitiveType = PRIMITIVE_TRIANGLES;
  205. const VertexAttributes *attributes;
  206. const BufferBindings *buffers;
  207. int indexCount = 0;
  208. int instanceCount = 1;
  209. IndexDataType indexType = INDEX_UINT16;
  210. Resource *indexBuffer;
  211. size_t indexBufferOffset = 0;
  212. Texture *texture = nullptr;
  213. // TODO: This should be moved out to a state transition API?
  214. CullMode cullMode = CULL_NONE;
  215. DrawIndexedCommand(const VertexAttributes *attribs, const BufferBindings *buffers, Resource *indexbuffer)
  216. : attributes(attribs)
  217. , buffers(buffers)
  218. , indexBuffer(indexbuffer)
  219. {}
  220. };
  221. struct BatchedDrawCommand
  222. {
  223. PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES;
  224. CommonFormat formats[2];
  225. TriangleIndexMode indexMode = TRIANGLEINDEX_NONE;
  226. int vertexCount = 0;
  227. Texture *texture = nullptr;
  228. Shader::StandardShader standardShaderType = Shader::STANDARD_DEFAULT;
  229. BatchedDrawCommand()
  230. {
  231. // VS2013 can't initialize arrays in the above manner...
  232. formats[1] = formats[0] = CommonFormat::NONE;
  233. }
  234. };
  235. struct BatchedVertexData
  236. {
  237. void *stream[2];
  238. };
  239. class TempTransform
  240. {
  241. public:
  242. TempTransform(Graphics *gfx)
  243. : gfx(gfx)
  244. {
  245. gfx->pushTransform();
  246. }
  247. TempTransform(Graphics *gfx, const Matrix4 &t)
  248. : gfx(gfx)
  249. {
  250. gfx->pushTransform();
  251. gfx->transformStack.back() *= t;
  252. }
  253. ~TempTransform()
  254. {
  255. gfx->popTransform();
  256. }
  257. private:
  258. Graphics *gfx;
  259. };
  260. struct ScreenshotInfo;
  261. typedef void (*ScreenshotCallback)(const ScreenshotInfo *info, love::image::ImageData *i, void *ud);
  262. struct ScreenshotInfo
  263. {
  264. ScreenshotCallback callback = nullptr;
  265. void *data = nullptr;
  266. };
  267. struct RenderTargetStrongRef;
  268. struct RenderTarget
  269. {
  270. Texture *texture;
  271. int slice;
  272. int mipmap;
  273. RenderTarget(Texture *texture, int slice = 0, int mipmap = 0)
  274. : texture(texture)
  275. , slice(slice)
  276. , mipmap(mipmap)
  277. {}
  278. RenderTarget()
  279. : texture(nullptr)
  280. , slice(0)
  281. , mipmap(0)
  282. {}
  283. bool operator != (const RenderTarget &other) const
  284. {
  285. return texture != other.texture || slice != other.slice || mipmap != other.mipmap;
  286. }
  287. bool operator != (const RenderTargetStrongRef &other) const
  288. {
  289. return texture != other.texture.get() || slice != other.slice || mipmap != other.mipmap;
  290. }
  291. };
  292. struct RenderTargetStrongRef
  293. {
  294. StrongRef<Texture> texture;
  295. int slice = 0;
  296. int mipmap = 0;
  297. RenderTargetStrongRef(Texture *texture, int slice = 0, int mipmap = 0)
  298. : texture(texture)
  299. , slice(slice)
  300. , mipmap(mipmap)
  301. {}
  302. bool operator != (const RenderTargetStrongRef &other) const
  303. {
  304. return texture.get() != other.texture.get() || slice != other.slice || mipmap != other.mipmap;
  305. }
  306. bool operator != (const RenderTarget &other) const
  307. {
  308. return texture.get() != other.texture || slice != other.slice || mipmap != other.mipmap;
  309. }
  310. };
  311. struct RenderTargets
  312. {
  313. std::vector<RenderTarget> colors;
  314. RenderTarget depthStencil;
  315. uint32 temporaryRTFlags;
  316. RenderTargets()
  317. : depthStencil(nullptr)
  318. , temporaryRTFlags(0)
  319. {}
  320. const RenderTarget &getFirstTarget() const
  321. {
  322. return colors.empty() ? depthStencil : colors[0];
  323. }
  324. bool operator == (const RenderTargets &other) const
  325. {
  326. size_t ncolors = colors.size();
  327. if (ncolors != other.colors.size())
  328. return false;
  329. for (size_t i = 0; i < ncolors; i++)
  330. {
  331. if (colors[i] != other.colors[i])
  332. return false;
  333. }
  334. if (depthStencil != other.depthStencil || temporaryRTFlags != other.temporaryRTFlags)
  335. return false;
  336. return true;
  337. }
  338. };
  339. struct RenderTargetsStrongRef
  340. {
  341. std::vector<RenderTargetStrongRef> colors;
  342. RenderTargetStrongRef depthStencil;
  343. uint32 temporaryRTFlags;
  344. RenderTargetsStrongRef()
  345. : depthStencil(nullptr)
  346. , temporaryRTFlags(0)
  347. {}
  348. const RenderTargetStrongRef &getFirstTarget() const
  349. {
  350. return colors.empty() ? depthStencil : colors[0];
  351. }
  352. };
  353. Graphics();
  354. virtual ~Graphics();
  355. // Implements Module.
  356. virtual ModuleType getModuleType() const { return M_GRAPHICS; }
  357. virtual Texture *newTexture(const Texture::Settings &settings, const Texture::Slices *data = nullptr) = 0;
  358. Quad *newQuad(Quad::Viewport v, double sw, double sh);
  359. Font *newFont(love::font::Rasterizer *data);
  360. Font *newDefaultFont(int size, font::TrueTypeRasterizer::Hinting hinting);
  361. Video *newVideo(love::video::VideoStream *stream, float dpiscale);
  362. SpriteBatch *newSpriteBatch(Texture *texture, int size, BufferDataUsage usage);
  363. ParticleSystem *newParticleSystem(Texture *texture, int size);
  364. Shader *newShader(const std::vector<std::string> &stagessource);
  365. Shader *newComputeShader(const std::string &source);
  366. virtual Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) = 0;
  367. virtual Buffer *newBuffer(const Buffer::Settings &settings, DataFormat format, const void *data, size_t size, size_t arraylength);
  368. Mesh *newMesh(const std::vector<Buffer::DataDeclaration> &vertexformat, int vertexcount, PrimitiveType drawmode, BufferDataUsage usage);
  369. Mesh *newMesh(const std::vector<Buffer::DataDeclaration> &vertexformat, const void *data, size_t datasize, PrimitiveType drawmode, BufferDataUsage usage);
  370. Mesh *newMesh(const std::vector<Mesh::BufferAttribute> &attributes, PrimitiveType drawmode);
  371. Text *newText(Font *font, const std::vector<Font::ColoredString> &text = {});
  372. bool validateShader(bool gles, const std::vector<std::string> &stages, std::string &err);
  373. /**
  374. * Resets the current color, background color, line style, and so forth.
  375. **/
  376. void reset();
  377. virtual void clear(OptionalColorf color, OptionalInt stencil, OptionalDouble depth) = 0;
  378. virtual void clear(const std::vector<OptionalColorf> &colors, OptionalInt stencil, OptionalDouble depth) = 0;
  379. virtual void discard(const std::vector<bool> &colorbuffers, bool depthstencil) = 0;
  380. /**
  381. * Flips buffers. (Rendered geometry is presented on screen).
  382. **/
  383. virtual void present(void *screenshotCallbackData) = 0;
  384. /**
  385. * Sets the current graphics display viewport dimensions.
  386. **/
  387. virtual void setViewportSize(int width, int height, int pixelwidth, int pixelheight) = 0;
  388. /**
  389. * Sets the current graphics display viewport and initializes the renderer.
  390. * @param width The viewport width.
  391. * @param height The viewport height.
  392. **/
  393. virtual bool setMode(int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil, int msaa) = 0;
  394. /**
  395. * Un-sets the current graphics display mode (uninitializing objects if
  396. * necessary.)
  397. **/
  398. virtual void unSetMode() = 0;
  399. /**
  400. * Sets whether the module is active (internal use only.)
  401. **/
  402. virtual void setActive(bool active) = 0;
  403. /**
  404. * Gets whether the module is active. Graphics module methods are only
  405. * guaranteed to work when it is active. Calling them otherwise may cause
  406. * the program to crash (or worse.)
  407. * Normally the module will always be active as long as a window exists, it
  408. * may be different on some platforms (especially mobile ones.)
  409. **/
  410. bool isActive() const;
  411. /**
  412. * True if a graphics viewport is set.
  413. **/
  414. bool isCreated() const;
  415. int getWidth() const;
  416. int getHeight() const;
  417. int getPixelWidth() const;
  418. int getPixelHeight() const;
  419. double getCurrentDPIScale() const;
  420. double getScreenDPIScale() const;
  421. virtual int getRequestedBackbufferMSAA() const = 0;
  422. virtual int getBackbufferMSAA() const = 0;
  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 setRenderTarget(RenderTarget rt, uint32 temporaryRTFlags);
  445. void setRenderTargets(const RenderTargets &rts);
  446. void setRenderTargets(const RenderTargetsStrongRef &rts);
  447. void setRenderTarget();
  448. RenderTargets getRenderTargets() const;
  449. bool isRenderTargetActive() const;
  450. bool isRenderTargetActive(Texture *texture) const;
  451. bool isRenderTargetActive(Texture *texture, 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(Winding winding) = 0;
  486. 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 sampler state for textures, videos, and fonts.
  507. **/
  508. void setDefaultSamplerState(const SamplerState &s);
  509. /**
  510. * Gets the default sampler state for textures, videos, and fonts.
  511. **/
  512. const SamplerState &getDefaultSamplerState() const;
  513. /**
  514. * Sets the line width.
  515. * @param width The new width of the line.
  516. **/
  517. void setLineWidth(float width);
  518. float getLineWidth() const;
  519. /**
  520. * Sets the line style.
  521. * @param style LINE_ROUGH or LINE_SMOOTH.
  522. **/
  523. void setLineStyle(LineStyle style);
  524. LineStyle getLineStyle() const;
  525. /**
  526. * Sets the line join mode.
  527. **/
  528. void setLineJoin(LineJoin style);
  529. LineJoin getLineJoin() const;
  530. /**
  531. * Sets the size of points.
  532. **/
  533. virtual void setPointSize(float size) = 0;
  534. /**
  535. * Gets the point size.
  536. **/
  537. float getPointSize() const;
  538. /**
  539. * Sets whether graphics will be drawn as wireframe lines instead of filled
  540. * triangles (has no effect for drawn points.)
  541. * This should only be used as a debugging tool. The wireframe lines do not
  542. * behave the same as regular love.graphics lines.
  543. **/
  544. virtual void setWireframe(bool enable) = 0;
  545. /**
  546. * Gets whether wireframe drawing mode is enabled.
  547. **/
  548. bool isWireframe() const;
  549. void captureScreenshot(const ScreenshotInfo &info);
  550. void copyBuffer(Buffer *source, Buffer *dest, size_t sourceoffset, size_t destoffset, size_t size);
  551. void dispatchThreadgroups(Shader* shader, int x, int y, int z);
  552. void draw(Drawable *drawable, const Matrix4 &m);
  553. void draw(Texture *texture, Quad *quad, const Matrix4 &m);
  554. void drawLayer(Texture *texture, int layer, const Matrix4 &m);
  555. void drawLayer(Texture *texture, int layer, Quad *quad, const Matrix4 &m);
  556. void drawInstanced(Mesh *mesh, const Matrix4 &m, int instancecount);
  557. /**
  558. * Draws text at the specified coordinates
  559. **/
  560. void print(const std::vector<Font::ColoredString> &str, const Matrix4 &m);
  561. void print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m);
  562. /**
  563. * Draws formatted text on screen at the specified coordinates.
  564. **/
  565. void printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m);
  566. void printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m);
  567. /**
  568. * Draws a series of points at the specified positions.
  569. **/
  570. void points(const Vector2 *positions, const Colorf *colors, size_t numpoints);
  571. /**
  572. * Draws a series of lines connecting the given vertices.
  573. * @param coords Vertex positions (v1, ..., vn). If v1 == vn the line will be drawn closed.
  574. * @param count Number of vertices.
  575. **/
  576. void polyline(const Vector2 *vertices, size_t count);
  577. /**
  578. * Draws a rectangle.
  579. * @param x Position along x-axis for top-left corner.
  580. * @param y Position along y-axis for top-left corner.
  581. * @param w The width of the rectangle.
  582. * @param h The height of the rectangle.
  583. **/
  584. void rectangle(DrawMode mode, float x, float y, float w, float h);
  585. /**
  586. * Variant of rectangle that draws a rounded rectangle.
  587. * @param mode The mode of drawing (line/filled).
  588. * @param x X-coordinate of top-left corner
  589. * @param y Y-coordinate of top-left corner
  590. * @param w The width of the rectangle.
  591. * @param h The height of the rectangle.
  592. * @param rx The radius of the corners on the x axis
  593. * @param ry The radius of the corners on the y axis
  594. * @param points The number of points to use per corner
  595. **/
  596. void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points);
  597. void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry);
  598. /**
  599. * Draws a circle using the specified arguments.
  600. * @param mode The mode of drawing (line/filled).
  601. * @param x X-coordinate.
  602. * @param y Y-coordinate.
  603. * @param radius Radius of the circle.
  604. * @param points Number of points to use to draw the circle.
  605. **/
  606. void circle(DrawMode mode, float x, float y, float radius, int points);
  607. void circle(DrawMode mode, float x, float y, float radius);
  608. /**
  609. * Draws an ellipse using the specified arguments.
  610. * @param mode The mode of drawing (line/filled).
  611. * @param x X-coordinate of center
  612. * @param y Y-coordinate of center
  613. * @param a Radius in x-direction
  614. * @param b Radius in y-direction
  615. * @param points Number of points to use to draw the circle.
  616. **/
  617. void ellipse(DrawMode mode, float x, float y, float a, float b, int points);
  618. void ellipse(DrawMode mode, float x, float y, float a, float b);
  619. /**
  620. * Draws an arc using the specified arguments.
  621. * @param drawmode The mode of drawing (line/filled).
  622. * @param arcmode The type of arc.
  623. * @param x X-coordinate.
  624. * @param y Y-coordinate.
  625. * @param radius Radius of the arc.
  626. * @param angle1 The angle at which the arc begins.
  627. * @param angle2 The angle at which the arc terminates.
  628. * @param points Number of points to use to draw the arc.
  629. **/
  630. void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points);
  631. void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2);
  632. /**
  633. * Draws a polygon with an arbitrary number of vertices.
  634. * @param mode The type of drawing (line/filled).
  635. * @param coords Vertex positions.
  636. * @param count Vertex array size.
  637. **/
  638. void polygon(DrawMode mode, const Vector2 *vertices, size_t count, bool skipLastFilledVertex = true);
  639. /**
  640. * Gets the graphics capabilities (feature support, limit values, and
  641. * supported texture types) of this system.
  642. **/
  643. const Capabilities &getCapabilities() const;
  644. /**
  645. * Converts PIXELFORMAT_NORMAL and PIXELFORMAT_HDR into a real format.
  646. **/
  647. virtual PixelFormat getSizedFormat(PixelFormat format, bool rendertarget, bool readable) const = 0;
  648. /**
  649. * Gets whether the specified pixel format is supported.
  650. **/
  651. virtual bool isPixelFormatSupported(PixelFormat format, bool rendertarget, bool readable, bool sRGB = false) = 0;
  652. /**
  653. * Gets the renderer used by love.graphics.
  654. **/
  655. virtual Renderer getRenderer() const = 0;
  656. /**
  657. * Returns system-dependent renderer information.
  658. * Returned strings can vary greatly between systems! Do not rely on it for
  659. * anything!
  660. **/
  661. virtual RendererInfo getRendererInfo() const = 0;
  662. /**
  663. * Returns performance-related statistics.
  664. **/
  665. Stats getStats() const;
  666. size_t getStackDepth() const;
  667. void push(StackType type = STACK_TRANSFORM);
  668. void pop();
  669. const Matrix4 &getTransform() const;
  670. const Matrix4 &getProjection() const;
  671. void rotate(float r);
  672. void scale(float x, float y = 1.0f);
  673. void translate(float x, float y);
  674. void shear(float kx, float ky);
  675. void origin();
  676. void applyTransform(love::math::Transform *transform);
  677. void replaceTransform(love::math::Transform *transform);
  678. Vector2 transformPoint(Vector2 point);
  679. Vector2 inverseTransformPoint(Vector2 point);
  680. virtual void draw(const DrawCommand &cmd) = 0;
  681. virtual void draw(const DrawIndexedCommand &cmd) = 0;
  682. virtual void drawQuads(int start, int count, const VertexAttributes &attributes, const BufferBindings &buffers, Texture *texture) = 0;
  683. void flushBatchedDraws();
  684. BatchedVertexData requestBatchedDraw(const BatchedDrawCommand &command);
  685. static void flushBatchedDrawsGlobal();
  686. void cleanupCachedShaderStage(ShaderStageType type, const std::string &cachekey);
  687. template <typename T>
  688. T *getScratchBuffer(size_t count)
  689. {
  690. size_t bytes = sizeof(T) * count;
  691. if (scratchBuffer.size() < bytes)
  692. scratchBuffer.resize(bytes);
  693. return (T *) scratchBuffer.data();
  694. }
  695. STRINGMAP_CLASS_DECLARE(DrawMode);
  696. STRINGMAP_CLASS_DECLARE(ArcMode);
  697. STRINGMAP_CLASS_DECLARE(LineStyle);
  698. STRINGMAP_CLASS_DECLARE(LineJoin);
  699. STRINGMAP_CLASS_DECLARE(Feature);
  700. STRINGMAP_CLASS_DECLARE(SystemLimit);
  701. STRINGMAP_CLASS_DECLARE(StackType);
  702. protected:
  703. struct DisplayState
  704. {
  705. DisplayState();
  706. Colorf color = Colorf(1.0, 1.0, 1.0, 1.0);
  707. Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 1.0);
  708. BlendState blend = computeBlendState(BLEND_ALPHA, BLENDALPHA_MULTIPLY);
  709. float lineWidth = 1.0f;
  710. LineStyle lineStyle = LINE_SMOOTH;
  711. LineJoin lineJoin = LINE_JOIN_MITER;
  712. float pointSize = 1.0f;
  713. bool scissor = false;
  714. Rect scissorRect = Rect();
  715. CompareMode stencilCompare = COMPARE_ALWAYS;
  716. int stencilTestValue = 0;
  717. CompareMode depthTest = COMPARE_ALWAYS;
  718. bool depthWrite = false;
  719. CullMode meshCullMode = CULL_NONE;
  720. Winding winding = WINDING_CCW;
  721. StrongRef<Font> font;
  722. StrongRef<Shader> shader;
  723. RenderTargetsStrongRef renderTargets;
  724. ColorChannelMask colorMask;
  725. bool wireframe = false;
  726. // Default mipmap filter is set in the DisplayState constructor.
  727. SamplerState defaultSamplerState = SamplerState();
  728. };
  729. struct BatchedDrawState
  730. {
  731. StreamBuffer *vb[2];
  732. StreamBuffer *indexBuffer = nullptr;
  733. PrimitiveType primitiveMode = PRIMITIVE_TRIANGLES;
  734. CommonFormat formats[2];
  735. StrongRef<Texture> texture;
  736. Shader::StandardShader standardShaderType = Shader::STANDARD_DEFAULT;
  737. int vertexCount = 0;
  738. int indexCount = 0;
  739. StreamBuffer::MapInfo vbMap[2];
  740. StreamBuffer::MapInfo indexBufferMap = StreamBuffer::MapInfo();
  741. BatchedDrawState()
  742. {
  743. vb[0] = vb[1] = nullptr;
  744. formats[0] = formats[1] = CommonFormat::NONE;
  745. vbMap[0] = vbMap[1] = StreamBuffer::MapInfo();
  746. }
  747. };
  748. struct TemporaryTexture
  749. {
  750. Texture *texture;
  751. int framesSinceUse;
  752. TemporaryTexture(Texture *tex)
  753. : texture(tex)
  754. , framesSinceUse(0)
  755. {}
  756. };
  757. ShaderStage *newShaderStage(ShaderStageType stage, const std::string &source, const Shader::SourceInfo &info);
  758. virtual ShaderStage *newShaderStageInternal(ShaderStageType stage, const std::string &cachekey, const std::string &source, bool gles) = 0;
  759. virtual Shader *newShaderInternal(StrongRef<ShaderStage> stages[SHADERSTAGE_MAX_ENUM]) = 0;
  760. virtual StreamBuffer *newStreamBuffer(BufferUsage type, size_t size) = 0;
  761. virtual bool dispatch(int x, int y, int z) = 0;
  762. virtual void setRenderTargetsInternal(const RenderTargets &rts, int w, int h, int pixelw, int pixelh, bool hasSRGBtexture) = 0;
  763. virtual void initCapabilities() = 0;
  764. virtual void getAPIStats(int &shaderswitches) const = 0;
  765. void createQuadIndexBuffer();
  766. Texture *getTemporaryTexture(PixelFormat format, int w, int h, int samples);
  767. void restoreState(const DisplayState &s);
  768. void restoreStateChecked(const DisplayState &s);
  769. void pushTransform();
  770. void pushIdentityTransform();
  771. void popTransform();
  772. int width;
  773. int height;
  774. int pixelWidth;
  775. int pixelHeight;
  776. bool created;
  777. bool active;
  778. bool writingToStencil;
  779. StrongRef<love::graphics::Font> defaultFont;
  780. std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
  781. BatchedDrawState batchedDrawState;
  782. std::vector<Matrix4> transformStack;
  783. Matrix4 projectionMatrix;
  784. std::vector<double> pixelScaleStack;
  785. std::vector<DisplayState> states;
  786. std::vector<StackType> stackTypeStack;
  787. std::vector<TemporaryTexture> temporaryTextures;
  788. int renderTargetSwitchCount;
  789. int drawCalls;
  790. int drawCallsBatched;
  791. Buffer *quadIndexBuffer;
  792. Capabilities capabilities;
  793. Deprecations deprecations;
  794. static const size_t MAX_USER_STACK_DEPTH = 128;
  795. static const int MAX_TEMPORARY_TEXTURE_UNUSED_FRAMES = 16;
  796. private:
  797. void checkSetDefaultFont();
  798. int calculateEllipsePoints(float rx, float ry) const;
  799. std::vector<uint8> scratchBuffer;
  800. std::unordered_map<std::string, ShaderStage *> cachedShaderStages[SHADERSTAGE_MAX_ENUM];
  801. }; // Graphics
  802. } // graphics
  803. } // love
  804. #endif // LOVE_GRAPHICS_GRAPHICS_H