lxgui
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 <limits>
15 #include <memory>
16 
17 namespace lxgui::gui::gl {
18 
20 class renderer final : public gui::renderer {
21 public:
28  explicit renderer(const vector2ui& window_dimensions, bool init_glew = true);
29 
34  std::string get_name() const override;
35 
43  matrix4f get_view() const override;
44 
49  std::size_t get_texture_max_size() const override;
50 
57  bool is_texture_atlas_supported() const override;
58 
63  bool is_texture_vertex_color_supported() const override;
64 
72  std::shared_ptr<gui::material> create_material(
73  const vector2ui& dimensions,
74  const color32* pixel_data,
76 
83  std::shared_ptr<gui::material>
84  create_material(std::shared_ptr<gui::render_target> target, const bounds2f& location) override;
85 
91  std::shared_ptr<gui::render_target> create_render_target(
92  const vector2ui& dimensions, material::filter filt = material::filter::none) override;
93 
98  bool is_vertex_cache_supported() const override;
99 
105  std::shared_ptr<gui::vertex_cache> create_vertex_cache(gui::vertex_cache::type type) override;
106 
111  void notify_window_resized(const vector2ui& new_dimensions) override;
112 
113 #if !defined(LXGUI_OPENGL3)
118  static bool is_gl_extension_supported(const std::string& extension);
119 #endif
120 
121 protected:
129  std::shared_ptr<gui::material>
130  create_material_(const std::string& file_name, material::filter filt) override;
131 
137  std::shared_ptr<gui::atlas> create_atlas_(material::filter filt) override;
138 
149  std::shared_ptr<gui::font> create_font_(
150  const std::string& font_file,
151  std::size_t size,
152  std::size_t outline,
153  const std::vector<code_point_range>& code_points,
154  char32_t default_code_point) override;
155 
160  void begin_(std::shared_ptr<gui::render_target> target) override;
161 
163  void end_() override;
164 
181  void set_view_(const matrix4f& view_matrix) override;
182 
193  const gui::material* mat, const std::vector<std::array<vertex, 4>>& quad_list) override;
194 
213  const gui::material* mat,
214  const gui::vertex_cache& cache,
215  const matrix4f& model_transform) override;
216 
217 private:
218  void update_view_matrix_() const;
219 #if defined(LXGUI_OPENGL3)
220  void compile_programs_();
221  void setup_buffers_();
222 #endif
223 
224  std::shared_ptr<gui::material>
225  create_material_png_(const std::string& file_name, material::filter filt) const;
226 
227  vector2ui window_dimensions_;
228 
229  std::shared_ptr<gui::gl::render_target> current_target_;
230  matrix4f current_view_matrix_ = matrix4f::identity;
231 
232 #if defined(LXGUI_OPENGL3)
233  struct shader_cache {
234  shader_cache() = default;
235  shader_cache(const shader_cache&) = delete;
236  shader_cache(shader_cache&&) = delete;
237  ~shader_cache();
238 
239  std::uint32_t program = 0;
240  int sampler_location = 0;
241  int proj_location = 0;
242  int model_location = 0;
243  int type_location = 0;
244  };
245 
246  static thread_local std::weak_ptr<shader_cache> static_shader_cache;
247  std::shared_ptr<shader_cache> shader_cache_;
248 
249  static constexpr std::size_t cache_cycle_size = 1024u;
250  std::array<std::shared_ptr<gl::vertex_cache>, cache_cycle_size> quad_cache_;
251  std::array<std::shared_ptr<gl::vertex_cache>, cache_cycle_size> array_cache_;
252  std::uint32_t quad_cycle_cache_ = 0u;
253  std::uint32_t array_cycle_cache_ = 0u;
254 
255  std::uint32_t previous_texture_ = std::numeric_limits<std::uint32_t>::max();
256 #endif
257 };
258 
259 } // namespace lxgui::gui::gl
260 
261 #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).
bool is_texture_atlas_supported() const override
Checks if the renderer supports texture atlases natively.
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.
matrix4f get_view() const override
Returns the current view matrix to use when rendering (viewport).
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 set_view_(const matrix4f &view_matrix) override
Sets the view matrix to use when rendering (viewport).
void render_quads_(const gui::material *mat, const std::vector< std::array< vertex, 4 >> &quad_list) override
Renders a set of quads.
renderer(const vector2ui &window_dimensions, bool init_glew=true)
Constructor.
std::shared_ptr< gui::atlas > create_atlas_(material::filter filt) override
Creates a new atlas with a given texture filter mode.
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::string get_name() const override
Returns a human-readable name for this renderer.
void end_() override
Ends rendering.
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.
void notify_window_resized(const vector2ui &new_dimensions) override
Notifies the renderer that the render window has been resized.
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(std::shared_ptr< gui::render_target > target, const bounds2f &location) override
Creates a new material from a portion of a render target.
std::shared_ptr< gui::vertex_cache > create_vertex_cache(gui::vertex_cache::type type) override
Creates a new empty vertex cache.
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:75
A 4x4 matrix, used for coordinate transformations.
Definition: gui_matrix4.hpp:13
static const matrix4f identity
Definition: gui_matrix4.hpp:51