Skip to content

Commit

Permalink
updated ATF texture support for final data format
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed Jul 2, 2012
1 parent bf10073 commit 7739a9e
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 224 deletions.
Binary file modified samples/demo/media/textures/1x/compressed_texture.atf
Binary file not shown.
Binary file modified samples/demo/media/textures/2x/compressed_texture.atf
Binary file not shown.
2 changes: 1 addition & 1 deletion samples/demo/src/Startup.as
Expand Up @@ -23,7 +23,7 @@ package

mStarling = new Starling(Game, stage);
mStarling.simulateMultitouch = true;
mStarling.enableErrorChecking = Capabilities.isDebugger;
mStarling.enableErrorChecking = false;
mStarling.start();

// this event is dispatched when stage3D is set up
Expand Down
27 changes: 20 additions & 7 deletions samples/demo/src/scenes/TextureScene.as
@@ -1,6 +1,7 @@
package scenes
{
import starling.display.Image;
import starling.text.TextField;
import starling.textures.Texture;
import starling.textures.TextureAtlas;

Expand All @@ -27,13 +28,25 @@ package scenes
image3.y = -60;
addChild(image3);

// display a compressed texture

var compressedTexture:Texture = Assets.getTexture("CompressedTexture");
var image:Image = new Image(compressedTexture);
image.x = Constants.CenterX - image.width / 2;
image.y = 280;
addChild(image);
try
{
// display a compressed texture
var compressedTexture:Texture = Assets.getTexture("CompressedTexture");
var image:Image = new Image(compressedTexture);
image.x = Constants.CenterX - image.width / 2;
image.y = 280;
addChild(image);
}
catch (e:Error)
{
// if it fails, it's probably not supported
var textField:TextField = new TextField(220, 128,
"Update to Flash Player 11.4 or AIR 3.4 (swf-version=17) to see a compressed " +
"ATF texture instead of this boring text.", "Verdana", 14);
textField.x = Constants.CenterX - textField.width / 2;
textField.y = 280;
addChild(textField);
}
}
}
}
1 change: 1 addition & 0 deletions starling/.flexLibProperties
Expand Up @@ -52,6 +52,7 @@
<classEntry path="starling.utils.getNextPowerOfTwo"/>
<classEntry path="starling.utils.rad2deg"/>
<classEntry path="starling.utils.MatrixUtil"/>
<classEntry path="starling.textures.AtfData"/>
</includeClasses>
<includeResources/>
<namespaceManifests/>
Expand Down
224 changes: 119 additions & 105 deletions starling/src/com/adobe/utils/AGALMiniAssembler.as

Large diffs are not rendered by default.

143 changes: 56 additions & 87 deletions starling/src/starling/display/QuadBatch.as
Expand Up @@ -14,12 +14,14 @@ package starling.display

import flash.display3D.Context3D;
import flash.display3D.Context3DProgramType;
import flash.display3D.Context3DTextureFormat;
import flash.display3D.Context3DVertexBufferFormat;
import flash.display3D.IndexBuffer3D;
import flash.display3D.VertexBuffer3D;
import flash.geom.Matrix;
import flash.geom.Matrix3D;
import flash.geom.Rectangle;
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;

import starling.core.RenderSupport;
Expand Down Expand Up @@ -80,6 +82,7 @@ package starling.display
private static var sHelperMatrix:Matrix = new Matrix();
private static var sRenderAlpha:Vector.<Number> = new <Number>[1.0, 1.0, 1.0, 1.0];
private static var sRenderMatrix:Matrix3D = new Matrix3D();
private static var sProgramNameCache:Dictionary = new Dictionary();

/** Creates a new QuadBatch instance with empty batch data. */
public function QuadBatch()
Expand Down Expand Up @@ -202,7 +205,7 @@ package starling.display
var context:Context3D = Starling.context;
var tinted:Boolean = mTinted || (parentAlpha != 1.0);
var programName:String = mTexture ?
getImageProgramName(tinted, mTexture.mipMapping, mTexture.repeat, mSmoothing) :
getImageProgramName(tinted, mTexture.mipMapping, mTexture.repeat, mTexture.format, mSmoothing) :
QUAD_PROGRAM_NAME;

sRenderAlpha[0] = sRenderAlpha[1] = sRenderAlpha[2] = pma ? parentAlpha : 1.0;
Expand Down Expand Up @@ -551,110 +554,76 @@ package starling.display
TextureSmoothing.TRILINEAR
];

var formats:Array = [
Context3DTextureFormat.BGRA,
Context3DTextureFormat.COMPRESSED,
"compressedAlpha" // use explicit string for compatibility
];

