Explorar el Código

fixes in gl output (no warnings in modern glsl)

ncannasse hace 8 años
padre
commit
0ad66ecf08
Se han modificado 2 ficheros con 71 adiciones y 33 borrados
  1. 6 2
      h3d/impl/GlDriver.hx
  2. 65 31
      hxsl/GlslOut.hx

+ 6 - 2
h3d/impl/GlDriver.hx

@@ -140,8 +140,7 @@ class GlDriver extends Driver {
 	}
 	}
 
 
 	override function getNativeShaderCode( shader : hxsl.RuntimeShader ) {
 	override function getNativeShaderCode( shader : hxsl.RuntimeShader ) {
-		var glout = new hxsl.GlslOut();
-		return "// vertex:\n" + glout.run(shader.vertex.data) + "// fragment:\n" + glout.run(shader.fragment.data);
+		return "// vertex:\n" + hxsl.GlslOut.toGlsl(shader.vertex.data) + "// fragment:\n" + hxsl.GlslOut.toGlsl(shader.fragment.data);
 	}
 	}
 
 
 	function compileShader( glout : hxsl.GlslOut, shader : hxsl.RuntimeShader.RuntimeShaderData ) {
 	function compileShader( glout : hxsl.GlslOut, shader : hxsl.RuntimeShader.RuntimeShaderData ) {
@@ -150,6 +149,8 @@ class GlDriver extends Driver {
 		var code = glout.run(shader.data);
 		var code = glout.run(shader.data);
 		gl.shaderSource(s, code);
 		gl.shaderSource(s, code);
 		gl.compileShader(s);
 		gl.compileShader(s);
+		var log = gl.getShaderInfoLog(s);
+		if( log != "" ) trace(log);
 		if ( gl.getShaderParameter(s, GL.COMPILE_STATUS) != cast 1 ) {
 		if ( gl.getShaderParameter(s, GL.COMPILE_STATUS) != cast 1 ) {
 			var log = gl.getShaderInfoLog(s);
 			var log = gl.getShaderInfoLog(s);
 			var lid = Std.parseInt(log.substr(9));
 			var lid = Std.parseInt(log.substr(9));
@@ -176,6 +177,9 @@ class GlDriver extends Driver {
 		if( p == null ) {
 		if( p == null ) {
 			p = new CompiledProgram();
 			p = new CompiledProgram();
 			var glout = new hxsl.GlslOut();
 			var glout = new hxsl.GlslOut();
+			#if js
+			glout.glES = true;
+			#end
 			p.vertex = compileShader(glout,shader.vertex);
 			p.vertex = compileShader(glout,shader.vertex);
 			p.fragment = compileShader(glout,shader.fragment);
 			p.fragment = compileShader(glout,shader.fragment);
 			p.p = gl.createProgram();
 			p.p = gl.createProgram();

+ 65 - 31
hxsl/GlslOut.hx

@@ -37,6 +37,8 @@ class GlslOut {
 	var outIndexes : Map<Int, Int>;
 	var outIndexes : Map<Int, Int>;
 	public var varNames : Map<Int,String>;
 	public var varNames : Map<Int,String>;
 	public var flipY : Bool;
 	public var flipY : Bool;
+	public var glES : Bool;
+	public var version : Null<Int>;
 
 
 	public function new() {
 	public function new() {
 		varNames = new Map();
 		varNames = new Map();
@@ -160,11 +162,24 @@ class GlslOut {
 			buf = tmp;
 			buf = tmp;
 			add(name);
 			add(name);
 			add("()");
 			add("()");
+		case TIf(econd, eif, eelse):
+			add("( ");
+			addValue(econd, tabs);
+			add(" ) ? ");
+			addValue(eif, tabs);
+			add(" : ");
+			addValue(eelse, tabs);
+		case TMeta(_, _, e):
+			addValue(e, tabs);
 		default:
 		default:
 			addExpr(e, tabs);
 			addExpr(e, tabs);
 		}
 		}
 	}
 	}
 
 
+	function addBlock( e : TExpr, tabs : String ) {
+		addExpr(e, tabs);
+	}
+
 	function addExpr( e : TExpr, tabs : String ) {
 	function addExpr( e : TExpr, tabs : String ) {
 		switch( e.e ) {
 		switch( e.e ) {
 		case TConst(c):
 		case TConst(c):
@@ -198,7 +213,12 @@ class GlslOut {
 			case Texture2D:
 			case Texture2D:
 				// convert S/T (bottom left) to U/V (top left)
 				// convert S/T (bottom left) to U/V (top left)
 				// we don't use 1. because of pixel rounding (fixes artifacts in blur)
 				// we don't use 1. because of pixel rounding (fixes artifacts in blur)
-				decl("vec4 _texture2D( sampler2D t, vec2 v ) { return texture2D(t,vec2(v.x,"+(flipY?"0.999999-v.y":"v.y")+")); }");
+				decl('vec4 _texture2D( sampler2D t, vec2 v ) { return ${glES?"texture2D":"texture"}(t,vec2(v.x,${flipY?"0.999999-v.y":"v.y"})); }');
+			case TextureCube:
+				if( !glES ) {
+					add("texture");
+					return;
+				}
 			default:
 			default:
 			}
 			}
 			add(GLOBALS.get(g));
 			add(GLOBALS.get(g));
@@ -212,7 +232,7 @@ class GlslOut {
 			for( e in el ) {
 			for( e in el ) {
 				add(t2);
 				add(t2);
 				addExpr(e, t2);
 				addExpr(e, t2);
-				add(";\n");
+				newLine(e);
 			}
 			}
 			add(tabs);
 			add(tabs);
 			add("}");
 			add("}");
@@ -348,7 +368,7 @@ class GlslOut {
 			add(") ");
 			add(") ");
 			addExpr(eif, tabs);
 			addExpr(eif, tabs);
 			if( eelse != null ) {
 			if( eelse != null ) {
-				if( !eif.e.match(TBlock(_)) ) add(";");
+				if( !isBlock(eif) ) add(";");
 				add(" else ");
 				add(" else ");
 				addExpr(eelse, tabs);
 				addExpr(eelse, tabs);
 			}
 			}
@@ -370,33 +390,24 @@ class GlslOut {
 				addValue(e1,tabs);
 				addValue(e1,tabs);
 				add(";"+v.name+"<");
 				add(";"+v.name+"<");
 				addValue(e2,tabs);
 				addValue(e2,tabs);
-				add(";" + v.name+"++) {");
-				tabs += "\t";
-				add("\n" + tabs);
-				addExpr(loop, tabs);
-				tabs = tabs.substr(1);
-				add("\n" + tabs+"}");
+				add(";" + v.name+"++) ");
+				addBlock(loop, tabs);
 			default:
 			default:
 				throw "assert";
 				throw "assert";
 			}
 			}
 		case TWhile(e, loop, false):
 		case TWhile(e, loop, false):
 			var old = tabs;
 			var old = tabs;
 			tabs += "\t";
 			tabs += "\t";
-			add("do {\n" + tabs);
-			addExpr(loop,tabs);
-			tabs = old;
-			add("\n" + tabs + "} while( ");
+			add("do ");
+			addBlock(loop,tabs);
+			add(" while( ");
 			addValue(e,tabs);
 			addValue(e,tabs);
 			add(" )");
 			add(" )");
 		case TWhile(e, loop, _):
 		case TWhile(e, loop, _):
 			add("while( ");
 			add("while( ");
 			addValue(e, tabs);
 			addValue(e, tabs);
-			var old = tabs;
-			tabs += "\t";
-			add(" ) {\n" + tabs);
-			addExpr(loop,tabs);
-			tabs = old;
-			add("\n" + tabs + "}");
+			add(" ) ");
+			addBlock(loop,tabs);
 		case TSwitch(_):
 		case TSwitch(_):
 			add("switch(...)");
 			add("switch(...)");
 		case TContinue:
 		case TContinue:
@@ -416,6 +427,8 @@ class GlslOut {
 				addValue(e,tabs);
 				addValue(e,tabs);
 			}
 			}
 			add("]");
 			add("]");
+		case TMeta(_, _, e):
+			addExpr(e, tabs);
 		}
 		}
 	}
 	}
 
 
@@ -445,6 +458,24 @@ class GlslOut {
 		return n;
 		return n;
 	}
 	}
 
 
+	function newLine( e : TExpr ) {
+		if( isBlock(e) )
+			add("\n");
+		else
+			add(";\n");
+	}
+
+	function isBlock( e : TExpr ) {
+		switch( e.e ) {
+		case TFor(_, _, loop), TWhile(_,loop,true):
+			return isBlock(loop);
+		case TBlock(_):
+			return true;
+		default:
+			return false;
+		}
+	}
+
 	public function run( s : ShaderData ) {
 	public function run( s : ShaderData ) {
 		locals = new Map();
 		locals = new Map();
 		decls = [];
 		decls = [];
@@ -464,9 +495,9 @@ class GlslOut {
 			case Param, Global:
 			case Param, Global:
 				add("uniform ");
 				add("uniform ");
 			case Input:
 			case Input:
-				add("attribute ");
+				add( glES ? "attribute " : "in ");
 			case Var:
 			case Var:
-				add("varying ");
+				add( glES ? "varying " : (isVertex ? "out " : "in "));
 			case Output:
 			case Output:
 				outIndexes.set(v.id, outIndex++);
 				outIndexes.set(v.id, outIndex++);
 				continue;
 				continue;
@@ -492,7 +523,7 @@ class GlslOut {
 
 
 		if( outIndex < 2 )
 		if( outIndex < 2 )
 			outIndexes = null;
 			outIndexes = null;
-		else if( !isVertex )
+		else if( !isVertex && glES )
 			decl("#extension GL_EXT_draw_buffers : enable");
 			decl("#extension GL_EXT_draw_buffers : enable");
 
 
 		var tmp = buf;
 		var tmp = buf;
@@ -503,7 +534,7 @@ class GlslOut {
 			for( e in el ) {
 			for( e in el ) {
 				add("\t");
 				add("\t");
 				addExpr(e, "\t");
 				addExpr(e, "\t");
-				add(";\n");
+				newLine(e);
 			}
 			}
 		default:
 		default:
 			addExpr(f.expr, "");
 			addExpr(f.expr, "");
@@ -523,13 +554,12 @@ class GlslOut {
 			add("\n\n");
 			add("\n\n");
 		}
 		}
 
 
-		//#if GL_ES_VERSION_2_0  would be the test to use at compilation time, but would require a GL context to call glGetString (GL_SHADING_LANGUAGE_VERSION)
-		//#ifdef GL_ES is to test in the shader itself but #version  muse be declared first
-		#if((cpp && mobile)||js)
-		decl("#version 100");
-		#else
-		decl("#version 130");
-		#end
+		if( version != null )
+			decl("#version " + version);
+		else if( glES )
+			decl("#version 100");
+		else
+			decl("#version 130"); // OpenGL 3.0
 
 
 		decls.push(buf.toString());
 		decls.push(buf.toString());
 		buf = null;
 		buf = null;
@@ -537,7 +567,11 @@ class GlslOut {
 	}
 	}
 
 
 	public static function toGlsl( s : ShaderData ) {
 	public static function toGlsl( s : ShaderData ) {
-		return new GlslOut().run(s);
+		var out = new GlslOut();
+		#if js
+		out.glES = true;
+		#end
+		return out.run(s);
 	}
 	}
 
 
 }
 }