Explorar el Código

directx fixes, mrt working

ncannasse hace 8 años
padre
commit
1ba234f652
Se han modificado 4 ficheros con 42 adiciones y 10 borrados
  1. 38 6
      h3d/impl/DirectXDriver.hx
  2. 1 1
      h3d/pass/Depth.hx
  3. 1 1
      h3d/pass/ShadowMap.hx
  4. 2 2
      hxsl/HlslOut.hx

+ 38 - 6
h3d/impl/DirectXDriver.hx

@@ -58,6 +58,7 @@ class DirectXDriver extends h3d.impl.Driver {
 	var defaultTarget : RenderTargetView;
 	var defaultDepth : DepthBuffer;
 	var defaultDepthInst : h3d.mat.DepthBuffer;
+	var extraDepthInst : h3d.mat.DepthBuffer;
 
 	var viewport : hl.BytesAccess<hl.F32> = new hl.Bytes(4 * VIEWPORTS_ELTS);
 	var rects : hl.BytesAccess<Int> = new hl.Bytes(4 * RECTS_ELTS);
@@ -134,6 +135,11 @@ class DirectXDriver extends h3d.impl.Driver {
 			defaultDepthInst.width = width;
 			defaultDepthInst.height = height;
 		}
+		if( extraDepthInst != null ) @:privateAccess {
+			extraDepthInst.width = width;
+			extraDepthInst.height = height;
+			extraDepthInst.dispose();
+		}
 
 		var buf = Driver.getBackBuffer();
 		defaultTarget = Driver.createRenderTargetView(buf);
@@ -175,7 +181,11 @@ class DirectXDriver extends h3d.impl.Driver {
 	}
 
 	override function getDefaultDepthBuffer():h3d.mat.DepthBuffer {
-		return defaultDepthInst;
+		if( extraDepthInst == null )
+			extraDepthInst = new h3d.mat.DepthBuffer(outputWidth, outputHeight);
+		if( extraDepthInst.isDisposed() )
+			allocDepthBuffer(extraDepthInst);
+		return extraDepthInst;
 	}
 
 	override function allocVertexes(m:ManagedBuffer):VertexBuffer {
@@ -438,16 +448,17 @@ class DirectXDriver extends h3d.impl.Driver {
 			throw "Can't render to texture which is not allocated with Target flag";
 		if( tex.depthBuffer != null && (tex.depthBuffer.width != tex.width || tex.depthBuffer.height != tex.height) )
 			throw "Invalid depth buffer size : does not match render target size";
-		currentDepth = @:privateAccess (tex.depthBuffer == null ? null : tex.depthBuffer.b);
+		currentDepth = @:privateAccess (tex.depthBuffer == null ? defaultDepth : tex.depthBuffer.b);
 		currentTargets[0] = tex.t.rt;
-		Driver.omSetRenderTargets(1, currentTargets, currentDepth == null ? null : currentDepth.view);
+		Driver.omSetRenderTargets(1, currentTargets, currentDepth.view);
 		viewport[2] = tex.width;
 		viewport[3] = tex.height;
 		viewport[5] = 1.;
 		Driver.rsSetViewports(1, viewport);
+		unbind(tex.t.view);
+	}
 
-		// unbind from resources !
-		var res = tex.t.view;
+	function unbind( res ) {
 		for( i in 0...64 ) {
 			if( vertexShader.resources[i] == res )
 				vertexShader.resources[i] = null;
@@ -457,7 +468,28 @@ class DirectXDriver extends h3d.impl.Driver {
 	}
 
 	override function setRenderTargets(textures:Array<h3d.mat.Texture>) {
-		throw "TODO";
+		if( textures.length == 0 ) {
+			setRenderTarget(null);
+			return;
+		}
+		var tex = textures[0];
+		curTexture = textures[0];
+		if( tex.depthBuffer != null && (tex.depthBuffer.width != tex.width || tex.depthBuffer.height != tex.height) )
+			throw "Invalid depth buffer size : does not match render target size";
+		currentDepth = @:privateAccess (tex.depthBuffer == null ? defaultDepth : tex.depthBuffer.b);
+		for( i in 0...textures.length ) {
+			var tex = textures[i];
+			if( tex.t == null )
+				tex.alloc();
+			currentTargets[i] = tex.t.rt;
+			unbind(tex.t.view);
+		}
+		Driver.omSetRenderTargets(textures.length, currentTargets, currentDepth.view);
+
+		viewport[2] = tex.width;
+		viewport[3] = tex.height;
+		viewport[5] = 1.;
+		Driver.rsSetViewports(1, viewport);
 	}
 
 	override function setRenderZone(x:Int, y:Int, width:Int, height:Int) {

+ 1 - 1
h3d/pass/Depth.hx

@@ -17,7 +17,7 @@ class Depth extends Default {
 	}
 
 	override function draw( passes ) {
-		var texture = tcache.allocTarget("depthMap", ctx, ctx.engine.width >> reduceSize, ctx.engine.height >> reduceSize, true #if hldx , ALPHA32F #end);
+		var texture = tcache.allocTarget("depthMap", ctx, ctx.engine.width >> reduceSize, ctx.engine.height >> reduceSize, true);
 		ctx.engine.pushTarget(texture);
 		ctx.engine.clear(enableSky ? 0 : 0xFF0000, 1);
 		passes = super.draw(passes);

+ 1 - 1
h3d/pass/ShadowMap.hx

@@ -118,7 +118,7 @@ class ShadowMap extends Default {
 	}
 
 	override function draw( passes ) {
-		var texture = tcache.allocTarget("shadowMap", ctx, size, size, false #if hldx ,ALPHA32F #end);
+		var texture = tcache.allocTarget("shadowMap", ctx, 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);

+ 2 - 2
hxsl/HlslOut.hx

@@ -237,9 +237,9 @@ class HlslOut {
 				// negative power might not work
 				decl("#pragma warning(disable:3571)");
 			case Pack:
-				decl("float4 pack( float x ) { return float4(x,x,x,x); }");
+				decl("float4 pack( float v ) { float4 color = frac(v * float4(1, 255, 255.*255., 255.*255.*255.)); return color - color.yzww * float4(1. / 255., 1. / 255., 1. / 255., 0.); }");
 			case Unpack:
-				decl("float unpack( float4 v ) { return v.x; }");
+				decl("float unpack( float4 color ) { return dot(color,float4(1., 1. / 255., 1. / (255. * 255.), 1. / (255. * 255. * 255.))); }");
 			case PackNormal:
 				decl("float4 packNormal( float3 n ) { return float4((n + 1.) * 0.5,1.); }");
 			case UnpackNormal: