Browse Source

gl : clear texture when setRenderTarget (same as DX)

Nicolas Cannasse 5 years ago
parent
commit
6b18fafe63
2 changed files with 13 additions and 4 deletions
  1. 0 2
      h2d/Scene.hx
  2. 13 2
      h3d/impl/GlDriver.hx

+ 0 - 2
h2d/Scene.hx

@@ -606,8 +606,6 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
 		ctx.begin();
 		ctx.pushTargets(texs);
 		if( outputs != null ) @:privateAccess ctx.manager.setOutput(outputs);
-		if( texs.length == 1 && !texs[0].flags.has(WasCleared) )
-			ctx.engine.clear(0);
 		s.drawRec(ctx);
 		if( outputs != null ) @:privateAccess ctx.manager.setOutput();
 		ctx.popTarget();

+ 13 - 2
h3d/impl/GlDriver.hx

@@ -170,6 +170,8 @@ class GlDriver extends Driver {
 
 	var drawMode : Int;
 
+	static var BLACK = new h3d.Vector();
+
 	/**
 		Perform OUT_OF_MEMORY checks when allocating textures/buffers.
 		Default true, except in WebGL (false)
@@ -1464,7 +1466,6 @@ class GlDriver extends Driver {
 			restoreBind();
 		}
 
-		tex.flags.set(WasCleared); // once we draw to, do not clear again
 		tex.lastFrame = frame;
 		curTargetLayer = layer;
 		curTargetMip = mipLevel;
@@ -1489,6 +1490,11 @@ class GlDriver extends Driver {
 		for( i in 0...boundTextures.length )
 			boundTextures[i] = null;
 
+		if( !tex.flags.has(WasCleared) ) {
+			tex.flags.set(WasCleared); // once we draw to, do not clear again
+			clear(BLACK);
+		}
+
 		#if js
 		if( glDebug ) {
 			var code = gl.checkFramebufferStatus(GL.FRAMEBUFFER);
@@ -1504,6 +1510,7 @@ class GlDriver extends Driver {
 		if( textures.length < 2 )
 			return;
 		numTargets = textures.length;
+		var needClear = false;
 		for( i in 1...textures.length ) {
 			var tex = textures[i];
 			if( tex.t == null )
@@ -1515,9 +1522,13 @@ class GlDriver extends Driver {
 			gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0 + i, GL.TEXTURE_2D, tex.t.t, 0);
 			curTargets[i] = tex;
 			tex.lastFrame = frame;
-			tex.flags.set(WasCleared); // once we draw to, do not clear again
+			if( !tex.flags.has(WasCleared) ) {
+				tex.flags.set(WasCleared); // once we draw to, do not clear again
+				needClear = true;
+			}
 		}
 		setDrawBuffers(textures.length);
+		if( needClear ) clear(BLACK);
 	}
 
 	override function init( onCreate : Bool -> Void, forceSoftware = false ) {