nux-1.14.0
|
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 // When locking it shouldn't matter if we use GL_PIXEL_UNPACK_BUFFER_ARB or GL_PIXEL_PACK_BUFFER_ARB. 00079 // We just want a pointer to the data. 00080 CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, _OpenGLID) ); 00081 // Map the Entire buffer into system memory 00082 _MemMap = (BYTE *) glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE); // we maybe reading or writing to the PBO. 00083 CHECKGL_MSG (glMapBufferARB); 00084 *ppbData = (void *) (_MemMap + OffsetToLock); 00085 00086 _OffsetToLock = OffsetToLock; 00087 _SizeToLock = SizeToLock; 00088 00089 CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0) ); 00090 CHECKGL ( glBindBufferARB (GL_PIXEL_PACK_BUFFER_ARB, 0) ); 00091 00092 return OGL_OK; 00093 } 00094 00095 int IOpenGLPixelBufferObject::Unlock() 00096 { 00097 CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, _OpenGLID) ); 00098 //CHECKGL( glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap) ); 00099 00100 CHECKGL ( glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) ); 00101 CHECKGL ( glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0) ); 00102 CHECKGL ( glBindBufferARB (GL_PIXEL_PACK_BUFFER_ARB, 0) ); 00103 00104 00105 _MemMap = 0; 00106 _OffsetToLock = 0; 00107 _SizeToLock = 0; 00108 return OGL_OK; 00109 } 00110 00111 void IOpenGLPixelBufferObject::BindPackPixelBufferObject() 00112 { 00113 CHECKGL (glBindBufferARB (GL_PIXEL_PACK_BUFFER_ARB, _OpenGLID) ); 00114 } 00115 00116 void IOpenGLPixelBufferObject::BindUnpackPixelBufferObject() 00117 { 00118 CHECKGL (glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, _OpenGLID) ); 00119 } 00120 00121 t_u32 IOpenGLPixelBufferObject::GetSize() 00122 { 00123 return _Length; 00124 } 00125 00126 }