blob: 44bd247ed2502ae70ebf0c503dd3ed44140f5a95 [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.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"
16#include "libGLESv2/Program.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000017#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com37b141e2011-01-08 05:46:13 +000018#include "libGLESv2/main.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000019
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000020#include "libGLESv2/vertexconversion.h"
21#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000022
23namespace
24{
25 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000026 // This has to be at least 4k or else it fails on ATI cards.
27 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000028}
29
30namespace gl
31{
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000032unsigned int VertexBuffer::mCurrentSerial = 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000033
jbauman@chromium.org059fc152011-11-18 19:26:17 +000034int elementsInBuffer(const VertexAttribute &attribute, int size)
35{
36 int stride = attribute.stride();
37 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
38}
39
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000040VertexDataManager::VertexDataManager(rx::Renderer9 *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000041{
daniel@transgaming.com83921382011-01-08 05:46:00 +000042 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000043 {
daniel@transgaming.com83921382011-01-08 05:46:00 +000044 mDirtyCurrentValue[i] = true;
45 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000046 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000047 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000048
daniel@transgaming.com621ce052012-10-31 17:52:29 +000049 // D3D9_REPLACE
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +000050 checkVertexCaps(renderer->getCapsDeclTypes());
daniel@transgaming.com83921382011-01-08 05:46:00 +000051
daniel@transgaming.comb7386992012-10-31 18:29:58 +000052 mStreamingBuffer = new StreamingVertexBuffer(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000053
54 if (!mStreamingBuffer)
55 {
56 ERR("Failed to allocate the streaming vertex buffer.");
57 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000058}
59
60VertexDataManager::~VertexDataManager()
61{
daniel@transgaming.com83921382011-01-08 05:46:00 +000062 delete mStreamingBuffer;
63
64 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
65 {
66 delete mCurrentValueBuffer[i];
67 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000068}
69
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000070std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute, GLsizei instances)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000071{
daniel@transgaming.com83921382011-01-08 05:46:00 +000072 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000073
daniel@transgaming.com83921382011-01-08 05:46:00 +000074 int inputStride = attribute.stride();
75 int elementSize = attribute.typeSize();
76 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000077 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000078
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000079 void *output = NULL;
80
81 if (vertexBuffer)
82 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000083 output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset);
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000084 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000085
86 if (output == NULL)
87 {
88 ERR("Failed to map vertex buffer.");
89 return -1;
90 }
91
92 const char *input = NULL;
93
94 if (buffer)
95 {
96 int offset = attribute.mOffset;
97
98 input = static_cast<const char*>(buffer->data()) + offset;
99 }
100 else
101 {
102 input = static_cast<const char*>(attribute.mPointer);
103 }
104
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000105 if (instances == 0 || attribute.mDivisor == 0)
106 {
107 input += inputStride * start;
108 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000109
110 if (converter.identity && inputStride == elementSize)
111 {
112 memcpy(output, input, count * inputStride);
113 }
114 else
115 {
116 converter.convertArray(input, inputStride, count, output);
117 }
118
119 vertexBuffer->unmap();
120
121 return streamOffset;
122}
123
daniel@transgaming.com408caa52012-10-31 18:47:01 +0000124GLenum VertexDataManager::prepareVertexData(const VertexAttributeArray &attribs, ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000125{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000126 if (!mStreamingBuffer)
127 {
128 return GL_OUT_OF_MEMORY;
129 }
130
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000131 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000132 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000133 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000134 }
135
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000136 // 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 +0000137 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
138 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000139 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000140 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000141 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000142 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000143
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000144 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000145 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000146 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000147 {
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000148 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000149 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000150 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000151 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000152 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000153 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000154 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
155 for (int previous = 0; previous < i; previous++)
156 {
157 if (translated[previous].active && attribs[previous].mArrayEnabled)
158 {
159 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
160 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
161
162 if (staticBuffer == previousStaticBuffer)
163 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000164 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000165 }
166 }
167 }
168
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000169 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000170
171 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000172 }
173 }
174 else
175 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000176 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000177 }
178 }
179 }
180
181 // Reserve the required space per used buffer
182 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
183 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000184 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000185 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000186 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000187 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000188 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
189
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000190 if (vertexBuffer)
191 {
192 vertexBuffer->reserveRequiredSpace();
193 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000194 }
195 }
196
197 // Perform the vertex data translations
198 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
199 {
200 if (translated[i].active)
201 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000202 if (attribs[i].mArrayEnabled)
203 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000204 Buffer *buffer = attribs[i].mBoundBuffer.get();
205
daniel@transgaming.com83921382011-01-08 05:46:00 +0000206 if (!buffer && attribs[i].mPointer == NULL)
207 {
208 // This is an application error that would normally result in a crash, but we catch it and return an error
209 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000210 return GL_INVALID_OPERATION;
211 }
212
daniel@transgaming.com83921382011-01-08 05:46:00 +0000213 const FormatConverter &converter = formatConverter(attribs[i]);
214
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000215 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000216 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
217
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000218 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000219
220 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000221 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000222 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
223
224 if (streamOffset == -1)
225 {
226 // Convert the entire buffer
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000227 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000228 int startIndex = attribs[i].mOffset / attribs[i].stride();
229
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000230 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000231 }
232
233 if (streamOffset != -1)
234 {
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000235 streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
236
237 if (instances == 0 || attribs[i].mDivisor == 0)
238 {
239 streamOffset += start * converter.outputElementSize;
240 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000241 }
242 }
243 else
244 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000245 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000246 }
247
daniel@transgaming.com83921382011-01-08 05:46:00 +0000248 if (streamOffset == -1)
249 {
250 return GL_OUT_OF_MEMORY;
251 }
252
253 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000254 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000255 translated[i].divisor = attribs[i].mDivisor;
256
daniel@transgaming.com83921382011-01-08 05:46:00 +0000257 translated[i].type = converter.d3dDeclType;
258 translated[i].stride = converter.outputElementSize;
259 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000260 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000261 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000262 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000263 if (!mCurrentValueBuffer[i])
264 {
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000265 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000266 }
267
268 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
269
daniel@transgaming.com83921382011-01-08 05:46:00 +0000270 if (mDirtyCurrentValue[i])
271 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000272 const int requiredSpace = 4 * sizeof(float);
273 buffer->addRequiredSpace(requiredSpace);
274 buffer->reserveRequiredSpace();
275 float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
276 if (data)
277 {
278 data[0] = attribs[i].mCurrentValue[0];
279 data[1] = attribs[i].mCurrentValue[1];
280 data[2] = attribs[i].mCurrentValue[2];
281 data[3] = attribs[i].mCurrentValue[3];
282 buffer->unmap();
283 mDirtyCurrentValue[i] = false;
284 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000285 }
286
287 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000288 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000289 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000290
291 translated[i].type = D3DDECLTYPE_FLOAT4;
292 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000293 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000294 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000295 }
296 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000297
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000298 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
299 {
300 if (translated[i].active && attribs[i].mArrayEnabled)
301 {
302 Buffer *buffer = attribs[i].mBoundBuffer.get();
303
304 if (buffer)
305 {
306 buffer->promoteStaticUsage(count * attribs[i].typeSize());
307 }
308 }
309 }
310
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000311 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000312}
313
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000314std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000315{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000316 size_t elementSize = formatConverter(attrib).outputElementSize;
317
318 if (instances == 0 || attrib.mDivisor == 0)
319 {
320 return elementSize * count;
321 }
322 else
323 {
324 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
325 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000326}
327
328// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
329//
330// BYTE SHORT (Cast)
331// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
332// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
333// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
334// SHORT SHORT (Identity)
335// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
336// UNSIGNED_SHORT FLOAT (Cast)
337// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
338// FIXED (not in WebGL) FLOAT (FixedToFloat)
339// FLOAT FLOAT (Identity)
340
341// GLToCType maps from GL type (as GLenum) to the C typedef.
342template <GLenum GLType> struct GLToCType { };
343
344template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
345template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
346template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
347template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
348template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
349template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
350
351// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
352enum D3DVertexType
353{
354 D3DVT_FLOAT,
355 D3DVT_SHORT,
356 D3DVT_SHORT_NORM,
357 D3DVT_UBYTE,
358 D3DVT_UBYTE_NORM,
359 D3DVT_USHORT_NORM
360};
361
362// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
363template <unsigned int D3DType> struct D3DToCType { };
364
365template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
366template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
367template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
368template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
369template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
370template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
371
372// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
373template <unsigned int type, int size>
374struct WidenRule
375{
376};
377
378template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
379template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
380template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
381template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
382template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
383template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
384
385// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
386template <unsigned int d3dtype, int size>
387struct VertexTypeFlags
388{
389};
390
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000391template <unsigned int _capflag, unsigned int _declflag>
daniel@transgaming.com83921382011-01-08 05:46:00 +0000392struct VertexTypeFlagsHelper
393{
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000394 enum { capflag = _capflag };
395 enum { declflag = _declflag };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000396};
397
398template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
399template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
400template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
401template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
402template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
403template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
404template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
405template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
406template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
407template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
408template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
409template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
410
411
412// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
413template <GLenum GLtype, bool normalized>
414struct VertexTypeMapping
415{
416};
417
418template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
419struct VertexTypeMappingBase
420{
421 enum { preferred = Preferred };
422 enum { fallback = Fallback };
423};
424
425template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
426template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
427template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
428template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
429template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
430template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
431template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
432template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
433template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
434template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
435
436
437// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
438// The conversion rules themselves are defined in vertexconversion.h.
439
440// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
441template <GLenum fromType, bool normalized, unsigned int toType>
442struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
443{
444};
445
446// All conversions from normalized types to float use the Normalize operator.
447template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
448
449// 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 +0000450template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLint, 16> { };
451template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLint, 16> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000452
453// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
454// whether it is normalized or not.
455template <class T, bool normalized>
456struct DefaultVertexValuesStage2
457{
458};
459
460template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
461template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
462
463// Work out the default value rule for a D3D type (expressed as the C type) and
464template <class T, bool normalized>
465struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
466{
467};
468
469template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
470
471// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
472// The fallback conversion produces an output that all D3D9 devices must support.
473template <class T> struct UsePreferred { enum { type = T::preferred }; };
474template <class T> struct UseFallback { enum { type = T::fallback }; };
475
476// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
477// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
478// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
479template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
480struct Converter
481 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
482 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
483 ConversionRule<fromType,
484 normalized,
485 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
486 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
487{
488private:
489 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
490 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
491
492public:
493 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
494 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
495};
496
497// Initialise a TranslationInfo
498#define TRANSLATION(type, norm, size, preferred) \
499 { \
500 Converter<type, norm, size, preferred>::identity, \
501 Converter<type, norm, size, preferred>::finalSize, \
502 Converter<type, norm, size, preferred>::convertArray, \
503 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
504 }
505
506#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
507 { \
508 Converter<type, norm, size, UsePreferred>::capflag, \
509 TRANSLATION(type, norm, size, UsePreferred), \
510 TRANSLATION(type, norm, size, UseFallback) \
511 }
512
513#define TRANSLATIONS_FOR_TYPE(type) \
514 { \
515 { 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) }, \
516 { 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) }, \
517 }
518
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000519#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
520 { \
521 { 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) }, \
522 { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \
523 }
524
daniel@transgaming.com83921382011-01-08 05:46:00 +0000525const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
526{
527 TRANSLATIONS_FOR_TYPE(GL_BYTE),
528 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
529 TRANSLATIONS_FOR_TYPE(GL_SHORT),
530 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000531 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
532 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000533};
534
535void VertexDataManager::checkVertexCaps(DWORD declTypes)
536{
537 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
538 {
539 for (unsigned int j = 0; j < 2; j++)
540 {
541 for (unsigned int k = 0; k < 4; k++)
542 {
543 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
544 {
545 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
546 }
547 else
548 {
549 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
550 }
551 }
552 }
553 }
554}
555
556// This is used to index mAttributeTypes and mPossibleTranslations.
557unsigned int VertexDataManager::typeIndex(GLenum type) const
558{
559 switch (type)
560 {
561 case GL_BYTE: return 0;
562 case GL_UNSIGNED_BYTE: return 1;
563 case GL_SHORT: return 2;
564 case GL_UNSIGNED_SHORT: return 3;
565 case GL_FIXED: return 4;
566 case GL_FLOAT: return 5;
567
568 default: UNREACHABLE(); return 5;
569 }
570}
571
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000572VertexBuffer::VertexBuffer(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags) : mRenderer(renderer), mVertexBuffer(NULL)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000573{
574 if (size > 0)
575 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000576 // D3D9_REPLACE
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000577 HRESULT result = mRenderer->createVertexBuffer(size, usageFlags,&mVertexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000578 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000579
580 if (FAILED(result))
581 {
582 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
583 }
584 }
585}
586
587VertexBuffer::~VertexBuffer()
588{
589 if (mVertexBuffer)
590 {
591 mVertexBuffer->Release();
592 }
593}
594
595void VertexBuffer::unmap()
596{
597 if (mVertexBuffer)
598 {
599 mVertexBuffer->Unlock();
600 }
601}
602
603IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
604{
605 return mVertexBuffer;
606}
607
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000608unsigned int VertexBuffer::getSerial() const
609{
610 return mSerial;
611}
612
613unsigned int VertexBuffer::issueSerial()
614{
615 return mCurrentSerial++;
616}
617
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000618ArrayVertexBuffer::ArrayVertexBuffer(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags) : VertexBuffer(renderer, size, usageFlags)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000619{
620 mBufferSize = size;
621 mWritePosition = 0;
622 mRequiredSpace = 0;
623}
624
625ArrayVertexBuffer::~ArrayVertexBuffer()
626{
627}
628
629void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
630{
631 mRequiredSpace += requiredSpace;
632}
633
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000634StreamingVertexBuffer::StreamingVertexBuffer(rx::Renderer9 *renderer, std::size_t initialSize) : ArrayVertexBuffer(renderer, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000635{
636}
637
638StreamingVertexBuffer::~StreamingVertexBuffer()
639{
640}
641
642void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
643{
644 void *mapPtr = NULL;
645
646 if (mVertexBuffer)
647 {
648 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
649
650 if (FAILED(result))
651 {
652 ERR("Lock failed with error 0x%08x", result);
653 return NULL;
654 }
655
656 *offset = mWritePosition;
657 mWritePosition += requiredSpace;
658 }
659
660 return mapPtr;
661}
662
663void StreamingVertexBuffer::reserveRequiredSpace()
664{
665 if (mRequiredSpace > mBufferSize)
666 {
667 if (mVertexBuffer)
668 {
669 mVertexBuffer->Release();
670 mVertexBuffer = NULL;
671 }
672
673 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 +0000674
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000675 // D3D9_REPLACE
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000676 HRESULT result = mRenderer->createVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, &mVertexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000677 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000678
679 if (FAILED(result))
680 {
681 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
682 }
683
684 mWritePosition = 0;
685 }
686 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
687 {
688 if (mVertexBuffer)
689 {
690 void *dummy;
691 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
692 mVertexBuffer->Unlock();
693 }
694
695 mWritePosition = 0;
696 }
697
698 mRequiredSpace = 0;
699}
700
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000701StaticVertexBuffer::StaticVertexBuffer(rx::Renderer9 *renderer) : ArrayVertexBuffer(renderer, 0, D3DUSAGE_WRITEONLY)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000702{
703}
704
705StaticVertexBuffer::~StaticVertexBuffer()
706{
707}
708
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000709void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000710{
711 void *mapPtr = NULL;
712
713 if (mVertexBuffer)
714 {
715 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
716
717 if (FAILED(result))
718 {
719 ERR("Lock failed with error 0x%08x", result);
720 return NULL;
721 }
722
723 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000724 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000725 mCache.push_back(element);
726
727 *streamOffset = mWritePosition;
728 mWritePosition += requiredSpace;
729 }
730
731 return mapPtr;
732}
733
734void StaticVertexBuffer::reserveRequiredSpace()
735{
736 if (!mVertexBuffer && mBufferSize == 0)
737 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000738 // D3D9_REPLACE
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000739 HRESULT result = mRenderer->createVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, &mVertexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000740 mSerial = issueSerial();
741
daniel@transgaming.com83921382011-01-08 05:46:00 +0000742 if (FAILED(result))
743 {
744 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
745 }
746
747 mBufferSize = mRequiredSpace;
748 }
749 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
750 {
751 // Already allocated
752 }
753 else UNREACHABLE(); // Static vertex buffers can't be resized
754
755 mRequiredSpace = 0;
756}
757
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000758std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000759{
760 for (unsigned int element = 0; element < mCache.size(); element++)
761 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000762 if (mCache[element].type == attribute.mType &&
763 mCache[element].size == attribute.mSize &&
764 mCache[element].stride == attribute.stride() &&
765 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000766 {
767 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
768 {
769 return mCache[element].streamOffset;
770 }
771 }
772 }
773
774 return -1;
775}
776
777const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
778{
779 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
780}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000781}