Browse Source

completed runtime support

Nicolas Cannasse 11 years ago
parent
commit
96f0c567cc
5 changed files with 127 additions and 24 deletions
  1. 38 11
      hxsl/Cache.hx
  2. 9 0
      hxsl/Globals.hx
  3. 71 8
      hxsl/Macros.hx
  4. 5 3
      hxsl/Shader.hx
  5. 4 2
      hxsl/Types.hx

+ 38 - 11
hxsl/Cache.hx

@@ -5,11 +5,13 @@ class AllocParam {
 	public var pos : Int;
 	public var instance : Int;
 	public var index : Int;
+	public var type : Type;
 	public var perObjectGlobal : AllocGlobal;
-	public function new(pos, instance, index) {
+	public function new(pos, instance, index, type) {
 		this.pos = pos;
 		this.instance = instance;
 		this.index = index;
+		this.type = type;
 	}
 }
 
@@ -17,22 +19,38 @@ class AllocGlobal {
 	public var pos : Int;
 	public var gid : Int;
 	public var path : String;
-	public function new(pos, path) {
+	public var type : Type;
+	public function new(pos, path, type) {
 		this.pos = pos;
 		this.path = path;
 		this.gid = Globals.allocID(path);
+		this.type = type;
 	}
 }
 
 class CompleteShader {
+	static var UID = 0;
+	public var id : Int;
 	public var data : ShaderData;
 	public var params : Array<AllocParam>;
 	public var paramsSize : Int;
 	public var globals : Array<AllocGlobal>;
 	public var globalsSize : Int;
-	public var paramsTexture : Array<AllocParam>;
-	public var globalsTexture : Array<AllocGlobal>;
+	public var paramTextures : Array<AllocParam>;
+	public var globalTextures : Array<AllocGlobal>;
 	public function new() {
+		id = UID++;
+	}
+}
+
+class ShaderBuffers {
+	public var globals : haxe.ds.Vector<Float>;
+	public var params : haxe.ds.Vector<Float>;
+	public var tex : haxe.ds.Vector<Types.Texture>;
+	public function new( c : CompleteShader ) {
+		globals = new haxe.ds.Vector(c.globalsSize);
+		params = new haxe.ds.Vector(c.paramsSize);
+		tex = new haxe.ds.Vector(c.globalTextures.length + c.paramTextures.length);
 	}
 }
 
@@ -133,26 +151,26 @@ class Cache {
 					if( a.v == null ) continue; // padding
 					var p = params.get(a.v.id);
 					if( p == null ) {
-						var ap = new AllocParam(a.pos, -1, -1);
-						ap.perObjectGlobal = new AllocGlobal( -1, getPath(a.v));
+						var ap = new AllocParam(a.pos, -1, -1, a.v.type);
+						ap.perObjectGlobal = new AllocGlobal( -1, getPath(a.v), a.v.type);
 						out.push(ap);
 						continue;
 					}
-					out.push(new AllocParam(a.pos, p.instance, p.index));
+					out.push(new AllocParam(a.pos, p.instance, p.index, a.v.type));
 				}
 				switch( g.type ) {
 				case TArray(TSampler2D, _):
-					c.paramsTexture = out;
+					c.paramTextures = out;
 				case TArray(TVec(4, VFloat),SConst(size)):
 					c.params = out;
 					c.paramsSize = size;
 				default: throw "assert";
 				}
 			case Global:
-				var out = [for( a in alloc ) if( a.v != null ) new AllocGlobal(a.pos, getPath(a.v))];
+				var out = [for( a in alloc ) if( a.v != null ) new AllocGlobal(a.pos, getPath(a.v), a.v.type)];
 				switch( g.type ) {
 				case TArray(TSampler2D, _):
-					c.globalsTexture = out;
+					c.globalTextures = out;
 				case TArray(TVec(4, VFloat),SConst(size)):
 					c.globals = out;
 					c.globalsSize = size;
@@ -162,7 +180,16 @@ class Cache {
 			default: throw "assert";
 			}
 		}
-		trace(c);
+		if( c.globals == null ) {
+			c.globals = [];
+			c.globalsSize = 0;
+		}
+		if( c.params == null ) {
+			c.params = [];
+			c.paramsSize = 0;
+		}
+		if( c.globalTextures == null ) c.globalTextures = [];
+		if( c.paramTextures == null ) c.paramTextures = [];
 		c.data = data;
 		return c;
 	}

+ 9 - 0
hxsl/Globals.hx

@@ -1,5 +1,14 @@
 package hxsl;
 
+abstract GlobalSlot<T>(Int) {
+	public inline function new(name:String) {
+		this = Globals.allocID(name);
+	}
+	public inline function set( globals : Globals, v : T ) {
+		globals.fastSet(this, v);
+	}
+}
+
 class Globals {
 
 	var map : Map<Int,Dynamic>;

+ 71 - 8
hxsl/Macros.hx

@@ -83,7 +83,7 @@ class Macros {
 	
 	static function buildFields( shader : ShaderData, pos : Position ) {
 		var fields = new Array<Field>();
-		var globals = [], consts = [];
+		var globals = [], consts = [], params = [];
 		for( v in shader.vars ) {
 			var cpos = consts.length;
 			getConsts(v, pos, consts);
@@ -93,9 +93,14 @@ class Macros {
 				var f : Field = {
 					name : v.name,
 					pos : pos,
-					kind : FProp("get","set", t, makeDef(v.type,pos)),
+					kind : FProp("get","set", t),
 					access : [APublic],
-					meta : [{ name : ":isVar", pos : pos }],
+				};
+				var name = v.name + "__";
+				var f2 : Field = {
+					name : name,
+					pos : pos,
+					kind : FVar(t,makeDef(v.type,pos)),
 				};
 				var fget : Field = {
 					name : "get_" + v.name,
@@ -104,11 +109,11 @@ class Macros {
 						ret : t,
 						args : [],
 						expr : if( consts.length == cpos || (consts.length == cpos+1 && consts[cpos].v == v) )
-							macro return $i{ v.name };
+							macro return $i{ name };
 						else
 							macro {
 								constModified = true;
-								return $i{ v.name };
+								return $i{ name };
 							},
 					}),
 					access : [AInline],
@@ -120,16 +125,18 @@ class Macros {
 						ret : t,
 						args : [ { name : "_v", type : t } ],
 						expr : if( consts.length == cpos )
-							macro return $i{ v.name } = _v;
+							macro return $i{ name } = _v;
 						else
 							macro {
 								constModified = true;
-								return $i{ v.name } = _v;
+								return $i{ name } = _v;
 							}
 					}),
 					access : [AInline],
 				};
 				fields.push(f);
+				fields.push(f2);
+				params.push(name);
 				fields.push(fget);
 				fields.push(fset);
 			case Global:
@@ -141,7 +148,7 @@ class Macros {
 		var exprs = [];
 		function getPath(v:TVar) {
 			if( v.parent == null )
-				return { expr : haxe.macro.Expr.ExprDef.EConst(CIdent(v.name)), pos : pos };
+				return { expr : haxe.macro.Expr.ExprDef.EConst(CIdent(v.name+"__")), pos : pos };
 			return { expr : haxe.macro.Expr.ExprDef.EField(getPath(v.parent), v.name), pos : pos };
 		}
 		for( c in consts ) {
@@ -174,6 +181,23 @@ class Macros {
 			}),
 			access : [AOverride],
 		});
+		var index = 0;
+		fields.push( {
+			name : "getParamValue",
+			pos : pos,
+			kind : FFun( {
+				ret : macro : Dynamic,
+				args : [ { name : "index", type : macro : Int } ],
+				expr : {
+					expr : EBlock([
+						{ expr : ESwitch(macro index, [for( p in params ) { values : [macro $v{ index++ } ], expr : macro return $i{ p } } ], null), pos : pos },
+						macro return null,
+					]),
+					pos : pos,
+				},
+			}),
+			access : [AOverride],
+		});
 		return fields;
 	}
 	
@@ -207,4 +231,43 @@ class Macros {
 		return fields;
 	}
 	
+	public static function buildGlobals() {
+		var fields = Context.getBuildFields();
+		var globals = [];
+		var sets = [];
+		for( f in fields ) {
+			for( m in f.meta ) {
+				if( m.name == "global" )
+					switch( [f.kind, m.params[0].expr] ) {
+					case [FVar(t, set), EConst(CString(name))]:
+						f.kind = FVar(macro : hxsl.Globals.GlobalSlot<$t>);
+						globals.push(macro $i{ f.name } = new hxsl.Globals.GlobalSlot($v { name } ));
+						if( set != null )
+							sets.push(macro $i{ f.name }.set(globals, $set));
+					default:
+					}
+			}
+		}
+		var p = Context.currentPos();
+		fields.push({
+			name : "initGlobals",
+			kind : FFun({
+				ret : null,
+				expr : { expr : EBlock(globals), pos : p },
+				args : [],
+			}),
+			pos : p,
+		});
+		fields.push({
+			name : "setGlobals",
+			kind : FFun({
+				ret : null,
+				expr : { expr : EBlock(sets), pos : p },
+				args : [],
+			}),
+			pos : p,
+		});
+		return fields;
+	}
+	
 }

+ 5 - 3
hxsl/Shader.hx

@@ -19,6 +19,11 @@ class Shader {
 		}
 	}
 	
