Skip to content

Commit 10fefbd

Browse files
author
David Gillen
committed
Added buffer size tracking and buffer capping capabilities
1 parent ff10486 commit 10fefbd

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/easeljs/display/StageGL.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,22 @@ this.createjs = this.createjs||{};
130130
* for more information on texture purging.
131131
* @param {String|int} [options.clearColor=undefined] Automatically calls {{#crossLink "StageGL/setClearColor"}}{{/crossLink}}
132132
* after init is complete, can be overridden and changed manually later.
133+
* @param {String|int} [options.batchSize=DEFAULT_MAX_BATCH_SIZE] The size of the buffer used to retain a batch.
134+
* Making it smaller reduces GPU load, but making it too small adds extra GPU calls. Figure out your maximum batch
135+
* count and set it to a small buffer above that per-project. Check src/utils/WebGLInspector to track.
133136
*/
134137
function StageGL(canvas, options) {
135138
this.Stage_constructor(canvas);
136139

137-
var transparent, antialias, preserveBuffer, autoPurge, directDraw;
140+
var transparent, antialias, preserveBuffer, autoPurge, directDraw, batchSize;
138141
if (options !== undefined) {
139142
if (typeof options !== "object"){ throw("Invalid options object"); }
140143
transparent = options.transparent;
141144
antialias = options.antialias;
142145
preserveBuffer = options.preserveBuffer;
143146
autoPurge = options.autoPurge;
144147
directDraw = options.directDraw;
148+
batchSize = options.batchSize;
145149
}
146150

147151
// public properties:
@@ -269,14 +273,18 @@ this.createjs = this.createjs||{};
269273
this._clearColor = {r: 0.50, g: 0.50, b: 0.50, a: 0.00};
270274

271275
/**
272-
* The maximum number of cards (aka a single sprite) that can be drawn in one draw call. Use getter/setters to
273-
* modify otherwise internal buffers may be incorrect sizes.
276+
* The maximum number of verticies (6 make a single sprite) that can be drawn in one draw call. Use constructor props
277+
* to modify otherwise internal buffers may be invalid sizes.
274278
* @property _maxBatchVertexCount
275279
* @protected
276280
* @type {Number}
277281
* @default StageGL.DEFAULT_MAX_BATCH_SIZE * StageGL.INDICIES_PER_CARD
278282
*/
279-
this._maxBatchVertexCount = StageGL.DEFAULT_MAX_BATCH_SIZE * StageGL.INDICIES_PER_CARD;
283+
this._maxBatchVertexCount = Math.max(
284+
Math.min(
285+
Number(batchSize) || StageGL.DEFAULT_MAX_BATCH_SIZE,
286+
StageGL.DEFAULT_MAX_BATCH_SIZE)
287+
, StageGL.DEFAULT_MIN_BATCH_SIZE) * StageGL.INDICIES_PER_CARD;
280288

281289
/**
282290
* The shader program used to draw the current batch.
@@ -705,15 +713,28 @@ this.createjs = this.createjs||{};
705713
/**
706714
* The default value for the maximum number of cards we want to process in a batch. See
707715
* {{#crossLink "StageGL/WEBGL_MAX_INDEX_NUM:property"}}{{/crossLink}} for a hard limit.
716+
* this value comes is designed to sneak under that limit.
708717
* @property DEFAULT_MAX_BATCH_SIZE
709718
* @static
710719
* @final
711720
* @type {Number}
712-
* @default 10000
721+
* @default 10920
713722
* @readonly
714723
*/
715724
StageGL.DEFAULT_MAX_BATCH_SIZE = 10920;
716725

726+
/**
727+
* The default value for the minimum number of cards we want to process in a batch. Less
728+
* max cards can mean better performance, but anything below this is probably not worth it.
729+
* @property DEFAULT_MIN_BATCH_SIZE
730+
* @static
731+
* @final
732+
* @type {Number}
733+
* @default 170
734+
* @readonly
735+
*/
736+
StageGL.DEFAULT_MIN_BATCH_SIZE = 170;
737+
717738
/**
718739
* The maximum size WebGL allows for element index numbers. Uses a 16 bit unsigned integer. It takes 6 indices to
719740
* make a unique card.

src/easeljs/utils/WebGLInspector.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ this.createjs = this.createjs||{};
8282
WebGLInspector.logAll = function(stage) {
8383
if (!stage){ stage = WebGLInspector.stage; }
8484

85-
WebGLInspector.log("Batches Per Draw", (stage._batchID/stage._drawID).toFixed(4));
85+
WebGLInspector.log("Average batches Per Draw", (stage._batchID/stage._drawID).toFixed(4));
8686
WebGLInspector.logContextInfo(stage._webGLContext);
8787
WebGLInspector.logDepth(stage.children, "");
8888
WebGLInspector.logTextureFill(stage);
@@ -231,6 +231,22 @@ this.createjs = this.createjs||{};
231231
WebGLInspector.log(prepend, item.toString()+"\t", p,r);
232232
};
233233

234+
/**
235+
* Utility function for use with {{#crossLink "replaceRenderBatchCall"))((/crossLink}}.
236+
* Tracks the highest element per batch count any render has achieved, useful for fine tuning max performance.
237+
* Use `WebGLInspector.__lastHighest;` to inspect value.
238+
* Warning, this will not show values higher than your current batchSize.
239+
*/
240+
WebGLInspector.trackMaxBatchDraw = function() {
241+
var cardCount = this._batchVertexCount/createjs.StageGL.INDICIES_PER_CARD;
242+
if(!(cardCount < WebGLInspector.__lastHighest)) { //backwards handles NaNs inline
243+
WebGLInspector.__lastHighest = cardCount;
244+
}
245+
246+
// don't break regular behavior
247+
stage._renderBatch_();
248+
};
249+
234250
/**
235251
* Utility function for use with {{#crossLink "replaceRenderBatchCall"))((/crossLink}}.
236252
* Performs no GL draw command.

0 commit comments

Comments
 (0)