فهرست منبع

changed h3d.mat.Texture API to use new+flags

Nicolas Cannasse 11 سال پیش
والد
کامیت
295466ef1a
10فایلهای تغییر یافته به همراه99 افزوده شده و 184 حذف شده
  1. 15 25
      h2d/CachedBitmap.hx
  2. 4 16
      h2d/Scene.hx
  3. 2 2
      h2d/Tile.hx
  4. 6 74
      h3d/impl/MemoryManager.hx
  5. 23 31
      h3d/impl/Stage3dDriver.hx
  6. 15 4
      h3d/mat/Data.hx
  7. 29 24
      h3d/mat/Texture.hx
  8. 1 1
      h3d/parts/Editor.hx
  9. 3 6
      hxd/res/Image.hx
  10. 1 1
      tools/fbx/Viewer.hx

+ 15 - 25
h2d/CachedBitmap.hx

@@ -2,14 +2,11 @@ package h2d;
 
 class CachedBitmap extends Drawable {
 
-	var tex : h3d.mat.Texture;
 	public var width(default, set) : Int;
 	public var height(default, set) : Int;
 	public var freezed : Bool;
 	
 	var renderDone : Bool;
-	var realWidth : Int;
-	var realHeight : Int;
 	var tile : Tile;
 	
 	public function new( ?parent, width = -1, height = -1 ) {
@@ -19,11 +16,10 @@ class CachedBitmap extends Drawable {
 	}
 
 	function clean() {
-		if( tex != null ) {
-			tex.dispose();
-			tex = null;
+		if( tile != null ) {
+			tile.dispose();
+			tile = null;
 		}
-		tile = null;
 	}
 
 	override function onDelete() {
@@ -38,25 +34,20 @@ class CachedBitmap extends Drawable {
 	}
 
 	function set_height(h) {
-		if( tex != null ) {
-			tex.dispose();
-			tex = null;
-		}
+		clean();
 		height = h;
 		return h;
 	}
 	
 	public function getTile() {
 		if( tile == null ) {
-			var tw = 1, th = 1;
-			var engine = h3d.Engine.getCurrent();
-			realWidth = width < 0 ? engine.width : width;
-			realHeight = height < 0 ? engine.height : height;
-			while( tw < realWidth ) tw <<= 1;
-			while( th < realHeight ) th <<= 1;
-			tex = engine.mem.allocTargetTexture(tw, th);
+			var scene = getScene();
+			if( scene == null ) return null;
+			var tw = width < 0 ? scene.width : width;
+			var th = height < 0 ? scene.height : height;
+			var tex = new h3d.mat.Texture(tw, th, [Target]);
 			renderDone = false;
-			tile = new Tile(tex,0, 0, realWidth, realHeight);
+			tile = Tile.fromTexture(tex);
 		}
 		return tile;
 	}
@@ -72,7 +63,8 @@ class CachedBitmap extends Drawable {
 				c.posChanged = true;
 			posChanged = false;
 		}
-		if( tex != null && ((width < 0 && tex.width < ctx.engine.width) || (height < 0 && tex.height < ctx.engine.height)) )
+		var scene = getScene();
+		if( tile != null && ((width < 0 && scene.width != tile.width) || (height < 0 && scene.height != tile.height)) )
 			clean();
 		var tile = getTile();
 		if( !freezed || !renderDone ) {
@@ -87,8 +79,8 @@ class CachedBitmap extends Drawable {
 			absY = 0;
 			
 			// adds a pixels-to-viewport transform
-			var w = 2 / tex.width;
-			var h = -2 / tex.height;
+			var w = 2 / tile.width;
+			var h = -2 / tile.height;
 			absX = absX * w - 1;
 			absY = absY * h + 1;
 			matA *= w;
@@ -102,12 +94,10 @@ class CachedBitmap extends Drawable {
 				c.sync(ctx);
 			}
 
-			ctx.engine.setTarget(tex);
-			ctx.engine.setRenderZone(0, 0, realWidth, realHeight);
+			ctx.engine.setTarget(tile.getTexture());
 			for( c in childs )
 				c.drawRec(ctx);
 			ctx.engine.setTarget(null);
-			ctx.engine.setRenderZone();
 			
 			// restore
 			matA = oldA;

+ 4 - 16
h2d/Scene.hx

@@ -99,24 +99,15 @@ class Scene extends Layers implements h3d.IDrawable {
 		case EPush: cancelFocus = true; checkPush = true;
 		case ERelease: checkPush = true;
 		case EKeyUp, EKeyDown, EWheel:
-			if( currentFocus != null )
+			if( currentFocus != null ) {
 				currentFocus.handleEvent(event);
-			else {
-				if( currentOver != null ) {
-					event.propagate = true;
-					currentOver.handleEvent(event);
-					if( !event.propagate ) return;
-				}
-				dispatchListeners(event);
+				if( !event.propagate )
+					return;
 			}
-			return;
 		default:
 		}
 		for( i in interactive ) {
 			
-
-			// TODO : we are not sure that the positions are correctly updated !
-			
 			// this is a bit tricky since we are not in the not-euclide viewport space
 			// (r = ratio correction)
 			var dx = rx - i.absX;
@@ -428,10 +419,7 @@ class Scene extends Layers implements h3d.IDrawable {
 	public function captureBitmap( ?target : Tile ) {
 		var engine = h3d.Engine.getCurrent();
 		if( target == null ) {
-			var tw = 1, th = 1;
-			while( tw < width ) tw <<= 1;
-			while( th < height ) th <<= 1;
-			var tex = engine.mem.allocTargetTexture(tw, th);
+			var tex = new h3d.mat.Texture(width, height, [Target]);
 			target = new Tile(tex,0, 0, width, height);
 		}
 		engine.begin();

+ 2 - 2
h2d/Tile.hx

@@ -170,7 +170,7 @@ class Tile {
 			w <<= 1;
 		while( h < bmp.height )
 			h <<= 1;
-		var tex = h3d.Engine.getCurrent().mem.allocTexture(w, h, false, allocPos);
+		var tex = new h3d.mat.Texture(w, h, allocPos);
 		var t = new Tile(tex, 0, 0, bmp.width, bmp.height);
 		t.upload(bmp);
 		return t;
@@ -185,7 +185,7 @@ class Tile {
 			w <<= 1;
 		while( h < bmp.height )
 			h <<= 1;
-		var tex = h3d.Engine.getCurrent().mem.allocTexture(w, h, false, allocPos);
+		var tex = new h3d.mat.Texture(w, h, allocPos);
 		for( y in 0...Std.int(bmp.height / height) ) {
 			var a = [];
 			tl[y] = a;

+ 6 - 74
h3d/impl/MemoryManager.hx

@@ -231,21 +231,6 @@ class MemoryManager {
 		#end
 	}
 	
-	function newTexture(fmt, w, h, cubic, target, mm, allocPos) {
-		var t = new h3d.mat.Texture(this, fmt, w, h, cubic, target, mm);
-		#if debug
-		t.allocPos = allocPos;
-		#end
-		initTexture(t);
-		return t;
-	}
-	
-	function initTexture( t : h3d.mat.Texture ) {
-		t.t = driver.allocTexture(t);
-		tdict.set(t, t.t);
-		textures.push(t.t);
-	}
-
 	@:allow(h3d.impl.Indexes.dispose)
 	function deleteIndexes( i : Indexes ) {
 		idict.remove(i);
@@ -262,67 +247,14 @@ class MemoryManager {
 		t.t = null;
 	}
 
-	@:allow(h3d.mat.Texture.resize)
-	function resizeTexture( t : h3d.mat.Texture, width, height ) {
-		t.dispose();
-		t.width = width;
-		t.height = height;
-		initTexture(t);
-	}
-	
-	public function readAtfHeader( data : haxe.io.Bytes ) {
-		var cubic = (data.get(6) & 0x80) != 0;
-		var alpha = false, compress = false;
-		switch( data.get(6) & 0x7F ) {
-		case 0:
-		case 1: alpha = true;
-		case 2: compress = true;
-		case 3, 4: alpha = true; compress = true;
-		case f: throw "Invalid ATF format " + f;
-		}
-		var width = 1 << data.get(7);
-		var height = 1 << data.get(8);
-		var mips = data.get(9) - 1;
-		return {
-			width : width,
-			height : height,
-			cubic : cubic,
-			alpha : alpha,
-			compress : compress,
-			mips : mips,
-		};
-	}
-
-	public function allocCustomTexture( fmt : h3d.mat.Data.TextureFormat, width : Int, height : Int, mipLevels : Int = 0, cubic : Bool = false, target : Bool = false, ?allocPos : AllocPos ) {
-		freeTextures();
-		return newTexture(fmt, width, height, cubic, target, mipLevels, allocPos);
-	}
-	
-	public function allocTexture( width : Int, height : Int, ?mipMap = false, ?allocPos : AllocPos ) {
+	@:allow(h3d.mat.Texture.alloc)
+	function allocTexture( t : h3d.mat.Texture ) {
 		freeTextures();
-		var levels = 0;
-		if( mipMap ) {
-			while( width > (1 << levels) && height > (1 << levels) )
-				levels++;
-		}
-		return newTexture(Rgba, width, height, false, false, levels, allocPos);
+		t.t = driver.allocTexture(t);
+		tdict.set(t, t.t);
+		textures.push(t.t);
 	}
 	
-	public function allocTargetTexture( width : Int, height : Int, ?allocPos : AllocPos ) {
-		freeTextures();
-		return newTexture(Rgba, width, height, false, true, 0, allocPos);
-	}
-
-	public function allocCubeTexture( size : Int, ?mipMap = false, ?allocPos : AllocPos ) {
-		freeTextures();
-		var levels = 0;
-		if( mipMap ) {
-			while( size > (1 << levels) )
-				levels++;
-		}
-		return newTexture(Rgba, size, size, true, false, levels, allocPos);
-	}
-
 	public function allocIndex( indices : hxd.IndexBuffer, pos = 0, count = -1 ) {
 		if( count < 0 ) count = indices.length;
 		var ibuf = driver.allocIndexes(count);
@@ -523,7 +455,7 @@ class MemoryManager {
 				t.dispose();
 			else {
 				textures.remove(t.t);
-				initTexture(t);
+				//initTexture(t);
 				t.onContextLost();
 			}
 		}

+ 23 - 31
h3d/impl/Stage3dDriver.hx

@@ -162,36 +162,40 @@ class Stage3dDriver extends Driver {
 		return ctx.createIndexBuffer(count);
 	}
 	
+	function getMipLevels( t : h3d.mat.Texture ) {
+		if( !t.flags.has(MipMapped) )
+			return 0;
+		var levels = 0;
+		while( t.width > (1 << levels) || t.height > (1 << levels) )
+			levels++;
+		return levels;
+	}
+	
 	override function allocTexture( t : h3d.mat.Texture ) : Texture {
-		var fmt = switch( t.format ) {
-		case Rgba, Atf:
-			flash.display3D.Context3DTextureFormat.BGRA;
-		case AtfCompressed(alpha):
-			alpha ? flash.display3D.Context3DTextureFormat.COMPRESSED_ALPHA : flash.display3D.Context3DTextureFormat.COMPRESSED;
-		}
+		var fmt = flash.display3D.Context3DTextureFormat.BGRA;
 		var rect = false;
-		if( t.isTarget && !t.isCubic && t.mipLevels == 0 ) {
+		if( t.flags.has(Target) && !t.flags.has(Cubic) && !t.flags.has(MipMapped) ) {
 			var tw = 1, th = 1;
 			while( tw < t.width ) tw <<= 1;
 			while( th < t.height) th <<= 1;
 			if( tw != t.width || th != t.height )
 				rect = true;
 		}
-		return if( t.isCubic )
-			ctx.createCubeTexture(t.width, fmt, t.isTarget, t.mipLevels);
+		return if( t.flags.has(Cubic) )
+			ctx.createCubeTexture(t.width, fmt, t.flags.has(Target), getMipLevels(t));
 		else if( rect ) {
 			#if !flash11_8
 			throw "Support for rectangle texture requires Flash 11.8+ compilation";
 			#else
-			ctx.createRectangleTexture(t.width, t.height, fmt, t.isTarget);
+			ctx.createRectangleTexture(t.width, t.height, fmt, t.flags.has(Target));
 			#end
 		}
 		else
-			ctx.createTexture(t.width, t.height, fmt, t.isTarget, t.mipLevels);
+			ctx.createTexture(t.width, t.height, fmt, t.flags.has(Target), getMipLevels(t));
 	}
 
 	override function uploadTextureBitmap( t : h3d.mat.Texture, bmp : hxd.BitmapData, mipLevel : Int, side : Int ) {
-		if( t.isCubic ) {
+		if( t.flags.has(Cubic) ) {
 			var t = flash.Lib.as(t.t, flash.display3D.textures.CubeTexture);
 			t.uploadFromBitmapData(bmp.toNative(), side, mipLevel);
 		}
@@ -204,25 +208,13 @@ class Stage3dDriver extends Driver {
 	override function uploadTexturePixels( t : h3d.mat.Texture, pixels : hxd.Pixels, mipLevel : Int, side : Int ) {
 		pixels.convert(BGRA);
 		var data = pixels.bytes.getData();
-		switch( t.format ) {
-		case Atf, AtfCompressed(_):
-			if( t.isCubic ) {
-				var t = flash.Lib.as(t.t, flash.display3D.textures.CubeTexture);
-				t.uploadCompressedTextureFromByteArray(data, 0);
-			}
-			else {
-				var t = flash.Lib.as(t.t,  flash.display3D.textures.Texture);
-				t.uploadCompressedTextureFromByteArray(data, 0);
-			}
-		default:
-			if( t.isCubic ) {
-				var t = flash.Lib.as(t.t, flash.display3D.textures.CubeTexture);
-				t.uploadFromByteArray(data, 0, side, mipLevel);
-			}
-			else {
-				var t = flash.Lib.as(t.t,  flash.display3D.textures.Texture);
-				t.uploadFromByteArray(data, 0, mipLevel);
-			}
+		if( t.flags.has(Cubic) ) {
+			var t = flash.Lib.as(t.t, flash.display3D.textures.CubeTexture);
+			t.uploadFromByteArray(data, 0, side, mipLevel);
+		}
+		else {
+			var t = flash.Lib.as(t.t,  flash.display3D.textures.Texture);
+			t.uploadFromByteArray(data, 0, mipLevel);
 		}
 	}
 	

+ 15 - 4
h3d/mat/Data.hx

@@ -51,10 +51,21 @@ enum Filter {
 enum Wrap {
 	Clamp;
 	Repeat;
+	//Mirrored;
 }
 
-enum TextureFormat {
-	Rgba;
-	Atf;
-	AtfCompressed( alpha : Bool );
+enum Operation {
+	Add;
+	Sub;
+	ReverseSub;
+}
+
+enum TextureFlags {
+	Target;
+	Cubic;
+	/**
+		Used to prevent culling inversion
+	**/
+	NoFlipY;
+	MipMapped;
 }

+ 29 - 24
h3d/mat/Texture.hx

@@ -1,5 +1,4 @@
 package h3d.mat;
-
 import h3d.mat.Data;
 
 @:allow(h3d)
@@ -16,10 +15,7 @@ class Texture {
 	public var name(default, null) : String;
 	public var width(default, null) : Int;
 	public var height(default, null) : Int;
-	public var isCubic(default, null) : Bool;
-	public var isTarget(default, null) : Bool;
-	public var mipLevels(default, null) : Int;
-	public var format(default, null) : TextureFormat;
+	public var flags(default, null) : haxe.EnumFlags<TextureFlags>;
 	
 	var bits : Int;
 	public var mipMap(default,set) : MipMap;
@@ -32,19 +28,28 @@ class Texture {
 	**/
 	public var onContextLost : Void -> Void;
 	
-	function new(m, fmt, w, h, c, ta, mm) {
+	public function new(w, h, ?flags : Array<TextureFlags>, ?allocPos : h3d.impl.AllocPos ) {
+		this.mem = h3d.Engine.getCurrent().mem;
 		this.id = ++UID;
-		this.format = fmt;
-		this.mem = m;
-		this.isTarget = ta;
+		this.flags = new haxe.EnumFlags();
+		if( flags != null )
+			for( f in flags )
+				this.flags.set(f);
 		this.width = w;
 		this.height = h;
-		this.isCubic = c;
-		this.mipLevels = mm;
-		this.mipMap = mm > 0 ? Nearest : None;
+		this.mipMap = this.flags.has(MipMapped) ? Nearest : None;
 		this.filter = Linear;
 		this.wrap = Clamp;
 		bits &= 0x7FFF;
+		#if debug
+		this.allocPos = allocPos;
+		#end
+		alloc();
+	}
+	
+	function alloc() {
+		if( t == null )
+			mem.allocTexture(this);
 	}
 	
 	function toString() {
@@ -82,7 +87,10 @@ class Texture {
 	}
 	
 	public function resize(width, height) {
-		mem.resizeTexture(this, width, height);
+		dispose();
+		this.width = width;
+		this.height = height;
+		alloc();
 	}
 
 	/*
@@ -134,26 +142,27 @@ class Texture {
 	}
 	
 	public static function fromBitmap( bmp : hxd.BitmapData, ?allocPos : h3d.impl.AllocPos ) {
-		var mem = h3d.Engine.getCurrent().mem;
-		var t = mem.allocTexture(bmp.width, bmp.height, false, allocPos);
+		var t = new Texture(bmp.width, bmp.height, allocPos);
 		t.uploadBitmap(bmp);
 		return t;
 	}
 	
 	public static function fromPixels( pixels : hxd.Pixels, ?allocPos : h3d.impl.AllocPos ) {
-		var mem = h3d.Engine.getCurrent().mem;
-		var t = mem.allocTexture(pixels.width, pixels.height, false, allocPos);
+		var t = new Texture(pixels.width, pixels.height, allocPos);
 		t.uploadPixels(pixels);
 		return t;
 	}
 	
 	static var tmpPixels : hxd.Pixels = null;
+	static var COLOR_CACHE = new Map<Int,h3d.mat.Texture>();
 	/**
 		Creates a 1x1 texture using the ARGB color passed as parameter.
 	**/
 	public static function fromColor( color : Int, ?allocPos : h3d.impl.AllocPos ) {
-		var mem = h3d.Engine.getCurrent().mem;
-		var t = mem.allocTexture(1, 1, false, allocPos);
+		var t = COLOR_CACHE.get(color);
+		if( t != null && !t.isDisposed() )
+			return t;
+		var t = new Texture(1, 1, null, allocPos);
 		if( tmpPixels == null ) tmpPixels = new hxd.Pixels(1, 1, haxe.io.Bytes.alloc(4), BGRA);
 		tmpPixels.format = BGRA;
 		tmpPixels.bytes.set(0, color & 0xFF);
@@ -161,12 +170,8 @@ class Texture {
 		tmpPixels.bytes.set(2, (color>>16) & 0xFF);
 		tmpPixels.bytes.set(3, color>>>24);
 		t.uploadPixels(tmpPixels);
+		COLOR_CACHE.set(color, t);
 		return t;
 	}
-	
-	public static function alloc( width : Int, height : Int, isTarget = false, ?allocPos : h3d.impl.AllocPos ) {
-		var engine = h3d.Engine.getCurrent();
-		return isTarget ? engine.mem.allocTargetTexture(width, height, allocPos) : engine.mem.allocTexture(width, height, null, allocPos);
-	}
 
 }

+ 1 - 1
h3d/parts/Editor.hx

@@ -751,7 +751,7 @@ class Editor extends h2d.Sprite implements Randomized {
 		}
 		curveBG = h2d.Tile.fromBitmap(bg);
 		bg.dispose();
-		curveTexture = 	h2d.Tile.fromTexture(h3d.Engine.getCurrent().mem.allocTexture(512, 512)).sub(0, 0, curveBG.width, curveBG.height);
+		curveTexture = h2d.Tile.fromTexture(new h3d.mat.Texture(512, 512)).sub(0, 0, curveBG.width, curveBG.height);
 	}
 	
 	function rebuildCurve() {

+ 3 - 6
hxd/res/Image.hx

@@ -139,21 +139,18 @@ class Image extends Resource {
 		}
 		getSize();
 		var w = inf.width, h = inf.height;
-		var tw = 1, th = 1;
-		while( tw < w ) tw <<= 1;
-		while( th < h ) th <<= 1;
 
 		if( inf.isPNG && entry.isAvailable ) {
 			// direct upload
 			needResize = false;
-			tex = h3d.Engine.getCurrent().mem.allocTexture(tw, th, false);
+			tex = new h3d.mat.Texture(w, h);
 		} else {
 			// create a temp 1x1 texture while we're loading
 			tex = h3d.mat.Texture.fromColor(0xFF0000FF);
 			needResize = true;
 			@:privateAccess {
-				tex.width = tw;
-				tex.height = th;
+				tex.width = w;
+				tex.height = h;
 			}
 		}
 		loadTexture();

+ 1 - 1
tools/fbx/Viewer.hx

@@ -267,7 +267,7 @@ class Viewer {
 	}
 	
 	function textureLoader( textureName : String, matData : h3d.fbx.Data.FbxNode ) {
-		var t = engine.mem.allocTexture(1024, 1024);
+		var t = new h3d.mat.Texture(1024, 1024);
 		var bmp = new flash.display.BitmapData(1024, 1024, true, 0xFFFF0000);
 		var mat = new h3d.mat.MeshMaterial(t);
 		t.uploadBitmap(hxd.BitmapData.fromNative(bmp));