Skip to content

Commit

Permalink
now prefering RectangleTextures even for POT textures (closes Gamua#409)
Browse files Browse the repository at this point in the history
Because of Stage3D-internals, POT-Textures use up more memory as necessary (they always allocate mipmaps, whether you need or not). RectangleTextures do not suffer from this problem, so it makes sense to default to RectangleTextures all the time.

The downside: after it has been created, you cannot change the "repeat" property of a texture any more. The repeat setting must now be chosen when you create the texture. For this reason, all "Texture.from..." methods now contain a new "repeat" property.

Thus, this change breaks the API at this point (although I don't expect the repeat property to be in use very often).
  • Loading branch information
PrimaryFeather committed Jan 10, 2014
1 parent ef5b722 commit 7e1daeb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
19 changes: 12 additions & 7 deletions starling/src/starling/textures/ConcreteTexture.as
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package starling.textures
import starling.errors.MissingContextError;
import starling.events.Event;
import starling.utils.Color;
import starling.utils.getNextPowerOfTwo;

use namespace starling_internal;

Expand All @@ -40,6 +39,7 @@ package starling.textures
private var mPremultipliedAlpha:Boolean;
private var mOptimizedForRenderTexture:Boolean;
private var mScale:Number;
private var mRepeat:Boolean;
private var mOnRestore:Function;
private var mDataUploaded:Boolean;

Expand All @@ -51,7 +51,7 @@ package starling.textures
public function ConcreteTexture(base:TextureBase, format:String, width:int, height:int,
mipMapping:Boolean, premultipliedAlpha:Boolean,
optimizedForRenderTexture:Boolean=false,
scale:Number=1)
scale:Number=1, repeat:Boolean=false)
{
mScale = scale <= 0 ? 1.0 : scale;
mBase = base;
Expand All @@ -61,6 +61,7 @@ package starling.textures
mMipMapping = mipMapping;
mPremultipliedAlpha = premultipliedAlpha;
mOptimizedForRenderTexture = optimizedForRenderTexture;
mRepeat = repeat;
mOnRestore = null;
mDataUploaded = false;
}
Expand Down Expand Up @@ -127,7 +128,7 @@ package starling.textures
canvas.dispose();
}
}
else // if (nativeTexture is RectangleTexture)
else // if (mBase is RectangleTexture)
{
mBase["uploadFromBitmapData"](data);
}
Expand Down Expand Up @@ -155,6 +156,9 @@ package starling.textures
var potTexture:flash.display3D.textures.Texture =
mBase as flash.display3D.textures.Texture;

if (potTexture == null)
throw new Error("This texture type does not support ATF data");

if (async is Function)
potTexture.addEventListener(eventType, onTextureReady);

Expand Down Expand Up @@ -193,13 +197,11 @@ package starling.textures
starling_internal function createBase():void
{
var context:Context3D = Starling.context;
var isPot:Boolean = mWidth == getNextPowerOfTwo(mWidth) &&
mHeight == getNextPowerOfTwo(mHeight);

if (isPot)
if (mBase is flash.display3D.textures.Texture)
mBase = context.createTexture(mWidth, mHeight, mFormat,
mOptimizedForRenderTexture);
else
else // if (mBase is RectangleTexture)
mBase = context["createRectangleTexture"](mWidth, mHeight, mFormat,
mOptimizedForRenderTexture);

Expand Down Expand Up @@ -283,5 +285,8 @@ package starling.textures

/** @inheritDoc */
public override function get premultipliedAlpha():Boolean { return mPremultipliedAlpha; }

/** @inheritDoc */
public override function get repeat():Boolean { return mRepeat; }
}
}
3 changes: 3 additions & 0 deletions starling/src/starling/textures/SubTexture.as
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ package starling.textures
/** @inheritDoc */
public override function get scale():Number { return mParent.scale; }

/** @inheritDoc */
public override function get repeat():Boolean { return mParent.repeat; }

