Graphics.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_OPENGL_GRAPHICS_H
  21. #define LOVE_GRAPHICS_OPENGL_GRAPHICS_H
  22. // STD
  23. #include <stack>
  24. #include <vector>
  25. #include <unordered_map>
  26. // OpenGL
  27. #include "OpenGL.h"
  28. // LOVE
  29. #include "graphics/Graphics.h"
  30. #include "common/Color.h"
  31. #include "image/Image.h"
  32. #include "image/ImageData.h"
  33. #include "Image.h"
  34. #include "Canvas.h"
  35. #include "Shader.h"
  36. #include "libraries/xxHash/xxhash.h"
  37. namespace love
  38. {
  39. namespace graphics
  40. {
  41. namespace opengl
  42. {
  43. class Graphics final : public love::graphics::Graphics
  44. {
  45. public:
  46. Graphics();
  47. virtual ~Graphics();
  48. // Implements Module.
  49. const char *getName() const override;
  50. love::graphics::Image *newImage(const Texture::Slices &data, const Image::Settings &settings) override;
  51. love::graphics::Image *newImage(TextureType textype, PixelFormat format, int width, int height, int slices, const Image::Settings &settings) override;
  52. love::graphics::Canvas *newCanvas(const Canvas::Settings &settings) override;
  53. love::graphics::Buffer *newBuffer(size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags) override;
  54. void setViewportSize(int width, int height, int pixelwidth, int pixelheight) override;
  55. bool setMode(int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil) override;
  56. void unSetMode() override;
  57. void setActive(bool active) override;
  58. void draw(const DrawCommand &cmd) override;
  59. void draw(const DrawIndexedCommand &cmd) override;
  60. void drawQuads(int start, int count, const vertex::Attributes &attributes, const vertex::BufferBindings &buffers, Texture *texture) override;
  61. void clear(OptionalColorf color, OptionalInt stencil, OptionalDouble depth) override;
  62. void clear(const std::vector<OptionalColorf> &colors, OptionalInt stencil, OptionalDouble depth) override;
  63. void discard(const std::vector<bool> &colorbuffers, bool depthstencil) override;
  64. void present(void *screenshotCallbackData) override;
  65. void setColor(Colorf c) override;
  66. void setScissor(const Rect &rect) override;
  67. void setScissor() override;
  68. void drawToStencilBuffer(StencilAction action, int value) override;
  69. void stopDrawToStencilBuffer() override;
  70. void setStencilTest(CompareMode compare, int value) override;
  71. void setDepthMode(CompareMode compare, bool write) override;
  72. void setFrontFaceWinding(vertex::Winding winding) override;
  73. void setColorMask(ColorChannelMask mask) override;
  74. void setBlendState(const BlendState &blend) override;
  75. void setPointSize(float size) override;
  76. void setWireframe(bool enable) override;
  77. PixelFormat getSizedFormat(PixelFormat format, bool rendertarget, bool readable, bool sRGB) const override;
  78. bool isPixelFormatSupported(PixelFormat format, bool rendertarget, bool readable, bool sRGB = false) override;
  79. Renderer getRenderer() const override;
  80. RendererInfo getRendererInfo() const override;
  81. Shader::Language getShaderLanguageTarget() const override;
  82. // Internal use.
  83. void cleanupCanvas(Canvas *canvas);
  84. private:
  85. struct CachedFBOHasher
  86. {
  87. size_t operator() (const RenderTargets &rts) const
  88. {
  89. RenderTarget hashtargets[MAX_COLOR_RENDER_TARGETS + 1];
  90. int hashcount = 0;
  91. for (size_t i = 0; i < rts.colors.size(); i++)
  92. hashtargets[hashcount++] = rts.colors[i];
  93. if (rts.depthStencil.canvas != nullptr)
  94. hashtargets[hashcount++] = rts.depthStencil;
  95. else if (rts.temporaryRTFlags != 0)
  96. hashtargets[hashcount++] = RenderTarget(nullptr, -1, rts.temporaryRTFlags);
  97. return XXH32(hashtargets, sizeof(RenderTarget) * hashcount, 0);
  98. }
  99. };
  100. love::graphics::ShaderStage *newShaderStageInternal(ShaderStage::StageType stage, const std::string &cachekey, const std::string &source, bool gles) override;
  101. love::graphics::Shader *newShaderInternal(love::graphics::ShaderStage *vertex, love::graphics::ShaderStage *pixel) override;
  102. love::graphics::StreamBuffer *newStreamBuffer(BufferType type, size_t size) override;
  103. void setCanvasInternal(const RenderTargets &rts, int w, int h, int pixelw, int pixelh, bool hasSRGBcanvas) override;
  104. void initCapabilities() override;
  105. void getAPIStats(int &shaderswitches) const override;
  106. void endPass();
  107. void bindCachedFBO(const RenderTargets &targets);
  108. void discard(OpenGL::FramebufferTarget target, const std::vector<bool> &colorbuffers, bool depthstencil);
  109. void setDebug(bool enable);
  110. std::unordered_map<RenderTargets, GLuint, CachedFBOHasher> framebufferObjects;
  111. bool windowHasStencil;
  112. GLuint mainVAO;
  113. // [rendertarget][readable][srgb]
  114. OptionalBool supportedFormats[PIXELFORMAT_MAX_ENUM][2][2][2];
  115. }; // Graphics
  116. } // opengl
  117. } // graphics
  118. } // love
  119. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H