Browse Source

single list of textures (both params and globals), fixed tests with good matrix mult order

Nicolas Cannasse 11 years ago
parent
commit
1e7317d273
5 changed files with 17 additions and 90 deletions
  1. 5 8
      hxsl/Cache.hx
  2. 4 5
      hxsl/Flatten.hx
  3. 0 69
      hxsl/GetVars.hx
  4. 1 1
      hxsl/Macros.hx
  5. 7 7
      test/Test.hx

+ 5 - 8
hxsl/Cache.hx

@@ -36,8 +36,7 @@ class CompleteShader {
 	public var paramsSize : Int;
 	public var globals : Array<AllocGlobal>;
 	public var globalsSize : Int;
-	public var paramTextures : Array<AllocParam>;
-	public var globalTextures : Array<AllocGlobal>;
+	public var textures : Array<AllocParam>;
 	public function new() {
 		id = UID++;
 	}
@@ -50,7 +49,7 @@ class ShaderBuffers {
 	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);
+		tex = new haxe.ds.Vector(c.textures.length);
 	}
 }
 
@@ -160,7 +159,7 @@ class Cache {
 				}
 				switch( g.type ) {
 				case TArray(TSampler2D, _):
-					c.paramTextures = out;
+					c.textures = out;
 				case TArray(TVec(4, VFloat),SConst(size)):
 					c.params = out;
 					c.paramsSize = size;
@@ -169,8 +168,6 @@ class Cache {
 			case Global:
 				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.globalTextures = out;
 				case TArray(TVec(4, VFloat),SConst(size)):
 					c.globals = out;
 					c.globalsSize = size;
@@ -188,8 +185,8 @@ class Cache {
 			c.params = [];
 			c.paramsSize = 0;
 		}
-		if( c.globalTextures == null ) c.globalTextures = [];
-		if( c.paramTextures == null ) c.paramTextures = [];
+		if( c.textures == null )
+			c.textures = [];
 		c.data = data;
 		return c;
 	}

+ 4 - 5
hxsl/Flatten.hx

@@ -39,10 +39,9 @@ class Flatten {
 		case Fragment: "fragment";
 		default: throw "assert";
 		}
-		pack(prefix+"Globals", Global, globals, VFloat);
+		pack(prefix + "Globals", Global, globals, VFloat);
 		pack(prefix + "Params", Param, params, VFloat);
-		packTextures(prefix + "GTextures", Global, globals, TSampler2D);
-		packTextures(prefix + "PTextures", Param, params, TSampler2D);
+		packTextures(prefix + "Textures", globals.concat(params), TSampler2D);
 		return {
 			name : s.name,
 			vars : outVars,
@@ -138,13 +137,13 @@ class Flatten {
 		return e;
 	}
 	
-	function packTextures( name : String, kind : VarKind, vars : Array<TVar>, t : Type ) {
+	function packTextures( name : String, vars : Array<TVar>, t : Type ) {
 		var alloc = new Array<Alloc>();
 		var g : TVar = {
 			id : Tools.allocVarId(),
 			name : name,
 			type : t,
-			kind : kind,
+			kind : Param,
 		};
 		for( v in vars ) {
 			if( v.type != t ) continue;

+ 0 - 69
hxsl/GetVars.hx

@@ -1,69 +0,0 @@
-package hxsl;
-import hxsl.Ast;
-
-/**
-	Retreive which variables are read/written by a given entry function and all its dependencies.
-**/
-class GetVars {
-	
-	var functions : Map<String,Bool>;
-	var vars : Map<TVar, { v : TVar, read : Bool, written : Bool } >;
-	var nullVar : { v : TVar, read : Bool, written : Bool };
-	
-	public function new() {
-		nullVar = { v : null, read : true, written : true };
-	}
-	
-	public function getVars( s : ShaderData, entryFunc : String ) {
-		vars = new Map();
-		functions = new Map();
-		for( f in s.funs )
-			if( f.name == entryFunc ) {
-				getFun(f);
-				return Lambda.array(vars);
-			}
-		return [];
-	}
-	
-	function getVar( v : TVar ) {
-		switch( v.kind ) {
-		case Local:
-			return nullVar;
-		case Var, Input, Param, Global:
-			var vinf = vars.get(v);
-			if( vinf == null ) {
-				vinf = { v : v, read : false, written : false };
-				vars.set(v, vinf);
-			}
-			return vinf;
-		}
-	}
-	
-	function getFun( f : TFunction ) {
-		if( functions.exists(f.name) )
-			return;
-		functions.set(f.name, true);
-		getExpr(f.expr);
-	}
-	
-	function getExpr( e : TExpr ) {
-		switch( e.e ) {
-		case TBinop(OpAssign, { e : TVar(v) }, e2):
-			getExpr(e2);
-			getVar(v).written = true;
-		case TBinop(OpAssignOp(_), { e : TVar(v) }, e2):
-			getExpr(e2);
-			var v = getVar(v);
-			v.read = true;
-			v.written = true;
-		case TVar(v):
-			getVar(v).read = true;
-		case TFunVar(f):
-			getFun(f);
-		default:
-			Tools.iter(e, getExpr);
-		}
-	}
-	
-	
-}

+ 1 - 1
hxsl/Macros.hx

@@ -190,7 +190,7 @@ class Macros {
 				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 },
+						{ expr : ESwitch(macro index, [for( p in params ) { values : [macro $v{ index++ } ], expr : macro return $i{ p } } ], macro {}), pos : pos },
 						macro return null,
 					]),
 					pos : pos,

+ 7 - 7
test/Test.hx

@@ -42,9 +42,9 @@ class Proto extends hxsl.Shader {
 	
 	// each __init__ expr is out of order dependency-based
 	function __init__() {
-		transformedPosition = global.modelView.mat3x4() * input.position;
-		projectedPosition = camera.viewProj * vec4(transformedPosition, 1);
-		transformedNormal = global.modelViewInverse.mat3() * input.normal;
+		transformedPosition = input.position * global.modelView.mat3x4();
+		projectedPosition = vec4(transformedPosition, 1) * camera.viewProj;
+		transformedNormal = input.normal * global.modelViewInverse.mat3();
 		camera.dir = (camera.position - transformedPosition).normalize();
 		pixelColor = color;
 	}
@@ -229,9 +229,9 @@ static var SRC = {
 	
 	function vertex() {
 		var p = input.pos, n = input.normal;
-		// TODO : accessing skimMatrixes should multiply by 255*3 in AGAL (byte converted to [0-1] float + stride for 3 vec4 per element)
-		transformedPosition = skinMatrixes[input.indexes.x] * (p * input.weights.x) + skinMatrixes[input.indexes.y] * (p * input.weights.y) + skinMatrixes[input.indexes.z] * (p * input.weights.z);
-		transformedNormal = skinMatrixes[input.indexes.x].mat3() * (n * input.weights.x) + skinMatrixes[input.indexes.y].mat3() * (n * input.weights.y) + skinMatrixes[input.indexes.z].mat3() * (n * input.weights.z);
+		// TODO : accessing skinMatrixes should multiply by 255*3 in AGAL (byte converted to [0-1] float + stride for 3 vec4 per element)
+		transformedPosition = p * input.weights.x * skinMatrixes[input.indexes.x] + p * input.weights.y * skinMatrixes[input.indexes.y] + p * input.weights.z * skinMatrixes[input.indexes.z];
+		transformedNormal = n * input.weights.x * skinMatrixes[input.indexes.x].mat3() + n * input.weights.y * skinMatrixes[input.indexes.y].mat3() + n * input.weights.z * skinMatrixes[input.indexes.z].mat3();
 	}
 
 }
@@ -267,7 +267,7 @@ static var SRC = {
 	@private var tshadowPos : Vec3;
 	
 	function vertex() {
-		tshadowPos = shadow.lightProj.mat3x4() * (shadow.lightCenter.mat3x4() * transformedPosition);
+		tshadowPos = transformedPosition * shadow.lightCenter.mat3x4() * shadow.lightProj.mat3x4();
 	}
 	
 	function fragment() {