graphicsOutput.cxx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // Filename: graphicsOutput.cxx
  2. // Created by: drose (06Feb04)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "graphicsOutput.h"
  19. #include "graphicsPipe.h"
  20. #include "graphicsEngine.h"
  21. #include "graphicsWindow.h"
  22. #include "config_display.h"
  23. #include "mutexHolder.h"
  24. #include "renderBuffer.h"
  25. #include "indirectLess.h"
  26. #include "pStatTimer.h"
  27. #include "configVariableBool.h"
  28. #include "camera.h"
  29. #include "displayRegion.h"
  30. #include "lens.h"
  31. #include "perspectiveLens.h"
  32. #include "pointerTo.h"
  33. #include "compassEffect.h"
  34. TypeHandle GraphicsOutput::_type_handle;
  35. PStatCollector GraphicsOutput::_make_current_pcollector("Draw:Make current");
  36. PStatCollector GraphicsOutput::_copy_texture_pcollector("Draw:Copy texture");
  37. struct CubeFaceDef {
  38. CubeFaceDef(const char *name, const LPoint3f &look_at, const LVector3f &up) :
  39. _name(name), _look_at(look_at), _up(up) { }
  40. const char *_name;
  41. LPoint3f _look_at;
  42. LVector3f _up;
  43. };
  44. static CubeFaceDef cube_faces[6] = {
  45. CubeFaceDef("positive_x", LPoint3f(1, 0, 0), LVector3f(0, -1, 0)),
  46. CubeFaceDef("negative_x", LPoint3f(-1, 0, 0), LVector3f(0, -1, 0)),
  47. CubeFaceDef("positive_y", LPoint3f(0, 1, 0), LVector3f(0, 0, 1)),
  48. CubeFaceDef("negative_y", LPoint3f(0, -1, 0), LVector3f(0, 0, -1)),
  49. CubeFaceDef("positive_z", LPoint3f(0, 0, 1), LVector3f(0, -1, 0)),
  50. CubeFaceDef("negative_z", LPoint3f(0, 0, -1), LVector3f(0, -1, 0))
  51. };
  52. ////////////////////////////////////////////////////////////////////
  53. // Function: GraphicsOutput::Constructor
  54. // Access: Protected
  55. // Description: Normally, the GraphicsOutput constructor is not
  56. // called directly; these are created instead via the
  57. // GraphicsEngine::make_window() function.
  58. ////////////////////////////////////////////////////////////////////
  59. GraphicsOutput::
  60. GraphicsOutput(GraphicsPipe *pipe, GraphicsStateGuardian *gsg,
  61. const string &name) {
  62. #ifdef DO_MEMORY_USAGE
  63. MemoryUsage::update_type(this, this);
  64. #endif
  65. _pipe = pipe;
  66. _gsg = gsg;
  67. _name = name;
  68. _x_size = 0;
  69. _y_size = 0;
  70. _has_size = false;
  71. _is_valid = false;
  72. _rtm_mode = RTM_none;
  73. _flip_ready = false;
  74. _needs_context = true;
  75. _cube_map_index = -1;
  76. _sort = 0;
  77. _internal_sort_index = 0;
  78. _active = true;
  79. _one_shot = false;
  80. _inverted = window_inverted;
  81. _delete_flag = false;
  82. int mode = gsg->get_properties().get_frame_buffer_mode();
  83. if ((mode & FrameBufferProperties::FM_buffer) == FrameBufferProperties::FM_single_buffer) {
  84. // Single buffered; we must draw into the front buffer.
  85. _draw_buffer_type = RenderBuffer::T_front;
  86. }
  87. // We start out with one DisplayRegion that covers the whole window,
  88. // which we may use internally for full-window operations like
  89. // clear() and get_screenshot().
  90. _default_display_region = make_display_region(0.0f, 1.0f, 0.0f, 1.0f);
  91. _default_display_region->set_active(false);
  92. _display_regions_stale = false;
  93. // By default, each new GraphicsOutput is set up to clear color and
  94. // depth.
  95. set_clear_color_active(true);
  96. set_clear_depth_active(true);
  97. switch (background_color.get_num_words()) {
  98. case 1:
  99. set_clear_color(Colorf(background_color[0], background_color[0], background_color[0], 0.0f));
  100. break;
  101. case 2:
  102. set_clear_color(Colorf(background_color[0], background_color[0], background_color[0], background_color[1]));
  103. break;
  104. case 3:
  105. set_clear_color(Colorf(background_color[0], background_color[1], background_color[2], 0.0f));
  106. break;
  107. case 4:
  108. set_clear_color(Colorf(background_color[0], background_color[1], background_color[2], background_color[3]));
  109. break;
  110. default:
  111. display_cat.warning()
  112. << "Invalid background-color specification: "
  113. << background_color.get_string_value() << "\n";
  114. }
  115. }
  116. ////////////////////////////////////////////////////////////////////
  117. // Function: GraphicsOutput::Copy Constructor
  118. // Access: Private
  119. // Description:
  120. ////////////////////////////////////////////////////////////////////
  121. GraphicsOutput::
  122. GraphicsOutput(const GraphicsOutput &) {
  123. nassertv(false);
  124. }
  125. ////////////////////////////////////////////////////////////////////
  126. // Function: GraphicsOutput::Copy Assignment Operator
  127. // Access: Private
  128. // Description:
  129. ////////////////////////////////////////////////////////////////////
  130. void GraphicsOutput::
  131. operator = (const GraphicsOutput &) {
  132. nassertv(false);
  133. }
  134. ////////////////////////////////////////////////////////////////////
  135. // Function: GraphicsOutput::Destructor
  136. // Access: Published, Virtual
  137. // Description:
  138. ////////////////////////////////////////////////////////////////////
  139. GraphicsOutput::
  140. ~GraphicsOutput() {
  141. // The window should be closed by the time we destruct.
  142. nassertv(!is_valid());
  143. // We shouldn't have a GraphicsPipe pointer anymore.
  144. nassertv(_pipe == (GraphicsPipe *)NULL);
  145. // We don't have to destruct our child display regions explicitly,
  146. // since they are all reference-counted and will go away when their
  147. // pointers do. However, we do need to zero out their pointers to
  148. // us.
  149. TotalDisplayRegions::iterator dri;
  150. for (dri = _total_display_regions.begin();
  151. dri != _total_display_regions.end();
  152. ++dri) {
  153. (*dri)->_window = NULL;
  154. }
  155. _total_display_regions.clear();
  156. _active_display_regions.clear();
  157. _default_display_region = NULL;
  158. }
  159. ////////////////////////////////////////////////////////////////////
  160. // Function: GraphicsOutput::detach_texture
  161. // Access: Published
  162. // Description: Disassociates the texture from the GraphicsOutput.
  163. // The texture will no longer be filled as the frame
  164. // renders, and it may be used as an ordinary texture in
  165. // its own right. However, its contents may be
  166. // undefined.
  167. ////////////////////////////////////////////////////////////////////
  168. void GraphicsOutput::
  169. detach_texture() {
  170. MutexHolder holder(_lock);
  171. if (_rtm_mode == RTM_bind_texture && _gsg != (GraphicsStateGuardian *)NULL) {
  172. _gsg->framebuffer_release_texture(this, get_texture());
  173. }
  174. _texture = NULL;
  175. _rtm_mode = RTM_none;
  176. set_inverted(window_inverted);
  177. }
  178. ////////////////////////////////////////////////////////////////////
  179. // Function: GraphicsOutput::setup_render_texture
  180. // Access: Published
  181. // Description: Creates a new Texture object, suitable for rendering
  182. // the contents of this buffer into, and stores it in
  183. // _texture. This also disassociates the previous
  184. // texture (if any).
  185. //
  186. // If tex is not NULL, it is the texture that will be
  187. // set up for rendering into; otherwise, a new Texture
  188. // object will be created (in which case you may call
  189. // get_texture() to retrieve the new texture pointer
  190. // later).
  191. //
  192. // If allow_bind is true, and this GraphicsOutput is an
  193. // offscreen graphics buffer that has not yet been
  194. // rendered into, it will attempt to set up the buffer
  195. // for rendering directly into the texture, avoiding the
  196. // cost of the copy-to-texture-memory each frame. This
  197. // is not supported by all graphics hardware, but if it
  198. // is not supported, this option is quietly ignored.
  199. //
  200. // If to_ram is true, the texture image will be
  201. // downloaded from the framebuffer memory into system
  202. // RAM each frame, which is more expensive but allows
  203. // the texture to subsequently be applied to any GSG.
  204. // Otherwise, the texture image remains in texture
  205. // memory only.
  206. //
  207. // Also see make_texture_buffer(), which is a
  208. // higher-level interface for preparing
  209. // render-to-a-texture mode.
  210. ////////////////////////////////////////////////////////////////////
  211. void GraphicsOutput::
  212. setup_render_texture(Texture *tex, bool allow_bind, bool to_ram) {
  213. MutexHolder holder(_lock);
  214. if (_rtm_mode == RTM_bind_texture && _gsg != (GraphicsStateGuardian *)NULL) {
  215. _gsg->framebuffer_release_texture(this, get_texture());
  216. }
  217. if (tex == (Texture *)NULL) {
  218. _texture = new Texture(get_name());
  219. _texture->set_wrap_u(Texture::WM_clamp);
  220. _texture->set_wrap_v(Texture::WM_clamp);
  221. } else {
  222. _texture = tex;
  223. _texture->clear_ram_image();
  224. }
  225. _texture->set_match_framebuffer_format(true);
  226. // Go ahead and tell the texture our anticipated size, even if it
  227. // might be inaccurate (particularly if this is a GraphicsWindow,
  228. // which has system-imposed restrictions on size).
  229. _texture->set_x_size(get_x_size());
  230. _texture->set_y_size(get_y_size());
  231. if (to_ram) {
  232. _rtm_mode = RTM_copy_ram;
  233. } else if (allow_bind) {
  234. _rtm_mode = RTM_bind_if_possible;
  235. } else {
  236. _rtm_mode = RTM_copy_texture;
  237. }
  238. nassertv(_gsg != (GraphicsStateGuardian *)NULL);
  239. set_inverted(_gsg->get_copy_texture_inverted());
  240. }
  241. ////////////////////////////////////////////////////////////////////
  242. // Function: GraphicsOutput::set_active
  243. // Access: Published
  244. // Description: Sets the active flag associated with the
  245. // GraphicsOutput. If the GraphicsOutput is marked
  246. // inactive, nothing is rendered.
  247. ////////////////////////////////////////////////////////////////////
  248. void GraphicsOutput::
  249. set_active(bool active) {
  250. _active = active;
  251. }
  252. ////////////////////////////////////////////////////////////////////
  253. // Function: GraphicsOutput::is_active
  254. // Access: Published, Virtual
  255. // Description: Returns true if the window is ready to be rendered
  256. // into, false otherwise.
  257. ////////////////////////////////////////////////////////////////////
  258. bool GraphicsOutput::
  259. is_active() const {
  260. return _active && is_valid();
  261. }
  262. ////////////////////////////////////////////////////////////////////
  263. // Function: GraphicsOutput::set_inverted
  264. // Access: Published
  265. // Description: Changes the current setting of the inverted flag.
  266. // When this is true, the scene is rendered into the
  267. // window upside-down and backwards, that is, inverted
  268. // as if viewed through a mirror placed on the floor.
  269. //
  270. // This is primarily intended to support DirectX (and a
  271. // few buggy OpenGL graphics drivers) that perform a
  272. // framebuffer-to-texture copy upside-down from the
  273. // usual OpenGL (and Panda) convention. Panda will
  274. // automatically set this flag for offscreen buffers on
  275. // hardware that is known to do this, to compensate when
  276. // rendering offscreen into a texture.
  277. ////////////////////////////////////////////////////////////////////
  278. void GraphicsOutput::
  279. set_inverted(bool inverted) {
  280. if (_inverted != inverted) {
  281. _inverted = inverted;
  282. if (_y_size != 0) {
  283. // All of our DisplayRegions need to recompute their pixel
  284. // positions now.
  285. TotalDisplayRegions::iterator dri;
  286. for (dri = _total_display_regions.begin();
  287. dri != _total_display_regions.end();
  288. ++dri) {
  289. (*dri)->compute_pixels(_x_size, _y_size);
  290. }
  291. }
  292. }
  293. }
  294. ////////////////////////////////////////////////////////////////////
  295. // Function: GraphicsOutput::set_sort
  296. // Access: Published
  297. // Description: Adjusts the sorting order of this particular
  298. // GraphicsOutput, relative to other GraphicsOutputs.
  299. ////////////////////////////////////////////////////////////////////
  300. void GraphicsOutput::
  301. set_sort(int sort) {
  302. if (_sort != sort) {
  303. if (_gsg != (GraphicsStateGuardian *)NULL &&
  304. _gsg->get_engine() != (GraphicsEngine *)NULL) {
  305. _gsg->get_engine()->set_window_sort(this, sort);
  306. }
  307. }
  308. }
  309. ////////////////////////////////////////////////////////////////////
  310. // Function: GraphicsOutput::remove_display_region
  311. // Access: Published
  312. // Description: Removes the indicated DisplayRegion from the window,
  313. // and destructs it if there are no other references.
  314. //
  315. // Returns true if the DisplayRegion is found and
  316. // removed, false if it was not a part of the window.
  317. ////////////////////////////////////////////////////////////////////
  318. bool GraphicsOutput::
  319. remove_display_region(DisplayRegion *display_region) {
  320. MutexHolder holder(_lock);
  321. nassertr(display_region != _default_display_region, false);
  322. PT(DisplayRegion) drp = display_region;
  323. TotalDisplayRegions::iterator dri =
  324. find(_total_display_regions.begin(), _total_display_regions.end(), drp);
  325. if (dri != _total_display_regions.end()) {
  326. display_region->_window = NULL;
  327. _total_display_regions.erase(dri);
  328. if (display_region->is_active()) {
  329. _display_regions_stale = true;
  330. }
  331. return true;
  332. }
  333. return false;
  334. }
  335. ////////////////////////////////////////////////////////////////////
  336. // Function: GraphicsOutput::get_num_display_regions
  337. // Access: Published
  338. // Description: Returns the number of DisplayRegions that have
  339. // been created within the window, active or otherwise.
  340. ////////////////////////////////////////////////////////////////////
  341. int GraphicsOutput::
  342. get_num_display_regions() const {
  343. determine_display_regions();
  344. int result;
  345. {
  346. MutexHolder holder(_lock);
  347. result = _total_display_regions.size();
  348. }
  349. return result;
  350. }
  351. ////////////////////////////////////////////////////////////////////
  352. // Function: GraphicsOutput::get_display_region
  353. // Access: Published
  354. // Description: Returns the nth DisplayRegion of those that have been
  355. // created within the window. This may return NULL if n
  356. // is out of bounds; particularly likely if the number
  357. // of display regions has changed since the last call to
  358. // get_num_display_regions().
  359. ////////////////////////////////////////////////////////////////////
  360. PT(DisplayRegion) GraphicsOutput::
  361. get_display_region(int n) const {
  362. determine_display_regions();
  363. PT(DisplayRegion) result;
  364. {
  365. MutexHolder holder(_lock);
  366. if (n >= 0 && n < (int)_total_display_regions.size()) {
  367. result = _total_display_regions[n];
  368. } else {
  369. result = NULL;
  370. }
  371. }
  372. return result;
  373. }
  374. ////////////////////////////////////////////////////////////////////
  375. // Function: GraphicsOutput::get_num_active_display_regions
  376. // Access: Published
  377. // Description: Returns the number of active DisplayRegions that have
  378. // been created within the window.
  379. ////////////////////////////////////////////////////////////////////
  380. int GraphicsOutput::
  381. get_num_active_display_regions() const {
  382. determine_display_regions();
  383. int result;
  384. {
  385. MutexHolder holder(_lock);
  386. result = _active_display_regions.size();
  387. }
  388. return result;
  389. }
  390. ////////////////////////////////////////////////////////////////////
  391. // Function: GraphicsOutput::get_active_display_region
  392. // Access: Published
  393. // Description: Returns the nth active DisplayRegion of those that
  394. // have been created within the window. This may return
  395. // NULL if n is out of bounds; particularly likely if
  396. // the number of display regions has changed since the
  397. // last call to get_num_active_display_regions().
  398. ////////////////////////////////////////////////////////////////////
  399. PT(DisplayRegion) GraphicsOutput::
  400. get_active_display_region(int n) const {
  401. determine_display_regions();
  402. PT(DisplayRegion) result;
  403. {
  404. MutexHolder holder(_lock);
  405. if (n >= 0 && n < (int)_active_display_regions.size()) {
  406. result = _active_display_regions[n];
  407. } else {
  408. result = NULL;
  409. }
  410. }
  411. return result;
  412. }
  413. ////////////////////////////////////////////////////////////////////
  414. // Function: GraphicsOutput::make_texture_buffer
  415. // Access: Published
  416. // Description: Creates and returns an offscreen buffer for rendering
  417. // into, the result of which will be a texture suitable
  418. // for applying to geometry within the scene rendered
  419. // into this window.
  420. //
  421. // If tex is not NULL, it is the texture that will be
  422. // set up for rendering into; otherwise, a new Texture
  423. // object will be created. In either case, the target
  424. // texture can be retrieved from the return value with
  425. // buffer->get_texture() (assuming the return value is
  426. // not NULL).
  427. //
  428. // If to_ram is true, the buffer will be set up to
  429. // download its contents to the system RAM memory
  430. // associated with the Texture object, instead of
  431. // keeping it strictly within texture memory; this is
  432. // much slower, but it allows using the texture with any
  433. // GSG.
  434. //
  435. // This will attempt to be smart about maximizing render
  436. // performance while minimizing framebuffer waste. It
  437. // might return a GraphicsBuffer set to render directly
  438. // into a texture, if possible; or it might return a
  439. // ParasiteBuffer that renders into this window. The
  440. // return value is NULL if the buffer could not be
  441. // created for some reason.
  442. //
  443. // When you are done using the buffer, you should remove
  444. // it with a call to GraphicsEngine::remove_window() (or
  445. // set the one_shot flag so it removes itself after one
  446. // frame).
  447. ////////////////////////////////////////////////////////////////////
  448. GraphicsOutput *GraphicsOutput::
  449. make_texture_buffer(const string &name, int x_size, int y_size,
  450. Texture *tex, bool to_ram) {
  451. GraphicsStateGuardian *gsg = get_gsg();
  452. GraphicsEngine *engine = gsg->get_engine();
  453. GraphicsOutput *host = get_host();
  454. // The new buffer should be drawn before this buffer is drawn. If
  455. // the user requires more control than this, he can set the sort
  456. // value himself.
  457. int sort = get_sort() - 1;
  458. GraphicsOutput *buffer = NULL;
  459. if (show_buffers) {
  460. // If show_buffers is true, just go ahead and call make_buffer(),
  461. // since it all amounts to the same thing anyway--this will
  462. // actually create a new GraphicsWindow.
  463. buffer = engine->make_buffer(gsg, name, sort, x_size, y_size);
  464. buffer->setup_render_texture(tex, false, to_ram);
  465. return buffer;
  466. }
  467. bool allow_bind =
  468. (prefer_texture_buffer && gsg->get_supports_render_texture() && !to_ram);
  469. // If the user so indicated in the Config.prc file, try to create a
  470. // parasite buffer first. We can only do this if the requested size
  471. // fits within the available framebuffer size. Also, don't do this
  472. // if we want to try using render-to-a-texture mode, since using a
  473. // ParasiteButter will preclude that.
  474. if (prefer_parasite_buffer && !allow_bind &&
  475. (x_size <= host->get_x_size() && y_size <= host->get_y_size())) {
  476. buffer = engine->make_parasite(host, name, sort, x_size, y_size);
  477. if (buffer != (GraphicsOutput *)NULL) {
  478. buffer->setup_render_texture(tex, false, to_ram);
  479. return buffer;
  480. }
  481. }
  482. // Attempt to create a single-buffered offscreen buffer.
  483. if (prefer_single_buffer) {
  484. FrameBufferProperties sb_props = gsg->get_properties();
  485. int orig_mode = sb_props.get_frame_buffer_mode();
  486. int sb_mode = (orig_mode & ~FrameBufferProperties::FM_buffer) | FrameBufferProperties::FM_single_buffer;
  487. sb_props.set_frame_buffer_mode(sb_mode);
  488. if (sb_mode != orig_mode) {
  489. PT(GraphicsStateGuardian) sb_gsg =
  490. engine->make_gsg(gsg->get_pipe(), sb_props, gsg);
  491. if (sb_gsg != (GraphicsStateGuardian *)NULL) {
  492. buffer = engine->make_buffer(sb_gsg, name, sort, x_size, y_size);
  493. if (buffer != (GraphicsOutput *)NULL) {
  494. // Check the buffer for goodness.
  495. buffer->setup_render_texture(tex, allow_bind, to_ram);
  496. engine->open_windows();
  497. if (buffer->is_valid()) {
  498. return buffer;
  499. }
  500. // No good; delete the buffer and keep trying.
  501. bool removed = engine->remove_window(buffer);
  502. nassertr(removed, NULL);
  503. buffer = (GraphicsOutput *)NULL;
  504. }
  505. }
  506. }
  507. }
  508. // All right, attempt to create an offscreen buffer, using the same
  509. // GSG. This will be a double-buffered offscreen buffer, if the
  510. // source window is double-buffered.
  511. buffer = engine->make_buffer(gsg, name, sort, x_size, y_size);
  512. if (buffer != (GraphicsOutput *)NULL) {
  513. buffer->setup_render_texture(tex, allow_bind, to_ram);
  514. engine->open_windows();
  515. if (buffer->is_valid()) {
  516. return buffer;
  517. }
  518. bool removed = engine->remove_window(buffer);
  519. nassertr(removed, NULL);
  520. buffer = (GraphicsOutput *)NULL;
  521. }
  522. // Looks like we have to settle for a parasite buffer.
  523. if (x_size <= host->get_x_size() && y_size <= host->get_y_size()) {
  524. buffer = engine->make_parasite(host, name, sort, x_size, y_size);
  525. buffer->setup_render_texture(tex, false, to_ram);
  526. return buffer;
  527. }
  528. return NULL;
  529. }
  530. ////////////////////////////////////////////////////////////////////
  531. // Function: GraphicsOutput::make_cube_map
  532. // Access: Published
  533. // Description: This is similar to make_texture_buffer() in that it
  534. // allocates a separate buffer suitable for rendering to
  535. // a texture that can be assigned to geometry in this
  536. // window, but in this case, the buffer is set up to
  537. // render the six faces of a cube map.
  538. //
  539. // The buffer is automatically set up with six display
  540. // regions and six cameras, each of which are assigned
  541. // the indicated draw_mask and parented to the given
  542. // camera_rig node (which you should then put in your
  543. // scene to render the cube map from the appropriate
  544. // point of view).
  545. //
  546. // You may take the texture associated with the buffer
  547. // and apply it to geometry, particularly with
  548. // TexGenAttrib::M_world_cube_map also in effect, to
  549. // apply a reflection of everything seen by the camera
  550. // rig.
  551. ////////////////////////////////////////////////////////////////////
  552. GraphicsOutput *GraphicsOutput::
  553. make_cube_map(const string &name, int size, bool to_ram,
  554. NodePath &camera_rig, DrawMask camera_mask) {
  555. if (!to_ram) {
  556. // Check the limits imposed by the GSG. (However, if we're
  557. // rendering the texture to RAM only, these limits may be
  558. // irrelevant.)
  559. GraphicsStateGuardian *gsg = get_gsg();
  560. int max_dimension = gsg->get_max_cube_map_dimension();
  561. if (max_dimension == 0) {
  562. // The GSG doesn't support cube mapping; too bad for you.
  563. return NULL;
  564. }
  565. if (max_dimension > 0) {
  566. size = min(max_dimension, size);
  567. }
  568. }
  569. // Usually, we want the whole camera_rig to keep itself unrotated
  570. // with respect to the world coordinate space, so the user can apply
  571. // TexGenAttrib::M_world_cube_map to the objects on which the cube
  572. // map texture is applied. If for some reason the user doesn't want
  573. // this behavior, he can take this effect off again.
  574. camera_rig.node()->set_effect(CompassEffect::make(NodePath()));
  575. PT(Texture) tex = new Texture(name);
  576. tex->setup_cube_map();
  577. tex->set_wrap_u(Texture::WM_clamp);
  578. tex->set_wrap_v(Texture::WM_clamp);
  579. GraphicsOutput *buffer = make_texture_buffer(name, size, size, tex, to_ram);
  580. // We don't need to clear the overall buffer; instead, we'll clear
  581. // each display region.
  582. buffer->set_clear_color_active(false);
  583. buffer->set_clear_depth_active(false);
  584. PT(Lens) lens = new PerspectiveLens;
  585. lens->set_fov(90.0f);
  586. for (int i = 0; i < 6; i++) {
  587. PT(Camera) camera = new Camera(cube_faces[i]._name);
  588. camera->set_lens(lens);
  589. camera->set_camera_mask(camera_mask);
  590. NodePath camera_np = camera_rig.attach_new_node(camera);
  591. camera_np.look_at(cube_faces[i]._look_at, cube_faces[i]._up);
  592. DisplayRegion *dr = buffer->make_display_region();
  593. dr->set_cube_map_index(i);
  594. dr->copy_clear_settings(*this);
  595. dr->set_camera(camera_np);
  596. }
  597. return buffer;
  598. }
  599. ////////////////////////////////////////////////////////////////////
  600. // Function: GraphicsOutput::get_host
  601. // Access: Public, Virtual
  602. // Description: This is normally called only from within
  603. // make_texture_buffer(). When called on a
  604. // ParasiteBuffer, it returns the host of that buffer;
  605. // but when called on some other buffer, it returns the
  606. // buffer itself.
  607. ////////////////////////////////////////////////////////////////////
  608. GraphicsOutput *GraphicsOutput::
  609. get_host() {
  610. return this;
  611. }
  612. ////////////////////////////////////////////////////////////////////
  613. // Function: GraphicsOutput::request_open
  614. // Access: Public, Virtual
  615. // Description: This is called by the GraphicsEngine to request that
  616. // the window (or whatever) open itself or, in general,
  617. // make itself valid, at the next call to
  618. // process_events().
  619. ////////////////////////////////////////////////////////////////////
  620. void GraphicsOutput::
  621. request_open() {
  622. }
  623. ////////////////////////////////////////////////////////////////////
  624. // Function: GraphicsOutput::request_close
  625. // Access: Public, Virtual
  626. // Description: This is called by the GraphicsEngine to request that
  627. // the window (or whatever) close itself or, in general,
  628. // make itself invalid, at the next call to
  629. // process_events(). By that time we promise the gsg
  630. // pointer will be cleared.
  631. ////////////////////////////////////////////////////////////////////
  632. void GraphicsOutput::
  633. request_close() {
  634. }
  635. ////////////////////////////////////////////////////////////////////
  636. // Function: GraphicsOutput::set_close_now
  637. // Access: Public, Virtual
  638. // Description: This is called by the GraphicsEngine to insist that
  639. // the output be closed immediately. This is only
  640. // called from the window thread.
  641. ////////////////////////////////////////////////////////////////////
  642. void GraphicsOutput::
  643. set_close_now() {
  644. }
  645. ////////////////////////////////////////////////////////////////////
  646. // Function: GraphicsOutput::reset_window
  647. // Access: Protected, Virtual
  648. // Description: resets the window framebuffer from its derived
  649. // children. Does nothing here.
  650. ////////////////////////////////////////////////////////////////////
  651. void GraphicsOutput::
  652. reset_window(bool swapchain) {
  653. display_cat.info()
  654. << "Resetting " << get_type() << "\n";
  655. }
  656. ////////////////////////////////////////////////////////////////////
  657. // Function: GraphicsOutput::begin_frame
  658. // Access: Public, Virtual
  659. // Description: This function will be called within the draw thread
  660. // before beginning rendering for a given frame. It
  661. // should do whatever setup is required, and return true
  662. // if the frame should be rendered, or false if it
  663. // should be skipped.
  664. ////////////////////////////////////////////////////////////////////
  665. bool GraphicsOutput::
  666. begin_frame() {
  667. if (display_cat.is_spam()) {
  668. display_cat.spam()
  669. << "begin_frame(): " << get_type() << " "
  670. << get_name() << " " << (void *)this << "\n";
  671. }
  672. if (_gsg == (GraphicsStateGuardian *)NULL) {
  673. return false;
  674. }
  675. if (needs_context()) {
  676. if (!make_context()) {
  677. return false;
  678. }
  679. }
  680. // Okay, we already have a GSG, so activate it.
  681. make_current();
  682. if (_rtm_mode == RTM_bind_texture) {
  683. // Release the texture so we can render into the frame buffer.
  684. _gsg->framebuffer_release_texture(this, get_texture());
  685. }
  686. _cube_map_index = -1;
  687. return _gsg->begin_frame();
  688. }
  689. ////////////////////////////////////////////////////////////////////
  690. // Function: GraphicsOutput::clear
  691. // Access: Public
  692. // Description: Clears the entire framebuffer before rendering,
  693. // according to the settings of get_color_clear_active()
  694. // and get_depth_clear_active() (inherited from
  695. // DrawableRegion).
  696. //
  697. // This function is called only within the draw thread.
  698. ////////////////////////////////////////////////////////////////////
  699. void GraphicsOutput::
  700. clear() {
  701. if (is_any_clear_active()) {
  702. if (display_cat.is_spam()) {
  703. display_cat.spam()
  704. << "clear(): " << get_type() << " "
  705. << get_name() << " " << (void *)this << "\n";
  706. }
  707. nassertv(_gsg != (GraphicsStateGuardian *)NULL);
  708. DisplayRegionStack old_dr = _gsg->push_display_region(_default_display_region);
  709. _gsg->clear(this);
  710. _gsg->pop_display_region(old_dr);
  711. }
  712. }
  713. ////////////////////////////////////////////////////////////////////
  714. // Function: GraphicsOutput::end_frame
  715. // Access: Public, Virtual
  716. // Description: This function will be called within the draw thread
  717. // after rendering is completed for a given frame. It
  718. // should do whatever finalization is required.
  719. ////////////////////////////////////////////////////////////////////
  720. void GraphicsOutput::
  721. end_frame() {
  722. if (display_cat.is_spam()) {
  723. display_cat.spam()
  724. << "end_frame(): " << get_type() << " "
  725. << get_name() << " " << (void *)this << "\n";
  726. }
  727. nassertv(_gsg != (GraphicsStateGuardian *)NULL);
  728. _gsg->end_frame();
  729. // If _rtm_mode isn't RTM_none, it means we should copy or lock the
  730. // framebuffer to the GraphicsOutput's associated texture after the
  731. // frame has rendered.
  732. if (_rtm_mode != RTM_none) {
  733. PStatTimer timer(_copy_texture_pcollector);
  734. nassertv(has_texture());
  735. // If _rtm_mode is RTM_bind_texture, it means we should attempt to
  736. // lock the framebuffer directly to the texture memory, avoiding
  737. // the copy.
  738. if (_rtm_mode == RTM_bind_texture) {
  739. if (display_cat.is_debug()) {
  740. display_cat.debug()
  741. << "Locking texture for " << get_name() << " at frame end.\n";
  742. }
  743. if (!_gsg->framebuffer_bind_to_texture(this, get_texture())) {
  744. display_cat.warning()
  745. << "Lock-to-texture failed, resorting to copy.\n";
  746. _rtm_mode = RTM_copy_texture;
  747. }
  748. }
  749. if (_rtm_mode != RTM_bind_texture) {
  750. if (display_cat.is_debug()) {
  751. display_cat.debug()
  752. << "Copying texture for " << get_name() << " at frame end.\n";
  753. display_cat.debug()
  754. << "cube_map_index = " << _cube_map_index << "\n";
  755. }
  756. RenderBuffer buffer = _gsg->get_render_buffer(get_draw_buffer_type());
  757. if (_rtm_mode == RTM_copy_ram) {
  758. _gsg->framebuffer_copy_to_ram(get_texture(), _cube_map_index,
  759. _default_display_region, buffer);
  760. } else {
  761. _gsg->framebuffer_copy_to_texture(get_texture(), _cube_map_index,
  762. _default_display_region, buffer);
  763. }
  764. }
  765. }
  766. // If we're not single-buffered, we're now ready to flip.
  767. if (!_gsg->get_properties().is_single_buffered()) {
  768. _flip_ready = true;
  769. }
  770. if (_one_shot && !show_buffers) {
  771. // In one-shot mode, we request the GraphicsEngine to delete the
  772. // window after we have rendered a frame. But when show-buffers
  773. // mode is enabled, we don't do this, to give the user a chance to
  774. // see the output.
  775. _active = false;
  776. _delete_flag = true;
  777. }
  778. _cube_map_index = -1;
  779. }
  780. ////////////////////////////////////////////////////////////////////
  781. // Function: GraphicsOutput::change_scenes
  782. // Access: Public
  783. // Description: Called by the GraphicsEngine when the window is about
  784. // to change to another DisplayRegion. This exists
  785. // mainly to provide a callback for switching the cube
  786. // map face, if we are rendering to the different faces
  787. // of a cube map.
  788. ////////////////////////////////////////////////////////////////////
  789. void GraphicsOutput::
  790. change_scenes(DisplayRegion *new_dr) {
  791. int new_cube_map_index = new_dr->get_cube_map_index();
  792. if (new_cube_map_index != -1 &&
  793. new_cube_map_index != _cube_map_index) {
  794. int old_cube_map_index = _cube_map_index;
  795. _cube_map_index = new_cube_map_index;
  796. if (_rtm_mode != RTM_none) {
  797. if (_rtm_mode == RTM_bind_texture) {
  798. // In render-to-texture mode, switch the rendering backend to
  799. // the new cube map face, so that the subsequent frame will be
  800. // rendered to the new face.
  801. select_cube_map(new_cube_map_index);
  802. } else if (old_cube_map_index != -1) {
  803. // In copy-to-texture mode, copy the just-rendered framebuffer
  804. // to the old cube map face.
  805. if (display_cat.is_debug()) {
  806. display_cat.debug()
  807. << "Copying texture for " << get_name() << " at scene change.\n";
  808. display_cat.debug()
  809. << "cube_map_index = " << old_cube_map_index << "\n";
  810. }
  811. RenderBuffer buffer = _gsg->get_render_buffer(get_draw_buffer_type());
  812. if (_rtm_mode == RTM_copy_ram) {
  813. _gsg->framebuffer_copy_to_ram(get_texture(), old_cube_map_index,
  814. _default_display_region, buffer);
  815. } else {
  816. _gsg->framebuffer_copy_to_texture(get_texture(), old_cube_map_index,
  817. _default_display_region, buffer);
  818. }
  819. }
  820. }
  821. }
  822. }
  823. ////////////////////////////////////////////////////////////////////
  824. // Function: GraphicsOutput::select_cube_map
  825. // Access: Public, Virtual
  826. // Description: Called internally when the window is in
  827. // render-to-a-texture mode and we are in the process of
  828. // rendering the six faces of a cube map. This should
  829. // do whatever needs to be done to switch the buffer to
  830. // the indicated face.
  831. ////////////////////////////////////////////////////////////////////
  832. void GraphicsOutput::
  833. select_cube_map(int) {
  834. }
  835. ////////////////////////////////////////////////////////////////////
  836. // Function: GraphicsOutput::make_context
  837. // Access: Public, Virtual
  838. // Description: If _needs_context is true, this will be called
  839. // in the draw thread prior to rendering into the
  840. // window. It should attempt to create a graphics
  841. // context, and return true if successful, false
  842. // otherwise. If it returns false the window will be
  843. // considered failed.
  844. ////////////////////////////////////////////////////////////////////
  845. bool GraphicsOutput::
  846. make_context() {
  847. _needs_context = false;
  848. return true;
  849. }
  850. ////////////////////////////////////////////////////////////////////
  851. // Function: GraphicsOutput::make_current
  852. // Access: Public, Virtual
  853. // Description: This function will be called within the draw thread
  854. // during begin_frame() to ensure the graphics context
  855. // is ready for drawing.
  856. ////////////////////////////////////////////////////////////////////
  857. void GraphicsOutput::
  858. make_current() {
  859. }
  860. ////////////////////////////////////////////////////////////////////
  861. // Function: GraphicsOutput::release_gsg
  862. // Access: Public
  863. // Description: Releases the current GSG pointer, if it is currently
  864. // held, and resets the GSG to NULL. The window will be
  865. // permanently unable to render; this is normally called
  866. // only just before destroying the window. This should
  867. // only be called from within the draw thread.
  868. ////////////////////////////////////////////////////////////////////
  869. void GraphicsOutput::
  870. release_gsg() {
  871. _gsg.clear();
  872. }
  873. ////////////////////////////////////////////////////////////////////
  874. // Function: GraphicsOutput::begin_flip
  875. // Access: Public, Virtual
  876. // Description: This function will be called within the draw thread
  877. // after end_frame() has been called on all windows, to
  878. // initiate the exchange of the front and back buffers.
  879. //
  880. // This should instruct the window to prepare for the
  881. // flip at the next video sync, but it should not wait.
  882. //
  883. // We have the two separate functions, begin_flip() and
  884. // end_flip(), to make it easier to flip all of the
  885. // windows at the same time.
  886. ////////////////////////////////////////////////////////////////////
  887. void GraphicsOutput::
  888. begin_flip() {
  889. }
  890. ////////////////////////////////////////////////////////////////////
  891. // Function: GraphicsOutput::end_flip
  892. // Access: Public, Virtual
  893. // Description: This function will be called within the draw thread
  894. // after begin_flip() has been called on all windows, to
  895. // finish the exchange of the front and back buffers.
  896. //
  897. // This should cause the window to wait for the flip, if
  898. // necessary.
  899. ////////////////////////////////////////////////////////////////////
  900. void GraphicsOutput::
  901. end_flip() {
  902. _flip_ready = false;
  903. }
  904. ////////////////////////////////////////////////////////////////////
  905. // Function: GraphicsOutput::process_events
  906. // Access: Public, Virtual
  907. // Description: Do whatever processing in the window thread is
  908. // appropriate for this output object each frame.
  909. //
  910. // This function is called only within the window
  911. // thread.
  912. ////////////////////////////////////////////////////////////////////
  913. void GraphicsOutput::
  914. process_events() {
  915. }
  916. ////////////////////////////////////////////////////////////////////
  917. // Function: GraphicsOutput::add_display_region
  918. // Access: Private
  919. // Description: Called by one of the make_display_region() methods to
  920. // add the new DisplayRegion to the list.
  921. ////////////////////////////////////////////////////////////////////
  922. DisplayRegion *GraphicsOutput::
  923. add_display_region(DisplayRegion *display_region) {
  924. MutexHolder holder(_lock);
  925. _total_display_regions.push_back(display_region);
  926. _display_regions_stale = true;
  927. return display_region;
  928. }
  929. ////////////////////////////////////////////////////////////////////
  930. // Function: GraphicsOutput::do_determine_display_regions
  931. // Access: Private
  932. // Description: Re-sorts the list of active DisplayRegions within
  933. // the window.
  934. ////////////////////////////////////////////////////////////////////
  935. void GraphicsOutput::
  936. do_determine_display_regions() {
  937. MutexHolder holder(_lock);
  938. _display_regions_stale = false;
  939. _active_display_regions.clear();
  940. _active_display_regions.reserve(_total_display_regions.size());
  941. TotalDisplayRegions::const_iterator dri;
  942. for (dri = _total_display_regions.begin();
  943. dri != _total_display_regions.end();
  944. ++dri) {
  945. DisplayRegion *display_region = (*dri);
  946. if (display_region->is_active()) {
  947. _active_display_regions.push_back(display_region);
  948. }
  949. }
  950. stable_sort(_active_display_regions.begin(), _active_display_regions.end(), IndirectLess<DisplayRegion>());
  951. }