for each (var repeat:Boolean in [true, false])
{
for each (var mipmap:Boolean in [true, false])
{
for each (var smoothing:String in smoothingTypes)
{
var options:Array = ["2d", repeat ? "repeat" : "clamp"];

if (smoothing == TextureSmoothing.NONE)
options.push("nearest", mipmap ? "mipnearest" : "mipnone");
else if (smoothing == TextureSmoothing.BILINEAR)
options.push("linear", mipmap ? "mipnearest" : "mipnone");
else
options.push("linear", mipmap ? "miplinear" : "mipnone");

fragmentProgramAssembler.assemble(Context3DProgramType.FRAGMENT,
fragmentProgramCode.replace("???", options.join()));

target.registerProgram(
getImageProgramName(tinted, mipmap, repeat, smoothing),
vertexProgramAssembler.agalcode, fragmentProgramAssembler.agalcode);
for each (var format:String in formats)
{
var options:Array = ["2d", repeat ? "repeat" : "clamp"];

if (format == Context3DTextureFormat.COMPRESSED)
options.push("dxt1");
else if (format == "compressedAlpha")
options.push("dxt5");

if (smoothing == TextureSmoothing.NONE)
options.push("nearest", mipmap ? "mipnearest" : "mipnone");
else if (smoothing == TextureSmoothing.BILINEAR)
options.push("linear", mipmap ? "mipnearest" : "mipnone");
else
options.push("linear", mipmap ? "miplinear" : "mipnone");

fragmentProgramAssembler.assemble(Context3DProgramType.FRAGMENT,
fragmentProgramCode.replace("???", options.join()));

target.registerProgram(
getImageProgramName(tinted, mipmap, repeat, format, smoothing),
vertexProgramAssembler.agalcode, fragmentProgramAssembler.agalcode);
}
}
}
}
}
}

