blob: 70f475f8d5f76fbfc80d0bb55d5cca79b5db4533 [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"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000016#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com37b141e2011-01-08 05:46:13 +000017#include "libGLESv2/main.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000019#include "libGLESv2/vertexconversion.h"
20#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000021
22namespace
23{
24 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000025 // This has to be at least 4k or else it fails on ATI cards.
26 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027}
28
29namespace gl
30{
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000031unsigned int VertexBuffer::mCurrentSerial = 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000032
jbauman@chromium.org059fc152011-11-18 19:26:17 +000033int elementsInBuffer(const VertexAttribute &attribute, int size)
34{
35 int stride = attribute.stride();
36 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
37}
38
daniel@transgaming.combaa74512011-04-13 14:56:47 +000039VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000040{
daniel@transgaming.com83921382011-01-08 05:46:00 +000041 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000042 {
daniel@transgaming.com83921382011-01-08 05:46:00 +000043 mDirtyCurrentValue[i] = true;
44 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000045 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000046 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000047
48 const D3DCAPS9 &caps = context->getDeviceCaps();
49 checkVertexCaps(caps.DeclTypes);
50
51 mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000052
53 if (!mStreamingBuffer)
54 {
55 ERR("Failed to allocate the streaming vertex buffer.");
56 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000057}
58
59VertexDataManager::~VertexDataManager()
60{
daniel@transgaming.com83921382011-01-08 05:46:00 +000061 delete mStreamingBuffer;
62
63 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
64 {
65 delete mCurrentValueBuffer[i];
66 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000067}
68
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000069std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute, GLsizei instances)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000070{
daniel@transgaming.com83921382011-01-08 05:46:00 +000071 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000072
daniel@transgaming.com83921382011-01-08 05:46:00 +000073 int inputStride = attribute.stride();
74 int elementSize = attribute.typeSize();
75 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000076 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000077
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000078 void *output = NULL;
79
80 if (vertexBuffer)
81 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000082 output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset);
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000083 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000084
85 if (output == NULL)
86 {
87 ERR("Failed to map vertex buffer.");
88 return -1;
89 }
90
91 const char *input = NULL;
92
93 if (buffer)
94 {
95 int offset = attribute.mOffset;
96
97 input = static_cast<const char*>(buffer->data()) + offset;
98 }
99 else
100 {
101 input = static_cast<const char*>(attribute.mPointer);
102 }
103
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000104 if (instances == 0 || attribute.mDivisor == 0)
105 {
106 input += inputStride * start;
107 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000108
109 if (converter.identity && inputStride == elementSize)
110 {
111 memcpy(output, input, count * inputStride);
112 }
113 else
114 {
115 converter.convertArray(input, inputStride, count, output);
116 }
117
118 vertexBuffer->unmap();
119
120 return streamOffset;
121}
122
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000123GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000125 if (!mStreamingBuffer)
126 {
127 return GL_OUT_OF_MEMORY;
128 }
129
daniel@transgaming.com83921382011-01-08 05:46:00 +0000130 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000131 Program *program = mContext->getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000132 ProgramBinary *programBinary = program->getProgramBinary();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000133
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000134 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000135 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000136 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000137 }
138
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000139 // 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 +0000140 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
141 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000142 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000143 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000144 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000145 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000146
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000147 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000148 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000149 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000150 {
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000151 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000152 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000153 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000154 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000155 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000156 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000157 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
158 for (int previous = 0; previous < i; previous++)
159 {
160 if (translated[previous].active && attribs[previous].mArrayEnabled)
161 {
162 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
163 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
164
165 if (staticBuffer == previousStaticBuffer)
166 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000167 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000168 }
169 }
170 }
171
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000172 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000173
174 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000175 }
176 }
177 else
178 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000179 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000180 }
181 }
182 }
183
184 // Reserve the required space per used buffer
185 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
186 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000187 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000188 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000189 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000190 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000191 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
192
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000193 if (vertexBuffer)
194 {
195 vertexBuffer->reserveRequiredSpace();
196 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000197 }
198 }
199
200 // Perform the vertex data translations
201 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
202 {
203 if (translated[i].active)
204 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000205 if (attribs[i].mArrayEnabled)
206 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000207 Buffer *buffer = attribs[i].mBoundBuffer.get();
208
daniel@transgaming.com83921382011-01-08 05:46:00 +0000209 if (!buffer && attribs[i].mPointer == NULL)
210 {
211 // This is an application error that would normally result in a crash, but we catch it and return an error
212 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000213 return GL_INVALID_OPERATION;
214 }
215
daniel@transgaming.com83921382011-01-08 05:46:00 +0000216 const FormatConverter &converter = formatConverter(attribs[i]);
217
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000218 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000219 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
220
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000221 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000222
223 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000224 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000225 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
226
227 if (streamOffset == -1)
228 {
229 // Convert the entire buffer
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000230 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000231 int startIndex = attribs[i].mOffset / attribs[i].stride();
232
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000233 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000234 }
235
236 if (streamOffset != -1)
237 {
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000238 streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
239
240 if (instances == 0 || attribs[i].mDivisor == 0)
241 {
242 streamOffset += start * converter.outputElementSize;
243 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000244 }
245 }
246 else
247 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000248 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000249 }
250
daniel@transgaming.com83921382011-01-08 05:46:00 +0000251 if (streamOffset == -1)
252 {
253 return GL_OUT_OF_MEMORY;
254 }
255
256 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000257 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000258 translated[i].divisor = attribs[i].mDivisor;
259
daniel@transgaming.com83921382011-01-08 05:46:00 +0000260 translated[i].type = converter.d3dDeclType;
261 translated[i].stride = converter.outputElementSize;
262 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000263 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000264 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000265 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000266 if (!mCurrentValueBuffer[i])
267 {
268 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mDevice, CONSTANT_VERTEX_BUFFER_SIZE);
269 }
270
271 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
272
daniel@transgaming.com83921382011-01-08 05:46:00 +0000273 if (mDirtyCurrentValue[i])
274 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000275 const int requiredSpace = 4 * sizeof(float);
276 buffer->addRequiredSpace(requiredSpace);
277 buffer->reserveRequiredSpace();
278 float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
279 if (data)
280 {
281 data[0] = attribs[i].mCurrentValue[0];
282 data[1] = attribs[i].mCurrentValue[1];
283 data[2] = attribs[i].mCurrentValue[2];
284 data[3] = attribs[i].mCurrentValue[3];
285 buffer->unmap();
286 mDirtyCurrentValue[i] = false;
287 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000288 }
289
290 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000291 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000292 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000293
294 translated[i].type = D3DDECLTYPE_FLOAT4;
295 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000296 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000297 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000298 }
299 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000300
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000301 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
302 {
303 if (translated[i].active && attribs[i].mArrayEnabled)
304 {
305 Buffer *buffer = attribs[i].mBoundBuffer.get();
306
307 if (buffer)
308 {
309 buffer->promoteStaticUsage(count * attribs[i].typeSize());
310 }
311 }
312 }
313
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000314 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000315}
316
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000317std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000318{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000319 size_t elementSize = formatConverter(attrib).outputElementSize;
320
321 if (instances == 0 || attrib.mDivisor == 0)
322 {
323 return elementSize * count;
324 }
325 else
326 {
327 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
328 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000329}
330
331// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
332//
333// BYTE SHORT (Cast)
334// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
335// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
336// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
337// SHORT SHORT (Identity)
338// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
339// UNSIGNED_SHORT FLOAT (Cast)
340// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
341// FIXED (not in WebGL) FLOAT (FixedToFloat)
342// FLOAT FLOAT (Identity)
343
344// GLToCType maps from GL type (as GLenum) to the C typedef.
345template <GLenum GLType> struct GLToCType { };
346
347template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
348template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
349template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
350template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
351template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
352template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
353
354// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
355enum D3DVertexType
356{
357 D3DVT_FLOAT,
358 D3DVT_SHORT,
359 D3DVT_SHORT_NORM,
360 D3DVT_UBYTE,
361 D3DVT_UBYTE_NORM,
362 D3DVT_USHORT_NORM
363};
364
365// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
366template <unsigned int D3DType> struct D3DToCType { };
367
368template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
369template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
370template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
371template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
372template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
373template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
374
375// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
376template <unsigned int type, int size>
377struct WidenRule
378{
379};
380
381template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
382template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
383template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
384template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
385template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
386template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
387
388// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
389template <unsigned int d3dtype, int size>
390struct VertexTypeFlags
391{
392};
393
394template <unsigned int capflag, unsigned int declflag>
395struct VertexTypeFlagsHelper
396{
397 enum { capflag = capflag };
398 enum { declflag = declflag };
399};
400
401template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
402template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
403template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
404template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
405template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
406template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
407template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
408template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
409template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
410template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
411template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
412template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
413
414
415// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
416template <GLenum GLtype, bool normalized>
417struct VertexTypeMapping
418{
419};
420
421template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
422struct VertexTypeMappingBase
423{
424 enum { preferred = Preferred };
425 enum { fallback = Fallback };
426};
427
428template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
429template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
430template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
431template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
432template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
433template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
434template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
435template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
436template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
437template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
438
439
440// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
441// The conversion rules themselves are defined in vertexconversion.h.
442
443// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
444template <GLenum fromType, bool normalized, unsigned int toType>
445struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
446{
447};
448
449// All conversions from normalized types to float use the Normalize operator.
450template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
451
452// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
daniel@transgaming.com1f1b0d52012-04-17 19:44:15 +0000453template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLint, 16> { };
454template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLint, 16> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000455
456// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
457// whether it is normalized or not.
458template <class T, bool normalized>
459struct DefaultVertexValuesStage2
460{
461};
462
463template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
464template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
465
466// Work out the default value rule for a D3D type (expressed as the C type) and
467template <class T, bool normalized>
468struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
469{
470};
471
472template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
473
474// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
475// The fallback conversion produces an output that all D3D9 devices must support.
476template <class T> struct UsePreferred { enum { type = T::preferred }; };
477template <class T> struct UseFallback { enum { type = T::fallback }; };
478
479// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
480// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
481// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
482template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
483struct Converter
484 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
485 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
486 ConversionRule<fromType,
487 normalized,
488 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
489 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
490{
491private:
492 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
493 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
494
495public:
496 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
497 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
498};
499
500// Initialise a TranslationInfo
501#define TRANSLATION(type, norm, size, preferred) \
502 { \
503 Converter<type, norm, size, preferred>::identity, \
504 Converter<type, norm, size, preferred>::finalSize, \
505 Converter<type, norm, size, preferred>::convertArray, \
506 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
507 }
508
509#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
510 { \
511 Converter<type, norm, size, UsePreferred>::capflag, \
512 TRANSLATION(type, norm, size, UsePreferred), \
513 TRANSLATION(type, norm, size, UseFallback) \
514 }
515
516#define TRANSLATIONS_FOR_TYPE(type) \
517 { \
518 { 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) }, \
519 { 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) }, \
520 }
521
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000522#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
523 { \
524 { 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) }, \
525 { 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) }, \
526 }
527
daniel@transgaming.com83921382011-01-08 05:46:00 +0000528const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
529{
530 TRANSLATIONS_FOR_TYPE(GL_BYTE),
531 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
532 TRANSLATIONS_FOR_TYPE(GL_SHORT),
533 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000534 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
535 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000536};
537
538void VertexDataManager::checkVertexCaps(DWORD declTypes)
539{
540 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
541 {
542 for (unsigned int j = 0; j < 2; j++)
543 {
544 for (unsigned int k = 0; k < 4; k++)
545 {
546 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
547 {
548 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
549 }
550 else
551 {
552 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
553 }
554 }
555 }
556 }
557}
558
559// This is used to index mAttributeTypes and mPossibleTranslations.
560unsigned int VertexDataManager::typeIndex(GLenum type) const
561{
562 switch (type)
563 {
564 case GL_BYTE: return 0;
565 case GL_UNSIGNED_BYTE: return 1;
566 case GL_SHORT: return 2;
567 case GL_UNSIGNED_SHORT: return 3;
568 case GL_FIXED: return 4;
569 case GL_FLOAT: return 5;
570
571 default: UNREACHABLE(); return 5;
572 }
573}
574
daniel@transgaming.com83921382011-01-08 05:46:00 +0000575VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
576{
577 if (size > 0)
578 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000579 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000580 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000581 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000582
583 if (FAILED(result))
584 {
585 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
586 }
587 }
588}
589
590VertexBuffer::~VertexBuffer()
591{
592 if (mVertexBuffer)
593 {
594 mVertexBuffer->Release();
595 }
596}
597
598void VertexBuffer::unmap()
599{
600 if (mVertexBuffer)
601 {
602 mVertexBuffer->Unlock();
603 }
604}
605
606IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
607{
608 return mVertexBuffer;
609}
610
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000611unsigned int VertexBuffer::getSerial() const
612{
613 return mSerial;
614}
615
616unsigned int VertexBuffer::issueSerial()
617{
618 return mCurrentSerial++;
619}
620
daniel@transgaming.com83921382011-01-08 05:46:00 +0000621ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
622{
623 mBufferSize = size;
624 mWritePosition = 0;
625 mRequiredSpace = 0;
626}
627
628ArrayVertexBuffer::~ArrayVertexBuffer()
629{
630}
631
632void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
633{
634 mRequiredSpace += requiredSpace;
635}
636
daniel@transgaming.com83921382011-01-08 05:46:00 +0000637StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
638{
639}
640
641StreamingVertexBuffer::~StreamingVertexBuffer()
642{
643}
644
645void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
646{
647 void *mapPtr = NULL;
648
649 if (mVertexBuffer)
650 {
651 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
652
653 if (FAILED(result))
654 {
655 ERR("Lock failed with error 0x%08x", result);
656 return NULL;
657 }
658
659 *offset = mWritePosition;
660 mWritePosition += requiredSpace;
661 }
662
663 return mapPtr;
664}
665
666void StreamingVertexBuffer::reserveRequiredSpace()
667{
668 if (mRequiredSpace > mBufferSize)
669 {
670 if (mVertexBuffer)
671 {
672 mVertexBuffer->Release();
673 mVertexBuffer = NULL;
674 }
675
676 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 +0000677
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000678 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000679 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000680 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000681
682 if (FAILED(result))
683 {
684 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
685 }
686
687 mWritePosition = 0;
688 }
689 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
690 {
691 if (mVertexBuffer)
692 {
693 void *dummy;
694 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
695 mVertexBuffer->Unlock();
696 }
697
698 mWritePosition = 0;
699 }
700
701 mRequiredSpace = 0;
702}
703
704StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
705{
706}
707
708StaticVertexBuffer::~StaticVertexBuffer()
709{
710}
711
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000712void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000713{
714 void *mapPtr = NULL;
715
716 if (mVertexBuffer)
717 {
718 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
719
720 if (FAILED(result))
721 {
722 ERR("Lock failed with error 0x%08x", result);
723 return NULL;
724 }
725
726 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000727 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000728 mCache.push_back(element);
729
730 *streamOffset = mWritePosition;
731 mWritePosition += requiredSpace;
732 }
733
734 return mapPtr;
735}
736
737void StaticVertexBuffer::reserveRequiredSpace()
738{
739 if (!mVertexBuffer && mBufferSize == 0)
740 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000741 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000742 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000743 mSerial = issueSerial();
744
daniel@transgaming.com83921382011-01-08 05:46:00 +0000745 if (FAILED(result))
746 {
747 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
748 }
749
750 mBufferSize = mRequiredSpace;
751 }
752 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
753 {
754 // Already allocated
755 }
756 else UNREACHABLE(); // Static vertex buffers can't be resized
757
758 mRequiredSpace = 0;
759}
760
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000761std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000762{
763 for (unsigned int element = 0; element < mCache.size(); element++)
764 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000765 if (mCache[element].type == attribute.mType &&
766 mCache[element].size == attribute.mSize &&
767 mCache[element].stride == attribute.stride() &&
768 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000769 {
770 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
771 {
772 return mCache[element].streamOffset;
773 }
774 }
775 }
776
777 return -1;
778}
779
780const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
781{
782 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
783}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000784}