internal ShaderGenerator(List <string> source) { TrimSource(source); ShaderGenerationContext context = null; try { RemoveComments(source); SourceMap sourceMap = new SourceMap(source); context = new ShaderGenerationContext(sourceMap, new StringBuilder()); GenerateOuter(context, (s) => { AppendProperties(s); context.KeywordMap.GetKeyword <KeywordShaderModel>().GeneratePasses(s); }); GeneratedShader += context.BuildString(); } catch (Exception e) { Debug.LogWarning(e); if (context != null) { GeneratedShader += context.BuildString(); } GeneratedShader += "\n #error"; GeneratedShader += e; Debug.LogWarning("Failed to import thshader"); } }
public void Generate(ShaderGenerationContext context) { List <PipelineStateKeyword> keywords = context.KeywordMap.GetKeywords <PipelineStateKeyword>(); string lightMode = context.CurrentPass.LightMode; KeywordLightMode keywordLightMode = context.KeywordMap.GetKeyword <KeywordLightMode>(); if (context.CurrentPass.IsMainPass && keywordLightMode.OverwriteLightMode != null) { lightMode = keywordLightMode.OverwriteLightMode; } if (context.CurrentPass != null && lightMode != null) { context.WriteLine($"Tags{{\"LightMode\" = \"{lightMode}\"}}"); } foreach (PipelineStateKeyword keyword in keywords) { keyword.Write(context); } if (context.CurrentPass.CustomStencilDefinition != null) { context.WriteLine(context.CurrentPass.CustomStencilDefinition); } }
internal virtual void WritePass(ShaderGenerationContext context, ShaderModel config, SourceMap.ShaderPassSource passSpecificCode) { var mode = GetShadowDepthMode(context); if (UsePassName != null) { if (mode == KeywordShadowDepthPass.ShadowDepthPassMode.DefaultPass) { new UsePass(UsePassName).WritePass(context, config, null); return; } if (mode == KeywordShadowDepthPass.ShadowDepthPassMode.Off) { return; } } context.LogShaderSection($"Shader Pass {GetType().Name}"); context.WriteLine("Pass{"); var shaderPassContext = context.CreatePassContext(context, this, config, passSpecificCode); shaderPassContext.WriteIndented(WriteInnerPass); context.WriteLine("}"); }
public static string ReadSourceFile(ShaderGenerationContext context, string path) { if (path == null) { return(""); } if (!context.KeywordMap.GetKeyword <KeywordDebugMode>().IsDebug) { return($"#include \"{path}\""); } else { string combine = Path.GetFullPath(path); try { context.LogShaderSection($"Imported include file: {Path.GetFileName(path)}"); return(File.ReadAllText(combine)); } catch (Exception e) { throw new KeywordMap.ShaderGenerationException("failed reading source file " + combine); } } }
private void WriteInnerPass(ShaderGenerationContext context) { _context = context; var pipelineState = new PipelineState(); pipelineState.Generate(context); context.WriteLine("HLSLPROGRAM"); OnBeginWritingPassCode(context); context.WriteLine("#pragma vertex vert"); context.WriteLine("#pragma fragment frag"); context.WriteLine($"#define {GetType().Name}"); context.KeywordMap.GetKeyword <KeywordVertexInput>().Write(context); context.KeywordMap.GetKeyword <KeywordFragmentInput>().Write(context); context.KeywordMap.GetMultiKeywords <KeywordCodeBlock>().ForEach(block => block.Write(context)); context.WriteLine(GetShaderHeader()); context.KeywordMap.GetKeyword <KeywordVertexShader>().Write(context); context.KeywordMap.GetKeyword <KeywordFragmentShader>().Write(context); context.WriteLine("ENDHLSL"); }
public ShaderGenerationContext CreatePassContext(ShaderGenerationContext parent, ShaderPass pass, ShaderModel config, SourceMap.ShaderPassSource passSourceSource) { return(new ShaderGenerationContext(parent.SourceMap, parent._stringBuilder, passSourceSource) { _indentCount = parent._indentCount, CurrentPass = pass, CurrentShaderModel = config, }); }
private void AppendProperties(ShaderGenerationContext context) { context.WriteLine($"Properties"); context.WriteLine($"{{"); context.KeywordMap.GetMultiKeywords <KeywordProperty>().ForEach(keyword => keyword.Write(context, true)); context.WriteLine($""); context.WriteLine($"}}"); }
private KeywordShadowDepthPass.ShadowDepthPassMode GetShadowDepthMode(ShaderGenerationContext context) { KeywordShadowDepthPass shadowDepthPass = context.KeywordMap.GetKeyword <KeywordShadowDepthPass>(); if (shadowDepthPass.Mode == KeywordShadowDepthPass.ShadowDepthPassMode.DefaultPass) { return(context.KeywordMap.GetKeyword <KeywordVertexShader>().ModifiesVertexPosition ? KeywordShadowDepthPass.ShadowDepthPassMode.On : KeywordShadowDepthPass.ShadowDepthPassMode.DefaultPass); } else { return(shadowDepthPass.Mode); } }
internal abstract void Write(ShaderGenerationContext context);
private void GenerateOuter(ShaderGenerationContext context, Action <ShaderGenerationContext> action) { context.WriteLine($"Shader \"{context.KeywordMap.GetKeyword<KeywordName>().ShaderName}\" {{"); context.WriteIndented(action); context.WriteLine($"}}"); }
internal override void Write(ShaderGenerationContext context) { context.WriteLine($"{Name} {FirstLineArguments}"); }
protected virtual void OnBeginWritingPassCode(ShaderGenerationContext context) { }