Back to index

nux  3.0.0
IOpenGLPixelBufferOject.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLDeviceObjects.h"
00024 #include "IOpenGLPixelBufferOject.h"
00025 
00026 namespace nux
00027 {
00028 
00029   NUX_IMPLEMENT_OBJECT_TYPE(IOpenGLPixelBufferObject);
00030 
00031   IOpenGLPixelBufferObject::IOpenGLPixelBufferObject(unsigned int Size, VBO_USAGE Usage, NUX_FILE_LINE_DECL)
00032     :   IOpenGLResource(RTVERTEXBUFFER, NUX_FILE_LINE_PARAM)
00033     ,   _Length(Size)
00034     ,   _Usage(Usage)
00035     ,   _MemMap(0)
00036     ,   _OffsetToLock(0)
00037     ,   _SizeToLock(0)
00038   {
00039     CHECKGL(glGenBuffersARB(1, &_OpenGLID));
00040     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, _OpenGLID));
00041     CHECKGL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, _Length, NULL, Usage));
00042     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
00043     GRunTimeStats.Register(this);
00044   }
00045 
00046   IOpenGLPixelBufferObject::~IOpenGLPixelBufferObject()
00047   {
00048     CHECKGL(glDeleteBuffersARB(1, &_OpenGLID));
00049     _OpenGLID = 0;
00050     GRunTimeStats.UnRegister(this);
00051   }
00052 
00053   int IOpenGLPixelBufferObject::Lock(
00054     unsigned int OffsetToLock,
00055     unsigned int SizeToLock,
00056     void **ppbData)
00057   {
00058     nuxAssert(SizeToLock <= _Length);
00059     nuxAssert(OffsetToLock + SizeToLock <= _Length);
00060 
00061     if (SizeToLock == 0)
00062     {
00063       if (OffsetToLock == 0)
00064       {
00065         // lock the entire buffer
00066         SizeToLock = _Length;
00067       }
00068       else
00069         return OGL_INVALID_CALL;
00070     }
00071 
00072     // If _MemMap, _OffsetToLock and _SizeToLock are not equal to zero, then we have already mapped the buffer
00073     // Unlock it before locking again.
00074     nuxAssert(_MemMap == 0);
00075     nuxAssert(_OffsetToLock == 0);
00076     nuxAssert(_SizeToLock == 0);
00077 
00078 #ifndef NUX_OPENGLES_20
00079 //FIXME: GLES 2.0 doesn't support PBOs, we need to allocate client memory and copy data there
00080     // When locking it shouldn't matter if we use GL_PIXEL_UNPACK_BUFFER_ARB or GL_PIXEL_PACK_BUFFER_ARB.
00081     // We just want a pointer to the data.
00082     CHECKGL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, _OpenGLID));
00083     // Map the Entire buffer into system memory
00084     _MemMap = (BYTE *) glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE); // we maybe reading or writing to the PBO.
00085     CHECKGL_MSG(glMapBufferARB);
00086     *ppbData = (void *) (_MemMap + OffsetToLock);
00087 
00088     _OffsetToLock   = OffsetToLock;
00089     _SizeToLock     = SizeToLock;
00090 
00091     CHECKGL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
00092     CHECKGL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
00093 #endif
00094 
00095     return OGL_OK;
00096   }
00097 
00098   int IOpenGLPixelBufferObject::Unlock()
00099   {
00100 #ifndef NUX_OPENGLES_20
00101     CHECKGL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, _OpenGLID));
00102     //CHECKGL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap));
00103 
00104     CHECKGL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
00105     CHECKGL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
00106     CHECKGL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
00107 #endif
00108 
00109     _MemMap         = 0;
00110     _OffsetToLock   = 0;
00111     _SizeToLock     = 0;
00112     return OGL_OK;
00113   }
00114 
00115   void IOpenGLPixelBufferObject::BindPackPixelBufferObject()
00116   {
00117 #ifndef NUX_OPENGLES_20
00118     CHECKGL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _OpenGLID));
00119 #endif
00120   }
00121 
00122   void IOpenGLPixelBufferObject::BindUnpackPixelBufferObject()
00123   {
00124 #ifndef NUX_OPENGLES_20
00125     CHECKGL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, _OpenGLID));
00126 #endif
00127   }
00128 
00129   unsigned int IOpenGLPixelBufferObject::GetSize()
00130   {
00131     return _Length;
00132   }
00133 
00134 }