/
Buffer.js
141 lines (128 loc) · 4.04 KB
/
Buffer.js
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
/*global define*/
define([
'../Core/DeveloperError',
'../Core/destroyObject'
], function(
DeveloperError,
destroyObject) {
"use strict";
/**
* DOC_TBA
*
* @alias Buffer
* @internalConstructor
*
* @see Context#createVertexBuffer
* @see Context#createIndexBuffer
*/
var Buffer = function(gl, bufferTarget, sizeInBytes, usage, buffer) {
this._gl = gl;
this._bufferTarget = bufferTarget;
this._sizeInBytes = sizeInBytes;
this._usage = usage;
this._buffer = buffer;
this._vertexArrayDestroyable = true;
};
/**
* DOC_TBA
* DOC_TBA: arrayView
*
* @memberof Buffer
* @param {Number} [offsetInBytes=0] DOC_TBA
*
* @exception {DeveloperError} arrayView is required.
* @exception {DeveloperError} This buffer is not large enough.
* @exception {DeveloperError} This buffer was destroyed, i.e., destroy() was called.
*/
Buffer.prototype.copyFromArrayView = function(arrayView, offsetInBytes) {
if (!arrayView) {
throw new DeveloperError('arrayView is required.');
}
offsetInBytes = offsetInBytes || 0;
if (offsetInBytes + arrayView.byteLength > this._sizeInBytes) {
throw new DeveloperError('This buffer is not large enough.');
}
var gl = this._gl;
var target = this._bufferTarget;
gl.bindBuffer(target, this._buffer);
gl.bufferSubData(target, offsetInBytes, arrayView);
gl.bindBuffer(target, null);
};
Buffer.prototype._getBuffer = function() {
return this._buffer;
};
/**
* DOC_TBA
* @memberof Buffer
*
* @returns {Number} DOC_TBA
* @exception {DeveloperError} This buffer was destroyed, i.e., destroy() was called.
*/
Buffer.prototype.getSizeInBytes = function() {
return this._sizeInBytes;
};
/**
* DOC_TBA
* @memberof Buffer
*
* @returns {GLenum} DOC_TBA
* @exception {DeveloperError} This buffer was destroyed, i.e., destroy() was called.
*/
Buffer.prototype.getUsage = function() {
return this._usage;
};
/**
* DOC_TBA
* @memberof Buffer
*/
Buffer.prototype.getVertexArrayDestroyable = function() {
return this._vertexArrayDestroyable;
};
/**
* DOC_TBA
* @memberof Buffer
*/
Buffer.prototype.setVertexArrayDestroyable = function(value) {
this._vertexArrayDestroyable = value;
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
* If this object was destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
*
* @memberof Buffer
*
* @returns {Boolean} True if this object was destroyed; otherwise, false.
*
* @see Buffer#destroy
*/
Buffer.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @memberof Buffer
*
* @returns {undefined}
*
* @exception {DeveloperError} This buffer was destroyed, i.e., destroy() was called.
*
* @see Buffer#isDestroyed
* @see <a href='http://www.khronos.org/opengles/sdk/2.0/docs/man/glDeleteBuffers.xml'>glDeleteBuffers</a>
*
* @example
* buffer = buffer && buffer.destroy();
*/
Buffer.prototype.destroy = function() {
this._gl.deleteBuffer(this._buffer);
return destroyObject(this);
};
return Buffer;
});