blob: 59b6b444ed69954db5c9f16613dafdb4980c8ce7 [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.com2fc9f902012-01-27 15:39:00 +000068std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute, GLsizei instances)
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 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000081 output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset);
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000082 }
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
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000119GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000120{
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.com2fc9f902012-01-27 15:39:00 +0000147 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0));
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 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000162 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000163 }
164 }
165 }
166
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000167 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
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 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000174 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
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
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000228 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000229 }
230
231 if (streamOffset != -1)
232 {
233 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
234 }
235 }
236 else
237 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000238 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances);
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.com2fc9f902012-01-27 15:39:00 +0000307std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000308{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000309 size_t elementSize = formatConverter(attrib).outputElementSize;
310
311 if (instances == 0 || attrib.mDivisor == 0)
312 {
313 return elementSize * count;
314 }
315 else
316 {
317 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
318 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000319}
320
321// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
322//
323// BYTE SHORT (Cast)
324// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
325// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
326// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
327// SHORT SHORT (Identity)
328// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
329// UNSIGNED_SHORT FLOAT (Cast)
330// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
331// FIXED (not in WebGL) FLOAT (FixedToFloat)
332// FLOAT FLOAT (Identity)
333
334// GLToCType maps from GL type (as GLenum) to the C typedef.
335template <GLenum GLType> struct GLToCType { };
336
337template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
338template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
339template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
340template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
341template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
342template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
343
344// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
345enum D3DVertexType
346{
347 D3DVT_FLOAT,
348 D3DVT_SHORT,
349 D3DVT_SHORT_NORM,
350 D3DVT_UBYTE,
351 D3DVT_UBYTE_NORM,
352 D3DVT_USHORT_NORM
353};
354
355// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
356template <unsigned int D3DType> struct D3DToCType { };
357
358template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
359template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
360template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
361template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
362template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
363template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
364
365// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
366template <unsigned int type, int size>
367struct WidenRule
368{
369};
370
371template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
372template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
373template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
374template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
375template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
376template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
377
378// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
379template <unsigned int d3dtype, int size>
380struct VertexTypeFlags
381{
382};
383
384template <unsigned int capflag, unsigned int declflag>
385struct VertexTypeFlagsHelper
386{
387 enum { capflag = capflag };
388 enum { declflag = declflag };
389};
390
391template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
392template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
393template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
394template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
395template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
396template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
397template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
398template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
399template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
400template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
401template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
402template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
403
404
405// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
406template <GLenum GLtype, bool normalized>
407struct VertexTypeMapping
408{
409};
410
411template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
412struct VertexTypeMappingBase
413{
414 enum { preferred = Preferred };
415 enum { fallback = Fallback };
416};
417
418template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
419template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
420template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
421template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
422template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
423template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
424template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
425template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
426template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
427template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
428
429
430// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
431// The conversion rules themselves are defined in vertexconversion.h.
432
433// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
434template <GLenum fromType, bool normalized, unsigned int toType>
435struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
436{
437};
438
439// All conversions from normalized types to float use the Normalize operator.
440template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
441
442// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
443template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
444template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
445
446// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
447// whether it is normalized or not.
448template <class T, bool normalized>
449struct DefaultVertexValuesStage2
450{
451};
452
453template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
454template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
455
456// Work out the default value rule for a D3D type (expressed as the C type) and
457template <class T, bool normalized>
458struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
459{
460};
461
462template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
463
464// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
465// The fallback conversion produces an output that all D3D9 devices must support.
466template <class T> struct UsePreferred { enum { type = T::preferred }; };
467template <class T> struct UseFallback { enum { type = T::fallback }; };
468
469// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
470// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
471// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
472template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
473struct Converter
474 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
475 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
476 ConversionRule<fromType,
477 normalized,
478 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
479 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
480{
481private:
482 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
483 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
484
485public:
486 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
487 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
488};
489
490// Initialise a TranslationInfo
491#define TRANSLATION(type, norm, size, preferred) \
492 { \
493 Converter<type, norm, size, preferred>::identity, \
494 Converter<type, norm, size, preferred>::finalSize, \
495 Converter<type, norm, size, preferred>::convertArray, \
496 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
497 }
498
499#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
500 { \
501 Converter<type, norm, size, UsePreferred>::capflag, \
502 TRANSLATION(type, norm, size, UsePreferred), \
503 TRANSLATION(type, norm, size, UseFallback) \
504 }
505
506#define TRANSLATIONS_FOR_TYPE(type) \
507 { \
508 { 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) }, \
509 { 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) }, \
510 }
511
512const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
513{
514 TRANSLATIONS_FOR_TYPE(GL_BYTE),
515 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
516 TRANSLATIONS_FOR_TYPE(GL_SHORT),
517 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
518 TRANSLATIONS_FOR_TYPE(GL_FIXED),
519 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
520};
521
522void VertexDataManager::checkVertexCaps(DWORD declTypes)
523{
524 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
525 {
526 for (unsigned int j = 0; j < 2; j++)
527 {
528 for (unsigned int k = 0; k < 4; k++)
529 {
530 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
531 {
532 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
533 }
534 else
535 {
536 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
537 }
538 }
539 }
540 }
541}
542
543// This is used to index mAttributeTypes and mPossibleTranslations.
544unsigned int VertexDataManager::typeIndex(GLenum type) const
545{
546 switch (type)
547 {
548 case GL_BYTE: return 0;
549 case GL_UNSIGNED_BYTE: return 1;
550 case GL_SHORT: return 2;
551 case GL_UNSIGNED_SHORT: return 3;
552 case GL_FIXED: return 4;
553 case GL_FLOAT: return 5;
554
555 default: UNREACHABLE(); return 5;
556 }
557}
558
daniel@transgaming.com83921382011-01-08 05:46:00 +0000559VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
560{
561 if (size > 0)
562 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000563 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000564 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000565 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000566
567 if (FAILED(result))
568 {
569 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
570 }
571 }
572}
573
574VertexBuffer::~VertexBuffer()
575{
576 if (mVertexBuffer)
577 {
578 mVertexBuffer->Release();
579 }
580}
581
582void VertexBuffer::unmap()
583{
584 if (mVertexBuffer)
585 {
586 mVertexBuffer->Unlock();
587 }
588}
589
590IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
591{
592 return mVertexBuffer;
593}
594
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000595unsigned int VertexBuffer::getSerial() const
596{
597 return mSerial;
598}
599
600unsigned int VertexBuffer::issueSerial()
601{
602 return mCurrentSerial++;
603}
604
daniel@transgaming.com83921382011-01-08 05:46:00 +0000605ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
606{
607 mBufferSize = size;
608 mWritePosition = 0;
609 mRequiredSpace = 0;
610}
611
612ArrayVertexBuffer::~ArrayVertexBuffer()
613{
614}
615
616void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
617{
618 mRequiredSpace += requiredSpace;
619}
620
daniel@transgaming.com83921382011-01-08 05:46:00 +0000621StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
622{
623}
624
625StreamingVertexBuffer::~StreamingVertexBuffer()
626{
627}
628
629void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
630{
631 void *mapPtr = NULL;
632
633 if (mVertexBuffer)
634 {
635 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
636
637 if (FAILED(result))
638 {
639 ERR("Lock failed with error 0x%08x", result);
640 return NULL;
641 }
642
643 *offset = mWritePosition;
644 mWritePosition += requiredSpace;
645 }
646
647 return mapPtr;
648}
649
650void StreamingVertexBuffer::reserveRequiredSpace()
651{
652 if (mRequiredSpace > mBufferSize)
653 {
654 if (mVertexBuffer)
655 {
656 mVertexBuffer->Release();
657 mVertexBuffer = NULL;
658 }
659
660 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 +0000661
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000662 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000663 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000664 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000665
666 if (FAILED(result))
667 {
668 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
669 }
670
671 mWritePosition = 0;
672 }
673 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
674 {
675 if (mVertexBuffer)
676 {
677 void *dummy;
678 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
679 mVertexBuffer->Unlock();
680 }
681
682 mWritePosition = 0;
683 }
684
685 mRequiredSpace = 0;
686}
687
688StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
689{
690}
691
692StaticVertexBuffer::~StaticVertexBuffer()
693{
694}
695
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000696void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000697{
698 void *mapPtr = NULL;
699
700 if (mVertexBuffer)
701 {
702 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
703
704 if (FAILED(result))
705 {
706 ERR("Lock failed with error 0x%08x", result);
707 return NULL;
708 }
709
710 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000711 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000712 mCache.push_back(element);
713
714 *streamOffset = mWritePosition;
715 mWritePosition += requiredSpace;
716 }
717
718 return mapPtr;
719}
720
721void StaticVertexBuffer::reserveRequiredSpace()
722{
723 if (!mVertexBuffer && mBufferSize == 0)
724 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000725 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000726 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000727 mSerial = issueSerial();
728
daniel@transgaming.com83921382011-01-08 05:46:00 +0000729 if (FAILED(result))
730 {
731 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
732 }
733
734 mBufferSize = mRequiredSpace;
735 }
736 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
737 {
738 // Already allocated
739 }
740 else UNREACHABLE(); // Static vertex buffers can't be resized
741
742 mRequiredSpace = 0;
743}
744
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000745std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000746{
747 for (unsigned int element = 0; element < mCache.size(); element++)
748 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000749 if (mCache[element].type == attribute.mType &&
750 mCache[element].size == attribute.mSize &&
751 mCache[element].stride == attribute.stride() &&
752 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000753 {
754 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
755 {
756 return mCache[element].streamOffset;
757 }
758 }
759 }
760
761 return -1;
762}
763
764const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
765{
766 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
767}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000768}