Bladeren bron

use hxsl v2 (wip)

ncannasse 12 jaren geleden
bovenliggende
commit
62d53a175d
6 gewijzigde bestanden met toevoegingen van 189 en 151 verwijderingen
  1. 1 1
      engine.hxml
  2. 1 1
      engine.hxproj
  3. 78 136
      h3d/impl/Macros.hx
  4. 96 12
      h3d/mat/MeshTexture.hx
  5. 12 0
      h3d/scene/Scene.hx
  6. 1 1
      haxelib.xml

+ 1 - 1
engine.hxml

@@ -4,6 +4,6 @@
 --flash-strict
 -swf-version 11
 -main Test
--lib format
+-lib hxsl
 --macro include('h3d')
 --macro include('h2d')

+ 1 - 1
engine.hxproj

@@ -23,7 +23,7 @@
     <option flashStrict="True" />
     <option mainClass="Test" />
     <option enabledebug="False" />
-    <option additional="-lib format&#xA;--macro include('h3d')&#xA;--macro include('h2d')" />
+    <option additional="-lib hxsl&#xA;--macro include('h3d')&#xA;--macro include('h2d')" />
   </build>
   <!-- haxelib libraries -->
   <haxelib>

+ 78 - 136
h3d/impl/Macros.hx

@@ -3,12 +3,14 @@ package h3d.impl;
 #if macro
 import haxe.macro.Context;
 import haxe.macro.Expr;
