public static string LoadShaderCallsite(ShaderProgram shader, string code, List <IAsset> includes, EnumShaderType type) { var ext = ".unknown"; switch (type) { case EnumShaderType.FragmentShader: ext = ".fsh"; break; case EnumShaderType.VertexShader: ext = ".vsh"; break; case EnumShaderType.GeometryShader: ext = ".gsh"; break; } var filename = shader.PassName + ext; code = VolumetricShadingMod.Instance.ShaderPatcher.Patch(filename, code); return(HandleIncludes(shader, code, includes)); }
public void OnShaderLoaded(ShaderProgram program, EnumShaderType shaderType) { Shader shader = null; if (shaderType == EnumShaderType.FragmentShader) { shader = program.FragmentShader; } else if (shaderType == EnumShaderType.VertexShader) { shader = program.VertexShader; } else if (shaderType == EnumShaderType.GeometryShader) { shader = program.GeometryShader; } if (shader == null) { return; } var prefixBuilder = new StringBuilder(); foreach (var property in ShaderProperties) { prefixBuilder.Append(property.GenerateOutput()); } shader.PrefixCode += prefixBuilder.ToString(); }
public static void LoadShaderPostfix(ShaderProgram program, EnumShaderType shaderType) { VolumetricShadingMod.Instance.ShaderInjector.OnShaderLoaded(program, shaderType); }
public void PatchShader(EnumShaderType shaderType, ShaderPatchFile patch, IShader shader) { Dictionary <string, ShaderPatch> shaderpatches; switch (shaderType) { case EnumShaderType.FragmentShader: shaderpatches = patch.Fragment; break; case EnumShaderType.VertexShader: shaderpatches = patch.Vertex; break; default: shaderpatches = patch.Fragment; break; } foreach (var shaderpatchfile in shaderpatches) { string code = shader.Code; int functionIndex = code.IndexOf(shaderpatchfile.Key + "("); string preCode = null; string postCode = null; if (shaderpatchfile.Value.Pre != null) { foreach (var cmd in shaderpatchfile.Value.Pre) { preCode += '\t' + cmd + "\r\n"; } } if (shaderpatchfile.Value.Post != null) { foreach (var cmd in shaderpatchfile.Value.Post) { postCode += '\t' + cmd + "\r\n"; } } preCode = preCode ?? ""; postCode = postCode ?? ""; if (shaderpatchfile.Value.Replace) { while (functionIndex < code.Length && code[functionIndex] != '{') { functionIndex++; } functionIndex++; int functionLength = 0; while (functionIndex + functionLength < code.Length && code[functionIndex + functionLength] != '}') { //one deep for now if (code[functionIndex + functionLength] == '/' && code[functionIndex + functionLength + 1] == '/') { while (functionIndex + functionLength < code.Length && code[functionIndex + functionLength] != '\n') { functionLength++; } } if (code[functionIndex + functionLength] == '/' && code[functionIndex + functionLength + 1] == '*') { while (functionIndex + functionLength < code.Length && code[functionIndex + functionLength] != '*' && code[functionIndex + functionLength] != '/') { functionLength++; } } if (code[functionIndex + functionLength] == '{') { while (functionIndex + functionLength < code.Length && code[functionIndex + functionLength] != '}') { functionLength++; } } functionLength++; } string func = code.Substring(functionIndex, functionLength); shader.Code = shader.Code.Replace(func, preCode + postCode); } else { while (functionIndex < code.Length && code[functionIndex] != '{') { functionIndex++; } shader.Code = code = code.Insert(functionIndex, preCode); while (functionIndex < code.Length && code[functionIndex] != '}') { //one deep for now if (code[functionIndex] == '/' && code[functionIndex + 1] == '/') { while (functionIndex < code.Length && code[functionIndex] != '\n') { functionIndex++; } } if (code[functionIndex] == '/' && code[functionIndex + 1] == '*') { while (functionIndex < code.Length && code[functionIndex] != '*' && code[functionIndex] != '/') { functionIndex++; } } if (code[functionIndex] == '{') { while (functionIndex < code.Length && code[functionIndex] != '}') { functionIndex++; } } functionIndex++; } shader.Code = code.Insert(functionIndex, postCode); } } }