nux-1.16.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 "GpuDevice.h" 00024 #include "GLDeviceObjects.h" 00025 #include "IOpenGLVertexBuffer.h" 00026 00027 namespace nux 00028 { 00029 00030 NUX_IMPLEMENT_OBJECT_TYPE (IOpenGLVertexBuffer); 00031 00032 IOpenGLVertexBuffer::IOpenGLVertexBuffer (t_u32 Length, VBO_USAGE Usage, NUX_FILE_LINE_DECL) 00033 : IOpenGLResource (RTVERTEXBUFFER, NUX_FILE_LINE_PARAM) 00034 , _Length (Length) 00035 , _Usage (Usage) 00036 , _MemMap (0) 00037 , _OffsetToLock (0) 00038 , _SizeToLock (0) 00039 { 00040 CHECKGL ( glGenBuffersARB (1, &_OpenGLID) ); 00041 CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) ); 00042 CHECKGL ( glBufferDataARB (GL_ARRAY_BUFFER_ARB, _Length, NULL, Usage) ); 00043 CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) ); 00044 GRunTimeStats.Register (this); 00045 } 00046 00047 IOpenGLVertexBuffer::~IOpenGLVertexBuffer() 00048 { 00049 CHECKGL ( glDeleteBuffersARB (1, &_OpenGLID) ); 00050 _OpenGLID = 0; 00051 GRunTimeStats.UnRegister (this); 00052 } 00053 00054 int IOpenGLVertexBuffer::Lock ( 00055 t_u32 OffsetToLock, 00056 t_u32 SizeToLock, 00057 void **ppbData) 00058 { 00059 nuxAssert (SizeToLock <= _Length); 00060 nuxAssert (OffsetToLock + SizeToLock <= _Length); 00061 00062 if (SizeToLock == 0) 00063 { 00064 if (OffsetToLock == 0) 00065 { 00066 // lock the entire buffer 00067 SizeToLock = _Length; 00068 } 00069 else 00070 { 00071 nuxDebugMsg (TEXT ("[IOpenGLVertexBuffer::Lock] Invalid parameters.") ); 00072 return OGL_INVALID_CALL; 00073 } 00074 } 00075 00076 // If _MemMap, _OffsetToLock and _SizeToLock are not equal to zero, then we have already mapped the buffer 00077 // Unlock it before locking again. 00078 nuxAssert (_MemMap == 0); 00079 nuxAssert (_OffsetToLock == 0); 00080 nuxAssert (_SizeToLock == 0); 00081 00082 CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) ); 00083 // Map the Entire buffer into system memory 00084 _MemMap = (BYTE *) glMapBufferARB (GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); 00085 CHECKGL_MSG (glMapBufferARB); 00086 *ppbData = (void *) (_MemMap + OffsetToLock); 00087 00088 _OffsetToLock = OffsetToLock; 00089 _SizeToLock = SizeToLock; 00090 00091 CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) ); 00092 00093 return OGL_OK; 00094 } 00095 00096 int IOpenGLVertexBuffer::Unlock() 00097 { 00098 CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) ); 00099 //CHECKGL( glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap) ); 00100 00101 CHECKGL ( glUnmapBufferARB (GL_ARRAY_BUFFER_ARB) ); 00102 CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) ); 00103 00104 _MemMap = 0; 00105 _OffsetToLock = 0; 00106 _SizeToLock = 0; 00107 return OGL_OK; 00108 } 00109 00110 void IOpenGLVertexBuffer::BindVertexBuffer() 00111 { 00112 CHECKGL (glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) ); 00113 } 00114 00115 t_u32 IOpenGLVertexBuffer::GetSize() 00116 { 00117 return _Length; 00118 } 00119 00120 }