Skip to content

Commit 777fd21

Browse files
author
David Gillen
committed
now fill and use arrays specified attribute configuration
1 parent 0128d2f commit 777fd21

File tree

1 file changed

+109
-86
lines changed

1 file changed

+109
-86
lines changed

src/easeljs/display/StageGL.js

Lines changed: 109 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -302,42 +302,6 @@ this.createjs = this.createjs||{};
302302
*/
303303
this._mainShader = null;
304304

305-
/**
306-
* The vertex position data for the current draw call.
307-
* @property _vertices
308-
* @protected
309-
* @type {Float32Array}
310-
* @default null
311-
*/
312-
this._vertices = null;
313-
314-
/**
315-
* The vertex UV data for the current draw call.
316-
* @property _uvs
317-
* @protected
318-
* @type {Float32Array}
319-
* @default null
320-
*/
321-
this._uvs = null;
322-
323-
/**
324-
* The vertex indices data for the current draw call.
325-
* @property _indices
326-
* @protected
327-
* @type {Float32Array}
328-
* @default null
329-
*/
330-
this._indices = null;
331-
332-
/**
333-
* The vertices data for the current draw call.
334-
* @property _alphas
335-
* @protected
336-
* @type {Float32Array}
337-
* @default null
338-
*/
339-
this._alphas = null;
340-
341305
/**
342306
* All the different vertex attribute sets that can be used with the render buffer. Currently only internal,
343307
* if/when alternate main shaders are possible, they'll register themselves here.
@@ -1127,7 +1091,7 @@ this.createjs = this.createjs||{};
11271091
},
11281092
"destination-atop": {
11291093
shader: (StageGL.BLEND_FRAGMENT_SIMPLE +
1130-
"gl_FragColor = vec4(dst.rgb * src.a + src.rgb * (1.0 - dst.a), src.a);" +
1094+
"gl_FragColor = vec4(dst.rgb * src.a + src.rgb * (1.0 - dst.a), src.a);" +
11311095
"}")
11321096
},
11331097
"destination-atop_cheap": {
@@ -2113,57 +2077,85 @@ this.createjs = this.createjs||{};
21132077

21142078
// TODO benchmark and test using unified main buffer
21152079

2080+
// regular
21162081
config = this._attributeConfig["default"] = {};
21172082

2118-
// the actual position information
21192083
groupSize = 2;
21202084
var vertices = new Float32Array(groupCount * groupSize);
2121-
for (i=0, l=vertices.length; i<l; i+=groupSize) { vertices[i] = vertices[i+1] = 0; }
2085+
for (i=0, l=vertices.length; i<l; i+=groupSize) { vertices[i] = vertices[i+1] = 0.0; }
21222086
atrBuffer = gl.createBuffer();
21232087
gl.bindBuffer(gl.ARRAY_BUFFER, atrBuffer);
21242088
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);
21252089
config["position"] = { array: vertices,
2126-
buffer: atrBuffer, type: gl.FLOAT, size: groupSize, spacing: groupSize, stride: 0, offset: 0
2090+
buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: 0, offset: 0, offB: 0, size: groupSize
21272091
};
21282092

2129-
// where on the texture it gets its information
21302093
groupSize = 2;
21312094
var uvs = new Float32Array(groupCount * groupSize);
2132-
for (i=0, l=uvs.length; i<l; i+=groupSize) { uvs[i] = uvs[i+1] = 0; }
2095+
for (i=0, l=uvs.length; i<l; i+=groupSize) { uvs[i] = uvs[i+1] = 0.0; }
21332096
atrBuffer = gl.createBuffer();
21342097
gl.bindBuffer(gl.ARRAY_BUFFER, atrBuffer);
21352098
gl.bufferData(gl.ARRAY_BUFFER, uvs, gl.DYNAMIC_DRAW);
21362099
config["uv"] = { array: uvs,
2137-
buffer: atrBuffer, type: gl.FLOAT, size: groupSize, spacing: groupSize, stride: 0, offset: 0
2100+
buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: 0, offset: 0, offB: 0, size: groupSize
21382101
};
21392102

2140-
// what texture it should use
21412103
groupSize = 1;
21422104
var indices = new Float32Array(groupCount * groupSize);
2143-
for (i=0, l=indices.length; i<l; i++) { indices[i] = 0; }
2105+
for (i=0, l=indices.length; i<l; i++) { indices[i] = 0.0; }
21442106
atrBuffer = gl.createBuffer();
21452107
gl.bindBuffer(gl.ARRAY_BUFFER, atrBuffer);
21462108
gl.bufferData(gl.ARRAY_BUFFER, indices, gl.DYNAMIC_DRAW);
21472109
config["texture"] = { array: indices,
2148-
buffer: atrBuffer, type: gl.FLOAT, size: groupSize, spacing: groupSize, stride: 0, offset: 0
2110+
buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: 0, offset: 0, offB: 0, size: groupSize
21492111
};
21502112

2151-
// what alpha it should have
21522113
groupSize = 1;
21532114
var alphas = new Float32Array(groupCount * groupSize);
21542115
for (i=0, l=alphas.length; i<l; i++) { alphas[i] = 1.0; }
21552116
atrBuffer = gl.createBuffer();
21562117
gl.bindBuffer(gl.ARRAY_BUFFER, atrBuffer);
21572118
gl.bufferData(gl.ARRAY_BUFFER, alphas, gl.DYNAMIC_DRAW);
21582119
config["alpha"] = { array: alphas,
2159-
buffer: atrBuffer, type: gl.FLOAT, size: groupSize, spacing: groupSize, stride: 0, offset: 0
2120+
buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: 0, offset: 0, offB: 0, size: groupSize
21602121
};
21612122

2162-
this._activeConfig = config;
2163-
this._vertices = vertices;
2164-
this._indices = indices;
2165-
this._alphas = alphas;
2166-
this._uvs = uvs;
2123+
// micro
2124+
config = this._attributeConfig["micro"] = {};
2125+
groupCount = 5; // we probably do not need this much space, but it's safer and barely more expensive
2126+
groupSize = 2 + 2 + 1 + 1;
2127+
var stride = groupSize * 4; // they're all floats, so 4 bytes each
2128+
2129+
var microArray = new Float32Array(groupCount * groupSize);
2130+
for (i=0, l=microArray.length; i<l; i+=groupSize) {
2131+
microArray[i] = microArray[i+1] = 0.0; // vertex
2132+
microArray[i+1] = microArray[i+2] = 0.0; // uv
2133+
microArray[i+3] = 0.0; // texture
2134+
microArray[i+4] = 1.0; // alpha
2135+
}
2136+
atrBuffer = gl.createBuffer();
2137+
gl.bindBuffer(gl.ARRAY_BUFFER, atrBuffer);
2138+
gl.bufferData(gl.ARRAY_BUFFER, microArray, gl.DYNAMIC_DRAW);
2139+
2140+
config["position"] = {
2141+
array: microArray, buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: stride,
2142+
offset: 0, offB: 0, size: 2
2143+
};
2144+
config["uv"] = {
2145+
array: microArray, buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: stride,
2146+
offset: 2, offB: 2*4, size: 2
2147+
};
2148+
config["texture"] = {
2149+
array: microArray, buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: stride,
2150+
offset: 4, offB: 4*4, size: 1
2151+
};
2152+
config["alpha"] = {
2153+
array: microArray, buffer: atrBuffer, type: gl.FLOAT, spacing: groupSize, stride: stride,
2154+
offset: 5, offB: 5*4, size: 1
2155+
};
2156+
2157+
// defaults
2158+
this._activeConfig = this._attributeConfig["default"];
21672159
};
21682160

21692161
/**
@@ -2702,11 +2694,6 @@ this.createjs = this.createjs||{};
27022694
}
27032695
if (!image) { continue; }
27042696

2705-
var uvs = this._uvs;
2706-
var vertices = this._vertices;
2707-
var texI = this._indices;
2708-
var alphas = this._alphas;
2709-
27102697
// calculate texture
27112698
if (image._storeID === undefined) {
27122699
// this texture is new to us so load it and add it to the batch
@@ -2772,36 +2759,72 @@ this.createjs = this.createjs||{};
27722759
subR = rect.width-frame.regX; subB = rect.height-frame.regY;
27732760
}
27742761

2775-
// These must be calculated here else a forced draw might happen after they're set
2776-
var offV1 = this._batchVertexCount; // offset for 1 component vectors
2777-
var offV2 = offV1*2; // offset for 2 component vectors
2778-
var vtxOff = offV2;
2779-
var uvOff = offV2;
2780-
var aOff = offV1;
2781-
var texOff = offV1;
2762+
var spacing = 0;
2763+
var cfg = this._activeConfig;
2764+
var vpos = cfg.position.array;
2765+
var uvs = cfg.uv.array;
2766+
var texI = cfg.texture.array;
2767+
var alphas = cfg.alpha.array;
27822768

2783-
//DHG: See Matrix2D.transformPoint for why this math specifically
27842769
// apply vertices
2785-
vertices[vtxOff] = subL *iMtx.a + subT *iMtx.c +iMtx.tx; vertices[vtxOff+1] = subL *iMtx.b + subT *iMtx.d +iMtx.ty;
2786-
vertices[vtxOff+2] = subL *iMtx.a + subB *iMtx.c +iMtx.tx; vertices[vtxOff+3] = subL *iMtx.b + subB *iMtx.d +iMtx.ty;
2787-
vertices[vtxOff+4] = subR *iMtx.a + subT *iMtx.c +iMtx.tx; vertices[vtxOff+5] = subR *iMtx.b + subT *iMtx.d +iMtx.ty;
2788-
vertices[vtxOff+6] = vertices[vtxOff+2]; vertices[vtxOff+7] = vertices[vtxOff+3];
2789-
vertices[vtxOff+8] = vertices[vtxOff+4]; vertices[vtxOff+9] = vertices[vtxOff+5];
2790-
vertices[vtxOff+10] = subR *iMtx.a + subB *iMtx.c +iMtx.tx; vertices[vtxOff+11] = subR *iMtx.b + subB *iMtx.d +iMtx.ty;
2770+
spacing = cfg.position.spacing;
2771+
var vtxOff = this._batchVertexCount * spacing + cfg.position.offset;
2772+
vpos[vtxOff] = subL*iMtx.a + subT*iMtx.c + iMtx.tx; vpos[vtxOff+1] = subL*iMtx.b + subT*iMtx.d + iMtx.ty;
2773+
vtxOff += spacing;
2774+
vpos[vtxOff] = subL*iMtx.a + subB*iMtx.c + iMtx.tx; vpos[vtxOff+1] = subL*iMtx.b + subB*iMtx.d + iMtx.ty;
2775+
vtxOff += spacing;
2776+
vpos[vtxOff] = subR*iMtx.a + subT*iMtx.c + iMtx.tx; vpos[vtxOff+1] = subR*iMtx.b + subT*iMtx.d + iMtx.ty;
2777+
vtxOff += spacing;
2778+
vpos[vtxOff] = subL*iMtx.a + subB*iMtx.c + iMtx.tx; vpos[vtxOff+1] = subL*iMtx.b + subB*iMtx.d + iMtx.ty;
2779+
vtxOff += spacing;
2780+
vpos[vtxOff] = subR*iMtx.a + subT*iMtx.c + iMtx.tx; vpos[vtxOff+1] = subR*iMtx.b + subT*iMtx.d + iMtx.ty;
2781+
vtxOff += spacing;
2782+
vpos[vtxOff] = subR*iMtx.a + subB*iMtx.c + iMtx.tx; vpos[vtxOff+1] = subR*iMtx.b + subB*iMtx.d + iMtx.ty;
27912783

27922784
// apply uvs
2793-
uvs[uvOff] = uvRect.l; uvs[uvOff+1] = uvRect.t;
2794-
uvs[uvOff+2] = uvRect.l; uvs[uvOff+3] = uvRect.b;
2795-
uvs[uvOff+4] = uvRect.r; uvs[uvOff+5] = uvRect.t;
2796-
uvs[uvOff+6] = uvRect.l; uvs[uvOff+7] = uvRect.b;
2797-
uvs[uvOff+8] = uvRect.r; uvs[uvOff+9] = uvRect.t;
2798-
uvs[uvOff+10] = uvRect.r; uvs[uvOff+11] = uvRect.b;
2785+
spacing = cfg.uv.spacing;
2786+
var uvOff = this._batchVertexCount * spacing + cfg.uv.offset;
2787+
uvs[uvOff] = uvRect.l; uvs[uvOff+1] = uvRect.t;
2788+
uvOff += spacing;
2789+
uvs[uvOff] = uvRect.l; uvs[uvOff+1] = uvRect.b;
2790+
uvOff += spacing;
2791+
uvs[uvOff] = uvRect.r; uvs[uvOff+1] = uvRect.t;
2792+
uvOff += spacing;
2793+
uvs[uvOff] = uvRect.l; uvs[uvOff+1] = uvRect.b;
2794+
uvOff += spacing;
2795+
uvs[uvOff] = uvRect.r; uvs[uvOff+1] = uvRect.t;
2796+
uvOff += spacing;
2797+
uvs[uvOff] = uvRect.r; uvs[uvOff+1] = uvRect.b;
27992798

28002799
// apply texture
2801-
texI[texOff] = texI[texOff+1] = texI[texOff+2] = texI[texOff+3] = texI[texOff+4] = texI[texOff+5] = texIndex;
2800+
spacing = cfg.texture.spacing;
2801+
var texOff = this._batchVertexCount * spacing + cfg.texture.offset;
2802+
texI[texOff] = texIndex;
2803+
texOff += spacing;
2804+
texI[texOff] = texIndex;
2805+
texOff += spacing;
2806+
texI[texOff] = texIndex;
2807+
texOff += spacing;
2808+
texI[texOff] = texIndex;
2809+
texOff += spacing;
2810+
texI[texOff] = texIndex;
2811+
texOff += spacing;
2812+
texI[texOff] = texIndex;
28022813

28032814
// apply alpha
2804-
alphas[aOff] = alphas[aOff+1] = alphas[aOff+2] = alphas[aOff+3] = alphas[aOff+4] = alphas[aOff+5] = itemAlpha * concatAlpha;
2815+
spacing = cfg.alpha.spacing;
2816+
var aOff = this._batchVertexCount * spacing + cfg.alpha.offset;
2817+
alphas[aOff] = itemAlpha * concatAlpha;
2818+
aOff += spacing;
2819+
alphas[aOff] = itemAlpha * concatAlpha;
2820+
aOff += spacing;
2821+
alphas[aOff] = itemAlpha * concatAlpha;
2822+
aOff += spacing;
2823+
alphas[aOff] = itemAlpha * concatAlpha;
2824+
aOff += spacing;
2825+
alphas[aOff] = itemAlpha * concatAlpha;
2826+
aOff += spacing;
2827+
alphas[aOff] = itemAlpha * concatAlpha;
28052828

28062829
this._batchVertexCount += StageGL.INDICIES_PER_CARD;
28072830

@@ -2877,22 +2900,22 @@ this.createjs = this.createjs||{};
28772900

28782901
pc = config.position;
28792902
gl.bindBuffer(gl.ARRAY_BUFFER, pc.buffer);
2880-
gl.vertexAttribPointer(shaderProgram.positionAttribute, pc.size, pc.type, false, pc.stride, pc.offset);
2903+
gl.vertexAttribPointer(shaderProgram.positionAttribute, pc.size, pc.type, false, pc.stride, pc.offB);
28812904
gl.bufferSubData(gl.ARRAY_BUFFER, 0, pc.array);
28822905

28832906
pc = config.texture;
28842907
gl.bindBuffer(gl.ARRAY_BUFFER, pc.buffer);
2885-
gl.vertexAttribPointer(shaderProgram.textureIndexAttribute, pc.size, pc.type, false, pc.stride, pc.offset);
2908+
gl.vertexAttribPointer(shaderProgram.textureIndexAttribute, pc.size, pc.type, false, pc.stride, pc.offB);
28862909
gl.bufferSubData(gl.ARRAY_BUFFER, 0, pc.array);
28872910

28882911
pc = config.uv;
28892912
gl.bindBuffer(gl.ARRAY_BUFFER, pc.buffer);
2890-
gl.vertexAttribPointer(shaderProgram.uvPositionAttribute, pc.size, pc.type, false, pc.stride, pc.offset);
2913+
gl.vertexAttribPointer(shaderProgram.uvPositionAttribute, pc.size, pc.type, false, pc.stride, pc.offB);
28912914
gl.bufferSubData(gl.ARRAY_BUFFER, 0, pc.array);
28922915

28932916
pc = config.alpha;
28942917
gl.bindBuffer(gl.ARRAY_BUFFER, pc.buffer);
2895-
gl.vertexAttribPointer(shaderProgram.alphaAttribute, pc.size, pc.type, false, pc.stride, pc.offset);
2918+
gl.vertexAttribPointer(shaderProgram.alphaAttribute, pc.size, pc.type, false, pc.stride, pc.offB);
28962919
gl.bufferSubData(gl.ARRAY_BUFFER, 0, pc.array);
28972920

28982921
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, gl.FALSE, this._projectionMatrix);
@@ -2927,12 +2950,12 @@ this.createjs = this.createjs||{};
29272950

29282951
pc = config.position;
29292952
gl.bindBuffer(gl.ARRAY_BUFFER, pc.buffer);
2930-
gl.vertexAttribPointer(shaderProgram.positionAttribute, pc.size, pc.type, false, pc.stride, pc.offset);
2953+
gl.vertexAttribPointer(shaderProgram.positionAttribute, pc.size, pc.type, false, pc.stride, pc.offB);
29312954
gl.bufferSubData(gl.ARRAY_BUFFER, 0, StageGL.COVER_VERT);
29322955

29332956
pc = config.uv;
29342957
gl.bindBuffer(gl.ARRAY_BUFFER, pc.buffer);
2935-
gl.vertexAttribPointer(shaderProgram.uvPositionAttribute, pc.size, pc.type, false, pc.stride, pc.offset);
2958+
gl.vertexAttribPointer(shaderProgram.uvPositionAttribute, pc.size, pc.type, false, pc.stride, pc.offB);
29362959
gl.bufferSubData(gl.ARRAY_BUFFER, 0, StageGL.COVER_UV);
29372960

29382961
gl.uniform1i(shaderProgram.samplerUniform, 0);

0 commit comments

Comments
 (0)