Hash :
d779f6a9
Author :
Date :
2018-03-23T01:44:33
D3D11: Refactor draw call functions. This allow for better code reuse for the Indirect draw calls. It also cleans up the InputLayoutCache to be only responsible for caching input layouts, and moves the logic for maintaining state into StateManager11. Bug: angleproject:2389 Change-Id: I84aae164bf1b94a394743cf58650adfdcfc2c17a Reviewed-on: https://chromium-review.googlesource.com/948796 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Geoff Lang <geofflang@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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
//
// 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 "common/utilities.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Context.h"
#include "libANGLE/VertexArray.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/d3d/BufferD3D.h"
#include "libANGLE/renderer/d3d/IndexBuffer.h"
namespace rx
{
namespace
{
template <typename InputT, typename DestT>
void ConvertIndexArray(const void *input,
GLenum sourceType,
void *output,
GLenum destinationType,
GLsizei count,
bool usePrimitiveRestartFixedIndex)
{
const InputT *in = static_cast<const InputT *>(input);
DestT *out = static_cast<DestT *>(output);
if (usePrimitiveRestartFixedIndex)
{
InputT srcRestartIndex = static_cast<InputT>(gl::GetPrimitiveRestartIndex(sourceType));
DestT destRestartIndex = static_cast<DestT>(gl::GetPrimitiveRestartIndex(destinationType));
for (GLsizei i = 0; i < count; i++)
{
out[i] = (in[i] == srcRestartIndex ? destRestartIndex : static_cast<DestT>(in[i]));
}
}
else
{
for (GLsizei i = 0; i < count; i++)
{
out[i] = static_cast<DestT>(in[i]);
}
}
}
void ConvertIndices(GLenum sourceType,
GLenum destinationType,
const void *input,
GLsizei count,
void *output,
bool usePrimitiveRestartFixedIndex)
{
if (sourceType == destinationType)
{
const gl::Type &typeInfo = gl::GetTypeInfo(destinationType);
memcpy(output, input, count * typeInfo.bytes);
return;
}
if (sourceType == GL_UNSIGNED_BYTE)
{
ASSERT(destinationType == GL_UNSIGNED_SHORT);
ConvertIndexArray<GLubyte, GLushort>(input, sourceType, output, destinationType, count,
usePrimitiveRestartFixedIndex);
}
else if (sourceType == GL_UNSIGNED_SHORT)
{
ASSERT(destinationType == GL_UNSIGNED_INT);
ConvertIndexArray<GLushort, GLuint>(input, sourceType, output, destinationType, count,
usePrimitiveRestartFixedIndex);
}
else
UNREACHABLE();
}
gl::Error StreamInIndexBuffer(IndexBufferInterface *buffer,
const void *data,
unsigned int count,
GLenum srcType,
GLenum dstType,
bool usePrimitiveRestartFixedIndex,
unsigned int *offset)
{
const gl::Type &dstTypeInfo = gl::GetTypeInfo(dstType);
if (count > (std::numeric_limits<unsigned int>::max() >> dstTypeInfo.bytesShift))
{
return gl::OutOfMemory() << "Reserving " << count << " indices of " << dstTypeInfo.bytes
<< " bytes each exceeds the maximum buffer size.";
}
unsigned int bufferSizeRequired = count << dstTypeInfo.bytesShift;
ANGLE_TRY(buffer->reserveBufferSpace(bufferSizeRequired, dstType));
void *output = nullptr;
ANGLE_TRY(buffer->mapBuffer(bufferSizeRequired, &output, offset));
ConvertIndices(srcType, dstType, data, count, output, usePrimitiveRestartFixedIndex);
ANGLE_TRY(buffer->unmapBuffer());
return gl::NoError();
}
unsigned int ElementTypeSize(GLenum elementType)
{
switch (elementType)
{
case GL_UNSIGNED_BYTE:
return sizeof(GLubyte);
case GL_UNSIGNED_SHORT:
return sizeof(GLushort);
case GL_UNSIGNED_INT:
return sizeof(GLuint);
default:
UNREACHABLE();
return 0;
}
}
} // anonymous namespace
bool IsOffsetAligned(GLenum elementType, unsigned int offset)
{
return (offset % ElementTypeSize(elementType) == 0);
}
// IndexDataManager implementation.
IndexDataManager::IndexDataManager(BufferFactoryD3D *factory)
: mFactory(factory), mStreamingBufferShort(), mStreamingBufferInt()
{
}
IndexDataManager::~IndexDataManager()
{
}
void IndexDataManager::deinitialize()
{
mStreamingBufferShort.reset();
mStreamingBufferInt.reset();
}
// This function translates a GL-style indices into DX-style indices, with their description
// returned in translated.
// GL can specify vertex data in immediate mode (pointer to CPU array of indices), which is not
// possible in DX and requires streaming (Case 1). If the GL indices are specified with a buffer
// (Case 2), in a format supported by DX (subcase a) then all is good.
// When we have a buffer with an unsupported format (subcase b) then we need to do some translation:
// we will start by falling back to streaming, and after a while will start using a static
// translated copy of the index buffer.
gl::Error IndexDataManager::prepareIndexData(const gl::Context *context,
GLenum srcType,
GLenum dstType,
GLsizei count,
gl::Buffer *glBuffer,
const void *indices,
TranslatedIndexData *translated)
{
const gl::Type &srcTypeInfo = gl::GetTypeInfo(srcType);
const gl::Type &dstTypeInfo = gl::GetTypeInfo(dstType);
BufferD3D *buffer = glBuffer ? GetImplAs<BufferD3D>(glBuffer) : nullptr;
translated->indexType = dstType;
translated->srcIndexData.srcBuffer = buffer;
translated->srcIndexData.srcIndices = indices;
translated->srcIndexData.srcIndexType = srcType;
translated->srcIndexData.srcCount = count;
// Context can be nullptr in perf tests.
bool primitiveRestartFixedIndexEnabled =
context ? context->getGLState().isPrimitiveRestartEnabled() : false;
// Case 1: the indices are passed by pointer, which forces the streaming of index data
if (glBuffer == nullptr)
{
translated->storage = nullptr;
return streamIndexData(indices, count, srcType, dstType, primitiveRestartFixedIndexEnabled,
translated);
}
// Case 2: the indices are already in a buffer
unsigned int offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices));
ASSERT(srcTypeInfo.bytes * static_cast<unsigned int>(count) + offset <= buffer->getSize());
bool offsetAligned = IsOffsetAligned(srcType, offset);
// Case 2a: the buffer can be used directly
if (offsetAligned && buffer->supportsDirectBinding() && dstType == srcType)
{
translated->storage = buffer;
translated->indexBuffer = nullptr;
translated->serial = buffer->getSerial();
translated->startIndex = (offset >> srcTypeInfo.bytesShift);
translated->startOffset = offset;
return gl::NoError();
}
translated->storage = nullptr;
// Case 2b: use a static translated copy or fall back to streaming
StaticIndexBufferInterface *staticBuffer = buffer->getStaticIndexBuffer();
bool staticBufferInitialized = staticBuffer && staticBuffer->getBufferSize() != 0;
bool staticBufferUsable =
staticBuffer && offsetAligned && staticBuffer->getIndexType() == dstType;
if (staticBufferInitialized && !staticBufferUsable)
{
buffer->invalidateStaticData(context);
staticBuffer = nullptr;
}
if (staticBuffer == nullptr || !offsetAligned)
{
const uint8_t *bufferData = nullptr;
ANGLE_TRY(buffer->getData(context, &bufferData));
ASSERT(bufferData != nullptr);
ANGLE_TRY(streamIndexData(bufferData + offset, count, srcType, dstType,
primitiveRestartFixedIndexEnabled, translated));
buffer->promoteStaticUsage(context, count << srcTypeInfo.bytesShift);
}
else
{
if (!staticBufferInitialized)
{
const uint8_t *bufferData = nullptr;
ANGLE_TRY(buffer->getData(context, &bufferData));
ASSERT(bufferData != nullptr);
unsigned int convertCount =
static_cast<unsigned int>(buffer->getSize()) >> srcTypeInfo.bytesShift;
ANGLE_TRY(StreamInIndexBuffer(staticBuffer, bufferData, convertCount, srcType, dstType,
primitiveRestartFixedIndexEnabled, nullptr));
}
ASSERT(offsetAligned && staticBuffer->getIndexType() == dstType);
translated->indexBuffer = staticBuffer->getIndexBuffer();
translated->serial = staticBuffer->getSerial();
translated->startIndex = (offset >> srcTypeInfo.bytesShift);
translated->startOffset = (offset >> srcTypeInfo.bytesShift) << dstTypeInfo.bytesShift;
}
return gl::NoError();
}
gl::Error IndexDataManager::streamIndexData(const void *data,
unsigned int count,
GLenum srcType,
GLenum dstType,
bool usePrimitiveRestartFixedIndex,
TranslatedIndexData *translated)
{
const gl::Type &dstTypeInfo = gl::GetTypeInfo(dstType);
IndexBufferInterface *indexBuffer = nullptr;
ANGLE_TRY(getStreamingIndexBuffer(dstType, &indexBuffer));
ASSERT(indexBuffer != nullptr);
unsigned int offset;
ANGLE_TRY(StreamInIndexBuffer(indexBuffer, data, count, srcType, dstType,
usePrimitiveRestartFixedIndex, &offset));
translated->indexBuffer = indexBuffer->getIndexBuffer();
translated->serial = indexBuffer->getSerial();
translated->startIndex = (offset >> dstTypeInfo.bytesShift);
translated->startOffset = offset;
return gl::NoError();
}
gl::Error IndexDataManager::getStreamingIndexBuffer(GLenum destinationIndexType,
IndexBufferInterface **outBuffer)
{
ASSERT(outBuffer);
ASSERT(destinationIndexType == GL_UNSIGNED_SHORT || destinationIndexType == GL_UNSIGNED_INT);
auto &streamingBuffer =
(destinationIndexType == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort;
if (!streamingBuffer)
{
StreamingBuffer newBuffer(new StreamingIndexBufferInterface(mFactory));
ANGLE_TRY(newBuffer->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, destinationIndexType));
streamingBuffer = std::move(newBuffer);
}
*outBuffer = streamingBuffer.get();
return gl::NoError();
}
gl::Error GetIndexTranslationDestType(const gl::Context *context,
const gl::DrawCallParams &drawCallParams,
bool usePrimitiveRestartWorkaround,
GLenum *destTypeOut)
{
// Avoid D3D11's primitive restart index value
// see http://msdn.microsoft.com/en-us/library/windows/desktop/bb205124(v=vs.85).aspx
if (usePrimitiveRestartWorkaround)
{
// Conservatively assume we need to translate the indices for draw indirect.
if (drawCallParams.isDrawIndirect())
{
*destTypeOut = GL_UNSIGNED_INT;
return gl::NoError();
}
ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context));
const gl::IndexRange &indexRange = drawCallParams.getIndexRange();
if (indexRange.end == gl::GetPrimitiveRestartIndex(drawCallParams.type()))
{
*destTypeOut = GL_UNSIGNED_INT;
return gl::NoError();
}
}
*destTypeOut = (drawCallParams.type() == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
return gl::NoError();
}
} // namespace rx