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 // VertexDataManager.h: Defines the VertexDataManager, a class that
8 // runs the Buffer translation process.
10 #include "libGLESv2/VertexDataManager.h"
12 #include "common/debug.h"
14 #include "libGLESv2/Buffer.h"
15 #include "libGLESv2/Program.h"
16 #include "libGLESv2/main.h"
18 #include "libGLESv2/vertexconversion.h"
19 #include "libGLESv2/IndexDataManager.h"
23 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
29 VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device)
31 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
33 mDirtyCurrentValue[i] = true;
34 mCurrentValueBuffer[i] = NULL;
37 const D3DCAPS9 &caps = context->getDeviceCaps();
38 checkVertexCaps(caps.DeclTypes);
40 mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
42 if (!mStreamingBuffer)
44 ERR("Failed to allocate the streaming vertex buffer.");
48 VertexDataManager::~VertexDataManager()
50 delete mStreamingBuffer;
52 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
54 delete mCurrentValueBuffer[i];
58 UINT VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
60 Buffer *buffer = attribute.mBoundBuffer.get();
62 int inputStride = attribute.stride();
63 int elementSize = attribute.typeSize();
64 const FormatConverter &converter = formatConverter(attribute);
65 UINT streamOffset = 0;
71 output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
76 ERR("Failed to map vertex buffer.");
80 const char *input = NULL;
84 int offset = attribute.mOffset;
86 input = static_cast<const char*>(buffer->data()) + offset;
90 input = static_cast<const char*>(attribute.mPointer);
93 input += inputStride * start;
95 if (converter.identity && inputStride == elementSize)
97 memcpy(output, input, count * inputStride);
101 converter.convertArray(input, inputStride, count, output);
104 vertexBuffer->unmap();
109 GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
111 if (!mStreamingBuffer)
113 return GL_OUT_OF_MEMORY;
116 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
117 Program *program = mContext->getCurrentProgram();
119 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
121 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
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++)
127 if (translated[i].active && attribs[i].mArrayEnabled)
129 Buffer *buffer = attribs[i].mBoundBuffer.get();
130 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
134 if (staticBuffer->size() == 0)
136 int totalCount = buffer->size() / attribs[i].stride();
137 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
139 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
141 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
142 mStreamingBuffer->addRequiredSpaceFor(staticBuffer);
143 buffer->invalidateStaticData();
145 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
150 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
155 // Reserve the required space per used buffer
156 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
158 if (translated[i].active && attribs[i].mArrayEnabled)
160 Buffer *buffer = attribs[i].mBoundBuffer.get();
161 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
162 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
166 vertexBuffer->reserveRequiredSpace();
171 // Perform the vertex data translations
172 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
174 if (translated[i].active)
176 if (attribs[i].mArrayEnabled)
178 Buffer *buffer = attribs[i].mBoundBuffer.get();
180 if (!buffer && attribs[i].mPointer == NULL)
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;
187 const FormatConverter &converter = formatConverter(attribs[i]);
189 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
190 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
192 UINT streamOffset = -1;
196 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
198 if (streamOffset == -1)
200 // Convert the entire buffer
201 int totalCount = buffer->size() / attribs[i].stride();
202 int startIndex = attribs[i].mOffset / attribs[i].stride();
204 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
207 if (streamOffset != -1)
209 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
214 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
217 if (streamOffset == -1)
219 return GL_OUT_OF_MEMORY;
222 translated[i].vertexBuffer = vertexBuffer->getBuffer();
223 translated[i].type = converter.d3dDeclType;
224 translated[i].stride = converter.outputElementSize;
225 translated[i].offset = streamOffset;
229 if (mDirtyCurrentValue[i])
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;
236 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
238 translated[i].type = D3DDECLTYPE_FLOAT4;
239 translated[i].stride = 0;
240 translated[i].offset = 0;
245 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
247 if (translated[i].active && attribs[i].mArrayEnabled)
249 Buffer *buffer = attribs[i].mBoundBuffer.get();
253 buffer->promoteStaticUsage(count * attribs[i].typeSize());
261 std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
263 return formatConverter(attrib).outputElementSize * count;
266 // Mapping from OpenGL-ES vertex attrib type to D3D decl type:
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)
279 // GLToCType maps from GL type (as GLenum) to the C typedef.
280 template <GLenum GLType> struct GLToCType { };
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; };
289 // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
300 // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
301 template <unsigned int D3DType> struct D3DToCType { };
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; };
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>
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> { };
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
329 template <unsigned int capflag, unsigned int declflag>
330 struct VertexTypeFlagsHelper
332 enum { capflag = capflag };
333 enum { declflag = declflag };
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> { };
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
356 template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
357 struct VertexTypeMappingBase
359 enum { preferred = Preferred };
360 enum { fallback = Fallback };
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
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.
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>
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> { };
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> { };
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
398 template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
399 template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
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>
407 template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
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 }; };
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>
419 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
420 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
421 ConversionRule<fromType,
423 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
424 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
427 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
428 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
431 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
432 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
435 // Initialise a TranslationInfo
436 #define TRANSLATION(type, norm, size, preferred) \
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) \
444 #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
446 Converter<type, norm, size, UsePreferred>::capflag, \
447 TRANSLATION(type, norm, size, UsePreferred), \
448 TRANSLATION(type, norm, size, UseFallback) \
451 #define TRANSLATIONS_FOR_TYPE(type) \
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) }, \
457 const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
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)
467 void VertexDataManager::checkVertexCaps(DWORD declTypes)
469 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
471 for (unsigned int j = 0; j < 2; j++)
473 for (unsigned int k = 0; k < 4; k++)
475 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
477 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
481 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
488 // This is used to index mAttributeTypes and mPossibleTranslations.
489 unsigned int VertexDataManager::typeIndex(GLenum type) const
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;
500 default: UNREACHABLE(); return 5;
504 VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
508 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
509 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
513 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
518 VertexBuffer::~VertexBuffer()
522 mVertexBuffer->Release();
526 void VertexBuffer::unmap()
530 mVertexBuffer->Unlock();
534 IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
536 return mVertexBuffer;
539 ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
545 HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
549 ERR("Lock failed with error 0x%08x", result);
555 float *vector = (float*)buffer;
562 mVertexBuffer->Unlock();
566 ConstantVertexBuffer::~ConstantVertexBuffer()
570 ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
577 ArrayVertexBuffer::~ArrayVertexBuffer()
581 void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
583 mRequiredSpace += requiredSpace;
586 void ArrayVertexBuffer::addRequiredSpaceFor(ArrayVertexBuffer *buffer)
588 mRequiredSpace += buffer->mRequiredSpace;
591 StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
595 StreamingVertexBuffer::~StreamingVertexBuffer()
599 void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
605 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
609 ERR("Lock failed with error 0x%08x", result);
613 *offset = mWritePosition;
614 mWritePosition += requiredSpace;
620 void StreamingVertexBuffer::reserveRequiredSpace()
622 if (mRequiredSpace > mBufferSize)
626 mVertexBuffer->Release();
627 mVertexBuffer = NULL;
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.
632 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
633 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
637 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
642 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
647 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
648 mVertexBuffer->Unlock();
657 StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
661 StaticVertexBuffer::~StaticVertexBuffer()
665 void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, UINT *streamOffset)
671 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
675 ERR("Lock failed with error 0x%08x", result);
679 int attributeOffset = attribute.mOffset % attribute.stride();
680 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
681 mCache.push_back(element);
683 *streamOffset = mWritePosition;
684 mWritePosition += requiredSpace;
690 void StaticVertexBuffer::reserveRequiredSpace()
692 if (!mVertexBuffer && mBufferSize == 0)
694 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
695 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
699 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
702 mBufferSize = mRequiredSpace;
704 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
708 else UNREACHABLE(); // Static vertex buffers can't be resized
713 UINT StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
715 for (unsigned int element = 0; element < mCache.size(); element++)
717 if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
719 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
721 return mCache[element].streamOffset;
729 const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
731 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];