Bläddra i källkod

Can use texture cache with array of flags.

clementlandrin 1 år sedan
förälder
incheckning
ff13041784
3 ändrade filer med 33 tillägg och 11 borttagningar
  1. 30 8
      h3d/impl/TextureCache.hx
  2. 1 1
      h3d/pass/Blur.hx
  3. 2 2
      h3d/pass/CubeShadowMap.hx

+ 30 - 8
h3d/impl/TextureCache.hx

@@ -40,12 +40,23 @@ class TextureCache {
 		position = 0;
 	}
 
-	function lookupTarget( name, width, height, format, isCube, isMipmapped ) {
+	function lookupTarget( name, width, height, format, flags : Array<h3d.mat.Data.TextureFlags> ) {
 		var t = cache[position];
 		// look for a suitable candidate
 		for( i in position+1...cache.length ) {
 			var t2 = cache[i];
-			if( t2 != null && !t2.isDisposed() && t2.width == width && t2.height == height && t2.format == format && isCube == t2.flags.has(Cube) ) {
+			if( t2 != null && !t2.isDisposed() && t2.width == width && t2.height == height && t2.format == format ) {
+				if ( flags != null ) {
+					var fitFlags = true;
+					for ( f in flags ) {
+						if ( !t2.flags.has(f) ) {
+							fitFlags = false;
+							break;
+						}
+					}
+					if ( !fitFlags )
+						continue;
+				}
 				// swap
 				cache[position] = t2;
 				cache[i] = t;
@@ -57,9 +68,9 @@ class TextureCache {
 			t.dispose();
 			t = null;
 		}
-		var flags : Array<h3d.mat.Data.TextureFlags> = [Target];
-		if( isCube ) flags.push(Cube);
-		if( isMipmapped ) flags.push(MipMapped);
+		if ( flags == null ) flags = [];
+		if ( !flags.contains(Target) )
+			flags.push(Target);
 		var newt = new h3d.mat.Texture(width, height, flags, format);
 		// make the texture disposable if we're out of memory
 		newt.realloc = function() {};
@@ -70,11 +81,22 @@ class TextureCache {
 		return newt;
 	}
 
-	public function allocTarget( name : String, width : Int, height : Int, defaultDepth=true, ?format:hxd.PixelFormat, isCube = false, isMipmapped = false ) {
+	public function allocTarget( name : String, width : Int, height : Int, defaultDepth=true, ?format:hxd.PixelFormat, flags : Array<h3d.mat.Data.TextureFlags> = null ) {
 		var t = cache[position];
 		if( format == null ) format = defaultFormat;
-		if( t == null || t.isDisposed() || t.width != width || t.height != height || t.format != format || isCube != t.flags.has(Cube) )
-			t = lookupTarget(name,width,height,format,isCube,isMipmapped);
+		var alloc = false;
+		if( t == null || t.isDisposed() || t.width != width || t.height != height || t.format != format )
+			alloc = true;
+		if ( !alloc && flags != null ) {
+			for ( f in flags ) {
+				if ( !t.flags.has(f) ) {
+					alloc = true;
+					break;
+				}
+			}
+		}
+		if ( alloc )
+			t = lookupTarget(name,width,height,format,flags);
 		t.depthBuffer = defaultDepth ? defaultDepthBuffer : null;
 		t.setName(name);
 		position++;

+ 1 - 1
h3d/pass/Blur.hx

@@ -135,7 +135,7 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 
 		var isCube = src.flags.has(Cube);
 		var faceCount = isCube ? 6 : 1;
-		var tmp = ctx.textures.allocTarget(src.name+"BlurTmp", src.width, src.height, false, src.format, isCube);
+		var tmp = ctx.textures.allocTarget(src.name+"BlurTmp", src.width, src.height, false, src.format, isCube ? [Cube] : null);
 
 		shader.Quality = values.length;
 		shader.values = values;

+ 2 - 2
h3d/pass/CubeShadowMap.hx

@@ -169,7 +169,7 @@ class CubeShadowMap extends Shadows {
 			return;
 		}
 
-		var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, true);
+		var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, [Cube]);
 		if( depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed() ) {
 			if( depth != null ) depth.dispose();
 			depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
@@ -221,7 +221,7 @@ class CubeShadowMap extends Shadows {
 		var validBakedTexture = (staticTexture != null && staticTexture.width == dynamicTex.width);
 		var merge : h3d.mat.Texture = null;
 		if( mode == Mixed && !ctx.computingStatic && validBakedTexture)
-			merge = ctx.textures.allocTarget("mergedPointShadowMap", size, size, false, format, true);
+			merge = ctx.textures.allocTarget("mergedPointShadowMap", size, size, false, format, [Cube]);
 
 		if( mode == Mixed && !ctx.computingStatic && merge != null ) {
 			for( i in 0 ... 6 ) {