Graphics.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Copyright (c) 2006-2024 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. love::graphics::Texture *newTexture(const Texture::Settings &settings, const Texture::Slices *data = nullptr) override;
  48. love::graphics::Texture *newTextureView(love::graphics::Texture *base, const Texture::ViewSettings &viewsettings) override;
  49. love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector<Buffer::DataDeclaration> &format, const void *data, size_t size, size_t arraylength) override;
  50. void backbufferChanged(int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override;
  51. bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool backbufferstencil, bool backbufferdepth, int msaa) override;
  52. void unSetMode() override;
  53. void setActive(bool active) override;
  54. bool dispatch(love::graphics::Shader *shader, int x, int y, int z) override;
  55. bool dispatch(love::graphics::Shader *shader, love::graphics::Buffer *indirectargs, size_t argsoffset) override;
  56. void draw(const DrawCommand &cmd) override;
  57. void draw(const DrawIndexedCommand &cmd) override;
  58. void drawQuads(int start, int count, const VertexAttributes &attributes, const BufferBindings &buffers, love::graphics::Texture *texture) override;
  59. void clear(OptionalColorD color, OptionalInt stencil, OptionalDouble depth) override;
  60. void clear(const std::vector<OptionalColorD> &colors, OptionalInt stencil, OptionalDouble depth) override;
  61. void discard(const std::vector<bool> &colorbuffers, bool depthstencil) override;
  62. void present(void *screenshotCallbackData) override;
  63. int getRequestedBackbufferMSAA() const override;
  64. int getBackbufferMSAA() const override;
  65. void setColor(Colorf c) override;
  66. void setScissor(const Rect &rect) override;
  67. void setScissor() override;
  68. void setStencilState(const StencilState &s) override;
  69. void setDepthMode(CompareMode compare, bool write) override;
  70. void setFrontFaceWinding(Winding winding) override;
  71. void setColorMask(ColorChannelMask mask) override;
  72. void setBlendState(const BlendState &blend) override;
  73. void setPointSize(float size) override;
  74. void setWireframe(bool enable) override;
  75. bool isPixelFormatSupported(PixelFormat format, uint32 usage) override;
  76. Renderer getRenderer() const override;
  77. bool usesGLSLES() const override;
  78. RendererInfo getRendererInfo() const override;
  79. // Internal use.
  80. void cleanupRenderTexture(love::graphics::Texture *texture);
  81. void *getBufferMapMemory(size_t size);
  82. void releaseBufferMapMemory(void *mem);
  83. private:
  84. struct CachedFBOHasher
  85. {
  86. size_t operator() (const RenderTargets &rts) const
  87. {
  88. RenderTarget hashtargets[MAX_COLOR_RENDER_TARGETS + 1];
  89. int hashcount = 0;
  90. for (size_t i = 0; i < rts.colors.size(); i++)
  91. hashtargets[hashcount++] = rts.colors[i];
  92. if (rts.depthStencil.texture != nullptr)
  93. hashtargets[hashcount++] = rts.depthStencil;
  94. else if (rts.temporaryRTFlags != 0)
  95. hashtargets[hashcount++] = RenderTarget(nullptr, -1, rts.temporaryRTFlags);
  96. return XXH32(hashtargets, sizeof(RenderTarget) * hashcount, 0);
  97. }
  98. };
  99. love::graphics::ShaderStage *newShaderStageInternal(ShaderStageType stage, const std::string &cachekey, const std::string &source, bool gles) override;
  100. love::graphics::Shader *newShaderInternal(StrongRef<love::graphics::ShaderStage> stages[SHADERSTAGE_MAX_ENUM], const Shader::CompileOptions &options) override;
  101. love::graphics::StreamBuffer *newStreamBuffer(BufferUsage type, size_t size) override;
  102. love::graphics::GraphicsReadback *newReadbackInternal(ReadbackMethod method, love::graphics::Buffer *buffer, size_t offset, size_t size, data::ByteData *dest, size_t destoffset) override;
  103. love::graphics::GraphicsReadback *newReadbackInternal(ReadbackMethod method, love::graphics::Texture *texture, int slice, int mipmap, const Rect &rect, image::ImageData *dest, int destx, int desty) override;
  104. void setRenderTargetsInternal(const RenderTargets &rts, int pixelw, int pixelh, bool hasSRGBtexture) override;
  105. void initCapabilities() override;
  106. void getAPIStats(int &shaderswitches) const override;
  107. void endPass(bool presenting);
  108. GLuint bindCachedFBO(const RenderTargets &targets);
  109. void discard(OpenGL::FramebufferTarget target, const std::vector<bool> &colorbuffers, bool depthstencil);
  110. void updateBackbuffer(int width, int height, int pixelwidth, int pixelheight, int msaa);
  111. GLuint getInternalBackbufferFBO() const;
  112. GLuint getSystemBackbufferFBO() const;
  113. void setDebug(bool enable);
  114. uint32 computePixelFormatUsage(PixelFormat format, bool readable);
  115. std::unordered_map<RenderTargets, GLuint, CachedFBOHasher> framebufferObjects;
  116. bool windowHasStencil;
  117. GLuint mainVAO;
  118. StrongRef<love::graphics::Texture> internalBackbuffer;
  119. StrongRef<love::graphics::Texture> internalBackbufferDepthStencil;
  120. GLuint internalBackbufferFBO;
  121. int requestedBackbufferMSAA;
  122. char *bufferMapMemory;
  123. size_t bufferMapMemorySize;
  124. // [non-readable, readable]
  125. uint32 pixelFormatUsage[PIXELFORMAT_MAX_ENUM][2];
  126. }; // Graphics
  127. } // opengl
  128. } // graphics
  129. } // love
  130. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H