initial import
[vuplus_webkit] / Source / ThirdParty / ANGLE / src / libGLESv2 / VertexDataManager.cpp
1 //
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.
5 //
6
7 // VertexDataManager.h: Defines the VertexDataManager, a class that
8 // runs the Buffer translation process.
9
10 #include "libGLESv2/VertexDataManager.h"
11
12 #include "common/debug.h"
13
14 #include "libGLESv2/Buffer.h"
15 #include "libGLESv2/Program.h"
16 #include "libGLESv2/main.h"
17
18 #include "libGLESv2/vertexconversion.h"
19 #include "libGLESv2/IndexDataManager.h"
20
21 namespace
22 {
23     enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
24 }
25
26 namespace gl
27 {
28
29 VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device)
30 {
31     for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
32     {
33         mDirtyCurrentValue[i] = true;
34         mCurrentValueBuffer[i] = NULL;
35     }
36
37     const D3DCAPS9 &caps = context->getDeviceCaps();
38     checkVertexCaps(caps.DeclTypes);
39
40     mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
41
42     if (!mStreamingBuffer)
43     {
44         ERR("Failed to allocate the streaming vertex buffer.");
45     }
46 }
47
48 VertexDataManager::~VertexDataManager()
49 {
50     delete mStreamingBuffer;
51
52     for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
53     {
54         delete mCurrentValueBuffer[i];
55     }
56 }
57
58 UINT VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
59 {
60     Buffer *buffer = attribute.mBoundBuffer.get();
61
62     int inputStride = attribute.stride();
63     int elementSize = attribute.typeSize();
64     const FormatConverter &converter = formatConverter(attribute);
65     UINT streamOffset = 0;
66
67     void *output = NULL;
68     
69     if (vertexBuffer)
70     {
71         output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
72     }
73
74     if (output == NULL)
75     {
76         ERR("Failed to map vertex buffer.");
77         return -1;
78     }
79
80     const char *input = NULL;
81
82     if (buffer)
83     {
84         int offset = attribute.mOffset;
85
86         input = static_cast<const char*>(buffer->data()) + offset;
87     }
88     else
89     {
90         input = static_cast<const char*>(attribute.mPointer);
91     }
92
93     input += inputStride * start;
94
95     if (converter.identity && inputStride == elementSize)
96     {
97         memcpy(output, input, count * inputStride);
98     }
99     else
100     {
101         converter.convertArray(input, inputStride, count, output);
102     }
103
104     vertexBuffer->unmap();
105
106     return streamOffset;
107 }
108
109 GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
110 {
111     if (!mStreamingBuffer)
112     {
113         return GL_OUT_OF_MEMORY;
114     }
115
116     const VertexAttributeArray &attribs = mContext->getVertexAttributes();
117     Program *program = mContext->getCurrentProgram();
118
119     for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
120     {
121         translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
122     }
123
124     // Determine the required storage size per used buffer, and invalidate static buffers that don't contain matching attributes
125     for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
126     {
127         if (translated[i].active && attribs[i].mArrayEnabled)
128         {
129             Buffer *buffer = attribs[i].mBoundBuffer.get();
130             StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
131
132             if (staticBuffer)
133             {
134                 if (staticBuffer->size() == 0)
135                 {
136                     int totalCount = buffer->size() / attribs[i].stride();
137                     staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
138                 }
139                 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
140                 {
141                     // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
142                     mStreamingBuffer->addRequiredSpaceFor(staticBuffer);
143                     buffer->invalidateStaticData();
144
145                     mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
146                 }    
147             }
148             else
149             {
150                 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
151             }
152         }
153     }
154
155     // Reserve the required space per used buffer
156     for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
157     {
158         if (translated[i].active && attribs[i].mArrayEnabled)
159         {
160             Buffer *buffer = attribs[i].mBoundBuffer.get();
161             ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
162             ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
163
164             if (vertexBuffer)
165             {
166                 vertexBuffer->reserveRequiredSpace();
167             }
168         }
169     }
170
171     // Perform the vertex data translations
172     for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
173     {
174         if (translated[i].active)
175         {
176             if (attribs[i].mArrayEnabled)
177             {
178                 Buffer *buffer = attribs[i].mBoundBuffer.get();
179
180                 if (!buffer && attribs[i].mPointer == NULL)
181                 {
182                     // This is an application error that would normally result in a crash, but we catch it and return an error
183                     ERR("An enabled vertex array has no buffer and no pointer.");
184                     return GL_INVALID_OPERATION;
185                 }
186
187                 const FormatConverter &converter = formatConverter(attribs[i]);
188
189                 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
190                 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
191
192                 UINT streamOffset = -1;
193
194                 if (staticBuffer)
195                 {
196                     streamOffset = staticBuffer->lookupAttribute(attribs[i]);
197
198                     if (streamOffset == -1)
199                     {
200                         // Convert the entire buffer
201                         int totalCount = buffer->size() / attribs[i].stride();
202                         int startIndex = attribs[i].mOffset / attribs[i].stride();
203
204                         streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
205                     }
206
207                     if (streamOffset != -1)
208                     {
209                         streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
210                     }
211                 }
212                 else
213                 {
214                     streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
215                 }
216
217                 if (streamOffset == -1)
218                 {
219                     return GL_OUT_OF_MEMORY;
220                 }
221
222                 translated[i].vertexBuffer = vertexBuffer->getBuffer();
223                 translated[i].type = converter.d3dDeclType;
224                 translated[i].stride = converter.outputElementSize;
225                 translated[i].offset = streamOffset;
226             }
227             else
228             {
229                 if (mDirtyCurrentValue[i])
230                 {
231                     delete mCurrentValueBuffer[i];
232                     mCurrentValueBuffer[i] = new ConstantVertexBuffer(mDevice, attribs[i].mCurrentValue[0], attribs[i].mCurrentValue[1], attribs[i].mCurrentValue[2], attribs[i].mCurrentValue[3]);
233                     mDirtyCurrentValue[i] = false;
234                 }
235
236                 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
237
238                 translated[i].type = D3DDECLTYPE_FLOAT4;
239                 translated[i].stride = 0;
240                 translated[i].offset = 0;
241             }
242         }
243     }
244
245     for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
246     {
247         if (translated[i].active && attribs[i].mArrayEnabled)
248         {
249             Buffer *buffer = attribs[i].mBoundBuffer.get();
250
251             if (buffer)
252             {
253                 buffer->promoteStaticUsage(count * attribs[i].typeSize());
254             }
255         }
256     }
257
258     return GL_NO_ERROR;
259 }
260
261 std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
262 {
263     return formatConverter(attrib).outputElementSize * count;
264 }
265
266 // Mapping from OpenGL-ES vertex attrib type to D3D decl type:
267 //
268 // BYTE                 SHORT (Cast)
269 // BYTE-norm            FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
270 // UNSIGNED_BYTE        UBYTE4 (Identity) or SHORT (Cast)
271 // UNSIGNED_BYTE-norm   UBYTE4N (Identity) or FLOAT (Normalize)
272 // SHORT                SHORT (Identity)
273 // SHORT-norm           SHORT-norm (Identity) or FLOAT (Normalize)
274 // UNSIGNED_SHORT       FLOAT (Cast)
275 // UNSIGNED_SHORT-norm  USHORT-norm (Identity) or FLOAT (Normalize)
276 // FIXED (not in WebGL) FLOAT (FixedToFloat)
277 // FLOAT                FLOAT (Identity)
278
279 // GLToCType maps from GL type (as GLenum) to the C typedef. 
280 template <GLenum GLType> struct GLToCType { };
281
282 template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
283 template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
284 template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
285 template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
286 template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
287 template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
288
289 // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
290 enum D3DVertexType
291 {
292     D3DVT_FLOAT,
293     D3DVT_SHORT,
294     D3DVT_SHORT_NORM,
295     D3DVT_UBYTE,
296     D3DVT_UBYTE_NORM,
297     D3DVT_USHORT_NORM
298 };
299
300 // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
301 template <unsigned int D3DType> struct D3DToCType { };
302
303 template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
304 template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
305 template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
306 template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
307 template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
308 template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
309
310 // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
311 template <unsigned int type, int size>
312 struct WidenRule
313 {
314 };
315
316 template <int size> struct WidenRule<D3DVT_FLOAT, size>          : gl::NoWiden<size> { };
317 template <int size> struct WidenRule<D3DVT_SHORT, size>          : gl::WidenToEven<size> { };
318 template <int size> struct WidenRule<D3DVT_SHORT_NORM, size>     : gl::WidenToEven<size> { };
319 template <int size> struct WidenRule<D3DVT_UBYTE, size>          : gl::WidenToFour<size> { };
320 template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size>     : gl::WidenToFour<size> { };
321 template <int size> struct WidenRule<D3DVT_USHORT_NORM, size>    : gl::WidenToEven<size> { };
322
323 // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
324 template <unsigned int d3dtype, int size>
325 struct VertexTypeFlags
326 {
327 };
328
329 template <unsigned int capflag, unsigned int declflag>
330 struct VertexTypeFlagsHelper
331 {
332     enum { capflag = capflag };
333     enum { declflag = declflag };
334 };
335
336 template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
337 template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
338 template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
339 template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
340 template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
341 template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
342 template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
343 template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
344 template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
345 template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
346 template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
347 template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
348
349
350 // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
351 template <GLenum GLtype, bool normalized>
352 struct VertexTypeMapping
353 {
354 };
355
356 template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
357 struct VertexTypeMappingBase
358 {
359     enum { preferred = Preferred };
360     enum { fallback = Fallback };
361 };
362
363 template <> struct VertexTypeMapping<GL_BYTE, false>                        : VertexTypeMappingBase<D3DVT_SHORT> { };                       // Cast
364 template <> struct VertexTypeMapping<GL_BYTE, true>                         : VertexTypeMappingBase<D3DVT_FLOAT> { };                       // Normalize
365 template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false>               : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { };          // Identity, Cast
366 template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true>                : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { };     // Identity, Normalize
367 template <> struct VertexTypeMapping<GL_SHORT, false>                       : VertexTypeMappingBase<D3DVT_SHORT> { };                       // Identity
368 template <> struct VertexTypeMapping<GL_SHORT, true>                        : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { };     // Cast, Normalize
369 template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false>              : VertexTypeMappingBase<D3DVT_FLOAT> { };                       // Cast
370 template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true>               : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { };    // Cast, Normalize
371 template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized>   : VertexTypeMappingBase<D3DVT_FLOAT> { };                       // FixedToFloat
372 template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized>   : VertexTypeMappingBase<D3DVT_FLOAT> { };                       // Identity
373
374
375 // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
376 // The conversion rules themselves are defined in vertexconversion.h.
377
378 // Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
379 template <GLenum fromType, bool normalized, unsigned int toType>
380 struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
381 {
382 };
383
384 // All conversions from normalized types to float use the Normalize operator.
385 template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
386
387 // Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
388 template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
389 template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
390
391 // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
392 // whether it is normalized or not.
393 template <class T, bool normalized>
394 struct DefaultVertexValuesStage2
395 {
396 };
397
398 template <class T> struct DefaultVertexValuesStage2<T, true>  : gl::NormalizedDefaultValues<T> { };
399 template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
400
401 // Work out the default value rule for a D3D type (expressed as the C type) and 
402 template <class T, bool normalized>
403 struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
404 {
405 };
406
407 template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
408
409 // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
410 // The fallback conversion produces an output that all D3D9 devices must support.
411 template <class T> struct UsePreferred { enum { type = T::preferred }; };
412 template <class T> struct UseFallback { enum { type = T::fallback }; };
413
414 // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
415 // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
416 // and the D3DDECLTYPE member needed for the vertex declaration in declflag.
417 template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
418 struct Converter
419     : gl::VertexDataConverter<typename GLToCType<fromType>::type,
420                               WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
421                               ConversionRule<fromType,
422                                              normalized,
423                                              PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
424                               DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
425 {
426 private:
427     enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
428     enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
429
430 public:
431     enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
432     enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
433 };
434
435 // Initialise a TranslationInfo
436 #define TRANSLATION(type, norm, size, preferred)                                    \
437     {                                                                               \
438         Converter<type, norm, size, preferred>::identity,                           \
439         Converter<type, norm, size, preferred>::finalSize,                          \
440         Converter<type, norm, size, preferred>::convertArray,                       \
441         static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag)  \
442     }
443
444 #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size)    \
445     {                                                       \
446         Converter<type, norm, size, UsePreferred>::capflag, \
447         TRANSLATION(type, norm, size, UsePreferred),        \
448         TRANSLATION(type, norm, size, UseFallback)          \
449     }
450
451 #define TRANSLATIONS_FOR_TYPE(type)                                                                                                                                                                         \
452     {                                                                                                                                                                                                       \
453         { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \
454         { TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) },     \
455     }
456
457 const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
458 {
459     TRANSLATIONS_FOR_TYPE(GL_BYTE),
460     TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
461     TRANSLATIONS_FOR_TYPE(GL_SHORT),
462     TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
463     TRANSLATIONS_FOR_TYPE(GL_FIXED),
464     TRANSLATIONS_FOR_TYPE(GL_FLOAT)
465 };
466
467 void VertexDataManager::checkVertexCaps(DWORD declTypes)
468 {
469     for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
470     {
471         for (unsigned int j = 0; j < 2; j++)
472         {
473             for (unsigned int k = 0; k < 4; k++)
474             {
475                 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
476                 {
477                     mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
478                 }
479                 else
480                 {
481                     mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
482                 }
483             }
484         }
485     }
486 }
487
488 // This is used to index mAttributeTypes and mPossibleTranslations.
489 unsigned int VertexDataManager::typeIndex(GLenum type) const
490 {
491     switch (type)
492     {
493       case GL_BYTE: return 0;
494       case GL_UNSIGNED_BYTE: return 1;
495       case GL_SHORT: return 2;
496       case GL_UNSIGNED_SHORT: return 3;
497       case GL_FIXED: return 4;
498       case GL_FLOAT: return 5;
499
500       default: UNREACHABLE(); return 5;
501     }
502 }
503
504 VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
505 {
506     if (size > 0)
507     {
508         D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
509         HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
510         
511         if (FAILED(result))
512         {
513             ERR("Out of memory allocating a vertex buffer of size %lu.", size);
514         }
515     }
516 }
517
518 VertexBuffer::~VertexBuffer()
519 {
520     if (mVertexBuffer)
521     {
522         mVertexBuffer->Release();
523     }
524 }
525
526 void VertexBuffer::unmap()
527 {
528     if (mVertexBuffer)
529     {
530         mVertexBuffer->Unlock();
531     }
532 }
533
534 IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
535 {
536     return mVertexBuffer;
537 }
538
539 ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
540 {
541     void *buffer = NULL;
542
543     if (mVertexBuffer)
544     {
545         HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
546      
547         if (FAILED(result))
548         {
549             ERR("Lock failed with error 0x%08x", result);
550         }
551     }
552
553     if (buffer)
554     {
555         float *vector = (float*)buffer;
556
557         vector[0] = x;
558         vector[1] = y;
559         vector[2] = z;
560         vector[3] = w;
561
562         mVertexBuffer->Unlock();
563     }
564 }
565
566 ConstantVertexBuffer::~ConstantVertexBuffer()
567 {
568 }
569
570 ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
571 {
572     mBufferSize = size;
573     mWritePosition = 0;
574     mRequiredSpace = 0;
575 }
576
577 ArrayVertexBuffer::~ArrayVertexBuffer()
578 {
579 }
580
581 void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
582 {
583     mRequiredSpace += requiredSpace;
584 }
585
586 void ArrayVertexBuffer::addRequiredSpaceFor(ArrayVertexBuffer *buffer)
587 {
588     mRequiredSpace += buffer->mRequiredSpace;
589 }
590
591 StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
592 {
593 }
594
595 StreamingVertexBuffer::~StreamingVertexBuffer()
596 {
597 }
598
599 void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
600 {
601     void *mapPtr = NULL;
602
603     if (mVertexBuffer)
604     {
605         HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
606         
607         if (FAILED(result))
608         {
609             ERR("Lock failed with error 0x%08x", result);
610             return NULL;
611         }
612
613         *offset = mWritePosition;
614         mWritePosition += requiredSpace;
615     }
616
617     return mapPtr;
618 }
619
620 void StreamingVertexBuffer::reserveRequiredSpace()
621 {
622     if (mRequiredSpace > mBufferSize)
623     {
624         if (mVertexBuffer)
625         {
626             mVertexBuffer->Release();
627             mVertexBuffer = NULL;
628         }
629
630         mBufferSize = std::max(mRequiredSpace, 3 * mBufferSize / 2);   // 1.5 x mBufferSize is arbitrary and should be checked to see we don't have too many reallocations.
631
632         D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
633         HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
634     
635         if (FAILED(result))
636         {
637             ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
638         }
639
640         mWritePosition = 0;
641     }
642     else if (mWritePosition + mRequiredSpace > mBufferSize)   // Recycle
643     {
644         if (mVertexBuffer)
645         {
646             void *dummy;
647             mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
648             mVertexBuffer->Unlock();
649         }
650
651         mWritePosition = 0;
652     }
653
654     mRequiredSpace = 0;
655 }
656
657 StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
658 {
659 }
660
661 StaticVertexBuffer::~StaticVertexBuffer()
662 {
663 }
664
665 void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, UINT *streamOffset)
666 {
667     void *mapPtr = NULL;
668
669     if (mVertexBuffer)
670     {
671         HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
672         
673         if (FAILED(result))
674         {
675             ERR("Lock failed with error 0x%08x", result);
676             return NULL;
677         }
678
679         int attributeOffset = attribute.mOffset % attribute.stride();
680         VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
681         mCache.push_back(element);
682
683         *streamOffset = mWritePosition;
684         mWritePosition += requiredSpace;
685     }
686
687     return mapPtr;
688 }
689
690 void StaticVertexBuffer::reserveRequiredSpace()
691 {
692     if (!mVertexBuffer && mBufferSize == 0)
693     {
694         D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
695         HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
696     
697         if (FAILED(result))
698         {
699             ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
700         }
701
702         mBufferSize = mRequiredSpace;
703     }
704     else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
705     {
706         // Already allocated
707     }
708     else UNREACHABLE();   // Static vertex buffers can't be resized
709
710     mRequiredSpace = 0;
711 }
712
713 UINT StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
714 {
715     for (unsigned int element = 0; element < mCache.size(); element++)
716     {
717         if (mCache[element].type == attribute.mType &&  mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
718         {
719             if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
720             {
721                 return mCache[element].streamOffset;
722             }
723         }
724     }
725
726     return -1;
727 }
728
729 const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
730 {
731     return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
732 }
733 }