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 struct VertexAttribCurrentValueData;
     19 }
     20 
     21 namespace rx
     22 {
     23 class Renderer;
     24 
     25 class VertexBuffer
     26 {
     27   public:
     28     VertexBuffer();
     29     virtual ~VertexBuffer();
     30 
     31     virtual bool initialize(unsigned int size, bool dynamicUsage) = 0;
     32 
     33     virtual bool storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData &currentValue,
     34                                        GLint start, GLsizei count, GLsizei instances, unsigned int offset) = 0;
     35     virtual bool getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances,
     36                                   unsigned int *outSpaceRequired) const = 0;
     37 
     38     virtual unsigned int getBufferSize() const = 0;
     39     virtual bool setBufferSize(unsigned int size) = 0;
     40     virtual bool discard() = 0;
     41 
     42     unsigned int getSerial() const;
     43 
     44   protected:
     45     void updateSerial();
     46 
     47   private:
     48     DISALLOW_COPY_AND_ASSIGN(VertexBuffer);
     49 
     50     unsigned int mSerial;
     51     static unsigned int mNextSerial;
     52 };
     53 
     54 class VertexBufferInterface
     55 {
     56   public:
     57     VertexBufferInterface(rx::Renderer *renderer, bool dynamic);
     58     virtual ~VertexBufferInterface();
     59 
     60     bool reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances);
     61 
     62     unsigned int getBufferSize() const;
     63 
     64     unsigned int getSerial() const;
     65 
     66     virtual bool storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData &currentValue,
     67                                       GLint start, GLsizei count, GLsizei instances, unsigned int *outStreamOffset);
     68 
     69     bool directStoragePossible(const gl::VertexAttribute &attrib,
     70                                const gl::VertexAttribCurrentValueData &currentValue) const;
     71 
     72     VertexBuffer* getVertexBuffer() const;
     73 
     74   protected:
     75     virtual bool reserveSpace(unsigned int size) = 0;
     76 
     77     unsigned int getWritePosition() const;
     78     void setWritePosition(unsigned int writePosition);
     79 
     80     bool discard();
     81 
     82     bool setBufferSize(unsigned int size);
     83 
     84   private:
     85     DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface);
     86 
     87     rx::Renderer *const mRenderer;
     88 
     89     VertexBuffer* mVertexBuffer;
     90 
     91     unsigned int mWritePosition;
     92     unsigned int mReservedSpace;
     93     bool mDynamic;
     94 };
     95 
     96 class StreamingVertexBufferInterface : public VertexBufferInterface
     97 {
     98   public:
     99     StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize);
    100     ~StreamingVertexBufferInterface();
    101 
    102   protected:
    103     bool reserveSpace(unsigned int size);
    104 };
    105 
    106 class StaticVertexBufferInterface : public VertexBufferInterface
    107 {
    108   public:
    109     explicit StaticVertexBufferInterface(rx::Renderer *renderer);
    110     ~StaticVertexBufferInterface();
    111 
    112     bool storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData &currentValue,
    113                               GLint start, GLsizei count, GLsizei instances, unsigned int *outStreamOffset);
    114 
    115     bool lookupAttribute(const gl::VertexAttribute &attribute, unsigned int* outStreamFffset);
    116 
    117   protected:
    118     bool reserveSpace(unsigned int size);
    119 
    120   private:
    121     struct VertexElement
    122     {
    123         GLenum type;
    124         GLint size;
    125         GLsizei stride;
    126         bool normalized;
    127         bool pureInteger;
    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_