compositor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* compositor.cpp */
  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. #include "compositor.h"
  31. #include "servers/rendering_server.h"
  32. /* Compositor Effect */
  33. void CompositorEffect::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &CompositorEffect::set_enabled);
  35. ClassDB::bind_method(D_METHOD("get_enabled"), &CompositorEffect::get_enabled);
  36. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
  37. ClassDB::bind_method(D_METHOD("set_effect_callback_type", "effect_callback_type"), &CompositorEffect::set_effect_callback_type);
  38. ClassDB::bind_method(D_METHOD("get_effect_callback_type"), &CompositorEffect::get_effect_callback_type);
  39. ADD_PROPERTY(PropertyInfo(Variant::INT, "effect_callback_type", PROPERTY_HINT_ENUM, "Pre Opaque,Post Opaque,Post Sky,Pre Transparent,Post Transparent"), "set_effect_callback_type", "get_effect_callback_type");
  40. ClassDB::bind_method(D_METHOD("set_access_resolved_color", "enable"), &CompositorEffect::set_access_resolved_color);
  41. ClassDB::bind_method(D_METHOD("get_access_resolved_color"), &CompositorEffect::get_access_resolved_color);
  42. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "access_resolved_color"), "set_access_resolved_color", "get_access_resolved_color");
  43. ClassDB::bind_method(D_METHOD("set_access_resolved_depth", "enable"), &CompositorEffect::set_access_resolved_depth);
  44. ClassDB::bind_method(D_METHOD("get_access_resolved_depth"), &CompositorEffect::get_access_resolved_depth);
  45. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "access_resolved_depth"), "set_access_resolved_depth", "get_access_resolved_depth");
  46. ClassDB::bind_method(D_METHOD("set_needs_motion_vectors", "enable"), &CompositorEffect::set_needs_motion_vectors);
  47. ClassDB::bind_method(D_METHOD("get_needs_motion_vectors"), &CompositorEffect::get_needs_motion_vectors);
  48. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "needs_motion_vectors"), "set_needs_motion_vectors", "get_needs_motion_vectors");
  49. ClassDB::bind_method(D_METHOD("set_needs_normal_roughness", "enable"), &CompositorEffect::set_needs_normal_roughness);
  50. ClassDB::bind_method(D_METHOD("get_needs_normal_roughness"), &CompositorEffect::get_needs_normal_roughness);
  51. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "needs_normal_roughness"), "set_needs_normal_roughness", "get_needs_normal_roughness");
  52. ClassDB::bind_method(D_METHOD("set_needs_separate_specular", "enable"), &CompositorEffect::set_needs_separate_specular);
  53. ClassDB::bind_method(D_METHOD("get_needs_separate_specular"), &CompositorEffect::get_needs_separate_specular);
  54. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "needs_separate_specular"), "set_needs_separate_specular", "get_needs_separate_specular");
  55. BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_PRE_OPAQUE)
  56. BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_POST_OPAQUE)
  57. BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_POST_SKY)
  58. BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT)
  59. BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_POST_TRANSPARENT)
  60. BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_MAX)
  61. GDVIRTUAL_BIND(_render_callback, "effect_callback_type", "render_data");
  62. }
  63. void CompositorEffect::_validate_property(PropertyInfo &p_property) const {
  64. if (p_property.name == "access_resolved_color" && effect_callback_type == EFFECT_CALLBACK_TYPE_POST_TRANSPARENT) {
  65. p_property.usage = PROPERTY_USAGE_NONE;
  66. }
  67. if (p_property.name == "access_resolved_depth" && effect_callback_type == EFFECT_CALLBACK_TYPE_POST_TRANSPARENT) {
  68. p_property.usage = PROPERTY_USAGE_NONE;
  69. }
  70. if (p_property.name == "needs_separate_specular" && effect_callback_type != EFFECT_CALLBACK_TYPE_POST_SKY) {
  71. p_property.usage = PROPERTY_USAGE_NONE;
  72. }
  73. }
  74. void CompositorEffect::set_enabled(bool p_enabled) {
  75. enabled = p_enabled;
  76. if (rid.is_valid()) {
  77. RenderingServer *rs = RenderingServer::get_singleton();
  78. ERR_FAIL_NULL(rs);
  79. rs->compositor_effect_set_enabled(rid, enabled);
  80. }
  81. }
  82. bool CompositorEffect::get_enabled() const {
  83. return enabled;
  84. }
  85. void CompositorEffect::set_effect_callback_type(EffectCallbackType p_callback_type) {
  86. effect_callback_type = p_callback_type;
  87. notify_property_list_changed();
  88. if (rid.is_valid()) {
  89. RenderingServer *rs = RenderingServer::get_singleton();
  90. ERR_FAIL_NULL(rs);
  91. rs->compositor_effect_set_callback(rid, RenderingServer::CompositorEffectCallbackType(effect_callback_type), Callable(this, "_render_callback"));
  92. }
  93. }
  94. CompositorEffect::EffectCallbackType CompositorEffect::get_effect_callback_type() const {
  95. return effect_callback_type;
  96. }
  97. void CompositorEffect::set_access_resolved_color(bool p_enabled) {
  98. access_resolved_color = p_enabled;
  99. if (rid.is_valid()) {
  100. RenderingServer *rs = RenderingServer::get_singleton();
  101. ERR_FAIL_NULL(rs);
  102. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_COLOR, access_resolved_color);
  103. }
  104. }
  105. bool CompositorEffect::get_access_resolved_color() const {
  106. return access_resolved_color;
  107. }
  108. void CompositorEffect::set_access_resolved_depth(bool p_enabled) {
  109. access_resolved_depth = p_enabled;
  110. if (rid.is_valid()) {
  111. RenderingServer *rs = RenderingServer::get_singleton();
  112. ERR_FAIL_NULL(rs);
  113. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, access_resolved_depth);
  114. }
  115. }
  116. bool CompositorEffect::get_access_resolved_depth() const {
  117. return access_resolved_depth;
  118. }
  119. void CompositorEffect::set_needs_motion_vectors(bool p_enabled) {
  120. needs_motion_vectors = p_enabled;
  121. if (rid.is_valid()) {
  122. RenderingServer *rs = RenderingServer::get_singleton();
  123. ERR_FAIL_NULL(rs);
  124. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS, needs_motion_vectors);
  125. }
  126. }
  127. bool CompositorEffect::get_needs_motion_vectors() const {
  128. return needs_motion_vectors;
  129. }
  130. void CompositorEffect::set_needs_normal_roughness(bool p_enabled) {
  131. needs_normal_roughness = p_enabled;
  132. if (rid.is_valid()) {
  133. RenderingServer *rs = RenderingServer::get_singleton();
  134. ERR_FAIL_NULL(rs);
  135. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_ROUGHNESS, needs_normal_roughness);
  136. }
  137. }
  138. bool CompositorEffect::get_needs_normal_roughness() const {
  139. return needs_normal_roughness;
  140. }
  141. void CompositorEffect::set_needs_separate_specular(bool p_enabled) {
  142. needs_separate_specular = p_enabled;
  143. if (rid.is_valid()) {
  144. RenderingServer *rs = RenderingServer::get_singleton();
  145. ERR_FAIL_NULL(rs);
  146. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_SEPARATE_SPECULAR, needs_separate_specular);
  147. }
  148. }
  149. bool CompositorEffect::get_needs_separate_specular() const {
  150. return needs_separate_specular;
  151. }
  152. CompositorEffect::CompositorEffect() {
  153. RenderingServer *rs = RenderingServer::get_singleton();
  154. if (rs != nullptr) {
  155. rid = rs->compositor_effect_create();
  156. rs->compositor_effect_set_callback(rid, RenderingServer::CompositorEffectCallbackType(effect_callback_type), Callable(this, "_render_callback"));
  157. }
  158. }
  159. CompositorEffect::~CompositorEffect() {
  160. RenderingServer *rs = RenderingServer::get_singleton();
  161. if (rs != nullptr && rid.is_valid()) {
  162. rs->free(rid);
  163. }
  164. }
  165. /* Compositor */
  166. void Compositor::_bind_methods() {
  167. // compositor effects
  168. ClassDB::bind_method(D_METHOD("set_compositor_effects", "compositor_effects"), &Compositor::set_compositor_effects);
  169. ClassDB::bind_method(D_METHOD("get_compositor_effects"), &Compositor::get_compositor_effects);
  170. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "compositor_effects", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("CompositorEffect")), "set_compositor_effects", "get_compositor_effects");
  171. }
  172. Compositor::Compositor() {
  173. RenderingServer *rs = RenderingServer::get_singleton();
  174. if (rs != nullptr) {
  175. compositor = rs->compositor_create();
  176. }
  177. }
  178. Compositor::~Compositor() {
  179. RenderingServer *rs = RenderingServer::get_singleton();
  180. if (rs != nullptr && compositor.is_valid()) {
  181. rs->free(compositor);
  182. }
  183. }
  184. // Compositor effects
  185. void Compositor::set_compositor_effects(const TypedArray<CompositorEffect> &p_compositor_effects) {
  186. Array effect_rids;
  187. effects.clear();
  188. for (int i = 0; i < p_compositor_effects.size(); i++) {
  189. // Cast to proper ref, if our object isn't a CompositorEffect resource this will be an empty Ref.
  190. Ref<CompositorEffect> compositor_effect = p_compositor_effects[i];
  191. // We add the effect even if this is an empty Ref, this allows the UI to add new entries.
  192. effects.push_back(compositor_effect);
  193. // But we only add a rid for valid Refs
  194. if (compositor_effect.is_valid()) {
  195. RID rid = compositor_effect->get_rid();
  196. effect_rids.push_back(rid);
  197. }
  198. }
  199. RenderingServer::get_singleton()->compositor_set_compositor_effects(compositor, effect_rids);
  200. }
  201. TypedArray<CompositorEffect> Compositor::get_compositor_effects() const {
  202. TypedArray<CompositorEffect> arr;
  203. for (uint32_t i = 0; i < effects.size(); i++) {
  204. arr.push_back(effects[i]);
  205. }
  206. return arr;
  207. }