Hash :
9a05a2b7
Author :
Date :
2025-06-23T16:24:13
Reland "Vulkan: Avoid some loops in VertexArray::onBufferChanged" This reverts commit 5df85793c602d64d47ef68e29542313be4116bc0. Reason for revert: the regression bug is fixed Some of the functionality related code has been merged into previous CL. This CL now mostly clean up. The old subject/observer code is been deleted from VertexArray. Bug: angleproject:400711938 Original change's description: > Revert "Vulkan: Avoid some loops in VertexArray::onBufferChanged" > > This reverts commit 8d6d127acc1072488e5b57ba4e7e93da2d94bfda. > > Reason for revert: https://issues.chromium.org/427064102 > > Bug: angleproject:400711938 > Original change's description: > > Vulkan: Avoid some loops in VertexArray::onBufferChanged > > > > Before this CL, VertexArray::onBufferChanged() loops over > > bufferBindingMask bits and calls onSubjectStateChange. In this CL, > > VertexArray::onSubjectStateChange is embedded into > > VertexArray::onBufferChanged(). DIRTY_BIT_ELEMENT_ARRAY_BUFFER and > > DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA is re-arranged so that we can map > > bufferBindingMask directly to VertexArray::mDirtyBits. This especially > > useful when one buffer is bound to multiple indices in the VertexArray. > > > > This CL also removes angle::ObserverInterface from VertexArray, since it > > no longer observes anything. > > > > ASSERT is added in gl::Buffer::mContentsObservers to ensure it only > > contains BufferTexture, since vertexArray is no longer using the > > subject/observer. > > > > Bug: angleproject:400711938 > > Change-Id: Ie6e7159d7a89f0da5e1b7ca0a9dbe60a1e6c682f > > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6569638 > > Reviewed-by: Geoff Lang <geofflang@chromium.org> > > Commit-Queue: Charlie Lao <cclao@google.com> > > Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> > > Bug: angleproject:400711938 > Change-Id: I3b8e77db7b3d06b9ed875bfe7787904ac753da11 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6664161 > Commit-Queue: Charlie Lao <cclao@google.com> > Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Bug: angleproject:400711938 Change-Id: If7989b26701dcccbb8e49c42d388c0217e2d0b7f Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6663536 Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Charlie Lao <cclao@google.com>
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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
//
// Copyright 2002 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.
//
// Buffer.cpp: Implements the gl::Buffer class, representing storage of vertex and/or
// index data. Implements GL buffer objects and related functionality.
// [OpenGL ES 2.0.24] section 2.9 page 21.
#include "libANGLE/Buffer.h"
#include "libANGLE/Context.h"
#include "libANGLE/renderer/BufferImpl.h"
#include "libANGLE/renderer/GLImplFactory.h"
namespace gl
{
namespace
{
constexpr size_t kInvalidContentsObserverIndex = std::numeric_limits<size_t>::max();
} // anonymous namespace
// VertexArrayBufferBindingMaskAndContext implementation
VertexArrayBufferBindingMaskAndContext::VertexArrayBufferBindingMaskAndContext() {}
VertexArrayBufferBindingMaskAndContext::~VertexArrayBufferBindingMaskAndContext()
{
mBufferBindingMask.clear();
}
void VertexArrayBufferBindingMaskAndContext::add(const gl::Context *context, size_t bindingIndex)
{
for (auto &contextAndMask : mBufferBindingMask)
{
if (contextAndMask.first == context)
{
contextAndMask.second.set(bindingIndex);
return;
}
}
mBufferBindingMask.emplace_back(context, VertexArrayBufferBindingMask({bindingIndex}));
}
void VertexArrayBufferBindingMaskAndContext::remove(const gl::Context *context, size_t bindingIndex)
{
for (auto iter = mBufferBindingMask.begin(); iter != mBufferBindingMask.end(); ++iter)
{
if (iter->first == context)
{
iter->second.reset(bindingIndex);
if (iter->second.none())
{
mBufferBindingMask.erase(iter);
}
return;
}
}
UNREACHABLE();
}
VertexArrayBufferBindingMask VertexArrayBufferBindingMaskAndContext::getBufferBindingMask(
const gl::Context *context) const
{
for (auto &contextAndMask : mBufferBindingMask)
{
if (contextAndMask.first == context)
{
return contextAndMask.second;
}
}
return VertexArrayBufferBindingMask::Zero();
}
// BufferState implementation
BufferState::BufferState()
: mLabel(),
mUsage(BufferUsage::StaticDraw),
mSize(0),
mAccessFlags(0),
mAccess(GL_WRITE_ONLY_OES),
mMapped(GL_FALSE),
mMapPointer(nullptr),
mMapOffset(0),
mMapLength(0),
mBindingCount(0),
mTransformFeedbackIndexedBindingCount(0),
mTransformFeedbackGenericBindingCount(0),
mImmutable(GL_FALSE),
mStorageExtUsageFlags(0),
mExternal(GL_FALSE),
mWebGLType(WebGLBufferType::Undefined)
{}
BufferState::~BufferState() {}
Buffer::Buffer(rx::GLImplFactory *factory, BufferID id)
: RefCountObject(factory->generateSerial(), id), mImpl(factory->createBuffer(mState))
{
}
Buffer::~Buffer()
{
SafeDelete(mImpl);
}
void Buffer::onDestroy(const Context *context)
{
mContentsObservers.clear();
// In tests, mImpl might be null.
if (mImpl)
mImpl->destroy(context);
}
void Buffer::onBind(const Context *context, BufferBinding target)
{
ASSERT(context->isWebGL());
if (mState.mWebGLType == WebGLBufferType::Undefined)
{
if (target == BufferBinding::ElementArray)
{
mState.mWebGLType = WebGLBufferType::ElementArray;
}
else
{
mState.mWebGLType = WebGLBufferType::OtherData;
}
}
}
angle::Result Buffer::setLabel(const Context *context, const std::string &label)
{
mState.mLabel = label;
if (mImpl)
{
return mImpl->onLabelUpdate(context);
}
return angle::Result::Continue;
}
const std::string &Buffer::getLabel() const
{
return mState.mLabel;
}
angle::Result Buffer::bufferStorageExternal(Context *context,
BufferBinding target,
GLsizeiptr size,
GLeglClientBufferEXT clientBuffer,
GLbitfield flags)
{
return bufferExternalDataImpl(context, target, clientBuffer, size, flags);
}
angle::Result Buffer::bufferStorage(Context *context,
BufferBinding target,
GLsizeiptr size,
const void *data,
GLbitfield flags)
{
return bufferDataImpl(context, target, data, size, BufferUsage::DynamicDraw, flags,
BufferStorage::Immutable);
}
angle::Result Buffer::bufferData(Context *context,
BufferBinding target,
const void *data,
GLsizeiptr size,
BufferUsage usage)
{
GLbitfield flags = (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_DYNAMIC_STORAGE_BIT_EXT);
return bufferDataImpl(context, target, data, size, usage, flags, BufferStorage::Mutable);
}
angle::Result Buffer::setDataWithUsageFlags(const gl::Context *context,
gl::BufferBinding target,
GLeglClientBufferEXT clientBuffer,
const void *data,
size_t size,
gl::BufferUsage usage,
GLbitfield flags,
gl::BufferStorage bufferStorage)
{
rx::BufferFeedback feedback;
angle::Result result = mImpl->setDataWithUsageFlags(context, target, clientBuffer, data, size,
usage, flags, bufferStorage, &feedback);
applyImplFeedback(context, feedback);
if (result == angle::Result::Stop)
{
// If setData fails, the buffer contents are undefined. Set a zero size to indicate that.
mIndexRangeCache.clear();
mState.mSize = 0;
// Notify when storage changes.
onStateChange(context, angle::SubjectMessage::SubjectChanged);
}
return result;
}
angle::Result Buffer::bufferDataImpl(Context *context,
BufferBinding target,
const void *data,
GLsizeiptr size,
BufferUsage usage,
GLbitfield flags,
BufferStorage bufferStorage)
{
const void *dataForImpl = data;
if (mState.isMapped())
{
// Per the OpenGL ES 3.0 spec, buffers are implicity unmapped when a call to
// BufferData happens on a mapped buffer:
//
// If any portion of the buffer object is mapped in the current context or any context
// current to another thread, it is as though UnmapBuffer (see section 2.10.3) is
// executed in each such context prior to deleting the existing data store.
//
GLboolean dontCare = GL_FALSE;
ANGLE_TRY(unmap(context, &dontCare));
}
// If we are using robust resource init, make sure the buffer starts cleared.
// Note: the Context is checked for nullptr because of some testing code.
// TODO(jmadill): Investigate lazier clearing.
if (context && context->isRobustResourceInitEnabled() && !data && size > 0)
{
angle::MemoryBuffer *scratchBuffer = nullptr;
ANGLE_CHECK_GL_ALLOC(
context, context->getZeroFilledBuffer(static_cast<size_t>(size), &scratchBuffer));
dataForImpl = scratchBuffer->data();
}
ANGLE_TRY(setDataWithUsageFlags(context, target, nullptr, dataForImpl, size, usage, flags,
bufferStorage));
bool wholeBuffer = size == mState.mSize;
mIndexRangeCache.clear();
mState.mUsage = usage;
mState.mSize = size;
mState.mImmutable = (bufferStorage == BufferStorage::Immutable);
mState.mStorageExtUsageFlags = flags;
// Notify when storage changes.
if (wholeBuffer)
{
onContentsChange(context);
}
else
{
onStateChange(context, angle::SubjectMessage::SubjectChanged);
}
return angle::Result::Continue;
}
angle::Result Buffer::bufferExternalDataImpl(Context *context,
BufferBinding target,
GLeglClientBufferEXT clientBuffer,
GLsizeiptr size,
GLbitfield flags)
{
if (mState.isMapped())
{
// Per the OpenGL ES 3.0 spec, buffers are implicitly unmapped when a call to
// BufferData happens on a mapped buffer:
//
// If any portion of the buffer object is mapped in the current context or any context
// current to another thread, it is as though UnmapBuffer (see section 2.10.3) is
// executed in each such context prior to deleting the existing data store.
//
GLboolean dontCare = GL_FALSE;
ANGLE_TRY(unmap(context, &dontCare));
}
ANGLE_TRY(setDataWithUsageFlags(context, target, clientBuffer, nullptr, size,
BufferUsage::InvalidEnum, flags, BufferStorage::Immutable));
mIndexRangeCache.clear();
mState.mUsage = BufferUsage::InvalidEnum;
mState.mSize = size;
mState.mImmutable = GL_TRUE;
mState.mStorageExtUsageFlags = flags;
mState.mExternal = GL_TRUE;
// Notify when storage changes.
onStateChange(context, angle::SubjectMessage::SubjectChanged);
return angle::Result::Continue;
}
angle::Result Buffer::bufferSubData(const Context *context,
BufferBinding target,
const void *data,
GLsizeiptr size,
GLintptr offset)
{
rx::BufferFeedback feedback;
ANGLE_TRY_WITH_FINALLY(mImpl->setSubData(context, target, data, size, offset, &feedback),
applyImplFeedback(context, feedback));
mIndexRangeCache.invalidateRange(static_cast<unsigned int>(offset),
static_cast<unsigned int>(size));
// Notify when data changes.
onContentsChange(context);
return angle::Result::Continue;
}
angle::Result Buffer::copyBufferSubData(const Context *context,
Buffer *source,
GLintptr sourceOffset,
GLintptr destOffset,
GLsizeiptr size)
{
rx::BufferFeedback feedback;
ANGLE_TRY_WITH_FINALLY(mImpl->copySubData(context, source->getImplementation(), sourceOffset,
destOffset, size, &feedback),
applyImplFeedback(context, feedback));
mIndexRangeCache.invalidateRange(static_cast<unsigned int>(destOffset),
static_cast<unsigned int>(size));
// Notify when data changes.
onContentsChange(context);
return angle::Result::Continue;
}
angle::Result Buffer::map(const Context *context, GLenum access)
{
ASSERT(!mState.mMapped);
rx::BufferFeedback feedback;
mState.mMapPointer = nullptr;
ANGLE_TRY_WITH_FINALLY(mImpl->map(context, access, &mState.mMapPointer, &feedback),
applyImplFeedback(context, feedback));
ASSERT(access == GL_WRITE_ONLY_OES);
mState.mMapped = GL_TRUE;
mState.mMapOffset = 0;
mState.mMapLength = mState.mSize;
mState.mAccess = access;
mState.mAccessFlags = GL_MAP_WRITE_BIT;
mIndexRangeCache.clear();
// Notify when state changes.
onStateChange(context, angle::SubjectMessage::SubjectMapped);
return angle::Result::Continue;
}
angle::Result Buffer::mapRange(const Context *context,
GLintptr offset,
GLsizeiptr length,
GLbitfield access)
{
ASSERT(!mState.mMapped);
ASSERT(offset + length <= mState.mSize);
rx::BufferFeedback feedback;
mState.mMapPointer = nullptr;
ANGLE_TRY_WITH_FINALLY(
mImpl->mapRange(context, offset, length, access, &mState.mMapPointer, &feedback),
applyImplFeedback(context, feedback));
mState.mMapped = GL_TRUE;
mState.mMapOffset = static_cast<GLint64>(offset);
mState.mMapLength = static_cast<GLint64>(length);
mState.mAccess = GL_WRITE_ONLY_OES;
mState.mAccessFlags = access;
// The OES_mapbuffer extension states that GL_WRITE_ONLY_OES is the only valid
// value for GL_BUFFER_ACCESS_OES because it was written against ES2. Since there is
// no update for ES3 and the GL_READ_ONLY and GL_READ_WRITE enums don't exist for ES,
// we cannot properly set GL_BUFFER_ACCESS_OES when glMapBufferRange is called.
if ((access & GL_MAP_WRITE_BIT) > 0)
{
mIndexRangeCache.invalidateRange(static_cast<unsigned int>(offset),
static_cast<unsigned int>(length));
}
// Notify when state changes.
onStateChange(context, angle::SubjectMessage::SubjectMapped);
return angle::Result::Continue;
}
angle::Result Buffer::unmap(const Context *context, GLboolean *result)
{
ASSERT(mState.mMapped);
rx::BufferFeedback feedback;
*result = GL_FALSE;
ANGLE_TRY_WITH_FINALLY(mImpl->unmap(context, result, &feedback),
applyImplFeedback(context, feedback));
mState.mMapped = GL_FALSE;
mState.mMapPointer = nullptr;
mState.mMapOffset = 0;
mState.mMapLength = 0;
mState.mAccess = GL_WRITE_ONLY_OES;
mState.mAccessFlags = 0;
// Notify when data changes.
onStateChange(context, angle::SubjectMessage::SubjectUnmapped);
return angle::Result::Continue;
}
void Buffer::onDataChanged(const Context *context)
{
mIndexRangeCache.clear();
// Notify when data changes.
onContentsChange(context);
mImpl->onDataChanged();
}
angle::Result Buffer::getIndexRange(const gl::Context *context,
DrawElementsType type,
size_t offset,
size_t count,
bool primitiveRestartEnabled,
IndexRange *outRange) const
{
if (mIndexRangeCache.findRange(type, offset, count, primitiveRestartEnabled, outRange))
{
return angle::Result::Continue;
}
ANGLE_TRY(
mImpl->getIndexRange(context, type, offset, count, primitiveRestartEnabled, outRange));
mIndexRangeCache.addRange(type, offset, count, primitiveRestartEnabled, *outRange);
return angle::Result::Continue;
}
GLint64 Buffer::getMemorySize() const
{
GLint64 implSize = mImpl->getMemorySize();
return implSize > 0 ? implSize : mState.mSize;
}
bool Buffer::isDoubleBoundForTransformFeedback() const
{
return mState.mTransformFeedbackIndexedBindingCount > 1;
}
void Buffer::onTFBindingChanged(const Context *context, bool bound, bool indexed)
{
ASSERT(bound || mState.mBindingCount > 0);
mState.mBindingCount += bound ? 1 : -1;
if (indexed)
{
ASSERT(bound || mState.mTransformFeedbackIndexedBindingCount > 0);
mState.mTransformFeedbackIndexedBindingCount += bound ? 1 : -1;
onStateChange(context, angle::SubjectMessage::BindingChanged);
}
else
{
mState.mTransformFeedbackGenericBindingCount += bound ? 1 : -1;
}
}
angle::Result Buffer::getSubData(const gl::Context *context,
GLintptr offset,
GLsizeiptr size,
void *outData)
{
return mImpl->getSubData(context, offset, size, outData);
}
size_t Buffer::getContentsObserverIndex(void *observer, uint32_t bufferIndex) const
{
ContentsObserver contentsObserver{bufferIndex, observer};
for (size_t observerIndex = 0; observerIndex < mContentsObservers.size(); ++observerIndex)
{
if (mContentsObservers[observerIndex] == contentsObserver)
{
return observerIndex;
}
}
return kInvalidContentsObserverIndex;
}
void Buffer::addContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex)
{
ASSERT(bufferIndex != ContentsObserver::kBufferTextureIndex);
if (getContentsObserverIndex(vertexArray, bufferIndex) == kInvalidContentsObserverIndex)
{
mContentsObservers.push_back({bufferIndex, vertexArray});
}
}
void Buffer::removeContentsObserverImpl(void *observer, uint32_t bufferIndex)
{
size_t foundObserver = getContentsObserverIndex(observer, bufferIndex);
if (foundObserver != kInvalidContentsObserverIndex)
{
size_t lastObserverIndex = mContentsObservers.size() - 1;
if (foundObserver != lastObserverIndex)
{
mContentsObservers[foundObserver] = mContentsObservers[lastObserverIndex];
}
mContentsObservers.pop_back();
}
}
void Buffer::removeContentsObserver(VertexArray *vertexArray, uint32_t bufferIndex)
{
removeContentsObserverImpl(vertexArray, bufferIndex);
}
void Buffer::addContentsObserver(Texture *texture)
{
if (!hasContentsObserver(texture))
{
mContentsObservers.push_back({ContentsObserver::kBufferTextureIndex, texture});
}
}
void Buffer::removeContentsObserver(Texture *texture)
{
removeContentsObserverImpl(texture, ContentsObserver::kBufferTextureIndex);
}
bool Buffer::hasContentsObserver(Texture *texture) const
{
return getContentsObserverIndex(texture, ContentsObserver::kBufferTextureIndex) !=
kInvalidContentsObserverIndex;
}
void Buffer::onStateChange(const Context *context, angle::SubjectMessage message)
{
// Pass message to other buffer observers such as XFB and Texture
angle::Subject::onStateChange(message);
// Apply the change directly on current context's current vertex array. All other vertex arrays
// requires a buffer rebind in order to pick up the change.
context->onBufferChanged(message,
mVertexArrayBufferBindingMaskAndContext.getBufferBindingMask(context));
}
void Buffer::onContentsChange(const Context *context)
{
for (const ContentsObserver &contentsObserver : mContentsObservers)
{
ASSERT(contentsObserver.bufferIndex == ContentsObserver::kBufferTextureIndex);
static_cast<Texture *>(contentsObserver.observer)->onBufferContentsChange();
}
context->onBufferChanged(angle::SubjectMessage::ContentsChanged,
mVertexArrayBufferBindingMaskAndContext.getBufferBindingMask(context));
}
void Buffer::applyImplFeedback(const gl::Context *context, const rx::BufferFeedback &feedback)
{
// Pass it along to observer of Buffer
if (feedback.internalMemoryAllocationChanged)
{
onStateChange(context, angle::SubjectMessage::InternalMemoryAllocationChanged);
}
if (feedback.bufferStateChanged)
{
onStateChange(context, angle::SubjectMessage::SubjectChanged);
}
}
} // namespace gl