Permalink
Browse files

RenderSupport now uses each QuadBatch only once per frame for rendering

  • Loading branch information...
1 parent 7063d17 commit 669ee77c8af2912d0427c1c627253bd95390a736 @PrimaryFeather PrimaryFeather committed Dec 4, 2011
@@ -13,7 +13,7 @@ package starling.core
import flash.display3D.*;
import flash.geom.*;
import flash.utils.*;
-
+
import starling.display.*;
import starling.errors.*;
import starling.textures.Texture;
@@ -34,7 +34,12 @@ package starling.core
private var mMvpMatrix:Matrix3D;
private var mMatrixStack:Vector.<Matrix3D>;
private var mMatrixStackSize:int;
- private var mQuadBatch:QuadBatch;
+
+ private var mQuadBatches:Vector.<QuadBatch>;
+ private var mCurrentQuadBatchID:int;
+
+ /** Helper object. */
+ private static var sMatrixCoords:Vector.<Number> = new Vector.<Number>(16, true);
// construction
@@ -46,26 +51,38 @@ package starling.core
mMvpMatrix = new Matrix3D();
mMatrixStack = new <Matrix3D>[];
mMatrixStackSize = 0;
- mQuadBatch = new QuadBatch();
+
+ mCurrentQuadBatchID = 0;
+ mQuadBatches = new <QuadBatch>[new QuadBatch()];
loadIdentity();
setOrthographicProjection(400, 300);
}
+ public function dispose():void
+ {
+ for each (var quadBatch:QuadBatch in mQuadBatches)
+ quadBatch.dispose();
+ }
+
// matrix manipulation
/** Sets up the projection matrix for ortographic 2D rendering. */
public function setOrthographicProjection(width:Number, height:Number,
near:Number=-1.0, far:Number=1.0):void
{
- var coords:Vector.<Number> = new <Number>[
- 2.0/width, 0.0, 0.0, 0.0,
- 0.0, -2.0/height, 0.0, 0.0,
- 0.0, 0.0, -2.0/(far-near), 0.0,
- -1.0, 1.0, -(far+near)/(far-near), 1.0
- ];
+ sMatrixCoords[0] = 2.0 / width;
+ sMatrixCoords[1] = sMatrixCoords[2] = sMatrixCoords[3] = sMatrixCoords[4] = 0.0;
+ sMatrixCoords[5] = -2.0 / height;
+ sMatrixCoords[6] = sMatrixCoords[7] = sMatrixCoords[8] = sMatrixCoords[9] = 0.0;
+ sMatrixCoords[10] = -2.0 / (far - near);
+ sMatrixCoords[11] = 0.0;
+ sMatrixCoords[12] = -1.0;
+ sMatrixCoords[13] = 1.0;
+ sMatrixCoords[14] = -(far+near) / (far-near);
+ sMatrixCoords[15] = 1.0;
- mProjectionMatrix.copyRawDataFrom(coords);
+ mProjectionMatrix.copyRawDataFrom(sMatrixCoords);
}
/** Changes the modelview matrix to the identity matrix. */
@@ -144,21 +161,38 @@ package starling.core
/** Adds a quad to the current batch of unrendered quads. If there is a state change,
* all previous quads are rendered at once, and the batch is reset. */
- public function addToQuadBatch(quad:Quad, alpha:Number,
- texture:Texture=null, smoothing:String=null):void
+ public function batchQuad(quad:Quad, alpha:Number,
+ texture:Texture=null, smoothing:String=null):void
{
- if (mQuadBatch.isStateChange(quad, texture, smoothing))
+ if (currentQuadBatch.isStateChange(quad, texture, smoothing))
finishQuadBatch();
- mQuadBatch.addQuad(quad, alpha, texture, smoothing, mModelViewMatrix);
+ currentQuadBatch.addQuad(quad, alpha, texture, smoothing, mModelViewMatrix);
}
/** Renders the current quad batch and resets it. */
public function finishQuadBatch():void
{
- mQuadBatch.syncBuffers();
- mQuadBatch.render(mProjectionMatrix);
- mQuadBatch.reset();
+ currentQuadBatch.syncBuffers();
+ currentQuadBatch.render(mProjectionMatrix);
+ currentQuadBatch.reset();
+
+ ++mCurrentQuadBatchID;
+
+ if (mQuadBatches.length <= mCurrentQuadBatchID)
+ mQuadBatches.push(new QuadBatch());
+ }
+
+ /** Resets the matrix stack and the quad batch index. */
+ public function nextFrame():void
+ {
+ resetMatrix();
+ mCurrentQuadBatchID = 0;
+ }
+
+ private function get currentQuadBatch():QuadBatch
+ {
+ return mQuadBatches[mCurrentQuadBatchID];
}
// other helper methods
@@ -193,6 +193,7 @@ package starling.core
if (mContext) mContext.dispose();
if (mTouchProcessor) mTouchProcessor.dispose();
+ if (mSupport) mSupport.dispose();
}
// functions
@@ -247,7 +248,7 @@ package starling.core
mStage.render(mSupport, 1.0);
mSupport.finishQuadBatch();
- mSupport.resetMatrix();
+ mSupport.nextFrame();
mContext.present();
}
@@ -84,11 +84,12 @@ package starling.display
updateVertexDataCache();
}
- /** Gets the texture coordinates of a vertex. Coordinates are in the range [0, 1].
- * To avoid allocations, the output is saved into a parameter. */
- public function getTexCoords(vertexID:int, coords:Point):void
+ /** Gets the texture coordinates of a vertex. Coordinates are in the range [0, 1]. */
+ public function getTexCoords(vertexID:int):Point
{
+ var coords:Point = new Point();
mVertexData.getTexCoords(vertexID, coords);
+ return coords;
}
/** Copies the raw vertex data to a VertexData instance.
@@ -149,7 +150,7 @@ package starling.display
/** @inheritDoc */
public override function render(support:RenderSupport, alpha:Number):void
{
- support.addToQuadBatch(this, alpha, mTexture, mSmoothing);
+ support.batchQuad(this, alpha, mTexture, mSmoothing);
}
}
}
@@ -138,7 +138,7 @@ package starling.display
/** @inheritDoc */
public override function render(support:RenderSupport, alpha:Number):void
{
- support.addToQuadBatch(this, alpha);
+ support.batchQuad(this, alpha);
}
}
}
@@ -10,13 +10,15 @@
package starling.textures
{
+ import flash.display3D.Context3D;
import flash.display3D.textures.TextureBase;
import flash.geom.Rectangle;
import starling.core.RenderSupport;
import starling.core.Starling;
import starling.display.DisplayObject;
import starling.display.Image;
+ import starling.errors.MissingContextError;
import starling.utils.VertexData;
import starling.utils.getNextPowerOfTwo;
@@ -114,8 +116,11 @@ package starling.textures
* switches and allows you to draw multiple objects into a non-persistent texture. */
public function drawBundled(drawingBlock:Function, antiAliasing:int=0):void
{
+ var context:Context3D = Starling.context;
+ if (context == null) throw new MissingContextError();
+
// limit drawing to relevant area
- Starling.context.setScissorRectangle(
+ context.setScissorRectangle(
new Rectangle(0, 0, mActiveTexture.width, mActiveTexture.height));
// persistent drawing uses double buffering, as Molehill forces us to call 'clear'
@@ -130,7 +135,7 @@ package starling.textures
mHelperImage.texture = mBufferTexture;
}
- Starling.context.setRenderToTexture(mActiveTexture.base, false, antiAliasing);
+ context.setRenderToTexture(mActiveTexture.base, false, antiAliasing);
RenderSupport.setDefaultBlendFactors(true);
mSupport.setOrthographicProjection(mNativeWidth, mNativeHeight);
@@ -152,25 +157,28 @@ package starling.textures
{
mDrawing = false;
mSupport.finishQuadBatch();
- mSupport.resetMatrix();
- Starling.context.setScissorRectangle(null);
- Starling.context.setRenderToBackBuffer();
+ mSupport.nextFrame();
+ context.setScissorRectangle(null);
+ context.setRenderToBackBuffer();
}
}
/** Clears the texture (restoring full transparency). */
public function clear():void
{
- Starling.context.setRenderToTexture(mActiveTexture.base);
+ var context:Context3D = Starling.context;
+ if (context == null) throw new MissingContextError();
+
+ context.setRenderToTexture(mActiveTexture.base);
mSupport.clear();
if (isPersistent)
{
- Starling.context.setRenderToTexture(mActiveTexture.base);
+ context.setRenderToTexture(mActiveTexture.base);
mSupport.clear();
}
- Starling.context.setRenderToBackBuffer();
+ context.setRenderToBackBuffer();
}
/** @inheritDoc */

0 comments on commit 669ee77

Please sign in to comment.