Hash :
b48e8b07
Author :
Date :
2015-04-15T14:26:37
D3D11: Only rewrite for primitive restart when needed. We would rewrite our index data every draw call. Change the index check to see if we're writing to the same sized / typed static buffer and only rewrite the data if the user re-uploaded. Also add a performance test for the primitive restart workaround. As a future improvement we could avoid creating new D3D objects every time we reinitialize static data, since BufferSubData calls don't change the size of the buffer if the index type remains the same. BUG=476658 Change-Id: I9d2540ad8b1b34fa0142ba0bf794cf572da8c61d Reviewed-on: https://chromium-review.googlesource.com/265838 Tested-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Brandon Jones <bajones@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
//
// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// IndexDataManager.cpp: Defines the IndexDataManager, a class that
// runs the Buffer translation process for index buffers.
#include "libANGLE/renderer/d3d/IndexDataManager.h"
#include "libANGLE/renderer/d3d/BufferD3D.h"
#include "libANGLE/renderer/d3d/IndexBuffer.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/formatutils.h"
namespace rx
{
static void ConvertIndices(GLenum sourceType, GLenum destinationType, const void *input, GLsizei count, void *output)
{
if (sourceType == GL_UNSIGNED_BYTE)
{
ASSERT(destinationType == GL_UNSIGNED_SHORT);
const GLubyte *in = static_cast<const GLubyte*>(input);
GLushort *out = static_cast<GLushort*>(output);
for (GLsizei i = 0; i < count; i++)
{
out[i] = in[i];
}
}
else if (sourceType == GL_UNSIGNED_INT)
{
ASSERT(destinationType == GL_UNSIGNED_INT);
memcpy(output, input, count * sizeof(GLuint));
}
else if (sourceType == GL_UNSIGNED_SHORT)
{
if (destinationType == GL_UNSIGNED_SHORT)
{
memcpy(output, input, count * sizeof(GLushort));
}
else if (destinationType == GL_UNSIGNED_INT)
{
const GLushort *in = static_cast<const GLushort*>(input);
GLuint *out = static_cast<GLuint*>(output);
for (GLsizei i = 0; i < count; i++)
{
out[i] = in[i];
}
}
else UNREACHABLE();
}
else UNREACHABLE();
}
IndexDataManager::IndexDataManager(BufferFactoryD3D *factory, RendererClass rendererClass)
: mFactory(factory),
mRendererClass(rendererClass),
mStreamingBufferShort(nullptr),
mStreamingBufferInt(nullptr)
{
}
IndexDataManager::~IndexDataManager()
{
SafeDelete(mStreamingBufferShort);
SafeDelete(mStreamingBufferInt);
}
gl::Error IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated)
{
const gl::Type &typeInfo = gl::GetTypeInfo(type);
GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
unsigned int offset = 0;
bool alignedOffset = false;
BufferD3D *storage = NULL;
if (buffer != NULL)
{
offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices));
storage = GetImplAs<BufferD3D>(buffer);
// We'll trust that the compiler will optimize the % below:
// the operands are unsigned and the divisor is a constant.
switch (type)
{
case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break;
case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break;
case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break;
default: UNREACHABLE(); alignedOffset = false;
}
ASSERT(typeInfo.bytes * static_cast<unsigned int>(count) + offset <= storage->getSize());
const uint8_t *bufferData = NULL;
gl::Error error = storage->getData(&bufferData);
if (error.isError())
{
return error;
}
indices = bufferData + offset;
}
StaticIndexBufferInterface *staticBuffer = storage ? storage->getStaticIndexBuffer() : NULL;
IndexBufferInterface *indexBuffer = NULL;
bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() &&
destinationIndexType == type;
unsigned int streamOffset = 0;
// Avoid D3D11's primitive restart index value
// see http://msdn.microsoft.com/en-us/library/windows/desktop/bb205124(v=vs.85).aspx
bool primitiveRestartWorkaround = mRendererClass == RENDERER_D3D11 &&
translated->indexRange.end == 0xFFFF &&
type == GL_UNSIGNED_SHORT;
if (primitiveRestartWorkaround)
{
destinationIndexType = GL_UNSIGNED_INT;
directStorage = false;
}
const gl::Type &destTypeInfo = gl::GetTypeInfo(destinationIndexType);
if (directStorage)
{
streamOffset = offset;
}
else if (staticBuffer &&
staticBuffer->getBufferSize() != 0 &&
alignedOffset &&
staticBuffer->getIndexType() == destinationIndexType)
{
indexBuffer = staticBuffer;
// Using bit-shift here is faster than using division.
streamOffset = (offset >> typeInfo.bytesShift) << destTypeInfo.bytesShift;
}
if (!directStorage && !indexBuffer)
{
gl::Error error = getStreamingIndexBuffer(destinationIndexType, &indexBuffer);
if (error.isError())
{
return error;
}
unsigned int convertCount = count;
if (staticBuffer)
{
if (staticBuffer->getBufferSize() == 0 && alignedOffset)
{
indexBuffer = staticBuffer;
// Using bit-shift here is faster than using division.
convertCount = storage->getSize() >> typeInfo.bytesShift;
}
else
{
storage->invalidateStaticData();
staticBuffer = NULL;
}
}
ASSERT(indexBuffer);
// Using bit-shift here is faster than using division.
if (convertCount > (std::numeric_limits<unsigned int>::max() >> destTypeInfo.bytesShift))
{
return gl::Error(GL_OUT_OF_MEMORY, "Reserving %u indices of %u bytes each exceeds the maximum buffer size.",
convertCount, destTypeInfo.bytes);
}
unsigned int bufferSizeRequired = convertCount << destTypeInfo.bytesShift;
error = indexBuffer->reserveBufferSpace(bufferSizeRequired, destinationIndexType);
if (error.isError())
{
return error;
}
void* output = NULL;
error = indexBuffer->mapBuffer(bufferSizeRequired, &output, &streamOffset);
if (error.isError())
{
return error;
}
const uint8_t *dataPointer = reinterpret_cast<const uint8_t*>(indices);
if (staticBuffer)
{
error = storage->getData(&dataPointer);
if (error.isError())
{
return error;
}
}
ConvertIndices(type, destinationIndexType, dataPointer, convertCount, output);
error = indexBuffer->unmapBuffer();
if (error.isError())
{
return error;
}
if (staticBuffer)
{
// Using bit-shift here is faster than using division.
streamOffset = (offset >> typeInfo.bytesShift) << destTypeInfo.bytesShift;
}
}
translated->storage = directStorage ? storage : NULL;
translated->indexBuffer = indexBuffer ? indexBuffer->getIndexBuffer() : NULL;
translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial();
// Using bit-shift here is faster than using division.
translated->startIndex = (streamOffset >> destTypeInfo.bytesShift);
translated->startOffset = streamOffset;
translated->indexType = destinationIndexType;
if (storage)
{
storage->promoteStaticUsage(count << typeInfo.bytesShift);
}
return gl::Error(GL_NO_ERROR);
}
gl::Error IndexDataManager::getStreamingIndexBuffer(GLenum destinationIndexType, IndexBufferInterface **outBuffer)
{
ASSERT(outBuffer);
if (destinationIndexType == GL_UNSIGNED_INT)
{
if (!mStreamingBufferInt)
{
mStreamingBufferInt = new StreamingIndexBufferInterface(mFactory);
gl::Error error = mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
if (error.isError())
{
SafeDelete(mStreamingBufferInt);
return error;
}
}
*outBuffer = mStreamingBufferInt;
return gl::Error(GL_NO_ERROR);
}
else
{
ASSERT(destinationIndexType == GL_UNSIGNED_SHORT);
if (!mStreamingBufferShort)
{
mStreamingBufferShort = new StreamingIndexBufferInterface(mFactory);
gl::Error error = mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT);
if (error.isError())
{
SafeDelete(mStreamingBufferShort);
return error;
}
}
*outBuffer = mStreamingBufferShort;
return gl::Error(GL_NO_ERROR);
}
}
}