From eecb1fb3f2e03c4620dd3c6e6810b7a600edd2c1 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 11 Nov 2015 17:13:30 +0100 Subject: [PATCH] Cache text states --- panda/src/text/textAssembler.cxx | 25 ++----------- panda/src/text/textProperties.I | 12 ++++++ panda/src/text/textProperties.cxx | 61 +++++++++++++++++++++++++++++++ panda/src/text/textProperties.h | 8 ++++ 4 files changed, 85 insertions(+), 21 deletions(-) diff --git a/panda/src/text/textAssembler.cxx b/panda/src/text/textAssembler.cxx index 609ea8c676..99db6b28b4 100644 --- a/panda/src/text/textAssembler.cxx +++ b/panda/src/text/textAssembler.cxx @@ -587,33 +587,16 @@ assemble_text() { if (placement->_properties != properties) { // Get a new set of properties for future glyphs. properties = placement->_properties; - text_state = RenderState::make_empty(); - shadow_state = RenderState::make_empty(); + text_state = properties->get_text_state(); shadow_xform = LMatrix4::ident_mat(); - if (properties->has_text_color()) { - text_state = text_state->add_attrib(ColorAttrib::make_flat(properties->get_text_color())); - if (properties->get_text_color()[3] != 1.0) { - text_state = text_state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha)); - } - } - - if (properties->has_bin()) { - text_state = text_state->add_attrib(CullBinAttrib::make(properties->get_bin(), properties->get_draw_order() + 2)); - } - if (properties->has_shadow()) { - shadow_state = shadow_state->add_attrib(ColorAttrib::make_flat(properties->get_shadow_color())); - if (properties->get_shadow_color()[3] != 1.0) { - shadow_state = shadow_state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha)); - } - - if (properties->has_bin()) { - shadow_state = shadow_state->add_attrib(CullBinAttrib::make(properties->get_bin(), properties->get_draw_order() + 1)); - } + shadow_state = properties->get_shadow_state(); LVector2 offset = properties->get_shadow(); shadow_xform = LMatrix4::translate_mat(offset[0], 0.0f, -offset[1]); + } else { + shadow_state.clear(); } } diff --git a/panda/src/text/textProperties.I b/panda/src/text/textProperties.I index 05c3521b05..0adeb553b0 100644 --- a/panda/src/text/textProperties.I +++ b/panda/src/text/textProperties.I @@ -550,6 +550,7 @@ INLINE void TextProperties:: set_text_color(const LColor &text_color) { _text_color = text_color; _specified |= F_has_text_color; + _text_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -562,6 +563,7 @@ INLINE void TextProperties:: clear_text_color() { _text_color.set(1.0f, 1.0f, 1.0f, 1.0f); _specified &= ~F_has_text_color; + _text_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -603,6 +605,7 @@ INLINE void TextProperties:: set_shadow_color(const LColor &shadow_color) { _shadow_color = shadow_color; _specified |= F_has_shadow_color; + _shadow_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -614,6 +617,7 @@ INLINE void TextProperties:: clear_shadow_color() { _shadow_color.set(0.0f, 0.0f, 0.0f, 1.0f); _specified &= ~F_has_shadow_color; + _shadow_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -711,6 +715,8 @@ INLINE void TextProperties:: set_bin(const string &bin) { _bin = bin; _specified |= F_has_bin; + _text_state.clear(); + _shadow_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -725,6 +731,8 @@ INLINE void TextProperties:: clear_bin() { _bin = string(); _specified &= ~F_has_bin; + _text_state.clear(); + _shadow_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -768,6 +776,8 @@ INLINE int TextProperties:: set_draw_order(int draw_order) { _draw_order = draw_order; _specified |= F_has_draw_order; + _text_state.clear(); + _shadow_state.clear(); return _draw_order + 3; } @@ -780,6 +790,8 @@ INLINE void TextProperties:: clear_draw_order() { _draw_order = 1; _specified &= ~F_has_draw_order; + _text_state.clear(); + _shadow_state.clear(); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/text/textProperties.cxx b/panda/src/text/textProperties.cxx index ae764288dd..e5ea6822b6 100644 --- a/panda/src/text/textProperties.cxx +++ b/panda/src/text/textProperties.cxx @@ -61,6 +61,8 @@ TextProperties() { TextProperties:: TextProperties(const TextProperties ©) { (*this) = copy; + _text_state = copy._text_state; + _shadow_state = copy._shadow_state; } //////////////////////////////////////////////////////////////////// @@ -91,6 +93,9 @@ operator = (const TextProperties ©) { _glyph_scale = copy._glyph_scale; _glyph_shift = copy._glyph_shift; _text_scale = copy._text_scale; + + _text_state.clear(); + _shadow_state.clear(); } //////////////////////////////////////////////////////////////////// @@ -372,6 +377,62 @@ write(ostream &out, int indent_level) const { } } +//////////////////////////////////////////////////////////////////// +// Function: TextProperties::get_text_state +// Access: Public +// Description: Returns a RenderState object suitable for rendering +// text with these properties. +//////////////////////////////////////////////////////////////////// +const RenderState *TextProperties:: +get_text_state() const { + if (!_text_state.is_null()) { + return _text_state; + } + + CPT(RenderState) state = RenderState::make_empty(); + + if (has_text_color()) { + state = state->add_attrib(ColorAttrib::make_flat(get_text_color())); + if (get_text_color()[3] != 1.0) { + state = state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha)); + } + } + + if (has_bin()) { + state = state->add_attrib(CullBinAttrib::make(get_bin(), get_draw_order() + 2)); + } + + swap(_text_state, state); + return _text_state; +} + +//////////////////////////////////////////////////////////////////// +// Function: TextProperties::get_shadow_state +// Access: Public +// Description: Returns a RenderState object suitable for rendering +// the shadow of this text with these properties. +//////////////////////////////////////////////////////////////////// +const RenderState *TextProperties:: +get_shadow_state() const { + if (!_shadow_state.is_null()) { + return _shadow_state; + } + + CPT(RenderState) state = RenderState::make_empty(); + + state = state->add_attrib(ColorAttrib::make_flat(get_shadow_color())); + if (get_shadow_color()[3] != 1.0) { + state = state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha)); + } + + if (has_bin()) { + state = state->add_attrib(CullBinAttrib::make(get_bin(), get_draw_order() + 1)); + } + + swap(_shadow_state, state); + return _shadow_state; +} + //////////////////////////////////////////////////////////////////// // Function: TextProperties::load_default_font // Access: Private, Static diff --git a/panda/src/text/textProperties.h b/panda/src/text/textProperties.h index 68710bbbba..e3478d41fa 100644 --- a/panda/src/text/textProperties.h +++ b/panda/src/text/textProperties.h @@ -21,6 +21,7 @@ #include "luse.h" #include "textFont.h" #include "pointerTo.h" +#include "renderState.h" //////////////////////////////////////////////////////////////////// // Class : TextProperties @@ -169,6 +170,10 @@ PUBLISHED: void write(ostream &out, int indent_level = 0) const; +public: + const RenderState *get_text_state() const; + const RenderState *get_shadow_state() const; + private: static void load_default_font(); @@ -216,6 +221,9 @@ private: PN_stdfloat _glyph_shift; PN_stdfloat _text_scale; + mutable CPT(RenderState) _text_state; + mutable CPT(RenderState) _shadow_state; + static PT(TextFont) _default_font; static bool _loaded_default_font;