AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
OpenGLRenderer.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14
15#include <AUI/GL/Program.h>
16#include <AUI/GL/Framebuffer.h>
17#include <AUI/GL/Vao.h>
18#include "AUI/Render/ABorderStyle.h"
19#include "AUI/Render/IRenderer.h"
20#include "AUI/GL/RenderTarget/TextureRenderTarget.h"
21
22class OpenGLRenderer final: public IRenderer {
23friend class OpenGLPrerenderedString;
24friend class OpenGLMultiStringCanvas;
25public:
27 Util::SimpleTexturePacker texturePacker;
28 gl::Texture2D texture;
29 bool isTextureInvalid = true;
30
32 texture.bind();
33 texture.setupNearest();
34 }
35 };
36
37private:
38 AOptional<gl::Program> mSolidShader;
39 AOptional<gl::Program> mGradientShader;
40 AOptional<gl::Program> mRoundedSolidShader;
41 AOptional<gl::Program> mRoundedSolidShaderBorder;
42 AOptional<gl::Program> mRoundedGradientShader;
43 AOptional<gl::Program> mBoxShadowShader;
44 AOptional<gl::Program> mBoxShadowInnerShader;
45 AOptional<gl::Program> mTexturedShader;
46 AOptional<gl::Program> mUnblendShader;
47 AOptional<gl::Program> mSymbolShader;
48 AOptional<gl::Program> mSymbolShaderSubPixel;
49 AOptional<gl::Program> mSquareSectorShader;
50 AOptional<gl::Program> mLineSolidDashedShader;
51 gl::Vao mRectangleVao;
52 gl::Vao mBorderVao;
53 gl::Texture2D mGradientTexture;
54
55
56 struct CharacterData {
57 glm::vec4 uv;
58 };
59
60 ADeque<CharacterData> mCharData;
61 ADeque<FontEntryData> mFontEntryData;
62 IRenderViewToTexture* mRenderToTextureTarget = nullptr;
63
64 struct FramebufferWithTextureRT {
65 gl::Framebuffer framebuffer;
67 };
68
69 struct FramebufferBackToPool {
70 OpenGLRenderer* renderer;
71 void operator()(FramebufferWithTextureRT* framebuffer) const;
72 };
73
74 using FramebufferFromPool = std::unique_ptr<FramebufferWithTextureRT, FramebufferBackToPool>;
75 using OffscreenFramebufferPool = AVector<FramebufferFromPool>;
76
80 OffscreenFramebufferPool mFramebuffersForMultiPassEffectsPool;
81
82
83 static std::array<glm::vec2, 4> getVerticesForRect(glm::vec2 position, glm::vec2 size);
84
85 void uploadToShaderCommon();
86
87 FontEntryData* getFontEntryData(const AFontStyle& fontStyle);
88
92 bool setupLineShader(const ABrush& brush, const ABorderStyle& style, float widthPx);
93
94
112 FramebufferFromPool getFramebufferForMultiPassEffect(glm::uvec2 minRequiredSize);
113
114 void backdrops(glm::ivec2 fbSize, glm::ivec2 size, std::span<ass::Backdrop::Preprocessed> backdrops) override;
115
116protected:
117 _unique<ITexture> createNewTexture() override;
118
119public:
121 ~OpenGLRenderer() override = default;
122 void identityUv();
123 bool isVaoAvailable() const noexcept;
124
125 void rectangle(const ABrush& brush,
126 glm::vec2 position,
127 glm::vec2 size) override;
128
129 void roundedRectangle(const ABrush& brush,
130 glm::vec2 position,
131 glm::vec2 size,
132 float radius) override;
133
134 void rectangleBorder(const ABrush& brush,
135 glm::vec2 position,
136 glm::vec2 size,
137 float lineWidth) override;
138
139 void roundedRectangleBorder(const ABrush& brush,
140 glm::vec2 position,
141 glm::vec2 size,
142 float radius,
143 int borderWidth) override;
144
145 void boxShadow(glm::vec2 position,
146 glm::vec2 size,
147 float blurRadius,
148 const AColor& color) override;
149
150 void boxShadowInner(glm::vec2 position,
151 glm::vec2 size,
152 float blurRadius,
153 float spreadRadius,
154 float borderRadius,
155 const AColor& color,
156 glm::vec2 offset) override;
157
158 void string(glm::vec2 position,
159 const AString& string,
160 const AFontStyle& fs) override;
161
162 _<IPrerenderedString> prerenderString(glm::vec2 position, const AString& text, const AFontStyle& fs) override;
163
164 void drawRectImpl(glm::vec2 position, glm::vec2 size);
165
166 void setBlending(Blending blending) override;
167
168 _<IMultiStringCanvas> newMultiStringCanvas(const AFontStyle& style) override;
169
170 glm::mat4 getProjectionMatrix() const override;
171
172 void lines(const ABrush& brush, AArrayView<glm::vec2> points, const ABorderStyle& style, AMetric width) override;
173
174 void lines(const ABrush& brush, AArrayView<std::pair<glm::vec2, glm::vec2>> points, const ABorderStyle& style, AMetric width) override;
175
176 void points(const ABrush& brush, AArrayView<glm::vec2> points, AMetric size) override;
177
178 void squareSector(const ABrush& brush,
179 const glm::vec2& position,
180 const glm::vec2& size,
181 AAngleRadians begin,
182 AAngleRadians end) override;
183
184 void pushMaskBefore() override;
185 void pushMaskAfter() override;
186 void popMaskBefore() override;
187
188 _unique<IRenderViewToTexture> newRenderViewToTexture() noexcept override;
189
190 void popMaskAfter() override;
191
192 void beginPaint(glm::uvec2 windowSize);
193 void endPaint();
194
195 uint32_t getDefaultFb() const noexcept;
196 void bindTemporaryVao() const noexcept;
197};
198
199
Strong type used to store angle in radians.
Definition: AAngleRadians.h:42
Definition: AArrayView.h:25
Describes border style, like CSS border-style.
Definition: ABorderStyle.h:28
Represents a 4-component floating point color.
Definition: AColor.h:27
A std::deque with AUI extensions.
Definition: ADeque.h:27
Stores dimensions in scalable units (dp, pt, etc...).
Definition: AMetric.h:75
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Represents a Unicode character string.
Definition: AString.h:37
Rendering view to texture storage interface.
Definition: IRenderViewToTexture.h:26
Base class for rendering.
Definition: IRenderer.h:149
Definition: OpenGLRenderer.cpp:646
Definition: OpenGLRenderer.cpp:535
Definition: OpenGLRenderer.h:22
void squareSector(const ABrush &brush, const glm::vec2 &position, const glm::vec2 &size, AAngleRadians begin, AAngleRadians end) override
Draws sector in rectangle shape. The sector is drawn clockwise from begin to end angles.
Definition: OpenGLRenderer.cpp:964
void roundedRectangle(const ABrush &brush, glm::vec2 position, glm::vec2 size, float radius) override
Draws rounded rect (with antialiasing, if msaa enabled).
Definition: OpenGLRenderer.cpp:334
void points(const ABrush &brush, AArrayView< glm::vec2 > points, AMetric size) override
Draws points list.
Definition: OpenGLRenderer.cpp:934
void setBlending(Blending blending) override
Sets blending mode.
Definition: OpenGLRenderer.cpp:494
void popMaskAfter() override
Switches drawing to the color buffer back from the stencil. Decreases stencil depth.
Definition: OpenGLRenderer.cpp:833
void pushMaskBefore() override
witches drawing to the stencil buffer instead of color buffer.
Definition: OpenGLRenderer.cpp:813
void popMaskBefore() override
Switches drawing to the stencil buffer instead of color buffer.
Definition: OpenGLRenderer.cpp:826
_< IPrerenderedString > prerenderString(glm::vec2 position, const AString &text, const AFontStyle &fs) override
Analyzes string and creates an instance of IRenderer::IPrerenderedString which helps IRenderer to eff...
Definition: OpenGLRenderer.cpp:777
void rectangle(const ABrush &brush, glm::vec2 position, glm::vec2 size) override
Draws simple rectangle.
Definition: OpenGLRenderer.cpp:304
void lines(const ABrush &brush, AArrayView< glm::vec2 > points, const ABorderStyle &style, AMetric width) override
Draws polyline (non-loop line strip).
Definition: OpenGLRenderer.cpp:879
_< IMultiStringCanvas > newMultiStringCanvas(const AFontStyle &style) override
Creates new canvas for batching multiple prerender string calls.
Definition: OpenGLRenderer.cpp:805
void roundedRectangleBorder(const ABrush &brush, glm::vec2 position, glm::vec2 size, float radius, int borderWidth) override
Draws rounded rectangle's border.
Definition: OpenGLRenderer.cpp:391
void boxShadowInner(glm::vec2 position, glm::vec2 size, float blurRadius, float spreadRadius, float borderRadius, const AColor &color, glm::vec2 offset) override
Draws inner (inset) rectangle-shaped shadow.
Definition: OpenGLRenderer.cpp:451
void pushMaskAfter() override
Switches drawing to the color buffer back from the stencil. Increases stencil depth.
Definition: OpenGLRenderer.cpp:820
void boxShadow(glm::vec2 position, glm::vec2 size, float blurRadius, const AColor &color) override
Draws a rectangle-shaped shadow.
Definition: OpenGLRenderer.cpp:414
_unique< IRenderViewToTexture > newRenderViewToTexture() noexcept override
Returns a new instance of IRenderViewToTexture interface associated with this renderer.
Definition: OpenGLRenderer.cpp:1035
void rectangleBorder(const ABrush &brush, glm::vec2 position, glm::vec2 size, float lineWidth) override
Draws rectangle's border.
Definition: OpenGLRenderer.cpp:351
Definition: SimpleTexturePacker.h:18
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Definition: Framebuffer.h:26
Definition: Texture2D.h:20
Definition: Vao.h:21
Definition: AFontStyle.h:24
Definition: OpenGLRenderer.h:26
Definition: Size.h:22
Definition: Text.h:21
Forbids copy of your class.
Definition: values.h:40