blob: 95bc3fb8783c295a732a132d71725e5cbbf96f4a [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
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000103 if (instances == 0 || attribute.mDivisor == 0)
104 {
105 input += inputStride * start;
106 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000107
108 if (converter.identity && inputStride == elementSize)
109 {
110 memcpy(output, input, count * inputStride);
111 }
112 else
113 {
114 converter.convertArray(input, inputStride, count, output);
115 }
116
117 vertexBuffer->unmap();
118
119 return streamOffset;
120}
121
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000122GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000123{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000124 if (!mStreamingBuffer)
125 {
126 return GL_OUT_OF_MEMORY;
127 }
128
daniel@transgaming.com83921382011-01-08 05:46:00 +0000129 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000130 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000131
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000132 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000133 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000134 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000135 }
136
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000137 // 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 +0000138 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
139 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000140 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000141 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000142 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000143 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000144
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000145 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000146 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000147 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000148 {
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000149 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000150 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000151 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000152 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000153 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000154 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000155 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
156 for (int previous = 0; previous < i; previous++)
157 {
158 if (translated[previous].active && attribs[previous].mArrayEnabled)
159 {
160 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
161 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
162
163 if (staticBuffer == previousStaticBuffer)
164 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000165 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000166 }
167 }
168 }
169
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000170 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000171
172 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000173 }
174 }
175 else
176 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000177 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000178 }
179 }
180 }
181
182 // Reserve the required space per used buffer
183 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
184 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000185 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000186 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000187 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000188 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000189 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
190
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000191 if (vertexBuffer)
192 {
193 vertexBuffer->reserveRequiredSpace();
194 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000195 }
196 }
197
198 // Perform the vertex data translations
199 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
200 {
201 if (translated[i].active)
202 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000203 if (attribs[i].mArrayEnabled)
204 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000205 Buffer *buffer = attribs[i].mBoundBuffer.get();
206
daniel@transgaming.com83921382011-01-08 05:46:00 +0000207 if (!buffer && attribs[i].mPointer == NULL)
208 {
209 // This is an application error that would normally result in a crash, but we catch it and return an error
210 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000211 return GL_INVALID_OPERATION;
212 }
213
daniel@transgaming.com83921382011-01-08 05:46:00 +0000214 const FormatConverter &converter = formatConverter(attribs[i]);
215
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000216 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000217 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
218
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000219 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000220
221 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000222 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000223 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
224
225 if (streamOffset == -1)
226 {
227 // Convert the entire buffer
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000228 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000229 int startIndex = attribs[i].mOffset / attribs[i].stride();
230
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000231 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000232 }
233
234 if (streamOffset != -1)
235 {
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000236 streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
237
238 if (instances == 0 || attribs[i].mDivisor == 0)
239 {
240 streamOffset += start * converter.outputElementSize;
241 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000242 }
243 }
244 else
245 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000246 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000247 }
248
daniel@transgaming.com83921382011-01-08 05:46:00 +0000249 if (streamOffset == -1)
250 {
251 return GL_OUT_OF_MEMORY;
252 }
253
254 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000255 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000256 translated[i].divisor = attribs[i].mDivisor;
257
daniel@transgaming.com83921382011-01-08 05:46:00 +0000258 translated[i].type = converter.d3dDeclType;
259 translated[i].stride = converter.outputElementSize;
260 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000261 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000262 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000263 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000264 if (!mCurrentValueBuffer[i])
265 {
266 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mDevice, CONSTANT_VERTEX_BUFFER_SIZE);
267 }
268
269 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
270
daniel@transgaming.com83921382011-01-08 05:46:00 +0000271 if (mDirtyCurrentValue[i])
272 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000273 const int requiredSpace = 4 * sizeof(float);
274 buffer->addRequiredSpace(requiredSpace);
275 buffer->reserveRequiredSpace();
276 float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
277 if (data)
278 {
279 data[0] = attribs[i].mCurrentValue[0];
280 data[1] = attribs[i].mCurrentValue[1];
281 data[2] = attribs[i].mCurrentValue[2];
282 data[3] = attribs[i].mCurrentValue[3];
283 buffer->unmap();
284 mDirtyCurrentValue[i] = false;
285 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000286 }
287
288 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000289 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000290 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000291
292 translated[i].type = D3DDECLTYPE_FLOAT4;
293 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000294 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000295 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000296 }
297 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000298
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000299 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
300 {
301 if (translated[i].active && attribs[i].mArrayEnabled)
302 {
303 Buffer *buffer = attribs[i].mBoundBuffer.get();
304
305 if (buffer)
306 {
307 buffer->promoteStaticUsage(count * attribs[i].typeSize());
308 }
309 }
310 }
311
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000312 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000313}
314
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000315std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000316{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000317 size_t elementSize = formatConverter(attrib).outputElementSize;
318
319 if (instances == 0 || attrib.mDivisor == 0)
320 {
321 return elementSize * count;
322 }
323 else
324 {
325 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
326 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000327}
328
329// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
330//
331// BYTE SHORT (Cast)
332// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
333// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
334// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
335// SHORT SHORT (Identity)
336// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
337// UNSIGNED_SHORT FLOAT (Cast)
338// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
339// FIXED (not in WebGL) FLOAT (FixedToFloat)
340// FLOAT FLOAT (Identity)
341
342// GLToCType maps from GL type (as GLenum) to the C typedef.
343template <GLenum GLType> struct GLToCType { };
344
345template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
346template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
347template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
348template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
349template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
350template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
351
352// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
353enum D3DVertexType
354{
355 D3DVT_FLOAT,
356 D3DVT_SHORT,
357 D3DVT_SHORT_NORM,
358 D3DVT_UBYTE,
359 D3DVT_UBYTE_NORM,
360 D3DVT_USHORT_NORM
361};
362
363// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
364template <unsigned int D3DType> struct D3DToCType { };
365
366template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
367template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
368template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
369template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
370template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
371template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
372
373// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
374template <unsigned int type, int size>
375struct WidenRule
376{
377};
378
379template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
380template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
381template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
382template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
383template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
384template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
385
386// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
387template <unsigned int d3dtype, int size>
388struct VertexTypeFlags
389{
390};
391
392template <unsigned int capflag, unsigned int declflag>
393struct VertexTypeFlagsHelper
394{
395 enum { capflag = capflag };
396 enum { declflag = declflag };
397};
398
399template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
400template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
401template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
402template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
403template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
404template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
405template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
406template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
407template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
408template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
409template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
410template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
411
412
413// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
414template <GLenum GLtype, bool normalized>
415struct VertexTypeMapping
416{
417};
418
419template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
420struct VertexTypeMappingBase
421{
422 enum { preferred = Preferred };
423 enum { fallback = Fallback };
424};
425
426template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
427template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
428template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
429template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
430template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
431template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
432template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
433template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
434template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
435template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
436
437
438// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
439// The conversion rules themselves are defined in vertexconversion.h.
440
441// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
442template <GLenum fromType, bool normalized, unsigned int toType>
443struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
444{
445};
446
447// All conversions from normalized types to float use the Normalize operator.
448template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
449
450// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
451template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
452template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
453
454// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
455// whether it is normalized or not.
456template <class T, bool normalized>
457struct DefaultVertexValuesStage2
458{
459};
460
461template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
462template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
463
464// Work out the default value rule for a D3D type (expressed as the C type) and
465template <class T, bool normalized>
466struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
467{
468};
469
470template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
471
472// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
473// The fallback conversion produces an output that all D3D9 devices must support.
474template <class T> struct UsePreferred { enum { type = T::preferred }; };
475template <class T> struct UseFallback { enum { type = T::fallback }; };
476
477// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
478// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
479// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
480template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
481struct Converter
482 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
483 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
484 ConversionRule<fromType,
485 normalized,
486 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
487 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
488{
489private:
490 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
491 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
492
493public:
494 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
495 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
496};
497
498// Initialise a TranslationInfo
499#define TRANSLATION(type, norm, size, preferred) \
500 { \
501 Converter<type, norm, size, preferred>::identity, \
502 Converter<type, norm, size, preferred>::finalSize, \
503 Converter<type, norm, size, preferred>::convertArray, \
504 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
505 }
506
507#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
508 { \
509 Converter<type, norm, size, UsePreferred>::capflag, \
510 TRANSLATION(type, norm, size, UsePreferred), \
511 TRANSLATION(type, norm, size, UseFallback) \
512 }
513
514#define TRANSLATIONS_FOR_TYPE(type) \
515 { \
516 { 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) }, \
517 { 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) }, \
518 }
519
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000520#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
521 { \
522 { 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) }, \
523 { 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) }, \
524 }
525
daniel@transgaming.com83921382011-01-08 05:46:00 +0000526const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
527{
528 TRANSLATIONS_FOR_TYPE(GL_BYTE),
529 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
530 TRANSLATIONS_FOR_TYPE(GL_SHORT),
531 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000532 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
533 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000534};
535
536void VertexDataManager::checkVertexCaps(DWORD declTypes)
537{
538 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
539 {
540 for (unsigned int j = 0; j < 2; j++)
541 {
542 for (unsigned int k = 0; k < 4; k++)
543 {
544 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
545 {
546 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
547 }
548 else
549 {
550 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
551 }
552 }
553 }
554 }
555}
556
557// This is used to index mAttributeTypes and mPossibleTranslations.
558unsigned int VertexDataManager::typeIndex(GLenum type) const
559{
560 switch (type)
561 {
562 case GL_BYTE: return 0;
563 case GL_UNSIGNED_BYTE: return 1;
564 case GL_SHORT: return 2;
565 case GL_UNSIGNED_SHORT: return 3;
566 case GL_FIXED: return 4;
567 case GL_FLOAT: return 5;
568
569 default: UNREACHABLE(); return 5;
570 }
571}
572
daniel@transgaming.com83921382011-01-08 05:46:00 +0000573VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
574{
575 if (size > 0)
576 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000577 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000578 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000579 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000580
581 if (FAILED(result))
582 {
583 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
584 }
585 }
586}
587
588VertexBuffer::~VertexBuffer()
589{
590 if (mVertexBuffer)
591 {
592 mVertexBuffer->Release();
593 }
594}
595
596void VertexBuffer::unmap()
597{
598 if (mVertexBuffer)
599 {
600 mVertexBuffer->Unlock();
601 }
602}
603
604IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
605{
606 return mVertexBuffer;
607}
608
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000609unsigned int VertexBuffer::getSerial() const
610{
611 return mSerial;
612}
613
614unsigned int VertexBuffer::issueSerial()
615{
616 return mCurrentSerial++;
617}
618
daniel@transgaming.com83921382011-01-08 05:46:00 +0000619ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
620{
621 mBufferSize = size;
622 mWritePosition = 0;
623 mRequiredSpace = 0;
624}
625
626ArrayVertexBuffer::~ArrayVertexBuffer()
627{
628}
629
630void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
631{
632 mRequiredSpace += requiredSpace;
633}
634
daniel@transgaming.com83921382011-01-08 05:46:00 +0000635StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
636{
637}
638
639StreamingVertexBuffer::~StreamingVertexBuffer()
640{
641}
642
643void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
644{
645 void *mapPtr = NULL;
646
647 if (mVertexBuffer)
648 {
649 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
650
651 if (FAILED(result))
652 {
653 ERR("Lock failed with error 0x%08x", result);
654 return NULL;
655 }
656
657 *offset = mWritePosition;
658 mWritePosition += requiredSpace;
659 }
660
661 return mapPtr;
662}
663
664void StreamingVertexBuffer::reserveRequiredSpace()
665{
666 if (mRequiredSpace > mBufferSize)
667 {
668 if (mVertexBuffer)
669 {
670 mVertexBuffer->Release();
671 mVertexBuffer = NULL;
672 }
673
674 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 +0000675
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000676 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000677 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000678 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000679
680 if (FAILED(result))
681 {
682 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
683 }
684
685 mWritePosition = 0;
686 }
687 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
688 {
689 if (mVertexBuffer)
690 {
691 void *dummy;
692 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
693 mVertexBuffer->Unlock();
694 }
695
696 mWritePosition = 0;
697 }
698
699 mRequiredSpace = 0;
700}
701
702StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
703{
704}
705
706StaticVertexBuffer::~StaticVertexBuffer()
707{
708}
709
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000710void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000711{
712 void *mapPtr = NULL;
713
714 if (mVertexBuffer)
715 {
716 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
717
718 if (FAILED(result))
719 {
720 ERR("Lock failed with error 0x%08x", result);
721 return NULL;
722 }
723
724 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000725 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000726 mCache.push_back(element);
727
728 *streamOffset = mWritePosition;
729 mWritePosition += requiredSpace;
730 }
731
732 return mapPtr;
733}
734
735void StaticVertexBuffer::reserveRequiredSpace()
736{
737 if (!mVertexBuffer && mBufferSize == 0)
738 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000739 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000740 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000741 mSerial = issueSerial();
742
daniel@transgaming.com83921382011-01-08 05:46:00 +0000743 if (FAILED(result))
744 {
745 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
746 }
747
748 mBufferSize = mRequiredSpace;
749 }
750 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
751 {
752 // Already allocated
753 }
754 else UNREACHABLE(); // Static vertex buffers can't be resized
755
756 mRequiredSpace = 0;
757}
758
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000759std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000760{
761 for (unsigned int element = 0; element < mCache.size(); element++)
762 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000763 if (mCache[element].type == attribute.mType &&
764 mCache[element].size == attribute.mSize &&
765 mCache[element].stride == attribute.stride() &&
766 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000767 {
768 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
769 {
770 return mCache[element].streamOffset;
771 }
772 }
773 }
774
775 return -1;
776}
777
778const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
779{
780 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
781}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000782}