00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GRAPHICSENGINE_H
00020 #define GRAPHICSENGINE_H
00021
00022 #include "pandabase.h"
00023 #include "graphicsWindow.h"
00024 #include "frameBufferProperties.h"
00025 #include "graphicsThreadingModel.h"
00026 #include "sceneSetup.h"
00027 #include "pointerTo.h"
00028 #include "thread.h"
00029 #include "pmutex.h"
00030 #include "conditionVar.h"
00031 #include "pset.h"
00032 #include "pStatCollector.h"
00033
00034 class Pipeline;
00035 class DisplayRegion;
00036 class GraphicsPipe;
00037 class FrameBufferProperties;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 class EXPCL_PANDA GraphicsEngine {
00055 PUBLISHED:
00056 GraphicsEngine(Pipeline *pipeline = NULL);
00057 ~GraphicsEngine();
00058
00059 void set_frame_buffer_properties(const FrameBufferProperties &properties);
00060 FrameBufferProperties get_frame_buffer_properties() const;
00061
00062 void set_threading_model(const GraphicsThreadingModel &threading_model);
00063 GraphicsThreadingModel get_threading_model() const;
00064
00065 INLINE void set_auto_flip(bool auto_flip);
00066 INLINE bool get_auto_flip() const;
00067
00068 INLINE PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe);
00069 PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe,
00070 const FrameBufferProperties &properties,
00071 const GraphicsThreadingModel &threading_model);
00072
00073 INLINE GraphicsWindow *make_window(GraphicsPipe *pipe,
00074 GraphicsStateGuardian *gsg);
00075 GraphicsWindow *make_window(GraphicsPipe *pipe,
00076 GraphicsStateGuardian *gsg,
00077 const GraphicsThreadingModel &threading_model);
00078 bool remove_window(GraphicsWindow *window);
00079 void remove_all_windows();
00080 bool is_empty() const;
00081
00082 void render_frame();
00083 void sync_frame();
00084 void flip_frame();
00085
00086 void render_subframe(GraphicsStateGuardian *gsg, DisplayRegion *dr,
00087 bool cull_sorting);
00088
00089 private:
00090 typedef pset< PT(GraphicsWindow) > Windows;
00091 typedef pset< PT(GraphicsStateGuardian) > GSGs;
00092
00093 void cull_and_draw_together(const Windows &wlist);
00094 void cull_and_draw_together(GraphicsStateGuardian *gsg, DisplayRegion *dr);
00095
00096 void cull_bin_draw(const Windows &wlist);
00097 void cull_bin_draw(GraphicsStateGuardian *gsg, DisplayRegion *dr);
00098
00099 void process_events(const GraphicsEngine::Windows &wlist);
00100 void flip_windows(const GraphicsEngine::Windows &wlist);
00101 void do_sync_frame();
00102 void do_flip_frame();
00103 INLINE void close_gsg(GraphicsPipe *pipe, GraphicsStateGuardian *gsg);
00104
00105 PT(SceneSetup) setup_scene(const NodePath &camera,
00106 GraphicsStateGuardian *gsg);
00107 void do_cull(CullHandler *cull_handler, SceneSetup *scene_setup,
00108 GraphicsStateGuardian *gsg);
00109 void do_draw(CullResult *cull_result, SceneSetup *scene_setup,
00110 GraphicsStateGuardian *gsg, DisplayRegion *dr);
00111
00112 bool setup_gsg(GraphicsStateGuardian *gsg, SceneSetup *scene_setup);
00113
00114 void do_remove_window(GraphicsWindow *window);
00115 void terminate_threads();
00116
00117
00118
00119
00120 class WindowRenderer {
00121 public:
00122 void add_gsg(GraphicsStateGuardian *gsg);
00123 void add_window(Windows &wlist, GraphicsWindow *window);
00124 void remove_window(GraphicsWindow *window);
00125 void do_frame(GraphicsEngine *engine);
00126 void do_flip(GraphicsEngine *engine);
00127 void do_release(GraphicsEngine *engine);
00128 void do_close(GraphicsEngine *engine);
00129 void do_pending(GraphicsEngine *engine);
00130 bool any_done_gsgs() const;
00131
00132 Windows _cull;
00133 Windows _cdraw;
00134 Windows _draw;
00135 Windows _window;
00136 Windows _pending_release;
00137 Windows _pending_close;
00138 GSGs _gsgs;
00139 Mutex _wl_lock;
00140 };
00141
00142 enum ThreadState {
00143 TS_wait,
00144 TS_do_frame,
00145 TS_do_flip,
00146 TS_do_release,
00147 TS_terminate
00148 };
00149
00150 class RenderThread : public Thread, public WindowRenderer {
00151 public:
00152 RenderThread(const string &name, GraphicsEngine *engine);
00153 virtual void thread_main();
00154
00155 GraphicsEngine *_engine;
00156 Mutex _cv_mutex;
00157 ConditionVar _cv;
00158 ThreadState _thread_state;
00159 };
00160
00161 WindowRenderer *get_window_renderer(const string &name);
00162
00163 Pipeline *_pipeline;
00164 Windows _windows;
00165
00166 WindowRenderer _app;
00167 typedef pmap<string, PT(RenderThread) > Threads;
00168 Threads _threads;
00169 FrameBufferProperties _frame_buffer_properties;
00170 GraphicsThreadingModel _threading_model;
00171 bool _auto_flip;
00172
00173 enum FlipState {
00174 FS_draw,
00175 FS_sync,
00176 FS_flip,
00177 };
00178 FlipState _flip_state;
00179 Mutex _lock;
00180
00181 static PStatCollector _cull_pcollector;
00182 static PStatCollector _draw_pcollector;
00183 friend class WindowRenderer;
00184 };
00185
00186 #include "graphicsEngine.I"
00187
00188 #endif
00189