/** @inheritDoc */
public override function get frame():Rectangle
{
Expand Down
34 changes: 20 additions & 14 deletions starling/src/starling/textures/Texture.as
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,27 @@ package starling.textures
* render target
* @param scale: the scale factor of the created texture.
* @param format: the context3D texture format to use. Ignored for ATF data.
* @param repeat: the repeat value of the texture. Only useful for power-of-two textures.
*/
public static function fromEmbeddedAsset(assetClass:Class, mipMapping:Boolean=true,
optimizeForRenderToTexture:Boolean=false,
scale:Number=1, format:String="bgra"):Texture
scale:Number=1, format:String="bgra",
repeat:Boolean=false):Texture
{
var texture:Texture;
var asset:Object = new assetClass();

if (asset is Bitmap)
{
texture = Texture.fromBitmap(asset as Bitmap, mipMapping, false, scale, format);
texture = Texture.fromBitmap(asset as Bitmap, mipMapping, false, scale, format, repeat);
texture.root.onRestore = function():void
{
texture.root.uploadBitmap(new assetClass());
};
}
else if (asset is ByteArray)
{
texture = Texture.fromAtfData(asset as ByteArray, scale, mipMapping);
texture = Texture.fromAtfData(asset as ByteArray, scale, mipMapping, null, repeat);
texture.root.onRestore = function():void
{
texture.root.uploadAtfData(new assetClass());
Expand Down Expand Up @@ -182,13 +184,15 @@ package starling.textures
* @param format: the context3D texture format to use. Pass one of the packed or
* compressed formats to save memory (at the price of reduced image
* quality).
* @param repeat: the repeat value of the texture. Only useful for power-of-two textures.
*/
public static function fromBitmap(bitmap:Bitmap, generateMipMaps:Boolean=true,
optimizeForRenderToTexture:Boolean=false,
scale:Number=1, format:String="bgra"):Texture
scale:Number=1, format:String="bgra",
repeat:Boolean=false):Texture
{
return fromBitmapData(bitmap.bitmapData, generateMipMaps, optimizeForRenderToTexture,
scale, format);
scale, format, repeat);
}

/** Creates a texture object from bitmap data.
Expand All @@ -204,14 +208,16 @@ package starling.textures
* @param format: the context3D texture format to use. Pass one of the packed or
* compressed formats to save memory (at the price of reduced image
* quality).
* @param repeat: the repeat value of the texture. Only useful for power-of-two textures.
*/
public static function fromBitmapData(data:BitmapData, generateMipMaps:Boolean=true,
optimizeForRenderToTexture:Boolean=false,
scale:Number=1, format:String="bgra"):Texture
scale:Number=1, format:String="bgra",
repeat:Boolean=false):Texture
{
var texture:Texture = Texture.empty(data.width / scale, data.height / scale, true,
generateMipMaps, optimizeForRenderToTexture, scale,
format);
format, repeat);

texture.root.uploadBitmapData(data);
texture.root.onRestore = function():void
Expand All @@ -231,7 +237,7 @@ package starling.textures
* asynchronously. It can only be used when the callback has been executed. This is the
* expected function definition: <code>function(texture:Texture):void;</code></p> */
public static function fromAtfData(data:ByteArray, scale:Number=1, useMipMaps:Boolean=true,
async:Function=null):Texture
async:Function=null, repeat:Boolean=false):Texture
{
var context:Context3D = Starling.context;
if (context == null) throw new MissingContextError();
Expand All @@ -241,7 +247,7 @@ package starling.textures
atfData.width, atfData.height, atfData.format, false);
var concreteTexture:ConcreteTexture = new ConcreteTexture(nativeTexture, atfData.format,
atfData.width, atfData.height, useMipMaps && atfData.numTextures > 1,
false, false, scale);
false, false, scale, repeat);

concreteTexture.uploadAtfData(data, 0, async);
concreteTexture.onRestore = function():void
Expand Down Expand Up @@ -292,10 +298,11 @@ package starling.textures
* @param scale: if you omit this parameter, 'Starling.contentScaleFactor' will be used.
* @param format: the context3D texture format to use. Pass one of the packed or
* compressed formats to save memory (at the price of reduced image quality).
* @param repeat: the repeat mode of the texture. Only useful for power-of-two textures.
*/
public static function empty(width:Number, height:Number, premultipliedAlpha:Boolean=true,
mipMapping:Boolean=true, optimizeForRenderToTexture:Boolean=false,
scale:Number=-1, format:String="bgra"):Texture
scale:Number=-1, format:String="bgra", repeat:Boolean=false):Texture
{
if (scale <= 0) scale = Starling.contentScaleFactor;

Expand All @@ -310,7 +317,7 @@ package starling.textures
var potWidth:int = getNextPowerOfTwo(origWidth);
var potHeight:int = getNextPowerOfTwo(origHeight);
var isPot:Boolean = (origWidth == potWidth && origHeight == potHeight);
var useRectTexture:Boolean = !isPot && !mipMapping &&
var useRectTexture:Boolean = !mipMapping && !repeat &&
Starling.current.profile != "baselineConstrained" &&
"createRectangleTexture" in context && format.indexOf("compressed") == -1;

Expand All @@ -335,7 +342,7 @@ package starling.textures

var concreteTexture:ConcreteTexture = new ConcreteTexture(nativeTexture, format,
actualWidth, actualHeight, mipMapping, premultipliedAlpha,
optimizeForRenderToTexture, scale);
optimizeForRenderToTexture, scale, repeat);

concreteTexture.onRestore = concreteTexture.clear;

Expand Down Expand Up @@ -393,8 +400,7 @@ package starling.textures
/** Indicates if the texture should repeat like a wallpaper or stretch the outermost pixels.
* Note: this only works in textures with sidelengths that are powers of two and
* that are not loaded from a texture atlas (i.e. no subtextures). @default false */
public function get repeat():Boolean { return mRepeat; }
public function set repeat(value:Boolean):void { mRepeat = value; }
public function get repeat():Boolean { return false; }

/** The width of the texture in points. */
public function get width():Number { return 0; }
Expand Down

0 comments on commit 7e1daeb

Please sign in to comment.