Hash :
b61e173a
Author :
Date :
2015-06-05T11:49:55
Only sync attributes used by the current program in RendererGL. Improves draw call overhead of RendererGL. DrawCallPerf_gl: Before: 136973 score After: 153317 score Improvement: 11.932% BUG=angleproject:959 Change-Id: Ib75f6fdd756648e4a07f6e970cda03abbdbcf009 Reviewed-on: https://chromium-review.googlesource.com/275409 Reviewed-by: Kenneth Russell <kbr@chromium.org> Tested-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
//
// Copyright 2015 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.
//
// VertexArrayGL.cpp: Implements the class methods for VertexArrayGL.
#include "libANGLE/renderer/gl/VertexArrayGL.h"
#include "common/debug.h"
#include "common/mathutil.h"
#include "common/utilities.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/gl/BufferGL.h"
#include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/StateManagerGL.h"
namespace rx
{
VertexArrayGL::VertexArrayGL(const gl::VertexArray::Data &data, const FunctionsGL *functions, StateManagerGL *stateManager)
: VertexArrayImpl(data),
mFunctions(functions),
mStateManager(stateManager),
mVertexArrayID(0),
mAppliedElementArrayBuffer(0),
mStreamingElementArrayBufferSize(0),
mStreamingElementArrayBuffer(0),
mStreamingArrayBufferSize(0),
mStreamingArrayBuffer(0)
{
ASSERT(mFunctions);
ASSERT(mStateManager);
mFunctions->genVertexArrays(1, &mVertexArrayID);
// Set the cached vertex attribute array size
GLint maxVertexAttribs;
mFunctions->getIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
mAppliedAttributes.resize(maxVertexAttribs);
}
VertexArrayGL::~VertexArrayGL()
{
mStateManager->deleteVertexArray(mVertexArrayID);
mVertexArrayID = 0;
mStateManager->deleteBuffer(mStreamingElementArrayBuffer);
mStreamingElementArrayBufferSize = 0;
mStreamingElementArrayBuffer = 0;
mStateManager->deleteBuffer(mStreamingArrayBuffer);
mStreamingArrayBufferSize = 0;
mStreamingArrayBuffer = 0;
for (size_t idx = 0; idx < mAppliedAttributes.size(); idx++)
{
mAppliedAttributes[idx].buffer.set(nullptr);
}
}
void VertexArrayGL::setElementArrayBuffer(const gl::Buffer *buffer)
{
// If the buffer is being unbound/deleted, reset the currently applied buffer ID
// so that even if a new buffer is generated with the same ID, it will be re-bound.
if (buffer == nullptr && mAppliedElementArrayBuffer != mStreamingElementArrayBuffer)
{
mAppliedElementArrayBuffer = 0;
}
}
void VertexArrayGL::setAttribute(size_t idx, const gl::VertexAttribute &attr)
{
}
void VertexArrayGL::setAttributeDivisor(size_t idx, GLuint divisor)
{
}
void VertexArrayGL::enableAttribute(size_t idx, bool enabledState)
{
}
gl::Error VertexArrayGL::syncDrawArraysState(const std::vector<GLuint> &activeAttribLocations, GLint first, GLsizei count) const
{
return syncDrawState(activeAttribLocations, first, count, GL_NONE, nullptr, nullptr);
}
gl::Error VertexArrayGL::syncDrawElementsState(const std::vector<GLuint> &activeAttribLocations, GLsizei count,
GLenum type, const GLvoid *indices, const GLvoid **outIndices) const
{
return syncDrawState(activeAttribLocations, 0, count, type, indices, outIndices);
}
gl::Error VertexArrayGL::syncDrawState(const std::vector<GLuint> &activeAttribLocations, GLint first, GLsizei count, GLenum type, const GLvoid *indices, const GLvoid **outIndices) const
{
mStateManager->bindVertexArray(mVertexArrayID, mAppliedElementArrayBuffer);
// Check if any attributes need to be streamed, determines if the index range needs to be computed
bool attributesNeedStreaming = doAttributesNeedStreaming(activeAttribLocations);
// Determine if an index buffer needs to be streamed and the range of vertices that need to be copied
gl::RangeUI indexRange(0, 0);
if (type != GL_NONE)
{
gl::Error error = syncIndexData(count, type, indices, attributesNeedStreaming, &indexRange, outIndices);
if (error.isError())
{
return error;
}
}
else
{
// Not an indexed call, set the range to [first, first + count)
indexRange.start = first;
indexRange.end = first + count;
}
// Sync the vertex attribute state and track what data needs to be streamed
size_t streamingDataSize = 0;
size_t maxAttributeDataSize = 0;
gl::Error error = syncAttributeState(activeAttribLocations, attributesNeedStreaming, indexRange,
&streamingDataSize, &maxAttributeDataSize);
if (error.isError())
{
return error;
}
if (streamingDataSize > 0)
{
ASSERT(attributesNeedStreaming);
gl::Error error = streamAttributes(activeAttribLocations, streamingDataSize, maxAttributeDataSize,
indexRange);
if (error.isError())
{
return error;
}
}
return gl::Error(GL_NO_ERROR);
}
bool VertexArrayGL::doAttributesNeedStreaming(const std::vector<GLuint> &activeAttribLocations) const
{
// TODO: if GLES, nothing needs to be streamed
const auto &attribs = mData.getVertexAttributes();
for (size_t activeAttrib = 0; activeAttrib < activeAttribLocations.size(); activeAttrib++)
{
GLuint idx = activeAttribLocations[activeAttrib];
if (attribs[idx].enabled && attribs[idx].buffer.get() == nullptr)
{
return true;
}
}
return false;
}
gl::Error VertexArrayGL::syncAttributeState(const std::vector<GLuint> &activeAttribLocations, bool attributesNeedStreaming,
const gl::RangeUI &indexRange, size_t *outStreamingDataSize, size_t *outMaxAttributeDataSize) const
{
*outStreamingDataSize = 0;
*outMaxAttributeDataSize = 0;
const auto &attribs = mData.getVertexAttributes();
for (size_t activeAttrib = 0; activeAttrib < activeAttribLocations.size(); activeAttrib++)
{
GLuint idx = activeAttribLocations[activeAttrib];
const auto &attrib = attribs[idx];
// Always sync the enabled and divisor state, they are required for both streaming and buffered
// attributes
if (mAppliedAttributes[idx].enabled != attrib.enabled)
{
if (attrib.enabled)
{
mFunctions->enableVertexAttribArray(idx);
}
else
{
mFunctions->disableVertexAttribArray(idx);
}
mAppliedAttributes[idx].enabled = attrib.enabled;
}
if (mAppliedAttributes[idx].divisor != attrib.divisor)
{
mFunctions->vertexAttribDivisor(idx, attrib.divisor);
mAppliedAttributes[idx].divisor = attrib.divisor;
}
if (attribs[idx].enabled && attrib.buffer.get() == nullptr)
{
ASSERT(attributesNeedStreaming);
const size_t streamedVertexCount = indexRange.end - indexRange.start + 1;
// If streaming is going to be required, compute the size of the required buffer
// and how much slack space at the beginning of the buffer will be required by determining
// the attribute with the largest data size.
size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
*outStreamingDataSize += typeSize * streamedVertexCount;
*outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
}
else
{
// Sync the attribute with no translation
if (mAppliedAttributes[idx] != attrib)
{
const gl::Buffer *arrayBuffer = attrib.buffer.get();
if (arrayBuffer != nullptr)
{
const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID());
}
else
{
mStateManager->bindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attrib.pureInteger)
{
mFunctions->vertexAttribIPointer(idx, attrib.size, attrib.type,
attrib.stride, attrib.pointer);
}
else
{
mFunctions->vertexAttribPointer(idx, attrib.size, attrib.type,
attrib.normalized, attrib.stride,
attrib.pointer);
}
mAppliedAttributes[idx] = attrib;
}
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error VertexArrayGL::syncIndexData(GLsizei count, GLenum type, const GLvoid *indices, bool attributesNeedStreaming,
gl::RangeUI *outIndexRange, const GLvoid **outIndices) const
{
ASSERT(outIndices);
gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
// Need to check the range of indices if attributes need to be streamed
if (elementArrayBuffer != nullptr)
{
const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
GLuint elementArrayBufferID = bufferGL->getBufferID();
if (elementArrayBufferID != mAppliedElementArrayBuffer)
{
mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBufferID);
mAppliedElementArrayBuffer = elementArrayBufferID;
}
// Only compute the index range if the attributes also need to be streamed
if (attributesNeedStreaming)
{
ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
gl::Error error = mData.getElementArrayBuffer()->getIndexRange(type, static_cast<size_t>(elementArrayBufferOffset), count, outIndexRange);
if (error.isError())
{
return error;
}
}
// Indices serves as an offset into the index buffer in this case, use the same value for the draw call
*outIndices = indices;
}
else
{
// Need to stream the index buffer
// TODO: if GLES, nothing needs to be streamed
// Only compute the index range if the attributes also need to be streamed
if (attributesNeedStreaming)
{
*outIndexRange = gl::ComputeIndexRange(type, indices, count);
}
// Allocate the streaming element array buffer
if (mStreamingElementArrayBuffer == 0)
{
mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
mStreamingElementArrayBufferSize = 0;
}
mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer);
mAppliedElementArrayBuffer = mStreamingElementArrayBuffer;
// Make sure the element array buffer is large enough
const gl::Type &indexTypeInfo = gl::GetTypeInfo(type);
size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
{
// Copy the indices in while resizing the buffer
mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices, GL_DYNAMIC_DRAW);
mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
}
else
{
// Put the indices at the beginning of the buffer
mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize, indices);
}
// Set the index offset for the draw call to zero since the supplied index pointer is to client data
*outIndices = nullptr;
}
return gl::Error(GL_NO_ERROR);
}
gl::Error VertexArrayGL::streamAttributes(const std::vector<GLuint> &activeAttribLocations, size_t streamingDataSize,
size_t maxAttributeDataSize, const gl::RangeUI &indexRange) const
{
if (mStreamingArrayBuffer == 0)
{
mFunctions->genBuffers(1, &mStreamingArrayBuffer);
mStreamingArrayBufferSize = 0;
}
// If first is greater than zero, a slack space needs to be left at the beginning of the buffer so that
// the same 'first' argument can be passed into the draw call.
const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer);
if (requiredBufferSize > mStreamingArrayBufferSize)
{
mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
mStreamingArrayBufferSize = requiredBufferSize;
}
// Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
// somehow (such as by a screen change), retry writing the data a few times and return OUT_OF_MEMORY
// if that fails.
GLboolean unmapResult = GL_FALSE;
size_t unmapRetryAttempts = 5;
while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
{
uint8_t *bufferPointer = reinterpret_cast<uint8_t*>(mFunctions->mapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
size_t curBufferOffset = bufferEmptySpace;
const size_t streamedVertexCount = indexRange.end - indexRange.start + 1;
const auto &attribs = mData.getVertexAttributes();
for (size_t activeAttrib = 0; activeAttrib < activeAttribLocations.size(); activeAttrib++)
{
GLuint idx = activeAttribLocations[activeAttrib];
const auto &attrib = attribs[idx];
if (attrib.enabled && attrib.buffer.get() == nullptr)
{
const size_t sourceStride = ComputeVertexAttributeStride(attrib);
const size_t destStride = ComputeVertexAttributeTypeSize(attrib);
const uint8_t *inputPointer = reinterpret_cast<const uint8_t*>(attrib.pointer);
// Pack the data when copying it, user could have supplied a very large stride that would
// cause the buffer to be much larger than needed.
if (destStride == sourceStride)
{
// Can copy in one go, the data is packed
memcpy(bufferPointer + curBufferOffset,
inputPointer + (sourceStride * indexRange.start),
destStride * streamedVertexCount);
}
else
{
// Copy each vertex individually
for (size_t vertexIdx = indexRange.start; vertexIdx <= indexRange.end; vertexIdx++)
{
memcpy(bufferPointer + curBufferOffset + (destStride * vertexIdx),
inputPointer + (sourceStride * vertexIdx),
destStride);
}
}
// Compute where the 0-index vertex would be.
const size_t vertexStartOffset = curBufferOffset - (indexRange.start * destStride);
mFunctions->vertexAttribPointer(idx, attrib.size, attrib.type,
attrib.normalized, destStride,
reinterpret_cast<const GLvoid*>(vertexStartOffset));
curBufferOffset += destStride * streamedVertexCount;
// Mark the applied attribute as dirty by setting an invalid size so that if it doesn't
// need to be streamed later, there is no chance that the caching will skip it.
mAppliedAttributes[idx].size = static_cast<GLuint>(-1);
}
}
unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
}
if (unmapResult != GL_TRUE)
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to unmap the client data streaming buffer.");
}
return gl::Error(GL_NO_ERROR);
}
GLuint VertexArrayGL::getVertexArrayID() const
{
return mVertexArrayID;
}
GLuint VertexArrayGL::getAppliedElementArrayBufferID() const
{
return mAppliedElementArrayBuffer;
}
}