blob: bb9246eab3f945db0727dbc8006658f131e2b288 [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{
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000033unsigned int VertexBuffer::mCurrentSerial = 1;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000034
daniel@transgaming.com31240482012-11-28 21:06:41 +000035int elementsInBuffer(const gl::VertexAttribute &attribute, int size)
jbauman@chromium.org059fc152011-11-18 19:26:17 +000036{
37 int stride = attribute.stride();
38 return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride;
39}
40
daniel@transgaming.com31240482012-11-28 21:06:41 +000041VertexDataManager::VertexDataManager(Renderer9 *renderer) : mRenderer(renderer)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000042{
daniel@transgaming.com31240482012-11-28 21:06:41 +000043 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000044 {
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000045 mCurrentValue[i][0] = std::numeric_limits<float>::quiet_NaN();
46 mCurrentValue[i][1] = std::numeric_limits<float>::quiet_NaN();
47 mCurrentValue[i][2] = std::numeric_limits<float>::quiet_NaN();
48 mCurrentValue[i][3] = std::numeric_limits<float>::quiet_NaN();
daniel@transgaming.com83921382011-01-08 05:46:00 +000049 mCurrentValueBuffer[i] = NULL;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +000050 mCurrentValueOffsets[i] = 0;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000051 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000052
daniel@transgaming.com621ce052012-10-31 17:52:29 +000053 // D3D9_REPLACE
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +000054 checkVertexCaps(renderer->getCapsDeclTypes());
daniel@transgaming.com83921382011-01-08 05:46:00 +000055
daniel@transgaming.comb7386992012-10-31 18:29:58 +000056 mStreamingBuffer = new StreamingVertexBuffer(renderer, INITIAL_STREAM_BUFFER_SIZE);
daniel@transgaming.com72b9e182011-04-13 14:58:33 +000057
58 if (!mStreamingBuffer)
59 {
60 ERR("Failed to allocate the streaming vertex buffer.");
61 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000062}
63
64VertexDataManager::~VertexDataManager()
65{
daniel@transgaming.com83921382011-01-08 05:46:00 +000066 delete mStreamingBuffer;
67
daniel@transgaming.com31240482012-11-28 21:06:41 +000068 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +000069 {
70 delete mCurrentValueBuffer[i];
71 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000072}
73
daniel@transgaming.com31240482012-11-28 21:06:41 +000074std::size_t VertexDataManager::writeAttributeData(ArrayVertexBuffer *vertexBuffer, GLint start, GLsizei count, const gl::VertexAttribute &attribute, GLsizei instances)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000075{
daniel@transgaming.com31240482012-11-28 21:06:41 +000076 gl::Buffer *buffer = attribute.mBoundBuffer.get();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000077
daniel@transgaming.com83921382011-01-08 05:46:00 +000078 int inputStride = attribute.stride();
79 int elementSize = attribute.typeSize();
80 const FormatConverter &converter = formatConverter(attribute);
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +000081 std::size_t streamOffset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000082
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000083 void *output = NULL;
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000084
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000085 if (vertexBuffer)
86 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +000087 output = vertexBuffer->map(attribute, spaceRequired(attribute, count, instances), &streamOffset);
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +000088 }
daniel@transgaming.com83921382011-01-08 05:46:00 +000089
90 if (output == NULL)
91 {
92 ERR("Failed to map vertex buffer.");
93 return -1;
94 }
95
96 const char *input = NULL;
97
98 if (buffer)
99 {
100 int offset = attribute.mOffset;
101
102 input = static_cast<const char*>(buffer->data()) + offset;
103 }
104 else
105 {
106 input = static_cast<const char*>(attribute.mPointer);
107 }
108
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000109 if (instances == 0 || attribute.mDivisor == 0)
110 {
111 input += inputStride * start;
112 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000113
114 if (converter.identity && inputStride == elementSize)
115 {
116 memcpy(output, input, count * inputStride);
117 }
118 else
119 {
120 converter.convertArray(input, inputStride, count, output);
121 }
122
123 vertexBuffer->unmap();
124
125 return streamOffset;
126}
127
daniel@transgaming.com31240482012-11-28 21:06:41 +0000128GLenum 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 +0000129{
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000130 if (!mStreamingBuffer)
131 {
132 return GL_OUT_OF_MEMORY;
133 }
134
daniel@transgaming.com31240482012-11-28 21:06:41 +0000135 for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000136 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000137 translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000138 }
139
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000140 // 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 +0000141 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000143 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000144 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000145 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000146 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.comc828b142010-05-12 03:42:04 +0000147
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000148 if (staticBuffer)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000149 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000150 if (staticBuffer->size() == 0)
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000151 {
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000152 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000153 staticBuffer->addRequiredSpace(spaceRequired(attribs[i], totalCount, 0));
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000154 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000155 else if (staticBuffer->lookupAttribute(attribs[i]) == -1)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000156 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000157 // This static buffer doesn't have matching attributes, so fall back to using the streaming buffer
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000158 // Add the space of all previous attributes belonging to the invalidated static buffer to the streaming buffer
159 for (int previous = 0; previous < i; previous++)
160 {
161 if (translated[previous].active && attribs[previous].mArrayEnabled)
162 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000163 gl::Buffer *previousBuffer = attribs[previous].mBoundBuffer.get();
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000164 StaticVertexBuffer *previousStaticBuffer = previousBuffer ? previousBuffer->getStaticVertexBuffer() : NULL;
165
166 if (staticBuffer == previousStaticBuffer)
167 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000168 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[previous], count, instances));
daniel@transgaming.comb0eb6972011-07-08 16:23:42 +0000169 }
170 }
171 }
172
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000173 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.comcb325c82011-08-02 12:34:36 +0000174
175 buffer->invalidateStaticData();
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000176 }
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000177 }
178 else
179 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000180 mStreamingBuffer->addRequiredSpace(spaceRequired(attribs[i], count, instances));
daniel@transgaming.com83921382011-01-08 05:46:00 +0000181 }
182 }
183 }
184
185 // Reserve the required space per used buffer
daniel@transgaming.com31240482012-11-28 21:06:41 +0000186 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000187 {
daniel@transgaming.com72b9e182011-04-13 14:58:33 +0000188 if (translated[i].active && attribs[i].mArrayEnabled)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000189 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000190 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000191 ArrayVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000192 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : mStreamingBuffer;
193
daniel@transgaming.com5ee2ad02011-01-08 05:46:20 +0000194 if (vertexBuffer)
195 {
196 vertexBuffer->reserveRequiredSpace();
197 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000198 }
199 }
200
201 // Perform the vertex data translations
daniel@transgaming.com31240482012-11-28 21:06:41 +0000202 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000203 {
204 if (translated[i].active)
205 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000206 if (attribs[i].mArrayEnabled)
207 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000208 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000209
daniel@transgaming.com83921382011-01-08 05:46:00 +0000210 if (!buffer && attribs[i].mPointer == NULL)
211 {
212 // This is an application error that would normally result in a crash, but we catch it and return an error
213 ERR("An enabled vertex array has no buffer and no pointer.");
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000214 return GL_INVALID_OPERATION;
215 }
216
daniel@transgaming.com83921382011-01-08 05:46:00 +0000217 const FormatConverter &converter = formatConverter(attribs[i]);
218
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000219 StaticVertexBuffer *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000220 ArrayVertexBuffer *vertexBuffer = staticBuffer ? staticBuffer : static_cast<ArrayVertexBuffer*>(mStreamingBuffer);
221
daniel@transgaming.com58f76fe2011-06-21 14:21:07 +0000222 std::size_t streamOffset = -1;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000223
224 if (staticBuffer)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000225 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000226 streamOffset = staticBuffer->lookupAttribute(attribs[i]);
227
228 if (streamOffset == -1)
229 {
230 // Convert the entire buffer
jbauman@chromium.org059fc152011-11-18 19:26:17 +0000231 int totalCount = elementsInBuffer(attribs[i], buffer->size());
daniel@transgaming.com83921382011-01-08 05:46:00 +0000232 int startIndex = attribs[i].mOffset / attribs[i].stride();
233
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000234 streamOffset = writeAttributeData(staticBuffer, -startIndex, totalCount, attribs[i], 0);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000235 }
236
237 if (streamOffset != -1)
238 {
daniel@transgaming.comc41a6fe2012-01-27 15:39:04 +0000239 streamOffset += (attribs[i].mOffset / attribs[i].stride()) * converter.outputElementSize;
240
241 if (instances == 0 || attribs[i].mDivisor == 0)
242 {
243 streamOffset += start * converter.outputElementSize;
244 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000245 }
246 }
247 else
248 {
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000249 streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i], instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +0000250 }
251
daniel@transgaming.com83921382011-01-08 05:46:00 +0000252 if (streamOffset == -1)
253 {
254 return GL_OUT_OF_MEMORY;
255 }
256
257 translated[i].vertexBuffer = vertexBuffer->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000258 translated[i].serial = vertexBuffer->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000259 translated[i].divisor = attribs[i].mDivisor;
260
daniel@transgaming.com83921382011-01-08 05:46:00 +0000261 translated[i].type = converter.d3dDeclType;
262 translated[i].stride = converter.outputElementSize;
263 translated[i].offset = streamOffset;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000264 }
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000265 else
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000266 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000267 if (!mCurrentValueBuffer[i])
268 {
daniel@transgaming.comb7386992012-10-31 18:29:58 +0000269 mCurrentValueBuffer[i] = new StreamingVertexBuffer(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE);
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000270 }
271
272 StreamingVertexBuffer *buffer = mCurrentValueBuffer[i];
273
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000274 if (mCurrentValue[i][0] != attribs[i].mCurrentValue[0] ||
275 mCurrentValue[i][1] != attribs[i].mCurrentValue[1] ||
276 mCurrentValue[i][2] != attribs[i].mCurrentValue[2] ||
277 mCurrentValue[i][3] != attribs[i].mCurrentValue[3])
daniel@transgaming.com83921382011-01-08 05:46:00 +0000278 {
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000279 const int requiredSpace = 4 * sizeof(float);
280 buffer->addRequiredSpace(requiredSpace);
281 buffer->reserveRequiredSpace();
daniel@transgaming.com31240482012-11-28 21:06:41 +0000282 float *data = static_cast<float*>(buffer->map(gl::VertexAttribute(), requiredSpace, &mCurrentValueOffsets[i]));
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000283 if (data)
284 {
285 data[0] = attribs[i].mCurrentValue[0];
286 data[1] = attribs[i].mCurrentValue[1];
287 data[2] = attribs[i].mCurrentValue[2];
288 data[3] = attribs[i].mCurrentValue[3];
289 buffer->unmap();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000290
291 mCurrentValue[i][0] = attribs[i].mCurrentValue[0];
292 mCurrentValue[i][1] = attribs[i].mCurrentValue[1];
293 mCurrentValue[i][2] = attribs[i].mCurrentValue[2];
294 mCurrentValue[i][3] = attribs[i].mCurrentValue[3];
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000295 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000296 }
297
298 translated[i].vertexBuffer = mCurrentValueBuffer[i]->getBuffer();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000299 translated[i].serial = mCurrentValueBuffer[i]->getSerial();
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +0000300 translated[i].divisor = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000301
302 translated[i].type = D3DDECLTYPE_FLOAT4;
303 translated[i].stride = 0;
jbauman@chromium.org83b61bc2011-09-02 18:59:24 +0000304 translated[i].offset = mCurrentValueOffsets[i];
daniel@transgaming.com9a0606c2010-05-12 03:42:00 +0000305 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000306 }
307 }
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000308
daniel@transgaming.com31240482012-11-28 21:06:41 +0000309 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000310 {
311 if (translated[i].active && attribs[i].mArrayEnabled)
312 {
daniel@transgaming.com31240482012-11-28 21:06:41 +0000313 gl::Buffer *buffer = attribs[i].mBoundBuffer.get();
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000314
315 if (buffer)
316 {
317 buffer->promoteStaticUsage(count * attribs[i].typeSize());
318 }
319 }
320 }
321
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000322 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000323}
324
daniel@transgaming.com31240482012-11-28 21:06:41 +0000325std::size_t VertexDataManager::spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000326{
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +0000327 size_t elementSize = formatConverter(attrib).outputElementSize;
328
329 if (instances == 0 || attrib.mDivisor == 0)
330 {
331 return elementSize * count;
332 }
333 else
334 {
335 return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor);
336 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000337}
338
339// Mapping from OpenGL-ES vertex attrib type to D3D decl type:
340//
341// BYTE SHORT (Cast)
342// BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
343// UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
344// UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
345// SHORT SHORT (Identity)
346// SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
347// UNSIGNED_SHORT FLOAT (Cast)
348// UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
349// FIXED (not in WebGL) FLOAT (FixedToFloat)
350// FLOAT FLOAT (Identity)
351
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000352// GLToCType maps from GL type (as GLenum) to the C typedef.
daniel@transgaming.com83921382011-01-08 05:46:00 +0000353template <GLenum GLType> struct GLToCType { };
354
355template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
356template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
357template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
358template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
359template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
360template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
361
362// This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
363enum D3DVertexType
364{
365 D3DVT_FLOAT,
366 D3DVT_SHORT,
367 D3DVT_SHORT_NORM,
368 D3DVT_UBYTE,
369 D3DVT_UBYTE_NORM,
370 D3DVT_USHORT_NORM
371};
372
373// D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
374template <unsigned int D3DType> struct D3DToCType { };
375
376template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
377template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
378template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
379template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
380template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
381template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
382
383// Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
384template <unsigned int type, int size>
385struct WidenRule
386{
387};
388
daniel@transgaming.com31240482012-11-28 21:06:41 +0000389template <int size> struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size> { };
390template <int size> struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size> { };
391template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size> { };
392template <int size> struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size> { };
393template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size> { };
394template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000395
396// VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
397template <unsigned int d3dtype, int size>
398struct VertexTypeFlags
399{
400};
401
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000402template <unsigned int _capflag, unsigned int _declflag>
daniel@transgaming.com83921382011-01-08 05:46:00 +0000403struct VertexTypeFlagsHelper
404{
daniel@transgaming.com29ab9522012-08-27 16:25:37 +0000405 enum { capflag = _capflag };
406 enum { declflag = _declflag };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000407};
408
409template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
410template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
411template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
412template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
413template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
414template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
415template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
416template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
417template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
418template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
419template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
420template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
421
422
423// VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
424template <GLenum GLtype, bool normalized>
425struct VertexTypeMapping
426{
427};
428
429template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
430struct VertexTypeMappingBase
431{
432 enum { preferred = Preferred };
433 enum { fallback = Fallback };
434};
435
436template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
437template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
438template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
439template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
440template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
441template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
442template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
443template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
444template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
445template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
446
447
448// Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
449// The conversion rules themselves are defined in vertexconversion.h.
450
451// Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
452template <GLenum fromType, bool normalized, unsigned int toType>
daniel@transgaming.com31240482012-11-28 21:06:41 +0000453struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
daniel@transgaming.com83921382011-01-08 05:46:00 +0000454{
455};
456
457// All conversions from normalized types to float use the Normalize operator.
daniel@transgaming.com31240482012-11-28 21:06:41 +0000458template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000459
460// 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 +0000461template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
462template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000463
464// A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
465// whether it is normalized or not.
466template <class T, bool normalized>
467struct DefaultVertexValuesStage2
468{
469};
470
daniel@transgaming.com31240482012-11-28 21:06:41 +0000471template <class T> struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T> { };
472template <class T> struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000473
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000474// Work out the default value rule for a D3D type (expressed as the C type) and
daniel@transgaming.com83921382011-01-08 05:46:00 +0000475template <class T, bool normalized>
476struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
477{
478};
479
daniel@transgaming.com31240482012-11-28 21:06:41 +0000480template <bool normalized> struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float> { };
daniel@transgaming.com83921382011-01-08 05:46:00 +0000481
482// Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
483// The fallback conversion produces an output that all D3D9 devices must support.
484template <class T> struct UsePreferred { enum { type = T::preferred }; };
485template <class T> struct UseFallback { enum { type = T::fallback }; };
486
487// Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
488// it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
489// and the D3DDECLTYPE member needed for the vertex declaration in declflag.
490template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
491struct Converter
daniel@transgaming.com31240482012-11-28 21:06:41 +0000492 : VertexDataConverter<typename GLToCType<fromType>::type,
493 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
494 ConversionRule<fromType,
495 normalized,
496 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
497 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
daniel@transgaming.com83921382011-01-08 05:46:00 +0000498{
499private:
500 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
501 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
502
503public:
504 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
505 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
506};
507
508// Initialise a TranslationInfo
509#define TRANSLATION(type, norm, size, preferred) \
510 { \
511 Converter<type, norm, size, preferred>::identity, \
512 Converter<type, norm, size, preferred>::finalSize, \
513 Converter<type, norm, size, preferred>::convertArray, \
514 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
515 }
516
517#define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
518 { \
519 Converter<type, norm, size, UsePreferred>::capflag, \
520 TRANSLATION(type, norm, size, UsePreferred), \
521 TRANSLATION(type, norm, size, UseFallback) \
522 }
523
524#define TRANSLATIONS_FOR_TYPE(type) \
525 { \
526 { 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) }, \
527 { 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) }, \
528 }
529
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000530#define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
531 { \
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 { 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) }, \
534 }
535
daniel@transgaming.com83921382011-01-08 05:46:00 +0000536const VertexDataManager::TranslationDescription VertexDataManager::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
537{
538 TRANSLATIONS_FOR_TYPE(GL_BYTE),
539 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
540 TRANSLATIONS_FOR_TYPE(GL_SHORT),
541 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
daniel@transgaming.comcb37afd2012-02-01 18:10:40 +0000542 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
543 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000544};
545
546void VertexDataManager::checkVertexCaps(DWORD declTypes)
547{
548 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
549 {
550 for (unsigned int j = 0; j < 2; j++)
551 {
552 for (unsigned int k = 0; k < 4; k++)
553 {
554 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
555 {
556 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
557 }
558 else
559 {
560 mAttributeTypes[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
561 }
562 }
563 }
564 }
565}
566
567// This is used to index mAttributeTypes and mPossibleTranslations.
568unsigned int VertexDataManager::typeIndex(GLenum type) const
569{
570 switch (type)
571 {
572 case GL_BYTE: return 0;
573 case GL_UNSIGNED_BYTE: return 1;
574 case GL_SHORT: return 2;
575 case GL_UNSIGNED_SHORT: return 3;
576 case GL_FIXED: return 4;
577 case GL_FLOAT: return 5;
578
579 default: UNREACHABLE(); return 5;
580 }
581}
582
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000583VertexBuffer::VertexBuffer(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags) : mRenderer(renderer), mVertexBuffer(NULL)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000584{
585 if (size > 0)
586 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000587 // D3D9_REPLACE
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000588 HRESULT result = mRenderer->createVertexBuffer(size, usageFlags,&mVertexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000589 mSerial = issueSerial();
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000590
daniel@transgaming.com83921382011-01-08 05:46:00 +0000591 if (FAILED(result))
592 {
593 ERR("Out of memory allocating a vertex buffer of size %lu.", size);
594 }
595 }
596}
597
598VertexBuffer::~VertexBuffer()
599{
600 if (mVertexBuffer)
601 {
602 mVertexBuffer->Release();
603 }
604}
605
606void VertexBuffer::unmap()
607{
608 if (mVertexBuffer)
609 {
610 mVertexBuffer->Unlock();
611 }
612}
613
614IDirect3DVertexBuffer9 *VertexBuffer::getBuffer() const
615{
616 return mVertexBuffer;
617}
618
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000619unsigned int VertexBuffer::getSerial() const
620{
621 return mSerial;
622}
623
624unsigned int VertexBuffer::issueSerial()
625{
626 return mCurrentSerial++;
627}
628
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000629ArrayVertexBuffer::ArrayVertexBuffer(rx::Renderer9 *renderer, std::size_t size, DWORD usageFlags) : VertexBuffer(renderer, size, usageFlags)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000630{
631 mBufferSize = size;
632 mWritePosition = 0;
633 mRequiredSpace = 0;
634}
635
636ArrayVertexBuffer::~ArrayVertexBuffer()
637{
638}
639
640void ArrayVertexBuffer::addRequiredSpace(UINT requiredSpace)
641{
642 mRequiredSpace += requiredSpace;
643}
644
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000645StreamingVertexBuffer::StreamingVertexBuffer(rx::Renderer9 *renderer, std::size_t initialSize) : ArrayVertexBuffer(renderer, initialSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000646{
647}
648
649StreamingVertexBuffer::~StreamingVertexBuffer()
650{
651}
652
daniel@transgaming.com31240482012-11-28 21:06:41 +0000653void *StreamingVertexBuffer::map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *offset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000654{
655 void *mapPtr = NULL;
656
657 if (mVertexBuffer)
658 {
659 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, D3DLOCK_NOOVERWRITE);
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000660
daniel@transgaming.com83921382011-01-08 05:46:00 +0000661 if (FAILED(result))
662 {
663 ERR("Lock failed with error 0x%08x", result);
664 return NULL;
665 }
666
667 *offset = mWritePosition;
668 mWritePosition += requiredSpace;
669 }
670
671 return mapPtr;
672}
673
674void StreamingVertexBuffer::reserveRequiredSpace()
675{
676 if (mRequiredSpace > mBufferSize)
677 {
678 if (mVertexBuffer)
679 {
680 mVertexBuffer->Release();
681 mVertexBuffer = NULL;
682 }
683
684 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 +0000685
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000686 // D3D9_REPLACE
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000687 HRESULT result = mRenderer->createVertexBuffer(mBufferSize, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, &mVertexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000688 mSerial = issueSerial();
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000689
daniel@transgaming.com83921382011-01-08 05:46:00 +0000690 if (FAILED(result))
691 {
692 ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
693 }
694
695 mWritePosition = 0;
696 }
697 else if (mWritePosition + mRequiredSpace > mBufferSize) // Recycle
698 {
699 if (mVertexBuffer)
700 {
701 void *dummy;
702 mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
703 mVertexBuffer->Unlock();
704 }
705
706 mWritePosition = 0;
707 }
708
709 mRequiredSpace = 0;
710}
711
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000712StaticVertexBuffer::StaticVertexBuffer(rx::Renderer9 *renderer) : ArrayVertexBuffer(renderer, 0, D3DUSAGE_WRITEONLY)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000713{
714}
715
716StaticVertexBuffer::~StaticVertexBuffer()
717{
718}
719
daniel@transgaming.com31240482012-11-28 21:06:41 +0000720void *StaticVertexBuffer::map(const gl::VertexAttribute &attribute, std::size_t requiredSpace, std::size_t *streamOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000721{
722 void *mapPtr = NULL;
723
724 if (mVertexBuffer)
725 {
726 HRESULT result = mVertexBuffer->Lock(mWritePosition, requiredSpace, &mapPtr, 0);
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000727
daniel@transgaming.com83921382011-01-08 05:46:00 +0000728 if (FAILED(result))
729 {
730 ERR("Lock failed with error 0x%08x", result);
731 return NULL;
732 }
733
734 int attributeOffset = attribute.mOffset % attribute.stride();
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000735 VertexElement element = {attribute.mType, attribute.mSize, attribute.stride(), attribute.mNormalized, attributeOffset, mWritePosition};
daniel@transgaming.com83921382011-01-08 05:46:00 +0000736 mCache.push_back(element);
737
738 *streamOffset = mWritePosition;
739 mWritePosition += requiredSpace;
740 }
741
742 return mapPtr;
743}
744
745void StaticVertexBuffer::reserveRequiredSpace()
746{
747 if (!mVertexBuffer && mBufferSize == 0)
748 {
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000749 // D3D9_REPLACE
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000750 HRESULT result = mRenderer->createVertexBuffer(mRequiredSpace, D3DUSAGE_WRITEONLY, &mVertexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000751 mSerial = issueSerial();
752
daniel@transgaming.com83921382011-01-08 05:46:00 +0000753 if (FAILED(result))
754 {
755 ERR("Out of memory allocating a vertex buffer of size %lu.", mRequiredSpace);
756 }
757
758 mBufferSize = mRequiredSpace;
759 }
760 else if (mVertexBuffer && mBufferSize >= mRequiredSpace)
761 {
762 // Already allocated
763 }
764 else UNREACHABLE(); // Static vertex buffers can't be resized
765
766 mRequiredSpace = 0;
767}
768
daniel@transgaming.com31240482012-11-28 21:06:41 +0000769std::size_t StaticVertexBuffer::lookupAttribute(const gl::VertexAttribute &attribute)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000770{
771 for (unsigned int element = 0; element < mCache.size(); element++)
772 {
daniel@transgaming.comfd802542011-09-23 18:19:41 +0000773 if (mCache[element].type == attribute.mType &&
774 mCache[element].size == attribute.mSize &&
775 mCache[element].stride == attribute.stride() &&
776 mCache[element].normalized == attribute.mNormalized)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000777 {
778 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
779 {
780 return mCache[element].streamOffset;
781 }
782 }
783 }
784
785 return -1;
786}
787
daniel@transgaming.com31240482012-11-28 21:06:41 +0000788const VertexDataManager::FormatConverter &VertexDataManager::formatConverter(const gl::VertexAttribute &attribute) const
daniel@transgaming.com83921382011-01-08 05:46:00 +0000789{
790 return mAttributeTypes[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1];
791}
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000792}