canvas_item_material.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**************************************************************************/
  2. /* canvas_item_material.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef CANVAS_ITEM_MATERIAL_H
  31. #define CANVAS_ITEM_MATERIAL_H
  32. #include "scene/resources/material.h"
  33. class CanvasItemMaterial : public Material {
  34. GDCLASS(CanvasItemMaterial, Material);
  35. public:
  36. enum BlendMode {
  37. BLEND_MODE_MIX,
  38. BLEND_MODE_ADD,
  39. BLEND_MODE_SUB,
  40. BLEND_MODE_MUL,
  41. BLEND_MODE_PREMULT_ALPHA,
  42. BLEND_MODE_DISABLED
  43. };
  44. enum LightMode {
  45. LIGHT_MODE_NORMAL,
  46. LIGHT_MODE_UNSHADED,
  47. LIGHT_MODE_LIGHT_ONLY
  48. };
  49. private:
  50. union MaterialKey {
  51. struct {
  52. uint32_t blend_mode : 4;
  53. uint32_t light_mode : 4;
  54. uint32_t particles_animation : 1;
  55. uint32_t invalid_key : 1;
  56. };
  57. uint32_t key = 0;
  58. static uint32_t hash(const MaterialKey &p_key) {
  59. return hash_murmur3_one_32(p_key.key);
  60. }
  61. bool operator==(const MaterialKey &p_key) const {
  62. return key == p_key.key;
  63. }
  64. };
  65. struct ShaderNames {
  66. StringName particles_anim_h_frames;
  67. StringName particles_anim_v_frames;
  68. StringName particles_anim_loop;
  69. };
  70. static ShaderNames *shader_names;
  71. struct ShaderData {
  72. RID shader;
  73. int users = 0;
  74. };
  75. static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map;
  76. MaterialKey current_key;
  77. _FORCE_INLINE_ MaterialKey _compute_key() const {
  78. MaterialKey mk;
  79. mk.key = 0;
  80. mk.blend_mode = blend_mode;
  81. mk.light_mode = light_mode;
  82. mk.particles_animation = particles_animation;
  83. return mk;
  84. }
  85. static Mutex material_mutex;
  86. static SelfList<CanvasItemMaterial>::List dirty_materials;
  87. SelfList<CanvasItemMaterial> element;
  88. void _update_shader();
  89. _FORCE_INLINE_ void _queue_shader_change();
  90. BlendMode blend_mode = BLEND_MODE_MIX;
  91. LightMode light_mode = LIGHT_MODE_NORMAL;
  92. bool particles_animation = false;
  93. // Proper values set in constructor.
  94. int particles_anim_h_frames = 0;
  95. int particles_anim_v_frames = 0;
  96. bool particles_anim_loop = false;
  97. protected:
  98. static void _bind_methods();
  99. void _validate_property(PropertyInfo &p_property) const;
  100. public:
  101. void set_blend_mode(BlendMode p_blend_mode);
  102. BlendMode get_blend_mode() const;
  103. void set_light_mode(LightMode p_light_mode);
  104. LightMode get_light_mode() const;
  105. void set_particles_animation(bool p_particles_anim);
  106. bool get_particles_animation() const;
  107. void set_particles_anim_h_frames(int p_frames);
  108. int get_particles_anim_h_frames() const;
  109. void set_particles_anim_v_frames(int p_frames);
  110. int get_particles_anim_v_frames() const;
  111. void set_particles_anim_loop(bool p_loop);
  112. bool get_particles_anim_loop() const;
  113. static void init_shaders();
  114. static void finish_shaders();
  115. static void flush_changes();
  116. virtual RID get_shader_rid() const override;
  117. virtual Shader::Mode get_shader_mode() const override;
  118. CanvasItemMaterial();
  119. virtual ~CanvasItemMaterial();
  120. };
  121. VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode)
  122. VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode)
  123. #endif // CANVAS_ITEM_MATERIAL_H