blob: d61968dd3cd16e0d2967dc5a1ea04ec8f36ce9a7 [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{
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000028unsigned int VertexBuffer::mCurrentSerial = 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000029
daniel@transgaming.combaa74512011-04-13 14:56:47 +000030VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000031{
daniel@transgaming.com83921382011-01-08 05:46:00 +000032 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000033 {
daniel@transgaming.com83921382011-01-08 05:46:00 +000034 mDirtyCurrentValue[i] = true;
35 mCurrentValueBuffer[i] = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000036 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000037
38 const D3DCAPS9 &caps = context->getDeviceCaps();
39 checkVertexCaps(caps.DeclTypes);
40
41 mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000042
43 if (!mStreamingBuffer)
44 {
45 ERR("Failed to allocate the streaming vertex buffer.");
46 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000047}
48
49VertexDataManager::~VertexDataManager()
50{
daniel@transgaming.com83921382011-01-08 05:46:00 +000051 delete mStreamingBuffer;
52
53 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
54 {
55 delete mCurrentValueBuffer[i];
56 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000057}
58
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000059std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000060{
daniel@transgaming.com83921382011-01-08 05:46:00 +000061 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000062
daniel@transgaming.com83921382011-01-08 05:46:00 +000063 int inputStride = attribute.stride();
64 int elementSize = attribute.typeSize();
65 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000066 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000067
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000068 void *output = NULL;
69
70 if (vertexBuffer)
71 {
72 output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
73 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000074
75 if (output == NULL)
76 {
77 ERR("Failed to map vertex buffer.");
78 return -1;
79 }
80
81 const char *input = NULL;
82
83 if (buffer)
84 {
85 int offset = attribute.mOffset;
86
87 input = static_cast<const char*>(buffer->data()) + offset;
88 }
89 else
90 {
91 input = static_cast<const char*>(attribute.mPointer);
92 }
93
94 input += inputStride * start;
95
96 if (converter.identity && inputStride == elementSize)
97 {
98 memcpy(output, input, count * inputStride);
99 }
100 else
101 {
102 converter.convertArray(input, inputStride, count, output);
103 }
104
105 vertexBuffer->unmap();
106
107 return streamOffset;
108}
109
110GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
111{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000112 if (!mStreamingBuffer)
113 {
114 return GL_OUT_OF_MEMORY;
115 }
116
daniel@transgaming.com83921382011-01-08 05:46:00 +0000117 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000118 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000119
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000120 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000121 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000122 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000123 }
124
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000125 // 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 +0000126 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
127 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000128 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000129 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000130 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000131 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000132
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000133 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000134 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000135 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000136 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000137 int totalCount = buffer->size() / attribs[i].stride();
138 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000139 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000140 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000141 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000142 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000143 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
144 for (int previous = 0; previous < i; previous++)
145 {
146 if (translated[previous].active && attribs[previous].mArrayEnabled)
147 {
148 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
149 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
150
151 if (staticBuffer == previousStaticBuffer)
152 {
153 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count));
154 }
155 }
156 }
157
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000158 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000159
160 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000161 }
162 }
163 else
164 {
165 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000166 }
167 }
168 }
169
170 // Reserve the required space per used buffer
171 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
172 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000173 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000174 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000175 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000176 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000177 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
178
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000179 if (vertexBuffer)
180 {
181 vertexBuffer->reserveRequiredSpace();
182 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000183 }
184 }
185
186 // Perform the vertex data translations
187 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
188 {
189 if (translated[i].active)
190 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000191 if (attribs[i].mArrayEnabled)
192 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000193 Buffer *buffer = attribs[i].mBoundBuffer.get();
194
daniel@transgaming.com83921382011-01-08 05:46:00 +0000195 if (!buffer && attribs[i].mPointer == NULL)
196 {
197 // This is an application error that would normally result in a crash, but we catch it and return an error
198 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000199 return GL_INVALID_OPERATION;
200 }
201
daniel@transgaming.com83921382011-01-08 05:46:00 +0000202 const FormatConverter &converter = formatConverter(attribs[i]);
203
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000204 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000205 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
206
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000207 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000208
209 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000210 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000211 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
212
213 if (streamOffset == -1)
214 {
215 // Convert the entire buffer
216 int totalCount = buffer->size() / attribs[i].stride();
217 int startIndex = attribs[i].mOffset / attribs[i].stride();
218
219 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
220 }
221
222 if (streamOffset != -1)
223 {
224 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
225 }
226 }
227 else
228 {
229 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000230 }
231
daniel@transgaming.com83921382011-01-08 05:46:00 +0000232 if (streamOffset == -1)
233 {
234 return GL_OUT_OF_MEMORY;
235 }
236
237 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000238 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000239 translated[i].type = converter.d3dDeclType;
240 translated[i].stride = converter.outputElementSize;
241 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000242 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000243 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000244 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000245 if (mDirtyCurrentValue[i])
246 {
247 delete mCurrentValueBuffer[i];
248 mCurrentValueBuffer[i] = new ConstantVertexBuffer(mDevice, attribs[i].mCurrentValue[0], attribs[i].mCurrentValue[1], attribs[i].mCurrentValue[2], attribs[i].mCurrentValue[3]);
249 mDirtyCurrentValue[i] = false;
250 }
251
252 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000253 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000254
255 translated[i].type = D3DDECLTYPE_FLOAT4;
256 translated[i].stride = 0;
257 translated[i].offset = 0;
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000258 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000259 }
260 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000261
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000262 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
263 {
264 if (translated[i].active && attribs[i].mArrayEnabled)
265 {
266 Buffer *buffer = attribs[i].mBoundBuffer.get();
267
268 if (buffer)
269 {
270 buffer->promoteStaticUsage(count * attribs[i].typeSize());
271 }
272 }
273 }
274
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000275 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000276}
277
daniel@transgaming.com83921382011-01-08 05:46:00 +0000278std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
279{
280 return formatConverter(attrib).outputElementSize * count;
281}
282
283// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
284//
285// BYTE SHORT (Cast)
286// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
287// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
288// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
289// SHORT SHORT (Identity)
290// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
291// UNSIGNED_SHORT FLOAT (Cast)
292// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
293// FIXED (not in WebGL) FLOAT (FixedToFloat)
294// FLOAT FLOAT (Identity)
295
296// GLToCType maps from GL type (as GLenum) to the C typedef.
297template <GLenum GLType> struct GLToCType { };
298
299template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
300template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
301template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
302template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
303template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
304template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
305
306// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
307enum D3DVertexType
308{
309 D3DVT_FLOAT,
310 D3DVT_SHORT,
311 D3DVT_SHORT_NORM,
312 D3DVT_UBYTE,
313 D3DVT_UBYTE_NORM,
314 D3DVT_USHORT_NORM
315};
316
317// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
318template <unsigned int D3DType> struct D3DToCType { };
319
320template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
321template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
322template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
323template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
324template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
325template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
326
327// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
328template <unsigned int type, int size>
329struct WidenRule
330{
331};
332
333template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
334template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
335template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
336template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
337template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
338template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
339
340// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
341template <unsigned int d3dtype, int size>
342struct VertexTypeFlags
343{
344};
345
346template <unsigned int capflag, unsigned int declflag>
347struct VertexTypeFlagsHelper
348{
349 enum { capflag = capflag };
350 enum { declflag = declflag };
351};
352
353template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
354template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
355template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
356template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
357template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
358template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
359template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
360template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
361template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
362template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
363template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
364template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
365
366
367// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
368template <GLenum GLtype, bool normalized>
369struct VertexTypeMapping
370{
371};
372
373template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
374struct VertexTypeMappingBase
375{
376 enum { preferred = Preferred };
377 enum { fallback = Fallback };
378};
379
380template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
381template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
382template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
383template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
384template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
385template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
386template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
387template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
388template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
389template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
390
391
392// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
393// The conversion rules themselves are defined in vertexconversion.h.
394
395// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
396template <GLenum fromType, bool normalized, unsigned int toType>
397struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
398{
399};
400
401// All conversions from normalized types to float use the Normalize operator.
402template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
403
404// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
405template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
406template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
407
408// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
409// whether it is normalized or not.
410template <class T, bool normalized>
411struct DefaultVertexValuesStage2
412{
413};
414
415template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
416template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
417
418// Work out the default value rule for a D3D type (expressed as the C type) and
419template <class T, bool normalized>
420struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
421{
422};
423
424template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
425
426// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
427// The fallback conversion produces an output that all D3D9 devices must support.
428template <class T> struct UsePreferred { enum { type = T::preferred }; };
429template <class T> struct UseFallback { enum { type = T::fallback }; };
430
431// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
432// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
433// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
434template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
435struct Converter
436 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
437 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
438 ConversionRule<fromType,
439 normalized,
440 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
441 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
442{
443private:
444 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
445 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
446
447public:
448 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
449 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
450};
451
452// Initialise a TranslationInfo
453#define TRANSLATION(type, norm, size, preferred) \
454 { \
455 Converter<type, norm, size, preferred>::identity, \
456 Converter<type, norm, size, preferred>::finalSize, \
457 Converter<type, norm, size, preferred>::convertArray, \
458 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
459 }
460
461#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
462 { \
463 Converter<type, norm, size, UsePreferred>::capflag, \
464 TRANSLATION(type, norm, size, UsePreferred), \
465 TRANSLATION(type, norm, size, UseFallback) \
466 }
467
468#define TRANSLATIONS_FOR_TYPE(type) \
469 { \
470 { 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) }, \
471 { 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) }, \
472 }
473
474const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
475{
476 TRANSLATIONS_FOR_TYPE(GL_BYTE),
477 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
478 TRANSLATIONS_FOR_TYPE(GL_SHORT),
479 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
480 TRANSLATIONS_FOR_TYPE(GL_FIXED),
481 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
482};
483
484void VertexDataManager::checkVertexCaps(DWORD declTypes)
485{
486 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
487 {
488 for (unsigned int j = 0; j < 2; j++)
489 {
490 for (unsigned int k = 0; k < 4; k++)
491 {
492 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
493 {
494 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
495 }
496 else
497 {
498 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
499 }
500 }
501 }
502 }
503}
504
505// This is used to index mAttributeTypes and mPossibleTranslations.
506unsigned int VertexDataManager::typeIndex(GLenum type) const
507{
508 switch (type)
509 {
510 case GL_BYTE: return 0;
511 case GL_UNSIGNED_BYTE: return 1;
512 case GL_SHORT: return 2;
513 case GL_UNSIGNED_SHORT: return 3;
514 case GL_FIXED: return 4;
515 case GL_FLOAT: return 5;
516
517 default: UNREACHABLE(); return 5;
518 }
519}
520
daniel@transgaming.com83921382011-01-08 05:46:00 +0000521VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
522{
523 if (size > 0)
524 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000525 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000526 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000527 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000528
529 if (FAILED(result))
530 {
531 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
532 }
533 }
534}
535
536VertexBuffer::~VertexBuffer()
537{
538 if (mVertexBuffer)
539 {
540 mVertexBuffer->Release();
541 }
542}
543
544void VertexBuffer::unmap()
545{
546 if (mVertexBuffer)
547 {
548 mVertexBuffer->Unlock();
549 }
550}
551
552IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
553{
554 return mVertexBuffer;
555}
556
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000557unsigned int VertexBuffer::getSerial() const
558{
559 return mSerial;
560}
561
562unsigned int VertexBuffer::issueSerial()
563{
564 return mCurrentSerial++;
565}
566
daniel@transgaming.com83921382011-01-08 05:46:00 +0000567ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
568{
569 void *buffer = NULL;
570
571 if (mVertexBuffer)
572 {
573 HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
574
575 if (FAILED(result))
576 {
577 ERR("Lock failed with error 0x%08x", result);
578 }
579 }
580
581 if (buffer)
582 {
583 float *vector = (float*)buffer;
584
585 vector[0] = x;
586 vector[1] = y;
587 vector[2] = z;
588 vector[3] = w;
589
590 mVertexBuffer->Unlock();
591 }
592}
593
594ConstantVertexBuffer::~ConstantVertexBuffer()
595{
596}
597
598ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
599{
600 mBufferSize = size;
601 mWritePosition = 0;
602 mRequiredSpace = 0;
603}
604
605ArrayVertexBuffer::~ArrayVertexBuffer()
606{
607}
608
609void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
610{
611 mRequiredSpace += requiredSpace;
612}
613
daniel@transgaming.com83921382011-01-08 05:46:00 +0000614StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
615{
616}
617
618StreamingVertexBuffer::~StreamingVertexBuffer()
619{
620}
621
622void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
623{
624 void *mapPtr = NULL;
625
626 if (mVertexBuffer)
627 {
628 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
629
630 if (FAILED(result))
631 {
632 ERR("Lock failed with error 0x%08x", result);
633 return NULL;
634 }
635
636 *offset = mWritePosition;
637 mWritePosition += requiredSpace;
638 }
639
640 return mapPtr;
641}
642
643void StreamingVertexBuffer::reserveRequiredSpace()
644{
645 if (mRequiredSpace > mBufferSize)
646 {
647 if (mVertexBuffer)
648 {
649 mVertexBuffer->Release();
650 mVertexBuffer = NULL;
651 }
652
653 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 +0000654
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000655 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000656 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000657 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000658
659 if (FAILED(result))
660 {
661 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
662 }
663
664 mWritePosition = 0;
665 }
666 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
667 {
668 if (mVertexBuffer)
669 {
670 void *dummy;
671 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
672 mVertexBuffer->Unlock();
673 }
674
675 mWritePosition = 0;
676 }
677
678 mRequiredSpace = 0;
679}
680
681StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
682{
683}
684
685StaticVertexBuffer::~StaticVertexBuffer()
686{
687}
688
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000689void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000690{
691 void *mapPtr = NULL;
692
693 if (mVertexBuffer)
694 {
695 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
696
697 if (FAILED(result))
698 {
699 ERR("Lock failed with error 0x%08x", result);
700 return NULL;
701 }
702
703 int attributeOffset = attribute.mOffset % attribute.stride();
704 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
705 mCache.push_back(element);
706
707 *streamOffset = mWritePosition;
708 mWritePosition += requiredSpace;
709 }
710
711 return mapPtr;
712}
713
714void StaticVertexBuffer::reserveRequiredSpace()
715{
716 if (!mVertexBuffer && mBufferSize == 0)
717 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000718 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000719 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000720 mSerial = issueSerial();
721
daniel@transgaming.com83921382011-01-08 05:46:00 +0000722 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
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000738std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000739{
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}