123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- import Check from "../Core/Check.js";
- import defaultValue from "../Core/defaultValue.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";
- function Buffer(options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
-
- 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.");
- }
-
- var gl = options.context._gl;
- var bufferTarget = options.bufferTarget;
- var typedArray = options.typedArray;
- var sizeInBytes = options.sizeInBytes;
- var usage = options.usage;
- var hasArray = defined(typedArray);
- if (hasArray) {
- sizeInBytes = typedArray.byteLength;
- }
-
- Check.typeOf.number.greaterThan("sizeInBytes", sizeInBytes, 0);
-
- var buffer = gl.createBuffer();
- gl.bindBuffer(bufferTarget, buffer);
- gl.bufferData(bufferTarget, hasArray ? typedArray : sizeInBytes, usage);
- gl.bindBuffer(bufferTarget, null);
- this._gl = gl;
- this._webgl2 = options.context._webgl2;
- this._bufferTarget = bufferTarget;
- this._sizeInBytes = sizeInBytes;
- this._usage = usage;
- this._buffer = buffer;
- this.vertexArrayDestroyable = true;
- }
- Buffer.createVertexBuffer = function (options) {
-
- Check.defined("options.context", options.context);
-
- return new Buffer({
- context: options.context,
- bufferTarget: WebGLConstants.ARRAY_BUFFER,
- typedArray: options.typedArray,
- sizeInBytes: options.sizeInBytes,
- usage: options.usage,
- });
- };
- Buffer.createIndexBuffer = function (options) {
-
- 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."
- );
- }
-
- var context = options.context;
- var indexDatatype = options.indexDatatype;
- var bytesPerIndex = IndexDatatype.getSizeInBytes(indexDatatype);
- var buffer = new Buffer({
- context: context,
- bufferTarget: WebGLConstants.ELEMENT_ARRAY_BUFFER,
- typedArray: options.typedArray,
- sizeInBytes: options.sizeInBytes,
- usage: options.usage,
- });
- var 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.copyFromArrayView = function (arrayView, offsetInBytes) {
- offsetInBytes = defaultValue(offsetInBytes, 0);
-
- Check.defined("arrayView", arrayView);
- Check.typeOf.number.lessThanOrEquals(
- "offsetInBytes + arrayView.byteLength",
- offsetInBytes + arrayView.byteLength,
- this._sizeInBytes
- );
-
- var gl = this._gl;
- var 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
- ) {
-
- if (!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."
- );
- }
-
- var readTarget = WebGLConstants.COPY_READ_BUFFER;
- var writeTarget = WebGLConstants.COPY_WRITE_BUFFER;
- var 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 = defaultValue(sourceOffset, 0);
- destinationOffset = defaultValue(destinationOffset, 0);
-
- if (!this._webgl2) {
- throw new DeveloperError("A WebGL 2 context is required.");
- }
- if (!defined(arrayView)) {
- throw new DeveloperError("arrayView is required.");
- }
- var copyLength;
- var elementSize;
- var 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."
- );
- }
-
- var gl = this._gl;
- var 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;
|