1 // 2 // Copyright 2002 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 LIBANGLE_RENDERER_D3D_VERTEXBUFFER_H_ 11 #define LIBANGLE_RENDERER_D3D_VERTEXBUFFER_H_ 12 13 #include "common/PackedEnums.h" 14 #include "common/angleutils.h" 15 #include "libANGLE/Error.h" 16 #include "libANGLE/renderer/Format.h" 17 18 #include <GLES2/gl2.h> 19 20 #include <cstddef> 21 #include <cstdint> 22 #include <vector> 23 24 namespace gl 25 { 26 class Context; 27 struct VertexAttribute; 28 class VertexBinding; 29 struct VertexAttribCurrentValueData; 30 } // namespace gl 31 32 namespace rx 33 { 34 class BufferFactoryD3D; 35 36 // Use a ref-counting scheme with self-deletion on release. We do this so that we can more 37 // easily manage the static buffer cache, without deleting currently bound buffers. 38 class VertexBuffer : angle::NonCopyable 39 { 40 public: 41 VertexBuffer(); 42 43 virtual angle::Result initialize(const gl::Context *context, 44 unsigned int size, 45 bool dynamicUsage) = 0; 46 47 // Warning: you should ensure binding really matches attrib.bindingIndex before using this 48 // function. 49 virtual angle::Result storeVertexAttributes(const gl::Context *context, 50 const gl::VertexAttribute &attrib, 51 const gl::VertexBinding &binding, 52 gl::VertexAttribType currentValueType, 53 GLint start, 54 size_t count, 55 GLsizei instances, 56 unsigned int offset, 57 const uint8_t *sourceData) = 0; 58 59 virtual unsigned int getBufferSize() const = 0; 60 virtual angle::Result setBufferSize(const gl::Context *context, unsigned int size) = 0; 61 virtual angle::Result discard(const gl::Context *context) = 0; 62 63 unsigned int getSerial() const; 64 65 // This may be overridden (e.g. by VertexBuffer11) if necessary. hintUnmapResource()66 virtual void hintUnmapResource() {} 67 68 // Reference counting. 69 void addRef(); 70 void release(); 71 72 protected: 73 void updateSerial(); 74 virtual ~VertexBuffer(); 75 76 private: 77 unsigned int mSerial; 78 static unsigned int mNextSerial; 79 unsigned int mRefCount; 80 }; 81 82 class VertexBufferInterface : angle::NonCopyable 83 { 84 public: 85 VertexBufferInterface(BufferFactoryD3D *factory, bool dynamic); 86 virtual ~VertexBufferInterface(); 87 88 unsigned int getBufferSize() const; empty()89 bool empty() const { return getBufferSize() == 0; } 90 91 unsigned int getSerial() const; 92 93 VertexBuffer *getVertexBuffer() const; 94 95 protected: 96 angle::Result discard(const gl::Context *context); 97 98 angle::Result setBufferSize(const gl::Context *context, unsigned int size); 99 100 angle::Result getSpaceRequired(const gl::Context *context, 101 const gl::VertexAttribute &attrib, 102 const gl::VertexBinding &binding, 103 size_t count, 104 GLsizei instances, 105 unsigned int *spaceInBytesOut) const; 106 BufferFactoryD3D *const mFactory; 107 VertexBuffer *mVertexBuffer; 108 bool mDynamic; 109 }; 110 111 class StreamingVertexBufferInterface : public VertexBufferInterface 112 { 113 public: 114 StreamingVertexBufferInterface(BufferFactoryD3D *factory); 115 ~StreamingVertexBufferInterface() override; 116 117 angle::Result initialize(const gl::Context *context, std::size_t initialSize); 118 void reset(); 119 120 angle::Result storeDynamicAttribute(const gl::Context *context, 121 const gl::VertexAttribute &attrib, 122 const gl::VertexBinding &binding, 123 gl::VertexAttribType currentValueType, 124 GLint start, 125 size_t count, 126 GLsizei instances, 127 unsigned int *outStreamOffset, 128 const uint8_t *sourceData); 129 130 angle::Result reserveVertexSpace(const gl::Context *context, 131 const gl::VertexAttribute &attribute, 132 const gl::VertexBinding &binding, 133 size_t count, 134 GLsizei instances); 135 136 private: 137 angle::Result reserveSpace(const gl::Context *context, unsigned int size); 138 139 unsigned int mWritePosition; 140 unsigned int mReservedSpace; 141 }; 142 143 class StaticVertexBufferInterface : public VertexBufferInterface 144 { 145 public: 146 explicit StaticVertexBufferInterface(BufferFactoryD3D *factory); 147 ~StaticVertexBufferInterface() override; 148 149 // Warning: you should ensure binding really matches attrib.bindingIndex before using these 150 // functions. 151 angle::Result storeStaticAttribute(const gl::Context *context, 152 const gl::VertexAttribute &attrib, 153 const gl::VertexBinding &binding, 154 GLint start, 155 GLsizei count, 156 GLsizei instances, 157 const uint8_t *sourceData); 158 159 bool matchesAttribute(const gl::VertexAttribute &attribute, 160 const gl::VertexBinding &binding) const; 161 162 void setAttribute(const gl::VertexAttribute &attribute, const gl::VertexBinding &binding); 163 164 private: 165 class AttributeSignature final : angle::NonCopyable 166 { 167 public: 168 AttributeSignature(); 169 170 bool matchesAttribute(const gl::VertexAttribute &attrib, 171 const gl::VertexBinding &binding) const; 172 173 void set(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding); 174 175 private: 176 angle::FormatID formatID; 177 GLuint stride; 178 size_t offset; 179 }; 180 181 AttributeSignature mSignature; 182 }; 183 184 } // namespace rx 185 186 #endif // LIBANGLE_RENDERER_D3D_VERTEXBUFFER_H_ 187