Browse Source

Implement @doc meta for shaders (#891)

Pavel Alexandrov 4 years ago
parent
commit
be8204af9b
6 changed files with 22 additions and 3 deletions
  1. 12 0
      hxsl/Ast.hx
  2. 1 1
      hxsl/Checker.hx
  3. 3 0
      hxsl/MacroParser.hx
  4. 3 2
      hxsl/Macros.hx
  5. 1 0
      hxsl/Printer.hx
  6. 2 0
      hxsl/Serializer.hx

+ 12 - 0
hxsl/Ast.hx

@@ -83,6 +83,7 @@ enum VarQualifier {
 	Range( min : Float, max : Float );
 	Ignore; // the variable is ignored in reflection (inspector)
 	PerInstance( v : Int );
+	Doc( s : String );
 }
 
 enum Prec {
@@ -315,6 +316,17 @@ class Tools {
 		return v.name;
 	}
 
+	public static function getDoc( v : TVar ) {
+		if ( v.qualifiers == null )
+			return null;
+		for ( q in v.qualifiers )
+			switch ( q ) {
+			case Doc(s): return s;
+			default:
+			}
+		return null;
+	}
+
 	public static function getConstBits( v : TVar ) {
 		switch( v.type ) {
 		case TBool:

+ 1 - 1
hxsl/Checker.hx

@@ -794,7 +794,7 @@ class Checker {
 					default:
 						error("Precision qualifier not supported on " + v.type, pos);
 					}
-				case Ignore:
+				case Ignore, Doc(_):
 				}
 		}
 		if( tv.type != null )

+ 3 - 0
hxsl/MacroParser.hx

@@ -26,6 +26,9 @@ class MacroParser {
 		case [ { expr : EConst(CInt(a)) } ] if( m.name == "perInstance" ):
 			v.qualifiers.push(PerInstance(Std.parseInt(a)));
 			return;
+		case [ { expr: EConst(CString(s)), pos: pos } ] if (m.name == "doc"):
+			v.qualifiers.push(Doc(s));
+			return;
 		default:
 			error("Invalid meta parameter for "+m.name, m.pos);
 		}

+ 3 - 2
hxsl/Macros.hx

@@ -4,7 +4,7 @@ import haxe.macro.Expr;
 using hxsl.Ast;
 
 class Macros {
-
+	#if macro
 	static function makeType( t : Type ) : ComplexType {
 		return switch( t ) {
 		case TVoid: macro : Void;
@@ -151,6 +151,7 @@ class Macros {
 					pos : pos,
 					kind : FProp("get","set", t),
 					access : [APublic],
+					doc: v.getDoc(),
 				};
 				var name = v.name + "__";
 				var initVal = null;
@@ -477,5 +478,5 @@ class Macros {
 		});
 		return fields;
 	}
-
+	#end
 }

+ 1 - 0
hxsl/Printer.hx

@@ -61,6 +61,7 @@ class Printer {
 				case Range(min, max): "range(" + min + "," + max + ")";
 				case Ignore: "ignore";
 				case PerInstance(n): "perInstance("+n+")";
+				case Doc(s): "doc(\"" + StringTools.replace(s, '"', '\\"') + "\")";
 				}) + " ");
 		}
 		if( v.kind != defKind )

+ 2 - 0
hxsl/Serializer.hx

@@ -184,6 +184,7 @@ class Serializer {
 				case Precision(p): out.addByte(p.getIndex());
 				case Range(min, max): out.addDouble(min); out.addDouble(max);
 				case PerInstance(v): out.addInt32(v);
+				case Doc(s): writeString(s);
 				}
 			}
 		}
@@ -397,6 +398,7 @@ class Serializer {
 				case 7: Range(input.readDouble(), input.readDouble());
 				case 8: Ignore;
 				case 9: PerInstance(input.readInt32());
+				case 10: Doc(readString());
 				default: throw "assert";
 				}
 				v.qualifiers.push(q);