blob: 0768d25d4d91dfb262e048ca4b688a367f0d8a46 [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.comb7386992012-10-31 18:29:58 +000039VertexDataManager::VertexDataManager(Context *context, renderer::Renderer *renderer) : mContext(context), mRenderer(renderer)
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
daniel@transgaming.com621ce052012-10-31 17:52:29 +000048 // D3D9_REPLACE
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +000049 checkVertexCaps(renderer->getCapsDeclTypes());
daniel@transgaming.com83921382011-01-08 05:46:00 +000050
daniel@transgaming.comb7386992012-10-31 18:29:58 +000051 mStreamingBuffer = new StreamingVertexBuffer(renderer, 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.com62a28462012-07-24 18:33:59 +0000131 ProgramBinary *programBinary = mContext->getCurrentProgramBinary();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000132
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000133 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000134 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000135 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000136 }
137
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000138 // 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 +0000139 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
140 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000141 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000142 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000143 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000144 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000145
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000146 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000147 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000148 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000149 {
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000150 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000151 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000152 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000153 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000154 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000155 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000156 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
157 for (int previous = 0; previous < i; previous++)
158 {
159 if (translated[previous].active && attribs[previous].mArrayEnabled)
160 {
161 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
162 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
163
164 if (staticBuffer == previousStaticBuffer)
165 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000166 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000167 }
168 }
169 }
170
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000171 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000172
173 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000174 }
175 }
176 else
177 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000178 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000179 }
180 }
181 }
182
183 // Reserve the required space per used buffer
184 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
185 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000186 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000187 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000188 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000189 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000190 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
191
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000192 if (vertexBuffer)
193 {
194 vertexBuffer->reserveRequiredSpace();
195 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000196 }
197 }
198
199 // Perform the vertex data translations
200 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
201 {
202 if (translated[i].active)
203 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000204 if (attribs[i].mArrayEnabled)
205 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000206 Buffer *buffer = attribs[i].mBoundBuffer.get();
207
daniel@transgaming.com83921382011-01-08 05:46:00 +0000208 if (!buffer && attribs[i].mPointer == NULL)
209 {
210 // This is an application error that would normally result in a crash, but we catch it and return an error
211 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000212 return GL_INVALID_OPERATION;
213 }
214
daniel@transgaming.com83921382011-01-08 05:46:00 +0000215 const FormatConverter &converter = formatConverter(attribs[i]);
216
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000217 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000218 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
219
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000220 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000221
222 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000223 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000224 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
225
226 if (streamOffset == -1)
227 {
228 // Convert the entire buffer
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000229 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000230 int startIndex = attribs[i].mOffset / attribs[i].stride();
231
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000232 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000233 }
234
235 if (streamOffset != -1)
236 {
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000237 streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
238
239 if (instances == 0 || attribs[i].mDivisor == 0)
240 {
241 streamOffset += start * converter.outputElementSize;
242 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000243 }
244 }
245 else
246 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000247 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000248 }
249
daniel@transgaming.com83921382011-01-08 05:46:00 +0000250 if (streamOffset == -1)
251 {
252 return GL_OUT_OF_MEMORY;
253 }
254
255 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000256 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000257 translated[i].divisor = attribs[i].mDivisor;
258
daniel@transgaming.com83921382011-01-08 05:46:00 +0000259 translated[i].type = converter.d3dDeclType;
260 translated[i].stride = converter.outputElementSize;
261 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000262 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000263 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000264 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000265 if (!mCurrentValueBuffer[i])
266 {
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000267 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000268 }
269
270 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
271
daniel@transgaming.com83921382011-01-08 05:46:00 +0000272 if (mDirtyCurrentValue[i])
273 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000274 const int requiredSpace = 4 * sizeof(float);
275 buffer->addRequiredSpace(requiredSpace);
276 buffer->reserveRequiredSpace();
277 float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
278 if (data)
279 {
280 data[0] = attribs[i].mCurrentValue[0];
281 data[1] = attribs[i].mCurrentValue[1];
282 data[2] = attribs[i].mCurrentValue[2];
283 data[3] = attribs[i].mCurrentValue[3];
284 buffer->unmap();
285 mDirtyCurrentValue[i] = false;
286 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000287 }
288
289 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000290 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000291 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000292
293 translated[i].type = D3DDECLTYPE_FLOAT4;
294 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000295 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000296 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000297 }
298 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000299
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000300 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
301 {
302 if (translated[i].active && attribs[i].mArrayEnabled)
303 {
304 Buffer *buffer = attribs[i].mBoundBuffer.get();
305
306 if (buffer)
307 {
308 buffer->promoteStaticUsage(count * attribs[i].typeSize());
309 }
310 }
311 }
312
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000313 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000314}
315
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000316std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000317{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000318 size_t elementSize = formatConverter(attrib).outputElementSize;
319
320 if (instances == 0 || attrib.mDivisor == 0)
321 {
322 return elementSize * count;
323 }
324 else
325 {
326 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
327 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000328}
329
330// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
331//
332// BYTE SHORT (Cast)
333// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
334// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
335// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
336// SHORT SHORT (Identity)
337// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
338// UNSIGNED_SHORT FLOAT (Cast)
339// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
340// FIXED (not in WebGL) FLOAT (FixedToFloat)
341// FLOAT FLOAT (Identity)
342
343// GLToCType maps from GL type (as GLenum) to the C typedef.
344template <GLenum GLType> struct GLToCType { };
345
346template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
347template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
348template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
349template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
350template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
351template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
352
353// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
354enum D3DVertexType
355{
356 D3DVT_FLOAT,
357 D3DVT_SHORT,
358 D3DVT_SHORT_NORM,
359 D3DVT_UBYTE,
360 D3DVT_UBYTE_NORM,
361 D3DVT_USHORT_NORM
362};
363
364// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
365template <unsigned int D3DType> struct D3DToCType { };
366
367template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
368template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
369template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
370template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
371template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
372template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
373
374// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
375template <unsigned int type, int size>
376struct WidenRule
377{
378};
379
380template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
381template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
382template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
383template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
384template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
385template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
386
387// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
388template <unsigned int d3dtype, int size>
389struct VertexTypeFlags
390{
391};
392
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000393template <unsigned int _capflag, unsigned int _declflag>
daniel@transgaming.com83921382011-01-08 05:46:00 +0000394struct VertexTypeFlagsHelper
395{
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000396 enum { capflag = _capflag };
397 enum { declflag = _declflag };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000398};
399
400template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
401template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
402template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
403template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
404template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
405template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
406template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
407template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
408template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
409template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
410template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
411template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
412
413
414// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
415template <GLenum GLtype, bool normalized>
416struct VertexTypeMapping
417{
418};
419
420template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
421struct VertexTypeMappingBase
422{
423 enum { preferred = Preferred };
424 enum { fallback = Fallback };
425};
426
427template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
428template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
429template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
430template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
431template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
432template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
433template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
434template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
435template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
436template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
437
438
439// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
440// The conversion rules themselves are defined in vertexconversion.h.
441
442// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
443template <GLenum fromType, bool normalized, unsigned int toType>
444struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
445{
446};
447
448// All conversions from normalized types to float use the Normalize operator.
449template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
450
451// 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 +0000452template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLint, 16> { };
453template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLint, 16> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000454
455// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
456// whether it is normalized or not.
457template <class T, bool normalized>
458struct DefaultVertexValuesStage2
459{
460};
461
462template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
463template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
464
465// Work out the default value rule for a D3D type (expressed as the C type) and
466template <class T, bool normalized>
467struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
468{
469};
470
471template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
472
473// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
474// The fallback conversion produces an output that all D3D9 devices must support.
475template <class T> struct UsePreferred { enum { type = T::preferred }; };
476template <class T> struct UseFallback { enum { type = T::fallback }; };
477
478// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
479// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
480// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
481template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
482struct Converter
483 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
484 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
485 ConversionRule<fromType,
486 normalized,
487 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
488 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
489{
490private:
491 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
492 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
493
494public:
495 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
496 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
497};
498
499// Initialise a TranslationInfo
500#define TRANSLATION(type, norm, size, preferred) \
501 { \
502 Converter<type, norm, size, preferred>::identity, \
503 Converter<type, norm, size, preferred>::finalSize, \
504 Converter<type, norm, size, preferred>::convertArray, \
505 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
506 }
507
508#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
509 { \
510 Converter<type, norm, size, UsePreferred>::capflag, \
511 TRANSLATION(type, norm, size, UsePreferred), \
512 TRANSLATION(type, norm, size, UseFallback) \
513 }
514
515#define TRANSLATIONS_FOR_TYPE(type) \
516 { \
517 { 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) }, \
518 { 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) }, \
519 }
520
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000521#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
522 { \
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 { 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 }
526
daniel@transgaming.com83921382011-01-08 05:46:00 +0000527const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
528{
529 TRANSLATIONS_FOR_TYPE(GL_BYTE),
530 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
531 TRANSLATIONS_FOR_TYPE(GL_SHORT),
532 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000533 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
534 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000535};
536
537void VertexDataManager::checkVertexCaps(DWORD declTypes)
538{
539 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
540 {
541 for (unsigned int j = 0; j < 2; j++)
542 {
543 for (unsigned int k = 0; k < 4; k++)
544 {
545 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
546 {
547 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
548 }
549 else
550 {
551 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
552 }
553 }
554 }
555 }
556}
557
558// This is used to index mAttributeTypes and mPossibleTranslations.
559unsigned int VertexDataManager::typeIndex(GLenum type) const
560{
561 switch (type)
562 {
563 case GL_BYTE: return 0;
564 case GL_UNSIGNED_BYTE: return 1;
565 case GL_SHORT: return 2;
566 case GL_UNSIGNED_SHORT: return 3;
567 case GL_FIXED: return 4;
568 case GL_FLOAT: return 5;
569
570 default: UNREACHABLE(); return 5;
571 }
572}
573
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000574VertexBuffer::VertexBuffer(renderer::Renderer *renderer, std::size_t size, DWORD usageFlags) : mRenderer(renderer), mVertexBuffer(NULL)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000575{
576 if (size > 0)
577 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000578 // D3D9_REPLACE
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000579 D3DPOOL pool = mRenderer->getBufferPool(usageFlags);
580 HRESULT result = mRenderer->getDevice()->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.comb7386992012-10-31 18:29:58 +0000621ArrayVertexBuffer::ArrayVertexBuffer(renderer::Renderer *renderer, std::size_t size, DWORD usageFlags) : VertexBuffer(renderer, size, usageFlags)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000622{
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.comb7386992012-10-31 18:29:58 +0000637StreamingVertexBuffer::StreamingVertexBuffer(renderer::Renderer *renderer, std::size_t initialSize) : ArrayVertexBuffer(renderer, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000638{
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.com621ce052012-10-31 17:52:29 +0000678 // D3D9_REPLACE
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000679 D3DPOOL pool = mRenderer->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
680 HRESULT result = mRenderer->getDevice()->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000681 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000682
683 if (FAILED(result))
684 {
685 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
686 }
687
688 mWritePosition = 0;
689 }
690 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
691 {
692 if (mVertexBuffer)
693 {
694 void *dummy;
695 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
696 mVertexBuffer->Unlock();
697 }
698
699 mWritePosition = 0;
700 }
701
702 mRequiredSpace = 0;
703}
704
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000705StaticVertexBuffer::StaticVertexBuffer(renderer::Renderer *renderer) : ArrayVertexBuffer(renderer, 0, D3DUSAGE_WRITEONLY)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000706{
707}
708
709StaticVertexBuffer::~StaticVertexBuffer()
710{
711}
712
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000713void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000714{
715 void *mapPtr = NULL;
716
717 if (mVertexBuffer)
718 {
719 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
720
721 if (FAILED(result))
722 {
723 ERR("Lock failed with error 0x%08x", result);
724 return NULL;
725 }
726
727 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000728 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000729 mCache.push_back(element);
730
731 *streamOffset = mWritePosition;
732 mWritePosition += requiredSpace;
733 }
734
735 return mapPtr;
736}
737
738void StaticVertexBuffer::reserveRequiredSpace()
739{
740 if (!mVertexBuffer && mBufferSize == 0)
741 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000742 // D3D9_REPLACE
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000743 D3DPOOL pool = mRenderer->getBufferPool(D3DUSAGE_WRITEONLY);
744 HRESULT result = mRenderer->getDevice()->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000745 mSerial = issueSerial();
746
daniel@transgaming.com83921382011-01-08 05:46:00 +0000747 if (FAILED(result))
748 {
749 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
750 }
751
752 mBufferSize = mRequiredSpace;
753 }
754 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
755 {
756 // Already allocated
757 }
758 else UNREACHABLE(); // Static vertex buffers can't be resized
759
760 mRequiredSpace = 0;
761}
762
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000763std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000764{
765 for (unsigned int element = 0; element < mCache.size(); element++)
766 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000767 if (mCache[element].type == attribute.mType &&
768 mCache[element].size == attribute.mSize &&
769 mCache[element].stride == attribute.stride() &&
770 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000771 {
772 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
773 {
774 return mCache[element].streamOffset;
775 }
776 }
777 }
778
779 return -1;
780}
781
782const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
783{
784 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
785}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000786}