Kaynağa Gözat

slightly refactored texture handling (using format enum)

ncannasse 12 yıl önce
ebeveyn
işleme
641dc8ec28
3 değiştirilmiş dosya ile 34 ekleme ve 34 silme
  1. 25 31
      h3d/impl/MemoryManager.hx
  2. 6 0
      h3d/mat/Data.hx
  3. 3 3
      h3d/mat/Texture.hx

+ 25 - 31
h3d/impl/MemoryManager.hx

@@ -224,15 +224,29 @@ class MemoryManager {
 		#end
 	}
 	
-	function newTexture(t, w, h, cubic, target, mm, allocPos) {
-		var t = new h3d.mat.Texture(this, t, w, h, cubic, target, mm);
-		tdict.set(t, t.t);
-		textures.push(t.t);
+	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 ) {
+		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;
+		}
+		if( t.isCubic )
+			t.t = ctx.createCubeTexture(t.width, fmt, t.isTarget, t.mipLevels);
+		else
+			t.t = ctx.createTexture(t.width, t.height, fmt, t.isTarget, t.mipLevels);
+		tdict.set(t, t.t);
+		textures.push(t.t);
+	}
 
 	@:allow(h3d.impl.Indexes.dispose)
 	function deleteIndexes( i : Indexes ) {
@@ -252,16 +266,10 @@ class MemoryManager {
 
 	@:allow(h3d.mat.Texture.resize)
 	function resizeTexture( t : h3d.mat.Texture, width, height ) {
-		if( t.t != null ) {
-			textures.remove(t.t);
-			t.t.dispose();
-			t.t = null;
-		}
-		t.t = t.isCubic ? ctx.createCubeTexture(width, flash.display3D.Context3DTextureFormat.BGRA, t.isTarget) : ctx.createTexture(width, height, flash.display3D.Context3DTextureFormat.BGRA, t.isTarget);
+		t.dispose();
 		t.width = width;
 		t.height = height;
-		tdict.set(t, t.t);
-		textures.push(t.t);
+		initTexture(t);
 	}
 	
 	public function readAtfHeader( data : haxe.io.Bytes ) {
@@ -290,13 +298,7 @@ class MemoryManager {
 	public function allocAtfTexture( width : Int, height : Int, mipLevels : Int = 0, alpha : Bool = false, compress : Bool = false, cubic : Bool = false, ?allocPos : AllocPos ) {
 		freeTextures();
 		var fmt = compress ? (alpha ? flash.display3D.Context3DTextureFormat.COMPRESSED_ALPHA : flash.display3D.Context3DTextureFormat.COMPRESSED) : flash.display3D.Context3DTextureFormat.BGRA;
-		var t = if( cubic )
-			ctx.createCubeTexture(width, fmt, false, mipLevels)
-		else
-			ctx.createTexture(width, height, fmt, false, mipLevels);
-		var t = newTexture(t, width, height, cubic, false, mipLevels, allocPos);
-		t.atfProps = { alpha : alpha, compress : compress };
-		return t;
+		return newTexture(compress ? AtfCompressed(alpha) : Atf, width, height, cubic, false, mipLevels, allocPos);
 	}
 	
 	public function allocTexture( width : Int, height : Int, mipMap = false, ?allocPos : AllocPos ) {
@@ -306,12 +308,12 @@ class MemoryManager {
 			while( width > (1 << levels) && height > (1 << levels) )
 				levels++;
 		}
-		return newTexture(ctx.createTexture(width, height, flash.display3D.Context3DTextureFormat.BGRA, false, levels), width, height, false, false, levels, allocPos);
+		return newTexture(Rgba, width, height, false, false, levels, allocPos);
 	}
 	
 	public function allocTargetTexture( width : Int, height : Int, ?allocPos : AllocPos ) {
 		freeTextures();
-		return newTexture(ctx.createTexture(width, height, flash.display3D.Context3DTextureFormat.BGRA, true, 0), width, height, false, true, 0, allocPos);
+		return newTexture(Rgba, width, height, false, true, 0, allocPos);
 	}
 
 	public function makeTexture( ?bmp : flash.display.BitmapData, ?mbmp : h3d.mat.Bitmap, hasMipMap = false, ?allocPos : AllocPos ) {
@@ -334,7 +336,7 @@ class MemoryManager {
 			while( size > (1 << levels) )
 				levels++;
 		}
-		return newTexture(ctx.createCubeTexture(size, flash.display3D.Context3DTextureFormat.BGRA, false, levels), size, size, true, false, levels, allocPos);
+		return newTexture(Rgba, size, size, true, false, levels, allocPos);
 	}
 
 	public function allocIndex( indices : flash.Vector<UInt> ) {
@@ -552,15 +554,7 @@ class MemoryManager {
 				t.dispose();
 			else {
 				textures.remove(t.t);
-				var fmt = flash.display3D.Context3DTextureFormat.BGRA;
-				if( t.atfProps != null )
-					fmt = t.atfProps.compress ? (t.atfProps.alpha ? flash.display3D.Context3DTextureFormat.COMPRESSED_ALPHA : flash.display3D.Context3DTextureFormat.COMPRESSED) : flash.display3D.Context3DTextureFormat.BGRA;
-				if( t.isCubic )
-					t.t = ctx.createCubeTexture(t.width, fmt, t.isTarget, t.mipLevels);
-				else
-					t.t = ctx.createTexture(t.width, t.height, fmt, t.isTarget, t.mipLevels);
-				tdict.set(t, t.t);
-				textures.push(t.t);
+				initTexture(t);
 				t.onContextLost();
 			}
 		}

+ 6 - 0
h3d/mat/Data.hx

@@ -46,3 +46,9 @@ enum Wrap {
 	Clamp;
 	Repeat;
 }
+
+enum TextureFormat {
+	Rgba;
+	Atf;
+	AtfCompressed( alpha : Bool );
+}

+ 3 - 3
h3d/mat/Texture.hx

@@ -9,7 +9,6 @@ class Texture {
 	
 	var t : flash.display3D.textures.TextureBase;
 	var mem : h3d.impl.MemoryManager;
-	var atfProps : { alpha : Bool, compress : Bool };
 	#if debug
 	var allocPos : h3d.impl.AllocPos;
 	#end
@@ -19,6 +18,7 @@ class Texture {
 	public var isCubic(default, null) : Bool;
 	public var isTarget(default, null) : Bool;
 	public var mipLevels(default, null) : Int;
+	public var format(default, null) : TextureFormat;
 	
 	var bits : Int;
 	public var mipMap(default,set) : MipMap;
@@ -31,10 +31,10 @@ class Texture {
 	**/
 	public var onContextLost : Void -> Void;
 	
-	function new(m, t, w, h, c, ta, mm) {
+	function new(m, fmt, w, h, c, ta, mm) {
 		this.id = ++UID;
+		this.format = fmt;
 		this.mem = m;
-		this.t = t;
 		this.isTarget = ta;
 		this.width = w;
 		this.height = h;