Skip to content

Commit

Permalink
now showing gpu memory in statsDisplay if possible (closes #864)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed Jul 21, 2016
1 parent a8e656f commit 58062d1
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions starling/src/starling/core/StatsDisplay.as
Expand Up @@ -20,42 +20,58 @@ package starling.core
import starling.styles.MeshStyle;
import starling.text.BitmapFont;
import starling.text.TextField;
import starling.text.TextFormat;
import starling.utils.Align;

/** A small, lightweight box that displays the current framerate, memory consumption and
* the number of draw calls per frame. The display is updated automatically once per frame. */
internal class StatsDisplay extends Sprite
{
private const UPDATE_INTERVAL:Number = 0.5;
private static const UPDATE_INTERVAL:Number = 0.5;
private static const B_TO_MB:Number = 1.0 / (1024 * 1024); // convert from bytes to MB

private var _background:Quad;
private var _textField:TextField;
private var _labels:TextField;
private var _values:TextField;

private var _frameCount:int = 0;
private var _totalTime:Number = 0;

private var _fps:Number = 0;
private var _memory:Number = 0;
private var _gpuMemory:Number = 0;
private var _drawCount:int = 0;
private var _skipCount:int = 0;

/** Creates a new Statistics Box. */
public function StatsDisplay()
{
var format:TextFormat = new TextFormat(BitmapFont.MINI, BitmapFont.NATIVE_SIZE,
0xffffff, Align.LEFT, Align.TOP);
const fontName:String = BitmapFont.MINI;
const fontSize:Number = BitmapFont.NATIVE_SIZE;
const fontColor:uint = 0xffffff;
const width:Number = 90;
const height:Number = supportsGpuMem ? 35 : 27;
const gpuLabel:String = supportsGpuMem ? "\ngpu memory:" : "";
const labels:String = "frames/sec:\nstd memory:" + gpuLabel + "\ndraw calls:";

_labels = new TextField(width, height, labels);
_labels.format.setTo(fontName, fontSize, fontColor, Align.LEFT);
_labels.batchable = true;
_labels.x = 2;

_background = new Quad(50, 25, 0x0);
_textField = new TextField(48, 25, "", format);
_textField.x = 2;
_values = new TextField(width - 1, height, "");
_values.format.setTo(fontName, fontSize, fontColor, Align.RIGHT);
_values.batchable = true;

_background = new Quad(width, height, 0x0);

// make sure that rendering takes 2 draw calls
if (_background.style.type != MeshStyle) _background.style = new MeshStyle();
if ( _textField.style.type != MeshStyle) _textField.style = new MeshStyle();
if (_labels.style.type != MeshStyle) _labels.style = new MeshStyle();
if (_values.style.type != MeshStyle) _values.style = new MeshStyle();

addChild(_background);
addChild(_textField);
addChild(_labels);
addChild(_values);

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
Expand Down Expand Up @@ -88,12 +104,18 @@ package starling.core
/** Updates the displayed values. */
public function update():void
{
_fps = _totalTime > 0 ? _frameCount / _totalTime : 0;
_memory = System.totalMemory * 0.000000954; // 1.0 / (1024*1024) to convert to MB
_background.color = _skipCount > _frameCount / 2 ? 0x003F00 : 0x0;
_textField.text = "FPS: " + _fps.toFixed(_fps < 100 ? 1 : 0) +
"\nMEM: " + _memory.toFixed(_memory < 100 ? 1 : 0) +
"\nDRW: " + (_totalTime > 0 ? _drawCount-2 : _drawCount); // ignore self
_fps = _totalTime > 0 ? _frameCount / _totalTime : 0;
_memory = System.totalMemory * B_TO_MB;
_gpuMemory = supportsGpuMem ? Starling.context['totalGPUMemory'] * B_TO_MB : -1;

var fpsText:String = _fps.toFixed(_fps < 100 ? 1 : 0);
var memText:String = _memory.toFixed(_memory < 100 ? 1 : 0);
var gpuMemText:String = _gpuMemory.toFixed(_gpuMemory < 100 ? 1 : 0);
var drwText:String = (_totalTime > 0 ? _drawCount-2 : _drawCount).toString(); // ignore self

_values.text = fpsText + "\n" + memText + "\n" +
(_gpuMemory >= 0 ? gpuMemText + "\n" : "") + drwText;
}

/** Call this once in every frame that can skip rendering because nothing changed. */
Expand All @@ -112,6 +134,12 @@ package starling.core
painter.finishMeshBatch();
super.render(painter);
}

/** Indicates if the current runtime supports the 'totalGPUMemory' API. */
private function get supportsGpuMem():Boolean
{
return "totalGPUMemory" in Starling.context;
}

/** The number of Stage3D draw calls per second. */
public function get drawCount():int { return _drawCount; }
Expand All @@ -121,8 +149,12 @@ package starling.core
public function get fps():Number { return _fps; }
public function set fps(value:Number):void { _fps = value; }

/** The currently required system memory in MB. */
/** The currently used system memory in MB. */
public function get memory():Number { return _memory; }
public function set memory(value:Number):void { _memory = value; }

/** The currently used graphics memory in MB. */
public function get gpuMemory():Number { return _gpuMemory; }
public function set gpuMemory(value:Number):void { _gpuMemory = value; }
}
}

0 comments on commit 58062d1

Please sign in to comment.