forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-buffer.js
More file actions
170 lines (148 loc) · 5.82 KB
/
index-buffer.js
File metadata and controls
170 lines (148 loc) · 5.82 KB
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
pc.extend(pc, function () {
'use strict';
/**
* @name pc.IndexBuffer
* @class An index buffer is the mechanism via which the application specifies primitive
* index data to the graphics hardware.
* @description Creates a new index buffer.
* @example
* // Create an index buffer holding 3 16-bit indices
* // The buffer is marked as static, hinting that the buffer will never be modified
* var indexBuffer = new pc.IndexBuffer(graphicsDevice, pc.INDEXFORMAT_UINT16, 3, pc.BUFFER_STATIC);
* @param {pc.GraphicsDevice} graphicsDevice The graphics device used to manage this index buffer.
* @param {Number} format The type of each index to be stored in the index buffer (see pc.INDEXFORMAT_*).
* @param {Number} numIndices The number of indices to be stored in the index buffer.
* @param {Number} [usage] The usage type of the vertex buffer (see pc.BUFFER_*).
* @param {ArrayBuffer} [initialData] Initial data.
*/
var IndexBuffer = function (graphicsDevice, format, numIndices, usage, initialData) {
// Initialize optional parameters
// By default, index buffers are static (better for performance since buffer data can be cached in VRAM)
this.usage = usage || pc.BUFFER_STATIC;
// Store the index format
this.format = format;
// Store the number of indices
this.numIndices = numIndices;
// Create the WebGL buffer
this.device = graphicsDevice;
var gl = this.device.gl;
// Allocate the storage
var bytesPerIndex;
if (format === pc.INDEXFORMAT_UINT8) {
bytesPerIndex = 1;
this.glFormat = gl.UNSIGNED_BYTE;
} else if (format === pc.INDEXFORMAT_UINT16) {
bytesPerIndex = 2;
this.glFormat = gl.UNSIGNED_SHORT;
} else if (format === pc.INDEXFORMAT_UINT32) {
bytesPerIndex = 4;
this.glFormat = gl.UNSIGNED_INT;
}
this.bytesPerIndex = bytesPerIndex;
this.numBytes = this.numIndices * bytesPerIndex;
if (initialData) {
this.setData(initialData);
} else {
this.storage = new ArrayBuffer(this.numBytes);
}
graphicsDevice._vram.ib += this.numBytes;
this.device.buffers.push(this);
};
IndexBuffer.prototype = {
/**
* @function
* @name pc.IndexBuffer#destroy
* @description Frees resources associated with this index buffer.
*/
destroy: function () {
var device = this.device;
var idx = device.buffers.indexOf(this);
if (idx !== -1) {
device.buffers.splice(idx, 1);
}
if (this.bufferId) {
var gl = this.device.gl;
gl.deleteBuffer(this.bufferId);
this.device._vram.ib -= this.storage.byteLength;
this.bufferId = null;
if (this.device.indexBuffer === this) {
this.device.indexBuffer = null;
}
}
},
/**
* @function
* @name pc.IndexBuffer#getFormat
* @description Returns the data format of the specified index buffer.
* @returns {Number} The data format of the specified index buffer (see pc.INDEXFORMAT_*).
*/
getFormat: function () {
return this.format;
},
/**
* @function
* @name pc.IndexBuffer#getNumIndices
* @description Returns the number of indices stored in the specified index buffer.
* @returns {Number} The number of indices stored in the specified index buffer.
*/
getNumIndices: function () {
return this.numIndices;
},
/**
* @function
* @name pc.IndexBuffer#lock
* @description Gives access to the block of memory that stores the buffer's indices.
* @returns {ArrayBuffer} A contiguous block of memory where index data can be written to.
*/
lock: function () {
return this.storage;
},
/**
* @function
* @name pc.IndexBuffer#unlock
* @description Signals that the block of memory returned by a call to the lock function is
* ready to be given to the graphics hardware. Only unlocked index buffers can be set on the
* currently active device.
*/
unlock: function () {
// Upload the new index data
var gl = this.device.gl;
if (!this.bufferId) {
this.bufferId = gl.createBuffer();
}
var glUsage;
switch (this.usage) {
case pc.BUFFER_STATIC:
glUsage = gl.STATIC_DRAW;
break;
case pc.BUFFER_DYNAMIC:
glUsage = gl.DYNAMIC_DRAW;
break;
case pc.BUFFER_STREAM:
glUsage = gl.STREAM_DRAW;
break;
case pc.BUFFER_GPUDYNAMIC:
if (this.device.webgl2) {
glUsage = gl.DYNAMIC_COPY;
} else {
glUsage = gl.STATIC_DRAW;
}
break;
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.bufferId);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.storage, glUsage);
},
setData: function (data) {
if (data.byteLength !== this.numBytes) {
console.error("IndexBuffer: wrong initial data size: expected " + this.numBytes + ", got " + data.byteLength);
return false;
}
this.storage = data;
this.unlock();
return true;
}
};
return {
IndexBuffer: IndexBuffer
};
}());