blob: 74d1022210eed07ce7cfa3edc8a6f7a1b5d23f51 [file] [log] [blame]
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001//
Nicolas Capens8d62bcc2014-07-25 15:08:21 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +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// IndexDataManager.cpp: Defines the IndexDataManager, a class that
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00008// runs the Buffer translation process for index buffers.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/d3d/IndexDataManager.h"
11#include "libANGLE/renderer/d3d/BufferD3D.h"
12#include "libANGLE/renderer/d3d/IndexBuffer.h"
13#include "libANGLE/renderer/d3d/RendererD3D.h"
14#include "libANGLE/Buffer.h"
15#include "libANGLE/formatutils.h"
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000016
daniel@transgaming.com31240482012-11-28 21:06:41 +000017namespace rx
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000018{
19
Jamie Madill2b976812014-08-25 15:47:49 -040020static void ConvertIndices(GLenum sourceType, GLenum destinationType, const void *input, GLsizei count, void *output)
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000021{
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040022 if (sourceType == GL_UNSIGNED_BYTE)
daniel@transgaming.com83921382011-01-08 05:46:00 +000023 {
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040024 ASSERT(destinationType == GL_UNSIGNED_SHORT);
daniel@transgaming.com83921382011-01-08 05:46:00 +000025 const GLubyte *in = static_cast<const GLubyte*>(input);
26 GLushort *out = static_cast<GLushort*>(output);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000027
daniel@transgaming.com83921382011-01-08 05:46:00 +000028 for (GLsizei i = 0; i < count; i++)
29 {
30 out[i] = in[i];
31 }
32 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040033 else if (sourceType == GL_UNSIGNED_INT)
daniel@transgaming.com83921382011-01-08 05:46:00 +000034 {
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040035 ASSERT(destinationType == GL_UNSIGNED_INT);
daniel@transgaming.com83921382011-01-08 05:46:00 +000036 memcpy(output, input, count * sizeof(GLuint));
37 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040038 else if (sourceType == GL_UNSIGNED_SHORT)
daniel@transgaming.com83921382011-01-08 05:46:00 +000039 {
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040040 if (destinationType == GL_UNSIGNED_SHORT)
41 {
42 memcpy(output, input, count * sizeof(GLushort));
43 }
44 else if (destinationType == GL_UNSIGNED_INT)
45 {
46 const GLushort *in = static_cast<const GLushort*>(input);
47 GLuint *out = static_cast<GLuint*>(output);
48
49 for (GLsizei i = 0; i < count; i++)
50 {
51 out[i] = in[i];
52 }
53 }
54 else UNREACHABLE();
daniel@transgaming.com83921382011-01-08 05:46:00 +000055 }
56 else UNREACHABLE();
57}
58
Jamie Madill93e13fb2014-11-06 15:27:25 -050059IndexDataManager::IndexDataManager(RendererD3D *renderer)
Geoff Lang1c134e62014-09-08 15:32:18 -040060 : mRenderer(renderer),
61 mStreamingBufferShort(NULL),
62 mStreamingBufferInt(NULL)
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000063{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000064}
65
Jamie Madill2b976812014-08-25 15:47:49 -040066IndexDataManager::~IndexDataManager()
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000067{
Jamie Madill2b976812014-08-25 15:47:49 -040068 SafeDelete(mStreamingBufferShort);
69 SafeDelete(mStreamingBufferInt);
daniel@transgaming.com83921382011-01-08 05:46:00 +000070}
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000071
Geoff Langc9e69b12014-09-08 16:06:25 -040072gl::Error IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
daniel@transgaming.com83921382011-01-08 05:46:00 +000073{
Geoff Lang5d601382014-07-22 15:14:06 -040074 const gl::Type &typeInfo = gl::GetTypeInfo(type);
75
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +000076 GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
Geoff Lang5d601382014-07-22 15:14:06 -040077
Geoff Langb23fc092013-08-06 10:43:14 -040078 unsigned int offset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000079 bool alignedOffset = false;
80
Brandon Jonesd38f9262014-06-18 16:26:45 -070081 BufferD3D *storage = NULL;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +000082
daniel@transgaming.com83921382011-01-08 05:46:00 +000083 if (buffer != NULL)
84 {
Geoff Langb23fc092013-08-06 10:43:14 -040085 offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices));
86
Jamie Madill9236b412015-02-02 16:51:52 -050087 storage = GetImplAs<BufferD3D>(buffer);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +000088
Olli Etuaho11ffe1b2015-03-24 17:28:18 +020089 // We'll trust that the compiler will optimize the % below:
90 // the operands are unsigned and the divisor is a constant.
daniel@transgaming.com83921382011-01-08 05:46:00 +000091 switch (type)
92 {
93 case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break;
94 case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break;
95 case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break;
96 default: UNREACHABLE(); alignedOffset = false;
97 }
98
Jamie Madillae3000b2014-08-25 15:47:51 -040099 ASSERT(typeInfo.bytes * static_cast<unsigned int>(count) + offset <= storage->getSize());
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +0000100
Geoff Langc8d297a2014-09-19 11:09:08 -0400101 const uint8_t *bufferData = NULL;
102 gl::Error error = storage->getData(&bufferData);
103 if (error.isError())
104 {
105 return error;
106 }
107
108 indices = bufferData + offset;
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +0000109 }
110
Brandon Jonesd38f9262014-06-18 16:26:45 -0700111 StaticIndexBufferInterface *staticBuffer = storage ? storage->getStaticIndexBuffer() : NULL;
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400112 IndexBufferInterface *indexBuffer = NULL;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000113 bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() &&
114 destinationIndexType == type;
Geoff Langa36ead42013-08-02 11:54:08 -0400115 unsigned int streamOffset = 0;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000116
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000117 if (directStorage)
118 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000119 streamOffset = offset;
Geoff Langf23eb282013-07-22 10:52:19 -0400120
Jamie Madill2b976812014-08-25 15:47:49 -0400121 if (!buffer->getIndexRangeCache()->findRange(type, offset, count, NULL, NULL))
Geoff Langf23eb282013-07-22 10:52:19 -0400122 {
Jamie Madillef9d63e2014-08-04 10:48:02 -0400123 buffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, offset);
Geoff Langf23eb282013-07-22 10:52:19 -0400124 }
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000125 }
shannon.woods@transgaming.com38e87882013-02-28 23:18:32 +0000126 else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000127 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000128 indexBuffer = staticBuffer;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000129
Jamie Madill2b976812014-08-25 15:47:49 -0400130 if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, NULL, &streamOffset))
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000131 {
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200132 // Using bit-shift here is faster than using division.
133 streamOffset = (offset >> typeInfo.bytesShift) << gl::GetTypeInfo(destinationIndexType).bytesShift;
Jamie Madill39b43462014-08-18 16:39:50 -0400134 staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, streamOffset);
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000135 }
Olli Etuaho8fcd4e02015-03-24 16:19:33 +0200136 if (!buffer->getIndexRangeCache()->findRange(type, offset, count, nullptr, nullptr))
137 {
138 buffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, offset);
139 }
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000140 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400141
142 // Avoid D3D11's primitive restart index value
143 // see http://msdn.microsoft.com/en-us/library/windows/desktop/bb205124(v=vs.85).aspx
Jamie Madill39b43462014-08-18 16:39:50 -0400144 if (translated->indexRange.end == 0xFFFF && type == GL_UNSIGNED_SHORT && mRenderer->getMajorShaderModel() > 3)
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400145 {
146 destinationIndexType = GL_UNSIGNED_INT;
147 directStorage = false;
148 indexBuffer = NULL;
149 }
150
Geoff Lang5d601382014-07-22 15:14:06 -0400151 const gl::Type &destTypeInfo = gl::GetTypeInfo(destinationIndexType);
152
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400153 if (!directStorage && !indexBuffer)
154 {
Geoff Langc9e69b12014-09-08 16:06:25 -0400155 gl::Error error = getStreamingIndexBuffer(destinationIndexType, &indexBuffer);
156 if (error.isError())
Geoff Lang1c134e62014-09-08 15:32:18 -0400157 {
Geoff Langc9e69b12014-09-08 16:06:25 -0400158 return error;
Geoff Lang1c134e62014-09-08 15:32:18 -0400159 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400160
Geoff Langa36ead42013-08-02 11:54:08 -0400161 unsigned int convertCount = count;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000162
daniel@transgaming.com83921382011-01-08 05:46:00 +0000163 if (staticBuffer)
164 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000165 if (staticBuffer->getBufferSize() == 0 && alignedOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000166 {
167 indexBuffer = staticBuffer;
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200168 // Using bit-shift here is faster than using division.
169 convertCount = storage->getSize() >> typeInfo.bytesShift;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000170 }
171 else
172 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700173 storage->invalidateStaticData();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000174 staticBuffer = NULL;
175 }
176 }
177
Jamie Madillae3000b2014-08-25 15:47:51 -0400178 ASSERT(indexBuffer);
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000179
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200180 // Using bit-shift here is faster than using division.
181 if (convertCount > (std::numeric_limits<unsigned int>::max() >> destTypeInfo.bytesShift))
Geoff Langa36ead42013-08-02 11:54:08 -0400182 {
Geoff Langc9e69b12014-09-08 16:06:25 -0400183 return gl::Error(GL_OUT_OF_MEMORY, "Reserving %u indices of %u bytes each exceeds the maximum buffer size.",
184 convertCount, destTypeInfo.bytes);
Geoff Langa36ead42013-08-02 11:54:08 -0400185 }
186
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200187 unsigned int bufferSizeRequired = convertCount << destTypeInfo.bytesShift;
Geoff Langc9e69b12014-09-08 16:06:25 -0400188 error = indexBuffer->reserveBufferSpace(bufferSizeRequired, type);
189 if (error.isError())
Geoff Langa36ead42013-08-02 11:54:08 -0400190 {
Geoff Langc9e69b12014-09-08 16:06:25 -0400191 return error;
Geoff Langa36ead42013-08-02 11:54:08 -0400192 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000193
194 void* output = NULL;
Geoff Langc9e69b12014-09-08 16:06:25 -0400195 error = indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset);
196 if (error.isError())
daniel@transgaming.com83921382011-01-08 05:46:00 +0000197 {
Geoff Langc9e69b12014-09-08 16:06:25 -0400198 return error;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000199 }
200
Geoff Langc8d297a2014-09-19 11:09:08 -0400201 const uint8_t *dataPointer = reinterpret_cast<const uint8_t*>(indices);
202 if (staticBuffer)
203 {
204 error = storage->getData(&dataPointer);
205 if (error.isError())
206 {
207 return error;
208 }
209 }
210 ConvertIndices(type, destinationIndexType, dataPointer, convertCount, output);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000211
Geoff Langc9e69b12014-09-08 16:06:25 -0400212 error = indexBuffer->unmapBuffer();
213 if (error.isError())
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000214 {
Geoff Langc9e69b12014-09-08 16:06:25 -0400215 return error;
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000216 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000217
daniel@transgaming.com83921382011-01-08 05:46:00 +0000218 if (staticBuffer)
219 {
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200220 // Using bit-shift here is faster than using division.
221 streamOffset = (offset >> typeInfo.bytesShift) << destTypeInfo.bytesShift;
Jamie Madill39b43462014-08-18 16:39:50 -0400222 staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, streamOffset);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000223 }
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000224 }
225
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000226 translated->storage = directStorage ? storage : NULL;
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400227 translated->indexBuffer = indexBuffer ? indexBuffer->getIndexBuffer() : NULL;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000228 translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial();
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200229 // Using bit-shift here is faster than using division.
230 translated->startIndex = (streamOffset >> destTypeInfo.bytesShift);
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000231 translated->startOffset = streamOffset;
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400232 translated->indexType = destinationIndexType;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000233
Brandon Jonesd38f9262014-06-18 16:26:45 -0700234 if (storage)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000235 {
Olli Etuaho11ffe1b2015-03-24 17:28:18 +0200236 storage->promoteStaticUsage(count << typeInfo.bytesShift);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000237 }
238
Geoff Langc9e69b12014-09-08 16:06:25 -0400239 return gl::Error(GL_NO_ERROR);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240}
241
Geoff Langc9e69b12014-09-08 16:06:25 -0400242gl::Error IndexDataManager::getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer)
Geoff Lang1c134e62014-09-08 15:32:18 -0400243{
244 ASSERT(outBuffer);
245 if (destinationIndexType == GL_UNSIGNED_INT)
246 {
247 if (!mStreamingBufferInt)
248 {
249 mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer);
Geoff Langc9e69b12014-09-08 16:06:25 -0400250 gl::Error error = mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
251 if (error.isError())
Geoff Lang1c134e62014-09-08 15:32:18 -0400252 {
253 SafeDelete(mStreamingBufferInt);
Geoff Langc9e69b12014-09-08 16:06:25 -0400254 return error;
Geoff Lang1c134e62014-09-08 15:32:18 -0400255 }
256 }
257
258 *outBuffer = mStreamingBufferInt;
Geoff Langc9e69b12014-09-08 16:06:25 -0400259 return gl::Error(GL_NO_ERROR);
Geoff Lang1c134e62014-09-08 15:32:18 -0400260 }
261 else
262 {
263 ASSERT(destinationIndexType == GL_UNSIGNED_SHORT);
264
265 if (!mStreamingBufferShort)
266 {
267 mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer);
Geoff Langc9e69b12014-09-08 16:06:25 -0400268 gl::Error error = mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT);
269 if (error.isError())
Geoff Lang1c134e62014-09-08 15:32:18 -0400270 {
271 SafeDelete(mStreamingBufferShort);
Geoff Langc9e69b12014-09-08 16:06:25 -0400272 return error;
Geoff Lang1c134e62014-09-08 15:32:18 -0400273 }
274 }
275
276 *outBuffer = mStreamingBufferShort;
Geoff Langc9e69b12014-09-08 16:06:25 -0400277 return gl::Error(GL_NO_ERROR);
Geoff Lang1c134e62014-09-08 15:32:18 -0400278 }
279}
280
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000281}