Ver código fonte

added getParamFloatValue to prevent allocation for float parameters

Nicolas Cannasse 8 anos atrás
pai
commit
79600e4c24
3 arquivos alterados com 43 adições e 5 exclusões
  1. 16 1
      h3d/shader/Manager.hx
  2. 22 4
      hxsl/Macros.hx
  3. 5 0
      hxsl/Shader.hx

+ 16 - 1
h3d/shader/Manager.hx

@@ -112,6 +112,13 @@ class Manager {
 				out[pos++] = m._43;
 			}
 			return len * 12;
+		case TArray(TFloat, SConst(len)):
+			var v : Array<Float> = v;
+			var size = 0;
+			var count = v.length < len ? v.length : len;
+			for( i in 0...count )
+				out[pos++] = v[i];
+			return len;
 		case TArray(t, SConst(len)):
 			var v : Array<Dynamic> = v;
 			var size = 0;
@@ -172,7 +179,15 @@ class Manager {
 		inline function fill(buf:Buffers.ShaderBuffers, s:hxsl.RuntimeShader.RuntimeShaderData) {
 			var p = s.params;
 			while( p != null ) {
-				var v = getParamValue(p, shaders);
+				if( p.type == TFloat && p.perObjectGlobal == null ) {
+					var si = shaders;
+					var n = p.instance;
+					while( n-- > 0 ) si = si.next;
+					buf.params[p.pos] = si.s.getParamFloatValue(p.index);
+					p = p.next;
+					continue;
+				}
+				var v : Dynamic = getParamValue(p, shaders);
 				fillRec(v, p.type, buf.params, p.pos);
 				p = p.next;
 			}

+ 22 - 4
hxsl/Macros.hx

@@ -83,19 +83,20 @@ class Macros {
 		}
 	}
 
-	static function addParamRec( eparams : Array<haxe.macro.Expr>, e : haxe.macro.Expr, t : Type ) {
+	static function addParamRec( eparams : Array<haxe.macro.Expr>, tparams : Array<Type>, e : haxe.macro.Expr, t : Type ) {
 		switch( t ) {
 		case TStruct(vl):
 			for( v in vl )
-				addParamRec(eparams, { expr : EField(e, v.name), pos : e.pos }, v.type);
+				addParamRec(eparams, tparams, { expr : EField(e, v.name), pos : e.pos }, v.type);
 		default:
 			eparams.push(e);
+			tparams.push(t);
 		}
 	}
 
 	static function buildFields( shader : ShaderData, pos : Position ) {
 		var fields = new Array<Field>();
-		var globals = [], consts = [], params = [], eparams = [];
+		var globals = [], consts = [], params = [], eparams = [], tparams = [];
 		for( v in shader.vars ) {
 			var cpos = consts.length;
 			getConsts(v, pos, consts);
@@ -152,7 +153,7 @@ class Macros {
 				fields.push(f);
 				fields.push(f2);
 				params.push(name);
-				addParamRec(eparams, { expr : EConst(CIdent(name)), pos:pos }, v.type);
+				addParamRec(eparams, tparams, { expr : EConst(CIdent(name)), pos:pos }, v.type);
 				fields.push(fget);
 				fields.push(fset);
 			case Global:
@@ -214,6 +215,22 @@ class Macros {
 			}),
 			access : [AOverride],
 		});
+		fields.push( {
+			name : "getParamFloatValue",
+			pos : pos,
+			kind : FFun( {
+				ret : macro : Float,
+				args : [ { name : "index", type : macro : Int } ],
+				expr : {
+					expr : EBlock([
+						{ expr : ESwitch(macro index, [for( i in 0...tparams.length ) if( tparams[i] == TFloat ) { values : [macro $v{i}], expr : macro return ${eparams[i]} }], macro {}), pos : pos },
+						macro return 0.,
+					]),
+					pos : pos,
+				},
+			}),
+			access : [AOverride],
+		});
 		if( params.length > 0 ) {
 			var cexpr = [];
 			var type = Context.getLocalClass().toString().split(".").pop();
@@ -287,6 +304,7 @@ class Macros {
 							shader = { expr : EBlock([ { expr : ECall( { expr : EIdent("extends"), pos : pos }, [ { expr : EConst(CString(sup)), pos : pos } ]), pos : pos }, shader]), pos : pos };
 							supFields.remove("updateConstants");
 							supFields.remove("getParamValue");
+							supFields.remove("getParamFloatValue");
 							supFields.remove("clone");
 							csup = tsup.superClass;
 						} while( true);

+ 5 - 0
hxsl/Shader.hx

@@ -33,6 +33,11 @@ class Shader {
 		return null;
 	}
 
+	public function getParamFloatValue( index : Int ) : Float {
+		throw "assert"; // will be subclassed in sub shaders
+		return 0.;
+	}
+
 	public function updateConstants( globals : Globals ) {
 		throw "assert";
 	}