From 84bc33ff0c6e941d2af7f2f0f8dba1e72ed451fc Mon Sep 17 00:00:00 2001 From: TerryCavanagh Date: Sun, 22 Jan 2017 18:39:57 +0000 Subject: [PATCH] Track drawstate to improve tile quadbatching, fixed memory leak in rendertexture --- haxegon/Gfx.hx | 32 ++++++++++++++++++++++---------- haxegon/Text.hx | 17 +++++++++++++++-- starling/core/RenderSupport.hx | 21 +++++++++++++++++++-- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/haxegon/Gfx.hx b/haxegon/Gfx.hx index 9e1e194..79579fd 100644 --- a/haxegon/Gfx.hx +++ b/haxegon/Gfx.hx @@ -146,6 +146,7 @@ class Gfx { /* Internal function for changing tile index to correct values for tileset */ private static function changetileset(tilesetname:String) { if (currenttilesetname != tilesetname) { + drawstate = DRAWSTATE_NONE; if(tilesetindex.exists(tilesetname)){ currenttileset = tilesetindex.get(tilesetname); currenttilesetname = tilesetname; @@ -554,12 +555,12 @@ class Gfx { shapematrix.translate(x, y); if (coltransform) { image.color = imagecolormult; - quadbatch.addImage(image, 1.0, shapematrix); + quadbatch.addImage(image, imagealphamult, shapematrix); image.color = Col.WHITE; }else { quadbatch.addImage(image, 1.0, shapematrix); } - } + } } /** Draws image by name. @@ -573,6 +574,7 @@ class Gfx { //This could definitely be improved later. See #118 endquadbatch(); updatequadbatch(); + drawstate = DRAWSTATE_IMAGE; haxegonimage = images[imageindex.get(imagename)]; x = imagealignx(haxegonimage.width, x); y = imagealigny(haxegonimage.height, y); @@ -801,19 +803,13 @@ class Gfx { //WIP: If we're using a tileset then we can batch draw stuff because it's all on the same texture //(providing we haven't messed with the tileset by creating images) - if (!tiles[currenttileset].sharedatlas) { - endquadbatch(); - } + if (drawstate != DRAWSTATE_TILES) endquadbatch(); updatequadbatch(); + drawstate = DRAWSTATE_TILES; x = tilealignx(x); y = tilealigny(y); internaldrawimage(x, y, tiles[currenttileset].tiles[tilenum], tiles[currenttileset].width, tiles[currenttileset].height); - - if (!tiles[currenttileset].sharedatlas) { - //This could definitely be improved later. See #118 - endquadbatch(); - } } private static function tilealignx(x:Float):Float { @@ -846,7 +842,9 @@ class Gfx { public static function drawline(x1:Float, y1:Float, x2:Float, y2:Float, color:Int, alpha:Float = 1.0) { if (color == Col.TRANSPARENT || drawto == null) return; + if (drawstate != DRAWSTATE_QUAD) endquadbatch(); updatequadbatch(); + drawstate = DRAWSTATE_QUAD; templine = new Line(x1, y1, x2, y2, linethickness, color); templine.alpha = alpha; @@ -916,7 +914,9 @@ class Gfx { public static function filltri(x1:Float, y1:Float, x2:Float, y2:Float, x3:Float, y3:Float, color:Int, alpha:Float = 1.0) { if (color == Col.TRANSPARENT || drawto == null) return; + if (drawstate != DRAWSTATE_POLY4) endquadbatch(); updatequadbatch(); + drawstate = DRAWSTATE_POLY4; temppoly4 = new Poly4(x1, y1, x2, y2, x3, y3, x3, y3, color); temppoly4.alpha = alpha; @@ -944,7 +944,9 @@ class Gfx { public static function fillbox(x:Float, y:Float, width:Float, height:Float, col:Int, alpha:Float = 1.0) { if (col == Col.TRANSPARENT) return; + if (drawstate != DRAWSTATE_QUAD) endquadbatch(); updatequadbatch(); + drawstate = DRAWSTATE_QUAD; tempquad.x = x; tempquad.y = y; @@ -967,6 +969,7 @@ class Gfx { quadbatch.reset(); quadbatchcount = 0; + drawstate = DRAWSTATE_NONE; } } @@ -1185,6 +1188,7 @@ class Gfx { } private static function startframe() { + drawstate = DRAWSTATE_NONE; drawto.bundlelock(); if (clearcolor != Col.TRANSPARENT) clearscreen(clearcolor); @@ -1207,6 +1211,14 @@ class Gfx { private static var temppoly4:Poly4; private static var templine:Line; + private static var drawstate:Int = 0; + private static inline var DRAWSTATE_NONE:Int = 0; + private static inline var DRAWSTATE_QUAD:Int = 1; + private static inline var DRAWSTATE_POLY4:Int = 2; + private static inline var DRAWSTATE_IMAGE:Int = 3; + private static inline var DRAWSTATE_TILES:Int = 4; + private static inline var DRAWSTATE_TEXT:Int = 5; + private static var starlingassets:AssetManager; private static var trect:Rectangle = new Rectangle(); private static var shapematrix:Matrix = new Matrix(); diff --git a/haxegon/Text.hx b/haxegon/Text.hx index 6d06440..92f4fad 100644 --- a/haxegon/Text.hx +++ b/haxegon/Text.hx @@ -202,6 +202,9 @@ class Text { private static function drawstringinput() { Gfx.endquadbatch(); + //if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch(); + //Gfx.updatequadbatch(); + //Gfx.drawstate = Gfx.DRAWSTATE_TEXT; if (input_show > 0) { setfont(input_font, input_textsize); @@ -336,6 +339,10 @@ class Text { public static function display(x:Float, y:Float, text:String, color:Int = 0xFFFFFF) { if (text == "") return; Gfx.endquadbatch(); + //Future developments! + //if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch(); + //Gfx.updatequadbatch(); + //Gfx.drawstate = Gfx.DRAWSTATE_TEXT; if (typeface.length == 0) { defaultfont(); @@ -367,11 +374,14 @@ class Text { } fontmatrix.translate(x, y); - Gfx.drawto.draw(typeface[currentindex].tf, fontmatrix); // Clumsy work around to force haxegon to change to the next draw call on TTF fonts. // to do: implement a pooling system for ttf fonts so that this isn't required. if (typeface[currentindex].type == "ttf") { + Gfx.drawto.draw(typeface[currentindex].tf, fontmatrix); Gfx.fillbox(-1, -1, 1, 1, Col.RED); + }else { + Gfx.drawto.draw(typeface[currentindex].tf, fontmatrix); + //Gfx.quadbatch.addQuadBatch(typeface[currentindex].tf.mQuadBatch, 1.0, fontmatrix); } } @@ -437,6 +447,8 @@ class Text { static function set_font(fontname:String):String { if (fontname == "" || fontname.toLowerCase() == "verdana") fontname = "Verdana"; if (fontname == currentfont) return currentfont; + + //if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch(); setfont(fontname, 1); return currentfont; } @@ -448,7 +460,8 @@ class Text { } static function set_size(fontsize:Float):Float { - if(currentsize != fontsize){ + if (currentsize != fontsize) { + //if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch(); changesize(fontsize); } return currentsize; diff --git a/starling/core/RenderSupport.hx b/starling/core/RenderSupport.hx index a637a2b..94c1916 100644 --- a/starling/core/RenderSupport.hx +++ b/starling/core/RenderSupport.hx @@ -196,8 +196,25 @@ class RenderSupport /** Changes the modelview matrix to the identity matrix. */ public function loadIdentity():Void { - mModelViewMatrix.identity(); - mModelViewMatrix3D.identity(); + mModelViewMatrix.identity(); + + //mModelViewMatrix3D.identity(); + mModelViewMatrix3D.rawData[0] = 1.0; + mModelViewMatrix3D.rawData[1] = 0.0; + mModelViewMatrix3D.rawData[2] = 0.0; + mModelViewMatrix3D.rawData[3] = 0.0; + mModelViewMatrix3D.rawData[4] = 0.0; + mModelViewMatrix3D.rawData[5] = 1.0; + mModelViewMatrix3D.rawData[6] = 0.0; + mModelViewMatrix3D.rawData[7] = 0.0; + mModelViewMatrix3D.rawData[8] = 0.0; + mModelViewMatrix3D.rawData[9] = 0.0; + mModelViewMatrix3D.rawData[10] = 1.0; + mModelViewMatrix3D.rawData[11] = 0.0; + mModelViewMatrix3D.rawData[12] = 0.0; + mModelViewMatrix3D.rawData[13] = 0.0; + mModelViewMatrix3D.rawData[14] = 0.0; + mModelViewMatrix3D.rawData[15] = 1.0; } /** Prepends a translation to the modelview matrix. */