コード例 #1
0
        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");
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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("}");
        }
コード例 #4
0
        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);
                }
            }
        }
コード例 #5
0
        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");
        }
コード例 #6
0
 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,
     });
 }
コード例 #7
0
        private void AppendProperties(ShaderGenerationContext context)
        {
            context.WriteLine($"Properties");
            context.WriteLine($"{{");

            context.KeywordMap.GetMultiKeywords <KeywordProperty>().ForEach(keyword => keyword.Write(context, true));

            context.WriteLine($"");
            context.WriteLine($"}}");
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
 internal abstract void Write(ShaderGenerationContext context);
コード例 #10
0
 private void GenerateOuter(ShaderGenerationContext context, Action <ShaderGenerationContext> action)
 {
     context.WriteLine($"Shader \"{context.KeywordMap.GetKeyword<KeywordName>().ShaderName}\" {{");
     context.WriteIndented(action);
     context.WriteLine($"}}");
 }
コード例 #11
0
 internal override void Write(ShaderGenerationContext context)
 {
     context.WriteLine($"{Name} {FirstLineArguments}");
 }
コード例 #12
0
 protected virtual void OnBeginWritingPassCode(ShaderGenerationContext context)
 {
 }