visual_server_viewport.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /**************************************************************************/
  2. /* visual_server_viewport.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 "visual_server_viewport.h"
  31. #include "core/project_settings.h"
  32. #include "visual_server_canvas.h"
  33. #include "visual_server_globals.h"
  34. #include "visual_server_scene.h"
  35. static Transform2D _canvas_get_transform(VisualServerViewport::Viewport *p_viewport, VisualServerCanvas::Canvas *p_canvas, VisualServerViewport::Viewport::CanvasData *p_canvas_data, const Vector2 &p_vp_size) {
  36. Transform2D xf = p_viewport->global_transform;
  37. float scale = 1.0;
  38. if (p_viewport->canvas_map.has(p_canvas->parent)) {
  39. Transform2D c_xform = p_viewport->canvas_map[p_canvas->parent].transform;
  40. xf = xf * c_xform;
  41. scale = p_canvas->parent_scale;
  42. }
  43. Transform2D c_xform = p_canvas_data->transform;
  44. xf = xf * c_xform;
  45. if (scale != 1.0 && !VSG::canvas->disable_scale) {
  46. Vector2 pivot = p_vp_size * 0.5;
  47. Transform2D xfpivot;
  48. xfpivot.set_origin(pivot);
  49. Transform2D xfscale;
  50. xfscale.scale(Vector2(scale, scale));
  51. xf = xfpivot.affine_inverse() * xf;
  52. xf = xfscale * xf;
  53. xf = xfpivot * xf;
  54. }
  55. return xf;
  56. }
  57. void VisualServerViewport::_draw_3d(Viewport *p_viewport, ARVRInterface::Eyes p_eye) {
  58. Ref<ARVRInterface> arvr_interface;
  59. if (ARVRServer::get_singleton() != nullptr) {
  60. arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  61. }
  62. if (p_viewport->use_arvr && arvr_interface.is_valid()) {
  63. VSG::scene->render_camera(arvr_interface, p_eye, p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  64. } else {
  65. VSG::scene->render_camera(p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  66. }
  67. }
  68. void VisualServerViewport::_draw_viewport(Viewport *p_viewport, ARVRInterface::Eyes p_eye) {
  69. /* Camera should always be BEFORE any other 3D */
  70. bool scenario_draw_canvas_bg = false; //draw canvas, or some layer of it, as BG for 3D instead of in front
  71. int scenario_canvas_max_layer = 0;
  72. if (!p_viewport->hide_canvas && !p_viewport->disable_environment && VSG::scene->scenario_owner.owns(p_viewport->scenario)) {
  73. VisualServerScene::Scenario *scenario = VSG::scene->scenario_owner.get(p_viewport->scenario);
  74. ERR_FAIL_COND(!scenario);
  75. if (VSG::scene_render->is_environment(scenario->environment)) {
  76. scenario_draw_canvas_bg = VSG::scene_render->environment_get_background(scenario->environment) == VS::ENV_BG_CANVAS;
  77. scenario_canvas_max_layer = VSG::scene_render->environment_get_canvas_max_layer(scenario->environment);
  78. }
  79. }
  80. bool can_draw_3d = !p_viewport->disable_3d && !p_viewport->disable_3d_by_usage && VSG::scene->camera_owner.owns(p_viewport->camera);
  81. if (p_viewport->clear_mode != VS::VIEWPORT_CLEAR_NEVER) {
  82. VSG::rasterizer->clear_render_target(p_viewport->transparent_bg ? Color(0, 0, 0, 0) : clear_color);
  83. if (p_viewport->clear_mode == VS::VIEWPORT_CLEAR_ONLY_NEXT_FRAME) {
  84. p_viewport->clear_mode = VS::VIEWPORT_CLEAR_NEVER;
  85. }
  86. }
  87. if (!scenario_draw_canvas_bg && can_draw_3d) {
  88. _draw_3d(p_viewport, p_eye);
  89. }
  90. if (!p_viewport->hide_canvas) {
  91. Map<Viewport::CanvasKey, Viewport::CanvasData *> canvas_map;
  92. Rect2 clip_rect(0, 0, p_viewport->size.x, p_viewport->size.y);
  93. RasterizerCanvas::Light *lights = nullptr;
  94. RasterizerCanvas::Light *lights_with_shadow = nullptr;
  95. RasterizerCanvas::Light *lights_with_mask = nullptr;
  96. Rect2 shadow_rect;
  97. for (Map<RID, Viewport::CanvasData>::Element *E = p_viewport->canvas_map.front(); E; E = E->next()) {
  98. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas *>(E->get().canvas);
  99. Transform2D xf = _canvas_get_transform(p_viewport, canvas, &E->get(), clip_rect.size);
  100. //find lights in canvas
  101. for (Set<RasterizerCanvas::Light *>::Element *F = canvas->lights.front(); F; F = F->next()) {
  102. RasterizerCanvas::Light *cl = F->get();
  103. if (cl->enabled && cl->texture.is_valid()) {
  104. //not super efficient..
  105. Size2 tsize = VSG::storage->texture_size_with_proxy(cl->texture);
  106. // Skip using lights with texture of 0 size
  107. if (!tsize.x || !tsize.y) {
  108. continue;
  109. }
  110. tsize *= cl->scale;
  111. Vector2 offset = tsize / 2.0;
  112. cl->rect_cache = Rect2(-offset + cl->texture_offset, tsize);
  113. if (!VSG::canvas->_interpolation_data.interpolation_enabled || !cl->interpolated) {
  114. cl->xform_cache = xf * cl->xform_curr;
  115. } else {
  116. real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
  117. TransformInterpolator::interpolate_transform_2d(cl->xform_prev, cl->xform_curr, cl->xform_cache, f);
  118. cl->xform_cache = xf * cl->xform_cache;
  119. }
  120. if (clip_rect.intersects_transformed(cl->xform_cache, cl->rect_cache)) {
  121. cl->filter_next_ptr = lights;
  122. lights = cl;
  123. cl->texture_cache = nullptr;
  124. Transform2D scale;
  125. scale.scale(cl->rect_cache.size);
  126. scale.elements[2] = cl->rect_cache.position;
  127. cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse();
  128. cl->light_shader_pos = cl->xform_cache[2];
  129. if (cl->shadow_buffer.is_valid()) {
  130. cl->shadows_next_ptr = lights_with_shadow;
  131. if (lights_with_shadow == nullptr) {
  132. shadow_rect = cl->xform_cache.xform(cl->rect_cache);
  133. } else {
  134. shadow_rect = shadow_rect.merge(cl->xform_cache.xform(cl->rect_cache));
  135. }
  136. lights_with_shadow = cl;
  137. cl->radius_cache = cl->rect_cache.size.length();
  138. }
  139. if (cl->mode == VS::CANVAS_LIGHT_MODE_MASK) {
  140. cl->mask_next_ptr = lights_with_mask;
  141. lights_with_mask = cl;
  142. }
  143. }
  144. VSG::canvas_render->light_internal_update(cl->light_internal, cl);
  145. }
  146. }
  147. canvas_map[Viewport::CanvasKey(E->key(), E->get().layer, E->get().sublayer)] = &E->get();
  148. }
  149. if (lights_with_shadow) {
  150. //update shadows if any
  151. RasterizerCanvas::LightOccluderInstance *occluders = nullptr;
  152. //make list of occluders
  153. for (Map<RID, Viewport::CanvasData>::Element *E = p_viewport->canvas_map.front(); E; E = E->next()) {
  154. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas *>(E->get().canvas);
  155. Transform2D xf = _canvas_get_transform(p_viewport, canvas, &E->get(), clip_rect.size);
  156. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *F = canvas->occluders.front(); F; F = F->next()) {
  157. RasterizerCanvas::LightOccluderInstance *occluder = F->get();
  158. if (!occluder->enabled) {
  159. continue;
  160. }
  161. if (!VSG::canvas->_interpolation_data.interpolation_enabled || !occluder->interpolated) {
  162. occluder->xform_cache = xf * occluder->xform_curr;
  163. } else {
  164. real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
  165. TransformInterpolator::interpolate_transform_2d(occluder->xform_prev, occluder->xform_curr, occluder->xform_cache, f);
  166. occluder->xform_cache = xf * occluder->xform_cache;
  167. }
  168. if (shadow_rect.intersects_transformed(occluder->xform_cache, occluder->aabb_cache)) {
  169. occluder->next = occluders;
  170. occluders = occluder;
  171. }
  172. }
  173. }
  174. //update the light shadowmaps with them
  175. RasterizerCanvas::Light *light = lights_with_shadow;
  176. while (light) {
  177. VSG::canvas_render->canvas_light_shadow_buffer_update(light->shadow_buffer, light->xform_cache.affine_inverse(), light->item_shadow_mask, light->radius_cache / 1000.0, light->radius_cache * 1.1, occluders, &light->shadow_matrix_cache);
  178. light = light->shadows_next_ptr;
  179. }
  180. //VSG::canvas_render->reset_canvas();
  181. }
  182. VSG::rasterizer->restore_render_target(!scenario_draw_canvas_bg && can_draw_3d);
  183. if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().get_layer() > scenario_canvas_max_layer) {
  184. if (!can_draw_3d) {
  185. VSG::scene->render_empty_scene(p_viewport->scenario, p_viewport->shadow_atlas);
  186. } else {
  187. _draw_3d(p_viewport, p_eye);
  188. }
  189. scenario_draw_canvas_bg = false;
  190. }
  191. for (Map<Viewport::CanvasKey, Viewport::CanvasData *>::Element *E = canvas_map.front(); E; E = E->next()) {
  192. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas *>(E->get()->canvas);
  193. Transform2D xform = _canvas_get_transform(p_viewport, canvas, E->get(), clip_rect.size);
  194. RasterizerCanvas::Light *canvas_lights = nullptr;
  195. RasterizerCanvas::Light *ptr = lights;
  196. int canvas_layer_id = E->get()->layer;
  197. while (ptr) {
  198. if (canvas_layer_id >= ptr->layer_min && canvas_layer_id <= ptr->layer_max) {
  199. ptr->next_ptr = canvas_lights;
  200. canvas_lights = ptr;
  201. }
  202. ptr = ptr->filter_next_ptr;
  203. }
  204. VSG::canvas->render_canvas(canvas, xform, canvas_lights, lights_with_mask, clip_rect, canvas_layer_id);
  205. if (scenario_draw_canvas_bg && E->key().get_layer() >= scenario_canvas_max_layer) {
  206. if (!can_draw_3d) {
  207. VSG::scene->render_empty_scene(p_viewport->scenario, p_viewport->shadow_atlas);
  208. } else {
  209. _draw_3d(p_viewport, p_eye);
  210. }
  211. scenario_draw_canvas_bg = false;
  212. }
  213. }
  214. if (scenario_draw_canvas_bg) {
  215. if (!can_draw_3d) {
  216. VSG::scene->render_empty_scene(p_viewport->scenario, p_viewport->shadow_atlas);
  217. } else {
  218. _draw_3d(p_viewport, p_eye);
  219. }
  220. }
  221. //VSG::canvas_render->canvas_debug_viewport_shadows(lights_with_shadow);
  222. }
  223. }
  224. void VisualServerViewport::draw_viewports() {
  225. // get our arvr interface in case we need it
  226. Ref<ARVRInterface> arvr_interface;
  227. if (ARVRServer::get_singleton() != nullptr) {
  228. arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  229. // process all our active interfaces
  230. ARVRServer::get_singleton()->_process();
  231. }
  232. if (Engine::get_singleton()->is_editor_hint()) {
  233. clear_color = GLOBAL_GET("rendering/environment/default_clear_color");
  234. }
  235. //sort viewports
  236. active_viewports.sort_custom<ViewportSort>();
  237. //draw viewports
  238. for (int i = 0; i < active_viewports.size(); i++) {
  239. Viewport *vp = active_viewports[i];
  240. if (vp->update_mode == VS::VIEWPORT_UPDATE_DISABLED) {
  241. continue;
  242. }
  243. ERR_CONTINUE(!vp->render_target.is_valid());
  244. if (vp->use_arvr) {
  245. // In ARVR mode it is our interface that controls our size
  246. if (arvr_interface.is_valid()) {
  247. // override our size, make sure it matches our required size
  248. vp->size = arvr_interface->get_render_targetsize();
  249. } else {
  250. // reset this, we can't render the output without a valid interface (this will likely be so when we're in the editor)
  251. vp->size = Vector2(0, 0);
  252. }
  253. }
  254. bool visible = vp->viewport_to_screen_rect != Rect2() || vp->update_mode == VS::VIEWPORT_UPDATE_ALWAYS || vp->update_mode == VS::VIEWPORT_UPDATE_ONCE || (vp->update_mode == VS::VIEWPORT_UPDATE_WHEN_VISIBLE && VSG::storage->render_target_was_used(vp->render_target));
  255. visible = visible && vp->size.x > 1 && vp->size.y > 1;
  256. if (!visible) {
  257. continue;
  258. }
  259. VSG::storage->render_target_clear_used(vp->render_target);
  260. if (vp->use_arvr && arvr_interface.is_valid()) {
  261. VSG::storage->render_target_set_size(vp->render_target, vp->size.x, vp->size.y);
  262. // render mono or left eye first
  263. ARVRInterface::Eyes leftOrMono = arvr_interface->is_stereo() ? ARVRInterface::EYE_LEFT : ARVRInterface::EYE_MONO;
  264. // check for an external texture destination for our left eye/mono
  265. VSG::storage->render_target_set_external_texture(vp->render_target, arvr_interface->get_external_texture_for_eye(leftOrMono), arvr_interface->get_external_depth_for_eye(leftOrMono));
  266. // set our render target as current
  267. VSG::rasterizer->set_current_render_target(vp->render_target);
  268. // and draw left eye/mono
  269. _draw_viewport(vp, leftOrMono);
  270. arvr_interface->commit_for_eye(leftOrMono, vp->render_target, vp->viewport_to_screen_rect);
  271. // render right eye
  272. if (leftOrMono == ARVRInterface::EYE_LEFT) {
  273. // check for an external texture destination for our right eye
  274. VSG::storage->render_target_set_external_texture(vp->render_target, arvr_interface->get_external_texture_for_eye(ARVRInterface::EYE_RIGHT), arvr_interface->get_external_depth_for_eye(ARVRInterface::EYE_RIGHT));
  275. // commit for eye may have changed the render target
  276. VSG::rasterizer->set_current_render_target(vp->render_target);
  277. _draw_viewport(vp, ARVRInterface::EYE_RIGHT);
  278. arvr_interface->commit_for_eye(ARVRInterface::EYE_RIGHT, vp->render_target, vp->viewport_to_screen_rect);
  279. }
  280. // and for our frame timing, mark when we've finished committing our eyes
  281. ARVRServer::get_singleton()->_mark_commit();
  282. } else {
  283. VSG::storage->render_target_set_external_texture(vp->render_target, 0, 0);
  284. VSG::rasterizer->set_current_render_target(vp->render_target);
  285. VSG::scene_render->set_debug_draw_mode(vp->debug_draw);
  286. VSG::storage->render_info_begin_capture();
  287. // render standard mono camera
  288. _draw_viewport(vp);
  289. VSG::storage->render_info_end_capture();
  290. vp->render_info[VS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_OBJECTS_IN_FRAME);
  291. vp->render_info[VS::VIEWPORT_RENDER_INFO_VERTICES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_VERTICES_IN_FRAME);
  292. vp->render_info[VS::VIEWPORT_RENDER_INFO_MATERIAL_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);
  293. vp->render_info[VS::VIEWPORT_RENDER_INFO_SHADER_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);
  294. vp->render_info[VS::VIEWPORT_RENDER_INFO_SURFACE_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);
  295. vp->render_info[VS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
  296. vp->render_info[VS::VIEWPORT_RENDER_INFO_2D_ITEMS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_2D_ITEMS_IN_FRAME);
  297. vp->render_info[VS::VIEWPORT_RENDER_INFO_2D_DRAW_CALLS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_2D_DRAW_CALLS_IN_FRAME);
  298. if (vp->viewport_to_screen_rect != Rect2() && (!vp->viewport_render_direct_to_screen || !VSG::rasterizer->is_low_end())) {
  299. //copy to screen if set as such
  300. VSG::rasterizer->set_current_render_target(RID());
  301. VSG::rasterizer->blit_render_target_to_screen(vp->render_target, vp->viewport_to_screen_rect, vp->viewport_to_screen);
  302. }
  303. }
  304. if (vp->update_mode == VS::VIEWPORT_UPDATE_ONCE) {
  305. vp->update_mode = VS::VIEWPORT_UPDATE_DISABLED;
  306. }
  307. VSG::scene_render->set_debug_draw_mode(VS::VIEWPORT_DEBUG_DRAW_DISABLED);
  308. }
  309. }
  310. RID VisualServerViewport::viewport_create() {
  311. Viewport *viewport = memnew(Viewport);
  312. RID rid = viewport_owner.make_rid(viewport);
  313. viewport->self = rid;
  314. viewport->hide_scenario = false;
  315. viewport->hide_canvas = false;
  316. viewport->render_target = VSG::storage->render_target_create();
  317. viewport->shadow_atlas = VSG::scene_render->shadow_atlas_create();
  318. viewport->viewport_render_direct_to_screen = false;
  319. return rid;
  320. }
  321. void VisualServerViewport::viewport_set_use_arvr(RID p_viewport, bool p_use_arvr) {
  322. Viewport *viewport = viewport_owner.getornull(p_viewport);
  323. ERR_FAIL_COND(!viewport);
  324. if (viewport->use_arvr == p_use_arvr) {
  325. return;
  326. }
  327. viewport->use_arvr = p_use_arvr;
  328. if (!viewport->use_arvr && viewport->size.width > 0 && viewport->size.height > 0) {
  329. // No longer controlled by our XR server, make sure we reset it
  330. VSG::storage->render_target_set_size(viewport->render_target, viewport->size.width, viewport->size.height);
  331. }
  332. }
  333. void VisualServerViewport::viewport_set_size(RID p_viewport, int p_width, int p_height) {
  334. ERR_FAIL_COND(p_width < 0 && p_height < 0);
  335. Viewport *viewport = viewport_owner.getornull(p_viewport);
  336. ERR_FAIL_COND(!viewport);
  337. viewport->size = Size2(p_width, p_height);
  338. if (!viewport->use_arvr) {
  339. // Only update if this is not controlled by our XR server
  340. VSG::storage->render_target_set_size(viewport->render_target, p_width, p_height);
  341. }
  342. }
  343. void VisualServerViewport::viewport_set_active(RID p_viewport, bool p_active) {
  344. Viewport *viewport = viewport_owner.getornull(p_viewport);
  345. ERR_FAIL_COND(!viewport);
  346. if (p_active) {
  347. ERR_FAIL_COND_MSG(active_viewports.find(viewport) != -1, "Can't make active a Viewport that is already active.");
  348. active_viewports.push_back(viewport);
  349. } else {
  350. active_viewports.erase(viewport);
  351. }
  352. }
  353. void VisualServerViewport::viewport_set_parent_viewport(RID p_viewport, RID p_parent_viewport) {
  354. Viewport *viewport = viewport_owner.getornull(p_viewport);
  355. ERR_FAIL_COND(!viewport);
  356. viewport->parent = p_parent_viewport;
  357. }
  358. void VisualServerViewport::viewport_set_clear_mode(RID p_viewport, VS::ViewportClearMode p_clear_mode) {
  359. Viewport *viewport = viewport_owner.getornull(p_viewport);
  360. ERR_FAIL_COND(!viewport);
  361. viewport->clear_mode = p_clear_mode;
  362. }
  363. void VisualServerViewport::viewport_attach_to_screen(RID p_viewport, const Rect2 &p_rect, int p_screen) {
  364. Viewport *viewport = viewport_owner.getornull(p_viewport);
  365. ERR_FAIL_COND(!viewport);
  366. // If using GLES2 we can optimize this operation by rendering directly to system_fbo
  367. // instead of rendering to fbo and copying to system_fbo after
  368. if (VSG::rasterizer->is_low_end() && viewport->viewport_render_direct_to_screen) {
  369. VSG::storage->render_target_set_size(viewport->render_target, p_rect.size.x, p_rect.size.y);
  370. VSG::storage->render_target_set_position(viewport->render_target, p_rect.position.x, p_rect.position.y);
  371. }
  372. viewport->viewport_to_screen_rect = p_rect;
  373. viewport->viewport_to_screen = p_screen;
  374. }
  375. void VisualServerViewport::viewport_set_render_direct_to_screen(RID p_viewport, bool p_enable) {
  376. Viewport *viewport = viewport_owner.getornull(p_viewport);
  377. ERR_FAIL_COND(!viewport);
  378. if (p_enable == viewport->viewport_render_direct_to_screen) {
  379. return;
  380. }
  381. // if disabled, reset render_target size and position
  382. if (!p_enable) {
  383. VSG::storage->render_target_set_position(viewport->render_target, 0, 0);
  384. VSG::storage->render_target_set_size(viewport->render_target, viewport->size.x, viewport->size.y);
  385. }
  386. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_DIRECT_TO_SCREEN, p_enable);
  387. viewport->viewport_render_direct_to_screen = p_enable;
  388. // if attached to screen already, setup screen size and position, this needs to happen after setting flag to avoid an unnecessary buffer allocation
  389. if (VSG::rasterizer->is_low_end() && viewport->viewport_to_screen_rect != Rect2() && p_enable) {
  390. VSG::storage->render_target_set_size(viewport->render_target, viewport->viewport_to_screen_rect.size.x, viewport->viewport_to_screen_rect.size.y);
  391. VSG::storage->render_target_set_position(viewport->render_target, viewport->viewport_to_screen_rect.position.x, viewport->viewport_to_screen_rect.position.y);
  392. }
  393. }
  394. void VisualServerViewport::viewport_detach(RID p_viewport) {
  395. Viewport *viewport = viewport_owner.getornull(p_viewport);
  396. ERR_FAIL_COND(!viewport);
  397. // if render_direct_to_screen was used, reset size and position
  398. if (VSG::rasterizer->is_low_end() && viewport->viewport_render_direct_to_screen) {
  399. VSG::storage->render_target_set_position(viewport->render_target, 0, 0);
  400. VSG::storage->render_target_set_size(viewport->render_target, viewport->size.x, viewport->size.y);
  401. }
  402. viewport->viewport_to_screen_rect = Rect2();
  403. viewport->viewport_to_screen = 0;
  404. }
  405. void VisualServerViewport::viewport_set_update_mode(RID p_viewport, VS::ViewportUpdateMode p_mode) {
  406. Viewport *viewport = viewport_owner.getornull(p_viewport);
  407. ERR_FAIL_COND(!viewport);
  408. viewport->update_mode = p_mode;
  409. }
  410. void VisualServerViewport::viewport_set_vflip(RID p_viewport, bool p_enable) {
  411. Viewport *viewport = viewport_owner.getornull(p_viewport);
  412. ERR_FAIL_COND(!viewport);
  413. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_VFLIP, p_enable);
  414. }
  415. RID VisualServerViewport::viewport_get_texture(RID p_viewport) const {
  416. const Viewport *viewport = viewport_owner.getornull(p_viewport);
  417. ERR_FAIL_COND_V(!viewport, RID());
  418. return VSG::storage->render_target_get_texture(viewport->render_target);
  419. }
  420. void VisualServerViewport::viewport_set_hide_scenario(RID p_viewport, bool p_hide) {
  421. Viewport *viewport = viewport_owner.getornull(p_viewport);
  422. ERR_FAIL_COND(!viewport);
  423. viewport->hide_scenario = p_hide;
  424. }
  425. void VisualServerViewport::viewport_set_hide_canvas(RID p_viewport, bool p_hide) {
  426. Viewport *viewport = viewport_owner.getornull(p_viewport);
  427. ERR_FAIL_COND(!viewport);
  428. viewport->hide_canvas = p_hide;
  429. }
  430. void VisualServerViewport::viewport_set_disable_environment(RID p_viewport, bool p_disable) {
  431. Viewport *viewport = viewport_owner.getornull(p_viewport);
  432. ERR_FAIL_COND(!viewport);
  433. viewport->disable_environment = p_disable;
  434. }
  435. void VisualServerViewport::viewport_set_disable_3d(RID p_viewport, bool p_disable) {
  436. Viewport *viewport = viewport_owner.getornull(p_viewport);
  437. ERR_FAIL_COND(!viewport);
  438. viewport->disable_3d = p_disable;
  439. //VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, p_disable);
  440. //this should be just for disabling rendering of 3D, to actually disable it, set usage
  441. }
  442. void VisualServerViewport::viewport_set_keep_3d_linear(RID p_viewport, bool p_keep_3d_linear) {
  443. Viewport *viewport = viewport_owner.getornull(p_viewport);
  444. ERR_FAIL_COND(!viewport);
  445. viewport->keep_3d_linear = p_keep_3d_linear;
  446. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR, p_keep_3d_linear);
  447. }
  448. void VisualServerViewport::viewport_attach_camera(RID p_viewport, RID p_camera) {
  449. Viewport *viewport = viewport_owner.getornull(p_viewport);
  450. ERR_FAIL_COND(!viewport);
  451. viewport->camera = p_camera;
  452. }
  453. void VisualServerViewport::viewport_set_scenario(RID p_viewport, RID p_scenario) {
  454. Viewport *viewport = viewport_owner.getornull(p_viewport);
  455. ERR_FAIL_COND(!viewport);
  456. viewport->scenario = p_scenario;
  457. }
  458. void VisualServerViewport::viewport_attach_canvas(RID p_viewport, RID p_canvas) {
  459. Viewport *viewport = viewport_owner.getornull(p_viewport);
  460. ERR_FAIL_COND(!viewport);
  461. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  462. VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
  463. ERR_FAIL_COND(!canvas);
  464. canvas->viewports.insert(p_viewport);
  465. viewport->canvas_map[p_canvas] = Viewport::CanvasData();
  466. viewport->canvas_map[p_canvas].layer = 0;
  467. viewport->canvas_map[p_canvas].sublayer = 0;
  468. viewport->canvas_map[p_canvas].canvas = canvas;
  469. }
  470. void VisualServerViewport::viewport_remove_canvas(RID p_viewport, RID p_canvas) {
  471. Viewport *viewport = viewport_owner.getornull(p_viewport);
  472. ERR_FAIL_COND(!viewport);
  473. VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
  474. ERR_FAIL_COND(!canvas);
  475. viewport->canvas_map.erase(p_canvas);
  476. canvas->viewports.erase(p_viewport);
  477. }
  478. void VisualServerViewport::viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset) {
  479. Viewport *viewport = viewport_owner.getornull(p_viewport);
  480. ERR_FAIL_COND(!viewport);
  481. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  482. viewport->canvas_map[p_canvas].transform = p_offset;
  483. }
  484. void VisualServerViewport::viewport_set_transparent_background(RID p_viewport, bool p_enabled) {
  485. Viewport *viewport = viewport_owner.getornull(p_viewport);
  486. ERR_FAIL_COND(!viewport);
  487. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_TRANSPARENT, p_enabled);
  488. viewport->transparent_bg = p_enabled;
  489. }
  490. void VisualServerViewport::viewport_set_global_canvas_transform(RID p_viewport, const Transform2D &p_transform) {
  491. Viewport *viewport = viewport_owner.getornull(p_viewport);
  492. ERR_FAIL_COND(!viewport);
  493. viewport->global_transform = p_transform;
  494. }
  495. void VisualServerViewport::viewport_set_canvas_stacking(RID p_viewport, RID p_canvas, int p_layer, int p_sublayer) {
  496. Viewport *viewport = viewport_owner.getornull(p_viewport);
  497. ERR_FAIL_COND(!viewport);
  498. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  499. viewport->canvas_map[p_canvas].layer = p_layer;
  500. viewport->canvas_map[p_canvas].sublayer = p_sublayer;
  501. }
  502. void VisualServerViewport::viewport_set_shadow_atlas_size(RID p_viewport, int p_size) {
  503. Viewport *viewport = viewport_owner.getornull(p_viewport);
  504. ERR_FAIL_COND(!viewport);
  505. viewport->shadow_atlas_size = p_size;
  506. VSG::scene_render->shadow_atlas_set_size(viewport->shadow_atlas, viewport->shadow_atlas_size);
  507. }
  508. void VisualServerViewport::viewport_set_shadow_atlas_quadrant_subdivision(RID p_viewport, int p_quadrant, int p_subdiv) {
  509. Viewport *viewport = viewport_owner.getornull(p_viewport);
  510. ERR_FAIL_COND(!viewport);
  511. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(viewport->shadow_atlas, p_quadrant, p_subdiv);
  512. }
  513. void VisualServerViewport::viewport_set_msaa(RID p_viewport, VS::ViewportMSAA p_msaa) {
  514. Viewport *viewport = viewport_owner.getornull(p_viewport);
  515. ERR_FAIL_COND(!viewport);
  516. VSG::storage->render_target_set_msaa(viewport->render_target, p_msaa);
  517. }
  518. void VisualServerViewport::viewport_set_use_fxaa(RID p_viewport, bool p_fxaa) {
  519. Viewport *viewport = viewport_owner.getornull(p_viewport);
  520. ERR_FAIL_COND(!viewport);
  521. VSG::storage->render_target_set_use_fxaa(viewport->render_target, p_fxaa);
  522. }
  523. void VisualServerViewport::viewport_set_use_debanding(RID p_viewport, bool p_debanding) {
  524. Viewport *viewport = viewport_owner.getornull(p_viewport);
  525. ERR_FAIL_COND(!viewport);
  526. VSG::storage->render_target_set_use_debanding(viewport->render_target, p_debanding);
  527. }
  528. void VisualServerViewport::viewport_set_sharpen_intensity(RID p_viewport, float p_intensity) {
  529. Viewport *viewport = viewport_owner.getornull(p_viewport);
  530. ERR_FAIL_COND(!viewport);
  531. VSG::storage->render_target_set_sharpen_intensity(viewport->render_target, p_intensity);
  532. }
  533. void VisualServerViewport::viewport_set_hdr(RID p_viewport, bool p_enabled) {
  534. Viewport *viewport = viewport_owner.getornull(p_viewport);
  535. ERR_FAIL_COND(!viewport);
  536. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_HDR, p_enabled);
  537. }
  538. void VisualServerViewport::viewport_set_use_32_bpc_depth(RID p_viewport, bool p_enabled) {
  539. Viewport *viewport = viewport_owner.getornull(p_viewport);
  540. ERR_FAIL_COND(!viewport);
  541. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_USE_32_BPC_DEPTH, p_enabled);
  542. }
  543. void VisualServerViewport::viewport_set_usage(RID p_viewport, VS::ViewportUsage p_usage) {
  544. Viewport *viewport = viewport_owner.getornull(p_viewport);
  545. ERR_FAIL_COND(!viewport);
  546. switch (p_usage) {
  547. case VS::VIEWPORT_USAGE_2D: {
  548. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, true);
  549. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true);
  550. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false);
  551. viewport->disable_3d_by_usage = true;
  552. } break;
  553. case VS::VIEWPORT_USAGE_2D_NO_SAMPLING: {
  554. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, true);
  555. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true);
  556. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, true);
  557. viewport->disable_3d_by_usage = true;
  558. } break;
  559. case VS::VIEWPORT_USAGE_3D: {
  560. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, false);
  561. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, false);
  562. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false);
  563. viewport->disable_3d_by_usage = false;
  564. } break;
  565. case VS::VIEWPORT_USAGE_3D_NO_EFFECTS: {
  566. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, false);
  567. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true);
  568. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false);
  569. viewport->disable_3d_by_usage = false;
  570. } break;
  571. }
  572. }
  573. int VisualServerViewport::viewport_get_render_info(RID p_viewport, VS::ViewportRenderInfo p_info) {
  574. ERR_FAIL_INDEX_V(p_info, VS::VIEWPORT_RENDER_INFO_MAX, -1);
  575. Viewport *viewport = viewport_owner.getornull(p_viewport);
  576. if (!viewport) {
  577. return 0; //there should be a lock here..
  578. }
  579. return viewport->render_info[p_info];
  580. }
  581. void VisualServerViewport::viewport_set_debug_draw(RID p_viewport, VS::ViewportDebugDraw p_draw) {
  582. Viewport *viewport = viewport_owner.getornull(p_viewport);
  583. ERR_FAIL_COND(!viewport);
  584. viewport->debug_draw = p_draw;
  585. }
  586. bool VisualServerViewport::free(RID p_rid) {
  587. if (viewport_owner.owns(p_rid)) {
  588. Viewport *viewport = viewport_owner.getornull(p_rid);
  589. VSG::storage->free(viewport->render_target);
  590. VSG::scene_render->free(viewport->shadow_atlas);
  591. while (viewport->canvas_map.front()) {
  592. viewport_remove_canvas(p_rid, viewport->canvas_map.front()->key());
  593. }
  594. viewport_set_scenario(p_rid, RID());
  595. active_viewports.erase(viewport);
  596. viewport_owner.free(p_rid);
  597. memdelete(viewport);
  598. return true;
  599. }
  600. return false;
  601. }
  602. void VisualServerViewport::set_default_clear_color(const Color &p_color) {
  603. clear_color = p_color;
  604. }
  605. VisualServerViewport::VisualServerViewport() {
  606. }