compositor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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::_call_render_callback(int p_effect_callback_type, const RenderData *p_render_data) {
  75. GDVIRTUAL_CALL(_render_callback, p_effect_callback_type, p_render_data);
  76. }
  77. void CompositorEffect::set_enabled(bool p_enabled) {
  78. enabled = p_enabled;
  79. if (rid.is_valid()) {
  80. RenderingServer *rs = RenderingServer::get_singleton();
  81. ERR_FAIL_NULL(rs);
  82. rs->compositor_effect_set_enabled(rid, enabled);
  83. }
  84. }
  85. bool CompositorEffect::get_enabled() const {
  86. return enabled;
  87. }
  88. void CompositorEffect::set_effect_callback_type(EffectCallbackType p_callback_type) {
  89. effect_callback_type = p_callback_type;
  90. notify_property_list_changed();
  91. if (rid.is_valid()) {
  92. RenderingServer *rs = RenderingServer::get_singleton();
  93. ERR_FAIL_NULL(rs);
  94. rs->compositor_effect_set_callback(rid, RenderingServer::CompositorEffectCallbackType(effect_callback_type), callable_mp(this, &CompositorEffect::_call_render_callback));
  95. }
  96. }
  97. CompositorEffect::EffectCallbackType CompositorEffect::get_effect_callback_type() const {
  98. return effect_callback_type;
  99. }
  100. void CompositorEffect::set_access_resolved_color(bool p_enabled) {
  101. access_resolved_color = p_enabled;
  102. if (rid.is_valid()) {
  103. RenderingServer *rs = RenderingServer::get_singleton();
  104. ERR_FAIL_NULL(rs);
  105. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_COLOR, access_resolved_color);
  106. }
  107. }
  108. bool CompositorEffect::get_access_resolved_color() const {
  109. return access_resolved_color;
  110. }
  111. void CompositorEffect::set_access_resolved_depth(bool p_enabled) {
  112. access_resolved_depth = p_enabled;
  113. if (rid.is_valid()) {
  114. RenderingServer *rs = RenderingServer::get_singleton();
  115. ERR_FAIL_NULL(rs);
  116. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, access_resolved_depth);
  117. }
  118. }
  119. bool CompositorEffect::get_access_resolved_depth() const {
  120. return access_resolved_depth;
  121. }
  122. void CompositorEffect::set_needs_motion_vectors(bool p_enabled) {
  123. needs_motion_vectors = p_enabled;
  124. if (rid.is_valid()) {
  125. RenderingServer *rs = RenderingServer::get_singleton();
  126. ERR_FAIL_NULL(rs);
  127. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS, needs_motion_vectors);
  128. }
  129. }
  130. bool CompositorEffect::get_needs_motion_vectors() const {
  131. return needs_motion_vectors;
  132. }
  133. void CompositorEffect::set_needs_normal_roughness(bool p_enabled) {
  134. needs_normal_roughness = p_enabled;
  135. if (rid.is_valid()) {
  136. RenderingServer *rs = RenderingServer::get_singleton();
  137. ERR_FAIL_NULL(rs);
  138. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_ROUGHNESS, needs_normal_roughness);
  139. }
  140. }
  141. bool CompositorEffect::get_needs_normal_roughness() const {
  142. return needs_normal_roughness;
  143. }
  144. void CompositorEffect::set_needs_separate_specular(bool p_enabled) {
  145. needs_separate_specular = p_enabled;
  146. if (rid.is_valid()) {
  147. RenderingServer *rs = RenderingServer::get_singleton();
  148. ERR_FAIL_NULL(rs);
  149. rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_SEPARATE_SPECULAR, needs_separate_specular);
  150. }
  151. }
  152. bool CompositorEffect::get_needs_separate_specular() const {
  153. return needs_separate_specular;
  154. }
  155. CompositorEffect::CompositorEffect() {
  156. RenderingServer *rs = RenderingServer::get_singleton();
  157. if (rs != nullptr) {
  158. rid = rs->compositor_effect_create();
  159. rs->compositor_effect_set_callback(rid, RenderingServer::CompositorEffectCallbackType(effect_callback_type), Callable(this, "_render_callback"));
  160. }
  161. }
  162. CompositorEffect::~CompositorEffect() {
  163. RenderingServer *rs = RenderingServer::get_singleton();
  164. if (rs != nullptr && rid.is_valid()) {
  165. rs->free(rid);
  166. }
  167. }
  168. /* Compositor */
  169. void Compositor::_bind_methods() {
  170. // compositor effects
  171. ClassDB::bind_method(D_METHOD("set_compositor_effects", "compositor_effects"), &Compositor::set_compositor_effects);
  172. ClassDB::bind_method(D_METHOD("get_compositor_effects"), &Compositor::get_compositor_effects);
  173. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "compositor_effects", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("CompositorEffect")), "set_compositor_effects", "get_compositor_effects");
  174. }
  175. Compositor::Compositor() {
  176. RenderingServer *rs = RenderingServer::get_singleton();
  177. if (rs != nullptr) {
  178. compositor = rs->compositor_create();
  179. }
  180. }
  181. Compositor::~Compositor() {
  182. RenderingServer *rs = RenderingServer::get_singleton();
  183. if (rs != nullptr && compositor.is_valid()) {
  184. rs->free(compositor);
  185. }
  186. }
  187. // Compositor effects
  188. void Compositor::set_compositor_effects(const TypedArray<CompositorEffect> &p_compositor_effects) {
  189. Array effect_rids;
  190. effects.clear();
  191. for (int i = 0; i < p_compositor_effects.size(); i++) {
  192. // Cast to proper ref, if our object isn't a CompositorEffect resource this will be an empty Ref.
  193. Ref<CompositorEffect> compositor_effect = p_compositor_effects[i];
  194. // We add the effect even if this is an empty Ref, this allows the UI to add new entries.
  195. effects.push_back(compositor_effect);
  196. // But we only add a rid for valid Refs
  197. if (compositor_effect.is_valid()) {
  198. RID rid = compositor_effect->get_rid();
  199. effect_rids.push_back(rid);
  200. }
  201. }
  202. RenderingServer::get_singleton()->compositor_set_compositor_effects(compositor, effect_rids);
  203. }
  204. TypedArray<CompositorEffect> Compositor::get_compositor_effects() const {
  205. TypedArray<CompositorEffect> arr;
  206. for (uint32_t i = 0; i < effects.size(); i++) {
  207. arr.push_back(effects[i]);
  208. }
  209. return arr;
  210. }