瀏覽代碼

Added optional pre-compiled code in RuntimeShaderData (#354)

Pascal Peridont 7 年之前
父節點
當前提交
8fdca0968e
共有 3 個文件被更改,包括 18 次插入11 次删除
  1. 8 5
      h3d/impl/DirectXDriver.hx
  2. 9 6
      h3d/impl/GlDriver.hx
  3. 1 0
      hxsl/RuntimeShader.hx

+ 8 - 5
h3d/impl/DirectXDriver.hx

@@ -520,21 +520,24 @@ class DirectXDriver extends h3d.impl.Driver {
 
 	function compileShader( shader : hxsl.RuntimeShader.RuntimeShaderData, compileOnly = false ) {
 		var h = new hxsl.HlslOut();
-		var source = h.run(shader.data);
-		var bytes = try dx.Driver.compileShader(source, "", "main", (shader.vertex?"vs_":"ps_")+shaderVersion, OptimizationLevel3) catch( err : String ) {
+		if( shader.code == null ){
+			shader.code = h.run(shader.data);
+			shader.data.funs = null;
+		}
+		var bytes = try dx.Driver.compileShader(shader.code, "", "main", (shader.vertex?"vs_":"ps_")+shaderVersion, OptimizationLevel3) catch( err : String ) {
 			err = ~/^\(([0-9]+),([0-9]+)-([0-9]+)\)/gm.map(err, function(r) {
 				var line = Std.parseInt(r.matched(1));
 				var char = Std.parseInt(r.matched(2));
 				var end = Std.parseInt(r.matched(3));
-				return "\n<< " + source.split("\n")[line - 1].substr(char-1,end - char + 1) +" >>";
+				return "\n<< " + shader.code.split("\n")[line - 1].substr(char-1,end - char + 1) +" >>";
 			});
-			throw "Shader compilation error " + err + "\n\nin\n\n" + source;
+			throw "Shader compilation error " + err + "\n\nin\n\n" + shader.code;
 		}
 		if( compileOnly )
 			return { s : null, bytes : bytes };
 		var s = shader.vertex ? Driver.createVertexShader(bytes) : Driver.createPixelShader(bytes);
 		if( s == null )
-			throw "Failed to create shader\n" + source;
+			throw "Failed to create shader\n" + shader.code;
 
 		var ctx = new ShaderContext(s);
 		ctx.globalsSize = shader.globalsSize;

+ 9 - 6
h3d/impl/GlDriver.hx

@@ -223,16 +223,19 @@ class GlDriver extends Driver {
 	function compileShader( glout : ShaderCompiler, shader : hxsl.RuntimeShader.RuntimeShaderData ) {
 		var type = shader.vertex ? GL.VERTEX_SHADER : GL.FRAGMENT_SHADER;
 		var s = gl.createShader(type);
-		var code = glout.run(shader.data);
-		gl.shaderSource(s, code);
+		if( shader.code == null ){
+			shader.code = glout.run(shader.data);
+			shader.data.funs = null;
+		}
+		gl.shaderSource(s, shader.code);
 		gl.compileShader(s);
 		var log = gl.getShaderInfoLog(s);
 		if ( gl.getShaderParameter(s, GL.COMPILE_STATUS) != cast 1 ) {
 			var log = gl.getShaderInfoLog(s);
 			var lid = Std.parseInt(log.substr(9));
-			var line = lid == null ? null : code.split("\n")[lid - 1];
+			var line = lid == null ? null : shader.code.split("\n")[lid - 1];
 			if( line == null ) line = "" else line = "(" + StringTools.trim(line) + ")";
-			var codeLines = code.split("\n");
+			var codeLines = shader.code.split("\n");
 			for( i in 0...codeLines.length )
 				codeLines[i] = (i+1) + "\t" + codeLines[i];
 			throw "An error occurred compiling the shaders: " + log + line+"\n\n"+codeLines.join("\n");
@@ -277,7 +280,7 @@ class GlDriver extends Driver {
 				for( v in shader.fragment.data.vars )
 					switch( v.kind ) {
 					case Output:
-						gl.bindFragDataLocation(p.p, outCount++, glout.varNames.get(v.id));
+						gl.bindFragDataLocation(p.p, outCount++, glout.varNames.exists(v.id) ? glout.varNames.get(v.id) : v.name);
 					default:
 					}
 			}
@@ -325,7 +328,7 @@ class GlDriver extends Driver {
 					case TFloat: 1;
 					default: throw "assert " + v.type;
 					}
-					var index = gl.getAttribLocation(p.p, glout.varNames.get(v.id));
+					var index = gl.getAttribLocation(p.p, glout.varNames.exists(v.id) ? glout.varNames.get(v.id) : v.name);
 					if( index < 0 ) {
 						p.stride += size;
 						continue;

+ 1 - 0
hxsl/RuntimeShader.hx

@@ -34,6 +34,7 @@ class AllocGlobal {
 class RuntimeShaderData {
 	public var vertex : Bool;
 	public var data : Ast.ShaderData;
+	public var code : String;
 	public var params : AllocParam;
 	public var paramsSize : Int;
 	public var globals : AllocGlobal;