-import format.hxsl.Data;
+import hxsl.Data;
 
 class Macros {
 
 	static function realType( t : VarType, p : Position ) : ComplexType {
 		return TPath(switch( t ) {
+		case TNull: throw "assert";
+		case TBool: { pack : [], name : "Bool", params : [], sub : null };
 		case TFloat: { pack : [], name : "Float", params : [], sub : null };
 		case TFloat2, TFloat3, TFloat4: { pack : ["h3d"], name : "Vector", params : [], sub : null };
 		case TInt: { pack : [], name : "Int", params : [], sub : null };
@@ -18,114 +20,6 @@ class Macros {
 		});
 	}
 
-	static function makeShaderVars( shader : Code, fields : Array<Field> ) {
-		var pos = 0;
-		var fset = shader.vertex ? "data.vertexVars" : "data.fragmentVars";
-		for( c in shader.args.concat(shader.tex) ) {
-			var set = [], get = "null";
-			var old = pos;
-			var iPrefix = "";
-			function add(e) {
-				set.push(fset + "[" + iPrefix + pos + "]=" + e + ";");
-				pos++;
-			}
-			function addType(n:String,t:VarType) {
-				switch( t ) {
-				case TFloat:
-					add(n);
-					add(n);
-					add(n);
-					add(n);
-				case TFloat2:
-					add(n + ".x");
-					add(n + ".y");
-					add("1.");
-					add("1.");
-				case TFloat3:
-					add(n + ".x");
-					add(n + ".y");
-					add(n + ".z");
-					add("1.");
-				case TFloat4:
-					add(n + ".x");
-					add(n + ".y");
-					add(n + ".z");
-					add(n + ".w");
-				case TMatrix(w,h,t):
-					for( y in 1...w+1 )
-						for( x in 1...h+1 ) {
-							if( t.t )
-								add(n+"._"+x+y);
-							else
-								add(n+"._"+y+x);
-						}
-				case TTexture(_):
-					get = "get_" + c.name;
-					set.push("data.textures[" + c.index + "] = " + n + ";");
-					set.push("data.texturesChanged = true;");
-					fields.push( {
-						name : get,
-						access : [APrivate],
-						kind : FFun( {
-							params : [],
-							args : [],
-							ret : null,
-							expr : Context.parse("return data.textures[" + c.index + "]", c.pos),
-						}),
-						pos : c.pos,
-					});
-				case TInt:
-					add("((" + n + ">>16) & 0xFF) / 255.0");
-					add("((" + n + ">>8) & 0xFF) / 255.0");
-					add("(" + n + " & 0xFF) / 255.0");
-					add("(" + n + ">>>24) / 255.0");
-				case TArray(t, count):
-					iPrefix += "_i*" + Tools.floatSize(t) + "+";
-					set.push("var max = " + n + ".length;");
-					set.push("for( _i in 0...(max < " + count + "?max:"+count+") ) {");
-					addType(n + "[_i]", t);
-					set.push("}");
-					iPrefix = "";
-					pos += Tools.floatSize(t) * (count - 1);
-				}
-			}
-			addType(c.name, c.type);
-			if( pos > old )
-				set.push(fset + "Changed=true;");
-			set.push("return " + c.name + ";");
-			
-			var t = realType(c.type,c.pos);
-			fields.push( {
-				name : c.name,
-				access : [APublic],
-				kind : FProp(get, "set_" + c.name, t),
-				pos : c.pos,
-			});
-			fields.push( {
-				name : "set_" + c.name,
-				access : [APrivate],
-				kind : FFun( {
-					params : [],
-					args : [ { name : c.name, type : t, opt : false, value : null } ],
-					ret : t,
-					expr : Context.parse("{"+set.join("")+"}",c.pos),
-				}),
-				pos : c.pos,
-			});
-		}
-		var cst = [];
-		for( i in 0...pos )
-			cst.push("0.");
-		for( c in shader.consts ) {
-			for( i in 0...4 ) {
-				var v = c[i];
-				if( v == null ) v = "0";
-				cst.push(v);
-			}
-		}
-		return cst;
-	}
-	
 	public static function buildShader() {
 		var fields = Context.getBuildFields();
 		var shaderCode = null;
@@ -151,44 +45,90 @@ class Macros {
 			Context.error("Shader SRC not found", cl.pos);
 		}
 		
-		var p = new format.hxsl.Parser();
+		var p = new hxsl.Parser();
 		p.includeFile = function(file) {
 			var f = Context.resolvePath(file);
 			return Context.parse("{"+sys.io.File.getContent(f)+"}", Context.makePosition( { min : 0, max : 0, file : f } ));
 		};
-		var v = try p.parse(shaderCode) catch( e : format.hxsl.Data.Error ) haxe.macro.Context.error(e.message, e.pos);
-		var c = new format.hxsl.Compiler();
-		c.warn = Context.warning;
-		var v = try c.compile(v) catch( e : format.hxsl.Data.Error ) haxe.macro.Context.error(e.message, e.pos);
 
-		var c = new format.agal.Compiler();
-		c.error = function(msg, p) { Context.error(msg, p); return null; }
+		var data = try p.parse(shaderCode) catch( e : hxsl.Data.Error ) haxe.macro.Context.error(e.message, e.pos);
+		var c = new hxsl.Compiler();
+		c.warn = Context.warning;
+		var data = try c.compile(data) catch( e : hxsl.Data.Error ) haxe.macro.Context.error(e.message, e.pos);
 
-		var vscode = c.compile(v.vertex);
-		var fscode = c.compile(v.fragment);
+		var pos = Context.currentPos();
 		
-		//trace("----");
-		//for( i in 0...vscode.code.length )
-		//	trace(i+": "+format.agal.Tools.opStr(vscode.code[i]));
-
-		var o = new haxe.io.BytesOutput();
-		new format.agal.Writer(o).write(vscode);
-		var vsbytes = haxe.Serializer.run(o.getBytes());
-
-		var o = new haxe.io.BytesOutput();
-		new format.agal.Writer(o).write(fscode);
-		var fsbytes = haxe.Serializer.run(o.getBytes());
+		// store HXSL data for runtime compilation
+		fields.push( {
+			name : "HXSL",
+			kind : FVar(null,{ expr : EConst(CString(hxsl.Serialize.serialize(data))), pos : pos }),
+			access : [AStatic],
+			pos : pos,
+		});
 		
-		var max = 200;
-		if( vscode.code.length > max )
-			Context.error("This vertex shader uses " + vscode.code.length + " opcodes but only " + max + " are allowed by Flash11", v.vertex.pos);
-		if( fscode.code.length > max )
-			Context.error("This fragment shader uses " + fscode.code.length + " opcodes but only " + max + " are allowed by Flash11", v.fragment.pos);
-	
+		// create all the variables accessors
+		var allVars = data.globals.concat(data.vertex.args).concat(data.fragment.args);
+		for( v in data.vertex.tex.concat(data.fragment.tex) )
+			allVars.push(v);
+		for( v in allVars ) {
+			switch( v.kind ) {
+			case VConst, VParam:
+				fields.push( {
+					name : v.name,
+					kind : FProp("never", "set", realType(v.type, v.pos)),
+					pos : v.pos,
+					access : [APublic],
+				});
+				/*
+				fields.push({
+					name : "get_" + v.name,
+					kind : FFun({
+					}),
+					pos : v.pos,
+					access : [AInline],
+				});
+				fields.push( {
+					name : "set_" + v.name,
+					kind : FFun( {
+						ret : null,
+						params : [ { ]],
+						expr :
+					}),
+					pos : v.pos,
+					access : [AInline],
+				});
+				*/
+			case VTexture:
+				fields.push( {
+					name : v.name,
+					kind : FProp("never", "set", realType(v.type, v.pos)),
+					pos : v.pos,
+					access : [APublic],
+				});
+				/*
+				fields.push( {
+					name : "set_" + v.name,
+					kind : FFun({
+					}),
+					pos : v.pos,
+					access : [AInline],
+				});
+				*/
+			case VInput, VVar, VOut:
+				// skip
+			default:
+				Context.warning(v.name, v.pos);
+				throw "assert";
+			}
+		}
+		
+		
+		
+		/*
 		var stride = 0;
 		var fmt = 0;
 		var pos = 0;
-		for( i in v.input ) {
+		for( i in v ) {
 			function add(size) {
 				fmt |= size << (pos * 3);
 				pos++;
@@ -261,6 +201,8 @@ class Macros {
 				ret : tbytes,
 			}),
 		});
+		*/
+		
 		
 		return fields;
 	}

+ 96 - 12
h3d/mat/MeshTexture.hx

@@ -1,38 +1,122 @@
 package h3d.mat;
 
-private class Shader extends h3d.Shader {
+private class MeshShader extends h3d.Shader {
+	
 	static var SRC = {
+
 		var input : {
 			pos : Float3,
 			uv : Float2,
 		};
+		
 		var tuv : Float2;
+		
+		var useMatrixPos : Bool;
+		var uvScale : Float2;
+		var uvDelta : Float2;
+		
 		function vertex( mpos : Matrix, mproj : Matrix ) {
-			out = pos.xyzw * mpos * mproj;
-			tuv = uv;
+			out = if( useMatrixPos ) (pos.xyzw * mpos) * mproj else pos.xyzw * mproj;
+			var t = uv;
+			if( uvScale != null ) t *= uvScale;
+			if( uvDelta != null ) t += uvDelta;
+			tuv = t;
 		}
-		function fragment( tex : Texture ) {
-			out = tex.get(tuv.xy);
+		
+		function fragment( tex : Texture, killAlpha : Bool, colorDelta : Float4, colorMult : Float4, colorMatrix : M44 ) {
+			var c = tex.get(tuv.xy);
+			if( killAlpha ) kill(c.a - 0.001);
+			if( colorDelta != null ) c += colorDelta;
+			if( colorMult != null ) c = c * colorMult;
+			if( colorMatrix != null ) c = c * colorMatrix;
+			out = c;
 		}
+		
 	}
+	
 }
 
 class MeshTexture extends MeshMaterial {
 
-	static var SHADER = new Shader();
+	var mshader : MeshShader;
 	
 	public var texture : Texture;
+
+	public var useMatrixPos : Bool;
+	public var uvScale(get,set) : Null<h3d.Vector>;
+	public var uvDelta(get,set) : Null<h3d.Vector>;
+
+	public var killAlpha(get,set) : Bool;
+
+	public var colorDelta(get,set) : Null<h3d.Color>;
+	public var colorMult(get,set) : Null<h3d.Color>;
+	public var colorMatrix(get,set) : Null<h3d.Matrix>;
 	
 	public function new(texture) {
-		super(SHADER);
+		mshader = new MeshShader();
+		super(mshader);
 		this.texture = texture;
+		useMatrixPos = true;
 	}
 	
 	public override function setMatrixes( mpos, mproj ) {
-		var s = SHADER;
-		s.mpos = mpos;
-		s.mproj = mproj;
-		s.tex = texture;
+		mshader.mpos = useMatrixPos ? mpos : null;
+		mshader.mproj = mproj;
+		mshader.tex = texture;
 	}
 	
-}
+	inline function get_uvScale() {
+		return mshader.uvScale;
+	}
+
+	inline function set_uvScale(v) {
+		mshader.uvScale = v;
+		return v;
+	}
+
+	inline function get_uvDelta() {
+		return mshader.uvDelta;
+	}
+
+	inline function set_uvDelta(v) {
+		mshader.uvDelta = v;
+		return v;
+	}
+
+	inline function get_killAlpha() {
+		return mshader.killAlpha;
+	}
+
+	inline function set_killAlpha(v) {
+		mshader.killAlpha = v;
+		return v;
+	}
+
+	inline function get_colorDelta() {
+		return mshader.colorDelta;
+	}
+
+	inline function set_colorDelta(v) {
+		mshader.colorDelta = v;
+		return v;
+	}
+
+	inline function get_colorMult() {
+		return mshader.colorMult;
+	}
+
+	inline function set_colorMult(v) {
+		mshader.colorMult = v;
+		return v;
+	}
+
+	inline function get_colorMatrix() {
+		return mshader.colorMatrix;
+	}
+
+	inline function set_colorMatrix(v) {
+		mshader.colorMatrix = v;
+		return v;
+	}
+	
+}

+ 12 - 0
h3d/scene/Scene.hx

@@ -3,10 +3,20 @@ package h3d.scene;
 class Scene extends Layers {
 
 	public var camera : h3d.Camera;
+	var extraPasses : Array<{ function render( engine : h3d.Engine ) : Void; }>;
 	
 	public function new() {
 		super(null);
 		camera = new h3d.Camera();
+		extraPasses = [];
+	}
+	
+	public function addPass(p) {
+		extraPasses.push(p);
+	}
+	
+	public function removePass(p) {
+		extraPasses.remove(p);
 	}
 	
 	// make it public
@@ -16,6 +26,8 @@ class Scene extends Layers {
 		var oldProj = engine.curProjMatrix;
 		engine.curProjMatrix = camera.m;
 		super.render(engine);
+		for( p in extraPasses )
+			p.render(engine);
 		engine.curProjMatrix = oldProj;
 	}
 	

+ 1 - 1
haxelib.xml

@@ -2,7 +2,7 @@
     <user name="ncannasse"/>
     <tag v="flash"/>
     <tag v="3d"/>
-    <depends name="format"/>
+    <depends name="hxsl"/>
     <description>Lightweight Stage3D engine</description>
     <version name="0.1">Imported from MT</version>
 </project>