Home | History | Annotate | Download | only in libGLESv2
      1 #include "precompiled.h"
      2 //
      3 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
      4 // Use of this source code is governed by a BSD-style license that can be
      5 // found in the LICENSE file.
      6 //
      7 
      8 // Buffer.cpp: Implements the gl::Buffer class, representing storage of vertex and/or
      9 // index data. Implements GL buffer objects and related functionality.
     10 // [OpenGL ES 2.0.24] section 2.9 page 21.
     11 
     12 #include "libGLESv2/Buffer.h"
     13 
     14 #include "libGLESv2/renderer/VertexBuffer.h"
     15 #include "libGLESv2/renderer/IndexBuffer.h"
     16 #include "libGLESv2/renderer/BufferStorage.h"
     17 #include "libGLESv2/renderer/Renderer.h"
     18 
     19 namespace gl
     20 {
     21 
     22 Buffer::Buffer(rx::Renderer *renderer, GLuint id) : RefCountObject(id)
     23 {
     24     mRenderer = renderer;
     25     mUsage = GL_DYNAMIC_DRAW;
     26 
     27     mBufferStorage = renderer->createBufferStorage();
     28     mStaticVertexBuffer = NULL;
     29     mStaticIndexBuffer = NULL;
     30     mUnmodifiedDataUse = 0;
     31 }
     32 
     33 Buffer::~Buffer()
     34 {
     35     delete mBufferStorage;
     36     delete mStaticVertexBuffer;
     37     delete mStaticIndexBuffer;
     38 }
     39 
     40 void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
     41 {
     42     mBufferStorage->clear();
     43     mIndexRangeCache.clear();
     44     mBufferStorage->setData(data, size, 0);
     45 
     46     mUsage = usage;
     47 
     48     invalidateStaticData();
     49 
     50     if (usage == GL_STATIC_DRAW)
     51     {
     52         mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer);
     53         mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer);
     54     }
     55 }
     56 
     57 void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
     58 {
     59     mBufferStorage->setData(data, size, offset);
     60     mIndexRangeCache.invalidateRange(offset, size);
     61 
     62     if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->getBufferSize() != 0))
     63     {
     64         invalidateStaticData();
     65     }
     66 
     67     mUnmodifiedDataUse = 0;
     68 }
     69 
     70 rx::BufferStorage *Buffer::getStorage() const
     71 {
     72     return mBufferStorage;
     73 }
     74 
     75 unsigned int Buffer::size()
     76 {
     77     return mBufferStorage->getSize();
     78 }
     79 
     80 GLenum Buffer::usage() const
     81 {
     82     return mUsage;
     83 }
     84 
     85 rx::StaticVertexBufferInterface *Buffer::getStaticVertexBuffer()
     86 {
     87     return mStaticVertexBuffer;
     88 }
     89 
     90 rx::StaticIndexBufferInterface *Buffer::getStaticIndexBuffer()
     91 {
     92     return mStaticIndexBuffer;
     93 }
     94 
     95 void Buffer::invalidateStaticData()
     96 {
     97     delete mStaticVertexBuffer;
     98     mStaticVertexBuffer = NULL;
     99 
    100     delete mStaticIndexBuffer;
    101     mStaticIndexBuffer = NULL;
    102 
    103     mUnmodifiedDataUse = 0;
    104 }
    105 
    106 // Creates static buffers if sufficient used data has been left unmodified
    107 void Buffer::promoteStaticUsage(int dataSize)
    108 {
    109     if (!mStaticVertexBuffer && !mStaticIndexBuffer)
    110     {
    111         mUnmodifiedDataUse += dataSize;
    112 
    113         if (mUnmodifiedDataUse > 3 * mBufferStorage->getSize())
    114         {
    115             mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer);
    116             mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer);
    117         }
    118     }
    119 }
    120 
    121 rx::IndexRangeCache *Buffer::getIndexRangeCache()
    122 {
    123     return &mIndexRangeCache;
    124 }
    125 
    126 }
    127