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