blob: cd775c19eb6d7bfd35898c21e6e108dca36bc43d [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
Brandon Jonesc7a41042014-06-23 12:03:25 -070010#include "libGLESv2/renderer/d3d/IndexDataManager.h"
Brandon Jonesd38f9262014-06-18 16:26:45 -070011#include "libGLESv2/renderer/d3d/BufferD3D.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040012#include "libGLESv2/renderer/d3d/IndexBuffer.h"
13#include "libGLESv2/renderer/Renderer.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014#include "libGLESv2/Buffer.h"
daniel@transgaming.com37b141e2011-01-08 05:46:13 +000015#include "libGLESv2/main.h"
Geoff Langf23eb282013-07-22 10:52:19 -040016#include "libGLESv2/formatutils.h"
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000017
daniel@transgaming.com31240482012-11-28 21:06:41 +000018namespace rx
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000019{
20
Jamie Madill2b976812014-08-25 15:47:49 -040021static void ConvertIndices(GLenum sourceType, GLenum destinationType, const void *input, GLsizei count, void *output)
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000022{
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040023 if (sourceType == GL_UNSIGNED_BYTE)
daniel@transgaming.com83921382011-01-08 05:46:00 +000024 {
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040025 ASSERT(destinationType == GL_UNSIGNED_SHORT);
daniel@transgaming.com83921382011-01-08 05:46:00 +000026 const GLubyte *in = static_cast<const GLubyte*>(input);
27 GLushort *out = static_cast<GLushort*>(output);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000028
daniel@transgaming.com83921382011-01-08 05:46:00 +000029 for (GLsizei i = 0; i < count; i++)
30 {
31 out[i] = in[i];
32 }
33 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040034 else if (sourceType == GL_UNSIGNED_INT)
daniel@transgaming.com83921382011-01-08 05:46:00 +000035 {
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040036 ASSERT(destinationType == GL_UNSIGNED_INT);
daniel@transgaming.com83921382011-01-08 05:46:00 +000037 memcpy(output, input, count * sizeof(GLuint));
38 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040039 else if (sourceType == GL_UNSIGNED_SHORT)
daniel@transgaming.com83921382011-01-08 05:46:00 +000040 {
Nicolas Capens8d62bcc2014-07-25 15:08:21 -040041 if (destinationType == GL_UNSIGNED_SHORT)
42 {
43 memcpy(output, input, count * sizeof(GLushort));
44 }
45 else if (destinationType == GL_UNSIGNED_INT)
46 {
47 const GLushort *in = static_cast<const GLushort*>(input);
48 GLuint *out = static_cast<GLuint*>(output);
49
50 for (GLsizei i = 0; i < count; i++)
51 {
52 out[i] = in[i];
53 }
54 }
55 else UNREACHABLE();
daniel@transgaming.com83921382011-01-08 05:46:00 +000056 }
57 else UNREACHABLE();
58}
59
Jamie Madill2b976812014-08-25 15:47:49 -040060IndexDataManager::IndexDataManager(Renderer *renderer)
Geoff Lang1c134e62014-09-08 15:32:18 -040061 : mRenderer(renderer),
62 mStreamingBufferShort(NULL),
63 mStreamingBufferInt(NULL)
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000064{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000065}
66
Jamie Madill2b976812014-08-25 15:47:49 -040067IndexDataManager::~IndexDataManager()
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000068{
Jamie Madill2b976812014-08-25 15:47:49 -040069 SafeDelete(mStreamingBufferShort);
70 SafeDelete(mStreamingBufferInt);
daniel@transgaming.com83921382011-01-08 05:46:00 +000071}
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000072
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +000073GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
daniel@transgaming.com83921382011-01-08 05:46:00 +000074{
Geoff Lang5d601382014-07-22 15:14:06 -040075 const gl::Type &typeInfo = gl::GetTypeInfo(type);
76
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +000077 GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
Geoff Lang5d601382014-07-22 15:14:06 -040078
Geoff Langb23fc092013-08-06 10:43:14 -040079 unsigned int offset = 0;
daniel@transgaming.com83921382011-01-08 05:46:00 +000080 bool alignedOffset = false;
81
Brandon Jonesd38f9262014-06-18 16:26:45 -070082 BufferD3D *storage = NULL;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +000083
daniel@transgaming.com83921382011-01-08 05:46:00 +000084 if (buffer != NULL)
85 {
Geoff Langb23fc092013-08-06 10:43:14 -040086 if (reinterpret_cast<uintptr_t>(indices) > std::numeric_limits<unsigned int>::max())
87 {
88 return GL_OUT_OF_MEMORY;
89 }
90 offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices));
91
Brandon Jonesd38f9262014-06-18 16:26:45 -070092 storage = BufferD3D::makeBufferD3D(buffer->getImplementation());
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +000093
daniel@transgaming.com83921382011-01-08 05:46:00 +000094 switch (type)
95 {
96 case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break;
97 case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break;
98 case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break;
99 default: UNREACHABLE(); alignedOffset = false;
100 }
101
Jamie Madillae3000b2014-08-25 15:47:51 -0400102 ASSERT(typeInfo.bytes * static_cast<unsigned int>(count) + offset <= storage->getSize());
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +0000103
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +0000104 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +0000105 }
106
Brandon Jonesd38f9262014-06-18 16:26:45 -0700107 StaticIndexBufferInterface *staticBuffer = storage ? storage->getStaticIndexBuffer() : NULL;
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400108 IndexBufferInterface *indexBuffer = NULL;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000109 bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() &&
110 destinationIndexType == type;
Geoff Langa36ead42013-08-02 11:54:08 -0400111 unsigned int streamOffset = 0;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000112
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000113 if (directStorage)
114 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000115 streamOffset = offset;
Geoff Langf23eb282013-07-22 10:52:19 -0400116
Jamie Madill2b976812014-08-25 15:47:49 -0400117 if (!buffer->getIndexRangeCache()->findRange(type, offset, count, NULL, NULL))
Geoff Langf23eb282013-07-22 10:52:19 -0400118 {
Jamie Madillef9d63e2014-08-04 10:48:02 -0400119 buffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, offset);
Geoff Langf23eb282013-07-22 10:52:19 -0400120 }
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000121 }
shannon.woods@transgaming.com38e87882013-02-28 23:18:32 +0000122 else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset)
apatrick@chromium.orgf99fbb72010-11-16 01:57:05 +0000123 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000124 indexBuffer = staticBuffer;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000125
Jamie Madill2b976812014-08-25 15:47:49 -0400126 if (!staticBuffer->getIndexRangeCache()->findRange(type, offset, count, NULL, &streamOffset))
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000127 {
Geoff Lang5d601382014-07-22 15:14:06 -0400128 streamOffset = (offset / typeInfo.bytes) * gl::GetTypeInfo(destinationIndexType).bytes;
Jamie Madill39b43462014-08-18 16:39:50 -0400129 staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, streamOffset);
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000130 }
131 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400132
133 // Avoid D3D11's primitive restart index value
134 // see http://msdn.microsoft.com/en-us/library/windows/desktop/bb205124(v=vs.85).aspx
Jamie Madill39b43462014-08-18 16:39:50 -0400135 if (translated->indexRange.end == 0xFFFF && type == GL_UNSIGNED_SHORT && mRenderer->getMajorShaderModel() > 3)
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400136 {
137 destinationIndexType = GL_UNSIGNED_INT;
138 directStorage = false;
139 indexBuffer = NULL;
140 }
141
Geoff Lang5d601382014-07-22 15:14:06 -0400142 const gl::Type &destTypeInfo = gl::GetTypeInfo(destinationIndexType);
143
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400144 if (!directStorage && !indexBuffer)
145 {
Geoff Lang1c134e62014-09-08 15:32:18 -0400146 GLenum err = getStreamingIndexBuffer(destinationIndexType, &indexBuffer);
147 if (err != GL_NO_ERROR)
148 {
149 return err;
150 }
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400151
Geoff Langa36ead42013-08-02 11:54:08 -0400152 unsigned int convertCount = count;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000153
daniel@transgaming.com83921382011-01-08 05:46:00 +0000154 if (staticBuffer)
155 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000156 if (staticBuffer->getBufferSize() == 0 && alignedOffset)
daniel@transgaming.com83921382011-01-08 05:46:00 +0000157 {
158 indexBuffer = staticBuffer;
Geoff Lang5d601382014-07-22 15:14:06 -0400159 convertCount = storage->getSize() / typeInfo.bytes;
daniel@transgaming.com83921382011-01-08 05:46:00 +0000160 }
161 else
162 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700163 storage->invalidateStaticData();
daniel@transgaming.com83921382011-01-08 05:46:00 +0000164 staticBuffer = NULL;
165 }
166 }
167
Jamie Madillae3000b2014-08-25 15:47:51 -0400168 ASSERT(indexBuffer);
daniel@transgaming.com50aadb02012-11-28 21:06:11 +0000169
Geoff Lang5d601382014-07-22 15:14:06 -0400170 if (convertCount > std::numeric_limits<unsigned int>::max() / destTypeInfo.bytes)
Geoff Langa36ead42013-08-02 11:54:08 -0400171 {
Geoff Lang5d601382014-07-22 15:14:06 -0400172 ERR("Reserving %u indicies of %u bytes each exceeds the maximum buffer size.", convertCount, destTypeInfo.bytes);
Geoff Langa36ead42013-08-02 11:54:08 -0400173 return GL_OUT_OF_MEMORY;
174 }
175
Geoff Lang5d601382014-07-22 15:14:06 -0400176 unsigned int bufferSizeRequired = convertCount * destTypeInfo.bytes;
Geoff Langa36ead42013-08-02 11:54:08 -0400177 if (!indexBuffer->reserveBufferSpace(bufferSizeRequired, type))
178 {
179 ERR("Failed to reserve %u bytes in an index buffer.", bufferSizeRequired);
180 return GL_OUT_OF_MEMORY;
181 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000182
183 void* output = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -0400184 if (!indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset))
daniel@transgaming.com83921382011-01-08 05:46:00 +0000185 {
186 ERR("Failed to map index buffer.");
187 return GL_OUT_OF_MEMORY;
188 }
189
Jamie Madill2b976812014-08-25 15:47:49 -0400190 ConvertIndices(type, destinationIndexType, staticBuffer ? storage->getData() : indices, convertCount, output);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +0000191
192 if (!indexBuffer->unmapBuffer())
193 {
194 ERR("Failed to unmap index buffer.");
195 return GL_OUT_OF_MEMORY;
196 }
daniel@transgaming.com83921382011-01-08 05:46:00 +0000197
daniel@transgaming.com83921382011-01-08 05:46:00 +0000198 if (staticBuffer)
199 {
Geoff Lang5d601382014-07-22 15:14:06 -0400200 streamOffset = (offset / typeInfo.bytes) * destTypeInfo.bytes;
Jamie Madill39b43462014-08-18 16:39:50 -0400201 staticBuffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, streamOffset);
daniel@transgaming.com83921382011-01-08 05:46:00 +0000202 }
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000203 }
204
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000205 translated->storage = directStorage ? storage : NULL;
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400206 translated->indexBuffer = indexBuffer ? indexBuffer->getIndexBuffer() : NULL;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000207 translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial();
Geoff Lang5d601382014-07-22 15:14:06 -0400208 translated->startIndex = streamOffset / destTypeInfo.bytes;
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +0000209 translated->startOffset = streamOffset;
Nicolas Capens8d62bcc2014-07-25 15:08:21 -0400210 translated->indexType = destinationIndexType;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000211
Brandon Jonesd38f9262014-06-18 16:26:45 -0700212 if (storage)
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000213 {
Geoff Lang5d601382014-07-22 15:14:06 -0400214 storage->promoteStaticUsage(count * typeInfo.bytes);
daniel@transgaming.com78624ca2011-04-22 04:17:57 +0000215 }
216
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +0000217 return GL_NO_ERROR;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000218}
219
Geoff Lang1c134e62014-09-08 15:32:18 -0400220GLenum IndexDataManager::getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer)
221{
222 ASSERT(outBuffer);
223 if (destinationIndexType == GL_UNSIGNED_INT)
224 {
225 if (!mStreamingBufferInt)
226 {
227 mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer);
228 if (!mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
229 {
230 SafeDelete(mStreamingBufferInt);
231 ERR("Failed to allocate the streaming GL_UNSIGNED_INT index buffer.");
232 return GL_OUT_OF_MEMORY;
233 }
234 }
235
236 *outBuffer = mStreamingBufferInt;
237 return GL_NO_ERROR;
238 }
239 else
240 {
241 ASSERT(destinationIndexType == GL_UNSIGNED_SHORT);
242
243 if (!mStreamingBufferShort)
244 {
245 mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer);
246 if (!mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
247 {
248 SafeDelete(mStreamingBufferShort);
249 ERR("Failed to allocate the streaming GL_UNSIGNED_SHORT index buffer.");
250 return GL_OUT_OF_MEMORY;
251 }
252 }
253
254 *outBuffer = mStreamingBufferShort;
255 return GL_NO_ERROR;
256 }
257}
258
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000259}