Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 891x 890x 2x 888x 884x 884x 884x 884x 884x 10359x 10359x 791x 791x 9568x 9568x 9568x 935x 9568x 8633x 884x 884x 935x 935x 935x 935x 935x 935x 935x 884x 884x 884x 884x 884x 884x 884x 1x 888x 888x 10364x 10364x 10364x 10364x 1x 10363x 10363x 1x 10362x 1x 885x 885x 10361x 10361x 10361x 1x 10360x 884x 1x 935x 935x 935x 9568x 9568x 935x 935x 935x 935x 935x 1x 935x 935x 935x 935x 9568x 9568x 9568x 9568x 935x 1x 886x 886x 886x 886x 937x 937x 937x 886x 1x 937x 937x 937x 2x 2x 2x 2x 24x 937x 937x 937x 9570x 9570x 937x 1x 968x 151046x 151046x 244x 787x 787x 787x 787x 10x 11x 11x 11x 11x 11x 8348x 2651414x 2651414x 2651414x 2651414x 2651414x 2651414x 1x 937x 937x 937x 9570x 9570x 1x 939x 939x 939x 1004x 1004x 939x 883x 883x 883x 883x 883x 883x 883x 934x 934x 934x 883x 883x 1004x 1001x 1001x 1001x 1001x 1001x 934x 1x 934x 934x 934x 67x 70x 1x 934x 934x 934x 9567x 9567x 1x 9x 2x 7x 1x 6x 6x 10x 10x 7x 7x 7x 1x 6x 6x 10x 2645x 2645x 1768x 877x 877x 877x 877x 1x 1x 1x 876x 876x 925x 925x 876x 876x | import Check from "../Core/Check.js";
import ComponentDatatype from "../Core/ComponentDatatype.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import CesiumMath from "../Core/Math.js";
import Buffer from "./Buffer.js";
import BufferUsage from "./BufferUsage.js";
import VertexArray from "./VertexArray.js";
/**
* @private
*/
function VertexArrayFacade(context, attributes, sizeInVertices, instanced) {
//>>includeStart('debug', pragmas.debug);
Check.defined("context", context);
if (!attributes || attributes.length === 0) {
throw new DeveloperError("At least one attribute is required.");
}
//>>includeEnd('debug');
const attrs = VertexArrayFacade._verifyAttributes(attributes);
sizeInVertices = sizeInVertices ?? 0;
const precreatedAttributes = [];
const attributesByUsage = {};
let attributesForUsage;
let usage;
// Bucket the attributes by usage.
const length = attrs.length;
for (let i = 0; i < length; ++i) {
const attribute = attrs[i];
// If the attribute already has a vertex buffer, we do not need
// to manage a vertex buffer or typed array for it.
if (attribute.vertexBuffer) {
precreatedAttributes.push(attribute);
continue;
}
usage = attribute.usage;
attributesForUsage = attributesByUsage[usage];
if (!defined(attributesForUsage)) {
attributesForUsage = attributesByUsage[usage] = [];
}
attributesForUsage.push(attribute);
}
// A function to sort attributes by the size of their components. From left to right, a vertex
// stores floats, shorts, and then bytes.
function compare(left, right) {
return (
ComponentDatatype.getSizeInBytes(right.componentDatatype) -
ComponentDatatype.getSizeInBytes(left.componentDatatype)
);
}
this._allBuffers = [];
for (usage in attributesByUsage) {
Eif (attributesByUsage.hasOwnProperty(usage)) {
attributesForUsage = attributesByUsage[usage];
attributesForUsage.sort(compare);
const vertexSizeInBytes =
VertexArrayFacade._vertexSizeInBytes(attributesForUsage);
const bufferUsage = attributesForUsage[0].usage;
const buffer = {
vertexSizeInBytes: vertexSizeInBytes,
vertexBuffer: undefined,
usage: bufferUsage,
needsCommit: false,
arrayBuffer: undefined,
arrayViews: VertexArrayFacade._createArrayViews(
attributesForUsage,
vertexSizeInBytes,
),
};
this._allBuffers.push(buffer);
}
}
this._size = 0;
this._instanced = instanced ?? false;
this._precreated = precreatedAttributes;
this._context = context;
this.writers = undefined;
this.va = undefined;
this.resize(sizeInVertices);
}
VertexArrayFacade._verifyAttributes = function (attributes) {
const attrs = [];
for (let i = 0; i < attributes.length; ++i) {
const attribute = attributes[i];
const attr = {
index: attribute.index ?? i,
enabled: attribute.enabled ?? true,
componentsPerAttribute: attribute.componentsPerAttribute,
componentDatatype: attribute.componentDatatype ?? ComponentDatatype.FLOAT,
normalize: attribute.normalize ?? false,
// There will be either a vertexBuffer or an [optional] usage.
vertexBuffer: attribute.vertexBuffer,
usage: attribute.usage ?? BufferUsage.STATIC_DRAW,
};
attrs.push(attr);
//>>includeStart('debug', pragmas.debug);
if (
attr.componentsPerAttribute !== 1 &&
attr.componentsPerAttribute !== 2 &&
attr.componentsPerAttribute !== 3 &&
attr.componentsPerAttribute !== 4
) {
throw new DeveloperError(
"attribute.componentsPerAttribute must be in the range [1, 4].",
);
}
const datatype = attr.componentDatatype;
if (!ComponentDatatype.validate(datatype)) {
throw new DeveloperError(
"Attribute must have a valid componentDatatype or not specify it.",
);
}
if (!BufferUsage.validate(attr.usage)) {
throw new DeveloperError(
"Attribute must have a valid usage or not specify it.",
);
}
//>>includeEnd('debug');
}
// Verify all attribute names are unique.
const uniqueIndices = new Array(attrs.length);
for (let j = 0; j < attrs.length; ++j) {
const currentAttr = attrs[j];
const index = currentAttr.index;
//>>includeStart('debug', pragmas.debug);
if (uniqueIndices[index]) {
throw new DeveloperError(
`Index ${index} is used by more than one attribute.`,
);
}
//>>includeEnd('debug');
uniqueIndices[index] = true;
}
return attrs;
};
VertexArrayFacade._vertexSizeInBytes = function (attributes) {
let sizeInBytes = 0;
const length = attributes.length;
for (let i = 0; i < length; ++i) {
const attribute = attributes[i];
sizeInBytes +=
attribute.componentsPerAttribute *
ComponentDatatype.getSizeInBytes(attribute.componentDatatype);
}
const maxComponentSizeInBytes =
length > 0
? ComponentDatatype.getSizeInBytes(attributes[0].componentDatatype)
: 0; // Sorted by size
const remainder =
maxComponentSizeInBytes > 0 ? sizeInBytes % maxComponentSizeInBytes : 0;
const padding = remainder === 0 ? 0 : maxComponentSizeInBytes - remainder;
sizeInBytes += padding;
return sizeInBytes;
};
VertexArrayFacade._createArrayViews = function (attributes, vertexSizeInBytes) {
const views = [];
let offsetInBytes = 0;
const length = attributes.length;
for (let i = 0; i < length; ++i) {
const attribute = attributes[i];
const componentDatatype = attribute.componentDatatype;
views.push({
index: attribute.index,
enabled: attribute.enabled,
componentsPerAttribute: attribute.componentsPerAttribute,
componentDatatype: componentDatatype,
normalize: attribute.normalize,
offsetInBytes: offsetInBytes,
vertexSizeInComponentType:
vertexSizeInBytes / ComponentDatatype.getSizeInBytes(componentDatatype),
view: undefined,
});
offsetInBytes +=
attribute.componentsPerAttribute *
ComponentDatatype.getSizeInBytes(componentDatatype);
}
return views;
};
/**
* Invalidates writers. Can't render again until commit is called.
*/
VertexArrayFacade.prototype.resize = function (sizeInVertices) {
this._size = sizeInVertices;
const allBuffers = this._allBuffers;
this.writers = [];
for (let i = 0, len = allBuffers.length; i < len; ++i) {
const buffer = allBuffers[i];
VertexArrayFacade._resize(buffer, this._size);
// Reserving invalidates the writers, so if client's cache them, they need to invalidate their cache.
VertexArrayFacade._appendWriters(this.writers, buffer);
}
// VAs are recreated next time commit is called.
destroyVA(this);
};
VertexArrayFacade._resize = function (buffer, size) {
Eif (buffer.vertexSizeInBytes > 0) {
// Create larger array buffer
const arrayBuffer = new ArrayBuffer(size * buffer.vertexSizeInBytes);
// Copy contents from previous array buffer
if (defined(buffer.arrayBuffer)) {
const destView = new Uint8Array(arrayBuffer);
const sourceView = new Uint8Array(buffer.arrayBuffer);
const sourceLength = sourceView.length;
for (let j = 0; j < sourceLength; ++j) {
destView[j] = sourceView[j];
}
}
// Create typed views into the new array buffer
const views = buffer.arrayViews;
const length = views.length;
for (let i = 0; i < length; ++i) {
const view = views[i];
view.view = ComponentDatatype.createArrayBufferView(
view.componentDatatype,
arrayBuffer,
view.offsetInBytes,
);
}
buffer.arrayBuffer = arrayBuffer;
}
};
const createWriters = [
// 1 component per attribute
function (buffer, view, vertexSizeInComponentType) {
return function (index, attribute) {
view[index * vertexSizeInComponentType] = attribute;
buffer.needsCommit = true;
};
},
// 2 component per attribute
function (buffer, view, vertexSizeInComponentType) {
return function (index, component0, component1) {
const i = index * vertexSizeInComponentType;
view[i] = component0;
view[i + 1] = component1;
buffer.needsCommit = true;
};
},
// 3 component per attribute
function (buffer, view, vertexSizeInComponentType) {
return function (index, component0, component1, component2) {
const i = index * vertexSizeInComponentType;
view[i] = component0;
view[i + 1] = component1;
view[i + 2] = component2;
buffer.needsCommit = true;
};
},
// 4 component per attribute
function (buffer, view, vertexSizeInComponentType) {
return function (index, component0, component1, component2, component3) {
const i = index * vertexSizeInComponentType;
view[i] = component0;
view[i + 1] = component1;
view[i + 2] = component2;
view[i + 3] = component3;
buffer.needsCommit = true;
};
},
];
VertexArrayFacade._appendWriters = function (writers, buffer) {
const arrayViews = buffer.arrayViews;
const length = arrayViews.length;
for (let i = 0; i < length; ++i) {
const arrayView = arrayViews[i];
writers[arrayView.index] = createWriters[
arrayView.componentsPerAttribute - 1
](buffer, arrayView.view, arrayView.vertexSizeInComponentType);
}
};
VertexArrayFacade.prototype.commit = function (indexBuffer) {
let recreateVA = false;
const allBuffers = this._allBuffers;
let buffer;
let i;
let length;
for (i = 0, length = allBuffers.length; i < length; ++i) {
buffer = allBuffers[i];
recreateVA = commit(this, buffer) || recreateVA;
}
///////////////////////////////////////////////////////////////////////
if (recreateVA || !defined(this.va)) {
destroyVA(this);
const va = (this.va = []);
const chunkSize = CesiumMath.SIXTY_FOUR_KILOBYTES - 4; // The 65535 index is reserved for primitive restart. Reserve the last 4 indices so that billboard quads are not broken up.
const numberOfVertexArrays =
defined(indexBuffer) && !this._instanced
? Math.ceil(this._size / chunkSize)
: 1;
for (let k = 0; k < numberOfVertexArrays; ++k) {
let attributes = [];
for (i = 0, length = allBuffers.length; i < length; ++i) {
buffer = allBuffers[i];
const offset = k * (buffer.vertexSizeInBytes * chunkSize);
VertexArrayFacade._appendAttributes(
attributes,
buffer,
offset,
this._instanced,
);
}
attributes = attributes.concat(this._precreated);
va.push({
va: new VertexArray({
context: this._context,
attributes: attributes,
indexBuffer: indexBuffer,
}),
indicesCount:
1.5 *
(k !== numberOfVertexArrays - 1 ? chunkSize : this._size % chunkSize),
// TODO: not hardcode 1.5, this assumes 6 indices per 4 vertices (as for Billboard quads).
});
}
}
};
function commit(vertexArrayFacade, buffer) {
if (buffer.needsCommit && buffer.vertexSizeInBytes > 0) {
buffer.needsCommit = false;
const vertexBuffer = buffer.vertexBuffer;
const vertexBufferSizeInBytes =
vertexArrayFacade._size * buffer.vertexSizeInBytes;
const vertexBufferDefined = defined(vertexBuffer);
if (
!vertexBufferDefined ||
vertexBuffer.sizeInBytes < vertexBufferSizeInBytes
) {
if (vertexBufferDefined) {
vertexBuffer.destroy();
}
buffer.vertexBuffer = Buffer.createVertexBuffer({
context: vertexArrayFacade._context,
typedArray: buffer.arrayBuffer,
usage: buffer.usage,
});
buffer.vertexBuffer.vertexArrayDestroyable = false;
return true; // Created new vertex buffer
}
buffer.vertexBuffer.copyFromArrayView(buffer.arrayBuffer);
}
return false; // Did not create new vertex buffer
}
VertexArrayFacade._appendAttributes = function (
attributes,
buffer,
vertexBufferOffset,
instanced,
) {
const arrayViews = buffer.arrayViews;
const length = arrayViews.length;
for (let i = 0; i < length; ++i) {
const view = arrayViews[i];
attributes.push({
index: view.index,
enabled: view.enabled,
componentsPerAttribute: view.componentsPerAttribute,
componentDatatype: view.componentDatatype,
normalize: view.normalize,
vertexBuffer: buffer.vertexBuffer,
offsetInBytes: vertexBufferOffset + view.offsetInBytes,
strideInBytes: buffer.vertexSizeInBytes,
instanceDivisor: instanced ? 1 : 0,
});
}
};
VertexArrayFacade.prototype.subCommit = function (
offsetInVertices,
lengthInVertices,
) {
//>>includeStart('debug', pragmas.debug);
if (offsetInVertices < 0 || offsetInVertices >= this._size) {
throw new DeveloperError(
"offsetInVertices must be greater than or equal to zero and less than the vertex array size.",
);
}
if (offsetInVertices + lengthInVertices > this._size) {
throw new DeveloperError(
"offsetInVertices + lengthInVertices cannot exceed the vertex array size.",
);
}
//>>includeEnd('debug');
const allBuffers = this._allBuffers;
for (let i = 0, len = allBuffers.length; i < len; ++i) {
subCommit(allBuffers[i], offsetInVertices, lengthInVertices);
}
};
function subCommit(buffer, offsetInVertices, lengthInVertices) {
if (buffer.needsCommit && buffer.vertexSizeInBytes > 0) {
const byteOffset = buffer.vertexSizeInBytes * offsetInVertices;
const byteLength = buffer.vertexSizeInBytes * lengthInVertices;
// PERFORMANCE_IDEA: If we want to get really crazy, we could consider updating
// individual attributes instead of the entire (sub-)vertex.
//
// PERFORMANCE_IDEA: Does creating the typed view add too much GC overhead?
buffer.vertexBuffer.copyFromArrayView(
new Uint8Array(buffer.arrayBuffer, byteOffset, byteLength),
byteOffset,
);
}
}
VertexArrayFacade.prototype.endSubCommits = function () {
const allBuffers = this._allBuffers;
for (let i = 0, len = allBuffers.length; i < len; ++i) {
allBuffers[i].needsCommit = false;
}
};
function destroyVA(vertexArrayFacade) {
const va = vertexArrayFacade.va;
if (!defined(va)) {
return;
}
const length = va.length;
for (let i = 0; i < length; ++i) {
va[i].va.destroy();
}
vertexArrayFacade.va = undefined;
}
VertexArrayFacade.prototype.isDestroyed = function () {
return false;
};
VertexArrayFacade.prototype.destroy = function () {
const allBuffers = this._allBuffers;
for (let i = 0, len = allBuffers.length; i < len; ++i) {
const buffer = allBuffers[i];
buffer.vertexBuffer = buffer.vertexBuffer && buffer.vertexBuffer.destroy();
}
destroyVA(this);
return destroyObject(this);
};
export default VertexArrayFacade;
|