Home | History | Annotate | Download | only in libGLESv2
      1 //
      2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // Buffer.cpp: Implements the gl::Buffer class, representing storage of vertex and/or
      8 // index data. Implements GL buffer objects and related functionality.
      9 // [OpenGL ES 2.0.24] section 2.9 page 21.
     10 
     11 #include "libGLESv2/Buffer.h"
     12 
     13 #include "libGLESv2/main.h"
     14 #include "libGLESv2/geometry/VertexDataManager.h"
     15 #include "libGLESv2/geometry/IndexDataManager.h"
     16 
     17 namespace gl
     18 {
     19 
     20 Buffer::Buffer(GLuint id) : RefCountObject(id)
     21 {
     22     mContents = NULL;
     23     mSize = 0;
     24     mUsage = GL_DYNAMIC_DRAW;
     25 
     26     mVertexBuffer = NULL;
     27     mIndexBuffer = NULL;
     28 }
     29 
     30 Buffer::~Buffer()
     31 {
     32     delete[] mContents;
     33     delete mVertexBuffer;
     34     delete mIndexBuffer;
     35 }
     36 
     37 void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
     38 {
     39     if (size == 0)
     40     {
     41         delete[] mContents;
     42         mContents = NULL;
     43     }
     44     else if (size != mSize)
     45     {
     46         delete[] mContents;
     47         mContents = new GLubyte[size];
     48         memset(mContents, 0, size);
     49     }
     50 
     51     if (data != NULL && size > 0)
     52     {
     53         memcpy(mContents, data, size);
     54     }
     55 
     56     mSize = size;
     57     mUsage = usage;
     58 
     59     invalidateStaticData();
     60 
     61     if (usage == GL_STATIC_DRAW)
     62     {
     63         mVertexBuffer = new StaticVertexBuffer(getDevice());
     64         mIndexBuffer = new StaticIndexBuffer(getDevice());
     65     }
     66 }
     67 
     68 void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
     69 {
     70     memcpy(mContents + offset, data, size);
     71 
     72     if ((mVertexBuffer && mVertexBuffer->size() != 0) || (mIndexBuffer && mIndexBuffer->size() != 0))
     73     {
     74         invalidateStaticData();
     75 
     76         if (mUsage == GL_STATIC_DRAW)
     77         {
     78             // If applications update the buffer data after it has already been used in a draw call,
     79             // it most likely isn't used as a static buffer so we should fall back to streaming usage
     80             // for best performance. So ignore the usage hint and don't create new static buffers.
     81         //  mVertexBuffer = new StaticVertexBuffer(getDevice());
     82         //  mIndexBuffer = new StaticIndexBuffer(getDevice());
     83         }
     84     }
     85 }
     86 
     87 StaticVertexBuffer *Buffer::getVertexBuffer()
     88 {
     89     return mVertexBuffer;
     90 }
     91 
     92 StaticIndexBuffer *Buffer::getIndexBuffer()
     93 {
     94     return mIndexBuffer;
     95 }
     96 
     97 void Buffer::invalidateStaticData()
     98 {
     99     delete mVertexBuffer;
    100     mVertexBuffer = NULL;
    101 
    102     delete mIndexBuffer;
    103     mIndexBuffer = NULL;
    104 }
    105 
    106 }
    107