blob: 463a2f7b0d0a0b6aefe91b5a5863a79f781038bd [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
jbauman@chromium.org2cec2f32011-04-06 18:59:51 +000029VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device), mMaxLru(0)
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 }
jbauman@chromium.org2cec2f32011-04-06 18:59:51 +000036 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
37 {
38 mVertexDeclCache[i].vertexDeclaration = NULL;
39 mVertexDeclCache[i].lruCount = 0;
40 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000041
42 const D3DCAPS9 &caps = context->getDeviceCaps();
43 checkVertexCaps(caps.DeclTypes);
44
45 mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
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 }
jbauman@chromium.org2cec2f32011-04-06 18:59:51 +000056 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
57 {
58 if (mVertexDeclCache[i].vertexDeclaration)
59 {
60 mVertexDeclCache[i].vertexDeclaration->Release();
61 }
62 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000063}
64
daniel@transgaming.com83921382011-01-08 05:46:00 +000065UINT VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000066{
daniel@transgaming.com83921382011-01-08 05:46:00 +000067 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000068
daniel@transgaming.com83921382011-01-08 05:46:00 +000069 int inputStride = attribute.stride();
70 int elementSize = attribute.typeSize();
71 const FormatConverter &converter = formatConverter(attribute);
72 UINT streamOffset = 0;
73
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000074 void *output = NULL;
75
76 if (vertexBuffer)
77 {
78 output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
79 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000080
81 if (output == NULL)
82 {
83 ERR("Failed to map vertex buffer.");
84 return -1;
85 }
86
87 const char *input = NULL;
88
89 if (buffer)
90 {
91 int offset = attribute.mOffset;
92
93 input = static_cast<const char*>(buffer->data()) + offset;
94 }
95 else
96 {
97 input = static_cast<const char*>(attribute.mPointer);
98 }
99
100 input += inputStride * start;
101
102 if (converter.identity && inputStride == elementSize)
103 {
104 memcpy(output, input, count * inputStride);
105 }
106 else
107 {
108 converter.convertArray(input, inputStride, count, output);
109 }
110
111 vertexBuffer->unmap();
112
113 return streamOffset;
114}
115
116GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
117{
118 GLenum error = GL_NO_ERROR;
119 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000120 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000121
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000122 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000123 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000125 }
126
daniel@transgaming.com83921382011-01-08 05:46:00 +0000127 // Determine the required storage size per used buffer
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000128 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
129 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000130 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000131
daniel@transgaming.com83921382011-01-08 05:46:00 +0000132 if (translated[i].active && attribs[i].mArrayEnabled && (buffer || attribs[i].mPointer))
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000133 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000134 StaticVertexBuffer *staticBuffer = buffer ? buffer->getVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000135
daniel@transgaming.com83921382011-01-08 05:46:00 +0000136 if (staticBuffer && staticBuffer->size() == 0)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000137 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000138 int totalCount = buffer->size() / attribs[i].stride();
139 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000140 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000141 else if (!staticBuffer || staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 {
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000143 if (mStreamingBuffer)
144 {
145 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
146 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000147 }
148 }
149 }
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000150
daniel@transgaming.com83921382011-01-08 05:46:00 +0000151 // Invalidate static buffers if the attribute formats no longer match
152 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
153 {
154 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000155
daniel@transgaming.com83921382011-01-08 05:46:00 +0000156 if (translated[i].active && attribs[i].mArrayEnabled && buffer)
157 {
158 StaticVertexBuffer *staticBuffer = buffer->getVertexBuffer();
159
160 if (staticBuffer && staticBuffer->size() != 0)
161 {
162 bool matchingAttributes = true;
163
164 for (int j = 0; j < MAX_VERTEX_ATTRIBS; j++)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000165 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000166 if (translated[j].active && attribs[j].mArrayEnabled && attribs[j].mBoundBuffer.get() == buffer)
167 {
168 if (staticBuffer->lookupAttribute(attribs[j]) == -1)
169 {
170 matchingAttributes = false;
171 break;
172 }
173 }
174 }
175
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000176 if (!matchingAttributes && mStreamingBuffer)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000177 {
178 mStreamingBuffer->addRequiredSpaceFor(staticBuffer);
179 buffer->invalidateStaticData();
180 }
181 }
182 }
183 }
184
185 // Reserve the required space per used buffer
186 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
187 {
188 Buffer *buffer = attribs[i].mBoundBuffer.get();
189
190 if (translated[i].active && attribs[i].mArrayEnabled && (buffer || attribs[i].mPointer))
191 {
192 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getVertexBuffer() : NULL;
193 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
194
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000195 if (vertexBuffer)
196 {
197 vertexBuffer->reserveRequiredSpace();
198 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000199 }
200 }
201
202 // Perform the vertex data translations
203 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
204 {
205 if (translated[i].active)
206 {
207 Buffer *buffer = attribs[i].mBoundBuffer.get();
208
209 if (attribs[i].mArrayEnabled)
210 {
211 if (!buffer && attribs[i].mPointer == NULL)
212 {
213 // This is an application error that would normally result in a crash, but we catch it and return an error
214 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000215 return GL_INVALID_OPERATION;
216 }
217
daniel@transgaming.com83921382011-01-08 05:46:00 +0000218 const FormatConverter &converter = formatConverter(attribs[i]);
219
220 StaticVertexBuffer *staticBuffer = buffer ? buffer->getVertexBuffer() : NULL;
221 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
222
223 UINT streamOffset = -1;
224
225 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000226 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000227 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
228
229 if (streamOffset == -1)
230 {
231 // Convert the entire buffer
232 int totalCount = buffer->size() / attribs[i].stride();
233 int startIndex = attribs[i].mOffset / attribs[i].stride();
234
235 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
236 }
237
238 if (streamOffset != -1)
239 {
240 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
241 }
242 }
243 else
244 {
245 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000246 }
247
daniel@transgaming.com83921382011-01-08 05:46:00 +0000248 if (streamOffset == -1)
249 {
250 return GL_OUT_OF_MEMORY;
251 }
252
253 translated[i].vertexBuffer = vertexBuffer->getBuffer();
254 translated[i].type = converter.d3dDeclType;
255 translated[i].stride = converter.outputElementSize;
256 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000257 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000258 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000259 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000260 if (mDirtyCurrentValue[i])
261 {
262 delete mCurrentValueBuffer[i];
263 mCurrentValueBuffer[i] = new ConstantVertexBuffer(mDevice, attribs[i].mCurrentValue[0], attribs[i].mCurrentValue[1], attribs[i].mCurrentValue[2], attribs[i].mCurrentValue[3]);
264 mDirtyCurrentValue[i] = false;
265 }
266
267 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
268
269 translated[i].type = D3DDECLTYPE_FLOAT4;
270 translated[i].stride = 0;
271 translated[i].offset = 0;
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000272 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000273 }
274 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000275
276 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000277}
278
daniel@transgaming.com83921382011-01-08 05:46:00 +0000279std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
280{
281 return formatConverter(attrib).outputElementSize * count;
282}
283
284// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
285//
286// BYTE SHORT (Cast)
287// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
288// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
289// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
290// SHORT SHORT (Identity)
291// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
292// UNSIGNED_SHORT FLOAT (Cast)
293// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
294// FIXED (not in WebGL) FLOAT (FixedToFloat)
295// FLOAT FLOAT (Identity)
296
297// GLToCType maps from GL type (as GLenum) to the C typedef.
298template <GLenum GLType> struct GLToCType { };
299
300template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
301template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
302template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
303template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
304template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
305template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
306
307// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
308enum D3DVertexType
309{
310 D3DVT_FLOAT,
311 D3DVT_SHORT,
312 D3DVT_SHORT_NORM,
313 D3DVT_UBYTE,
314 D3DVT_UBYTE_NORM,
315 D3DVT_USHORT_NORM
316};
317
318// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
319template <unsigned int D3DType> struct D3DToCType { };
320
321template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
322template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
323template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
324template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
325template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
326template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
327
328// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
329template <unsigned int type, int size>
330struct WidenRule
331{
332};
333
334template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
335template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
336template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
337template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
338template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
339template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
340
341// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
342template <unsigned int d3dtype, int size>
343struct VertexTypeFlags
344{
345};
346
347template <unsigned int capflag, unsigned int declflag>
348struct VertexTypeFlagsHelper
349{
350 enum { capflag = capflag };
351 enum { declflag = declflag };
352};
353
354template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
355template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
356template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
357template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
358template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
359template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
360template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
361template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
362template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
363template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
364template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
365template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
366
367
368// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
369template <GLenum GLtype, bool normalized>
370struct VertexTypeMapping
371{
372};
373
374template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
375struct VertexTypeMappingBase
376{
377 enum { preferred = Preferred };
378 enum { fallback = Fallback };
379};
380
381template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
382template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
383template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
384template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
385template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
386template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
387template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
388template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
389template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
390template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
391
392
393// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
394// The conversion rules themselves are defined in vertexconversion.h.
395
396// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
397template <GLenum fromType, bool normalized, unsigned int toType>
398struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
399{
400};
401
402// All conversions from normalized types to float use the Normalize operator.
403template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
404
405// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
406template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
407template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
408
409// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
410// whether it is normalized or not.
411template <class T, bool normalized>
412struct DefaultVertexValuesStage2
413{
414};
415
416template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
417template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
418
419// Work out the default value rule for a D3D type (expressed as the C type) and
420template <class T, bool normalized>
421struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
422{
423};
424
425template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
426
427// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
428// The fallback conversion produces an output that all D3D9 devices must support.
429template <class T> struct UsePreferred { enum { type = T::preferred }; };
430template <class T> struct UseFallback { enum { type = T::fallback }; };
431
432// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
433// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
434// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
435template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
436struct Converter
437 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
438 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
439 ConversionRule<fromType,
440 normalized,
441 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
442 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
443{
444private:
445 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
446 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
447
448public:
449 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
450 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
451};
452
453// Initialise a TranslationInfo
454#define TRANSLATION(type, norm, size, preferred) \
455 { \
456 Converter<type, norm, size, preferred>::identity, \
457 Converter<type, norm, size, preferred>::finalSize, \
458 Converter<type, norm, size, preferred>::convertArray, \
459 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
460 }
461
462#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
463 { \
464 Converter<type, norm, size, UsePreferred>::capflag, \
465 TRANSLATION(type, norm, size, UsePreferred), \
466 TRANSLATION(type, norm, size, UseFallback) \
467 }
468
469#define TRANSLATIONS_FOR_TYPE(type) \
470 { \
471 { 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) }, \
472 { 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) }, \
473 }
474
475const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
476{
477 TRANSLATIONS_FOR_TYPE(GL_BYTE),
478 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
479 TRANSLATIONS_FOR_TYPE(GL_SHORT),
480 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
481 TRANSLATIONS_FOR_TYPE(GL_FIXED),
482 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
483};
484
485void VertexDataManager::checkVertexCaps(DWORD declTypes)
486{
487 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
488 {
489 for (unsigned int j = 0; j < 2; j++)
490 {
491 for (unsigned int k = 0; k < 4; k++)
492 {
493 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
494 {
495 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
496 }
497 else
498 {
499 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
500 }
501 }
502 }
503 }
504}
505
506// This is used to index mAttributeTypes and mPossibleTranslations.
507unsigned int VertexDataManager::typeIndex(GLenum type) const
508{
509 switch (type)
510 {
511 case GL_BYTE: return 0;
512 case GL_UNSIGNED_BYTE: return 1;
513 case GL_SHORT: return 2;
514 case GL_UNSIGNED_SHORT: return 3;
515 case GL_FIXED: return 4;
516 case GL_FLOAT: return 5;
517
518 default: UNREACHABLE(); return 5;
519 }
520}
521
522void VertexDataManager::setupAttributes(const TranslatedAttribute *attributes)
523{
jbauman@chromium.org2cec2f32011-04-06 18:59:51 +0000524 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
daniel@transgaming.com83921382011-01-08 05:46:00 +0000525 D3DVERTEXELEMENT9 *element = &elements[0];
526
527 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
528 {
529 if (attributes[i].active)
530 {
531 mDevice->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
532
533 element->Stream = i;
534 element->Offset = 0;
535 element->Type = attributes[i].type;
536 element->Method = D3DDECLMETHOD_DEFAULT;
537 element->Usage = D3DDECLUSAGE_TEXCOORD;
538 element->UsageIndex = attributes[i].semanticIndex;
539 element++;
540 }
541 }
542
543 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
jbauman@chromium.org2cec2f32011-04-06 18:59:51 +0000544 *(element++) = end;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000545
jbauman@chromium.org2cec2f32011-04-06 18:59:51 +0000546 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
547 {
548 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
549 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
550 {
551 entry->lruCount = ++mMaxLru;
552 mDevice->SetVertexDeclaration(entry->vertexDeclaration);
553 return;
554 }
555 }
556
557 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
558
559 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
560 {
561 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
562 {
563 lastCache = &mVertexDeclCache[i];
564 }
565 }
566
567 if (lastCache->vertexDeclaration != NULL)
568 {
569 lastCache->vertexDeclaration->Release();
570 lastCache->vertexDeclaration = NULL;
571 }
572
573 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
574 mDevice->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
575 mDevice->SetVertexDeclaration(lastCache->vertexDeclaration);
576 lastCache->lruCount = ++mMaxLru;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000577}
578
579VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
580{
581 if (size > 0)
582 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000583 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000584 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000585
586 if (FAILED(result))
587 {
588 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
589 }
590 }
591}
592
593VertexBuffer::~VertexBuffer()
594{
595 if (mVertexBuffer)
596 {
597 mVertexBuffer->Release();
598 }
599}
600
601void VertexBuffer::unmap()
602{
603 if (mVertexBuffer)
604 {
605 mVertexBuffer->Unlock();
606 }
607}
608
609IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
610{
611 return mVertexBuffer;
612}
613
614ConstantVertexBuffer::ConstantVertexBuffer(IDirect3DDevice9 *device, float x, float y, float z, float w) : VertexBuffer(device, 4 * sizeof(float), D3DUSAGE_WRITEONLY)
615{
616 void *buffer = NULL;
617
618 if (mVertexBuffer)
619 {
620 HRESULT result = mVertexBuffer->Lock(0, 0, &buffer, 0);
621
622 if (FAILED(result))
623 {
624 ERR("Lock failed with error 0x%08x", result);
625 }
626 }
627
628 if (buffer)
629 {
630 float *vector = (float*)buffer;
631
632 vector[0] = x;
633 vector[1] = y;
634 vector[2] = z;
635 vector[3] = w;
636
637 mVertexBuffer->Unlock();
638 }
639}
640
641ConstantVertexBuffer::~ConstantVertexBuffer()
642{
643}
644
645ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
646{
647 mBufferSize = size;
648 mWritePosition = 0;
649 mRequiredSpace = 0;
650}
651
652ArrayVertexBuffer::~ArrayVertexBuffer()
653{
654}
655
656void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
657{
658 mRequiredSpace += requiredSpace;
659}
660
661void ArrayVertexBuffer::addRequiredSpaceFor(ArrayVertexBuffer *buffer)
662{
663 mRequiredSpace += buffer->mRequiredSpace;
664}
665
666StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
667{
668}
669
670StreamingVertexBuffer::~StreamingVertexBuffer()
671{
672}
673
674void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
675{
676 void *mapPtr = NULL;
677
678 if (mVertexBuffer)
679 {
680 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
681
682 if (FAILED(result))
683 {
684 ERR("Lock failed with error 0x%08x", result);
685 return NULL;
686 }
687
688 *offset = mWritePosition;
689 mWritePosition += requiredSpace;
690 }
691
692 return mapPtr;
693}
694
695void StreamingVertexBuffer::reserveRequiredSpace()
696{
697 if (mRequiredSpace > mBufferSize)
698 {
699 if (mVertexBuffer)
700 {
701 mVertexBuffer->Release();
702 mVertexBuffer = NULL;
703 }
704
705 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 +0000706
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000707 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000708 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000709
710 if (FAILED(result))
711 {
712 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
713 }
714
715 mWritePosition = 0;
716 }
717 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
718 {
719 if (mVertexBuffer)
720 {
721 void *dummy;
722 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
723 mVertexBuffer->Unlock();
724 }
725
726 mWritePosition = 0;
727 }
728
729 mRequiredSpace = 0;
730}
731
732StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
733{
734}
735
736StaticVertexBuffer::~StaticVertexBuffer()
737{
738}
739
740void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, UINT *streamOffset)
741{
742 void *mapPtr = NULL;
743
744 if (mVertexBuffer)
745 {
746 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
747
748 if (FAILED(result))
749 {
750 ERR("Lock failed with error 0x%08x", result);
751 return NULL;
752 }
753
754 int attributeOffset = attribute.mOffset % attribute.stride();
755 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
756 mCache.push_back(element);
757
758 *streamOffset = mWritePosition;
759 mWritePosition += requiredSpace;
760 }
761
762 return mapPtr;
763}
764
765void StaticVertexBuffer::reserveRequiredSpace()
766{
767 if (!mVertexBuffer && mBufferSize == 0)
768 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000769 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000770 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000771
772 if (FAILED(result))
773 {
774 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
775 }
776
777 mBufferSize = mRequiredSpace;
778 }
779 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
780 {
781 // Already allocated
782 }
783 else UNREACHABLE(); // Static vertex buffers can't be resized
784
785 mRequiredSpace = 0;
786}
787
788UINT StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
789{
790 for (unsigned int element = 0; element < mCache.size(); element++)
791 {
792 if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
793 {
794 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
795 {
796 return mCache[element].streamOffset;
797 }
798 }
799 }
800
801 return -1;
802}
803
804const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
805{
806 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
807}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000808}