blob: bf1c0185405d26ded9d81ed413143f121feca0d3 [file] [log] [blame]
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// VertexDataManager.h: Defines the VertexDataManager, a class that
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00008// runs the Buffer translation process.
9
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000010#include "libGLESv2/VertexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000011
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000012#include "common/debug.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000013
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014#include "libGLESv2/Buffer.h"
15#include "libGLESv2/Program.h"
daniel@transgaming.com37b141e2011-01-08 05:46:13 +000016#include "libGLESv2/main.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000017
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000018#include "libGLESv2/vertexconversion.h"
19#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000020
21namespace
22{
23 enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000024 // This has to be at least 4k or else it fails on ATI cards.
25 enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000026}
27
28namespace gl
29{
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000030unsigned int VertexBuffer::mCurrentSerial = 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000031
daniel@transgaming.combaa74512011-04-13 14:56:47 +000032VertexDataManager::VertexDataManager(Context *context, IDirect3DDevice9 *device) : mContext(context), mDevice(device)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000033{
daniel@transgaming.com83921382011-01-08 05:46:00 +000034 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000035 {
daniel@transgaming.com83921382011-01-08 05:46:00 +000036 mDirtyCurrentValue[i] = true;
37 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000038 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000040
41 const D3DCAPS9 &caps = context->getDeviceCaps();
42 checkVertexCaps(caps.DeclTypes);
43
44 mStreamingBuffer = new StreamingVertexBuffer(mDevice, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000045
46 if (!mStreamingBuffer)
47 {
48 ERR("Failed to allocate the streaming vertex buffer.");
49 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000050}
51
52VertexDataManager::~VertexDataManager()
53{
daniel@transgaming.com83921382011-01-08 05:46:00 +000054 delete mStreamingBuffer;
55
56 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
57 {
58 delete mCurrentValueBuffer[i];
59 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000060}
61
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000062std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000063{
daniel@transgaming.com83921382011-01-08 05:46:00 +000064 Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000065
daniel@transgaming.com83921382011-01-08 05:46:00 +000066 int inputStride = attribute.stride();
67 int elementSize = attribute.typeSize();
68 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000069 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000070
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000071 void *output = NULL;
72
73 if (vertexBuffer)
74 {
75 output = vertexBuffer->map(attribute, spaceRequired(attribute, count), &streamOffset);
76 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000077
78 if (output == NULL)
79 {
80 ERR("Failed to map vertex buffer.");
81 return -1;
82 }
83
84 const char *input = NULL;
85
86 if (buffer)
87 {
88 int offset = attribute.mOffset;
89
90 input = static_cast<const char*>(buffer->data()) + offset;
91 }
92 else
93 {
94 input = static_cast<const char*>(attribute.mPointer);
95 }
96
97 input += inputStride * start;
98
99 if (converter.identity && inputStride == elementSize)
100 {
101 memcpy(output, input, count * inputStride);
102 }
103 else
104 {
105 converter.convertArray(input, inputStride, count, output);
106 }
107
108 vertexBuffer->unmap();
109
110 return streamOffset;
111}
112
113GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
114{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000115 if (!mStreamingBuffer)
116 {
117 return GL_OUT_OF_MEMORY;
118 }
119
daniel@transgaming.com83921382011-01-08 05:46:00 +0000120 const VertexAttributeArray &attribs = mContext->getVertexAttributes();
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000121 Program *program = mContext->getCurrentProgram();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000122
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000123 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000124 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000125 translated[attributeIndex].active = (program->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000126 }
127
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000128 // 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 +0000129 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
130 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000131 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000132 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000133 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000134 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000135
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000136 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000137 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000138 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000139 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000140 int totalCount = buffer->size() / attribs[i].stride();
141 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000142 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000143 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000144 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000145 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000146 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
147 for (int previous = 0; previous < i; previous++)
148 {
149 if (translated[previous].active && attribs[previous].mArrayEnabled)
150 {
151 Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
152 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
153
154 if (staticBuffer == previousStaticBuffer)
155 {
156 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count));
157 }
158 }
159 }
160
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000161 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000162
163 buffer->invalidateStaticData();
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000164 }
165 }
166 else
167 {
168 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000169 }
170 }
171 }
172
173 // Reserve the required space per used buffer
174 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
175 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000176 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000177 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000178 Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000179 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000180 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
181
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000182 if (vertexBuffer)
183 {
184 vertexBuffer->reserveRequiredSpace();
185 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000186 }
187 }
188
189 // Perform the vertex data translations
190 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
191 {
192 if (translated[i].active)
193 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000194 if (attribs[i].mArrayEnabled)
195 {
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000196 Buffer *buffer = attribs[i].mBoundBuffer.get();
197
daniel@transgaming.com83921382011-01-08 05:46:00 +0000198 if (!buffer && attribs[i].mPointer == NULL)
199 {
200 // This is an application error that would normally result in a crash, but we catch it and return an error
201 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000202 return GL_INVALID_OPERATION;
203 }
204
daniel@transgaming.com83921382011-01-08 05:46:00 +0000205 const FormatConverter &converter = formatConverter(attribs[i]);
206
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000207 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000208 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
209
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000210 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000211
212 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000213 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000214 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
215
216 if (streamOffset == -1)
217 {
218 // Convert the entire buffer
219 int totalCount = buffer->size() / attribs[i].stride();
220 int startIndex = attribs[i].mOffset / attribs[i].stride();
221
222 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i]);
223 }
224
225 if (streamOffset != -1)
226 {
227 streamOffset += (start + attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
228 }
229 }
230 else
231 {
232 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000233 }
234
daniel@transgaming.com83921382011-01-08 05:46:00 +0000235 if (streamOffset == -1)
236 {
237 return GL_OUT_OF_MEMORY;
238 }
239
240 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000241 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000242 translated[i].type = converter.d3dDeclType;
243 translated[i].stride = converter.outputElementSize;
244 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000245 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000246 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000247 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000248 if (!mCurrentValueBuffer[i])
249 {
250 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mDevice, CONSTANT_VERTEX_BUFFER_SIZE);
251 }
252
253 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
254
daniel@transgaming.com83921382011-01-08 05:46:00 +0000255 if (mDirtyCurrentValue[i])
256 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000257 const int requiredSpace = 4 * sizeof(float);
258 buffer->addRequiredSpace(requiredSpace);
259 buffer->reserveRequiredSpace();
260 float *data = static_cast<float*>(buffer->map(VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
261 if (data)
262 {
263 data[0] = attribs[i].mCurrentValue[0];
264 data[1] = attribs[i].mCurrentValue[1];
265 data[2] = attribs[i].mCurrentValue[2];
266 data[3] = attribs[i].mCurrentValue[3];
267 buffer->unmap();
268 mDirtyCurrentValue[i] = false;
269 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000270 }
271
272 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000273 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000274
275 translated[i].type = D3DDECLTYPE_FLOAT4;
276 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000277 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000278 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000279 }
280 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000281
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000282 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
283 {
284 if (translated[i].active && attribs[i].mArrayEnabled)
285 {
286 Buffer *buffer = attribs[i].mBoundBuffer.get();
287
288 if (buffer)
289 {
290 buffer->promoteStaticUsage(count * attribs[i].typeSize());
291 }
292 }
293 }
294
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000295 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000296}
297
daniel@transgaming.com83921382011-01-08 05:46:00 +0000298std::size_t VertexDataManager::spaceRequired(const VertexAttribute &attrib, std::size_t count) const
299{
300 return formatConverter(attrib).outputElementSize * count;
301}
302
303// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
304//
305// BYTE SHORT (Cast)
306// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
307// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
308// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
309// SHORT SHORT (Identity)
310// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
311// UNSIGNED_SHORT FLOAT (Cast)
312// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
313// FIXED (not in WebGL) FLOAT (FixedToFloat)
314// FLOAT FLOAT (Identity)
315
316// GLToCType maps from GL type (as GLenum) to the C typedef.
317template <GLenum GLType> struct GLToCType { };
318
319template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
320template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
321template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
322template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
323template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
324template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
325
326// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
327enum D3DVertexType
328{
329 D3DVT_FLOAT,
330 D3DVT_SHORT,
331 D3DVT_SHORT_NORM,
332 D3DVT_UBYTE,
333 D3DVT_UBYTE_NORM,
334 D3DVT_USHORT_NORM
335};
336
337// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
338template <unsigned int D3DType> struct D3DToCType { };
339
340template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
341template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
342template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
343template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
344template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
345template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
346
347// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
348template <unsigned int type, int size>
349struct WidenRule
350{
351};
352
353template <int size> struct WidenRule<D3DVT_FLOAT, size> : gl::NoWiden<size> { };
354template <int size> struct WidenRule<D3DVT_SHORT, size> : gl::WidenToEven<size> { };
355template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : gl::WidenToEven<size> { };
356template <int size> struct WidenRule<D3DVT_UBYTE, size> : gl::WidenToFour<size> { };
357template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : gl::WidenToFour<size> { };
358template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : gl::WidenToEven<size> { };
359
360// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
361template <unsigned int d3dtype, int size>
362struct VertexTypeFlags
363{
364};
365
366template <unsigned int capflag, unsigned int declflag>
367struct VertexTypeFlagsHelper
368{
369 enum { capflag = capflag };
370 enum { declflag = declflag };
371};
372
373template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
374template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
375template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
376template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
377template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
378template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
379template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
380template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
381template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
382template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
383template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
384template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
385
386
387// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
388template <GLenum GLtype, bool normalized>
389struct VertexTypeMapping
390{
391};
392
393template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
394struct VertexTypeMappingBase
395{
396 enum { preferred = Preferred };
397 enum { fallback = Fallback };
398};
399
400template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
401template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
402template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
403template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
404template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
405template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
406template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
407template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
408template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
409template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
410
411
412// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
413// The conversion rules themselves are defined in vertexconversion.h.
414
415// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
416template <GLenum fromType, bool normalized, unsigned int toType>
417struct ConversionRule : gl::Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
418{
419};
420
421// All conversions from normalized types to float use the Normalize operator.
422template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : gl::Normalize<typename GLToCType<fromType>::type> { };
423
424// Use a full specialisation for this so that it preferentially matches ahead of the generic normalize-to-float rules.
425template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
426template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : gl::FixedToFloat<GLuint, 16> { };
427
428// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
429// whether it is normalized or not.
430template <class T, bool normalized>
431struct DefaultVertexValuesStage2
432{
433};
434
435template <class T> struct DefaultVertexValuesStage2<T, true> : gl::NormalizedDefaultValues<T> { };
436template <class T> struct DefaultVertexValuesStage2<T, false> : gl::SimpleDefaultValues<T> { };
437
438// Work out the default value rule for a D3D type (expressed as the C type) and
439template <class T, bool normalized>
440struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
441{
442};
443
444template <bool normalized> struct DefaultVertexValues<float, normalized> : gl::SimpleDefaultValues<float> { };
445
446// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
447// The fallback conversion produces an output that all D3D9 devices must support.
448template <class T> struct UsePreferred { enum { type = T::preferred }; };
449template <class T> struct UseFallback { enum { type = T::fallback }; };
450
451// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
452// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
453// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
454template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
455struct Converter
456 : gl::VertexDataConverter<typename GLToCType<fromType>::type,
457 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
458 ConversionRule<fromType,
459 normalized,
460 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
461 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
462{
463private:
464 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
465 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
466
467public:
468 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
469 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
470};
471
472// Initialise a TranslationInfo
473#define TRANSLATION(type, norm, size, preferred) \
474 { \
475 Converter<type, norm, size, preferred>::identity, \
476 Converter<type, norm, size, preferred>::finalSize, \
477 Converter<type, norm, size, preferred>::convertArray, \
478 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
479 }
480
481#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
482 { \
483 Converter<type, norm, size, UsePreferred>::capflag, \
484 TRANSLATION(type, norm, size, UsePreferred), \
485 TRANSLATION(type, norm, size, UseFallback) \
486 }
487
488#define TRANSLATIONS_FOR_TYPE(type) \
489 { \
490 { 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) }, \
491 { 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) }, \
492 }
493
494const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
495{
496 TRANSLATIONS_FOR_TYPE(GL_BYTE),
497 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
498 TRANSLATIONS_FOR_TYPE(GL_SHORT),
499 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
500 TRANSLATIONS_FOR_TYPE(GL_FIXED),
501 TRANSLATIONS_FOR_TYPE(GL_FLOAT)
502};
503
504void VertexDataManager::checkVertexCaps(DWORD declTypes)
505{
506 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
507 {
508 for (unsigned int j = 0; j < 2; j++)
509 {
510 for (unsigned int k = 0; k < 4; k++)
511 {
512 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
513 {
514 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
515 }
516 else
517 {
518 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
519 }
520 }
521 }
522 }
523}
524
525// This is used to index mAttributeTypes and mPossibleTranslations.
526unsigned int VertexDataManager::typeIndex(GLenum type) const
527{
528 switch (type)
529 {
530 case GL_BYTE: return 0;
531 case GL_UNSIGNED_BYTE: return 1;
532 case GL_SHORT: return 2;
533 case GL_UNSIGNED_SHORT: return 3;
534 case GL_FIXED: return 4;
535 case GL_FLOAT: return 5;
536
537 default: UNREACHABLE(); return 5;
538 }
539}
540
daniel@transgaming.com83921382011-01-08 05:46:00 +0000541VertexBuffer::VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : mDevice(device), mVertexBuffer(NULL)
542{
543 if (size > 0)
544 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000545 D3DPOOL pool = getDisplay()->getBufferPool(usageFlags);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000546 HRESULT result = device->CreateVertexBuffer(size, usageFlags, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000547 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000548
549 if (FAILED(result))
550 {
551 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
552 }
553 }
554}
555
556VertexBuffer::~VertexBuffer()
557{
558 if (mVertexBuffer)
559 {
560 mVertexBuffer->Release();
561 }
562}
563
564void VertexBuffer::unmap()
565{
566 if (mVertexBuffer)
567 {
568 mVertexBuffer->Unlock();
569 }
570}
571
572IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
573{
574 return mVertexBuffer;
575}
576
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000577unsigned int VertexBuffer::getSerial() const
578{
579 return mSerial;
580}
581
582unsigned int VertexBuffer::issueSerial()
583{
584 return mCurrentSerial++;
585}
586
daniel@transgaming.com83921382011-01-08 05:46:00 +0000587ArrayVertexBuffer::ArrayVertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags) : VertexBuffer(device, size, usageFlags)
588{
589 mBufferSize = size;
590 mWritePosition = 0;
591 mRequiredSpace = 0;
592}
593
594ArrayVertexBuffer::~ArrayVertexBuffer()
595{
596}
597
598void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
599{
600 mRequiredSpace += requiredSpace;
601}
602
daniel@transgaming.com83921382011-01-08 05:46:00 +0000603StreamingVertexBuffer::StreamingVertexBuffer(IDirect3DDevice9 *device, std::size_t initialSize) : ArrayVertexBuffer(device, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
604{
605}
606
607StreamingVertexBuffer::~StreamingVertexBuffer()
608{
609}
610
611void *StreamingVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
612{
613 void *mapPtr = NULL;
614
615 if (mVertexBuffer)
616 {
617 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
618
619 if (FAILED(result))
620 {
621 ERR("Lock failed with error 0x%08x", result);
622 return NULL;
623 }
624
625 *offset = mWritePosition;
626 mWritePosition += requiredSpace;
627 }
628
629 return mapPtr;
630}
631
632void StreamingVertexBuffer::reserveRequiredSpace()
633{
634 if (mRequiredSpace > mBufferSize)
635 {
636 if (mVertexBuffer)
637 {
638 mVertexBuffer->Release();
639 mVertexBuffer = NULL;
640 }
641
642 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 +0000643
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000644 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000645 HRESULT result = mDevice->CreateVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000646 mSerial = issueSerial();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000647
648 if (FAILED(result))
649 {
650 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
651 }
652
653 mWritePosition = 0;
654 }
655 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
656 {
657 if (mVertexBuffer)
658 {
659 void *dummy;
660 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
661 mVertexBuffer->Unlock();
662 }
663
664 mWritePosition = 0;
665 }
666
667 mRequiredSpace = 0;
668}
669
670StaticVertexBuffer::StaticVertexBuffer(IDirect3DDevice9 *device) : ArrayVertexBuffer(device, 0, D3DUSAGE_WRITEONLY)
671{
672}
673
674StaticVertexBuffer::~StaticVertexBuffer()
675{
676}
677
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000678void *StaticVertexBuffer::map(const VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000679{
680 void *mapPtr = NULL;
681
682 if (mVertexBuffer)
683 {
684 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
685
686 if (FAILED(result))
687 {
688 ERR("Lock failed with error 0x%08x", result);
689 return NULL;
690 }
691
692 int attributeOffset = attribute.mOffset % attribute.stride();
693 VertexElement element = {attribute.mType, attribute.mSize, attribute.mNormalized, attributeOffset, mWritePosition};
694 mCache.push_back(element);
695
696 *streamOffset = mWritePosition;
697 mWritePosition += requiredSpace;
698 }
699
700 return mapPtr;
701}
702
703void StaticVertexBuffer::reserveRequiredSpace()
704{
705 if (!mVertexBuffer && mBufferSize == 0)
706 {
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000707 D3DPOOL pool = getDisplay()->getBufferPool(D3DUSAGE_WRITEONLY);
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000708 HRESULT result = mDevice->CreateVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, 0, pool, &mVertexBuffer, NULL);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000709 mSerial = issueSerial();
710
daniel@transgaming.com83921382011-01-08 05:46:00 +0000711 if (FAILED(result))
712 {
713 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
714 }
715
716 mBufferSize = mRequiredSpace;
717 }
718 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
719 {
720 // Already allocated
721 }
722 else UNREACHABLE(); // Static vertex buffers can't be resized
723
724 mRequiredSpace = 0;
725}
726
daniel@transgaming.com0608ad12011-07-29 16:32:17 +0000727std::size_t StaticVertexBuffer::lookupAttribute(const VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000728{
729 for (unsigned int element = 0; element < mCache.size(); element++)
730 {
731 if (mCache[element].type == attribute.mType && mCache[element].size == attribute.mSize && mCache[element].normalized == attribute.mNormalized)
732 {
733 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
734 {
735 return mCache[element].streamOffset;
736 }
737 }
738 }
739
740 return -1;
741}
742
743const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const VertexAttribute &attribute) const
744{
745 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
746}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000747}