瀏覽代碼

wip: tinybuffer

David Rose 17 年之前
父節點
當前提交
ab486404e5

+ 1 - 0
panda/src/tinydisplay/Sources.pp

@@ -18,6 +18,7 @@
     tinyGeomMunger.I tinyGeomMunger.cxx tinyGeomMunger.h \
     tinyGeomMunger.I tinyGeomMunger.cxx tinyGeomMunger.h \
     tinySDLGraphicsPipe.I tinySDLGraphicsPipe.cxx tinySDLGraphicsPipe.h \
     tinySDLGraphicsPipe.I tinySDLGraphicsPipe.cxx tinySDLGraphicsPipe.h \
     tinySDLGraphicsWindow.h tinySDLGraphicsWindow.I tinySDLGraphicsWindow.cxx \
     tinySDLGraphicsWindow.h tinySDLGraphicsWindow.I tinySDLGraphicsWindow.cxx \
+    tinyGraphicsBuffer.h tinyGraphicsBuffer.I tinyGraphicsBuffer.cxx \
     tinyGraphicsStateGuardian.h tinyGraphicsStateGuardian.I \
     tinyGraphicsStateGuardian.h tinyGraphicsStateGuardian.I \
     tinyGraphicsStateGuardian.cxx \
     tinyGraphicsStateGuardian.cxx \
     tinyTextureContext.I tinyTextureContext.cxx tinyTextureContext.h \
     tinyTextureContext.I tinyTextureContext.cxx tinyTextureContext.h \

+ 2 - 0
panda/src/tinydisplay/config_tinydisplay.cxx

@@ -21,6 +21,7 @@
 #include "tinyOsxGraphicsWindow.h"
 #include "tinyOsxGraphicsWindow.h"
 #include "tinySDLGraphicsPipe.h"
 #include "tinySDLGraphicsPipe.h"
 #include "tinySDLGraphicsWindow.h"
 #include "tinySDLGraphicsWindow.h"
+#include "tinyGraphicsBuffer.h"
 #include "tinyGraphicsStateGuardian.h"
 #include "tinyGraphicsStateGuardian.h"
 #include "tinyGeomMunger.h"
 #include "tinyGeomMunger.h"
 #include "tinyTextureContext.h"
 #include "tinyTextureContext.h"
@@ -115,6 +116,7 @@ init_libtinydisplay() {
   }
   }
   initialized = true;
   initialized = true;
 
 
+  TinyGraphicsBuffer::init_type();
   TinyGraphicsStateGuardian::init_type();
   TinyGraphicsStateGuardian::init_type();
   TinyGeomMunger::init_type();
   TinyGeomMunger::init_type();
   TinyTextureContext::init_type();
   TinyTextureContext::init_type();

+ 14 - 0
panda/src/tinydisplay/tinyGraphicsBuffer.I

@@ -0,0 +1,14 @@
+// Filename: tinyGraphicsBuffer.I
+// Created by:  drose (08Aug08)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+

+ 180 - 0
panda/src/tinydisplay/tinyGraphicsBuffer.cxx

