diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2022-06-18 16:14:08 +0300 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2022-07-04 09:47:49 +0300 |
commit | 329923c6accccf7d39b0ec954f8e3b8384bbe1bc (patch) | |
tree | 0334329d786d5951eddcac07c07f8223728baef9 /scene/resources | |
parent | 5b3b06187b79ff859bb7da81362d2234c6e64a50 (diff) |
Use custom key structs, instead of raw hashes for the Label3D and TextMesh, to avoid potential hash collisions.
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/primitive_meshes.cpp | 21 | ||||
-rw-r--r-- | scene/resources/primitive_meshes.h | 26 |
2 files changed, 33 insertions, 14 deletions
diff --git a/scene/resources/primitive_meshes.cpp b/scene/resources/primitive_meshes.cpp index f4fd81b25f..9fa483bf36 100644 --- a/scene/resources/primitive_meshes.cpp +++ b/scene/resources/primitive_meshes.cpp @@ -2190,12 +2190,12 @@ RibbonTrailMesh::RibbonTrailMesh() { /* TextMesh */ /*************************************************************************/ -void TextMesh::_generate_glyph_mesh_data(uint32_t p_hash, const Glyph &p_gl) const { - if (cache.has(p_hash)) { +void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph &p_gl) const { + if (cache.has(p_key)) { return; } - GlyphMeshData &gl_data = cache[p_hash]; + GlyphMeshData &gl_data = cache[p_key]; Dictionary d = TS->font_get_glyph_contours(p_gl.font_rid, p_gl.font_size, p_gl.index); Vector2 origin = Vector2(p_gl.x_off, p_gl.y_off) * pixel_size; @@ -2437,11 +2437,9 @@ void TextMesh::_create_mesh_array(Array &p_arr) const { continue; } if (glyphs[i].font_rid != RID()) { - uint32_t hash = hash_one_uint64(glyphs[i].font_rid.get_id()); - hash = hash_murmur3_one_32(glyphs[i].index, hash); - - _generate_glyph_mesh_data(hash, glyphs[i]); - GlyphMeshData &gl_data = cache[hash]; + GlyphMeshKey key = GlyphMeshKey(glyphs[i].font_rid.get_id(), glyphs[i].index); + _generate_glyph_mesh_data(key, glyphs[i]); + GlyphMeshData &gl_data = cache[key]; p_size += glyphs[i].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1); i_size += glyphs[i].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1); @@ -2496,10 +2494,9 @@ void TextMesh::_create_mesh_array(Array &p_arr) const { continue; } if (glyphs[i].font_rid != RID()) { - uint32_t hash = hash_one_uint64(glyphs[i].font_rid.get_id()); - hash = hash_murmur3_one_32(glyphs[i].index, hash); - - const GlyphMeshData &gl_data = cache[hash]; + GlyphMeshKey key = GlyphMeshKey(glyphs[i].font_rid.get_id(), glyphs[i].index); + _generate_glyph_mesh_data(key, glyphs[i]); + const GlyphMeshData &gl_data = cache[key]; int64_t ts = gl_data.triangles.size(); const Vector2 *ts_ptr = gl_data.triangles.ptr(); diff --git a/scene/resources/primitive_meshes.h b/scene/resources/primitive_meshes.h index 38cc7db5fe..a6d8978c2d 100644 --- a/scene/resources/primitive_meshes.h +++ b/scene/resources/primitive_meshes.h @@ -475,6 +475,7 @@ private: sharp = p_sharp; }; }; + struct ContourInfo { real_t length = 0.0; bool ccw = true; @@ -484,6 +485,27 @@ private: ccw = p_ccw; } }; + + struct GlyphMeshKey { + uint64_t font_id; + uint32_t gl_id; + + bool operator==(const GlyphMeshKey &p_b) const { + return (font_id == p_b.font_id) && (gl_id == p_b.gl_id); + } + + GlyphMeshKey(uint64_t p_font_id, uint32_t p_gl_id) { + font_id = p_font_id; + gl_id = p_gl_id; + } + }; + + struct GlyphMeshKeyHasher { + _FORCE_INLINE_ static uint32_t hash(const GlyphMeshKey &p_a) { + return hash_murmur3_buffer(&p_a, sizeof(GlyphMeshKey)); + } + }; + struct GlyphMeshData { Vector<Vector2> triangles; Vector<Vector<ContourPoint>> contours; @@ -491,7 +513,7 @@ private: Vector2 min_p = Vector2(INFINITY, INFINITY); Vector2 max_p = Vector2(-INFINITY, -INFINITY); }; - mutable HashMap<uint32_t, GlyphMeshData> cache; + mutable HashMap<GlyphMeshKey, GlyphMeshData, GlyphMeshKeyHasher> cache; RID text_rid; String text; @@ -517,7 +539,7 @@ private: mutable bool dirty_font = true; mutable bool dirty_cache = true; - void _generate_glyph_mesh_data(uint32_t p_hash, const Glyph &p_glyph) const; + void _generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph &p_glyph) const; void _font_changed(); protected: |