Browse Source

refactor texture cache: move to RenderContext, no cache per pass

ncannasse 7 years ago
parent
commit
6d3f48e5b6

+ 2 - 4
h2d/RenderContext.hx

@@ -7,7 +7,6 @@ class RenderContext extends h3d.impl.RenderContext {
 	public var globalAlpha = 1.;
 	public var buffer : hxd.FloatBuffer;
 	public var bufPos : Int;
-	public var textures : h3d.impl.TextureCache;
 	public var scene : h2d.Scene;
 	public var defaultSmooth : Bool = false;
 	public var killAlpha : Bool;
@@ -66,7 +65,6 @@ class RenderContext extends h3d.impl.RenderContext {
 		targetsStack = [];
 		targetsStackIndex = 0;
 		filterStack = [];
-		textures = new h3d.impl.TextureCache();
 	}
 
 	public function dispose() {
@@ -98,11 +96,11 @@ class RenderContext extends h3d.impl.RenderContext {
 		baseShaderList.next = null;
 		initShaders(baseShaderList);
 		engine.selectMaterial(pass);
-		textures.begin(this);
+		textures.begin();
 	}
 
 	public function allocTarget(name, filter = false, size = 0) {
-		var t = textures.allocTarget(name, this, scene.width >> size, scene.height >> size, false);
+		var t = textures.allocTarget(name, scene.width >> size, scene.height >> size, false);
 		t.filter = filter ? Linear : Nearest;
 		return t;
 	}

+ 1 - 1
h2d/Sprite.hx

@@ -597,7 +597,7 @@ class Sprite {
 
 		if( width <= 0 || height <= 0 || total.xMax < total.xMin ) return;
 
-		var t = ctx.textures.allocTarget("filterTemp", ctx, width, height, false);
+		var t = ctx.textures.allocTarget("filterTemp", width, height, false);
 		ctx.pushTarget(t, xMin, yMin, width, height);
 		ctx.engine.clear(0);
 

+ 1 - 1
h2d/filter/Ambient.hx

@@ -17,7 +17,7 @@ class Ambient extends AbstractMask {
 	inline function set_invert(v) return pass.shader.maskInvert = v;
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
-		var out = ctx.textures.allocTarget("ambientTmp", ctx, t.width, t.height, false);
+		var out = ctx.textures.allocTarget("ambientTmp", t.width, t.height, false);
 		pass.apply(t.getTexture(), out, getMaskTexture(t), maskMatrix);
 		return h2d.Tile.fromTexture(out);
 	}

+ 1 - 1
h2d/filter/Bloom.hx

@@ -20,7 +20,7 @@ class Bloom extends Blur {
 	inline function set_power(v) return bloom.shader.power = v;
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
-		var dst = ctx.textures.allocTarget("dest", ctx, t.width, t.height, false);
+		var dst = ctx.textures.allocTarget("dest", t.width, t.height, false);
 		h3d.pass.Copy.run(t.getTexture(), dst);
 		var blurred = super.draw(ctx, t);
 		bloom.shader.texture = blurred.getTexture();

+ 2 - 2
h2d/filter/Blur.hx

@@ -51,10 +51,10 @@ class Blur extends Filter {
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
 		var out = t.getTexture();
 		if( reduceSize > 0 ) {
-			out = ctx.textures.allocTarget("blurOut", ctx, t.width >> reduceSize, t.height >> reduceSize, false);
+			out = ctx.textures.allocTarget("blurOut", t.width >> reduceSize, t.height >> reduceSize, false);
 			h3d.pass.Copy.run(t.getTexture(), out);
 		}
-		var tex = ctx.textures.allocTarget("blurTmp", ctx,out.width, out.height, false);
+		var tex = ctx.textures.allocTarget("blurTmp", out.width, out.height, false);
 		tex.filter = smooth ? Linear : Nearest;
 		pass.apply(out, tex);
 		if( reduceSize <= 0 )

+ 1 - 1
h2d/filter/ColorMatrix.hx

@@ -16,7 +16,7 @@ class ColorMatrix extends Filter {
 	inline function set_matrix(m) return pass.matrix = m;
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
-		var tout = ctx.textures.allocTarget("colorMatrixOut", ctx, t.width, t.height, false);
+		var tout = ctx.textures.allocTarget("colorMatrixOut", t.width, t.height, false);
 		pass.apply(t.getTexture(), tout);
 		return h2d.Tile.fromTexture(tout);
 	}

+ 1 - 1
h2d/filter/Displacement.hx

@@ -28,7 +28,7 @@ class Displacement extends Filter {
 	}
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
-		var out = ctx.textures.allocTarget("displacementOutput", ctx, t.width, t.height, false);
+		var out = ctx.textures.allocTarget("displacementOutput", t.width, t.height, false);
 		ctx.engine.pushTarget(out);
 		var s = disp.shader;
 		s.texture = t.getTexture();

+ 2 - 2
h2d/filter/DropShadow.hx

@@ -21,9 +21,9 @@ class DropShadow extends Glow {
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
 		setParams();
-		var save = ctx.textures.allocTarget("glowSave", ctx, t.width, t.height, false);
+		var save = ctx.textures.allocTarget("glowSave", t.width, t.height, false);
 		h3d.pass.Copy.run(t.getTexture(), save, None);
-		var glowTmpTex = (quality == 0) ? null : ctx.textures.allocTarget("glowTmp", ctx, t.width, t.height, false);
+		var glowTmpTex = (quality == 0) ? null : ctx.textures.allocTarget("glowTmp", t.width, t.height, false);
 		pass.apply(save, glowTmpTex);
 		var dx = Math.round(Math.cos(angle) * distance);
 		var dy = Math.round(Math.sin(angle) * distance);

+ 2 - 2
h2d/filter/Glow.hx

@@ -23,9 +23,9 @@ class Glow extends Blur {
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
 		setParams();
-		var save = ctx.textures.allocTarget("glowSave", ctx, t.width, t.height, false);
+		var save = ctx.textures.allocTarget("glowSave", t.width, t.height, false);
 		h3d.pass.Copy.run(t.getTexture(), save, None);
-		pass.apply(t.getTexture(), ctx.textures.allocTarget("glowTmp", ctx, t.width, t.height, false));
+		pass.apply(t.getTexture(), ctx.textures.allocTarget("glowTmp", t.width, t.height, false));
 		if( knockout )
 			h3d.pass.Copy.run(save, t.getTexture(), Erase);
 		else

+ 1 - 1
h2d/filter/Mask.hx

@@ -41,7 +41,7 @@ class Mask extends AbstractMask {
 		var mask = getMaskTexture(t);
 		if( mask == null )
 			throw "Mask should be rendered before masked object";
-		var out = ctx.textures.allocTarget("maskTmp", ctx, t.width, t.height, false);
+		var out = ctx.textures.allocTarget("maskTmp", t.width, t.height, false);
 		ctx.engine.pushTarget(out);
 		pass.shader.texture = t.getTexture();
 		pass.shader.mask = getMaskTexture(t);

+ 1 - 1
h2d/filter/Shader.hx

@@ -24,7 +24,7 @@ class Shader< T:h3d.shader.ScreenShader > extends Filter {
 	function get_shader() return pass.shader;
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
-		var out = ctx.textures.allocTarget("shaderTmp", ctx, t.width, t.height, false);
+		var out = ctx.textures.allocTarget("shaderTmp", t.width, t.height, false);
 		ctx.engine.pushTarget(out);
 		Reflect.setField(shader, textureParam + "__", t.getTexture());
 		if( nearest ) t.getTexture().filter = Nearest;

+ 2 - 0
h3d/impl/RenderContext.hx

@@ -6,12 +6,14 @@ class RenderContext {
 	public var time : Float;
 	public var elapsedTime : Float;
 	public var frame : Int;
+	public var textures : h3d.impl.TextureCache;
 
 	function new() {
 		engine = h3d.Engine.getCurrent();
 		frame = 0;
 		time = 0.;
 		elapsedTime = 1. / hxd.System.getDefaultFrameRate();
+		textures = new h3d.impl.TextureCache(this);
 	}
 
 }

+ 10 - 14
h3d/impl/TextureCache.hx

@@ -4,11 +4,12 @@ class TextureCache {
 
 	var cache : Array<h3d.mat.Texture>;
 	var position : Int = 0;
-	var frame : Int;
 	var defaultDepthBuffer : h3d.mat.DepthBuffer;
+	var ctx : h3d.impl.RenderContext;
 	public var defaultFormat : hxd.PixelFormat;
 
-	public function new() {
+	public function new(ctx) {
+		this.ctx = ctx;
 		cache = [];
 		var engine = h3d.Engine.getCurrent();
 		defaultFormat = h3d.mat.Texture.nativeFormat;
@@ -23,20 +24,16 @@ class TextureCache {
 		cache[index] = t;
 	}
 
-	public function begin( ctx : h3d.impl.RenderContext ) {
-		if( frame != ctx.frame ) {
-			// dispose extra textures we didn't use
-			while( cache.length > position ) {
-				var t = cache.pop();
-				if( t != null ) t.dispose();
-			}
-			frame = ctx.frame;
-			position = 0;
+	public function begin() {
+		// dispose extra textures we didn't use in previous run
+		while( cache.length > position ) {
+			var t = cache.pop();
+			if( t != null ) t.dispose();
 		}
+		position = 0;
 	}
 
-	public function allocTarget( name : String, ctx : h3d.impl.RenderContext, width : Int, height : Int, defaultDepth=true, ?format:hxd.PixelFormat ) {
-		begin(ctx);
+	public function allocTarget( name : String, width : Int, height : Int, defaultDepth=true, ?format:hxd.PixelFormat ) {
 		var t = cache[position];
 		if( format == null ) format = defaultFormat;
 		if( t == null || t.isDisposed() || t.width != width || t.height != height || t.format != format ) {
@@ -55,7 +52,6 @@ class TextureCache {
 		for( t in cache )
 			t.dispose();
 		cache = [];
-		frame = -1;
 	}
 
 }

+ 0 - 8
h3d/pass/Base.hx

@@ -9,14 +9,6 @@ class Base {
 		this.name = name;
 	}
 
-	public function getTexture( index = 0 ) : h3d.mat.Texture {
-		return null;
-	}
-
-	public function setTexture( t : h3d.mat.Texture, index = 0 ) {
-		throw "Not implemented";
-	}
-
 	public function compileShader( p : h3d.mat.Pass ) : hxsl.RuntimeShader {
 		throw "Not implemented for this pass";
 		return null;

+ 0 - 15
h3d/pass/Default.hx

@@ -7,7 +7,6 @@ class Default extends Base {
 	var manager : ShaderManager;
 	var globals(get, never) : hxsl.Globals;
 	var cachedBuffer : h3d.shader.Buffers;
-	var tcache : h3d.impl.TextureCache;
 	var shaderCount : Int = 1;
 	var textureCount : Int = 1;
 	var shaderIdMap : Array<Int>;
@@ -33,7 +32,6 @@ class Default extends Base {
 	public function new(name) {
 		super(name);
 		manager = new ShaderManager(getOutputs());
-		tcache = new h3d.impl.TextureCache();
 		shaderIdMap = [];
 		textureIdMap = [];
 		initGlobals();
@@ -47,19 +45,6 @@ class Default extends Base {
 		#end
 	}
 
-	override function getTexture( index = 0 ) : h3d.mat.Texture {
-		return tcache.get(index);
-	}
-
-	override public function setTexture(t:h3d.mat.Texture, index = 0) {
-		tcache.set(t, index);
-	}
-
-	override function dispose() {
-		super.dispose();
-		tcache.dispose();
-	}
-
 	function getOutputs() : Array<hxsl.Output> {
 		return [Value("output.color")];
 	}

+ 13 - 5
h3d/pass/MRT.hx

@@ -18,8 +18,8 @@ class MRTSubPass extends Default {
 		return [mrt.fragmentOutputs[output]];
 	}
 
-	override function getTexture( index = 0 ) {
-		return index == 0 ? mrt.getTexture(output) : null;
+	public function getTexture() {
+		return mrt.textures[output];
 	}
 
 	override function draw( passes ) {
@@ -41,6 +41,7 @@ class MRT extends Default {
 
 	var fragmentOutputs : Array<hxsl.Output>;
 	var outputNames : Array<String>;
+	var textures : Array<h3d.mat.Texture>;
 	public var clearColors : Array<Null<Int>>;
 	public var clearSameColor : Null<Int>;
 	public var clearDepth : Null<Float>;
@@ -70,18 +71,25 @@ class MRT extends Default {
 		return fragmentOutputs;
 	}
 
+	public function getTexture( index : Int ) {
+		return textures[index];
+	}
+
 	override function draw(passes:Object) {
-		var tex = [for( i in 0...fragmentOutputs.length ) tcache.allocTarget(outputNames[i], ctx, ctx.engine.width, ctx.engine.height, true)];
+		if( textures == null )
+			textures = [];
+		for( i in 0...fragmentOutputs.length )
+			textures[i] = ctx.textures.allocTarget(outputNames[i], ctx.engine.width, ctx.engine.height, true);
 		if( clearColors != null )
 			for( i in 0...fragmentOutputs.length ) {
 				var color = clearColors[i];
 				if( color != null ) {
-					ctx.engine.pushTarget(tex[i]);
+					ctx.engine.pushTarget(textures[i]);
 					ctx.engine.clear(color);
 					ctx.engine.popTarget();
 				}
 			}
-		ctx.engine.pushTargets(tex);
+		ctx.engine.pushTargets(textures);
 		if( clearDepth != null || clearSameColor != null )
 			ctx.engine.clear(clearSameColor, clearDepth);
 		passes = super.draw(passes);

+ 2 - 2
h3d/pass/ShadowMap.hx

@@ -117,7 +117,7 @@ class ShadowMap extends Default {
 	}
 
 	override function draw( passes ) {
-		var texture = tcache.allocTarget("shadowMap", ctx, size, size, false);
+		var texture = ctx.textures.allocTarget("shadowMap", size, size, false);
 		if( customDepth && (depth == null || depth.width != size || depth.height != size || depth.isDisposed()) ) {
 			if( depth != null ) depth.dispose();
 			depth = new h3d.mat.DepthBuffer(size, size);
@@ -144,7 +144,7 @@ class ShadowMap extends Default {
 		ctx.engine.popTarget();
 
 		if( blur.quality > 0 && blur.passes > 0 )
-			blur.apply(texture, tcache.allocTarget("tmpBlur", ctx, size, size, false), true);
+			blur.apply(texture, ctx.textures.allocTarget("tmpBlur", size, size, false), true);
 
 		ctx.setGlobalID(shadowMapId, { texture : texture });
 		ctx.setGlobalID(shadowProjId, lightCamera.m);

+ 2 - 2
h3d/scene/DefaultRenderer.hx

@@ -18,7 +18,7 @@ class DepthPass extends h3d.pass.Default {
 	}
 
 	override function draw( passes ) {
-		var texture = tcache.allocTarget("depthMap", ctx, ctx.engine.width >> reduceSize, ctx.engine.height >> reduceSize, true);
+		var texture = ctx.textures.allocTarget("depthMap", ctx.engine.width >> reduceSize, ctx.engine.height >> reduceSize, true);
 		ctx.engine.pushTarget(texture);
 		ctx.engine.clear(enableSky ? 0 : 0xFF0000, 1);
 		passes = super.draw(passes);
@@ -43,7 +43,7 @@ class NormalPass extends h3d.pass.Default {
 	}
 
 	override function draw( passes ) {
-		var texture = tcache.allocTarget("normalMal", ctx, ctx.engine.width, ctx.engine.height);
+		var texture = ctx.textures.allocTarget("normalMal", ctx.engine.width, ctx.engine.height);
 		ctx.engine.pushTarget(texture);
 		ctx.engine.clear(0x808080, 1);
 		passes = super.draw(passes);

+ 2 - 4
h3d/scene/Renderer.hx

@@ -18,12 +18,10 @@ class Renderer {
 	var passObjects : SMap<PassObjects>;
 	var allPasses : Array<h3d.pass.Base>;
 	var ctx : RenderContext;
-	var tcache : h3d.impl.TextureCache;
 	var hasSetTarget = false;
 
 	public function new() {
 		allPasses = [];
-		tcache = new h3d.impl.TextureCache();
 		passObjects = new SMap();
 	}
 
@@ -31,7 +29,6 @@ class Renderer {
 		for( p in allPasses )
 			p.dispose();
 		passObjects = new SMap();
-		tcache.dispose();
 	}
 
 	public function getPass<T:h3d.pass.Base>( c : Class<T> ) : T {
@@ -93,7 +90,7 @@ class Renderer {
 
 	// for legacy purposes
 	inline function allocTarget( name : String, size = 0, depth = true ) {
-		return tcache.allocTarget(name, ctx, ctx.engine.width >> size, ctx.engine.height >> size, depth);
+		return ctx.textures.allocTarget(name, ctx.engine.width >> size, ctx.engine.height >> size, depth);
 	}
 
 	function copy( from, to, ?blend ) {
@@ -152,6 +149,7 @@ class Renderer {
 			p.setContext(ctx);
 		for( p in passes )
 			passObjects.set(p.name, p);
+		ctx.textures.begin();
 		render();
 		resetTarget();
 		for( p in passes )

+ 4 - 3
hxd/inspect/SceneProps.hx

@@ -106,9 +106,10 @@ class SceneProps {
 		case Textures:
 
 			var props = [];
+			/*
 			var tp = getTextures(@:privateAccess r.tcache);
 			if( tp.length > 0 )
-				props.push(PGroup("Textures",tp));
+				props.push(PGroup("Textures",tp));*/
 
 			for( p in @:privateAccess r.allPasses )
 				props.push(PGroup("Pass " + p.name, getPassProps(p)));
@@ -365,8 +366,8 @@ class SceneProps {
 
 		addDynamicProps(props, p);
 
-		for( t in getTextures(@:privateAccess def.tcache) )
-			props.push(t);
+		//for( t in getTextures(@:privateAccess def.tcache) )
+		//	props.push(t);
 
 		return props;
 	}