visual_server_viewport.cpp 30 KB

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