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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/client/vertex_array_object_manager.h"
#include <stddef.h>
#include <stdint.h>
#include "base/check_op.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
namespace gpu {
namespace gles2 {
template <typename T>
static T RoundUpToMultipleOf4(T size) {
return (size + 3) & ~3;
}
// This class tracks VertexAttribPointers and helps emulate client side buffers.
//
// The way client side buffers work is we shadow all the Vertex Attribs so we
// know which ones are pointing to client side buffers.
//
// At Draw time, for any attribs pointing to client side buffers we copy them
// to a special VBO and reset the actual vertex attrib pointers to point to this
// VBO.
//
// This also means we have to catch calls to query those values so that when
// an attrib is a client side buffer we pass the info back the user expects.
class GLES2_IMPL_EXPORT VertexArrayObject {
public:
// Info about Vertex Attributes. This is used to track what the user currently
// has bound on each Vertex Attribute so we can simulate client side buffers
// at glDrawXXX time.
class VertexAttrib {
public:
VertexAttrib()
: enabled_(false),
buffer_id_(0),
size_(4),
type_(GL_FLOAT),
normalized_(GL_FALSE),
pointer_(nullptr),
gl_stride_(0),
divisor_(0),
integer_(GL_FALSE) {}
bool enabled() const {
return enabled_;
}
void set_enabled(bool enabled) {
enabled_ = enabled;
}
GLuint buffer_id() const {
return buffer_id_;
}
void set_buffer_id(GLuint id) {
buffer_id_ = id;
}
GLenum type() const {
return type_;
}
GLint size() const {
return size_;
}
GLsizei stride() const {
return gl_stride_;
}
GLboolean normalized() const {
return normalized_;
}
const GLvoid* pointer() const {
return pointer_;
}
bool IsClientSide() const {
return buffer_id_ == 0;
}
GLuint divisor() const {
return divisor_;
}
GLboolean integer() const {
return integer_;
}
void SetInfo(
GLuint buffer_id,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei gl_stride,
const GLvoid* pointer,
GLboolean integer) {
buffer_id_ = buffer_id;
size_ = size;
type_ = type;
normalized_ = normalized;
gl_stride_ = gl_stride;
pointer_ = pointer;
integer_ = integer;
}
void SetDivisor(GLuint divisor) {
divisor_ = divisor;
}
private:
// Whether or not this attribute is enabled.
bool enabled_;
// The id of the buffer. 0 = client side buffer.
GLuint buffer_id_;
// Number of components (1, 2, 3, 4).
GLint size_;
// GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
GLenum type_;
// GL_TRUE or GL_FALSE
GLboolean normalized_;
// The pointer/offset into the buffer.
// RAW_PTR_EXCLUSION: The assigned value may be an offset instead of a
// pointer.
RAW_PTR_EXCLUSION const GLvoid* pointer_;
// The stride that will be used to access the buffer. This is the bogus GL
// stride where 0 = compute the stride based on size and type.
GLsizei gl_stride_;
// Divisor, for geometry instancing.
GLuint divisor_;
GLboolean integer_;
};
typedef std::vector<VertexAttrib> VertexAttribs;
explicit VertexArrayObject(GLuint max_vertex_attribs);
VertexArrayObject(const VertexArrayObject&) = delete;
VertexArrayObject& operator=(const VertexArrayObject&) = delete;
void UnbindBuffer(GLuint id);
bool BindElementArray(GLuint id);
bool HaveEnabledClientSideBuffers() const;
void SetAttribEnable(GLuint index, bool enabled);
void SetAttribPointer(
GLuint buffer_id,
GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride,
const void* ptr, GLboolean integer);
bool GetVertexAttrib(GLuint index, GLenum pname, uint32_t* param) const;
void SetAttribDivisor(GLuint index, GLuint divisor);
bool GetAttribPointer(GLuint index, GLenum pname, void** ptr) const;
const VertexAttribs& vertex_attribs() const {
return vertex_attribs_;
}
GLuint bound_element_array_buffer() const {
return bound_element_array_buffer_id_;
}
private:
const VertexAttrib* GetAttrib(GLuint index) const;
int num_client_side_pointers_enabled_;
// The currently bound element array buffer.
GLuint bound_element_array_buffer_id_;
VertexAttribs vertex_attribs_;
};
VertexArrayObject::VertexArrayObject(GLuint max_vertex_attribs)
: num_client_side_pointers_enabled_(0),
bound_element_array_buffer_id_(0) {
vertex_attribs_.resize(max_vertex_attribs);
}
void VertexArrayObject::UnbindBuffer(GLuint id) {
if (id == 0) {
return;
}
for (size_t ii = 0; ii < vertex_attribs_.size(); ++ii) {
VertexAttrib& attrib = vertex_attribs_[ii];
if (attrib.buffer_id() == id) {
attrib.set_buffer_id(0);
if (attrib.enabled()) {
++num_client_side_pointers_enabled_;
}
}
}
if (bound_element_array_buffer_id_ == id) {
bound_element_array_buffer_id_ = 0;
}
}
bool VertexArrayObject::BindElementArray(GLuint id) {
if (id == bound_element_array_buffer_id_) {
return false;
}
bound_element_array_buffer_id_ = id;
return true;
}
bool VertexArrayObject::HaveEnabledClientSideBuffers() const {
return num_client_side_pointers_enabled_ > 0;
}
void VertexArrayObject::SetAttribEnable(GLuint index, bool enabled) {
if (index < vertex_attribs_.size()) {
VertexAttrib& attrib = vertex_attribs_[index];
if (attrib.enabled() != enabled) {
if (attrib.IsClientSide()) {
num_client_side_pointers_enabled_ += enabled ? 1 : -1;
DCHECK_GE(num_client_side_pointers_enabled_, 0);
}
attrib.set_enabled(enabled);
}
}
}
void VertexArrayObject::SetAttribPointer(
GLuint buffer_id,
GLuint index,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
const void* ptr,
GLboolean integer) {
if (index < vertex_attribs_.size()) {
VertexAttrib& attrib = vertex_attribs_[index];
if (attrib.IsClientSide() && attrib.enabled()) {
--num_client_side_pointers_enabled_;
DCHECK_GE(num_client_side_pointers_enabled_, 0);
}
attrib.SetInfo(buffer_id, size, type, normalized, stride, ptr, integer);
if (attrib.IsClientSide() && attrib.enabled()) {
++num_client_side_pointers_enabled_;
}
}
}
bool VertexArrayObject::GetVertexAttrib(GLuint index,
GLenum pname,
uint32_t* param) const {
const VertexAttrib* attrib = GetAttrib(index);
if (!attrib) {
return false;
}
switch (pname) {
case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
*param = attrib->buffer_id();
break;
case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
*param = attrib->enabled();
break;
case GL_VERTEX_ATTRIB_ARRAY_SIZE:
*param = attrib->size();
break;
case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
*param = attrib->stride();
break;
case GL_VERTEX_ATTRIB_ARRAY_TYPE:
*param = attrib->type();
break;
case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
*param = attrib->normalized();
break;
case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
*param = attrib->integer();
break;
default:
return false; // pass through to service side.
}
return true;
}
void VertexArrayObject::SetAttribDivisor(GLuint index, GLuint divisor) {
if (index < vertex_attribs_.size()) {
VertexAttrib& attrib = vertex_attribs_[index];
attrib.SetDivisor(divisor);
}
}
// Gets the Attrib pointer for an attrib but only if it's a client side
// pointer. Returns true if it got the pointer.
bool VertexArrayObject::GetAttribPointer(
GLuint index, GLenum pname, void** ptr) const {
const VertexAttrib* attrib = GetAttrib(index);
if (attrib && pname == GL_VERTEX_ATTRIB_ARRAY_POINTER) {
*ptr = const_cast<void*>(attrib->pointer());
return true;
}
return false;
}
// Gets an attrib if it's in range and it's client side.
const VertexArrayObject::VertexAttrib* VertexArrayObject::GetAttrib(
GLuint index) const {
if (index < vertex_attribs_.size()) {
const VertexAttrib* attrib = &vertex_attribs_[index];
return attrib;
}
return nullptr;
}
VertexArrayObjectManager::VertexArrayObjectManager(
GLuint max_vertex_attribs,
GLuint array_buffer_id,
GLuint element_array_buffer_id,
bool support_client_side_arrays)
: max_vertex_attribs_(max_vertex_attribs),
array_buffer_id_(array_buffer_id),
array_buffer_size_(0),
array_buffer_offset_(0),
element_array_buffer_id_(element_array_buffer_id),
element_array_buffer_size_(0),
collection_buffer_size_(0),
default_vertex_array_object_(
std::make_unique<VertexArrayObject>(max_vertex_attribs)),
bound_vertex_array_object_(default_vertex_array_object_.get()),
support_client_side_arrays_(support_client_side_arrays) {}
VertexArrayObjectManager::~VertexArrayObjectManager() = default;
bool VertexArrayObjectManager::IsReservedId(GLuint id) const {
return (id != 0 &&
(id == array_buffer_id_ || id == element_array_buffer_id_));
}
GLuint VertexArrayObjectManager::bound_element_array_buffer() const {
return bound_vertex_array_object_->bound_element_array_buffer();
}
void VertexArrayObjectManager::UnbindBuffer(GLuint id) {
bound_vertex_array_object_->UnbindBuffer(id);
}
bool VertexArrayObjectManager::BindElementArray(GLuint id) {
return bound_vertex_array_object_->BindElementArray(id);
}
void VertexArrayObjectManager::GenVertexArrays(
GLsizei n, const GLuint* arrays) {
DCHECK_GE(n, 0);
for (GLsizei i = 0; i < n; ++i) {
std::pair<VertexArrayObjectMap::iterator, bool> result =
vertex_array_objects_.insert(std::make_pair(
arrays[i],
std::make_unique<VertexArrayObject>(max_vertex_attribs_)));
DCHECK(result.second);
}
}
void VertexArrayObjectManager::DeleteVertexArrays(
GLsizei n, const GLuint* arrays) {
DCHECK_GE(n, 0);
for (GLsizei i = 0; i < n; ++i) {
GLuint id = arrays[i];
if (id) {
VertexArrayObjectMap::iterator it = vertex_array_objects_.find(id);
if (it != vertex_array_objects_.end()) {
if (bound_vertex_array_object_ == it->second.get()) {
bound_vertex_array_object_ = default_vertex_array_object_.get();
}
vertex_array_objects_.erase(it);
}
}
}
}
bool VertexArrayObjectManager::BindVertexArray(GLuint array, bool* changed) {
*changed = false;
VertexArrayObject* vertex_array_object = default_vertex_array_object_.get();
if (array != 0) {
VertexArrayObjectMap::iterator it = vertex_array_objects_.find(array);
if (it == vertex_array_objects_.end()) {
return false;
}
vertex_array_object = it->second.get();
}
*changed = vertex_array_object != bound_vertex_array_object_;
bound_vertex_array_object_ = vertex_array_object;
return true;
}
bool VertexArrayObjectManager::HaveEnabledClientSideBuffers() const {
return bound_vertex_array_object_->HaveEnabledClientSideBuffers();
}
void VertexArrayObjectManager::SetAttribEnable(GLuint index, bool enabled) {
bound_vertex_array_object_->SetAttribEnable(index, enabled);
}
bool VertexArrayObjectManager::GetVertexAttrib(GLuint index,
GLenum pname,
uint32_t* param) {
return bound_vertex_array_object_->GetVertexAttrib(index, pname, param);
}
bool VertexArrayObjectManager::GetAttribPointer(
GLuint index, GLenum pname, void** ptr) const {
return bound_vertex_array_object_->GetAttribPointer(index, pname, ptr);
}
bool VertexArrayObjectManager::SetAttribPointer(
GLuint buffer_id,
GLuint index,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
const void* ptr,
GLboolean integer) {
// Client side arrays are not allowed in vaos.
if (buffer_id == 0 && !IsDefaultVAOBound() && ptr) {
return false;
}
bound_vertex_array_object_->SetAttribPointer(
buffer_id, index, size, type, normalized, stride, ptr, integer);
return true;
}
void VertexArrayObjectManager::SetAttribDivisor(GLuint index, GLuint divisor) {
bound_vertex_array_object_->SetAttribDivisor(index, divisor);
}
// Collects the data into the collection buffer and returns the number of
// bytes collected.
GLsizei VertexArrayObjectManager::CollectData(
const void* data,
GLsizei bytes_per_element,
GLsizei real_stride,
GLsizei num_elements) {
GLsizei bytes_needed = bytes_per_element * num_elements;
if (collection_buffer_size_ < bytes_needed) {
collection_buffer_.reset(new int8_t[bytes_needed]);
collection_buffer_size_ = bytes_needed;
}
const int8_t* src = static_cast<const int8_t*>(data);
int8_t* dst = collection_buffer_.get();
int8_t* end = dst + bytes_per_element * num_elements;
for (; dst < end; src += real_stride, dst += bytes_per_element) {
memcpy(dst, src, bytes_per_element);
}
return bytes_needed;
}
bool VertexArrayObjectManager::IsDefaultVAOBound() const {
return bound_vertex_array_object_ == default_vertex_array_object_.get();
}
bool VertexArrayObjectManager::SupportsClientSideBuffers() {
return support_client_side_arrays_ &&
bound_vertex_array_object_->HaveEnabledClientSideBuffers();
}
// Returns true if buffers were setup.
bool VertexArrayObjectManager::SetupSimulatedClientSideBuffers(
const char* function_name,
GLES2Implementation* gl,
GLES2CmdHelper* gl_helper,
GLsizei num_elements,
GLsizei primcount,
bool* simulated) {
*simulated = false;
if (!SupportsClientSideBuffers())
return false;
if (!IsDefaultVAOBound()) {
gl->SetGLError(
GL_INVALID_OPERATION, function_name,
"client side arrays not allowed with vertex array object");
return false;
}
*simulated = true;
base::CheckedNumeric<GLsizei> checked_total_size = 0;
// Compute the size of the buffer we need.
const VertexArrayObject::VertexAttribs& vertex_attribs =
bound_vertex_array_object_->vertex_attribs();
for (GLuint ii = 0; ii < vertex_attribs.size(); ++ii) {
const VertexArrayObject::VertexAttrib& attrib = vertex_attribs[ii];
if (attrib.IsClientSide() && attrib.enabled()) {
uint32_t bytes_per_element =
GLES2Util::GetGroupSizeForBufferType(attrib.size(), attrib.type());
GLsizei elements = (primcount && attrib.divisor() > 0) ?
((primcount - 1) / attrib.divisor() + 1) : num_elements;
checked_total_size +=
RoundUpToMultipleOf4(base::CheckMul(bytes_per_element, elements));
}
}
GLsizei total_size = 0;
if (!checked_total_size.AssignIfValid(&total_size)) {
gl->SetGLError(GL_INVALID_OPERATION, function_name,
"size overflow for client side arrays");
return false;
}
gl_helper->BindBuffer(GL_ARRAY_BUFFER, array_buffer_id_);
array_buffer_offset_ = 0;
if (total_size > array_buffer_size_) {
gl->BufferDataHelper(GL_ARRAY_BUFFER, total_size, nullptr, GL_DYNAMIC_DRAW);
array_buffer_size_ = total_size;
}
for (GLuint ii = 0; ii < vertex_attribs.size(); ++ii) {
const VertexArrayObject::VertexAttrib& attrib = vertex_attribs[ii];
if (attrib.IsClientSide() && attrib.enabled()) {
uint32_t bytes_per_element =
GLES2Util::GetGroupSizeForBufferType(attrib.size(), attrib.type());
GLsizei real_stride = attrib.stride() ?
attrib.stride() : static_cast<GLsizei>(bytes_per_element);
GLsizei elements = (primcount && attrib.divisor() > 0) ?
((primcount - 1) / attrib.divisor() + 1) : num_elements;
GLsizei bytes_collected = CollectData(
attrib.pointer(), bytes_per_element, real_stride, elements);
gl->BufferSubDataHelper(
GL_ARRAY_BUFFER, array_buffer_offset_, bytes_collected,
collection_buffer_.get());
gl_helper->VertexAttribPointer(
ii, attrib.size(), attrib.type(), attrib.normalized(), 0,
array_buffer_offset_);
array_buffer_offset_ += RoundUpToMultipleOf4(bytes_collected);
DCHECK_LE(array_buffer_offset_, array_buffer_size_);
}
}
return true;
}
// Copies in indices to the service and returns the highest index accessed + 1
bool VertexArrayObjectManager::SetupSimulatedIndexAndClientSideBuffers(
const char* function_name,
GLES2Implementation* gl,
GLES2CmdHelper* gl_helper,
GLsizei count,
GLenum type,
GLsizei primcount,
const void* indices,
GLuint* offset,
bool* simulated) {
*simulated = false;
*offset = ToGLuint(indices);
if (!support_client_side_arrays_)
return true;
GLsizei num_elements = 0;
if (bound_vertex_array_object_->bound_element_array_buffer() == 0) {
*simulated = true;
*offset = 0;
GLsizei max_index = -1;
switch (type) {
case GL_UNSIGNED_BYTE: {
const uint8_t* src = static_cast<const uint8_t*>(indices);
for (GLsizei ii = 0; ii < count; ++ii) {
if (src[ii] > max_index) {
max_index = src[ii];
}
}
break;
}
case GL_UNSIGNED_SHORT: {
const uint16_t* src = static_cast<const uint16_t*>(indices);
for (GLsizei ii = 0; ii < count; ++ii) {
if (src[ii] > max_index) {
max_index = src[ii];
}
}
break;
}
case GL_UNSIGNED_INT: {
uint32_t max_glsizei =
static_cast<uint32_t>(std::numeric_limits<GLsizei>::max());
const uint32_t* src = static_cast<const uint32_t*>(indices);
for (GLsizei ii = 0; ii < count; ++ii) {
// Other parts of the API use GLsizei (signed) to store limits.
// As such, if we encounter a index that cannot be represented with
// an unsigned int we need to flag it as an error here.
if(src[ii] > max_glsizei) {
gl->SetGLError(
GL_INVALID_OPERATION, function_name, "index too large.");
return false;
}
GLsizei signed_index = static_cast<GLsizei>(src[ii]);
if (signed_index > max_index) {
max_index = signed_index;
}
}
break;
}
default:
break;
}
gl_helper->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_array_buffer_id_);
uint32_t bytes_per_element = GLES2Util::GetGLTypeSizeForBuffers(type);
GLsizei bytes_needed = 0;
if (!base::CheckMul(bytes_per_element, count)
.AssignIfValid(&bytes_needed)) {
gl->SetGLError(GL_INVALID_OPERATION, function_name,
"size overflow for client side index arrays");
return false;
}
if (bytes_needed > element_array_buffer_size_) {
element_array_buffer_size_ = bytes_needed;
gl->BufferDataHelper(GL_ELEMENT_ARRAY_BUFFER, bytes_needed, nullptr,
GL_DYNAMIC_DRAW);
}
gl->BufferSubDataHelper(
GL_ELEMENT_ARRAY_BUFFER, 0, bytes_needed, indices);
num_elements = max_index + 1;
} else if (bound_vertex_array_object_->HaveEnabledClientSideBuffers()) {
// Index buffer is GL buffer. Ask the service for the highest vertex
// that will be accessed. Note: It doesn't matter if another context
// changes the contents of any of the buffers. The service will still
// validate the indices. We just need to know how much to copy across.
num_elements = gl->GetMaxValueInBufferCHROMIUMHelper(
bound_vertex_array_object_->bound_element_array_buffer(),
count, type, ToGLuint(indices)) + 1;
}
bool simulated_client_side_buffers = false;
SetupSimulatedClientSideBuffers(
function_name, gl, gl_helper, num_elements, primcount,
&simulated_client_side_buffers);
*simulated = *simulated || simulated_client_side_buffers;
return true;
}
} // namespace gles2
} // namespace gpu
|