private static function getImageProgramName(tinted:Boolean,
mipMap:Boolean=true, repeat:Boolean=false,
private static function getImageProgramName(tinted:Boolean, mipMap:Boolean=true,
repeat:Boolean=false, format:String="bgra",
smoothing:String="bilinear"):String
{
// to avoid temporary string creation, the program name is not built dynamically;
// instead, we use a generated code that returns a string constant.
// code generator: https://gist.github.com/2774587
var bitField:uint = 0;

if (tinted)
{
if (mipMap)
{
if (repeat)
{
if (smoothing == 'bilinear') return "QB_i*MRB";
else if (smoothing == 'trilinear') return "QB_i*MRT";
else return "QB_i*MRN";
}
else
{
if (smoothing == 'bilinear') return "QB_i*MB";
else if (smoothing == 'trilinear') return "QB_i*MT";
else return "QB_i*MN";
}
}
else
{
if (repeat)
{
if (smoothing == 'bilinear') return "QB_i*RB";
else if (smoothing == 'trilinear') return "QB_i*RT";
else return "QB_i*RN";
}
else
{
if (smoothing == 'bilinear') return "QB_i*B";
else if (smoothing == 'trilinear') return "QB_i*T";
else return "QB_i*N";
}
}
}
else
if (tinted) bitField |= 1;
if (mipMap) bitField |= 1 << 1;
if (repeat) bitField |= 1 << 2;

if (smoothing == TextureSmoothing.NONE)
bitField |= 1 << 3;
else if (smoothing == TextureSmoothing.TRILINEAR)
bitField |= 1 << 4;

if (format == Context3DTextureFormat.COMPRESSED)
bitField |= 1 << 5;
else if (format == "compressedAlpha")
bitField |= 1 << 6;

var name:String = sProgramNameCache[bitField];

if (name == null)
{
if (mipMap)
{
if (repeat)
{
if (smoothing == 'bilinear') return "QB_i'MRB";
else if (smoothing == 'trilinear') return "QB_i'MRT";
else return "QB_i'MRN";
}
else
{
if (smoothing == 'bilinear') return "QB_i'MB";
else if (smoothing == 'trilinear') return "QB_i'MT";
else return "QB_i'MN";
}
}
else
{
if (repeat)
{
if (smoothing == 'bilinear') return "QB_i'RB";
else if (smoothing == 'trilinear') return "QB_i'RT";
else return "QB_i'RN";
}
else
{
if (smoothing == 'bilinear') return "QB_i'B";
else if (smoothing == 'trilinear') return "QB_i'T";
else return "QB_i'N";
}
}
name = "QB_i." + bitField.toString(16);
sProgramNameCache[bitField] = name;
}

// end of generated code
return name;
}

}
}
56 changes: 56 additions & 0 deletions starling/src/starling/textures/AtfData.as
@@ -0,0 +1,56 @@
// =================================================================================================
//
// Starling Framework
// Copyright 2012 Gamua OG. All Rights Reserved.
//
// This program is free software. You can redistribute and/or modify it
// in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package starling.textures
{
import flash.display3D.Context3DTextureFormat;
import flash.utils.ByteArray;

/** A parser for the ATF data format. */
internal class AtfData
{
private var mFormat:String;
private var mWidth:int;
private var mHeight:int;
private var mNumTextures:int;
private var mData:ByteArray;

/** Create a new instance by parsing the given byte array. */
public function AtfData(data:ByteArray)
{
var signature:String = String.fromCharCode(data[0], data[1], data[2]);
if (signature != "ATF") throw new ArgumentError("Invalid ATF data");

var format:String;
switch (data[6])
{
case 0:
case 1: mFormat = Context3DTextureFormat.BGRA; break;
case 2:
case 3: mFormat = Context3DTextureFormat.COMPRESSED; break;
case 4:
case 5: mFormat = "compressedAlpha"; break; // explicit string to stay compatible
// with older versions
default: throw new Error("Invalid ATF format");
}

mWidth = Math.pow(2, data[7]);
mHeight = Math.pow(2, data[8]);
mNumTextures = data[9];
mData = data;
}

public function get format():String { return mFormat; }
public function get width():int { return mWidth; }
public function get height():int { return mHeight; }
public function get numTextures():int { return mNumTextures; }
public function get data():ByteArray { return mData; }
}
}
22 changes: 12 additions & 10 deletions starling/src/starling/textures/ConcreteTexture.as
Expand Up @@ -23,6 +23,7 @@ package starling.textures
public class ConcreteTexture extends Texture
{
private var mBase:TextureBase;
private var mFormat:String;
private var mWidth:int;
private var mHeight:int;
private var mMipMapping:Boolean;
Expand All @@ -33,13 +34,14 @@ package starling.textures

/** Creates a ConcreteTexture object from a TextureBase, storing information about size,
* mip-mapping, and if the channels contain premultiplied alpha values. */
public function ConcreteTexture(base:TextureBase, width:int, height:int,
public function ConcreteTexture(base:TextureBase, format:String, width:int, height:int,
mipMapping:Boolean, premultipliedAlpha:Boolean,
optimizedForRenderTexture:Boolean=false,
scale:Number=1)
{
mScale = scale <= 0 ? 1.0 : scale;
mBase = base;
mFormat = format;
mWidth = width;
mHeight = height;
mMipMapping = mipMapping;
Expand Down Expand Up @@ -73,7 +75,7 @@ package starling.textures
{
var context:Context3D = Starling.context;
var bitmapData:BitmapData = mData as BitmapData;
var byteData:ByteArray = mData as ByteArray;
var atfData:AtfData = mData as AtfData;
var nativeTexture:flash.display3D.textures.Texture;

if (bitmapData)
Expand All @@ -82,16 +84,13 @@ package starling.textures
Context3DTextureFormat.BGRA, mOptimizedForRenderTexture);
Texture.uploadBitmapData(nativeTexture, bitmapData, mMipMapping);
}
else if (byteData)
else if (atfData)
{
var format:String = byteData[6] == 2 ? Context3DTextureFormat.COMPRESSED :
Context3DTextureFormat.BGRA;

nativeTexture = context.createTexture(mWidth, mHeight,
format, mOptimizedForRenderTexture);
Texture.uploadAtfData(nativeTexture, byteData);
nativeTexture = context.createTexture(atfData.width, atfData.height, atfData.format,
mOptimizedForRenderTexture);
Texture.uploadAtfData(nativeTexture, atfData.data);
}

mBase = nativeTexture;
}

Expand All @@ -103,6 +102,9 @@ package starling.textures
/** @inheritDoc */
public override function get base():TextureBase { return mBase; }

/** @inheritDoc */
public override function get format():String { return mFormat; }

/** @inheritDoc */
public override function get width():Number { return mWidth / mScale; }

Expand Down
3 changes: 3 additions & 0 deletions starling/src/starling/textures/SubTexture.as
Expand Up @@ -103,6 +103,9 @@ package starling.textures
/** @inheritDoc */
public override function get base():TextureBase { return mParent.base; }

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

/** @inheritDoc */
public override function get width():Number { return mParent.width * mClipping.width; }

Expand Down

0 comments on commit 7739a9e

Please sign in to comment.