Explorar o código

remove TargetFlipY, instead use pixelStorei + convert S/T to U/V in shader

Nicolas Cannasse %!s(int64=10) %!d(string=hai) anos
pai
achega
47be7b072f
Modificáronse 7 ficheiros con 29 adicións e 33 borrados
  1. 11 16
      h3d/impl/GlDriver.hx
  2. 1 1
      h3d/impl/TextureCache.hx
  3. 0 4
      h3d/mat/Data.hx
  4. 1 1
      h3d/pass/Blur.hx
  5. 0 4
      h3d/pass/Default.hx
  6. 0 5
      h3d/shader/BaseMesh.hx
  7. 16 2
      hxsl/GlslOut.hx

+ 11 - 16
h3d/impl/GlDriver.hx

@@ -63,7 +63,6 @@ class GlDriver extends Driver {
 	var curBuffer : h3d.Buffer;
 	var curMatBits : Int;
 	var programs : Map<Int, CompiledProgram>;
-	var hasTargetFlip : Bool;
 	var frame : Int;
 
 	var bufferWidth : Int;
@@ -86,6 +85,7 @@ class GlDriver extends Driver {
 		programs = new Map();
 		curAttribs = 0;
 		curMatBits = -1;
+		gl.pixelStorei(GL.UNPACK_FLIP_Y_WEBGL, 1);
 	}
 
 	override function logImpl( str : String ) {
@@ -109,7 +109,6 @@ class GlDriver extends Driver {
 		gl.useProgram(null);
 		curShader = null;
 		curBuffer = null;
-		hasTargetFlip = false;
 	}
 
 	override function getShaderInputNames() {
@@ -247,12 +246,6 @@ class GlDriver extends Driver {
 	}
 
 	function selectMaterialBits( bits : Int ) {
-		if( hasTargetFlip ) {
-			// switch culling font/back
-			var c = Pass.getCulling(bits);
-			if( c == 1 ) c = 2 else if( c == 2 ) c = 1;
-			bits = (bits & ~Pass.culling_mask) | (c << Pass.culling_offset);
-		}
 		var diff = bits ^ curMatBits;
 		if( curMatBits < 0 ) diff = -1;
 		if( diff == 0 )
@@ -555,17 +548,24 @@ class GlDriver extends Driver {
 		#end
 	}
 
+	override function setRenderZone( x : Int, y : Int, width : Int, height : Int ) {
+		if( x == 0 && y == 0 && width < 0 && height < 0 )
+			gl.disable(GL.SCISSOR_TEST);
+		else {
+			gl.enable(GL.SCISSOR_TEST);
+			gl.scissor(x, bufferHeight - (y + height), width, height);
+		}
+	}
+
 	override function setRenderTarget( tex : h3d.mat.Texture ) {
 		if( tex == null ) {
 			gl.bindFramebuffer(GL.FRAMEBUFFER, null);
 			gl.viewport(0, 0, bufferWidth, bufferHeight);
-			hasTargetFlip = false;
 			return;
 		}
 		if( tex.t == null )
 			tex.alloc();
 		tex.lastFrame = frame;
-		hasTargetFlip = !tex.flags.has(TargetNoFlipY);
 		gl.bindFramebuffer(GL.FRAMEBUFFER, tex.t.fb);
 		gl.viewport(0, 0, tex.width, tex.height);
 	}
@@ -615,13 +615,8 @@ class GlDriver extends Driver {
 
 	static var FACES = [
 		0,
-		#if js
-		GL.FRONT, // front/back reversed in WebGL
-		GL.BACK,
-		#else
-		GL.BACK,
 		GL.FRONT,
-		#end
+		GL.BACK,
 		GL.FRONT_AND_BACK,
 	];
 

+ 1 - 1
h3d/impl/TextureCache.hx

@@ -36,7 +36,7 @@ class TextureCache {
 		var t = cache[position];
 		if( t == null || t.isDisposed() || t.width != width || t.height != height || t.flags.has(hasDefaultDepth ? TargetUseDefaultDepth : TargetDepth) != hasDepth ) {
 			if( t != null ) t.dispose();
-			var flags : Array<h3d.mat.Data.TextureFlags> = [Target, TargetNoFlipY];
+			var flags : Array<h3d.mat.Data.TextureFlags> = [Target];
 			if( hasDepth ) flags.push(hasDefaultDepth ? TargetUseDefaultDepth : TargetDepth);
 			t = new h3d.mat.Texture(width, height, flags);
 			cache[position] = t;

+ 0 - 4
h3d/mat/Data.hx

@@ -77,10 +77,6 @@ enum TextureFlags {
 		Allocate a cubic texture. Might be restricted to power of two textures only.
 	**/
 	Cubic;
-	/**
-		Used to prevent culling inversion on target textures in GL.
-	**/
-	TargetNoFlipY;
 	/**
 		Activates Mip Mapping for this texture. Might not be available for target textures.
 	**/

+ 1 - 1
h3d/pass/Blur.hx

@@ -64,7 +64,7 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 
 		var alloc = tmp == null;
 		if( alloc )
-			tmp = new h3d.mat.Texture(src.width, src.height, [Target, TargetNoFlipY]);
+			tmp = new h3d.mat.Texture(src.width, src.height, [Target]);
 
 		if( values == null ) calcValues();
 

+ 0 - 4
h3d/pass/Default.hx

@@ -21,10 +21,6 @@ class Default extends Base {
 	@global("global.pixelSize") var pixelSize : h3d.Vector = new h3d.Vector(2 / ctx.engine.width, 2 / ctx.engine.height);
 	@global("global.modelView") var globalModelView : h3d.Matrix;
 	@global("global.modelViewInverse") var globalModelViewInverse : h3d.Matrix;
-	@global("global.flipY") var globalFlipY : Float = {
-		var t = ctx.engine.getTarget();
-		t != null && !t.flags.has(TargetNoFlipY) ? -1 : 1;
-	}
 
 	public function new() {
 		super();

+ 0 - 5
h3d/shader/BaseMesh.hx

@@ -16,7 +16,6 @@ class BaseMesh extends hxsl.Shader {
 
 		@global var global : {
 			var time : Float;
-			var flipY : Float;
 			var pixelSize : Vec2;
 			@perObject var modelView : Mat4;
 			@perObject var modelViewInverse : Mat4;
@@ -60,10 +59,6 @@ class BaseMesh extends hxsl.Shader {
 
 		function vertex() {
 			output.position = projectedPosition;
-			#if !flash
-			// this is done to flip the rendering on render targets in openGL
-			output.position.y *= global.flipY;
-			#end
 		}
 
 		function fragment() {

+ 16 - 2
hxsl/GlslOut.hx

@@ -18,6 +18,7 @@ class GlslOut {
 		m.set(ToInt, "int");
 		m.set(ToFloat, "float");
 		m.set(ToBool, "bool");
+		m.set(Texture2D, "_texture2D");
 		for( g in m )
 			KWDS.set(g, true);
 		m;
@@ -185,6 +186,9 @@ class GlslOut {
 				decl("vec4 packNormal( vec3 v ) { return vec4((v + vec3(1.)) * vec3(0.5),1.); }");
 			case UnpackNormal:
 				decl("vec3 unpackNormal( vec4 v ) { return normalize((v.xyz - vec3(0.5)) * vec3(2.)); }");
+			case Texture2D:
+				// convert S/T (bottom left) to U/V (top left)
+				decl("vec4 _texture2D( sampler2D t, vec2 v ) { return texture2D(t,vec2(v.x,1.-v.y)); }");
 			default:
 			}
 			add(GLOBALS.get(g));
@@ -392,8 +396,18 @@ class GlslOut {
 
 		var tmp = buf;
 		buf = new StringBuf();
-		add("void main(void) ");
-		addExpr(f.expr, "");
+		add("void main(void) {\n");
+		switch( f.expr.e ) {
+		case TBlock(el):
+			for( e in el ) {
+				add("\t");
+				addExpr(e, "\t");
+				add(";\n");
+			}
+		default:
+			addExpr(f.expr, "");
+		}
+		add("}");
 		exprValues.push(buf.toString());
 		buf = tmp;