blob: 80aa51c1a32b35aa00312e5b6dff52d0400ffeec [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00003// 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 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000024 // This has to be at least 4k or else it fails on ATI cards.
25 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000026}
27
28namespace gl
29{
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000030unsigned int VertexBuffer::mCurrentSerial = 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000031
jbauman@chromium.org059fc152011-11-18 19:26:17 +000032int elementsInBuffer(const VertexAttribute &attribute, int size)
33{
34 int stride = attribute.stride();
35 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
36}
37
daniel@transgaming.combaa74512011-04-13 14:56:47 +000038VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039{
daniel@transgaming.com83921382011-01-08 05:46:00 +000040 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000041 {
daniel@transgaming.com83921382011-01-08 05:46:00 +000042 mDirtyCurrentValue[i] = true;
43 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000044 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000045 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000046
47 const D3DCAPS9 &caps = context->getDeviceCaps();
48 checkVertexCaps(caps.DeclTypes);
49
50 mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000051
52 if (!mStreamingBuffer)
53 {
54 ERR("Failed to allocate the streaming vertex buffer.");
55 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000056}
57
58VertexDataManager::~VertexDataManager()
59{
daniel@transgaming.com83921382011-01-08 05:46:00 +000060 delete mStreamingBuffer;
61
62 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
63 {
64 delete mCurrentValueBuffer[i];
65 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000066}
67
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000068std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000069{
daniel@transgaming.com83921382011-01-08 05:46:00 +000070 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000071
daniel@transgaming.com83921382011-01-08 05:46:00 +000072 int inputStride = attribute.stride();
73 int elementSize = attribute.typeSize();
74 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000075 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000076
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000077 void *output = NULL;
78
79 if (vertexBuffer)
80 {
81 output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
82 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000083
84 if (output == NULL)
85 {
86 ERR("Failed to map vertex buffer.");
87 return -1;
88 }
89
90 const char *input = NULL;
91
92 if (buffer)
93 {
94 int offset = attribute.mOffset;
95
96 input = static_cast<const char*>(buffer->data()) + offset;
97 }
98 else
99 {
100 input = static_cast<const char*>(attribute.mPointer);
101 }
102
103 input += inputStride * start;
104
105 if (converter.identity && inputStride == elementSize)
106 {
107 memcpy(output, input, count * inputStride);
108 }
109 else
110 {
111 converter.convertArray(input, inputStride, count, output);
112 }
113
114 vertexBuffer->unmap();
115
116 return streamOffset;
117}
118
119GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
120{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000121 if (!mStreamingBuffer)
122 {
123 return GL_OUT_OF_MEMORY;
124 }
125
daniel@transgaming.com83921382011-01-08 05:46:00 +0000126 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000127 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000128
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000129 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000130 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000131 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000132 }
133
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000134 // 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 +0000135 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
136 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000137 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000138 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000139 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000140 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000141
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000142 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000143 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000144 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000145 {
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000146 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000147 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000148 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000149 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000150 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000151 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000152 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
153 for (int previous = 0; previous < i; previous++)
154 {
155 if (translated[previous].active && attribs[previous].mArrayEnabled)
156 {
157 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
158 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
159
160 if (staticBuffer == previousStaticBuffer)
161 {
162 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count));
163 }
164 }
165 }
166
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000167 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000168
169 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000170 }
171 }
172 else
173 {
174 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000175 }
176 }
177 }
178
179 // Reserve the required space per used buffer
180 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
181 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000182 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000183 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000184 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000185 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000186 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
187
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000188 if (vertexBuffer)
189 {
190 vertexBuffer->reserveRequiredSpace();
191 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000192 }
193 }
194
195 // Perform the vertex data translations
196 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
197 {
198 if (translated[i].active)
199 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000200 if (attribs[i].mArrayEnabled)
201 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000202 Buffer *buffer = attribs[i].mBoundBuffer.get();
203
daniel@transgaming.com83921382011-01-08 05:46:00 +0000204 if (!buffer && attribs[i].mPointer == NULL)
205 {
206 // This is an application error that would normally result in a crash, but we catch it and return an error
207 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000208 return GL_INVALID_OPERATION;
209 }
210
daniel@transgaming.com83921382011-01-08 05:46:00 +0000211 const FormatConverter &converter = formatConverter(attribs[i]);
212
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000213 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000214 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
215
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000216 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000217
218 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000219 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000220 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
221
222 if (streamOffset == -1)
223 {
224 // Convert the entire buffer
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000225 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000226 int startIndex = attribs[i].mOffset / attribs[i].stride();
227
228 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
229 }
230
231 if (streamOffset != -1)
232 {
233 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
234 }
235 }
236 else
237 {
238 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000239 }
240
daniel@transgaming.com83921382011-01-08 05:46:00 +0000241 if (streamOffset == -1)
242 {
243 return GL_OUT_OF_MEMORY;
244 }
245
246 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000247 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000248 translated[i].divisor = attribs[i].mDivisor;
249
daniel@transgaming.com83921382011-01-08 05:46:00 +0000250 translated[i].type = converter.d3dDeclType;
251 translated[i].stride = converter.outputElementSize;
252 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000253 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000254 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000255 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000256 if (!mCurrentValueBuffer[i])
257 {
258 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mDevice, CONSTANT_VERTEX_BUFFER_SIZE);
259 }
260
261 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
262
daniel@transgaming.com83921382011-01-08 05:46:00 +0000263 if (mDirtyCurrentValue[i])
264 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000265 const int requiredSpace = 4 * sizeof(float);
266 buffer->addRequiredSpace(requiredSpace);
267 buffer->reserveRequiredSpace();
268 float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
269 if (data)
270 {
271 data[0] = attribs[i].mCurrentValue[0];
272 data[1] = attribs[i].mCurrentValue[1];
273 data[2] = attribs[i].mCurrentValue[2];
274 data[3] = attribs[i].mCurrentValue[3];
275 buffer->unmap();
276 mDirtyCurrentValue[i] = false;
277 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000278 }
279
280 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000281 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000282 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000283
284 translated[i].type = D3DDECLTYPE_FLOAT4;
285 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000286 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000287 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000288 }
289 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000290
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000291 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
292 {
293 if (translated[i].active && attribs[i].mArrayEnabled)
294 {
295 Buffer *buffer = attribs[i].mBoundBuffer.get();
296
297 if (buffer)
298 {
299 buffer->promoteStaticUsage(count * attribs[i].typeSize());
300 }
301 }
302 }
303
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000304 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000305}
306
daniel@transgaming.com83921382011-01-08 05:46:00 +0000307std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
308{
309 return formatConverter(attrib).outputElementSize * count;
310}
311
312// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
313//
314// BYTE SHORT (Cast)
315// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
316// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
317// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
318// SHORT SHORT (Identity)
319// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
320// UNSIGNED_SHORT FLOAT (Cast)
321// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
322// FIXED (not in WebGL) FLOAT (FixedToFloat)
323// FLOAT FLOAT (Identity)
324
325// GLToCType maps from GL type (as GLenum) to the C typedef.
326template <GLenum GLType> struct GLToCType { };
327
328template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
329template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
330template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
331template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
332template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
333template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
334
335// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
336enum D3DVertexType
337{
338 D3DVT_FLOAT,
339 D3DVT_SHORT,
340 D3DVT_SHORT_NORM,
341 D3DVT_UBYTE,
342 D3DVT_UBYTE_NORM,
343 D3DVT_USHORT_NORM
344};
345
346// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
347template <unsigned int D3DType> struct D3DToCType { };
348
349template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
350template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
351template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
352template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
353template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
354template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
355
356// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
357template <unsigned int type, int size>
358struct WidenRule
359{
360};
361
362template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
363template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
364template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
365template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
366template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
367template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
368
369// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
370template <unsigned int d3dtype, int size>
371struct VertexTypeFlags
372{
373};
374
375template <unsigned int capflag, unsigned int declflag>
376struct VertexTypeFlagsHelper
377{
378 enum { capflag = capflag };
379 enum { declflag = declflag };
380};
381
382template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
383template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
384template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
385template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
386template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
387template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
388template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
389template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
390template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
391template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
392template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
393template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
394
395
396// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
397template <GLenum GLtype, bool normalized>
398struct VertexTypeMapping
399{
400};
401
402template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
403struct VertexTypeMappingBase
404{
405 enum { preferred = Preferred };
406 enum { fallback = Fallback };
407};
408
409template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
410template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
411template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
412template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
413template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
414template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
415template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
416template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
417template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
418template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
419
420
421// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
422// The conversion rules themselves are defined in vertexconversion.h.
423
424// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
425template <GLenum fromType, bool normalized, unsigned int toType>
426struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
427{
428};
429
430// All conversions from normalized types to float use the Normalize operator.
431template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
432
433// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
434template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
435template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
436
437// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
438// whether it is normalized or not.
439template <class T, bool normalized>
440struct DefaultVertexValuesStage2
441{
442};
443
444template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
445template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
446
447// Work out the default value rule for a D3D type (expressed as the C type) and
448template <class T, bool normalized>
449struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
450{
451};
452
453template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
454
455// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
456// The fallback conversion produces an output that all D3D9 devices must support.
457template <class T> struct UsePreferred { enum { type = T::preferred }; };
458template <class T> struct UseFallback { enum { type = T::fallback }; };
459
460// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
461// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
462// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
463template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
464struct Converter
465 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
466 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
467 ConversionRule<fromType,
468 normalized,
469 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
470 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
471{
472private:
473 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
474 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
475
476public:
477 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
478 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
479};
480
481// Initialise a TranslationInfo
482#define TRANSLATION(type, norm, size, preferred) \
483 { \
484 Converter<type, norm, size, preferred>::identity, \
485 Converter<type, norm, size, preferred>::finalSize, \
486 Converter<type, norm, size, preferred>::convertArray, \
487 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
488 }
489
490#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
491 { \
492 Converter<type, norm, size, UsePreferred>::capflag, \
493 TRANSLATION(type, norm, size, UsePreferred), \
494 TRANSLATION(type, norm, size, UseFallback) \
495 }
496
497#define TRANSLATIONS_FOR_TYPE(type) \
498 { \
499 { 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) }, \
500 { 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) }, \
501 }
502
503const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
504{
505 TRANSLATIONS_FOR_TYPE(GL_BYTE),
506 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
507 TRANSLATIONS_FOR_TYPE(GL_SHORT),
508 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
509 TRANSLATIONS_FOR_TYPE(GL_FIXED),
510 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
511};
512
513void VertexDataManager::checkVertexCaps(DWORD declTypes)
514{
515 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
516 {
517 for (unsigned int j = 0; j < 2; j++)
518 {
519 for (unsigned int k = 0; k < 4; k++)
520 {
521 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
522 {
523 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
524 }
525 else
526 {
527 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
528 }
529 }
530 }
531 }
532}
533
534// This is used to index mAttributeTypes and mPossibleTranslations.
535unsigned int VertexDataManager::typeIndex(GLenum type) const
536{
537 switch (type)
538 {
539 case GL_BYTE: return 0;
540 case GL_UNSIGNED_BYTE: return 1;
541 case GL_SHORT: return 2;
542 case GL_UNSIGNED_SHORT: return 3;
543 case GL_FIXED: return 4;
544 case GL_FLOAT: return 5;
545
546 default: UNREACHABLE(); return 5;
547 }
548}
549
daniel@transgaming.com83921382011-01-08 05:46:00 +0000550VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
551{
552 if (size > 0)
553 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000554 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000555 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000556 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000557
558 if (FAILED(result))
559 {
560 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
561 }
562 }
563}
564
565VertexBuffer::~VertexBuffer()
566{
567 if (mVertexBuffer)
568 {
569 mVertexBuffer->Release();
570 }
571}
572
573void VertexBuffer::unmap()
574{
575 if (mVertexBuffer)
576 {
577 mVertexBuffer->Unlock();
578 }
579}
580
581IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
582{
583 return mVertexBuffer;
584}
585
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000586unsigned int VertexBuffer::getSerial() const
587{
588 return mSerial;
589}
590
591unsigned int VertexBuffer::issueSerial()
592{
593 return mCurrentSerial++;
594}
595
daniel@transgaming.com83921382011-01-08 05:46:00 +0000596ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
597{
598 mBufferSize = size;
599 mWritePosition = 0;
600 mRequiredSpace = 0;
601}
602
603ArrayVertexBuffer::~ArrayVertexBuffer()
604{
605}
606
607void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
608{
609 mRequiredSpace += requiredSpace;
610}
611
daniel@transgaming.com83921382011-01-08 05:46:00 +0000612StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
613{
614}
615
616StreamingVertexBuffer::~StreamingVertexBuffer()
617{
618}
619
620void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
621{
622 void *mapPtr = NULL;
623
624 if (mVertexBuffer)
625 {
626 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
627
628 if (FAILED(result))
629 {
630 ERR("Lock failed with error 0x%08x", result);
631 return NULL;
632 }
633
634 *offset = mWritePosition;
635 mWritePosition += requiredSpace;
636 }
637
638 return mapPtr;
639}
640
641void StreamingVertexBuffer::reserveRequiredSpace()
642{
643 if (mRequiredSpace > mBufferSize)
644 {
645 if (mVertexBuffer)
646 {
647 mVertexBuffer->Release();
648 mVertexBuffer = NULL;
649 }
650
651 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 +0000652
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000653 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000654 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000655 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000656
657 if (FAILED(result))
658 {
659 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
660 }
661
662 mWritePosition = 0;
663 }
664 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
665 {
666 if (mVertexBuffer)
667 {
668 void *dummy;
669 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
670 mVertexBuffer->Unlock();
671 }
672
673 mWritePosition = 0;
674 }
675
676 mRequiredSpace = 0;
677}
678
679StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
680{
681}
682
683StaticVertexBuffer::~StaticVertexBuffer()
684{
685}
686
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000687void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000688{
689 void *mapPtr = NULL;
690
691 if (mVertexBuffer)
692 {
693 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
694
695 if (FAILED(result))
696 {
697 ERR("Lock failed with error 0x%08x", result);
698 return NULL;
699 }
700
701 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000702 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000703 mCache.push_back(element);
704
705 *streamOffset = mWritePosition;
706 mWritePosition += requiredSpace;
707 }
708
709 return mapPtr;
710}
711
712void StaticVertexBuffer::reserveRequiredSpace()
713{
714 if (!mVertexBuffer && mBufferSize == 0)
715 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000716 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000717 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000718 mSerial = issueSerial();
719
daniel@transgaming.com83921382011-01-08 05:46:00 +0000720 if (FAILED(result))
721 {
722 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
723 }
724
725 mBufferSize = mRequiredSpace;
726 }
727 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
728 {
729 // Already allocated
730 }
731 else UNREACHABLE(); // Static vertex buffers can't be resized
732
733 mRequiredSpace = 0;
734}
735
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000736std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000737{
738 for (unsigned int element = 0; element < mCache.size(); element++)
739 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000740 if (mCache[element].type == attribute.mType &&
741 mCache[element].size == attribute.mSize &&
742 mCache[element].stride == attribute.stride() &&
743 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000744 {
745 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
746 {
747 return mCache[element].streamOffset;
748 }
749 }
750 }
751
752 return -1;
753}
754
755const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
756{
757 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
758}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000759}