blob: f08ed64b8ddfef3b38e5f902431708c6ff8be57a [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.com50aadb02012-11-28 21:06:11 +000010#include "libGLESv2/renderer/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.com2507f412012-10-31 18:46:48 +000014#include "libGLESv2/renderer/Renderer9.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015#include "libGLESv2/Buffer.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.com50aadb02012-11-28 21:06:11 +000019#include "libGLESv2/renderer/vertexconversion.h"
20#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000021
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000022#include <limits>
23
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000024namespace
25{
26 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000027 // This has to be at least 4k or else it fails on ATI cards.
28 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000029}
30
daniel@transgaming.com31240482012-11-28 21:06:41 +000031namespace rx
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000032{
33
daniel@transgaming.com31240482012-11-28 21:06:41 +000034int elementsInBuffer(const gl::VertexAttribute &attribute, int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000035{
36 int stride = attribute.stride();
37 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
38}
39
daniel@transgaming.com31240482012-11-28 21:06:41 +000040VertexDataManager::VertexDataManager(Renderer9 *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000041{
daniel@transgaming.com31240482012-11-28 21:06:41 +000042 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000043 {
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000044 mCurrentValue[i][0] = std::numeric_limits<float>::quiet_NaN();
45 mCurrentValue[i][1] = std::numeric_limits<float>::quiet_NaN();
46 mCurrentValue[i][2] = std::numeric_limits<float>::quiet_NaN();
47 mCurrentValue[i][3] = std::numeric_limits<float>::quiet_NaN();
daniel@transgaming.com83921382011-01-08 05:46:00 +000048 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000049 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000050 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000051
daniel@transgaming.com621ce052012-10-31 17:52:29 +000052 // D3D9_REPLACE
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +000053 checkVertexCaps(renderer->getCapsDeclTypes());
daniel@transgaming.com83921382011-01-08 05:46:00 +000054
daniel@transgaming.come4e45062012-12-20 20:56:53 +000055 mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000056
57 if (!mStreamingBuffer)
58 {
59 ERR("Failed to allocate the streaming vertex buffer.");
60 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000061}
62
63VertexDataManager::~VertexDataManager()
64{
daniel@transgaming.com83921382011-01-08 05:46:00 +000065 delete mStreamingBuffer;
66
daniel@transgaming.com31240482012-11-28 21:06:41 +000067 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000068 {
69 delete mCurrentValueBuffer[i];
70 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000071}
72
daniel@transgaming.come4e45062012-12-20 20:56:53 +000073std::size_t VertexDataManager::writeAttributeData(VertexBufferInterface *vertexBuffer, GLint start, GLsizei count, const gl::VertexAttribute &attribute, GLsizei instances)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000074{
daniel@transgaming.com31240482012-11-28 21:06:41 +000075 gl::Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000076
daniel@transgaming.com83921382011-01-08 05:46:00 +000077 int inputStride = attribute.stride();
78 int elementSize = attribute.typeSize();
79 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000080 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000081
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000082 void *output = NULL;
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000083
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000084 if (vertexBuffer)
85 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000086 output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset);
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000087 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000088
89 if (output == NULL)
90 {
91 ERR("Failed to map vertex buffer.");
92 return -1;
93 }
94
95 const char *input = NULL;
96
97 if (buffer)
98 {
99 int offset = attribute.mOffset;
100
101 input = static_cast<const char*>(buffer->data()) + offset;
102 }
103 else
104 {
105 input = static_cast<const char*>(attribute.mPointer);
106 }
107
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000108 if (instances == 0 || attribute.mDivisor == 0)
109 {
110 input += inputStride * start;
111 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000112
113 if (converter.identity && inputStride == elementSize)
114 {
115 memcpy(output, input, count * inputStride);
116 }
117 else
118 {
119 converter.convertArray(input, inputStride, count, output);
120 }
121
122 vertexBuffer->unmap();
123
124 return streamOffset;
125}
126
daniel@transgaming.com31240482012-11-28 21:06:41 +0000127GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000128{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000129 if (!mStreamingBuffer)
130 {
131 return GL_OUT_OF_MEMORY;
132 }
133
daniel@transgaming.com31240482012-11-28 21:06:41 +0000134 for (int attributeIndex = 0; attributeIndex < gl::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.com31240482012-11-28 21:06:41 +0000140 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000141 {
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.com31240482012-11-28 21:06:41 +0000144 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000145 StaticVertexBufferInterface *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 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000162 gl::Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000163 StaticVertexBufferInterface *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000164
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.com50aadb02012-11-28 21:06:11 +0000175 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000176 }
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
daniel@transgaming.com31240482012-11-28 21:06:41 +0000185 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000186 {
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.com31240482012-11-28 21:06:41 +0000189 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000190 VertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
191 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000192
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
daniel@transgaming.com31240482012-11-28 21:06:41 +0000201 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000202 {
203 if (translated[i].active)
204 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000205 if (attribs[i].mArrayEnabled)
206 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000207 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000208
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.come4e45062012-12-20 20:56:53 +0000218 StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
219 VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000220
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 {
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000268 mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000269 }
270
daniel@transgaming.come4e45062012-12-20 20:56:53 +0000271 StreamingVertexBufferInterface *buffer = mCurrentValueBuffer[i];
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000272
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000273 if (mCurrentValue[i][0] != attribs[i].mCurrentValue[0] ||
274 mCurrentValue[i][1] != attribs[i].mCurrentValue[1] ||
275 mCurrentValue[i][2] != attribs[i].mCurrentValue[2] ||
276 mCurrentValue[i][3] != attribs[i].mCurrentValue[3])
daniel@transgaming.com83921382011-01-08 05:46:00 +0000277 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000278 const int requiredSpace = 4 * sizeof(float);
279 buffer->addRequiredSpace(requiredSpace);
280 buffer->reserveRequiredSpace();
daniel@transgaming.com31240482012-11-28 21:06:41 +0000281 float *data = static_cast<float*>(buffer->map(gl::VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000282 if (data)
283 {
284 data[0] = attribs[i].mCurrentValue[0];
285 data[1] = attribs[i].mCurrentValue[1];
286 data[2] = attribs[i].mCurrentValue[2];
287 data[3] = attribs[i].mCurrentValue[3];
288 buffer->unmap();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000289
290 mCurrentValue[i][0] = attribs[i].mCurrentValue[0];
291 mCurrentValue[i][1] = attribs[i].mCurrentValue[1];
292 mCurrentValue[i][2] = attribs[i].mCurrentValue[2];
293 mCurrentValue[i][3] = attribs[i].mCurrentValue[3];
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000294 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000295 }
296
297 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000298 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000299 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000300
301 translated[i].type = D3DDECLTYPE_FLOAT4;
302 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000303 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000304 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000305 }
306 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000307
daniel@transgaming.com31240482012-11-28 21:06:41 +0000308 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000309 {
310 if (translated[i].active && attribs[i].mArrayEnabled)
311 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000312 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000313
314 if (buffer)
315 {
316 buffer->promoteStaticUsage(count * attribs[i].typeSize());
317 }
318 }
319 }
320
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000321 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000322}
323
daniel@transgaming.com31240482012-11-28 21:06:41 +0000324std::size_t VertexDataManager::spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000325{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000326 size_t elementSize = formatConverter(attrib).outputElementSize;
327
328 if (instances == 0 || attrib.mDivisor == 0)
329 {
330 return elementSize * count;
331 }
332 else
333 {
334 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
335 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000336}
337
338// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
339//
340// BYTE SHORT (Cast)
341// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
342// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
343// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
344// SHORT SHORT (Identity)
345// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
346// UNSIGNED_SHORT FLOAT (Cast)
347// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
348// FIXED (not in WebGL) FLOAT (FixedToFloat)
349// FLOAT FLOAT (Identity)
350
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000351// GLToCType maps from GL type (as GLenum) to the C typedef.
daniel@transgaming.com83921382011-01-08 05:46:00 +0000352template <GLenum GLType> struct GLToCType { };
353
354template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
355template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
356template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
357template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
358template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
359template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
360
361// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
362enum D3DVertexType
363{
364 D3DVT_FLOAT,
365 D3DVT_SHORT,
366 D3DVT_SHORT_NORM,
367 D3DVT_UBYTE,
368 D3DVT_UBYTE_NORM,
369 D3DVT_USHORT_NORM
370};
371
372// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
373template <unsigned int D3DType> struct D3DToCType { };
374
375template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
376template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
377template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
378template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
379template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
380template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
381
382// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
383template <unsigned int type, int size>
384struct WidenRule
385{
386};
387
daniel@transgaming.com31240482012-11-28 21:06:41 +0000388template <int size> struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size> { };
389template <int size> struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size> { };
390template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size> { };
391template <int size> struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size> { };
392template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size> { };
393template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000394
395// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
396template <unsigned int d3dtype, int size>
397struct VertexTypeFlags
398{
399};
400
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000401template <unsigned int _capflag, unsigned int _declflag>
daniel@transgaming.com83921382011-01-08 05:46:00 +0000402struct VertexTypeFlagsHelper
403{
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000404 enum { capflag = _capflag };
405 enum { declflag = _declflag };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000406};
407
408template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
409template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
410template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
411template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
412template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
413template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
414template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
415template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
416template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
417template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
418template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
419template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
420
421
422// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
423template <GLenum GLtype, bool normalized>
424struct VertexTypeMapping
425{
426};
427
428template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
429struct VertexTypeMappingBase
430{
431 enum { preferred = Preferred };
432 enum { fallback = Fallback };
433};
434
435template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
436template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
437template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
438template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
439template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
440template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
441template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
442template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
443template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
444template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
445
446
447// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
448// The conversion rules themselves are defined in vertexconversion.h.
449
450// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
451template <GLenum fromType, bool normalized, unsigned int toType>
daniel@transgaming.com31240482012-11-28 21:06:41 +0000452struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
daniel@transgaming.com83921382011-01-08 05:46:00 +0000453{
454};
455
456// All conversions from normalized types to float use the Normalize operator.
daniel@transgaming.com31240482012-11-28 21:06:41 +0000457template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000458
459// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
daniel@transgaming.com31240482012-11-28 21:06:41 +0000460template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
461template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000462
463// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
464// whether it is normalized or not.
465template <class T, bool normalized>
466struct DefaultVertexValuesStage2
467{
468};
469
daniel@transgaming.com31240482012-11-28 21:06:41 +0000470template <class T> struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T> { };
471template <class T> struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000472
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000473// Work out the default value rule for a D3D type (expressed as the C type) and
daniel@transgaming.com83921382011-01-08 05:46:00 +0000474template <class T, bool normalized>
475struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
476{
477};
478
daniel@transgaming.com31240482012-11-28 21:06:41 +0000479template <bool normalized> struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000480
481// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
482// The fallback conversion produces an output that all D3D9 devices must support.
483template <class T> struct UsePreferred { enum { type = T::preferred }; };
484template <class T> struct UseFallback { enum { type = T::fallback }; };
485
486// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
487// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
488// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
489template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
490struct Converter
daniel@transgaming.com31240482012-11-28 21:06:41 +0000491 : VertexDataConverter<typename GLToCType<fromType>::type,
492 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
493 ConversionRule<fromType,
494 normalized,
495 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
496 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
daniel@transgaming.com83921382011-01-08 05:46:00 +0000497{
498private:
499 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
500 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
501
502public:
503 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
504 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
505};
506
507// Initialise a TranslationInfo
508#define TRANSLATION(type, norm, size, preferred) \
509 { \
510 Converter<type, norm, size, preferred>::identity, \
511 Converter<type, norm, size, preferred>::finalSize, \
512 Converter<type, norm, size, preferred>::convertArray, \
513 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
514 }
515
516#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
517 { \
518 Converter<type, norm, size, UsePreferred>::capflag, \
519 TRANSLATION(type, norm, size, UsePreferred), \
520 TRANSLATION(type, norm, size, UseFallback) \
521 }
522
523#define TRANSLATIONS_FOR_TYPE(type) \
524 { \
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 { 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) }, \
527 }
528
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000529#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
530 { \
531 { 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) }, \
532 { 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) }, \
533 }
534
daniel@transgaming.com83921382011-01-08 05:46:00 +0000535const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
536{
537 TRANSLATIONS_FOR_TYPE(GL_BYTE),
538 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
539 TRANSLATIONS_FOR_TYPE(GL_SHORT),
540 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000541 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
542 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000543};
544
545void VertexDataManager::checkVertexCaps(DWORD declTypes)
546{
547 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
548 {
549 for (unsigned int j = 0; j < 2; j++)
550 {
551 for (unsigned int k = 0; k < 4; k++)
552 {
553 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
554 {
555 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
556 }
557 else
558 {
559 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
560 }
561 }
562 }
563 }
564}
565
566// This is used to index mAttributeTypes and mPossibleTranslations.
567unsigned int VertexDataManager::typeIndex(GLenum type) const
568{
569 switch (type)
570 {
571 case GL_BYTE: return 0;
572 case GL_UNSIGNED_BYTE: return 1;
573 case GL_SHORT: return 2;
574 case GL_UNSIGNED_SHORT: return 3;
575 case GL_FIXED: return 4;
576 case GL_FLOAT: return 5;
577
578 default: UNREACHABLE(); return 5;
579 }
580}
581
daniel@transgaming.com83921382011-01-08 05:46:00 +0000582
daniel@transgaming.com31240482012-11-28 21:06:41 +0000583const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const gl::VertexAttribute &attribute) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000584{
585 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
586}
daniel@transgaming.com29787c32012-12-20 20:55:48 +0000587
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000588}