@@ -0,0 +1,180 @@
+// Filename: tinyGraphicsBuffer.cxx
+// Created by:  drose (08Aug08)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#include "pandabase.h"
+
+#include "tinyGraphicsBuffer.h"
+#include "config_tinydisplay.h"
+#include "tinyGraphicsStateGuardian.h"
+#include "pStatTimer.h"
+
+TypeHandle TinyGraphicsBuffer::_type_handle;
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+TinyGraphicsBuffer::
+TinyGraphicsBuffer(GraphicsPipe *pipe, 
+                   const string &name,
+                   const FrameBufferProperties &fb_prop,
+                   const WindowProperties &win_prop,
+                   int flags,
+                   GraphicsStateGuardian *gsg,
+                   GraphicsOutput *host) :
+  GraphicsBuffer(pipe, name, fb_prop, win_prop, flags, gsg, host)
+{
+  _frame_buffer = NULL;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::Destructor
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+TinyGraphicsBuffer::
+~TinyGraphicsBuffer() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::begin_frame
+//       Access: Public, Virtual
+//  Description: This function will be called within the draw thread
+//               before beginning rendering for a given frame.  It
+//               should do whatever setup is required, and return true
+//               if the frame should be rendered, or false if it
+//               should be skipped.
+////////////////////////////////////////////////////////////////////
+bool TinyGraphicsBuffer::
+begin_frame(FrameMode mode, Thread *current_thread) {
+  begin_frame_spam(mode);
+  if (_gsg == (GraphicsStateGuardian *)NULL) {
+    return false;
+  }
+
+  TinyGraphicsStateGuardian *tinygsg;
+  DCAST_INTO_R(tinygsg, _gsg, false);
+
+  tinygsg->_current_frame_buffer = _frame_buffer;
+  tinygsg->reset_if_new();
+  
+  _gsg->set_current_properties(&get_fb_properties());
+  return _gsg->begin_frame(current_thread);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::end_frame
+//       Access: Public, Virtual
+//  Description: This function will be called within the draw thread
+//               after rendering is completed for a given frame.  It
+//               should do whatever finalization is required.
+////////////////////////////////////////////////////////////////////
+void TinyGraphicsBuffer::
+end_frame(FrameMode mode, Thread *current_thread) {
+  end_frame_spam(mode);
+  nassertv(_gsg != (GraphicsStateGuardian *)NULL);
+
+  if (mode == FM_render) {
+    // end_render_texture();
+    copy_to_textures();
+  }
+
+  _gsg->end_frame(current_thread);
+
+  if (mode == FM_render) {
+    trigger_flip();
+    if (_one_shot) {
+      prepare_for_deletion();
+    }
+    clear_cube_map_selection();
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::close_buffer
+//       Access: Protected, Virtual
+//  Description: Closes the buffer right now.  Called from the buffer
+//               thread.
+////////////////////////////////////////////////////////////////////
+void TinyGraphicsBuffer::
+close_buffer() {
+  if (_gsg != (GraphicsStateGuardian *)NULL) {
+    TinyGraphicsStateGuardian *tinygsg;
+    DCAST_INTO_V(tinygsg, _gsg);
+    tinygsg->_current_frame_buffer = NULL;
+    _gsg.clear();
+    _active = false;
+  }
+
+  GraphicsBuffer::close_buffer();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::open_buffer
+//       Access: Protected, Virtual
+//  Description: Opens the buffer right now.  Called from the buffer
+//               thread.  Returns true if the buffer is successfully
+//               opened, or false if there was a problem.
+////////////////////////////////////////////////////////////////////
+bool TinyGraphicsBuffer::
+open_buffer() {
+  if (!GraphicsBuffer::open_buffer()) {
+    return false;
+  }
+
+  // GSG Creation/Initialization
+  TinyGraphicsStateGuardian *tinygsg;
+  if (_gsg == 0) {
+    // There is no old gsg.  Create a new one.
+    tinygsg = new TinyGraphicsStateGuardian(_pipe, NULL);
+    _gsg = tinygsg;
+  } else {
+    DCAST_INTO_R(tinygsg, _gsg, false);
+  }
+  
+  create_frame_buffer();
+  if (_frame_buffer == NULL) {
+    tinydisplay_cat.error()
+      << "Could not create frame buffer.\n";
+    return false;
+  }
+
+  tinygsg->_current_frame_buffer = _frame_buffer;
+  
+  tinygsg->reset_if_new();
+  if (!tinygsg->is_valid()) {
+    close_buffer();
+    return false;
+  }
+
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TinyGraphicsBuffer::create_frame_buffer
+//       Access: Private
+//  Description: Creates a suitable frame buffer for the current
+//               window size.
+////////////////////////////////////////////////////////////////////
+void TinyGraphicsBuffer::
+create_frame_buffer() {
+  if (_frame_buffer != NULL) {
+    ZB_close(_frame_buffer);
+    _frame_buffer = NULL;
+  }
+
+  _frame_buffer = ZB_open(get_fb_x_size(), get_fb_y_size(), ZB_MODE_RGBA, 0, 0, 0, 0);
+}
+

+ 71 - 0
panda/src/tinydisplay/tinyGraphicsBuffer.h

@@ -0,0 +1,71 @@
+// Filename: tinyGraphicsBuffer.h
+// Created by:  drose (08Aug08)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef TINYGRAPHICSBUFFER_H
+#define TINYGRAPHICSBUFFER_H
+
+#include "pandabase.h"
+#include "graphicsBuffer.h"
+#include "tinyGraphicsStateGuardian.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : TinyGraphicsBuffer
+// Description : An offscreen graphics buffer.
+////////////////////////////////////////////////////////////////////
+class EXPCL_TINYDISPLAY TinyGraphicsBuffer : public GraphicsBuffer {
+public:
+  TinyGraphicsBuffer(GraphicsPipe *pipe, 
+                     const string &name,
+                     const FrameBufferProperties &fb_prop,
+                     const WindowProperties &win_prop,
+                     int flags,
+                     GraphicsStateGuardian *gsg,
+                     GraphicsOutput *host);
+  virtual ~TinyGraphicsBuffer();
+
+  virtual bool begin_frame(FrameMode mode, Thread *current_thread);
+  virtual void end_frame(FrameMode mode, Thread *current_thread);
+
+protected:
+  virtual void close_buffer();
+  virtual bool open_buffer();
+
+private:
+  void create_frame_buffer();
+
+private:
+  ZBuffer *_frame_buffer;
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    GraphicsBuffer::init_type();
+    register_type(_type_handle, "TinyGraphicsBuffer",
+                  GraphicsBuffer::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#include "tinyGraphicsBuffer.I"
+
+#endif
+

+ 11 - 0
panda/src/tinydisplay/tinyOsxGraphicsPipe.cxx

@@ -19,6 +19,7 @@
 #include "tinyOsxGraphicsPipe.h"
 #include "tinyOsxGraphicsPipe.h"
 #include "config_tinydisplay.h"
 #include "config_tinydisplay.h"
 #include "tinyOsxGraphicsWindow.h"
 #include "tinyOsxGraphicsWindow.h"
+#include "tinyGraphicsBuffer.h"
 #include "pnmImage.h"
 #include "pnmImage.h"
 
 
 TypeHandle TinyOsxGraphicsPipe::_type_handle;
 TypeHandle TinyOsxGraphicsPipe::_type_handle;
@@ -236,6 +237,16 @@ make_output(const string &name,
                                      flags, gsg, host);
                                      flags, gsg, host);
   }
   }
   
   
+  // Second thing to try: a TinyGraphicsBuffer
+  if (retry == 1) {
+    if ((!support_render_texture)||
+        ((flags&BF_require_parasite)!=0)||
+        ((flags&BF_require_window)!=0)) {
+      return NULL;
+    }
+    return new TinyGraphicsBuffer(this, name, fb_prop, win_prop, flags, gsg, host);
+  }
+  
   // Nothing else left to try.
   // Nothing else left to try.
   return NULL;
   return NULL;
 }
 }