+	public function getParamValue( index : Int ) : Dynamic {
+		throw "assert"; // will be subclassed in sub shaders
+		return null;
+	}
+	
 	public function updateConstants( globals : Globals ) {
 		for( c in shader.consts )
 			if( c.globalId > 0 ) {
@@ -38,7 +43,4 @@ class Shader {
 		instance = shader.getInstance(constBits);
 	}
 	
-	public function setup( globals : Globals ) {
-	}
-	
 }

+ 4 - 2
hxsl/Types.hx

@@ -6,8 +6,9 @@ typedef Vec = h3d.Vector;
 typedef IVec = Array<Int>;
 typedef BVec = Array<Bool>;
 typedef Matrix = h3d.Matrix;
-typedef Sampler2D = /*h3d.mat.Texture*/ Dynamic;
-typedef SamplerCube = /*h3d.mat.Texture*/ Dynamic;
+typedef Texture = h3d.mat.Texture;
+typedef Sampler2D = h3d.mat.Texture;
+typedef SamplerCube = h3d.mat.Texture;
 
 #else
 
@@ -15,6 +16,7 @@ typedef Vec = Array<Float>;
 typedef IVec = Array<Int>;
 typedef BVec = Array<Bool>;
 typedef Matrix = Array<Float>;
+typedef Texture = Dynamic;
 typedef Sampler2D = Dynamic;
 typedef SamplerCube = Dynamic;