Panda3D
 All Classes Functions Variables Enumerations
graphicsOutput.h
1 // Filename: graphicsOutput.h
2 // Created by: drose (06Feb04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef GRAPHICSOUTPUT_H
16 #define GRAPHICSOUTPUT_H
17 
18 #include "pandabase.h"
19 
20 #include "graphicsPipe.h"
21 #include "displayRegion.h"
22 #include "stereoDisplayRegion.h"
23 #include "graphicsStateGuardian.h"
24 #include "drawableRegion.h"
25 #include "renderBuffer.h"
26 #include "graphicsOutputBase.h"
27 #include "luse.h"
28 #include "typedWritableReferenceCount.h"
29 #include "pandaNode.h"
30 #include "pStatCollector.h"
31 #include "pnotify.h"
32 #include "lightMutex.h"
33 #include "filename.h"
34 #include "drawMask.h"
35 #include "pvector.h"
36 #include "weakPointerTo.h"
37 #include "nodePath.h"
38 #include "cycleData.h"
39 #include "cycleDataLockedReader.h"
40 #include "cycleDataReader.h"
41 #include "cycleDataWriter.h"
42 #include "pipelineCycler.h"
43 #include "updateSeq.h"
44 
45 class PNMImage;
46 class GraphicsEngine;
47 
48 ////////////////////////////////////////////////////////////////////
49 // Class : GraphicsOutput
50 // Description : This is a base class for the various different
51 // classes that represent the result of a frame of
52 // rendering. The most common kind of GraphicsOutput is
53 // a GraphicsWindow, which is a real-time window on the
54 // desktop, but another example is GraphicsBuffer, which
55 // is an offscreen buffer.
56 //
57 // The actual rendering, and anything associated with
58 // the graphics context itself, is managed by the
59 // associated GraphicsStateGuardian (which might output
60 // to multiple GraphicsOutput objects).
61 //
62 // GraphicsOutputs are not actually writable to bam
63 // files, of course, but they may be passed as event
64 // parameters, so they inherit from
65 // TypedWritableReferenceCount instead of
66 // TypedReferenceCount for that convenience.
67 ////////////////////////////////////////////////////////////////////
68 class EXPCL_PANDA_DISPLAY GraphicsOutput : public GraphicsOutputBase, public DrawableRegion {
69 protected:
71  GraphicsPipe *pipe,
72  const string &name,
73  const FrameBufferProperties &fb_prop,
74  const WindowProperties &win_prop, int flags,
76  GraphicsOutput *host,
77  bool default_stereo_flags);
78 
79 private:
80  GraphicsOutput(const GraphicsOutput &copy);
81  void operator = (const GraphicsOutput &copy);
82 
83 PUBLISHED:
84  enum RenderTextureMode {
85  RTM_none,
86 
87  // Try to render to the texture directly, but if that is
88  // not possible, fall back to RTM_copy_texture.
89  RTM_bind_or_copy,
90 
91  // Copy the image from the buffer to the texture every frame.
92  RTM_copy_texture,
93 
94  // Copy the image from the buffer to system RAM every frame.
95  RTM_copy_ram,
96 
97  // Copy the image from the buffer to the texture after a
98  // call to trigger_copy().
99  RTM_triggered_copy_texture,
100 
101  // Copy the image from the buffer to system RAM after a
102  // call to trigger_copy().
103  RTM_triggered_copy_ram,
104 
105  // Render directly to a layered texture, such as a cube map,
106  // 3D texture or 2D texture array. The layer that is being
107  // rendered to is selected by a geometry shader.
108  RTM_bind_layered,
109  };
110 
111  // There are many reasons to call begin_frame/end_frame.
112  enum FrameMode {
113  FM_render, // We are rendering a frame.
114  FM_parasite, // We are rendering a frame of a parasite.
115  FM_refresh, // We are just refreshing the display or exposing the window.
116  };
117 
118  virtual ~GraphicsOutput();
119 
120  INLINE GraphicsStateGuardian *get_gsg() const;
121  INLINE GraphicsPipe *get_pipe() const;
122  INLINE GraphicsEngine *get_engine() const;
123  INLINE const string &get_name() const;
124 
125  INLINE int count_textures() const;
126  INLINE bool has_texture() const;
127  virtual INLINE Texture *get_texture(int i=0) const;
128  INLINE RenderTexturePlane get_texture_plane(int i=0) const;
129  INLINE RenderTextureMode get_rtm_mode(int i=0) const;
130  void clear_render_textures();
131  void add_render_texture(Texture *tex, RenderTextureMode mode,
132  RenderTexturePlane bitplane=RTP_COUNT);
133  void setup_render_texture(Texture *tex, bool allow_bind, bool to_ram);
134 
135  INLINE const LVecBase2i &get_size() const;
136  INLINE int get_x_size() const;
137  INLINE int get_y_size() const;
138  INLINE LVecBase2i get_fb_size() const;
139  INLINE int get_fb_x_size() const;
140  INLINE int get_fb_y_size() const;
141  INLINE LVecBase2i get_sbs_left_size() const;
142  INLINE int get_sbs_left_x_size() const;
143  INLINE int get_sbs_left_y_size() const;
144  INLINE LVecBase2i get_sbs_right_size() const;
145  INLINE int get_sbs_right_x_size() const;
146  INLINE int get_sbs_right_y_size() const;
147  INLINE bool has_size() const;
148  INLINE bool is_valid() const;
149  INLINE bool is_nonzero_size() const;
150 
151  void set_active(bool active);
152  virtual bool is_active() const;
153 
154  void set_one_shot(bool one_shot);
155  bool get_one_shot() const;
156 
157  void set_inverted(bool inverted);
158  INLINE bool get_inverted() const;
159 
160  INLINE void set_swap_eyes(bool swap_eyes);
161  INLINE bool get_swap_eyes() const;
162 
163  INLINE void set_red_blue_stereo(bool red_blue_stereo,
164  unsigned int left_eye_color_mask,
165  unsigned int right_eye_color_mask);
166  INLINE bool get_red_blue_stereo() const;
167  INLINE unsigned int get_left_eye_color_mask() const;
168  INLINE unsigned int get_right_eye_color_mask() const;
169 
170  void set_side_by_side_stereo(bool side_by_side_stereo);
171  void set_side_by_side_stereo(bool side_by_side_stereo,
172  const LVecBase4 &sbs_left_dimensions,
173  const LVecBase4 &sbs_right_dimensions);
174  INLINE bool get_side_by_side_stereo() const;
175  INLINE const LVecBase4 &get_sbs_left_dimensions() const;
176  INLINE const LVecBase4 &get_sbs_right_dimensions() const;
177 
178  INLINE const FrameBufferProperties &get_fb_properties() const;
179  INLINE bool is_stereo() const;
180 
181  INLINE void clear_delete_flag();
182  bool get_delete_flag() const;
183 
184  virtual void set_sort(int sort);
185  INLINE int get_sort() const;
186 
187  INLINE void set_child_sort(int child_sort);
188  INLINE void clear_child_sort();
189  INLINE int get_child_sort() const;
190 
191  INLINE void trigger_copy();
192 
193  INLINE DisplayRegion *make_display_region();
194  INLINE DisplayRegion *make_display_region(PN_stdfloat l, PN_stdfloat r, PN_stdfloat b, PN_stdfloat t);
195  DisplayRegion *make_display_region(const LVecBase4 &dimensions);
196  INLINE DisplayRegion *make_mono_display_region();
197  INLINE DisplayRegion *make_mono_display_region(PN_stdfloat l, PN_stdfloat r, PN_stdfloat b, PN_stdfloat t);
198  DisplayRegion *make_mono_display_region(const LVecBase4 &dimensions);
199  INLINE StereoDisplayRegion *make_stereo_display_region();
200  INLINE StereoDisplayRegion *make_stereo_display_region(PN_stdfloat l, PN_stdfloat r, PN_stdfloat b, PN_stdfloat t);
201  StereoDisplayRegion *make_stereo_display_region(const LVecBase4 &dimensions);
202  bool remove_display_region(DisplayRegion *display_region);
203  void remove_all_display_regions();
204 
205  INLINE DisplayRegion *get_overlay_display_region() const;
206  void set_overlay_display_region(DisplayRegion *display_region);
207 
208  int get_num_display_regions() const;
209  PT(DisplayRegion) get_display_region(int n) const;
210  MAKE_SEQ(get_display_regions, get_num_display_regions, get_display_region);
211 
212  int get_num_active_display_regions() const;
213  PT(DisplayRegion) get_active_display_region(int n) const;
214  MAKE_SEQ(get_active_display_regions, get_num_active_display_regions, get_active_display_region);
215 
216  GraphicsOutput *make_texture_buffer(
217  const string &name, int x_size, int y_size,
218  Texture *tex = NULL, bool to_ram = false, FrameBufferProperties *fbp = NULL);
219  GraphicsOutput *make_cube_map(const string &name, int size,
220  NodePath &camera_rig,
222  bool to_ram = false, FrameBufferProperties *fbp = NULL);
223 
224  INLINE static Filename make_screenshot_filename(
225  const string &prefix = "screenshot");
226  INLINE Filename save_screenshot_default(
227  const string &prefix = "screenshot");
228  INLINE bool save_screenshot(
229  const Filename &filename, const string &image_comment = "");
230  INLINE bool get_screenshot(PNMImage &image);
231  INLINE PT(Texture) get_screenshot();
232 
233  NodePath get_texture_card();
234 
235  virtual bool share_depth_buffer(GraphicsOutput *graphics_output);
236  virtual void unshare_depth_buffer();
237 
238  virtual bool get_supports_render_texture() const;
239 
240 PUBLISHED:
241  // These are not intended to be called directly by the user, but
242  // they're published anyway since they might occasionally be useful
243  // for low-level debugging.
244  virtual bool flip_ready() const;
245  virtual GraphicsOutput *get_host();
246 
247 public:
248  INLINE bool operator < (const GraphicsOutput &other) const;
249 
250  virtual void request_open();
251  virtual void request_close();
252 
253  virtual void set_close_now();
254  virtual void reset_window(bool swapchain);
255  virtual void clear_pipe();
256 
257  void set_size_and_recalc(int x, int y);
258 
259  // It is an error to call any of the following methods from any
260  // thread other than the draw thread. These methods are normally
261  // called by the GraphicsEngine.
262  void clear(Thread *current_thread);
263  virtual bool begin_frame(FrameMode mode, Thread *current_thread);
264  virtual void end_frame(FrameMode mode, Thread *current_thread);
265 
266  void change_scenes(DisplayRegionPipelineReader *new_dr);
267  virtual void select_target_tex_page(int page);
268 
269  // These methods will be called within the app (main) thread.
270  virtual void begin_flip();
271  virtual void ready_flip();
272  virtual void end_flip();
273 
274  // It is an error to call any of the following methods from any
275  // thread other than the window thread. These methods are normally
276  // called by the GraphicsEngine.
277  virtual void process_events();
278 
279  INLINE PStatCollector &get_cull_window_pcollector();
280  INLINE PStatCollector &get_draw_window_pcollector();
281 
282 protected:
283  virtual void pixel_factor_changed();
284  void prepare_for_deletion();
285  void promote_to_copy_texture();
286  bool copy_to_textures();
287 
288  INLINE void begin_frame_spam(FrameMode mode);
289  INLINE void end_frame_spam(FrameMode mode);
290  INLINE void clear_cube_map_selection();
291  INLINE void trigger_flip();
292 
293  class CData;
294 
295 private:
296  PT(GeomVertexData) create_texture_card_vdata(int x, int y);
297 
298  DisplayRegion *add_display_region(DisplayRegion *display_region);
299  bool do_remove_display_region(DisplayRegion *display_region);
300 
301  INLINE void win_display_regions_changed();
302 
303  INLINE void determine_display_regions() const;
304  void do_determine_display_regions(CData *cdata);
305 
306  static unsigned int parse_color_mask(const string &word);
307 
308 protected:
309  PT(GraphicsStateGuardian) _gsg;
310  GraphicsEngine *_engine;
311  PT(GraphicsPipe) _pipe;
312  PT(GraphicsOutput) _host;
313  FrameBufferProperties _fb_properties;
314  bool _stereo;
315  string _name;
316  bool _flip_ready;
317  int _target_tex_page;
318  int _target_tex_view;
319  DisplayRegion *_prev_page_dr;
320  PT(GeomNode) _texture_card;
321  bool _trigger_copy;
322 
323  class RenderTexture {
324  public:
325  PT(Texture) _texture;
326  RenderTexturePlane _plane;
327  RenderTextureMode _rtm_mode;
328  };
330 
331 private:
332  int _sort;
333  int _child_sort;
334  bool _got_child_sort;
335  unsigned int _internal_sort_index;
336 
337 protected:
338  bool _inverted;
339  bool _swap_eyes;
340  bool _red_blue_stereo;
341  unsigned int _left_eye_color_mask;
342  unsigned int _right_eye_color_mask;
343  bool _side_by_side_stereo;
344  LVecBase4 _sbs_left_dimensions;
345  LVecBase4 _sbs_right_dimensions;
346  bool _delete_flag;
347 
348  // These weak pointers are used to keep track of whether the
349  // buffer's bound Textures have been deleted or not. Until they
350  // have, we don't auto-close the buffer (since that would deallocate
351  // the memory associated with the texture).
352  pvector<WPT(Texture)> _hold_textures;
353 
354 protected:
355  LightMutex _lock;
356  // protects _display_regions.
357  PT(DisplayRegion) _overlay_display_region;
359  TotalDisplayRegions _total_display_regions;
361 
362  // This is the data that is associated with the GraphicsOutput that
363  // needs to be cycled every frame. Mostly we don't cycle this data,
364  // but we do cycle the textures list, and the active flag.
365  class EXPCL_PANDA_DISPLAY CData : public CycleData {
366  public:
367  CData();
368  CData(const CData &copy);
369 
370  virtual CycleData *make_copy() const;
371  virtual TypeHandle get_parent_type() const {
372  return GraphicsOutput::get_class_type();
373  }
374 
375  RenderTextures _textures;
376  UpdateSeq _textures_seq;
377  bool _active;
378  int _one_shot_frame;
379  ActiveDisplayRegions _active_display_regions;
380  bool _active_display_regions_stale;
381  };
382  PipelineCycler<CData> _cycler;
386 
387 protected:
388  int _creation_flags;
389  LVecBase2i _size;
390  bool _has_size;
391  bool _is_valid;
392  bool _is_nonzero_size;
393 
394  static PStatCollector _make_current_pcollector;
395  static PStatCollector _copy_texture_pcollector;
396  static PStatCollector _cull_pcollector;
397  static PStatCollector _draw_pcollector;
398  PStatCollector _cull_window_pcollector;
399  PStatCollector _draw_window_pcollector;
400 
401 public:
402  static TypeHandle get_class_type() {
403  return _type_handle;
404  }
405  static void init_type() {
406  GraphicsOutputBase::init_type();
407  register_type(_type_handle, "GraphicsOutput",
408  GraphicsOutputBase::get_class_type());
409  }
410  virtual TypeHandle get_type() const {
411  return get_class_type();
412  }
413  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
414 
415 private:
416  static TypeHandle _type_handle;
417 
418  friend class GraphicsPipe;
419  friend class GraphicsEngine;
420  friend class DisplayRegion;
421 };
422 
423 EXPCL_PANDA_DISPLAY ostream &operator << (ostream &out, GraphicsOutput::FrameMode mode);
424 
425 #include "graphicsOutput.I"
426 
427 #endif /* GRAPHICSOUTPUT_H */
Encapsulates the data from a DisplayRegion, pre-fetched for one stage of the pipeline.
This is a base class for GraphicsWindow (actually, GraphicsOutput) and DisplayRegion, both of which are conceptually rectangular regions into which drawing commands may be issued.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:2328
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
A lightweight class that represents a single element that may be timed and/or counted via stats...
A container for the various kinds of properties we might ask to have on a graphics window before we o...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
An object to create GraphicsOutputs that share a particular 3-D API.
Definition: graphicsPipe.h:58
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
This is a base class for the various different classes that represent the result of a frame of render...
This is a special DisplayRegion wrapper that actually includes a pair of DisplayRegions internally: t...
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
A thread; that is, a lightweight process.
Definition: thread.h:51
Encapsulates all the communication with a particular instance of a given rendering backend...
A rectangular subregion within a window for rendering into.
Definition: displayRegion.h:61
An abstract base class for GraphicsOutput, for all the usual reasons.
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is a sequence number that increments monotonically.
Definition: updateSeq.h:43
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition: lightMutex.h:45
static DrawMask get_all_camera_mask()
Returns a DrawMask that is appropriate for rendering to all cameras.
Definition: pandaNode.I:513
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37