lxgui
Loading...
Searching...
No Matches
gui_gl_renderer.hpp
1#ifndef LXGUI_GUI_GL_RENDERER_HPP
2#define LXGUI_GUI_GL_RENDERER_HPP
3
4#include "lxgui/impl/gui_gl_render_target.hpp"
5#if defined(LXGUI_OPENGL3)
6# include "lxgui/impl/gui_gl_vertex_cache.hpp"
7#endif
8
9#include "lxgui/gui_matrix4.hpp"
10#include "lxgui/gui_renderer.hpp"
11#include "lxgui/utils.hpp"
12
13#include <array>
14#include <cstdint>
15#include <limits>
16#include <memory>
17
18namespace lxgui::gui::gl {
19
21class renderer final : public gui::renderer {
22public:
29 explicit renderer(const vector2ui& window_dimensions, bool init_glew = true);
30
35 std::string get_name() const override;
36
44 matrix4f get_view() const override;
45
50 std::size_t get_texture_max_size() const override;
51
58 bool is_texture_atlas_supported() const override;
59
64 bool is_texture_vertex_color_supported() const override;
65
73 std::shared_ptr<gui::material> create_material(
74 const vector2ui& dimensions,
75 const color32* pixel_data,
77
84 std::shared_ptr<gui::material>
85 create_material(std::shared_ptr<gui::render_target> target, const bounds2f& location) override;
86
92 std::shared_ptr<gui::render_target> create_render_target(
93 const vector2ui& dimensions, material::filter filt = material::filter::none) override;
94
99 bool is_vertex_cache_supported() const override;
100
106 std::shared_ptr<gui::vertex_cache> create_vertex_cache(gui::vertex_cache::type type) override;
107
112 void notify_window_resized(const vector2ui& new_dimensions) override;
113
114#if !defined(LXGUI_OPENGL3)
119 static bool is_gl_extension_supported(const std::string& extension);
120#endif
121
122protected:
130 std::shared_ptr<gui::material>
131 create_material_(const std::string& file_name, material::filter filt) override;
132
138 std::shared_ptr<gui::atlas> create_atlas_(material::filter filt) override;
139
150 std::shared_ptr<gui::font> create_font_(
151 const std::string& font_file,
152 std::size_t size,
153 std::size_t outline,
154 const std::vector<code_point_range>& code_points,
155 char32_t default_code_point) override;
156
161 void begin_(std::shared_ptr<gui::render_target> target) override;
162
164 void end_() override;
165
182 void set_view_(const matrix4f& view_matrix) override;
183
194 const gui::material* mat, const std::vector<std::array<vertex, 4>>& quad_list) override;
195
214 const gui::material* mat,
215 const gui::vertex_cache& cache,
216 const matrix4f& model_transform) override;
217
218private:
219 void update_view_matrix_() const;
220#if defined(LXGUI_OPENGL3)
221 void compile_programs_();
222 void setup_buffers_();
223#endif
224
225 std::shared_ptr<gui::material>
226 create_material_png_(const std::string& file_name, material::filter filt) const;
227
228 vector2ui window_dimensions_;
229
230 std::shared_ptr<gui::gl::render_target> current_target_;
231 matrix4f current_view_matrix_ = matrix4f::identity;
232
233#if defined(LXGUI_OPENGL3)
234 struct shader_cache {
235 shader_cache() = default;
236 shader_cache(const shader_cache&) = delete;
237 shader_cache(shader_cache&&) = delete;
238 ~shader_cache();
239
240 std::uint32_t program = 0;
241 int sampler_location = 0;
242 int proj_location = 0;
243 int model_location = 0;
244 int type_location = 0;
245 };
246
247 static thread_local std::weak_ptr<shader_cache> static_shader_cache;
248 std::shared_ptr<shader_cache> shader_cache_;
249
250 static constexpr std::size_t cache_cycle_size = 1024u;
251 std::array<std::shared_ptr<gl::vertex_cache>, cache_cycle_size> quad_cache_;
252 std::array<std::shared_ptr<gl::vertex_cache>, cache_cycle_size> array_cache_;
253 std::uint32_t quad_cycle_cache_ = 0u;
254 std::uint32_t array_cycle_cache_ = 0u;
255
256 std::uint32_t previous_texture_ = std::numeric_limits<std::uint32_t>::max();
257#endif
258};
259
260} // namespace lxgui::gui::gl
261
262#endif
Open implementation of rendering.
void begin_(std::shared_ptr< gui::render_target > target) override
Begins rendering on a particular render target.
bool is_texture_vertex_color_supported() const override
Checks if the renderer supports setting colors for each vertex of a textured quad.
std::size_t get_texture_max_size() const override
Returns the maximum texture width/height (in pixels).
std::shared_ptr< gui::vertex_cache > create_vertex_cache(gui::vertex_cache::type type) override
Creates a new empty vertex cache.
bool is_texture_atlas_supported() const override
Checks if the renderer supports texture atlases natively.
matrix4f get_view() const override
Returns the current view matrix to use when rendering (viewport).
void set_view_(const matrix4f &view_matrix) override
Sets the view matrix to use when rendering (viewport).
renderer(const vector2ui &window_dimensions, bool init_glew=true)
Constructor.
std::string get_name() const override
Returns a human-readable name for this renderer.
std::shared_ptr< gui::render_target > create_render_target(const vector2ui &dimensions, material::filter filt=material::filter::none) override
Creates a new render target.
std::shared_ptr< gui::material > create_material_(const std::string &file_name, material::filter filt) override
Creates a new material from a texture file.
void end_() override
Ends rendering.
std::shared_ptr< gui::atlas > create_atlas_(material::filter filt) override
Creates a new atlas with a given texture filter mode.
void render_quads_(const gui::material *mat, const std::vector< std::array< vertex, 4 > > &quad_list) override
Renders a set of quads.
bool is_vertex_cache_supported() const override
Checks if the renderer supports vertex caches.
static bool is_gl_extension_supported(const std::string &extension)
Checks if a given OpenGL extension is supported by the machine.
void render_cache_(const gui::material *mat, const gui::vertex_cache &cache, const matrix4f &model_transform) override
Renders a vertex cache.
std::shared_ptr< gui::material > create_material(const vector2ui &dimensions, const color32 *pixel_data, material::filter filt=material::filter::none) override
Creates a new material from arbitrary pixel data.
void notify_window_resized(const vector2ui &new_dimensions) override
Notifies the renderer that the render window has been resized.
std::shared_ptr< gui::font > create_font_(const std::string &font_file, std::size_t size, std::size_t outline, const std::vector< code_point_range > &code_points, char32_t default_code_point) override
Creates a new font.
std::shared_ptr< gui::material > create_material(std::shared_ptr< gui::render_target > target, const bounds2f &location) override
Creates a new material from a portion of a render target.
A class that holds rendering data.
Abstract type for implementation specific management.
An object representing cached vertex data on the GPU.
type
The type of vertex data contained in a vertex_cache.
Holds a single color (byte RGBA, 32 bits)
Definition gui_color.hpp:76
A 4x4 matrix, used for coordinate transformations.
static const matrix4f identity