visual_server_viewport.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_global.h"
  34. #include "visual_server_scene.h"
  35. void VisualServerViewport::_draw_viewport(Viewport *p_viewport, ARVRInterface::Eyes p_eye) {
  36. /* Camera should always be BEFORE any other 3D */
  37. bool scenario_draw_canvas_bg = false; //draw canvas, or some layer of it, as BG for 3D instead of in front
  38. int scenario_canvas_max_layer = 0;
  39. if (!p_viewport->hide_canvas && !p_viewport->disable_environment && VSG::scene->scenario_owner.owns(p_viewport->scenario)) {
  40. VisualServerScene::Scenario *scenario = VSG::scene->scenario_owner.get(p_viewport->scenario);
  41. if (VSG::scene_render->is_environment(scenario->environment)) {
  42. scenario_draw_canvas_bg = VSG::scene_render->environment_get_background(scenario->environment) == VS::ENV_BG_CANVAS;
  43. scenario_canvas_max_layer = VSG::scene_render->environment_get_canvas_max_layer(scenario->environment);
  44. }
  45. }
  46. bool can_draw_3d = !p_viewport->disable_3d && !p_viewport->disable_3d_by_usage && VSG::scene->camera_owner.owns(p_viewport->camera);
  47. if (p_viewport->clear_mode != VS::VIEWPORT_CLEAR_NEVER) {
  48. VSG::rasterizer->clear_render_target(p_viewport->transparent_bg ? Color(0, 0, 0, 0) : clear_color);
  49. if (p_viewport->clear_mode == VS::VIEWPORT_CLEAR_ONLY_NEXT_FRAME) {
  50. p_viewport->clear_mode = VS::VIEWPORT_CLEAR_NEVER;
  51. }
  52. }
  53. if (!scenario_draw_canvas_bg && can_draw_3d) {
  54. Ref<ARVRInterface> arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  55. if (p_viewport->use_arvr && arvr_interface.is_valid()) {
  56. VSG::scene->render_camera(arvr_interface, p_eye, p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  57. } else {
  58. VSG::scene->render_camera(p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  59. }
  60. }
  61. if (!p_viewport->hide_canvas) {
  62. int i = 0;
  63. Map<Viewport::CanvasKey, Viewport::CanvasData *> canvas_map;
  64. Rect2 clip_rect(0, 0, p_viewport->size.x, p_viewport->size.y);
  65. RasterizerCanvas::Light *lights = NULL;
  66. RasterizerCanvas::Light *lights_with_shadow = NULL;
  67. RasterizerCanvas::Light *lights_with_mask = NULL;
  68. Rect2 shadow_rect;
  69. int light_count = 0;
  70. for (Map<RID, Viewport::CanvasData>::Element *E = p_viewport->canvas_map.front(); E; E = E->next()) {
  71. Transform2D xf = p_viewport->global_transform * E->get().transform;
  72. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas *>(E->get().canvas);
  73. //find lights in canvas
  74. for (Set<RasterizerCanvas::Light *>::Element *F = canvas->lights.front(); F; F = F->next()) {
  75. RasterizerCanvas::Light *cl = F->get();
  76. if (cl->enabled && cl->texture.is_valid()) {
  77. //not super efficient..
  78. Size2 tsize(VSG::storage->texture_get_width(cl->texture), VSG::storage->texture_get_height(cl->texture));
  79. tsize *= cl->scale;
  80. Vector2 offset = tsize / 2.0;
  81. cl->rect_cache = Rect2(-offset + cl->texture_offset, tsize);
  82. cl->xform_cache = xf * cl->xform;
  83. if (clip_rect.intersects_transformed(cl->xform_cache, cl->rect_cache)) {
  84. cl->filter_next_ptr = lights;
  85. lights = cl;
  86. cl->texture_cache = NULL;
  87. Transform2D scale;
  88. scale.scale(cl->rect_cache.size);
  89. scale.elements[2] = cl->rect_cache.position;
  90. cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse();
  91. cl->light_shader_pos = cl->xform_cache[2];
  92. if (cl->shadow_buffer.is_valid()) {
  93. cl->shadows_next_ptr = lights_with_shadow;
  94. if (lights_with_shadow == NULL) {
  95. shadow_rect = cl->xform_cache.xform(cl->rect_cache);
  96. } else {
  97. shadow_rect = shadow_rect.merge(cl->xform_cache.xform(cl->rect_cache));
  98. }
  99. lights_with_shadow = cl;
  100. cl->radius_cache = cl->rect_cache.size.length();
  101. }
  102. if (cl->mode == VS::CANVAS_LIGHT_MODE_MASK) {
  103. cl->mask_next_ptr = lights_with_mask;
  104. lights_with_mask = cl;
  105. }
  106. light_count++;
  107. }
  108. VSG::canvas_render->light_internal_update(cl->light_internal, cl);
  109. }
  110. }
  111. canvas_map[Viewport::CanvasKey(E->key(), E->get().layer, E->get().sublayer)] = &E->get();
  112. }
  113. if (lights_with_shadow) {
  114. //update shadows if any
  115. RasterizerCanvas::LightOccluderInstance *occluders = NULL;
  116. //make list of occluders
  117. for (Map<RID, Viewport::CanvasData>::Element *E = p_viewport->canvas_map.front(); E; E = E->next()) {
  118. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas *>(E->get().canvas);
  119. Transform2D xf = p_viewport->global_transform * E->get().transform;
  120. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *F = canvas->occluders.front(); F; F = F->next()) {
  121. if (!F->get()->enabled)
  122. continue;
  123. F->get()->xform_cache = xf * F->get()->xform;
  124. if (shadow_rect.intersects_transformed(F->get()->xform_cache, F->get()->aabb_cache)) {
  125. F->get()->next = occluders;
  126. occluders = F->get();
  127. }
  128. }
  129. }
  130. //update the light shadowmaps with them
  131. RasterizerCanvas::Light *light = lights_with_shadow;
  132. while (light) {
  133. VSG::canvas_render->canvas_light_shadow_buffer_update(light->shadow_buffer, light->xform_cache.affine_inverse(), light->item_mask, light->radius_cache / 1000.0, light->radius_cache * 1.1, occluders, &light->shadow_matrix_cache);
  134. light = light->shadows_next_ptr;
  135. }
  136. //VSG::canvas_render->reset_canvas();
  137. }
  138. VSG::rasterizer->restore_render_target();
  139. if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().get_layer() > scenario_canvas_max_layer) {
  140. Ref<ARVRInterface> arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  141. if (!can_draw_3d) {
  142. VSG::scene->render_empty_scene(p_viewport->scenario, p_viewport->shadow_atlas);
  143. } else if (p_viewport->use_arvr && arvr_interface.is_valid()) {
  144. VSG::scene->render_camera(arvr_interface, p_eye, p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  145. } else {
  146. VSG::scene->render_camera(p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  147. }
  148. scenario_draw_canvas_bg = false;
  149. }
  150. for (Map<Viewport::CanvasKey, Viewport::CanvasData *>::Element *E = canvas_map.front(); E; E = E->next()) {
  151. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas *>(E->get()->canvas);
  152. Transform2D xform = p_viewport->global_transform * E->get()->transform;
  153. RasterizerCanvas::Light *canvas_lights = NULL;
  154. RasterizerCanvas::Light *ptr = lights;
  155. while (ptr) {
  156. if (E->get()->layer >= ptr->layer_min && E->get()->layer <= ptr->layer_max) {
  157. ptr->next_ptr = canvas_lights;
  158. canvas_lights = ptr;
  159. }
  160. ptr = ptr->filter_next_ptr;
  161. }
  162. VSG::canvas->render_canvas(canvas, xform, canvas_lights, lights_with_mask, clip_rect);
  163. i++;
  164. if (scenario_draw_canvas_bg && E->key().get_layer() >= scenario_canvas_max_layer) {
  165. Ref<ARVRInterface> arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  166. if (!can_draw_3d) {
  167. VSG::scene->render_empty_scene(p_viewport->scenario, p_viewport->shadow_atlas);
  168. } else if (p_viewport->use_arvr && arvr_interface.is_valid()) {
  169. VSG::scene->render_camera(arvr_interface, p_eye, p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  170. } else {
  171. VSG::scene->render_camera(p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  172. }
  173. scenario_draw_canvas_bg = false;
  174. }
  175. }
  176. if (scenario_draw_canvas_bg) {
  177. Ref<ARVRInterface> arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  178. if (!can_draw_3d) {
  179. VSG::scene->render_empty_scene(p_viewport->scenario, p_viewport->shadow_atlas);
  180. } else if (p_viewport->use_arvr && arvr_interface.is_valid()) {
  181. VSG::scene->render_camera(arvr_interface, p_eye, p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  182. } else {
  183. VSG::scene->render_camera(p_viewport->camera, p_viewport->scenario, p_viewport->size, p_viewport->shadow_atlas);
  184. }
  185. scenario_draw_canvas_bg = false;
  186. }
  187. //VSG::canvas_render->canvas_debug_viewport_shadows(lights_with_shadow);
  188. }
  189. }
  190. void VisualServerViewport::draw_viewports() {
  191. // get our arvr interface in case we need it
  192. Ref<ARVRInterface> arvr_interface = ARVRServer::get_singleton()->get_primary_interface();
  193. // process all our active interfaces
  194. ARVRServer::get_singleton()->_process();
  195. if (Engine::get_singleton()->is_editor_hint()) {
  196. clear_color = GLOBAL_GET("rendering/environment/default_clear_color");
  197. }
  198. //sort viewports
  199. active_viewports.sort_custom<ViewportSort>();
  200. //draw viewports
  201. for (int i = 0; i < active_viewports.size(); i++) {
  202. Viewport *vp = active_viewports[i];
  203. if (vp->update_mode == VS::VIEWPORT_UPDATE_DISABLED)
  204. continue;
  205. ERR_CONTINUE(!vp->render_target.is_valid());
  206. 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));
  207. visible = visible && vp->size.x > 1 && vp->size.y > 1;
  208. if (!visible)
  209. continue;
  210. VSG::storage->render_target_clear_used(vp->render_target);
  211. if (vp->use_arvr && arvr_interface.is_valid()) {
  212. // override our size, make sure it matches our required size
  213. Size2 size = arvr_interface->get_render_targetsize();
  214. VSG::storage->render_target_set_size(vp->render_target, size.x, size.y);
  215. // render mono or left eye first
  216. ARVRInterface::Eyes leftOrMono = arvr_interface->is_stereo() ? ARVRInterface::EYE_LEFT : ARVRInterface::EYE_MONO;
  217. VSG::rasterizer->set_current_render_target(vp->render_target);
  218. _draw_viewport(vp, leftOrMono);
  219. arvr_interface->commit_for_eye(leftOrMono, vp->render_target, vp->viewport_to_screen_rect);
  220. // render right eye
  221. if (leftOrMono == ARVRInterface::EYE_LEFT) {
  222. // commit for eye may have changed the render target
  223. VSG::rasterizer->set_current_render_target(vp->render_target);
  224. _draw_viewport(vp, ARVRInterface::EYE_RIGHT);
  225. arvr_interface->commit_for_eye(ARVRInterface::EYE_RIGHT, vp->render_target, vp->viewport_to_screen_rect);
  226. }
  227. // and for our frame timing, mark when we've finished committing our eyes
  228. ARVRServer::get_singleton()->_mark_commit();
  229. } else {
  230. VSG::rasterizer->set_current_render_target(vp->render_target);
  231. VSG::scene_render->set_debug_draw_mode(vp->debug_draw);
  232. VSG::storage->render_info_begin_capture();
  233. // render standard mono camera
  234. _draw_viewport(vp);
  235. VSG::storage->render_info_end_capture();
  236. vp->render_info[VS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_OBJECTS_IN_FRAME);
  237. vp->render_info[VS::VIEWPORT_RENDER_INFO_VERTICES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_VERTICES_IN_FRAME);
  238. vp->render_info[VS::VIEWPORT_RENDER_INFO_MATERIAL_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);
  239. vp->render_info[VS::VIEWPORT_RENDER_INFO_SHADER_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);
  240. vp->render_info[VS::VIEWPORT_RENDER_INFO_SURFACE_CHANGES_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);
  241. vp->render_info[VS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME] = VSG::storage->get_captured_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
  242. if (vp->viewport_to_screen_rect != Rect2()) {
  243. //copy to screen if set as such
  244. VSG::rasterizer->set_current_render_target(RID());
  245. VSG::rasterizer->blit_render_target_to_screen(vp->render_target, vp->viewport_to_screen_rect, vp->viewport_to_screen);
  246. }
  247. }
  248. if (vp->update_mode == VS::VIEWPORT_UPDATE_ONCE) {
  249. vp->update_mode = VS::VIEWPORT_UPDATE_DISABLED;
  250. }
  251. VSG::scene_render->set_debug_draw_mode(VS::VIEWPORT_DEBUG_DRAW_DISABLED);
  252. }
  253. }
  254. RID VisualServerViewport::viewport_create() {
  255. Viewport *viewport = memnew(Viewport);
  256. RID rid = viewport_owner.make_rid(viewport);
  257. viewport->self = rid;
  258. viewport->hide_scenario = false;
  259. viewport->hide_canvas = false;
  260. viewport->render_target = VSG::storage->render_target_create();
  261. viewport->shadow_atlas = VSG::scene_render->shadow_atlas_create();
  262. return rid;
  263. }
  264. void VisualServerViewport::viewport_set_use_arvr(RID p_viewport, bool p_use_arvr) {
  265. Viewport *viewport = viewport_owner.getornull(p_viewport);
  266. ERR_FAIL_COND(!viewport);
  267. viewport->use_arvr = p_use_arvr;
  268. }
  269. void VisualServerViewport::viewport_set_size(RID p_viewport, int p_width, int p_height) {
  270. ERR_FAIL_COND(p_width < 0 && p_height < 0);
  271. Viewport *viewport = viewport_owner.getornull(p_viewport);
  272. ERR_FAIL_COND(!viewport);
  273. viewport->size = Size2(p_width, p_height);
  274. VSG::storage->render_target_set_size(viewport->render_target, p_width, p_height);
  275. }
  276. void VisualServerViewport::viewport_set_active(RID p_viewport, bool p_active) {
  277. Viewport *viewport = viewport_owner.getornull(p_viewport);
  278. ERR_FAIL_COND(!viewport);
  279. if (p_active) {
  280. ERR_FAIL_COND(active_viewports.find(viewport) != -1); //already active
  281. active_viewports.push_back(viewport);
  282. } else {
  283. active_viewports.erase(viewport);
  284. }
  285. }
  286. void VisualServerViewport::viewport_set_parent_viewport(RID p_viewport, RID p_parent_viewport) {
  287. Viewport *viewport = viewport_owner.getornull(p_viewport);
  288. ERR_FAIL_COND(!viewport);
  289. viewport->parent = p_parent_viewport;
  290. }
  291. void VisualServerViewport::viewport_set_clear_mode(RID p_viewport, VS::ViewportClearMode p_clear_mode) {
  292. Viewport *viewport = viewport_owner.getornull(p_viewport);
  293. ERR_FAIL_COND(!viewport);
  294. viewport->clear_mode = p_clear_mode;
  295. }
  296. void VisualServerViewport::viewport_attach_to_screen(RID p_viewport, const Rect2 &p_rect, int p_screen) {
  297. Viewport *viewport = viewport_owner.getornull(p_viewport);
  298. ERR_FAIL_COND(!viewport);
  299. viewport->viewport_to_screen_rect = p_rect;
  300. viewport->viewport_to_screen = p_screen;
  301. }
  302. void VisualServerViewport::viewport_detach(RID p_viewport) {
  303. Viewport *viewport = viewport_owner.getornull(p_viewport);
  304. ERR_FAIL_COND(!viewport);
  305. viewport->viewport_to_screen_rect = Rect2();
  306. viewport->viewport_to_screen = 0;
  307. }
  308. void VisualServerViewport::viewport_set_update_mode(RID p_viewport, VS::ViewportUpdateMode p_mode) {
  309. Viewport *viewport = viewport_owner.getornull(p_viewport);
  310. ERR_FAIL_COND(!viewport);
  311. viewport->update_mode = p_mode;
  312. }
  313. void VisualServerViewport::viewport_set_vflip(RID p_viewport, bool p_enable) {
  314. Viewport *viewport = viewport_owner.getornull(p_viewport);
  315. ERR_FAIL_COND(!viewport);
  316. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_VFLIP, p_enable);
  317. }
  318. RID VisualServerViewport::viewport_get_texture(RID p_viewport) const {
  319. const Viewport *viewport = viewport_owner.getornull(p_viewport);
  320. ERR_FAIL_COND_V(!viewport, RID());
  321. return VSG::storage->render_target_get_texture(viewport->render_target);
  322. }
  323. void VisualServerViewport::viewport_set_hide_scenario(RID p_viewport, bool p_hide) {
  324. Viewport *viewport = viewport_owner.getornull(p_viewport);
  325. ERR_FAIL_COND(!viewport);
  326. viewport->hide_scenario = p_hide;
  327. }
  328. void VisualServerViewport::viewport_set_hide_canvas(RID p_viewport, bool p_hide) {
  329. Viewport *viewport = viewport_owner.getornull(p_viewport);
  330. ERR_FAIL_COND(!viewport);
  331. viewport->hide_canvas = p_hide;
  332. }
  333. void VisualServerViewport::viewport_set_disable_environment(RID p_viewport, bool p_disable) {
  334. Viewport *viewport = viewport_owner.getornull(p_viewport);
  335. ERR_FAIL_COND(!viewport);
  336. viewport->disable_environment = p_disable;
  337. }
  338. void VisualServerViewport::viewport_set_disable_3d(RID p_viewport, bool p_disable) {
  339. Viewport *viewport = viewport_owner.getornull(p_viewport);
  340. ERR_FAIL_COND(!viewport);
  341. viewport->disable_3d = p_disable;
  342. //VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, p_disable);
  343. //this should be just for disabling rendering of 3D, to actually disable it, set usage
  344. }
  345. void VisualServerViewport::viewport_set_keep_3d_linear(RID p_viewport, bool p_keep_3d_linear) {
  346. Viewport *viewport = viewport_owner.getornull(p_viewport);
  347. ERR_FAIL_COND(!viewport);
  348. viewport->keep_3d_linear = p_keep_3d_linear;
  349. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR, p_keep_3d_linear);
  350. }
  351. void VisualServerViewport::viewport_attach_camera(RID p_viewport, RID p_camera) {
  352. Viewport *viewport = viewport_owner.getornull(p_viewport);
  353. ERR_FAIL_COND(!viewport);
  354. viewport->camera = p_camera;
  355. }
  356. void VisualServerViewport::viewport_set_scenario(RID p_viewport, RID p_scenario) {
  357. Viewport *viewport = viewport_owner.getornull(p_viewport);
  358. ERR_FAIL_COND(!viewport);
  359. viewport->scenario = p_scenario;
  360. }
  361. void VisualServerViewport::viewport_attach_canvas(RID p_viewport, RID p_canvas) {
  362. Viewport *viewport = viewport_owner.getornull(p_viewport);
  363. ERR_FAIL_COND(!viewport);
  364. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  365. VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
  366. ERR_FAIL_COND(!canvas);
  367. canvas->viewports.insert(p_viewport);
  368. viewport->canvas_map[p_canvas] = Viewport::CanvasData();
  369. viewport->canvas_map[p_canvas].layer = 0;
  370. viewport->canvas_map[p_canvas].sublayer = 0;
  371. viewport->canvas_map[p_canvas].canvas = canvas;
  372. }
  373. void VisualServerViewport::viewport_remove_canvas(RID p_viewport, RID p_canvas) {
  374. Viewport *viewport = viewport_owner.getornull(p_viewport);
  375. ERR_FAIL_COND(!viewport);
  376. VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
  377. ERR_FAIL_COND(!canvas);
  378. viewport->canvas_map.erase(p_canvas);
  379. canvas->viewports.erase(p_viewport);
  380. }
  381. void VisualServerViewport::viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset) {
  382. Viewport *viewport = viewport_owner.getornull(p_viewport);
  383. ERR_FAIL_COND(!viewport);
  384. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  385. viewport->canvas_map[p_canvas].transform = p_offset;
  386. }
  387. void VisualServerViewport::viewport_set_transparent_background(RID p_viewport, bool p_enabled) {
  388. Viewport *viewport = viewport_owner.getornull(p_viewport);
  389. ERR_FAIL_COND(!viewport);
  390. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_TRANSPARENT, p_enabled);
  391. viewport->transparent_bg = p_enabled;
  392. }
  393. void VisualServerViewport::viewport_set_global_canvas_transform(RID p_viewport, const Transform2D &p_transform) {
  394. Viewport *viewport = viewport_owner.getornull(p_viewport);
  395. ERR_FAIL_COND(!viewport);
  396. viewport->global_transform = p_transform;
  397. }
  398. void VisualServerViewport::viewport_set_canvas_stacking(RID p_viewport, RID p_canvas, int p_layer, int p_sublayer) {
  399. Viewport *viewport = viewport_owner.getornull(p_viewport);
  400. ERR_FAIL_COND(!viewport);
  401. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  402. viewport->canvas_map[p_canvas].layer = p_layer;
  403. viewport->canvas_map[p_canvas].sublayer = p_sublayer;
  404. }
  405. void VisualServerViewport::viewport_set_shadow_atlas_size(RID p_viewport, int p_size) {
  406. Viewport *viewport = viewport_owner.getornull(p_viewport);
  407. ERR_FAIL_COND(!viewport);
  408. viewport->shadow_atlas_size = p_size;
  409. VSG::scene_render->shadow_atlas_set_size(viewport->shadow_atlas, viewport->shadow_atlas_size);
  410. }
  411. void VisualServerViewport::viewport_set_shadow_atlas_quadrant_subdivision(RID p_viewport, int p_quadrant, int p_subdiv) {
  412. Viewport *viewport = viewport_owner.getornull(p_viewport);
  413. ERR_FAIL_COND(!viewport);
  414. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(viewport->shadow_atlas, p_quadrant, p_subdiv);
  415. }
  416. void VisualServerViewport::viewport_set_msaa(RID p_viewport, VS::ViewportMSAA p_msaa) {
  417. Viewport *viewport = viewport_owner.getornull(p_viewport);
  418. ERR_FAIL_COND(!viewport);
  419. VSG::storage->render_target_set_msaa(viewport->render_target, p_msaa);
  420. }
  421. void VisualServerViewport::viewport_set_hdr(RID p_viewport, bool p_enabled) {
  422. Viewport *viewport = viewport_owner.getornull(p_viewport);
  423. ERR_FAIL_COND(!viewport);
  424. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_HDR, p_enabled);
  425. }
  426. void VisualServerViewport::viewport_set_usage(RID p_viewport, VS::ViewportUsage p_usage) {
  427. Viewport *viewport = viewport_owner.getornull(p_viewport);
  428. ERR_FAIL_COND(!viewport);
  429. switch (p_usage) {
  430. case VS::VIEWPORT_USAGE_2D: {
  431. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, true);
  432. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true);
  433. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false);
  434. viewport->disable_3d_by_usage = true;
  435. } break;
  436. case VS::VIEWPORT_USAGE_2D_NO_SAMPLING: {
  437. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, true);
  438. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true);
  439. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, true);
  440. viewport->disable_3d_by_usage = true;
  441. } break;
  442. case VS::VIEWPORT_USAGE_3D: {
  443. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, false);
  444. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, false);
  445. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false);
  446. viewport->disable_3d_by_usage = false;
  447. } break;
  448. case VS::VIEWPORT_USAGE_3D_NO_EFFECTS: {
  449. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D, false);
  450. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS, true);
  451. VSG::storage->render_target_set_flag(viewport->render_target, RasterizerStorage::RENDER_TARGET_NO_SAMPLING, false);
  452. viewport->disable_3d_by_usage = false;
  453. } break;
  454. }
  455. }
  456. int VisualServerViewport::viewport_get_render_info(RID p_viewport, VS::ViewportRenderInfo p_info) {
  457. ERR_FAIL_INDEX_V(p_info, VS::VIEWPORT_RENDER_INFO_MAX, -1);
  458. Viewport *viewport = viewport_owner.getornull(p_viewport);
  459. if (!viewport)
  460. return 0; //there should be a lock here..
  461. return viewport->render_info[p_info];
  462. }
  463. void VisualServerViewport::viewport_set_debug_draw(RID p_viewport, VS::ViewportDebugDraw p_draw) {
  464. Viewport *viewport = viewport_owner.getornull(p_viewport);
  465. ERR_FAIL_COND(!viewport);
  466. viewport->debug_draw = p_draw;
  467. }
  468. bool VisualServerViewport::free(RID p_rid) {
  469. if (viewport_owner.owns(p_rid)) {
  470. Viewport *viewport = viewport_owner.getornull(p_rid);
  471. VSG::storage->free(viewport->render_target);
  472. VSG::scene_render->free(viewport->shadow_atlas);
  473. while (viewport->canvas_map.front()) {
  474. viewport_remove_canvas(p_rid, viewport->canvas_map.front()->key());
  475. }
  476. viewport_set_scenario(p_rid, RID());
  477. active_viewports.erase(viewport);
  478. viewport_owner.free(p_rid);
  479. memdelete(viewport);
  480. return true;
  481. }
  482. return false;
  483. }
  484. void VisualServerViewport::set_default_clear_color(const Color &p_color) {
  485. clear_color = p_color;
  486. }
  487. VisualServerViewport::VisualServerViewport() {
  488. }