Home | History | Annotate | Download | only in renderer
      1 //
      2 // Copyright (c) 2002-2012 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 // VertexBuffer.h: Defines the abstract VertexBuffer class and VertexBufferInterface
      8 // class with derivations, classes that perform graphics API agnostic vertex buffer operations.
      9 
     10 #ifndef LIBGLESV2_RENDERER_VERTEXBUFFER_H_
     11 #define LIBGLESV2_RENDERER_VERTEXBUFFER_H_
     12 
     13 #include "common/angleutils.h"
     14 
     15 namespace gl
     16 {
     17 class VertexAttribute;
     18 }
     19 
     20 namespace rx
     21 {
     22 class Renderer;
     23 
     24 class VertexBuffer
     25 {
     26   public:
     27     VertexBuffer();
     28     virtual ~VertexBuffer();
     29 
     30     virtual bool initialize(unsigned int size, bool dynamicUsage) = 0;
     31 
     32     virtual bool storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count,
     33                                        GLsizei instances, unsigned int offset) = 0;
     34     virtual bool storeRawData(const void* data, unsigned int size, unsigned int offset) = 0;
     35 
     36     virtual unsigned int getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count,
     37                                           GLsizei instances) const = 0;
     38 
     39     virtual bool requiresConversion(const gl::VertexAttribute &attrib) const = 0;
     40 
     41     virtual unsigned int getBufferSize() const = 0;
     42     virtual bool setBufferSize(unsigned int size) = 0;
     43     virtual bool discard() = 0;
     44 
     45     unsigned int getSerial() const;
     46 
     47   protected:
     48     void updateSerial();
     49 
     50   private:
     51     DISALLOW_COPY_AND_ASSIGN(VertexBuffer);
     52 
     53     unsigned int mSerial;
     54     static unsigned int mNextSerial;
     55 };
     56 
     57 class VertexBufferInterface
     58 {
     59   public:
     60     VertexBufferInterface(rx::Renderer *renderer, bool dynamic);
     61     virtual ~VertexBufferInterface();
     62 
     63     bool reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances);
     64     bool reserveRawDataSpace(unsigned int size);
     65 
     66     unsigned int getBufferSize() const;
     67 
     68     unsigned int getSerial() const;
     69 
     70     virtual int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances);
     71     virtual int storeRawData(const void* data, unsigned int size);
     72 
     73     VertexBuffer* getVertexBuffer() const;
     74 
     75   protected:
     76     virtual bool reserveSpace(unsigned int size) = 0;
     77 
     78     unsigned int getWritePosition() const;
     79     void setWritePosition(unsigned int writePosition);
     80 
     81     bool discard();
     82 
     83     bool setBufferSize(unsigned int size);
     84 
     85   private:
     86     DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface);
     87 
     88     rx::Renderer *const mRenderer;
     89 
     90     VertexBuffer* mVertexBuffer;
     91 
     92     unsigned int mWritePosition;
     93     unsigned int mReservedSpace;
     94     bool mDynamic;
     95 };
     96 
     97 class StreamingVertexBufferInterface : public VertexBufferInterface
     98 {
     99   public:
    100     StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize);
    101     ~StreamingVertexBufferInterface();
    102 
    103   protected:
    104     bool reserveSpace(unsigned int size);
    105 };
    106 
    107 class StaticVertexBufferInterface : public VertexBufferInterface
    108 {
    109   public:
    110     explicit StaticVertexBufferInterface(rx::Renderer *renderer);
    111     ~StaticVertexBufferInterface();
    112 
    113     int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances);
    114 
    115     // Returns the offset into the vertex buffer, or -1 if not found
    116     int lookupAttribute(const gl::VertexAttribute &attribute);
    117 
    118   protected:
    119     bool reserveSpace(unsigned int size);
    120 
    121   private:
    122     struct VertexElement
    123     {
    124         GLenum type;
    125         GLint size;
    126         GLsizei stride;
    127         bool normalized;
    128         int attributeOffset;
    129 
    130         unsigned int streamOffset;
    131     };
    132 
    133     std::vector<VertexElement> mCache;
    134 };
    135 
    136 }
    137 
    138 #endif // LIBGLESV2_RENDERER_VERTEXBUFFER_H_