blob: e339b10e585ede72be167f03c80ce778aec94688 [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.com58f76fe2011-06-21 14:21:07 +000058std::size_t 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);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000065 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000066
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.com78624ca2011-04-22 04:17:57 +0000130 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : 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 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000143
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000144 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
145 for (int previous = 0; previous < i; previous++)
146 {
147 if (translated[previous].active && attribs[previous].mArrayEnabled)
148 {
149 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
150 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
151
152 if (staticBuffer == previousStaticBuffer)
153 {
154 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count));
155 }
156 }
157 }
158
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000159 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
160 }
161 }
162 else
163 {
164 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000165 }
166 }
167 }
168
169 // Reserve the required space per used buffer
170 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
171 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000172 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000173 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000174 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000175 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000176 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
177
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000178 if (vertexBuffer)
179 {
180 vertexBuffer->reserveRequiredSpace();
181 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000182 }
183 }
184
185 // Perform the vertex data translations
186 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
187 {
188 if (translated[i].active)
189 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000190 if (attribs[i].mArrayEnabled)
191 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000192 Buffer *buffer = attribs[i].mBoundBuffer.get();
193
daniel@transgaming.com83921382011-01-08 05:46:00 +0000194 if (!buffer && attribs[i].mPointer == NULL)
195 {
196 // This is an application error that would normally result in a crash, but we catch it and return an error
197 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000198 return GL_INVALID_OPERATION;
199 }
200
daniel@transgaming.com83921382011-01-08 05:46:00 +0000201 const FormatConverter &converter = formatConverter(attribs[i]);
202
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000203 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000204 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
205
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000206 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000207
208 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000209 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000210 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
211
212 if (streamOffset == -1)
213 {
214 // Convert the entire buffer
215 int totalCount = buffer->size() / attribs[i].stride();
216 int startIndex = attribs[i].mOffset / attribs[i].stride();
217
218 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
219 }
220
221 if (streamOffset != -1)
222 {
223 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
224 }
225 }
226 else
227 {
228 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000229 }
230
daniel@transgaming.com83921382011-01-08 05:46:00 +0000231 if (streamOffset == -1)
232 {
233 return GL_OUT_OF_MEMORY;
234 }
235
236 translated[i].vertexBuffer = vertexBuffer->getBuffer();
237 translated[i].type = converter.d3dDeclType;
238 translated[i].stride = converter.outputElementSize;
239 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000241 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000242 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000243 if (mDirtyCurrentValue[i])
244 {
245 delete mCurrentValueBuffer[i];
246 mCurrentValueBuffer[i] = new ConstantVertexBuffer(mDevice, attribs[i].mCurrentValue[0], attribs[i].mCurrentValue[1], attribs[i].mCurrentValue[2], attribs[i].mCurrentValue[3]);
247 mDirtyCurrentValue[i] = false;
248 }
249
250 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
251
252 translated[i].type = D3DDECLTYPE_FLOAT4;
253 translated[i].stride = 0;
254 translated[i].offset = 0;
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000255 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000256 }
257 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000258
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000259 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
260 {
261 if (translated[i].active && attribs[i].mArrayEnabled)
262 {
263 Buffer *buffer = attribs[i].mBoundBuffer.get();
264
265 if (buffer)
266 {
267 buffer->promoteStaticUsage(count * attribs[i].typeSize());
268 }
269 }
270 }
271
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000272 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000273}
274
daniel@transgaming.com83921382011-01-08 05:46:00 +0000275std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
276{
277 return formatConverter(attrib).outputElementSize * count;
278}
279
280// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
281//
282// BYTE SHORT (Cast)
283// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
284// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
285// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
286// SHORT SHORT (Identity)
287// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
288// UNSIGNED_SHORT FLOAT (Cast)
289// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
290// FIXED (not in WebGL) FLOAT (FixedToFloat)
291// FLOAT FLOAT (Identity)
292
293// GLToCType maps from GL type (as GLenum) to the C typedef.
294template <GLenum GLType> struct GLToCType { };
295
296template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
297template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
298template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
299template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
300template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
301template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
302
303// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
304enum D3DVertexType
305{
306 D3DVT_FLOAT,
307 D3DVT_SHORT,
308 D3DVT_SHORT_NORM,
309 D3DVT_UBYTE,
310 D3DVT_UBYTE_NORM,
311 D3DVT_USHORT_NORM
312};
313
314// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
315template <unsigned int D3DType> struct D3DToCType { };
316
317template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
318template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
319template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
320template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
321template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
322template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
323
324// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
325template <unsigned int type, int size>
326struct WidenRule
327{
328};
329
330template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
331template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
332template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
333template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
334template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
335template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
336
337// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
338template <unsigned int d3dtype, int size>
339struct VertexTypeFlags
340{
341};
342
343template <unsigned int capflag, unsigned int declflag>
344struct VertexTypeFlagsHelper
345{
346 enum { capflag = capflag };
347 enum { declflag = declflag };
348};
349
350template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
351template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
352template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
353template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
354template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
355template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
356template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
357template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
358template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
359template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
360template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
361template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
362
363
364// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
365template <GLenum GLtype, bool normalized>
366struct VertexTypeMapping
367{
368};
369
370template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
371struct VertexTypeMappingBase
372{
373 enum { preferred = Preferred };
374 enum { fallback = Fallback };
375};
376
377template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
378template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
379template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
380template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
381template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
382template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
383template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
384template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
385template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
386template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
387
388
389// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
390// The conversion rules themselves are defined in vertexconversion.h.
391
392// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
393template <GLenum fromType, bool normalized, unsigned int toType>
394struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
395{
396};
397
398// All conversions from normalized types to float use the Normalize operator.
399template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
400
401// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
402template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
403template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
404
405// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
406// whether it is normalized or not.
407template <class T, bool normalized>
408struct DefaultVertexValuesStage2
409{
410};
411
412template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
413template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
414
415// Work out the default value rule for a D3D type (expressed as the C type) and
416template <class T, bool normalized>
417struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
418{
419};
420
421template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
422
423// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
424// The fallback conversion produces an output that all D3D9 devices must support.
425template <class T> struct UsePreferred { enum { type = T::preferred }; };
426template <class T> struct UseFallback { enum { type = T::fallback }; };
427
428// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
429// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
430// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
431template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
432struct Converter
433 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
434 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
435 ConversionRule<fromType,
436 normalized,
437 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
438 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
439{
440private:
441 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
442 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
443
444public:
445 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
446 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
447};
448
449// Initialise a TranslationInfo
450#define TRANSLATION(type, norm, size, preferred) \
451 { \
452 Converter<type, norm, size, preferred>::identity, \
453 Converter<type, norm, size, preferred>::finalSize, \
454 Converter<type, norm, size, preferred>::convertArray, \
455 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
456 }
457
458#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
459 { \
460 Converter<type, norm, size, UsePreferred>::capflag, \
461 TRANSLATION(type, norm, size, UsePreferred), \
462 TRANSLATION(type, norm, size, UseFallback) \
463 }
464
465#define TRANSLATIONS_FOR_TYPE(type) \
466 { \
467 { 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) }, \
468 { 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) }, \
469 }
470
471const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
472{
473 TRANSLATIONS_FOR_TYPE(GL_BYTE),
474 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
475 TRANSLATIONS_FOR_TYPE(GL_SHORT),
476 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
477 TRANSLATIONS_FOR_TYPE(GL_FIXED),
478 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
479};
480
481void VertexDataManager::checkVertexCaps(DWORD declTypes)
482{
483 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
484 {
485 for (unsigned int j = 0; j < 2; j++)
486 {
487 for (unsigned int k = 0; k < 4; k++)
488 {
489 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
490 {
491 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
492 }
493 else
494 {
495 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
496 }
497 }
498 }
499 }
500}
501
502// This is used to index mAttributeTypes and mPossibleTranslations.
503unsigned int VertexDataManager::typeIndex(GLenum type) const
504{
505 switch (type)
506 {
507 case GL_BYTE: return 0;
508 case GL_UNSIGNED_BYTE: return 1;
509 case GL_SHORT: return 2;
510 case GL_UNSIGNED_SHORT: return 3;
511 case GL_FIXED: return 4;
512 case GL_FLOAT: return 5;
513
514 default: UNREACHABLE(); return 5;
515 }
516}
517
daniel@transgaming.com83921382011-01-08 05:46:00 +0000518VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
519{
520 if (size > 0)
521 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000522 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000523 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000524
525 if (FAILED(result))
526 {
527 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
528 }
529 }
530}
531
532VertexBuffer::~VertexBuffer()
533{
534 if (mVertexBuffer)
535 {
536 mVertexBuffer->Release();
537 }
538}
539
540void VertexBuffer::unmap()
541{
542 if (mVertexBuffer)
543 {
544 mVertexBuffer->Unlock();
545 }
546}
547
548IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
549{
550 return mVertexBuffer;
551}
552
553ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
554{
555 void *buffer = NULL;
556
557 if (mVertexBuffer)
558 {
559 HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
560
561 if (FAILED(result))
562 {
563 ERR("Lock failed with error 0x%08x", result);
564 }
565 }
566
567 if (buffer)
568 {
569 float *vector = (float*)buffer;
570
571 vector[0] = x;
572 vector[1] = y;
573 vector[2] = z;
574 vector[3] = w;
575
576 mVertexBuffer->Unlock();
577 }
578}
579
580ConstantVertexBuffer::~ConstantVertexBuffer()
581{
582}
583
584ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
585{
586 mBufferSize = size;
587 mWritePosition = 0;
588 mRequiredSpace = 0;
589}
590
591ArrayVertexBuffer::~ArrayVertexBuffer()
592{
593}
594
595void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
596{
597 mRequiredSpace += requiredSpace;
598}
599
daniel@transgaming.com83921382011-01-08 05:46:00 +0000600StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
601{
602}
603
604StreamingVertexBuffer::~StreamingVertexBuffer()
605{
606}
607
608void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
609{
610 void *mapPtr = NULL;
611
612 if (mVertexBuffer)
613 {
614 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
615
616 if (FAILED(result))
617 {
618 ERR("Lock failed with error 0x%08x", result);
619 return NULL;
620 }
621
622 *offset = mWritePosition;
623 mWritePosition += requiredSpace;
624 }
625
626 return mapPtr;
627}
628
629void StreamingVertexBuffer::reserveRequiredSpace()
630{
631 if (mRequiredSpace > mBufferSize)
632 {
633 if (mVertexBuffer)
634 {
635 mVertexBuffer->Release();
636 mVertexBuffer = NULL;
637 }
638
639 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 +0000640
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000641 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000642 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000643
644 if (FAILED(result))
645 {
646 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
647 }
648
649 mWritePosition = 0;
650 }
651 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
652 {
653 if (mVertexBuffer)
654 {
655 void *dummy;
656 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
657 mVertexBuffer->Unlock();
658 }
659
660 mWritePosition = 0;
661 }
662
663 mRequiredSpace = 0;
664}
665
666StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
667{
668}
669
670StaticVertexBuffer::~StaticVertexBuffer()
671{
672}
673
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000674void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000675{
676 void *mapPtr = NULL;
677
678 if (mVertexBuffer)
679 {
680 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
681
682 if (FAILED(result))
683 {
684 ERR("Lock failed with error 0x%08x", result);
685 return NULL;
686 }
687
688 int attributeOffset = attribute.mOffset % attribute.stride();
689 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
690 mCache.push_back(element);
691
692 *streamOffset = mWritePosition;
693 mWritePosition += requiredSpace;
694 }
695
696 return mapPtr;
697}
698
699void StaticVertexBuffer::reserveRequiredSpace()
700{
701 if (!mVertexBuffer && mBufferSize == 0)
702 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000703 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000704 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000705
706 if (FAILED(result))
707 {
708 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
709 }
710
711 mBufferSize = mRequiredSpace;
712 }
713 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
714 {
715 // Already allocated
716 }
717 else UNREACHABLE(); // Static vertex buffers can't be resized
718
719 mRequiredSpace = 0;
720}
721
722UINT StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
723{
724 for (unsigned int element = 0; element < mCache.size(); element++)
725 {
726 if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
727 {
728 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
729 {
730 return mCache[element].streamOffset;
731 }
732 }
733 }
734
735 return -1;
736}
737
738const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
739{
740 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
741}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000742}