blob: 8a7d9f05d32657f3696a409af004ca4aaff18b11 [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 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.com78624ca2011-04-22 04:17:57 +0000161 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000162 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 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000176 if (attribs[i].mArrayEnabled)
177 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000178 Buffer *buffer = attribs[i].mBoundBuffer.get();
179
daniel@transgaming.com83921382011-01-08 05:46:00 +0000180 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
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000189 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000190 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
191
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000192 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000193
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
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000245 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
246 {
247 if (translated[i].active && attribs[i].mArrayEnabled)
248 {
249 Buffer *buffer = attribs[i].mBoundBuffer.get();
250
251 if (buffer)
252 {
253 buffer->promoteStaticUsage(count * attribs[i].typeSize());
254 }
255 }
256 }
257
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000258 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000259}
260
daniel@transgaming.com83921382011-01-08 05:46:00 +0000261std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
262{
263 return formatConverter(attrib).outputElementSize * count;
264}
265
266// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
267//
268// BYTE SHORT (Cast)
269// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
270// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
271// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
272// SHORT SHORT (Identity)
273// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
274// UNSIGNED_SHORT FLOAT (Cast)
275// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
276// FIXED (not in WebGL) FLOAT (FixedToFloat)
277// FLOAT FLOAT (Identity)
278
279// GLToCType maps from GL type (as GLenum) to the C typedef.
280template <GLenum GLType> struct GLToCType { };
281
282template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
283template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
284template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
285template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
286template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
287template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
288
289// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
290enum D3DVertexType
291{
292 D3DVT_FLOAT,
293 D3DVT_SHORT,
294 D3DVT_SHORT_NORM,
295 D3DVT_UBYTE,
296 D3DVT_UBYTE_NORM,
297 D3DVT_USHORT_NORM
298};
299
300// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
301template <unsigned int D3DType> struct D3DToCType { };
302
303template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
304template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
305template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
306template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
307template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
308template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
309
310// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
311template <unsigned int type, int size>
312struct WidenRule
313{
314};
315
316template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
317template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
318template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
319template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
320template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
321template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
322
323// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
324template <unsigned int d3dtype, int size>
325struct VertexTypeFlags
326{
327};
328
329template <unsigned int capflag, unsigned int declflag>
330struct VertexTypeFlagsHelper
331{
332 enum { capflag = capflag };
333 enum { declflag = declflag };
334};
335
336template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
337template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
338template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
339template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
340template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
341template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
342template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
343template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
344template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
345template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
346template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
347template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
348
349
350// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
351template <GLenum GLtype, bool normalized>
352struct VertexTypeMapping
353{
354};
355
356template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
357struct VertexTypeMappingBase
358{
359 enum { preferred = Preferred };
360 enum { fallback = Fallback };
361};
362
363template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
364template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
365template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
366template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
367template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
368template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
369template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
370template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
371template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
372template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
373
374
375// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
376// The conversion rules themselves are defined in vertexconversion.h.
377
378// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
379template <GLenum fromType, bool normalized, unsigned int toType>
380struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
381{
382};
383
384// All conversions from normalized types to float use the Normalize operator.
385template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
386
387// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
388template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
389template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
390
391// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
392// whether it is normalized or not.
393template <class T, bool normalized>
394struct DefaultVertexValuesStage2
395{
396};
397
398template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
399template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
400
401// Work out the default value rule for a D3D type (expressed as the C type) and
402template <class T, bool normalized>
403struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
404{
405};
406
407template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
408
409// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
410// The fallback conversion produces an output that all D3D9 devices must support.
411template <class T> struct UsePreferred { enum { type = T::preferred }; };
412template <class T> struct UseFallback { enum { type = T::fallback }; };
413
414// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
415// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
416// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
417template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
418struct Converter
419 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
420 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
421 ConversionRule<fromType,
422 normalized,
423 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
424 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
425{
426private:
427 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
428 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
429
430public:
431 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
432 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
433};
434
435// Initialise a TranslationInfo
436#define TRANSLATION(type, norm, size, preferred) \
437 { \
438 Converter<type, norm, size, preferred>::identity, \
439 Converter<type, norm, size, preferred>::finalSize, \
440 Converter<type, norm, size, preferred>::convertArray, \
441 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
442 }
443
444#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
445 { \
446 Converter<type, norm, size, UsePreferred>::capflag, \
447 TRANSLATION(type, norm, size, UsePreferred), \
448 TRANSLATION(type, norm, size, UseFallback) \
449 }
450
451#define TRANSLATIONS_FOR_TYPE(type) \
452 { \
453 { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \
454 { TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) }, \
455 }
456
457const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
458{
459 TRANSLATIONS_FOR_TYPE(GL_BYTE),
460 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
461 TRANSLATIONS_FOR_TYPE(GL_SHORT),
462 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
463 TRANSLATIONS_FOR_TYPE(GL_FIXED),
464 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
465};
466
467void VertexDataManager::checkVertexCaps(DWORD declTypes)
468{
469 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
470 {
471 for (unsigned int j = 0; j < 2; j++)
472 {
473 for (unsigned int k = 0; k < 4; k++)
474 {
475 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
476 {
477 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
478 }
479 else
480 {
481 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
482 }
483 }
484 }
485 }
486}
487
488// This is used to index mAttributeTypes and mPossibleTranslations.
489unsigned int VertexDataManager::typeIndex(GLenum type) const
490{
491 switch (type)
492 {
493 case GL_BYTE: return 0;
494 case GL_UNSIGNED_BYTE: return 1;
495 case GL_SHORT: return 2;
496 case GL_UNSIGNED_SHORT: return 3;
497 case GL_FIXED: return 4;
498 case GL_FLOAT: return 5;
499
500 default: UNREACHABLE(); return 5;
501 }
502}
503
daniel@transgaming.com83921382011-01-08 05:46:00 +0000504VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
505{
506 if (size > 0)
507 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000508 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000509 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000510
511 if (FAILED(result))
512 {
513 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
514 }
515 }
516}
517
518VertexBuffer::~VertexBuffer()
519{
520 if (mVertexBuffer)
521 {
522 mVertexBuffer->Release();
523 }
524}
525
526void VertexBuffer::unmap()
527{
528 if (mVertexBuffer)
529 {
530 mVertexBuffer->Unlock();
531 }
532}
533
534IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
535{
536 return mVertexBuffer;
537}
538
539ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
540{
541 void *buffer = NULL;
542
543 if (mVertexBuffer)
544 {
545 HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
546
547 if (FAILED(result))
548 {
549 ERR("Lock failed with error 0x%08x", result);
550 }
551 }
552
553 if (buffer)
554 {
555 float *vector = (float*)buffer;
556
557 vector[0] = x;
558 vector[1] = y;
559 vector[2] = z;
560 vector[3] = w;
561
562 mVertexBuffer->Unlock();
563 }
564}
565
566ConstantVertexBuffer::~ConstantVertexBuffer()
567{
568}
569
570ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
571{
572 mBufferSize = size;
573 mWritePosition = 0;
574 mRequiredSpace = 0;
575}
576
577ArrayVertexBuffer::~ArrayVertexBuffer()
578{
579}
580
581void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
582{
583 mRequiredSpace += requiredSpace;
584}
585
586void ArrayVertexBuffer::addRequiredSpaceFor(ArrayVertexBuffer *buffer)
587{
588 mRequiredSpace += buffer->mRequiredSpace;
589}
590
591StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
592{
593}
594
595StreamingVertexBuffer::~StreamingVertexBuffer()
596{
597}
598
599void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
600{
601 void *mapPtr = NULL;
602
603 if (mVertexBuffer)
604 {
605 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
606
607 if (FAILED(result))
608 {
609 ERR("Lock failed with error 0x%08x", result);
610 return NULL;
611 }
612
613 *offset = mWritePosition;
614 mWritePosition += requiredSpace;
615 }
616
617 return mapPtr;
618}
619
620void StreamingVertexBuffer::reserveRequiredSpace()
621{
622 if (mRequiredSpace > mBufferSize)
623 {
624 if (mVertexBuffer)
625 {
626 mVertexBuffer->Release();
627 mVertexBuffer = NULL;
628 }
629
630 mBufferSize = std::max(mRequiredSpace, 3 * mBufferSize / 2); // 1.5 x mBufferSize is arbitrary and should be checked to see we don't have too many reallocations.
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000631
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000632 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000633 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000634
635 if (FAILED(result))
636 {
637 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
638 }
639
640 mWritePosition = 0;
641 }
642 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
643 {
644 if (mVertexBuffer)
645 {
646 void *dummy;
647 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
648 mVertexBuffer->Unlock();
649 }
650
651 mWritePosition = 0;
652 }
653
654 mRequiredSpace = 0;
655}
656
657StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
658{
659}
660
661StaticVertexBuffer::~StaticVertexBuffer()
662{
663}
664
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000665void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000666{
667 void *mapPtr = NULL;
668
669 if (mVertexBuffer)
670 {
671 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
672
673 if (FAILED(result))
674 {
675 ERR("Lock failed with error 0x%08x", result);
676 return NULL;
677 }
678
679 int attributeOffset = attribute.mOffset % attribute.stride();
680 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
681 mCache.push_back(element);
682
683 *streamOffset = mWritePosition;
684 mWritePosition += requiredSpace;
685 }
686
687 return mapPtr;
688}
689
690void StaticVertexBuffer::reserveRequiredSpace()
691{
692 if (!mVertexBuffer && mBufferSize == 0)
693 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000694 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000695 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000696
697 if (FAILED(result))
698 {
699 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
700 }
701
702 mBufferSize = mRequiredSpace;
703 }
704 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
705 {
706 // Already allocated
707 }
708 else UNREACHABLE(); // Static vertex buffers can't be resized
709
710 mRequiredSpace = 0;
711}
712
713UINT StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
714{
715 for (unsigned int element = 0; element < mCache.size(); element++)
716 {
717 if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
718 {
719 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
720 {
721 return mCache[element].streamOffset;
722 }
723 }
724 }
725
726 return -1;
727}
728
729const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
730{
731 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
732}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000733}