Hash :
16f99b74
Author :
Date :
2015-07-02T14:09:06
Fix warnings with code analysis tools. Variable shadowing and a few other non-bug warnings. BUG=None Change-Id: Ibf62d6e7fef91fc213a53a6902f494147ed30ebc Reviewed-on: https://chromium-review.googlesource.com/283223 Tested-by: 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
//
// Copyright (c) 2002-2012 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.
//
// VertexDataManager.h: Defines the VertexDataManager, a class that
// runs the Buffer translation process.
#include "libANGLE/renderer/d3d/VertexDataManager.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Program.h"
#include "libANGLE/State.h"
#include "libANGLE/VertexAttribute.h"
#include "libANGLE/VertexArray.h"
#include "libANGLE/renderer/d3d/BufferD3D.h"
#include "libANGLE/renderer/d3d/VertexBuffer.h"
namespace
{
enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 };
// This has to be at least 4k or else it fails on ATI cards.
enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 };
}
namespace rx
{
static int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size)
{
// Size cannot be larger than a GLsizei
if (size > static_cast<unsigned int>(std::numeric_limits<int>::max()))
{
size = static_cast<unsigned int>(std::numeric_limits<int>::max());
}
GLsizei stride = ComputeVertexAttributeStride(attrib);
return (size - attrib.offset % stride + (stride - ComputeVertexAttributeTypeSize(attrib))) / stride;
}
static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount)
{
// For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices.
//
// A vertex attribute with a positive divisor loads one instanced vertex for every set of
// non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances.
if (instanceDrawCount > 0 && attrib.divisor > 0)
{
// When instanceDrawCount is not a multiple attrib.divisor, the division must round up.
// For instance, with 5 non-instanced vertices and a divisor equal to 3, we need 2 instanced vertices.
return (instanceDrawCount + attrib.divisor - 1) / attrib.divisor;
}
return vertexDrawCount;
}
VertexDataManager::CurrentValueState::CurrentValueState()
: buffer(nullptr),
offset(0)
{
data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN();
data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN();
data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN();
data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN();
data.Type = GL_FLOAT;
}
VertexDataManager::CurrentValueState::~CurrentValueState()
{
SafeDelete(buffer);
}
VertexDataManager::VertexDataManager(BufferFactoryD3D *factory)
: mFactory(factory),
mStreamingBuffer(nullptr),
// TODO(jmadill): use context caps
mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS)
{
mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE);
if (!mStreamingBuffer)
{
ERR("Failed to allocate the streaming vertex buffer.");
}
// TODO(jmadill): use context caps
mActiveEnabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
mActiveDisabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
}
VertexDataManager::~VertexDataManager()
{
SafeDelete(mStreamingBuffer);
}
void VertexDataManager::hintUnmapAllResources(const std::vector<gl::VertexAttribute> &vertexAttributes)
{
mStreamingBuffer->getVertexBuffer()->hintUnmapResource();
for (const TranslatedAttribute *translated : mActiveEnabledAttributes)
{
gl::Buffer *buffer = translated->attribute->buffer.get();
BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr;
StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : nullptr;
if (staticBuffer)
{
staticBuffer->getVertexBuffer()->hintUnmapResource();
}
}
for (auto ¤tValue : mCurrentValueCache)
{
if (currentValue.buffer != nullptr)
{
currentValue.buffer->getVertexBuffer()->hintUnmapResource();
}
}
}
gl::Error VertexDataManager::prepareVertexData(const gl::State &state,
GLint start,
GLsizei count,
std::vector<TranslatedAttribute> *translatedAttribs,
GLsizei instances)
{
if (!mStreamingBuffer)
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL.");
}
// Compute active enabled and active disable attributes, for speed.
// TODO(jmadill): don't recompute if there was no state change
const gl::VertexArray *vertexArray = state.getVertexArray();
const int *semanticIndexes = state.getProgram()->getSemanticIndexes();
const std::vector<gl::VertexAttribute> &vertexAttributes = vertexArray->getVertexAttributes();
mActiveEnabledAttributes.clear();
mActiveDisabledAttributes.clear();
translatedAttribs->clear();
for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex)
{
if (semanticIndexes[attribIndex] != -1)
{
// Resize automatically puts in empty attribs
translatedAttribs->resize(attribIndex + 1);
TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex];
// Record the attribute now
translated->active = true;
translated->attribute = &vertexAttributes[attribIndex];
translated->currentValueType = state.getVertexAttribCurrentValue(attribIndex).Type;
translated->divisor = vertexAttributes[attribIndex].divisor;
if (vertexAttributes[attribIndex].enabled)
{
mActiveEnabledAttributes.push_back(translated);
// Also invalidate static buffers that don't contain matching attributes
invalidateMatchingStaticData(vertexAttributes[attribIndex],
state.getVertexAttribCurrentValue(attribIndex));
}
else
{
mActiveDisabledAttributes.push_back(attribIndex);
}
}
}
// Reserve the required space in the buffers
for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes)
{
gl::Error error = reserveSpaceForAttrib(*activeAttrib, count, instances);
if (error.isError())
{
return error;
}
}
// Perform the vertex data translations
for (TranslatedAttribute *activeAttrib : mActiveEnabledAttributes)
{
gl::Error error = storeAttribute(activeAttrib, start, count, instances);
if (error.isError())
{
hintUnmapAllResources(vertexAttributes);
return error;
}
}
for (size_t attribIndex : mActiveDisabledAttributes)
{
if (mCurrentValueCache[attribIndex].buffer == nullptr)
{
mCurrentValueCache[attribIndex].buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE);
}
gl::Error error = storeCurrentValue(state.getVertexAttribCurrentValue(attribIndex),
&(*translatedAttribs)[attribIndex],
&mCurrentValueCache[attribIndex]);
if (error.isError())
{
hintUnmapAllResources(vertexAttributes);
return error;
}
}
// Hint to unmap all the resources
hintUnmapAllResources(vertexAttributes);
for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes)
{
gl::Buffer *buffer = activeAttrib->attribute->buffer.get();
if (buffer)
{
BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer);
size_t typeSize = ComputeVertexAttributeTypeSize(*activeAttrib->attribute);
bufferD3D->promoteStaticUsage(count * typeSize);
}
}
return gl::Error(GL_NO_ERROR);
}
void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib,
const gl::VertexAttribCurrentValueData ¤tValue) const
{
gl::Buffer *buffer = attrib.buffer.get();
if (buffer)
{
BufferD3D *bufferImpl = GetImplAs<BufferD3D>(buffer);
StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer();
if (staticBuffer &&
staticBuffer->getBufferSize() > 0 &&
!staticBuffer->lookupAttribute(attrib, NULL) &&
!staticBuffer->directStoragePossible(attrib, currentValue.Type))
{
bufferImpl->invalidateStaticData();
}
}
}
gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
GLsizei count,
GLsizei instances) const
{
const gl::VertexAttribute &attrib = *translatedAttrib.attribute;
gl::Buffer *buffer = attrib.buffer.get();
BufferD3D *bufferImpl = buffer ? GetImplAs<BufferD3D>(buffer) : NULL;
StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL;
VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
if (!vertexBuffer->directStoragePossible(attrib, translatedAttrib.currentValueType))
{
if (staticBuffer)
{
if (staticBuffer->getBufferSize() == 0)
{
int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize());
gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0);
if (error.isError())
{
return error;
}
}
}
else
{
int totalCount = StreamingBufferElementCount(attrib, count, instances);
ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount);
gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances);
if (error.isError())
{
return error;
}
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error VertexDataManager::storeAttribute(TranslatedAttribute *translated,
GLint start,
GLsizei count,
GLsizei instances)
{
const gl::VertexAttribute &attrib = *translated->attribute;
gl::Buffer *buffer = attrib.buffer.get();
ASSERT(buffer || attrib.pointer);
ASSERT(attrib.enabled);
BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : NULL;
StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL;
VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer);
bool directStorage = vertexBuffer->directStoragePossible(attrib, translated->currentValueType);
// Instanced vertices do not apply the 'start' offset
GLint firstVertexIndex = (instances > 0 && attrib.divisor > 0 ? 0 : start);
translated->vertexBuffer = vertexBuffer->getVertexBuffer();
if (directStorage)
{
translated->storage = storage;
translated->serial = storage->getSerial();
translated->stride = ComputeVertexAttributeStride(attrib);
translated->offset = static_cast<unsigned int>(attrib.offset + translated->stride * firstVertexIndex);
return gl::Error(GL_NO_ERROR);
}
// Compute source data pointer
const uint8_t *sourceData = nullptr;
if (buffer)
{
gl::Error error = storage->getData(&sourceData);
if (error.isError())
{
return error;
}
sourceData += static_cast<int>(attrib.offset);
}
else
{
sourceData = static_cast<const uint8_t*>(attrib.pointer);
}
unsigned int streamOffset = 0;
unsigned int outputElementSize = 0;
if (staticBuffer)
{
gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
if (error.isError())
{
return error;
}
if (!staticBuffer->lookupAttribute(attrib, &streamOffset))
{
// Convert the entire buffer
int totalCount = ElementsInBuffer(attrib, storage->getSize());
int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib);
error = staticBuffer->storeVertexAttributes(attrib,
translated->currentValueType,
-startIndex,
totalCount,
0,
&streamOffset,
sourceData);
if (error.isError())
{
return error;
}
}
unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize;
unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? firstVertexIndex * outputElementSize : 0;
if (streamOffset + firstElementOffset + startOffset < streamOffset)
{
return gl::Error(GL_OUT_OF_MEMORY);
}
streamOffset += firstElementOffset + startOffset;
}
else
{
int totalCount = StreamingBufferElementCount(attrib, count, instances);
gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize);
if (error.isError())
{
return error;
}
error = mStreamingBuffer->storeVertexAttributes(attrib,
translated->currentValueType,
firstVertexIndex,
totalCount,
instances,
&streamOffset,
sourceData);
if (error.isError())
{
return error;
}
}
translated->storage = nullptr;
translated->serial = vertexBuffer->getSerial();
translated->stride = outputElementSize;
translated->offset = streamOffset;
return gl::Error(GL_NO_ERROR);
}
gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData ¤tValue,
TranslatedAttribute *translated,
CurrentValueState *cachedState)
{
if (cachedState->data != currentValue)
{
const gl::VertexAttribute &attrib = *translated->attribute;
gl::Error error = cachedState->buffer->reserveVertexSpace(attrib, 1, 0);
if (error.isError())
{
return error;
}
const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues);
unsigned int streamOffset;
error = cachedState->buffer->storeVertexAttributes(attrib, currentValue.Type, 0, 1, 0, &streamOffset, sourceData);
if (error.isError())
{
return error;
}
cachedState->data = currentValue;
cachedState->offset = streamOffset;
}
translated->storage = NULL;
translated->vertexBuffer = cachedState->buffer->getVertexBuffer();
translated->serial = cachedState->buffer->getSerial();
translated->divisor = 0;
translated->stride = 0;
translated->offset = cachedState->offset;
return gl::Error(GL_NO_ERROR);
}
}