lxgui
gui_renderer.hpp
1 #ifndef LXGUI_GUI_RENDERER_HPP
2 #define LXGUI_GUI_RENDERER_HPP
3 
4 #include "lxgui/gui_code_point_range.hpp"
5 #include "lxgui/gui_material.hpp"
6 #include "lxgui/gui_matrix4.hpp"
7 #include "lxgui/gui_vertex_cache.hpp"
8 #include "lxgui/lxgui.hpp"
9 
10 #include <array>
11 #include <memory>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 
16 namespace lxgui::gui {
17 
18 class font;
19 class atlas;
20 class render_target;
21 class color;
22 struct quad;
23 struct vertex;
24 
26 class renderer {
27 public:
29  renderer() = default;
30 
32  renderer(const renderer&) = delete;
33 
35  renderer(renderer&&) = delete;
36 
38  renderer& operator=(const renderer&) = delete;
39 
41  renderer& operator=(renderer&&) = delete;
42 
44  virtual ~renderer() = default;
45 
50  virtual std::string get_name() const = 0;
51 
56  virtual bool is_texture_vertex_color_supported() const = 0;
57 
62  bool is_quad_batching_enabled() const;
63 
75  void set_quad_batching_enabled(bool enabled);
76 
81  virtual std::size_t get_texture_max_size() const = 0;
82 
89  virtual bool is_texture_atlas_supported() const = 0;
90 
95  bool is_texture_atlas_enabled() const;
96 
108  void set_texture_atlas_enabled(bool enabled);
109 
114  std::size_t get_texture_atlas_page_size() const;
115 
126  void set_texture_atlas_page_size(std::size_t page_size);
127 
132  std::size_t get_texture_atlas_page_count() const;
133 
138  virtual bool is_vertex_cache_supported() const = 0;
139 
144  bool is_vertex_cache_enabled() const;
145 
152  void set_vertex_cache_enabled(bool enabled);
153 
155  void auto_detect_settings();
156 
162  void reset_counters();
163 
168  std::size_t get_batch_count() const;
169 
174  std::size_t get_vertex_count() const;
175 
180  void begin(std::shared_ptr<render_target> target = nullptr);
181 
183  void end();
184 
199  void flush_quad_batch();
200 
217  void set_view(const matrix4f& view_matrix);
218 
226  virtual matrix4f get_view() const = 0;
227 
234  void render_quad(const quad& q);
235 
245  void render_quads(const material* mat, const std::vector<std::array<vertex, 4>>& quad_list);
246 
264  void render_cache(
265  const material* mat,
266  const vertex_cache& cache,
267  const matrix4f& model_transform = matrix4f::identity);
268 
277  std::shared_ptr<material>
278  create_material(const std::string& file_name, material::filter filt = material::filter::none);
279 
302  std::shared_ptr<material> create_atlas_material(
303  const std::string& atlas_category,
304  const std::string& file_name,
306 
313  virtual std::shared_ptr<material>
314  create_material(std::shared_ptr<render_target> target, const bounds2f& location) = 0;
315 
321  std::shared_ptr<material> create_material(std::shared_ptr<render_target> target);
322 
330  virtual std::shared_ptr<material> create_material(
331  const vector2ui& dimensions,
332  const color32* pixel_data,
334 
340  virtual std::shared_ptr<render_target> create_render_target(
341  const vector2ui& dimensions, material::filter filt = material::filter::none) = 0;
342 
357  std::shared_ptr<font> create_font(
358  const std::string& font_file,
359  std::size_t size,
360  std::size_t outline,
361  const std::vector<code_point_range>& code_points,
362  char32_t default_code_point);
363 
377  std::shared_ptr<font> create_atlas_font(
378  const std::string& atlas_category,
379  const std::string& font_file,
380  std::size_t size,
381  std::size_t outline,
382  const std::vector<code_point_range>& code_points,
383  char32_t default_code_point);
384 
390  virtual std::shared_ptr<vertex_cache> create_vertex_cache(gui::vertex_cache::type type) = 0;
391 
396  virtual void notify_window_resized(const vector2ui& dimensions);
397 
398 protected:
403  virtual void begin_(std::shared_ptr<render_target> target) = 0;
404 
406  virtual void end_() = 0;
407 
424  virtual void set_view_(const matrix4f& view_matrix) = 0;
425 
435  virtual void
436  render_quads_(const material* mat, const std::vector<std::array<vertex, 4>>& quad_list) = 0;
437 
455  virtual void render_cache_(
456  const material* mat, const vertex_cache& cache, const matrix4f& model_transform) = 0;
457 
466  virtual std::shared_ptr<material>
467  create_material_(const std::string& file_name, material::filter filt) = 0;
468 
474  virtual std::shared_ptr<atlas> create_atlas_(material::filter filt) = 0;
475 
487  virtual std::shared_ptr<font> create_font_(
488  const std::string& font_file,
489  std::size_t size,
490  std::size_t outline,
491  const std::vector<code_point_range>& code_points,
492  char32_t default_code_point) = 0;
493 
494  atlas& get_atlas_(const std::string& atlas_category, material::filter filt);
495 
496  std::unordered_map<std::string, std::weak_ptr<gui::material>> texture_list_;
497  std::unordered_map<std::string, std::shared_ptr<gui::atlas>> atlas_list_;
498  std::unordered_map<std::string, std::weak_ptr<gui::font>> font_list_;
499 
500 private:
501  bool uses_same_texture_(const material* mat1, const material* mat2) const;
502 
503  bool texture_atlas_enabled_ = true;
504  bool vertex_cache_enabled_ = true;
505  bool quad_batching_enabled_ = true;
506  std::size_t texture_atlas_page_size_ = 0u;
507 
508  struct quad_batcher {
509  std::vector<std::array<vertex, 4>> data;
510  std::shared_ptr<vertex_cache> cache;
511  };
512 
513  static constexpr std::size_t batching_cache_cycle_size = 16u;
514  std::array<quad_batcher, batching_cache_cycle_size> quad_cache_;
515 
516  const gui::material* current_material_ = nullptr;
517  std::size_t current_quad_cache_ = 0u;
518  std::size_t batch_count_ = 0u;
519  std::size_t vertex_count_ = 0u;
520  std::size_t last_frame_batch_count_ = 0u;
521  std::size_t last_frame_vertex_count_ = 0u;
522 };
523 
524 } // namespace lxgui::gui
525 
526 #endif
A class that holds multiple materials for efficient rendering.
Definition: gui_atlas.hpp:126
A class that holds rendering data.
Abstract type for implementation specific management.
renderer(renderer &&)=delete
Non-movable.
bool is_quad_batching_enabled() const
Checks if the renderer has quad render batching enabled.
std::shared_ptr< material > create_atlas_material(const std::string &atlas_category, const std::string &file_name, material::filter filt=material::filter::none)
Creates a new material from a texture file.
virtual std::shared_ptr< material > create_material(const vector2ui &dimensions, const color32 *pixel_data, material::filter filt=material::filter::none)=0
Creates a new material from arbitrary pixel data.
std::size_t get_batch_count() const
Returns the number of batches of vertices sent to the GPU since the last call to reset_counters.
renderer & operator=(const renderer &)=delete
Non-copiable.
std::shared_ptr< font > create_atlas_font(const std::string &atlas_category, 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)
Creates a new font.
renderer & operator=(renderer &&)=delete
Non-movable.
std::size_t get_texture_atlas_page_size() const
Returns the width/height of a texture atlas page (in pixels).
virtual std::shared_ptr< vertex_cache > create_vertex_cache(gui::vertex_cache::type type)=0
Creates a new empty vertex cache.
bool is_texture_atlas_enabled() const
Checks if the renderer has texture atlases enabled.
void render_quad(const quad &q)
Renders a quad.
virtual std::string get_name() const =0
Returns a human-readable name for this renderer.
renderer()=default
Constructor.
void set_view(const matrix4f &view_matrix)
Sets the view matrix to use when rendering (viewport).
virtual std::shared_ptr< 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)=0
Creates a new font.
void end()
Ends rendering.
virtual bool is_texture_atlas_supported() const =0
Checks if the renderer supports texture atlases natively.
void render_cache(const material *mat, const vertex_cache &cache, const matrix4f &model_transform=matrix4f::identity)
Renders a vertex cache.
virtual void notify_window_resized(const vector2ui &dimensions)
Notifies the renderer that the render window has been resized.
std::size_t get_vertex_count() const
Returns the number of vertices sent to the GPU since the last call to reset_counters.
virtual ~renderer()=default
Destructor.
std::unordered_map< std::string, std::weak_ptr< gui::font > > font_list_
void auto_detect_settings()
Automatically determines the best rendering settings for the current platform.
renderer(const renderer &)=delete
Non-copiable.
std::size_t get_texture_atlas_page_count() const
Count the total number of texture atlas pages currently in use.
void set_texture_atlas_enabled(bool enabled)
Enables/disables texture atlases.
atlas & get_atlas_(const std::string &atlas_category, material::filter filt)
virtual bool is_vertex_cache_supported() const =0
Checks if the renderer supports vertex caches.
virtual std::shared_ptr< render_target > create_render_target(const vector2ui &dimensions, material::filter filt=material::filter::none)=0
Creates a new render target.
virtual std::size_t get_texture_max_size() const =0
Returns the maximum texture width/height (in pixels).
virtual void set_view_(const matrix4f &view_matrix)=0
Sets the view matrix to use when rendering (viewport).
virtual std::shared_ptr< atlas > create_atlas_(material::filter filt)=0
Creates a new atlas with a given texture filter mode.
void set_quad_batching_enabled(bool enabled)
Enables/disables quad batching.
virtual std::shared_ptr< material > create_material(std::shared_ptr< render_target > target, const bounds2f &location)=0
Creates a new material from a portion of a render target.
void begin(std::shared_ptr< render_target > target=nullptr)
Begins rendering on a particular render target.
void set_texture_atlas_page_size(std::size_t page_size)
Set the width/height of a texture atlas page (in pixels).
void render_quads(const material *mat, const std::vector< std::array< vertex, 4 >> &quad_list)
Renders a set of quads.
virtual void end_()=0
Ends rendering.
virtual void render_quads_(const material *mat, const std::vector< std::array< vertex, 4 >> &quad_list)=0
Renders a set of quads.
std::unordered_map< std::string, std::weak_ptr< gui::material > > texture_list_
std::unordered_map< std::string, std::shared_ptr< gui::atlas > > atlas_list_
void reset_counters()
Resets the number of batches to zero (for analytics only).
void flush_quad_batch()
Flushes any pending quad batch render operation.
virtual bool is_texture_vertex_color_supported() const =0
Checks if the renderer supports setting colors for each vertex of a textured quad.
virtual std::shared_ptr< material > create_material_(const std::string &file_name, material::filter filt)=0
Creates a new material from a texture file.
std::shared_ptr< 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)
Creates a new font.
bool is_vertex_cache_enabled() const
Checks if the renderer has enabled support for vertex caches.
void set_vertex_cache_enabled(bool enabled)
Enables/disables vertex caches.
virtual void render_cache_(const material *mat, const vertex_cache &cache, const matrix4f &model_transform)=0
Renders a vertex cache.
virtual void begin_(std::shared_ptr< render_target > target)=0
Begins rendering on a particular render target.
std::shared_ptr< material > create_material(const std::string &file_name, material::filter filt=material::filter::none)
Creates a new material from a texture file.
virtual matrix4f get_view() const =0
Returns the current view matrix to use when rendering (viewport).
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
Simple structure holding four vertices and a material.
Definition: gui_quad.hpp:18