blob: 4d4f66a2b4ca32537c6596056aed8e515be21fca [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
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000010#include "libGLESv2/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.com8fd34bd2011-02-18 02:52:14 +000018#include "libGLESv2/vertexconversion.h"
19#include "libGLESv2/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.combaa74512011-04-13 14:56:47 +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.com72b9e182011-04-13 14:58:33 +000041
42 if (!mStreamingBuffer)
43 {
44 ERR("Failed to allocate the streaming vertex buffer.");
45 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000046}
47
48VertexDataManager::~VertexDataManager()
49{
daniel@transgaming.com83921382011-01-08 05:46:00 +000050 delete mStreamingBuffer;
51
52 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
53 {
54 delete mCurrentValueBuffer[i];
55 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000056}
57
daniel@transgaming.com83921382011-01-08 05:46:00 +000058UINT VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000059{
daniel@transgaming.com83921382011-01-08 05:46:00 +000060 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000061
daniel@transgaming.com83921382011-01-08 05:46:00 +000062 int inputStride = attribute.stride();
63 int elementSize = attribute.typeSize();
64 const FormatConverter &converter = formatConverter(attribute);
65 UINT streamOffset = 0;
66
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000067 void *output = NULL;
68
69 if (vertexBuffer)
70 {
71 output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
72 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000073
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
109GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
110{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000111 if (!mStreamingBuffer)
112 {
113 return GL_OUT_OF_MEMORY;
114 }
115
daniel@transgaming.com83921382011-01-08 05:46:00 +0000116 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000117 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000118
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000119 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000120 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000121 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000122 }
123
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000124 // Determine the required storage size per used buffer, and invalidate static buffers that don't contain matching attributes
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000125 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
126 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000127 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000128 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000129 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000130 StaticVertexBuffer *staticBuffer = buffer ? buffer->getVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000131
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000132 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000133 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000134 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000135 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000136 int totalCount = buffer->size() / attribs[i].stride();
137 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000138 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000139 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000140 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000141 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.com83921382011-01-08 05:46:00 +0000142 mStreamingBuffer->addRequiredSpaceFor(staticBuffer);
143 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000144
145 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
146 }
147 }
148 else
149 {
150 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000151 }
152 }
153 }
154
155 // Reserve the required space per used buffer
156 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
157 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000158 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000159 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000160 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000161 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getVertexBuffer() : NULL;
162 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
163
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000164 if (vertexBuffer)
165 {
166 vertexBuffer->reserveRequiredSpace();
167 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000168 }
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 Buffer *buffer = attribs[i].mBoundBuffer.get();
177
178 if (attribs[i].mArrayEnabled)
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.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000184 return GL_INVALID_OPERATION;
185 }
186
daniel@transgaming.com83921382011-01-08 05:46:00 +0000187 const FormatConverter &converter = formatConverter(attribs[i]);
188
189 StaticVertexBuffer *staticBuffer = buffer ? buffer->getVertexBuffer() : NULL;
190 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
191
192 UINT streamOffset = -1;
193
194 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000195 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000196 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]);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000215 }
216
daniel@transgaming.com83921382011-01-08 05:46:00 +0000217 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;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000226 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000227 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000228 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000229 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;
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000241 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000242 }
243 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000244
245 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000246}
247
daniel@transgaming.com83921382011-01-08 05:46:00 +0000248std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
249{
250 return formatConverter(attrib).outputElementSize * count;
251}
252
253// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
254//
255// BYTE SHORT (Cast)
256// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
257// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
258// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
259// SHORT SHORT (Identity)
260// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
261// UNSIGNED_SHORT FLOAT (Cast)
262// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
263// FIXED (not in WebGL) FLOAT (FixedToFloat)
264// FLOAT FLOAT (Identity)
265
266// GLToCType maps from GL type (as GLenum) to the C typedef.
267template <GLenum GLType> struct GLToCType { };
268
269template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
270template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
271template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
272template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
273template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
274template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
275
276// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
277enum D3DVertexType
278{
279 D3DVT_FLOAT,
280 D3DVT_SHORT,
281 D3DVT_SHORT_NORM,
282 D3DVT_UBYTE,
283 D3DVT_UBYTE_NORM,
284 D3DVT_USHORT_NORM
285};
286
287// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
288template <unsigned int D3DType> struct D3DToCType { };
289
290template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
291template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
292template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
293template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
294template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
295template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
296
297// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
298template <unsigned int type, int size>
299struct WidenRule
300{
301};
302
303template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
304template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
305template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
306template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
307template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
308template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
309
310// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
311template <unsigned int d3dtype, int size>
312struct VertexTypeFlags
313{
314};
315
316template <unsigned int capflag, unsigned int declflag>
317struct VertexTypeFlagsHelper
318{
319 enum { capflag = capflag };
320 enum { declflag = declflag };
321};
322
323template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
324template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
325template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
326template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
327template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
328template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
329template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
330template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
331template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
332template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
333template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
334template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
335
336
337// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
338template <GLenum GLtype, bool normalized>
339struct VertexTypeMapping
340{
341};
342
343template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
344struct VertexTypeMappingBase
345{
346 enum { preferred = Preferred };
347 enum { fallback = Fallback };
348};
349
350template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
351template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
352template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
353template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
354template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
355template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
356template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
357template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
358template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
359template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
360
361
362// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
363// The conversion rules themselves are defined in vertexconversion.h.
364
365// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
366template <GLenum fromType, bool normalized, unsigned int toType>
367struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
368{
369};
370
371// All conversions from normalized types to float use the Normalize operator.
372template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
373
374// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
375template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
376template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
377
378// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
379// whether it is normalized or not.
380template <class T, bool normalized>
381struct DefaultVertexValuesStage2
382{
383};
384
385template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
386template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
387
388// Work out the default value rule for a D3D type (expressed as the C type) and
389template <class T, bool normalized>
390struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
391{
392};
393
394template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
395
396// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
397// The fallback conversion produces an output that all D3D9 devices must support.
398template <class T> struct UsePreferred { enum { type = T::preferred }; };
399template <class T> struct UseFallback { enum { type = T::fallback }; };
400
401// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
402// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
403// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
404template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
405struct Converter
406 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
407 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
408 ConversionRule<fromType,
409 normalized,
410 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
411 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
412{
413private:
414 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
415 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
416
417public:
418 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
419 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
420};
421
422// Initialise a TranslationInfo
423#define TRANSLATION(type, norm, size, preferred) \
424 { \
425 Converter<type, norm, size, preferred>::identity, \
426 Converter<type, norm, size, preferred>::finalSize, \
427 Converter<type, norm, size, preferred>::convertArray, \
428 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
429 }
430
431#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
432 { \
433 Converter<type, norm, size, UsePreferred>::capflag, \
434 TRANSLATION(type, norm, size, UsePreferred), \
435 TRANSLATION(type, norm, size, UseFallback) \
436 }
437
438#define TRANSLATIONS_FOR_TYPE(type) \
439 { \
440 { 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) }, \
441 { 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) }, \
442 }
443
444const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
445{
446 TRANSLATIONS_FOR_TYPE(GL_BYTE),
447 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
448 TRANSLATIONS_FOR_TYPE(GL_SHORT),
449 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
450 TRANSLATIONS_FOR_TYPE(GL_FIXED),
451 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
452};
453
454void VertexDataManager::checkVertexCaps(DWORD declTypes)
455{
456 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
457 {
458 for (unsigned int j = 0; j < 2; j++)
459 {
460 for (unsigned int k = 0; k < 4; k++)
461 {
462 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
463 {
464 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
465 }
466 else
467 {
468 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
469 }
470 }
471 }
472 }
473}
474
475// This is used to index mAttributeTypes and mPossibleTranslations.
476unsigned int VertexDataManager::typeIndex(GLenum type) const
477{
478 switch (type)
479 {
480 case GL_BYTE: return 0;
481 case GL_UNSIGNED_BYTE: return 1;
482 case GL_SHORT: return 2;
483 case GL_UNSIGNED_SHORT: return 3;
484 case GL_FIXED: return 4;
485 case GL_FLOAT: return 5;
486
487 default: UNREACHABLE(); return 5;
488 }
489}
490
daniel@transgaming.com83921382011-01-08 05:46:00 +0000491VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
492{
493 if (size > 0)
494 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000495 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000496 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000497
498 if (FAILED(result))
499 {
500 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
501 }
502 }
503}
504
505VertexBuffer::~VertexBuffer()
506{
507 if (mVertexBuffer)
508 {
509 mVertexBuffer->Release();
510 }
511}
512
513void VertexBuffer::unmap()
514{
515 if (mVertexBuffer)
516 {
517 mVertexBuffer->Unlock();
518 }
519}
520
521IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
522{
523 return mVertexBuffer;
524}
525
526ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
527{
528 void *buffer = NULL;
529
530 if (mVertexBuffer)
531 {
532 HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
533
534 if (FAILED(result))
535 {
536 ERR("Lock failed with error 0x%08x", result);
537 }
538 }
539
540 if (buffer)
541 {
542 float *vector = (float*)buffer;
543
544 vector[0] = x;
545 vector[1] = y;
546 vector[2] = z;
547 vector[3] = w;
548
549 mVertexBuffer->Unlock();
550 }
551}
552
553ConstantVertexBuffer::~ConstantVertexBuffer()
554{
555}
556
557ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
558{
559 mBufferSize = size;
560 mWritePosition = 0;
561 mRequiredSpace = 0;
562}
563
564ArrayVertexBuffer::~ArrayVertexBuffer()
565{
566}
567
568void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
569{
570 mRequiredSpace += requiredSpace;
571}
572
573void ArrayVertexBuffer::addRequiredSpaceFor(ArrayVertexBuffer *buffer)
574{
575 mRequiredSpace += buffer->mRequiredSpace;
576}
577
578StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
579{
580}
581
582StreamingVertexBuffer::~StreamingVertexBuffer()
583{
584}
585
586void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
587{
588 void *mapPtr = NULL;
589
590 if (mVertexBuffer)
591 {
592 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
593
594 if (FAILED(result))
595 {
596 ERR("Lock failed with error 0x%08x", result);
597 return NULL;
598 }
599
600 *offset = mWritePosition;
601 mWritePosition += requiredSpace;
602 }
603
604 return mapPtr;
605}
606
607void StreamingVertexBuffer::reserveRequiredSpace()
608{
609 if (mRequiredSpace > mBufferSize)
610 {
611 if (mVertexBuffer)
612 {
613 mVertexBuffer->Release();
614 mVertexBuffer = NULL;
615 }
616
617 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 +0000618
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000619 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000620 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000621
622 if (FAILED(result))
623 {
624 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
625 }
626
627 mWritePosition = 0;
628 }
629 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
630 {
631 if (mVertexBuffer)
632 {
633 void *dummy;
634 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
635 mVertexBuffer->Unlock();
636 }
637
638 mWritePosition = 0;
639 }
640
641 mRequiredSpace = 0;
642}
643
644StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
645{
646}
647
648StaticVertexBuffer::~StaticVertexBuffer()
649{
650}
651
652void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, UINT *streamOffset)
653{
654 void *mapPtr = NULL;
655
656 if (mVertexBuffer)
657 {
658 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
659
660 if (FAILED(result))
661 {
662 ERR("Lock failed with error 0x%08x", result);
663 return NULL;
664 }
665
666 int attributeOffset = attribute.mOffset % attribute.stride();
667 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
668 mCache.push_back(element);
669
670 *streamOffset = mWritePosition;
671 mWritePosition += requiredSpace;
672 }
673
674 return mapPtr;
675}
676
677void StaticVertexBuffer::reserveRequiredSpace()
678{
679 if (!mVertexBuffer && mBufferSize == 0)
680 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000681 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000682 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000683
684 if (FAILED(result))
685 {
686 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
687 }
688
689 mBufferSize = mRequiredSpace;
690 }
691 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
692 {
693 // Already allocated
694 }
695 else UNREACHABLE(); // Static vertex buffers can't be resized
696
697 mRequiredSpace = 0;
698}
699
700UINT StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
701{
702 for (unsigned int element = 0; element < mCache.size(); element++)
703 {
704 if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
705 {
706 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
707 {
708 return mCache[element].streamOffset;
709 }
710 }
711 }
712
713 return -1;
714}
715
716const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
717{
718 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
719}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000720}