Kaynağa Gözat

entirely review clear() : no longer auto clear color/depth for target textures

Nicolas Cannasse 11 yıl önce
ebeveyn
işleme
6c28a5d630

+ 11 - 4
h3d/Engine.hx

@@ -16,7 +16,7 @@ class Engine {
 	public var drawCalls(default, null) : Int;
 	public var shaderSwitches(default, null) : Int;
 
-	public var backgroundColor : Int = 0xFF000000;
+	public var backgroundColor : Null<Int> = 0xFF000000;
 	public var autoResize : Bool;
 	public var fullScreen(default, set) : Bool;
 
@@ -26,6 +26,7 @@ class Engine {
 	var realFps : Float;
 	var lastTime : Float;
 	var antiAlias : Int;
+	var tmpVector = new h3d.Vector();
 
 	@:allow(h3d)
 	var curProjMatrix : h3d.Matrix;
@@ -249,7 +250,6 @@ class Engine {
 	public function begin() {
 		if( driver.isDisposed() )
 			return false;
-		driver.clear( ((backgroundColor>>16)&0xFF)/255 , ((backgroundColor>>8)&0xFF)/255, (backgroundColor&0xFF)/255, ((backgroundColor>>>24)&0xFF)/255);
 		// init
 		frameCount++;
 		drawTriangles = 0;
@@ -258,6 +258,7 @@ class Engine {
 		curProjMatrix = null;
 		currentTarget = null;
 		driver.begin(frameCount);
+		if( backgroundColor != null ) clear(backgroundColor, 1, 0);
 		return true;
 	}
 
@@ -285,9 +286,15 @@ class Engine {
 	 * Setus a render target to do off screen rendering, might be costly on low end devices
      * setTarget to null when you're finished rendering to it.
 	 */
-	public function setTarget( tex : h3d.mat.Texture,  clearColor = 0 ) {
+	public function setTarget( tex : h3d.mat.Texture ) {
 		currentTarget = tex;
-		driver.setRenderTarget(tex, clearColor);
+		driver.setRenderTarget(tex);
+	}
+
+	public function clear( ?color : Int, ?depth : Float, ?stencil : Int ) {
+		if( color != null )
+			tmpVector.setColor(color);
+		driver.clear(color == null ? null : tmpVector, depth, stencil);
 	}
 
 	/**

+ 2 - 2
h3d/impl/Driver.hx

@@ -56,7 +56,7 @@ class Driver {
 	function logImpl( str : String ) {
 	}
 
-	public function clear( r : Float, g : Float, b : Float, a : Float ) {
+	public function clear( ?color : h3d.Vector, ?depth : Float, ?stencil : Int ) {
 	}
 
 	public function setCapture( bmp : hxd.BitmapData, callb : Void -> Void ) {
@@ -101,7 +101,7 @@ class Driver {
 	public function setRenderZone( x : Int, y : Int, width : Int, height : Int ) {
 	}
 
-	public function setRenderTarget( tex : Null<h3d.mat.Texture>, clearColor : Int ) {
+	public function setRenderTarget( tex : Null<h3d.mat.Texture> ) {
 	}
 
 	public function present() {

+ 6 - 6
h3d/impl/LogDriver.hx

@@ -35,9 +35,9 @@ class LogDriver extends Driver {
 		d.begin(frame);
 	}
 
-	override function clear( r : Float, g : Float, b : Float, a : Float ) {
-		log('Clear [$r $g $b $a]');
-		d.clear(r, g, b, a);
+	override function clear( ?color : h3d.Vector, ?depth : Float, ?stencil : Int ) {
+		log('Clear color=$color depth=$depth stencil=$stencil');
+		d.clear(color, depth, stencil);
 	}
 
 	override function setCapture( bmp : hxd.BitmapData, callb : Void -> Void ) {
@@ -225,9 +225,9 @@ class LogDriver extends Driver {
 		d.setRenderZone(x, y, width, height);
 	}
 
-	override function setRenderTarget( tex : Null<h3d.mat.Texture>, clearColor : Int ) {
-		log('SetRenderTarget $tex ${StringTools.hex(clearColor,8)}');
-		d.setRenderTarget(tex, clearColor);
+	override function setRenderTarget( tex : Null<h3d.mat.Texture> ) {
+		log('SetRenderTarget $tex');
+		d.setRenderTarget(tex);
 	}
 
 	override function present() {

+ 12 - 4
h3d/impl/Stage3dDriver.hx

@@ -149,8 +149,12 @@ class Stage3dDriver extends Driver {
 		this.height = height;
 	}
 
-	override function clear(r, g, b, a) {
-		ctx.clear(r, g, b, a);
+	override function clear( ?color : h3d.Vector, ?depth : Float, ?stencil : Int ) {
+		var mask = 0;
+		if( color != null ) mask |= flash.display3D.Context3DClearMask.COLOR;
+		if( depth != null ) mask |= flash.display3D.Context3DClearMask.DEPTH;
+		if( stencil != null ) mask |= flash.display3D.Context3DClearMask.STENCIL;
+		ctx.clear( color == null ? 0 : color.r, color == null ? 0 : color.g, color == null ? 0 : color.b, color == null ? 1 : color.a, depth == null ? 1 : depth, stencil == null ? 0 : stencil, mask);
 	}
 
 	override function setCapture( bmp : hxd.BitmapData, onCapture : Void -> Void ) {
@@ -212,6 +216,7 @@ class Stage3dDriver extends Driver {
 	override function allocTexture( t : h3d.mat.Texture ) : Texture {
 		var fmt = flash.display3D.Context3DTextureFormat.BGRA;
 		t.lastFrame = frame;
+		t.flags.unset(WasCleared);
 		if( t.flags.has(TargetDepth) )
 			throw "Unsupported texture flag";
 		try {
@@ -624,7 +629,7 @@ class Stage3dDriver extends Driver {
 		}
 	}
 
-	override function setRenderTarget( t : Null<h3d.mat.Texture>, clearColor : Int ) {
+	override function setRenderTarget( t : Null<h3d.mat.Texture>) {
 		if( t == null ) {
 			ctx.setRenderToBackBuffer();
 			inTarget = null;
@@ -636,8 +641,11 @@ class Stage3dDriver extends Driver {
 			ctx.setRenderToTexture(t.t, t.flags.has(TargetUseDefaultDepth));
 			inTarget = t;
 			t.lastFrame = frame;
+			if( !t.flags.has(WasCleared) ) {
+				t.flags.set(WasCleared);
+				ctx.clear(0, 0, 0, 1, 1, 0, flash.display3D.Context3DClearMask.COLOR);
+			}
 			reset();
-			ctx.clear( ((clearColor>>16)&0xFF)/255 , ((clearColor>>8)&0xFF)/255, (clearColor&0xFF)/255, ((clearColor>>>24)&0xFF)/255);
 		}
 	}
 

+ 4 - 0
h3d/mat/Data.hx

@@ -117,6 +117,10 @@ enum TextureFlags {
 		Assumes that the color value of the texture is premultiplied by the alpha component.
 	**/
 	AlphaPremultiplied;
+	/**
+		Tells if the target texture has been cleared (reserved for internal engine usage).
+	**/
+	WasCleared;
 }
 
 

+ 1 - 1
h3d/pass/Blur.hx

@@ -67,7 +67,7 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 		shader.isDepth = isDepth;
 		shader.pixel.set(1 / src.width, 0);
 
-		engine.setTarget(tmp, 0xFFFF0000);
+		engine.setTarget(tmp);
 		render();
 		engine.setTarget(null);
 

+ 0 - 20
h3d/pass/Clear.hx

@@ -1,20 +0,0 @@
-package h3d.pass;
-
-class Clear extends ScreenFx<h3d.shader.Clear> {
-
-	public function new() {
-		super( new h3d.shader.Clear() );
-	}
-
-	public function apply( ?color : h3d.Vector, ?depth : Float ) {
-		pass.depth(depth != null, Always);
-		shader.depth = depth != null ? depth : 0;
-		if( color != null ) {
-			shader.color.load(color);
-			pass.colorMask = 15;
-		} else
-			pass.colorMask = 0;
-		render();
-	}
-
-}

+ 1 - 4
h3d/pass/Default.hx

@@ -8,7 +8,6 @@ class Default extends Base {
 	var globals(get, never) : hxsl.Globals;
 	var cachedBuffer : h3d.shader.Buffers;
 
-	var hasTargetDepth : Bool;
 	var textureCache : Array<h3d.mat.Texture>;
 	var textureCachePosition : Int = 0;
 	var textureCacheFrame : Int;
@@ -36,8 +35,6 @@ class Default extends Base {
 		manager = new h3d.shader.Manager(getOutputs());
 		initGlobals();
 		lightSystem = new LightSystem(globals);
-
-		hasTargetDepth = h3d.Engine.getCurrent().driver.hasFeature(TargetDepthBuffer);
 		textureCache = [];
 	}
 
@@ -69,7 +66,7 @@ class Default extends Base {
 		if( t == null || t.isDisposed() || t.width != width || t.height != height ) {
 			if( t != null ) t.dispose();
 			var flags : Array<h3d.mat.Data.TextureFlags> = [Target, TargetNoFlipY];
-			if( hasDepth ) flags.push(hasTargetDepth ? TargetDepth : TargetUseDefaultDepth);
+			if( hasDepth ) flags.push(TargetUseDefaultDepth);
 			t = new h3d.mat.Texture(width, height, flags);
 			textureCache[textureCachePosition] = t;
 		}

+ 2 - 7
h3d/pass/Distance.hx

@@ -2,7 +2,6 @@ package h3d.pass;
 
 class Distance extends Default {
 
-	var clear : Clear;
 	var distanceMapId : Int;
 	public var enableSky : Bool = false;
 
@@ -11,8 +10,6 @@ class Distance extends Default {
 		priority = 10;
 		lightSystem = null;
 		distanceMapId = hxsl.Globals.allocID("distanceMap");
-		if( !hasTargetDepth )
-			clear = new Clear();
 	}
 
 	override function getOutputs() {
@@ -21,13 +18,11 @@ class Distance extends Default {
 
 	override function draw( name : String, passes ) {
 		var texture = getTargetTexture("distanceMap", ctx.engine.width, ctx.engine.height);
-		ctx.engine.setTarget(texture, enableSky ? 0 : 0xFFFF0000);
+		ctx.engine.setTarget(texture);
+		ctx.engine.clear(enableSky ? 0 : 0xFFFF0000, 1);
 		passes = super.draw(name, passes);
 		ctx.engine.setTarget(null);
 		ctx.sharedGlobals.set(distanceMapId, texture);
-		if( !hasTargetDepth )
-			clear.apply(1);
-
 		return passes;
 	}
 

+ 2 - 7
h3d/pass/ShadowMap.hx

@@ -8,7 +8,6 @@ class ShadowMap extends Default {
 	var shadowColorId : Int;
 	var shadowPowerId : Int;
 	var shadowBiasId : Int;
-	var clear : Clear;
 	public var size : Int;
 	public var lightDirection : h3d.Vector;
 	public var color : h3d.Vector;
@@ -29,8 +28,6 @@ class ShadowMap extends Default {
 		shadowColorId = hxsl.Globals.allocID("shadow.color");
 		shadowPowerId = hxsl.Globals.allocID("shadow.power");
 		shadowBiasId = hxsl.Globals.allocID("shadow.bias");
-		if( !hasTargetDepth )
-			clear = new Clear();
 		color = new h3d.Vector();
 		blur = new Blur(2, 3);
 	}
@@ -61,16 +58,14 @@ class ShadowMap extends Default {
 		var ct = ctx.camera.target;
 		lightCamera.target.set(ct.x, ct.y, ct.z);
 		lightCamera.pos.set(ct.x - lightDirection.x, ct.y - lightDirection.y, ct.z - lightDirection.z);
-		ctx.engine.setTarget(texture, 0xFFFFFFFF);
+		ctx.engine.setTarget(texture);
+		ctx.engine.clear(0xFFFFFFFF, 1);
 		passes = super.draw(name, passes);
 		ctx.engine.setTarget(null);
 
 		if( blur.quality > 0 )
 			blur.apply(texture, getTargetTexture("tmpBlur", size, size, false), true);
 
-		if( !hasTargetDepth )
-			clear.apply(1);
-
 		ctx.sharedGlobals.set(shadowMapId, texture);
 		ctx.sharedGlobals.set(shadowProjId, lightCamera.m);
 		ctx.sharedGlobals.set(shadowColorId, color);

+ 0 - 25
h3d/shader/Clear.hx

@@ -1,25 +0,0 @@
-package h3d.shader;
-
-class Clear extends hxsl.Shader {
-
-	static var SRC = {
-		@input var input : {
-			position : Vec2,
-		};
-		var output : {
-			position : Vec4,
-			color : Vec4,
-		};
-
-		@param var depth : Float;
-		@param var color : Vec4;
-
-		function vertex() {
-			output.position = vec4(input.position, depth, 1);
-		}
-		function fragment() {
-			output.color = color;
-		}
-	}
-
-}