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.
7 // geometry/VertexDataManager.h: Defines the VertexDataManager, a class that
8 // runs the Buffer translation process.
10 #ifndef LIBGLESV2_GEOMETRY_VERTEXDATAMANAGER_H_
11 #define LIBGLESV2_GEOMETRY_VERTEXDATAMANAGER_H_
17 #include <GLES2/gl2.h>
19 #include "libGLESv2/Context.h"
24 struct TranslatedAttribute
30 UINT stride; // 0 means not to advance the read pointer at all
33 IDirect3DVertexBuffer9 *vertexBuffer;
39 VertexBuffer(IDirect3DDevice9 *device, UINT size, DWORD usageFlags);
40 virtual ~VertexBuffer();
44 IDirect3DVertexBuffer9 *getBuffer() const;
47 IDirect3DDevice9 *const mDevice;
48 IDirect3DVertexBuffer9 *mVertexBuffer;
51 DISALLOW_COPY_AND_ASSIGN(VertexBuffer);
54 class ConstantVertexBuffer : public VertexBuffer
57 ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w);
58 ~ConstantVertexBuffer();
61 class ArrayVertexBuffer : public VertexBuffer
64 ArrayVertexBuffer(IDirect3DDevice9 *device, UINT size, DWORD usageFlags);
67 UINT size() const { return mBufferSize; }
68 virtual void *map(const VertexAttribute &attribute, UINT requiredSpace, UINT *streamOffset) = 0;
69 virtual void reserveRequiredSpace() = 0;
70 void addRequiredSpace(UINT requiredSpace);
71 void addRequiredSpaceFor(ArrayVertexBuffer *buffer);
79 class StreamingVertexBuffer : public ArrayVertexBuffer
82 StreamingVertexBuffer(IDirect3DDevice9 *device, UINT initialSize);
83 ~StreamingVertexBuffer();
85 void *map(const VertexAttribute &attribute, UINT requiredSpace, UINT *streamOffset);
86 void reserveRequiredSpace();
89 class StaticVertexBuffer : public ArrayVertexBuffer
92 explicit StaticVertexBuffer(IDirect3DDevice9 *device);
93 ~StaticVertexBuffer();
95 void *map(const VertexAttribute &attribute, UINT requiredSpace, UINT *streamOffset);
96 void reserveRequiredSpace();
98 UINT lookupAttribute(const VertexAttribute &attribute); // Returns the offset into the vertex buffer, or -1 if not found
111 std::vector<VertexElement> mCache;
114 class VertexDataManager
117 VertexDataManager(Context *context, IDirect3DDevice9 *backend);
118 virtual ~VertexDataManager();
120 void dirtyCurrentValue(int index) { mDirtyCurrentValue[index] = true; }
122 void setupAttributes(const TranslatedAttribute *attributes);
123 GLenum prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *outAttribs);
126 DISALLOW_COPY_AND_ASSIGN(VertexDataManager);
128 UINT spaceRequired(const VertexAttribute &attrib, std::size_t count) const;
129 UINT writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute);
131 Context *const mContext;
132 IDirect3DDevice9 *const mDevice;
134 StreamingVertexBuffer *mStreamingBuffer;
136 bool mDirtyCurrentValue[MAX_VERTEX_ATTRIBS];
137 ConstantVertexBuffer *mCurrentValueBuffer[MAX_VERTEX_ATTRIBS];
139 // Attribute format conversion
140 struct FormatConverter
143 std::size_t outputElementSize;
144 void (*convertArray)(const void *in, std::size_t stride, std::size_t n, void *out);
145 D3DDECLTYPE d3dDeclType;
148 enum { NUM_GL_VERTEX_ATTRIB_TYPES = 6 };
150 FormatConverter mAttributeTypes[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size - 1]
152 struct TranslationDescription
155 FormatConverter preferredConversion;
156 FormatConverter fallbackConversion;
159 // This table is used to generate mAttributeTypes.
160 static const TranslationDescription mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size - 1]
162 void checkVertexCaps(DWORD declTypes);
164 unsigned int typeIndex(GLenum type) const;
165 const FormatConverter &formatConverter(const VertexAttribute &attribute) const;
170 #endif // LIBGLESV2_GEOMETRY_VERTEXDATAMANAGER_H_