Graphics.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "Texture.h"
  34. #include "Shader.h"
  35. #include "libraries/xxHash/xxhash.h"
  36. namespace love
  37. {
  38. namespace graphics
  39. {
  40. namespace opengl
  41. {
  42. class Graphics final : public love::graphics::Graphics
  43. {
  44. public:
  45. Graphics();
  46. virtual ~Graphics();
  47. // Implements Module.
  48. const char *getName() const override;
  49. love::graphics::Texture *newTexture(const Texture::Settings &settings, const Texture::Slices *data = nullptr) override;
  50. love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) override;
  51. void setViewportSize(int width, int height, int pixelwidth, int pixelheight) override;
  52. bool setMode(int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil, int msaa) override;
  53. void unSetMode() override;
  54. void setActive(bool active) override;
  55. void draw(const DrawCommand &cmd) override;
  56. void draw(const DrawIndexedCommand &cmd) override;
  57. void drawQuads(int start, int count, const VertexAttributes &attributes, const BufferBindings &buffers, love::graphics::Texture *texture) override;
  58. void clear(OptionalColorf color, OptionalInt stencil, OptionalDouble depth) override;
  59. void clear(const std::vector<OptionalColorf> &colors, OptionalInt stencil, OptionalDouble depth) override;
  60. void discard(const std::vector<bool> &colorbuffers, bool depthstencil) override;
  61. void present(void *screenshotCallbackData) override;
  62. int getRequestedBackbufferMSAA() const override;
  63. int getBackbufferMSAA() const override;
  64. void setColor(Colorf c) override;
  65. void setScissor(const Rect &rect) override;
  66. void setScissor() override;
  67. void drawToStencilBuffer(StencilAction action, int value) override;
  68. void stopDrawToStencilBuffer() override;
  69. void setStencilTest(CompareMode compare, int value) override;
  70. void setDepthMode(CompareMode compare, bool write) override;
  71. void setFrontFaceWinding(Winding winding) override;
  72. void setColorMask(ColorChannelMask mask) override;
  73. void setBlendState(const BlendState &blend) override;
  74. void setPointSize(float size) override;
  75. void setWireframe(bool enable) override;
  76. PixelFormat getSizedFormat(PixelFormat format, bool rendertarget, bool readable) const override;
  77. bool isPixelFormatSupported(PixelFormat format, bool rendertarget, bool readable, bool sRGB = false) override;
  78. Renderer getRenderer() const override;
  79. RendererInfo getRendererInfo() const override;
  80. // Internal use.
  81. void cleanupRenderTexture(love::graphics::Texture *texture);
  82. void *getBufferMapMemory(size_t size);
  83. void releaseBufferMapMemory(void *mem);
  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.texture != 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(BufferUsage type, size_t size) override;
  103. void setRenderTargetsInternal(const RenderTargets &rts, int w, int h, int pixelw, int pixelh, bool hasSRGBtexture) override;
  104. void initCapabilities() override;
  105. void getAPIStats(int &shaderswitches) const override;
  106. void endPass();
  107. GLuint bindCachedFBO(const RenderTargets &targets);
  108. void discard(OpenGL::FramebufferTarget target, const std::vector<bool> &colorbuffers, bool depthstencil);
  109. void updateBackbuffer(int width, int height, int pixelwidth, int pixelheight, int msaa);
  110. GLuint getInternalBackbufferFBO() const;
  111. GLuint getSystemBackbufferFBO() const;
  112. void setDebug(bool enable);
  113. std::unordered_map<RenderTargets, GLuint, CachedFBOHasher> framebufferObjects;
  114. bool windowHasStencil;
  115. GLuint mainVAO;
  116. StrongRef<love::graphics::Texture> internalBackbuffer;
  117. StrongRef<love::graphics::Texture> internalBackbufferDepthStencil;
  118. GLuint internalBackbufferFBO;
  119. int requestedBackbufferMSAA;
  120. char *bufferMapMemory;
  121. size_t bufferMapMemorySize;
  122. // Only needed for buffer types that can be bound to shaders.
  123. StrongRef<love::graphics::Buffer> defaultBuffers[BUFFERUSAGE_MAX_ENUM];
  124. // [rendertarget][readable][srgb]
  125. OptionalBool supportedFormats[PIXELFORMAT_MAX_ENUM][2][2][2];
  126. }; // Graphics
  127. } // opengl
  128. } // graphics
  129. } // love
  130. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H