From a7678cb3e48f83874e2d9d19dec7e12fb3edc8cb Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 10 Mar 2009 12:51:15 +0000 Subject: [PATCH] osxGraphicsBuffer --- panda/src/osxdisplay/config_osxdisplay.cxx | 2 +- panda/src/osxdisplay/osxGraphicsBuffer.cxx | 73 +++++++++++++++++-- panda/src/osxdisplay/osxGraphicsBuffer.h | 14 ++-- panda/src/osxdisplay/osxGraphicsPipe.cxx | 4 +- .../osxdisplay/osxGraphicsStateGuardian.cxx | 11 +-- .../src/osxdisplay/osxGraphicsStateGuardian.h | 3 +- panda/src/osxdisplay/osxGraphicsWindow.mm | 2 +- 7 files changed, 80 insertions(+), 29 deletions(-) diff --git a/panda/src/osxdisplay/config_osxdisplay.cxx b/panda/src/osxdisplay/config_osxdisplay.cxx index 6a4847a790..93a46002a5 100644 --- a/panda/src/osxdisplay/config_osxdisplay.cxx +++ b/panda/src/osxdisplay/config_osxdisplay.cxx @@ -65,7 +65,7 @@ init_libosxdisplay() { } initialized = true; - osxGraphicsStateGuardian::init_type(); + osxGraphicsBuffer::init_type(); osxGraphicsPipe::init_type(); osxGraphicsWindow::init_type(); osxGraphicsStateGuardian::init_type(); diff --git a/panda/src/osxdisplay/osxGraphicsBuffer.cxx b/panda/src/osxdisplay/osxGraphicsBuffer.cxx index 92dc770e81..80f719ece0 100644 --- a/panda/src/osxdisplay/osxGraphicsBuffer.cxx +++ b/panda/src/osxdisplay/osxGraphicsBuffer.cxx @@ -37,7 +37,11 @@ osxGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe, { osxGraphicsPipe *osx_pipe; DCAST_INTO_V(osx_pipe, _pipe); + + _pbuffer = NULL; + // Since the pbuffer never gets flipped, we get screenshots from the + // same buffer we draw into. _screenshot_buffer_type = _draw_buffer_type; } @@ -48,6 +52,7 @@ osxGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe, //////////////////////////////////////////////////////////////////// osxGraphicsBuffer:: ~osxGraphicsBuffer() { + nassertv(_pbuffer == NULL); } //////////////////////////////////////////////////////////////////// @@ -67,15 +72,23 @@ begin_frame(FrameMode mode, Thread *current_thread) { if (_gsg == (GraphicsStateGuardian *)NULL) { return false; } + nassertr(_pbuffer != NULL, false); osxGraphicsStateGuardian *osxgsg; DCAST_INTO_R(osxgsg, _gsg, false); -// osxMakeCurrent(_display, _pbuffer, osxgsg->_context); + if (!aglSetPBuffer(osxgsg->get_context(), _pbuffer, 0, 0, 0)) { + aglReportError("aglSetPBuffer"); + return false; + } + + if (!aglSetCurrentContext(osxgsg->get_context())) { + aglReportError("aglSetCurrentContext"); + return false; + } osxgsg->reset_if_new(); - if (mode == FM_render) - { + if (mode == FM_render) { for (int i=0; iget_context(), _pbuffer, 0, 0, 0); _gsg.clear(); _active = false; } + if (_pbuffer != NULL) { + aglDestroyPBuffer(_pbuffer); + _pbuffer = NULL; + } _is_valid = false; } @@ -140,11 +156,52 @@ close_buffer() { //////////////////////////////////////////////////////////////////// bool osxGraphicsBuffer:: open_buffer() { - if (_gsg == 0) { _gsg = new osxGraphicsStateGuardian(_engine, _pipe, NULL); } + + if (_pbuffer == NULL) { + if (!aglCreatePBuffer(_x_size, _y_size, GL_TEXTURE_2D, GL_RGBA, 0, &_pbuffer)) { + aglReportError("aglCreatePBuffer"); + close_buffer(); + return false; + } + } + + osxGraphicsStateGuardian *osxgsg; + DCAST_INTO_R(osxgsg, _gsg, false); + OSStatus stat = osxgsg->buildGL(false, true, _fb_properties); + if (stat != noErr) { + return false; + } + + if (!aglSetPBuffer(osxgsg->get_context(), _pbuffer, 0, 0, 0)) { + aglReportError("aglSetPBuffer"); + close_buffer(); + return false; + } + + if (!aglSetCurrentContext(osxgsg->get_context())) { + aglReportError("aglSetCurrentContext"); + return false; + } + + osxgsg->reset_if_new(); + if (!osxgsg->is_valid()) { + close_buffer(); + return false; + } + + /* + if (!osxgsg->get_fb_properties().verify_hardware_software + (_fb_properties, osxgsg->get_gl_renderer())) { + close_buffer(); + return false; + } + _fb_properties = osxgsg->get_fb_properties(); + */ - return false; + _is_valid = true; + return true; } diff --git a/panda/src/osxdisplay/osxGraphicsBuffer.h b/panda/src/osxdisplay/osxGraphicsBuffer.h index c1808ef9c7..afdd8378c9 100644 --- a/panda/src/osxdisplay/osxGraphicsBuffer.h +++ b/panda/src/osxdisplay/osxGraphicsBuffer.h @@ -23,12 +23,11 @@ #include "glgsg.h" //////////////////////////////////////////////////////////////////// -// Class : OSXGraphicsBuffer -// rhh mar-2006 -// Sorry ... this is not functional at all... I have no need for it yet ? -// +// Class : osxGraphicsBuffer +// Description : An offscreen buffer in the OSX environment. This +// creates an AGLPbuffer. //////////////////////////////////////////////////////////////////// -class EXPCL_PANDAGL osxGraphicsBuffer : public GraphicsBuffer { +class osxGraphicsBuffer : public GraphicsBuffer { public: osxGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe, const string &name, @@ -39,7 +38,6 @@ public: GraphicsOutput *host); virtual ~osxGraphicsBuffer(); - virtual bool begin_frame(FrameMode mode, Thread *current_thread); virtual void end_frame(FrameMode mode, Thread *current_thread); @@ -47,6 +45,8 @@ protected: virtual void close_buffer(); virtual bool open_buffer(); +private: + AGLPbuffer _pbuffer; public: static TypeHandle get_class_type() { @@ -54,7 +54,7 @@ public: } static void init_type() { GraphicsBuffer::init_type(); - register_type(_type_handle, "owsGraphicsBuffer", + register_type(_type_handle, "osxGraphicsBuffer", GraphicsBuffer::get_class_type()); } virtual TypeHandle get_type() const { diff --git a/panda/src/osxdisplay/osxGraphicsPipe.cxx b/panda/src/osxdisplay/osxGraphicsPipe.cxx index 683a5f8694..d071933c92 100644 --- a/panda/src/osxdisplay/osxGraphicsPipe.cxx +++ b/panda/src/osxdisplay/osxGraphicsPipe.cxx @@ -212,7 +212,6 @@ make_output(const string &name, GraphicsOutput *host, int retry, bool &precertify) { - if (!_is_valid) { return NULL; } @@ -269,7 +268,6 @@ make_output(const string &name, } // Third thing to try: an osxGraphicsBuffer - /* if (retry == 2) { if ((!support_render_texture)|| ((flags&BF_require_parasite)!=0)|| @@ -282,7 +280,7 @@ make_output(const string &name, return new osxGraphicsBuffer(engine, this, name, fb_prop, win_prop, flags, gsg, host); } - */ + // Nothing else left to try. return NULL; } diff --git a/panda/src/osxdisplay/osxGraphicsStateGuardian.cxx b/panda/src/osxdisplay/osxGraphicsStateGuardian.cxx index 9a7dcaf09d..0d4f026399 100644 --- a/panda/src/osxdisplay/osxGraphicsStateGuardian.cxx +++ b/panda/src/osxdisplay/osxGraphicsStateGuardian.cxx @@ -184,8 +184,7 @@ draw_resize_box() { // Description: This function will build up a context for a gsg.. //////////////////////////////////////////////////////////////////// OSStatus osxGraphicsStateGuardian:: -buildGL(osxGraphicsWindow &window, bool full_screen, - FrameBufferProperties &fb_props) { +buildGL(bool full_screen, bool pbuffer, FrameBufferProperties &fb_props) { if (_aglcontext) { describe_pixel_format(fb_props); return noErr; // already built @@ -230,16 +229,14 @@ buildGL(osxGraphicsWindow &window, bool full_screen, if (full_screen) { attrib.push_back(AGL_FULLSCREEN); } + if (pbuffer) { + attrib.push_back(AGL_PBUFFER); + } - // These are renderer modes, not pixel modes. Not sure if they have - // any meaning here; maybe we should handle these flags differently. if (fb_props.get_force_hardware()) { attrib.push_back(AGL_ACCELERATED); attrib.push_back(AGL_NO_RECOVERY); } - if (fb_props.get_force_software()) { - attrib.push_back(AGL_PBUFFER); - } // Allow the system to choose the largest buffers requested that // meets all our selections. diff --git a/panda/src/osxdisplay/osxGraphicsStateGuardian.h b/panda/src/osxdisplay/osxGraphicsStateGuardian.h index ff24378522..52321cb5d1 100644 --- a/panda/src/osxdisplay/osxGraphicsStateGuardian.h +++ b/panda/src/osxdisplay/osxGraphicsStateGuardian.h @@ -50,8 +50,7 @@ protected: virtual void *get_extension_func(const char *prefix, const char *name); public: - OSStatus buildGL(osxGraphicsWindow &window, bool full_screen, - FrameBufferProperties &fb_props); + OSStatus buildGL(bool full_screen, bool pbuffer, FrameBufferProperties &fb_props); AGLContext get_context(void) { return _aglcontext; }; const AGLPixelFormat getAGlPixelFormat() const { return _aglPixFmt; }; diff --git a/panda/src/osxdisplay/osxGraphicsWindow.mm b/panda/src/osxdisplay/osxGraphicsWindow.mm index a8d7c42e7f..72372772ea 100644 --- a/panda/src/osxdisplay/osxGraphicsWindow.mm +++ b/panda/src/osxdisplay/osxGraphicsWindow.mm @@ -692,7 +692,7 @@ buildGL(bool full_screen) { // make sure the ggs is up and runnig.. osxGraphicsStateGuardian *osxgsg = NULL; osxgsg = DCAST(osxGraphicsStateGuardian, _gsg); - OSStatus stat = osxgsg->buildGL(*this, full_screen, _fb_properties); + OSStatus stat = osxgsg->buildGL(full_screen, false, _fb_properties); if(stat != noErr) { return stat;