Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuffer.js
More file actions
Latest commit
152 lines (139 loc) · 4.42 KB
/
Copy pathbuffer.js
File metadata and controls
152 lines (139 loc) · 4.42 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
"use strict"
varpool=require("typedarray-pool")
varops=require("ndarray-ops")
varndarray=require("ndarray")
varSUPPORTED_TYPES=[
"uint8",
"uint8_clamped",
"uint16",
"uint32",
"int8",
"int16",
"int32",
"float32"]
functionGLBuffer(gl,type,handle,length,usage){
this.gl=gl
this.type=type
this.handle=handle
this.length=length
this.usage=usage
}
varproto=GLBuffer.prototype
proto.bind=function(){
this.gl.bindBuffer(this.type,this.handle)
}
proto.unbind=function(){
this.gl.bindBuffer(this.type,null)
}
proto.dispose=function(){
this.gl.deleteBuffer(this.handle)
}
functionupdateTypeArray(gl,type,len,usage,data,offset){
vardataLen=data.length*data.BYTES_PER_ELEMENT
if(offset<0){
gl.bufferData(type,data,usage)
returndataLen
}
if(dataLen+offset>len){
thrownewError("gl-buffer: If resizing buffer, must not specify offset")
}
gl.bufferSubData(type,offset,data)
returnlen
}
functionmakeScratchTypeArray(array,dtype){
varres=pool.malloc(array.length,dtype)
varn=array.length
for(vari=0;i<n;++i){
res[i]=array[i]
}
returnres
}
functionisPacked(shape,stride){
varn=1
for(vari=stride.length-1;i>=0;--i){
if(stride[i]!==n){
returnfalse
}
n*=shape[i]
}
returntrue
}
proto.update=function(array,offset){
if(typeofoffset!=="number"){
offset=-1
}
this.bind()
if(typeofarray==="object"&&typeofarray.shape!=="undefined"){//ndarray
vardtype=array.dtype
if(SUPPORTED_TYPES.indexOf(dtype)<0){
dtype="float32"
}
if(this.type===this.gl.ELEMENT_ARRAY_BUFFER){
varext=gl.getExtension('OES_element_index_uint')
if(ext&&dtype!=="uint16"){
dtype="uint32"
}else{
dtype="uint16"
}
}
if(dtype===array.dtype&&isPacked(array.shape,array.stride)){
if(array.offset===0&&array.data.length===array.shape[0]){
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,array.data,offset)
}else{
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,array.data.subarray(array.offset,array.shape[0]),offset)
}
}else{
vartmp=pool.malloc(array.size,dtype)
varndt=ndarray(tmp,array.shape)
ops.assign(ndt,array)
if(offset<0){
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,tmp,offset)
}else{
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,tmp.subarray(0,array.size),offset)
}
pool.free(tmp)
}
}elseif(Array.isArray(array)){//Vanilla array
vart
if(this.type===this.gl.ELEMENT_ARRAY_BUFFER){
t=makeScratchTypeArray(array,"uint16")
}else{
t=makeScratchTypeArray(array,"float32")
}
if(offset<0){
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,t,offset)
}else{
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,t.subarray(0,array.length),offset)
}
pool.free(t)
}elseif(typeofarray==="object"&&typeofarray.length==="number"){//Typed array
this.length=updateTypeArray(this.gl,this.type,this.length,this.usage,array,offset)
}elseif(typeofarray==="number"||array===undefined){//Number/default
if(offset>=0){
thrownewError("gl-buffer: Cannot specify offset when resizing buffer")
}
array=array|0
if(array<=0){
array=1
}
this.gl.bufferData(this.type,array|0,this.usage)
this.length=array
}else{//Error, case should not happen
thrownewError("gl-buffer: Invalid data type")
}
}
functioncreateBuffer(gl,data,type,usage){
type=type||gl.ARRAY_BUFFER
usage=usage||gl.DYNAMIC_DRAW
if(type!==gl.ARRAY_BUFFER&&type!==gl.ELEMENT_ARRAY_BUFFER){
thrownewError("gl-buffer: Invalid type for webgl buffer, must be either gl.ARRAY_BUFFER or gl.ELEMENT_ARRAY_BUFFER")
}
if(usage!==gl.DYNAMIC_DRAW&&usage!==gl.STATIC_DRAW&&usage!==gl.STREAM_DRAW){
thrownewError("gl-buffer: Invalid usage for buffer, must be either gl.DYNAMIC_DRAW, gl.STATIC_DRAW or gl.STREAM_DRAW")
}
varhandle=gl.createBuffer()
varresult=newGLBuffer(gl,type,handle,0,usage)
result.update(data)
returnresult
}
module.exports=createBuffer