~ubuntu-branches/ubuntu/oneiric/nux/oneiric

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#include "GpuDevice.h"
#include "GLDeviceObjects.h"
#include "IOpenGLVertexBuffer.h"

namespace nux
{

  NUX_IMPLEMENT_OBJECT_TYPE (IOpenGLVertexBuffer);

  IOpenGLVertexBuffer::IOpenGLVertexBuffer (t_u32 Length, VBO_USAGE Usage, NUX_FILE_LINE_DECL)
    :   IOpenGLResource (RTVERTEXBUFFER, NUX_FILE_LINE_PARAM)
    ,   _Length (Length)
    ,   _Usage (Usage)
    ,   _MemMap (0)
    ,   _OffsetToLock (0)
    ,   _SizeToLock (0)
  {
    CHECKGL ( glGenBuffersARB (1, &_OpenGLID) );
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
    CHECKGL ( glBufferDataARB (GL_ARRAY_BUFFER_ARB, _Length, NULL, Usage) );
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );
    GRunTimeStats.Register (this);
  }

  IOpenGLVertexBuffer::~IOpenGLVertexBuffer()
  {
    CHECKGL ( glDeleteBuffersARB (1, &_OpenGLID) );
    _OpenGLID = 0;
    GRunTimeStats.UnRegister (this);
  }

  int IOpenGLVertexBuffer::Lock (
    t_u32 OffsetToLock,
    t_u32 SizeToLock,
    void **ppbData)
  {
    nuxAssert (SizeToLock <= _Length);
    nuxAssert (OffsetToLock + SizeToLock <= _Length);

    if (SizeToLock == 0)
    {
      if (OffsetToLock == 0)
      {
        // lock the entire buffer
        SizeToLock = _Length;
      }
      else
      {
        nuxDebugMsg (TEXT ("[IOpenGLVertexBuffer::Lock] Invalid parameters.") );
        return OGL_INVALID_CALL;
      }
    }

    // If _MemMap, _OffsetToLock and _SizeToLock are not equal to zero, then we have already mapped the buffer
    // Unlock it before locking again.
    nuxAssert (_MemMap == 0);
    nuxAssert (_OffsetToLock == 0);
    nuxAssert (_SizeToLock == 0);

    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
    // Map the Entire buffer into system memory
    _MemMap = (BYTE *) glMapBufferARB (GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
    CHECKGL_MSG (glMapBufferARB);
    *ppbData = (void *) (_MemMap + OffsetToLock);

    _OffsetToLock   = OffsetToLock;
    _SizeToLock     = SizeToLock;

    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );

    return OGL_OK;
  }

  int IOpenGLVertexBuffer::Unlock()
  {
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
    //CHECKGL( glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap) );

    CHECKGL ( glUnmapBufferARB (GL_ARRAY_BUFFER_ARB) );
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );

    _MemMap         = 0;
    _OffsetToLock   = 0;
    _SizeToLock     = 0;
    return OGL_OK;
  }

  void IOpenGLVertexBuffer::BindVertexBuffer()
  {
    CHECKGL (glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
  }

  t_u32 IOpenGLVertexBuffer::GetSize()
  {
    return _Length;
  }

}