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