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 | 15922x 15922x 15922x 4x 15918x 4x 15914x 15856x 15856x 15910x 4x 15906x 15906x 15906x 15906x 15906x 15906x 15906x 15852x 15906x 15902x 15902x 15902x 15902x 15902x 15902x 15902x 15902x 15902x 15902x 15902x 15902x 1x 2x 2x 2x 1x 12515x 12513x 1x 3414x 3412x 2x 3410x 1x 3409x 3409x 3409x 3409x 3399x 3399x 41249x 41243x 41244x 3399x 1x 21379x 23x 1x 202850x 1x 1x 1x 753x 753x 751x 749x 749x 749x 749x 749x 1x 2x 2x 1x 5x 5x 5x 5x 1x 10455x 1x 14597x 14597x | import Check from "../Core/Check.js";
import createGuid from "../Core/createGuid.js";
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import IndexDatatype from "../Core/IndexDatatype.js";
import WebGLConstants from "../Core/WebGLConstants.js";
import BufferUsage from "./BufferUsage.js";
/**
* @private
*/
function Buffer(options) {
options = options ?? Frozen.EMPTY_OBJECT;
//>>includeStart('debug', pragmas.debug);
Check.defined("options.context", options.context);
if (!defined(options.typedArray) && !defined(options.sizeInBytes)) {
throw new DeveloperError(
"Either options.sizeInBytes or options.typedArray is required.",
);
}
if (defined(options.typedArray) && defined(options.sizeInBytes)) {
throw new DeveloperError(
"Cannot pass in both options.sizeInBytes and options.typedArray.",
);
}
if (defined(options.typedArray)) {
Check.typeOf.object("options.typedArray", options.typedArray);
Check.typeOf.number(
"options.typedArray.byteLength",
options.typedArray.byteLength,
);
}
if (!BufferUsage.validate(options.usage)) {
throw new DeveloperError("usage is invalid.");
}
//>>includeEnd('debug');
const gl = options.context._gl;
const bufferTarget = options.bufferTarget;
const typedArray = options.typedArray;
let sizeInBytes = options.sizeInBytes;
const usage = options.usage;
const hasArray = defined(typedArray);
if (hasArray) {
sizeInBytes = typedArray.byteLength;
}
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number.greaterThan("sizeInBytes", sizeInBytes, 0);
//>>includeEnd('debug');
const buffer = gl.createBuffer();
gl.bindBuffer(bufferTarget, buffer);
gl.bufferData(bufferTarget, hasArray ? typedArray : sizeInBytes, usage);
gl.bindBuffer(bufferTarget, null);
this._id = createGuid();
this._gl = gl;
this._webgl2 = options.context._webgl2;
this._bufferTarget = bufferTarget;
this._sizeInBytes = sizeInBytes;
this._usage = usage;
this._buffer = buffer;
this.vertexArrayDestroyable = true;
}
Buffer.createPixelBuffer = function (options) {
//>>includeStart('debug', pragmas.debug);
Check.defined("options.context", options.context);
//>>includeEnd('debug');
Eif (!options.context._webgl2) {
throw new DeveloperError(
"A WebGL 2 context is required to create PixelBuffers.",
);
}
return new Buffer({
context: options.context,
bufferTarget: WebGLConstants.PIXEL_PACK_BUFFER,
typedArray: options.typedArray,
sizeInBytes: options.sizeInBytes,
usage: options.usage,
});
};
/**
* Creates a vertex buffer, which contains untyped vertex data in GPU-controlled memory.
* <br /><br />
* A vertex array defines the actual makeup of a vertex, e.g., positions, normals, texture coordinates,
* etc., by interpreting the raw data in one or more vertex buffers.
*
* @param {object} options An object containing the following properties:
* @param {Context} options.context The context in which to create the buffer
* @param {ArrayBufferView} [options.typedArray] A typed array containing the data to copy to the buffer.
* @param {number} [options.sizeInBytes] A <code>Number</code> defining the size of the buffer in bytes. Required if options.typedArray is not given.
* @param {BufferUsage} options.usage Specifies the expected usage pattern of the buffer. On some GL implementations, this can significantly affect performance. See {@link BufferUsage}.
* @returns {VertexBuffer} The vertex buffer, ready to be attached to a vertex array.
*
* @exception {DeveloperError} Must specify either <options.typedArray> or <options.sizeInBytes>, but not both.
* @exception {DeveloperError} The buffer size must be greater than zero.
* @exception {DeveloperError} Invalid <code>usage</code>.
*
*
* @example
* // Example 1. Create a dynamic vertex buffer 16 bytes in size.
* const buffer = Buffer.createVertexBuffer({
* context : context,
* sizeInBytes : 16,
* usage : BufferUsage.DYNAMIC_DRAW
* });
*
* @example
* // Example 2. Create a dynamic vertex buffer from three floating-point values.
* // The data copied to the vertex buffer is considered raw bytes until it is
* // interpreted as vertices using a vertex array.
* const positionBuffer = buffer.createVertexBuffer({
* context : context,
* typedArray : new Float32Array([0, 0, 0]),
* usage : BufferUsage.STATIC_DRAW
* });
*
* @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGenBuffer.xml|glGenBuffer}
* @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindBuffer.xml|glBindBuffer} with <code>ARRAY_BUFFER</code>
* @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBufferData.xml|glBufferData} with <code>ARRAY_BUFFER</code>
*/
Buffer.createVertexBuffer = function (options) {
//>>includeStart('debug', pragmas.debug);
Check.defined("options.context", options.context);
//>>includeEnd('debug');
return new Buffer({
context: options.context,
bufferTarget: WebGLConstants.ARRAY_BUFFER,
typedArray: options.typedArray,
sizeInBytes: options.sizeInBytes,
usage: options.usage,
});
};
/**
* Creates an index buffer, which contains typed indices in GPU-controlled memory.
* <br /><br />
* An index buffer can be attached to a vertex array to select vertices for rendering.
* <code>Context.draw</code> can render using the entire index buffer or a subset
* of the index buffer defined by an offset and count.
*
* @param {object} options An object containing the following properties:
* @param {Context} options.context The context in which to create the buffer
* @param {ArrayBufferView} [options.typedArray] A typed array containing the data to copy to the buffer.
* @param {number} [options.sizeInBytes] A <code>Number</code> defining the size of the buffer in bytes. Required if options.typedArray is not given.
* @param {BufferUsage} options.usage Specifies the expected usage pattern of the buffer. On some GL implementations, this can significantly affect performance. See {@link BufferUsage}.
* @param {IndexDatatype} options.indexDatatype The datatype of indices in the buffer.
* @returns {IndexBuffer} The index buffer, ready to be attached to a vertex array.
*
* @exception {DeveloperError} Must specify either <options.typedArray> or <options.sizeInBytes>, but not both.
* @exception {DeveloperError} IndexDatatype.UNSIGNED_INT requires OES_element_index_uint, which is not supported on this system. Check context.elementIndexUint.
* @exception {DeveloperError} The size in bytes must be greater than zero.
* @exception {DeveloperError} Invalid <code>usage</code>.
* @exception {DeveloperError} Invalid <code>indexDatatype</code>.
*
*
* @example
* // Example 1. Create a stream index buffer of unsigned shorts that is
* // 16 bytes in size.
* const buffer = Buffer.createIndexBuffer({
* context : context,
* sizeInBytes : 16,
* usage : BufferUsage.STREAM_DRAW,
* indexDatatype : IndexDatatype.UNSIGNED_SHORT
* });
*
* @example
* // Example 2. Create a static index buffer containing three unsigned shorts.
* const buffer = Buffer.createIndexBuffer({
* context : context,
* typedArray : new Uint16Array([0, 1, 2]),
* usage : BufferUsage.STATIC_DRAW,
* indexDatatype : IndexDatatype.UNSIGNED_SHORT
* });
*
* @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGenBuffer.xml|glGenBuffer}
* @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindBuffer.xml|glBindBuffer} with <code>ELEMENT_ARRAY_BUFFER</code>
* @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBufferData.xml|glBufferData} with <code>ELEMENT_ARRAY_BUFFER</code>
*/
Buffer.createIndexBuffer = function (options) {
//>>includeStart('debug', pragmas.debug);
Check.defined("options.context", options.context);
if (!IndexDatatype.validate(options.indexDatatype)) {
throw new DeveloperError("Invalid indexDatatype.");
}
if (
options.indexDatatype === IndexDatatype.UNSIGNED_INT &&
!options.context.elementIndexUint
) {
throw new DeveloperError(
"IndexDatatype.UNSIGNED_INT requires OES_element_index_uint, which is not supported on this system. Check context.elementIndexUint.",
);
}
//>>includeEnd('debug');
const context = options.context;
const indexDatatype = options.indexDatatype;
const bytesPerIndex = IndexDatatype.getSizeInBytes(indexDatatype);
const buffer = new Buffer({
context: context,
bufferTarget: WebGLConstants.ELEMENT_ARRAY_BUFFER,
typedArray: options.typedArray,
sizeInBytes: options.sizeInBytes,
usage: options.usage,
});
const numberOfIndices = buffer.sizeInBytes / bytesPerIndex;
Object.defineProperties(buffer, {
indexDatatype: {
get: function () {
return indexDatatype;
},
},
bytesPerIndex: {
get: function () {
return bytesPerIndex;
},
},
numberOfIndices: {
get: function () {
return numberOfIndices;
},
},
});
return buffer;
};
Object.defineProperties(Buffer.prototype, {
sizeInBytes: {
get: function () {
return this._sizeInBytes;
},
},
usage: {
get: function () {
return this._usage;
},
},
});
Buffer.prototype._getBuffer = function () {
return this._buffer;
};
Buffer.prototype._bind = function () {
const gl = this._gl;
const target = this._bufferTarget;
gl.bindBuffer(target, this._buffer);
};
Buffer.prototype._unBind = function () {
const gl = this._gl;
const target = this._bufferTarget;
gl.bindBuffer(target, null);
};
Buffer.prototype.copyFromArrayView = function (arrayView, offsetInBytes) {
offsetInBytes = offsetInBytes ?? 0;
//>>includeStart('debug', pragmas.debug);
Check.defined("arrayView", arrayView);
Check.typeOf.number.lessThanOrEquals(
"offsetInBytes + arrayView.byteLength",
offsetInBytes + arrayView.byteLength,
this._sizeInBytes,
);
//>>includeEnd('debug');
const gl = this._gl;
const target = this._bufferTarget;
gl.bindBuffer(target, this._buffer);
gl.bufferSubData(target, offsetInBytes, arrayView);
gl.bindBuffer(target, null);
};
Buffer.prototype.copyFromBuffer = function (
readBuffer,
readOffset,
writeOffset,
sizeInBytes,
) {
//>>includeStart('debug', pragmas.debug);
Eif (!this._webgl2) {
throw new DeveloperError("A WebGL 2 context is required.");
}
if (!defined(readBuffer)) {
throw new DeveloperError("readBuffer must be defined.");
}
if (!defined(sizeInBytes) || sizeInBytes <= 0) {
throw new DeveloperError(
"sizeInBytes must be defined and be greater than zero.",
);
}
if (
!defined(readOffset) ||
readOffset < 0 ||
readOffset + sizeInBytes > readBuffer._sizeInBytes
) {
throw new DeveloperError(
"readOffset must be greater than or equal to zero and readOffset + sizeInBytes must be less than of equal to readBuffer.sizeInBytes.",
);
}
if (
!defined(writeOffset) ||
writeOffset < 0 ||
writeOffset + sizeInBytes > this._sizeInBytes
) {
throw new DeveloperError(
"writeOffset must be greater than or equal to zero and writeOffset + sizeInBytes must be less than of equal to this.sizeInBytes.",
);
}
if (
this._buffer === readBuffer._buffer &&
((writeOffset >= readOffset && writeOffset < readOffset + sizeInBytes) ||
(readOffset > writeOffset && readOffset < writeOffset + sizeInBytes))
) {
throw new DeveloperError(
"When readBuffer is equal to this, the ranges [readOffset + sizeInBytes) and [writeOffset, writeOffset + sizeInBytes) must not overlap.",
);
}
if (
(this._bufferTarget === WebGLConstants.ELEMENT_ARRAY_BUFFER &&
readBuffer._bufferTarget !== WebGLConstants.ELEMENT_ARRAY_BUFFER) ||
(this._bufferTarget !== WebGLConstants.ELEMENT_ARRAY_BUFFER &&
readBuffer._bufferTarget === WebGLConstants.ELEMENT_ARRAY_BUFFER)
) {
throw new DeveloperError(
"Can not copy an index buffer into another buffer type.",
);
}
//>>includeEnd('debug');
const readTarget = WebGLConstants.COPY_READ_BUFFER;
const writeTarget = WebGLConstants.COPY_WRITE_BUFFER;
const gl = this._gl;
gl.bindBuffer(writeTarget, this._buffer);
gl.bindBuffer(readTarget, readBuffer._buffer);
gl.copyBufferSubData(
readTarget,
writeTarget,
readOffset,
writeOffset,
sizeInBytes,
);
gl.bindBuffer(writeTarget, null);
gl.bindBuffer(readTarget, null);
};
Buffer.prototype.getBufferData = function (
arrayView,
sourceOffset,
destinationOffset,
length,
) {
sourceOffset = sourceOffset ?? 0;
destinationOffset = destinationOffset ?? 0;
//>>includeStart('debug', pragmas.debug);
Eif (!this._webgl2) {
throw new DeveloperError("A WebGL 2 context is required.");
}
if (!defined(arrayView)) {
throw new DeveloperError("arrayView is required.");
}
let copyLength;
let elementSize;
let arrayLength = arrayView.byteLength;
if (!defined(length)) {
if (defined(arrayLength)) {
copyLength = arrayLength - destinationOffset;
elementSize = 1;
} else {
arrayLength = arrayView.length;
copyLength = arrayLength - destinationOffset;
elementSize = arrayView.BYTES_PER_ELEMENT;
}
} else {
copyLength = length;
if (defined(arrayLength)) {
elementSize = 1;
} else {
arrayLength = arrayView.length;
elementSize = arrayView.BYTES_PER_ELEMENT;
}
}
if (destinationOffset < 0 || destinationOffset > arrayLength) {
throw new DeveloperError(
"destinationOffset must be greater than zero and less than the arrayView length.",
);
}
if (destinationOffset + copyLength > arrayLength) {
throw new DeveloperError(
"destinationOffset + length must be less than or equal to the arrayViewLength.",
);
}
if (sourceOffset < 0 || sourceOffset > this._sizeInBytes) {
throw new DeveloperError(
"sourceOffset must be greater than zero and less than the buffers size.",
);
}
if (sourceOffset + copyLength * elementSize > this._sizeInBytes) {
throw new DeveloperError(
"sourceOffset + length must be less than the buffers size.",
);
}
//>>includeEnd('debug');
const gl = this._gl;
const target = WebGLConstants.COPY_READ_BUFFER;
gl.bindBuffer(target, this._buffer);
gl.getBufferSubData(
target,
sourceOffset,
arrayView,
destinationOffset,
length,
);
gl.bindBuffer(target, null);
};
Buffer.prototype.isDestroyed = function () {
return false;
};
Buffer.prototype.destroy = function () {
this._gl.deleteBuffer(this._buffer);
return destroyObject(this);
};
export default Buffer;
|