graphicsEngine.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Filename: graphicsEngine.h
  2. // Created by: drose (24Feb02)
  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. #ifndef GRAPHICSENGINE_H
  19. #define GRAPHICSENGINE_H
  20. #include "pandabase.h"
  21. #include "graphicsWindow.h"
  22. #include "graphicsBuffer.h"
  23. #include "frameBufferProperties.h"
  24. #include "graphicsThreadingModel.h"
  25. #include "sceneSetup.h"
  26. #include "pointerTo.h"
  27. #include "thread.h"
  28. #include "pmutex.h"
  29. #include "conditionVar.h"
  30. #include "pStatCollector.h"
  31. #include "pset.h"
  32. #include "ordered_vector.h"
  33. #include "indirectLess.h"
  34. class Pipeline;
  35. class DisplayRegion;
  36. class GraphicsPipe;
  37. class FrameBufferProperties;
  38. class Texture;
  39. ////////////////////////////////////////////////////////////////////
  40. // Class : GraphicsEngine
  41. // Description : This class is the main interface to controlling the
  42. // render process. There is typically only one
  43. // GraphicsEngine in an application, and it synchronizes
  44. // rendering to all all of the active windows; although
  45. // it is possible to have multiple GraphicsEngine
  46. // objects if multiple synchronicity groups are
  47. // required.
  48. //
  49. // The GraphicsEngine is responsible for managing the
  50. // various cull and draw threads. The application
  51. // simply calls engine->render_frame() and considers it
  52. // done.
  53. ////////////////////////////////////////////////////////////////////
  54. class EXPCL_PANDA GraphicsEngine {
  55. PUBLISHED:
  56. GraphicsEngine(Pipeline *pipeline = NULL);
  57. ~GraphicsEngine();
  58. void set_frame_buffer_properties(const FrameBufferProperties &properties);
  59. FrameBufferProperties get_frame_buffer_properties() const;
  60. void set_threading_model(const GraphicsThreadingModel &threading_model);
  61. GraphicsThreadingModel get_threading_model() const;
  62. INLINE void set_auto_flip(bool auto_flip);
  63. INLINE bool get_auto_flip() const;
  64. INLINE void set_portal_cull(bool value);
  65. INLINE bool get_portal_cull() const;
  66. INLINE PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe);
  67. PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe,
  68. const FrameBufferProperties &properties,
  69. GraphicsStateGuardian *share_with = NULL);
  70. GraphicsWindow *make_window(GraphicsStateGuardian *gsg, const string &name,
  71. int sort);
  72. GraphicsOutput *make_buffer(GraphicsStateGuardian *gsg, const string &name,
  73. int sort, int x_size, int y_size);
  74. GraphicsOutput *make_parasite(GraphicsOutput *host, const string &name,
  75. int sort, int x_size, int y_size);
  76. bool remove_window(GraphicsOutput *window);
  77. void remove_all_windows();
  78. void reset_all_windows(bool swapchain);
  79. bool is_empty() const;
  80. int get_num_windows() const;
  81. GraphicsOutput *get_window(int n) const;
  82. void render_frame();
  83. void open_windows();
  84. void sync_frame();
  85. void flip_frame();
  86. void render_subframe(GraphicsOutput *win, DisplayRegion *dr,
  87. bool cull_sorting);
  88. public:
  89. enum ThreadState {
  90. TS_wait,
  91. TS_do_frame,
  92. TS_do_flip,
  93. TS_do_release,
  94. TS_do_windows,
  95. TS_terminate
  96. };
  97. enum CallbackTime {
  98. CB_pre_frame,
  99. CB_post_frame,
  100. CB_len // Not an option; just indicates the size of the list.
  101. };
  102. typedef void CallbackFunction(void *data);
  103. bool add_callback(const string &thread_name, CallbackTime callback_time,
  104. CallbackFunction *func, void *data);
  105. bool remove_callback(const string &thread_name, CallbackTime callback_time,
  106. CallbackFunction *func, void *data);
  107. private:
  108. class Callback {
  109. public:
  110. INLINE Callback(CallbackFunction *func, void *data);
  111. INLINE bool operator < (const Callback &other) const;
  112. INLINE void do_callback() const;
  113. private:
  114. CallbackFunction *_func;
  115. void *_data;
  116. };
  117. typedef ov_set< PT(GraphicsOutput), IndirectLess<GraphicsOutput> > Windows;
  118. typedef pset< PT(GraphicsStateGuardian) > GSGs;
  119. typedef pset< Callback > Callbacks;
  120. void set_window_sort(GraphicsOutput *window, int sort);
  121. void cull_and_draw_together(const Windows &wlist);
  122. void cull_and_draw_together(GraphicsOutput *win, DisplayRegion *dr);
  123. void cull_bin_draw(const Windows &wlist);
  124. void cull_bin_draw(GraphicsOutput *win, DisplayRegion *dr);
  125. void make_contexts(const Windows &wlist);
  126. void process_events(const Windows &wlist);
  127. void flip_windows(const Windows &wlist);
  128. void do_sync_frame();
  129. void do_flip_frame();
  130. INLINE void close_gsg(GraphicsPipe *pipe, GraphicsStateGuardian *gsg);
  131. PT(SceneSetup) setup_scene(GraphicsStateGuardian *gsg, DisplayRegion *dr);
  132. void do_cull(CullHandler *cull_handler, SceneSetup *scene_setup,
  133. GraphicsStateGuardian *gsg);
  134. void do_draw(CullResult *cull_result, SceneSetup *scene_setup,
  135. GraphicsOutput *win, DisplayRegion *dr);
  136. bool setup_gsg(GraphicsStateGuardian *gsg, SceneSetup *scene_setup);
  137. void do_add_window(GraphicsOutput *window, GraphicsStateGuardian *gsg,
  138. const GraphicsThreadingModel &threading_model);
  139. void do_remove_window(GraphicsOutput *window);
  140. void do_resort_windows();
  141. void terminate_threads();
  142. static const RenderState *get_invert_polygon_state();
  143. // The WindowRenderer class records the stages of the pipeline that
  144. // each thread (including the main thread, a.k.a. "app") should
  145. // process, and the list of windows for each stage.
  146. class WindowRenderer {
  147. public:
  148. void add_gsg(GraphicsStateGuardian *gsg);
  149. void add_window(Windows &wlist, GraphicsOutput *window);
  150. void remove_window(GraphicsOutput *window);
  151. void resort_windows();
  152. void do_frame(GraphicsEngine *engine);
  153. void do_windows(GraphicsEngine *engine);
  154. void do_flip(GraphicsEngine *engine);
  155. void do_release(GraphicsEngine *engine);
  156. void do_close(GraphicsEngine *engine);
  157. void do_pending(GraphicsEngine *engine);
  158. bool any_done_gsgs() const;
  159. bool add_callback(CallbackTime callback_time, const Callback &callback);
  160. bool remove_callback(CallbackTime callback_time, const Callback &callback);
  161. private:
  162. void do_callbacks(CallbackTime callback_time);
  163. public:
  164. Windows _cull; // cull stage
  165. Windows _cdraw; // cull-and-draw-together stage
  166. Windows _draw; // draw stage
  167. Windows _window; // window stage, i.e. process windowing events
  168. // These two are not kept sorted.
  169. Windows _pending_release; // moved from _draw, pending release_gsg.
  170. Windows _pending_close; // moved from _window, pending close.
  171. GSGs _gsgs; // draw stage
  172. Callbacks _callbacks[CB_len];
  173. Mutex _wl_lock;
  174. };
  175. class RenderThread : public Thread, public WindowRenderer {
  176. public:
  177. RenderThread(const string &name, GraphicsEngine *engine);
  178. virtual void thread_main();
  179. GraphicsEngine *_engine;
  180. Mutex _cv_mutex;
  181. ConditionVar _cv;
  182. ThreadState _thread_state;
  183. };
  184. WindowRenderer *get_window_renderer(const string &name);
  185. Pipeline *_pipeline;
  186. Windows _windows;
  187. bool _windows_sorted;
  188. unsigned int _window_sort_index;
  189. WindowRenderer _app;
  190. typedef pmap<string, PT(RenderThread) > Threads;
  191. Threads _threads;
  192. FrameBufferProperties _frame_buffer_properties;
  193. GraphicsThreadingModel _threading_model;
  194. bool _auto_flip;
  195. bool _portal_enabled; //toggle to portal culling on/off
  196. enum FlipState {
  197. FS_draw, // Still drawing.
  198. FS_sync, // All windows are done drawing.
  199. FS_flip, // All windows are done drawing and have flipped.
  200. };
  201. FlipState _flip_state;
  202. Mutex _lock;
  203. static PStatCollector _app_pcollector;
  204. static PStatCollector _yield_pcollector;
  205. static PStatCollector _cull_pcollector;
  206. static PStatCollector _draw_pcollector;
  207. static PStatCollector _sync_pcollector;
  208. static PStatCollector _flip_pcollector;
  209. static PStatCollector _flip_begin_pcollector;
  210. static PStatCollector _flip_end_pcollector;
  211. static PStatCollector _transform_states_pcollector;
  212. static PStatCollector _transform_states_unused_pcollector;
  213. static PStatCollector _render_states_pcollector;
  214. static PStatCollector _render_states_unused_pcollector;
  215. friend class WindowRenderer;
  216. friend class GraphicsOutput;
  217. };
  218. #include "graphicsEngine.I"
  219. #endif