diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-01-30 10:03:49 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-01-30 10:03:49 +0100 |
commit | 38f8411ed82e72380c9ca77bf242e41277e026bb (patch) | |
tree | 8bc9155614f4e01e6bf6b04fd6014e80e0fc5306 /scene/resources | |
parent | 62b0bfd343fec874765c2d3801cb05bf97ecada3 (diff) | |
parent | 1c774a608314f4915791c9d9e4ab5d9c5485813f (diff) |
Merge pull request #71964 from bruvzg/mat_key
Fix unsafe murmur3 hash use for the default material keys, expose alpha hash transparency mode for default materials and Label3D and Sprite3D.
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/material.cpp | 60 | ||||
-rw-r--r-- | scene/resources/material.h | 2 |
2 files changed, 20 insertions, 42 deletions
diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index f4aa0637f7..d6393966b1 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -2316,52 +2316,30 @@ BaseMaterial3D::TextureChannel BaseMaterial3D::get_refraction_texture_channel() return refraction_texture_channel; } -Ref<Material> BaseMaterial3D::get_material_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass, bool p_billboard, bool p_billboard_y, bool p_msdf, bool p_no_depth, bool p_fixed_size, TextureFilter p_filter, RID *r_shader_rid) { - int64_t hash = 0; - if (p_shaded) { - hash |= 1 << 0; - } - if (p_transparent) { - hash |= 1 << 1; - } - if (p_cut_alpha) { - hash |= 1 << 2; - } - if (p_opaque_prepass) { - hash |= 1 << 3; - } - if (p_double_sided) { - hash |= 1 << 4; - } - if (p_billboard) { - hash |= 1 << 5; - } - if (p_billboard_y) { - hash |= 1 << 6; - } - if (p_msdf) { - hash |= 1 << 7; - } - if (p_no_depth) { - hash |= 1 << 8; - } - if (p_fixed_size) { - hash |= 1 << 9; - } - hash = hash_murmur3_one_64(p_filter, hash); - - if (materials_for_2d.has(hash)) { +Ref<Material> BaseMaterial3D::get_material_for_2d(bool p_shaded, Transparency p_transparency, bool p_double_sided, bool p_billboard, bool p_billboard_y, bool p_msdf, bool p_no_depth, bool p_fixed_size, TextureFilter p_filter, RID *r_shader_rid) { + uint64_t key = 0; + key |= ((int8_t)p_shaded & 0x01) << 0; + key |= ((int8_t)p_transparency & 0x07) << 1; // Bits 1-3. + key |= ((int8_t)p_double_sided & 0x01) << 4; + key |= ((int8_t)p_billboard & 0x01) << 5; + key |= ((int8_t)p_billboard_y & 0x01) << 6; + key |= ((int8_t)p_msdf & 0x01) << 7; + key |= ((int8_t)p_no_depth & 0x01) << 8; + key |= ((int8_t)p_fixed_size & 0x01) << 9; + key |= ((int8_t)p_filter & 0x07) << 10; // Bits 10-13. + + if (materials_for_2d.has(key)) { if (r_shader_rid) { - *r_shader_rid = materials_for_2d[hash]->get_shader_rid(); + *r_shader_rid = materials_for_2d[key]->get_shader_rid(); } - return materials_for_2d[hash]; + return materials_for_2d[key]; } Ref<StandardMaterial3D> material; material.instantiate(); material->set_shading_mode(p_shaded ? SHADING_MODE_PER_PIXEL : SHADING_MODE_UNSHADED); - material->set_transparency(p_transparent ? (p_opaque_prepass ? TRANSPARENCY_ALPHA_DEPTH_PRE_PASS : (p_cut_alpha ? TRANSPARENCY_ALPHA_SCISSOR : TRANSPARENCY_ALPHA)) : TRANSPARENCY_DISABLED); + material->set_transparency(p_transparency); material->set_cull_mode(p_double_sided ? CULL_DISABLED : CULL_BACK); material->set_flag(FLAG_SRGB_VERTEX_COLOR, true); material->set_flag(FLAG_ALBEDO_FROM_VERTEX_COLOR, true); @@ -2374,13 +2352,13 @@ Ref<Material> BaseMaterial3D::get_material_for_2d(bool p_shaded, bool p_transpar material->set_billboard_mode(p_billboard_y ? BILLBOARD_FIXED_Y : BILLBOARD_ENABLED); } - materials_for_2d[hash] = material; + materials_for_2d[key] = material; if (r_shader_rid) { - *r_shader_rid = materials_for_2d[hash]->get_shader_rid(); + *r_shader_rid = materials_for_2d[key]->get_shader_rid(); } - return materials_for_2d[hash]; + return materials_for_2d[key]; } void BaseMaterial3D::set_on_top_of_alpha() { diff --git a/scene/resources/material.h b/scene/resources/material.h index fe1448cd4c..23f3a8824d 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -760,7 +760,7 @@ public: static void finish_shaders(); static void flush_changes(); - static Ref<Material> get_material_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass, bool p_billboard = false, bool p_billboard_y = false, bool p_msdf = false, bool p_no_depth = false, bool p_fixed_size = false, TextureFilter p_filter = TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RID *r_shader_rid = nullptr); + static Ref<Material> get_material_for_2d(bool p_shaded, Transparency p_transparency, bool p_double_sided, bool p_billboard = false, bool p_billboard_y = false, bool p_msdf = false, bool p_no_depth = false, bool p_fixed_size = false, TextureFilter p_filter = TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RID *r_shader_rid = nullptr); virtual RID get_shader_rid() const override; |