1 #include "lxgui/gui_renderer.hpp"
3 #include "lxgui/gui_atlas.hpp"
4 #include "lxgui/gui_out.hpp"
5 #include "lxgui/gui_quad.hpp"
6 #include "lxgui/gui_render_target.hpp"
7 #include "lxgui/utils_string.hpp"
13 current_material_ =
nullptr;
17 if (quad_cache_[0].cache ==
nullptr) {
18 for (std::size_t index = 0u; index < batching_cache_cycle_size; ++index) {
22 }
catch (
const std::exception& e) {
25 <<
"gui::renderer: Failed to create caches for quad batching. Vertex "
26 "caches will be disabled."
29 vertex_cache_enabled_ =
false;
46 last_frame_batch_count_ = batch_count_;
47 last_frame_vertex_count_ = vertex_count_;
53 return last_frame_batch_count_;
57 return last_frame_vertex_count_;
72 bool renderer::uses_same_texture_(
const material* mat1,
const material* mat2)
const {
90 const material* mat,
const std::vector<std::array<vertex, 4>>& quad_list) {
91 if (quad_list.empty())
96 vertex_count_ += quad_list.size() * 6;
102 if (!uses_same_texture_(mat, current_material_)) {
105 current_material_ = mat;
108 if (quad_cache_[current_quad_cache_].data.empty()) {
110 current_material_ = mat;
113 if (!current_material_) {
115 current_material_ = mat;
119 auto& cache = quad_cache_[current_quad_cache_];
125 cache.data.reserve(cache.data.size() + quad_list.size());
126 for (
const auto& orig_quad : quad_list) {
127 cache.data.push_back(orig_quad);
128 auto&
quad = cache.data.back();
132 cache.data.insert(cache.data.end(), quad_list.begin(), quad_list.end());
137 auto& cache = quad_cache_[current_quad_cache_];
138 if (cache.data.empty())
141 vertex_count_ += cache.data.size() * 6;
144 cache.cache->update(cache.data[0].data(), cache.data.size() * 4);
151 current_material_ =
nullptr;
153 ++current_quad_cache_;
154 if (current_quad_cache_ == batching_cache_cycle_size)
155 current_quad_cache_ = 0u;
174 return quad_batching_enabled_;
178 quad_batching_enabled_ = enabled;
181 std::shared_ptr<gui::material>
183 std::string backed_name = utils::to_string(
static_cast<std::size_t
>(filt)) +
'|' + file_name;
186 if (std::shared_ptr<gui::material> lock = iter->second.lock())
196 }
catch (
const std::exception& e) {
203 std::string hash_font_parameters(
204 const std::string& font_file,
207 const std::vector<code_point_range>& code_points,
208 char32_t default_code_point) {
209 std::string font_name = font_file +
"|s" + utils::to_string(size);
211 font_name +=
"|o" + utils::to_string(outline);
214 font_name +=
"|c" + utils::to_string(range.first) +
"-" + utils::to_string(range.last);
216 font_name +=
"|d" + utils::to_string(default_code_point);
223 const std::string& font_file,
226 const std::vector<code_point_range>& code_points,
227 char32_t default_code_point) {
228 const std::string font_name =
229 hash_font_parameters(font_file, size, outline, code_points, default_code_point);
233 if (std::shared_ptr<gui::font> lock = iter->second.lock())
239 std::shared_ptr<gui::font> fnt =
240 create_font_(font_file, size, outline, code_points, default_code_point);
251 texture_atlas_enabled_ = enabled;
255 if (texture_atlas_page_size_ == 0u)
258 return texture_atlas_page_size_;
262 texture_atlas_page_size_ = page_size;
266 std::size_t count = 0;
269 count += page.second->get_page_count();
280 vertex_cache_enabled_ = enabled;
284 vertex_cache_enabled_ =
true;
285 texture_atlas_enabled_ =
true;
286 quad_batching_enabled_ =
true;
290 std::shared_ptr<gui::atlas>
atlas;
292 std::string baked_atlas_name =
293 utils::to_string(
static_cast<std::size_t
>(filt)) +
'|' + atlas_category;
296 atlas = iter->second;
308 const std::string& atlas_category,
const std::string& file_name,
material::filter filt) {
330 const std::string& atlas_category,
331 const std::string& font_file,
334 const std::vector<code_point_range>& code_points,
335 char32_t default_code_point) {
337 return create_font(font_file, size, outline, code_points, default_code_point);
341 const std::string font_name =
342 hash_font_parameters(font_file, size, outline, code_points, default_code_point);
348 fnt =
create_font(font_file, size, outline, code_points, default_code_point);
360 const auto& rect = target->get_rect();
A class that holds multiple materials for efficient rendering.
std::shared_ptr< material > add_material(const std::string &file_name, const material &mat)
Add a new material to the atlas.
bool add_font(const std::string &font_name, std::shared_ptr< gui::font > fnt)
Add a new font to the atlas.
std::shared_ptr< material > fetch_material(const std::string &file_name) const
Find a material in this atlas (nullptr if not found).
std::shared_ptr< font > fetch_font(const std::string &font_name) const
Find a font in this atlas (nullptr if not found).
A class that holds rendering data.
virtual bool uses_same_texture(const material &other) const =0
Checks if another material is based on the same texture as the current material.
bool is_in_atlas() const
Checks if the material is embedded in an atlas.
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.
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.
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.
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.
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.
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.
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::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.
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.
An object representing cached vertex data on the GPU.
@ quads
3 vertices per element
std::size_t get_vertex_count() const
Returns the number of vertices stored in this cache.
vector2< float > vector2f
Holds 2D coordinates (as floats)
const std::string warning
Represents a contiguous range of unicode code points.
A 4x4 matrix, used for coordinate transformations.
static const matrix4f identity
Simple structure holding four vertices and a material.
std::shared_ptr< material > mat