visual_server_viewport.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**************************************************************************/
  2. /* visual_server_viewport.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 VISUAL_SERVER_VIEWPORT_H
  31. #define VISUAL_SERVER_VIEWPORT_H
  32. #include "core/self_list.h"
  33. #include "rasterizer.h"
  34. #include "servers/arvr/arvr_interface.h"
  35. #include "servers/visual_server.h"
  36. class VisualServerViewport {
  37. public:
  38. struct CanvasBase : public RID_Data {
  39. };
  40. struct Viewport : public RID_Data {
  41. RID self;
  42. RID parent;
  43. bool use_arvr; /* use arvr interface to override camera positioning and projection matrices and control output */
  44. Size2i size;
  45. RID camera;
  46. RID scenario;
  47. VS::ViewportUpdateMode update_mode;
  48. RID render_target;
  49. RID render_target_texture;
  50. int viewport_to_screen;
  51. Rect2 viewport_to_screen_rect;
  52. bool viewport_render_direct_to_screen;
  53. bool hide_scenario;
  54. bool hide_canvas;
  55. bool disable_environment;
  56. bool disable_3d;
  57. bool disable_3d_by_usage;
  58. bool keep_3d_linear;
  59. RID shadow_atlas;
  60. int shadow_atlas_size;
  61. int render_info[VS::VIEWPORT_RENDER_INFO_MAX];
  62. VS::ViewportDebugDraw debug_draw;
  63. VS::ViewportClearMode clear_mode;
  64. bool transparent_bg;
  65. struct CanvasKey {
  66. int64_t stacking;
  67. RID canvas;
  68. bool operator<(const CanvasKey &p_canvas) const {
  69. if (stacking == p_canvas.stacking) {
  70. return canvas < p_canvas.canvas;
  71. }
  72. return stacking < p_canvas.stacking;
  73. }
  74. CanvasKey() {
  75. stacking = 0;
  76. }
  77. CanvasKey(const RID &p_canvas, int p_layer, int p_sublayer) {
  78. canvas = p_canvas;
  79. int64_t sign = p_layer < 0 ? -1 : 1;
  80. stacking = sign * (((int64_t)ABS(p_layer)) << 32) + p_sublayer;
  81. }
  82. int get_layer() const { return stacking >> 32; }
  83. };
  84. struct CanvasData {
  85. CanvasBase *canvas;
  86. Transform2D transform;
  87. int layer;
  88. int sublayer;
  89. };
  90. Transform2D global_transform;
  91. Map<RID, CanvasData> canvas_map;
  92. Viewport() {
  93. update_mode = VS::VIEWPORT_UPDATE_WHEN_VISIBLE;
  94. clear_mode = VS::VIEWPORT_CLEAR_ALWAYS;
  95. transparent_bg = false;
  96. disable_environment = false;
  97. viewport_to_screen = 0;
  98. shadow_atlas_size = 0;
  99. disable_3d = false;
  100. disable_3d_by_usage = false;
  101. keep_3d_linear = false;
  102. debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED;
  103. for (int i = 0; i < VS::VIEWPORT_RENDER_INFO_MAX; i++) {
  104. render_info[i] = 0;
  105. }
  106. use_arvr = false;
  107. }
  108. };
  109. mutable RID_Owner<Viewport> viewport_owner;
  110. struct ViewportSort {
  111. _FORCE_INLINE_ bool operator()(const Viewport *p_left, const Viewport *p_right) const {
  112. bool left_to_screen = p_left->viewport_to_screen_rect.size != Size2();
  113. bool right_to_screen = p_right->viewport_to_screen_rect.size != Size2();
  114. if (left_to_screen == right_to_screen) {
  115. return p_left->parent == p_right->self;
  116. }
  117. return right_to_screen;
  118. }
  119. };
  120. Vector<Viewport *> active_viewports;
  121. private:
  122. Color clear_color;
  123. void _draw_3d(Viewport *p_viewport, ARVRInterface::Eyes p_eye);
  124. void _draw_viewport(Viewport *p_viewport, ARVRInterface::Eyes p_eye = ARVRInterface::EYE_MONO);
  125. public:
  126. RID viewport_create();
  127. void viewport_set_use_arvr(RID p_viewport, bool p_use_arvr);
  128. void viewport_set_size(RID p_viewport, int p_width, int p_height);
  129. void viewport_attach_to_screen(RID p_viewport, const Rect2 &p_rect = Rect2(), int p_screen = 0);
  130. void viewport_set_render_direct_to_screen(RID p_viewport, bool p_enable);
  131. void viewport_detach(RID p_viewport);
  132. void viewport_set_active(RID p_viewport, bool p_active);
  133. void viewport_set_parent_viewport(RID p_viewport, RID p_parent_viewport);
  134. void viewport_set_update_mode(RID p_viewport, VS::ViewportUpdateMode p_mode);
  135. void viewport_set_vflip(RID p_viewport, bool p_enable);
  136. void viewport_set_clear_mode(RID p_viewport, VS::ViewportClearMode p_clear_mode);
  137. RID viewport_get_texture(RID p_viewport) const;
  138. void viewport_set_hide_scenario(RID p_viewport, bool p_hide);
  139. void viewport_set_hide_canvas(RID p_viewport, bool p_hide);
  140. void viewport_set_disable_environment(RID p_viewport, bool p_disable);
  141. void viewport_set_disable_3d(RID p_viewport, bool p_disable);
  142. void viewport_set_keep_3d_linear(RID p_viewport, bool p_keep_3d_linear);
  143. void viewport_attach_camera(RID p_viewport, RID p_camera);
  144. void viewport_set_scenario(RID p_viewport, RID p_scenario);
  145. void viewport_attach_canvas(RID p_viewport, RID p_canvas);
  146. void viewport_remove_canvas(RID p_viewport, RID p_canvas);
  147. void viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset);
  148. void viewport_set_transparent_background(RID p_viewport, bool p_enabled);
  149. void viewport_set_global_canvas_transform(RID p_viewport, const Transform2D &p_transform);
  150. void viewport_set_canvas_stacking(RID p_viewport, RID p_canvas, int p_layer, int p_sublayer);
  151. void viewport_set_shadow_atlas_size(RID p_viewport, int p_size);
  152. void viewport_set_shadow_atlas_quadrant_subdivision(RID p_viewport, int p_quadrant, int p_subdiv);
  153. void viewport_set_msaa(RID p_viewport, VS::ViewportMSAA p_msaa);
  154. void viewport_set_use_fxaa(RID p_viewport, bool p_fxaa);
  155. void viewport_set_use_debanding(RID p_viewport, bool p_debanding);
  156. void viewport_set_sharpen_intensity(RID p_viewport, float p_intensity);
  157. void viewport_set_hdr(RID p_viewport, bool p_enabled);
  158. void viewport_set_use_32_bpc_depth(RID p_viewport, bool p_enabled);
  159. void viewport_set_usage(RID p_viewport, VS::ViewportUsage p_usage);
  160. virtual int viewport_get_render_info(RID p_viewport, VS::ViewportRenderInfo p_info);
  161. virtual void viewport_set_debug_draw(RID p_viewport, VS::ViewportDebugDraw p_draw);
  162. void set_default_clear_color(const Color &p_color);
  163. void draw_viewports();
  164. bool free(RID p_rid);
  165. VisualServerViewport();
  166. virtual ~VisualServerViewport() {}
  167. };
  168. #endif // VISUAL_SERVER_VIEWPORT_H