Browse Source

allow variable paths in array size decls

Nicolas Cannasse 11 năm trước cách đây
mục cha
commit
8983f4f96a
3 tập tin đã thay đổi với 39 bổ sung11 xóa
  1. 31 3
      hxsl/Checker.hx
  2. 4 1
      hxsl/MacroParser.hx
  3. 4 7
      test/Test.hx

+ 31 - 3
hxsl/Checker.hx

@@ -509,9 +509,37 @@ class Checker {
 			var s = switch( size ) {
 			case SConst(_): size;
 			case SVar(v):
-				var v2 = vars.get(v.name);
-				if( v2 == null ) error("Array size variable not found", pos);
-				if( !v2.isConst() ) error("Array size variable should be a constant", pos);
+				var path = v.name.split(".");
+				var v2 = null;
+				for( n in path ) {
+					if( v2 == null ) {
+						v2 = vars.get(n);
+						// special handling when we reference our own variable which is being currently declared
+						if( v2 == null && parent != null ) {
+							var p = parent;
+							while( p.parent != null )
+								p = p.parent;
+							if( p.name == n )
+								v2 = p;
+						}
+					} else {
+						v2 = switch( v2.type ) {
+						case TStruct(vl):
+							var f = null;
+							for( v in vl )
+								if( v.name == n ) {
+									f = v;
+									break;
+								}
+							f;
+						default:
+							null;
+						}
+					}
+					if( v2 == null ) break;
+				}
+				if( v2 == null ) error("Array size variable '" + v.name + "'not found", pos);
+				if( !v2.isConst() ) error("Array size variable '" + v.name + "'should be a constant", pos);
 				SVar(v2);
 			}
 			return TArray(makeVarType(t,parent,pos), s);

+ 4 - 1
hxsl/MacroParser.hx

@@ -74,7 +74,10 @@ class MacroParser {
 			}
 			var size : Ast.SizeDecl = switch( size ) {
 			case TPExpr({ expr : EConst(CInt(v)) }): SConst(Std.parseInt(v));
-			case TPType(TPath( { pack : [], name : name, sub : null, params : [] } )): SVar( { id : 0, type : null, name : name, kind : null } );
+			case TPType(TPath( { pack : pack, name : name, sub : null, params : [] } )):
+				var pack = pack.copy();
+				pack.push(name);
+				SVar( { id : 0, type : null, name : pack.join("."), kind : null } );
 			default: null;
 			}
 			if( t != null && size != null )

+ 4 - 7
test/Test.hx

@@ -137,16 +137,13 @@ static var SRC = {
 class LightSystem extends hxsl.Shader {
 static var SRC = {
 
-	@global("light.ndirs") @const(16) var NDirLights : Int;
-	@global("light.npoints") @const(64) var NPointLights : Int;
-	
 	@global var light : {
 		@const var perPixel : Bool;
-		@const(16) var ndirs : Int;
-		@const(64) var npoints : Int;
+		@const(16) var NDirs : Int;
+		@const(64) var NPoints : Int;
 		var ambient : Vec3;
-		var dirs : Array<{ dir : Vec3, color : Vec3 }, NDirLights>;
-		var points : Array<{ pos : Vec3, color : Vec3, att : Vec3 }, NPointLights>;
+		var dirs : Array<{ dir : Vec3, color : Vec3 }, light.NDirs>;
+		var points : Array<{ pos : Vec3, color : Vec3, att : Vec3 }, light.NPoints>;
 	};
 	
 	var transformedPosition : Vec3;