Browse Source

added features detection, hxsl support for standard derivatives

Nicolas Cannasse 11 years ago
parent
commit
e6c016f213
6 changed files with 50 additions and 5 deletions
  1. 4 0
      h3d/Engine.hx
  2. 9 0
      h3d/impl/Driver.hx
  3. 9 0
      h3d/impl/GlDriver.hx
  4. 4 0
      hxsl/Ast.hx
  5. 2 0
      hxsl/Checker.hx
  6. 22 5
      hxsl/GlslOut.hx

+ 4 - 0
h3d/Engine.hx

@@ -254,6 +254,10 @@ class Engine {
 	function reset() {
 		driver.reset();
 	}
+	
+	public function hasFeature(f) {
+		return driver.hasFeature(f);
+	}
 
 	public function end() {
 		driver.present();

+ 9 - 0
h3d/impl/Driver.hx

@@ -18,8 +18,17 @@ typedef VertexBuffer = Int;
 typedef Texture = Int;
 #end
 
+enum Feature {
+	StandardDerivatives;
+	FloatTextures;
+}
+
 class Driver {
 	
+	public function hasFeature( f : Feature ) {
+		return false;
+	}
+	
 	public function isDisposed() {
 		return true;
 	}

+ 9 - 0
h3d/impl/GlDriver.hx

@@ -471,6 +471,15 @@ class GlDriver extends Driver {
 		#end
 	}
 	
+	override function hasFeature( f : Feature ) : Bool {
+		return switch( f ) {
+		case StandardDerivatives:
+			gl.getExtension('OES_standard_derivatives') != null;
+		case FloatTextures:
+			gl.getExtension('OES_texture_float') != null;
+		}
+	}
+	
 	static var TFILTERS = [
 		[[GL.NEAREST,GL.NEAREST],[GL.LINEAR,GL.LINEAR]],
 		[[GL.NEAREST,GL.NEAREST_MIPMAP_NEAREST],[GL.LINEAR,GL.LINEAR_MIPMAP_NEAREST]],

+ 4 - 0
hxsl/Ast.hx

@@ -205,6 +205,10 @@ enum TGlobal {
 	// extra (not in GLSL ES)
 	Mat3x4;
 	Saturate;
+	// extensions
+	DFdx;
+	DFdy;
+	Fwidth;
 }
 
 enum Component {

+ 2 - 0
hxsl/Checker.hx

@@ -97,6 +97,8 @@ class Checker {
 						r.push( { args : [ { name : "edge0", type : TFloat }, { name : "edge1", type : TFloat }, { name : "x", type : t } ], ret : t } );
 				}
 				r;
+			case DFdx, DFdy, Fwidth:
+				genFloat;
 			}
 			if( def != null )
 				globals.set(g.toString(), { t : TFun(def), g : g } );

+ 22 - 5
hxsl/GlslOut.hx

@@ -2,11 +2,14 @@ package hxsl;
 import hxsl.Ast;
 
 class GlslOut {
+	
+	static var MAT34 = "struct mat3x4 { vec4 a; vec4 b; vec4 c; };";
 
 	var buf : StringBuf;
 	var keywords : Map<String,Bool>;
 	var exprValues : Array<String>;
 	var locals : Array<TVar>;
+	var decls : Array<String>;
 	var globalNames : Map<TGlobal,String>;
 	
 	public function new() {
@@ -32,6 +35,12 @@ class GlslOut {
 		add(keywords.exists(i) ? "_" + i : i);
 	}
 	
+	function decl( s : String ) {
+		for( d in decls )
+			if( d == s ) return;
+		decls.push(s);
+	}
+	
 	function addType( t : Type ) {
 		switch( t ) {
 		case TVoid:
@@ -60,6 +69,7 @@ class GlslOut {
 		case TMat4:
 			add("mat4");
 		case TMat3x4:
+			decl(MAT34);
 			add("mat3x4");
 		case TSampler2D:
 			add("sampler2D");
@@ -152,6 +162,14 @@ class GlslOut {
 		case TVar(v):
 			ident(v.name);
 		case TGlobal(g):
+			switch( g ) {
+			case Mat3x4:
+				decl(MAT34);
+				decl("vec3 m3x4mult( vec3 v, mat3x4 m) { vec4 ve = vec4(v,1.0); return vec3(dot(m.a,ve),dot(m.b,ve),dot(m.c,ve)); }");
+			case DFdx, DFdy, Fwidth:
+				decl("#extension GL_OES_standard_derivatives:enable");
+			default:
+			}
 			add(globalNames.get(g));
 		case TParenthesis(e):
 			add("(");
@@ -266,11 +284,10 @@ class GlslOut {
 	
 	public function run( s : ShaderData ) {
 		locals = [];
+		decls = [];
 		buf = new StringBuf();
 		exprValues = [];
-		add("precision mediump float;\n");
-		add("struct mat3x4 { vec4 a; vec4 b; vec4 c; };\n");
-		add("vec3 m3x4mult( vec3 v, mat3x4 m) { vec4 ve = vec4(v,1.0); return vec3(dot(m.a,ve),dot(m.b,ve),dot(m.c,ve)); }\n");
+		decls.push("precision mediump float;");
 
 		if( s.funs.length != 1 ) throw "assert";
 		var f = s.funs[0];
@@ -318,9 +335,9 @@ class GlslOut {
 			add(e);
 			add("\n\n");
 		}
-		var content = buf.toString();
+		decls.push(buf.toString());
 		buf = null;
-		return content;
+		return decls.join("\n");
 	}
 	
 	public static function toGlsl( s : ShaderData ) {