Ejemplo n.º 1
0
        private void InitializeVaryingsWithDefaultValues(ShaderGenerationContext context)
        {
            var vertexInput   = context.KeywordMap.GetKeyword <KeywordVertexInput>();
            var fragmentInput = context.KeywordMap.GetKeyword <KeywordFragmentInput>();

            bool hasDefinedFragmentShader = !IsDefault;

            var attributesToInit = new List <AttributeConfig>();

            var vertexAttributes = vertexInput.GetAttributes();

            foreach (var fragmentAttribute in fragmentInput.GetAttributes())
            {
                if (fragmentAttribute.AttributeType == AttributeType.Anonymous || fragmentAttribute.AttributeType == AttributeType.Position)
                {
                    continue;
                }

                if (vertexAttributes.Any(vertexAttribute => fragmentAttribute == vertexAttribute))
                {
                    attributesToInit.Add(fragmentAttribute);
                }
            }

            foreach (AttributeConfig config in attributesToInit)
            {
                context.WriteLineIndented($"output.__VARYINGS{config.AttributeType.ToString().ToUpper()} = input.__ATTRIBUTES{config.AttributeType.ToString().ToUpper()};");
            }
        }
Ejemplo n.º 2
0
        public virtual void Write(ShaderGenerationContext context)
        {
            AddPassRequiredAttributes(GetRequiredPassAttributes(context));
            AddPassKeywords(GetRequiredPassKeywords(context));

            context.LogShaderSection($"Generated Data Struct {AttributeStructName}");

            context.WriteLine($"struct {AttributeStructName}");
            context.WriteLine("{");

            context.WriteIndented((c) =>
            {
                for (int i = 0; i < _attributes.Count; i++)
                {
                    AttributeConfig config = _attributes[i];
                    if (config.AttributeType != AttributeType.Anonymous)
                    {
                        c.WriteLine($"#define __{AttributeStructName.ToUpper()}{config.AttributeType.ToString().ToUpper()}  {config.Name}");
                    }

                    config.WriteAttributeDefinition(c, i, this is KeywordFragmentInput);
                }
            });

            foreach (string keyword in _keywords)
            {
                context.WriteLine(keyword);
            }

            context.WriteLine("};");
            context.Newine();
        }
Ejemplo n.º 3
0
 public override void Write(ShaderGenerationContext context)
 {
     base.Write(context);
     if (GetAttribute(AttributeType.Position).UserDefined)
     {
         throw new KeywordMap.ShaderGenerationException("You are not supposed to manipulate the clip space position manually. Use SETPOSITIONOS(float3) or SETPOSITIONWS(float3) instead to ensure compatibility with all templates");
     }
 }
Ejemplo n.º 4
0
 public void Write(ShaderGenerationContext context)
 {
     if (!IsDefault)
     {
         context.WriteLine("HLSLINCLUDE");
         context.WriteLine("#define _NormalMap");
         context.WriteLine("ENDHLSL");
     }
 }
Ejemplo n.º 5
0
        internal void GeneratePasses(ShaderGenerationContext context)
        {
            context.WriteLine("SubShader{");

            var passes = GetShaderModel();

            context.WriteIndented(passes.Write);

            context.WriteLine("}");
        }
Ejemplo n.º 6
0
        public override void Write(ShaderGenerationContext context)
        {
            context.WriteLine("#define UNITY_SHADER_NO_UPGRADE 1");

            context.LogShaderSection("Vertex Shader");

            context.WriteIndented(WriteInnerVertexShader);

            context.WriteLine("}");
        }
Ejemplo n.º 7
0
 public override void GeneratePasses(ShaderGenerationContext context, out ShaderPass mainPass, out List <ShaderPass> additionalPasses)
 {
     mainPass         = new ShaderPassLitForward();
     additionalPasses = new List <ShaderPass>()
     {
         new ShaderPassShadow(),
         new ShaderPassDepthOnly(),
         new ShaderPassLitMeta(),
     };
 }
Ejemplo n.º 8
0
 public virtual void Write(ShaderGenerationContext context)
 {
     if (IsDefault || !context.CurrentPass.IsMainPass)
     {
         return;
     }
     context.LogShaderSection("FinalColor Code Block");
     context.WriteLine("float4 finalColor = __color;");
     context.WriteLine(_parsedCode);
     context.WriteLine("__color = finalColor;");
 }
Ejemplo n.º 9
0
 public void Write(ShaderGenerationContext context)
 {
     context.WriteLine($"#define __SAMPLETEXTURE{Name.ToUpper()}(uv) ");
     if (KeywordProperty.HasProperty(context, PropertyType.Texture, Name))
     {
         context.Write($"tex2D({Name}, uv)");
         context.WriteLine($"#define __HASETEXTURE{Name.ToUpper()} True");
     }
     else
     {
         context.Write($" float4({DefaultValue.x}, {DefaultValue.y}, {DefaultValue.z}, {DefaultValue.w})");
     }
 }
Ejemplo n.º 10
0
        public static bool HasProperty(ShaderGenerationContext context, PropertyType propertyType, string name)
        {
            var allProperties = context.KeywordMap.GetMultiKeywords <KeywordProperty>();

            foreach (KeywordProperty property in allProperties)
            {
                if (property._propertyName == name)
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 11
0
        public override void Write(ShaderGenerationContext context)
        {
            if (IsDefault)
            {
                AddFragmentShaderRequiredAttributes(context);
            }

            if (_attributes.Any(pair => pair.AttributeType == AttributeType.Anonymous))
            {
                throw new KeywordMap.ShaderGenerationException("Can't Vertex Attribute needs an explicit AttributeType");
            }

            base.Write(context);
        }
Ejemplo n.º 12
0
        private void AddFragmentShaderRequiredAttributes(ShaderGenerationContext context)
        {
            var fragmentAttributes = context.KeywordMap.GetKeyword <KeywordFragmentInput>().GetAttributes();

            foreach (AttributeConfig fragmentAttribute in fragmentAttributes)
            {
                if (fragmentAttribute.AttributeType == AttributeType.Anonymous || fragmentAttribute.AttributeType == AttributeType.Position)
                {
                    continue;
                }

                _attributes.Add(fragmentAttribute);
            }
        }
Ejemplo n.º 13
0
        private void WriteInnerVertexShader(ShaderGenerationContext context)
        {
            context.WriteLine("Varyings vert(Attributes input){");
            context.WriteLine("	Varyings output = (Varyings)0;");

            context.WriteLine("VertexPositionInputs __vertexPositionInputs = GetVertexPositionInputs(input.__ATTRIBUTESPOSITION);");

            InitializeVaryingsWithDefaultValues(context);

            context.WriteLine(context.CurrentPass.GetVertexShaderHeader());

            base.Write(context);

            context.WriteLine(context.CurrentPass.GetVertexShaderFooter());

            context.WriteLine("output.__VARYINGSPOSITION = __vertexPositionInputs.positionCS;");

            context.WriteLine("	return output;");
        }
Ejemplo n.º 14
0
        public override void Write(ShaderGenerationContext context)
        {
            context.LogShaderSection("Fragment Shader");

            //todo it migh be more performant to only output a float3 for opaque shaders
            context.WriteLine($"{context.CurrentPass.FragmentReturnFormat} frag(Varyings input) : COLOR{{");

            context.WriteIndented(WriteFragmentShaderHeader);

            base.Write(context);

            context.WriteIndented(WriteFragmentShaderFooter);

            context.KeywordMap.GetKeyword <KeywordModifyFinalColor>().Write(context);

            context.WriteLineIndented("return __color;");

            context.WriteLine("}");
        }
Ejemplo n.º 15
0
        public void Write(ShaderGenerationContext context)
        {
            context.KeywordMap.GetKeyword <KeywordQueue>().Write(context);
            //todo workaround for now
            context.KeywordMap.GetKeyword <KeywordHasNormalMap>().Write(context);

            context.WriteLine("HLSLINCLUDE");

            context.WriteLine("#include \"Packages/com.henningboat.thshader/ShaderLibrary/Common.cginc\"");

            foreach (ShaderModelTexture modelTexture in OptionalShaderModelTextures)
            {
                modelTexture.Write(context);
            }

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

            context.WriteLine(ShaderPass.ReadSourceFile(context, SubShaderHeader));

            context.WriteLine("ENDHLSL");

            GeneratePasses(context, out ShaderPass mainPass, out List <ShaderPass> additionalPasses);

            if (context.SourceMap.CustomPasses.Count > 0)
            {
                foreach (SourceMap.ShaderPassSource customPass in context.SourceMap.CustomPasses)
                {
                    context.WriteIndented(buildContext => mainPass.WritePass(buildContext, this, customPass));
                }
            }
            else
            {
                context.WriteIndented(buildContext => mainPass.WritePass(buildContext, this, null));
            }

            foreach (ShaderPass pass in additionalPasses)
            {
                context.WriteIndented(buildContext => pass.WritePass(buildContext, this, null));
            }
        }
Ejemplo n.º 16
0
        public void WriteAttributeDefinition(ShaderGenerationContext context, int attributeIndex, bool isFragmentShader)
        {
            context.WriteLine($"{DataTypeAndDimensionsString} {Name} : ");
            if (AttributeType == AttributeType.Anonymous)
            {
                context.Write($"COLOR1{attributeIndex}");
            }
            else
            {
                if (isFragmentShader && AttributeType == AttributeType.Position)
                {
                    context.Write("SV_POSITION");
                }
                else
                {
                    context.Write(AttributeType.ToString().ToUpper());
                }
            }

            context.Write(";");
        }
Ejemplo n.º 17
0
 public override void GeneratePasses(ShaderGenerationContext context, out ShaderPass mainPass, out List <ShaderPass> additionalPasses)
 {
     mainPass         = new ShaderUnlitPass();
     additionalPasses = new List <ShaderPass>();
 }
Ejemplo n.º 18
0
 private void WriteFragmentShaderHeader(ShaderGenerationContext context)
 {
     context.WriteLine(context.CurrentPass.GetFragmentShaderHeader());
     context.Newine();
 }
Ejemplo n.º 19
0
 internal override void WritePass(ShaderGenerationContext context, ShaderModel config, SourceMap.ShaderPassSource passSpecificCode)
 {
     context.WriteLine($"UsePass \"{_passName}\"");
 }
Ejemplo n.º 20
0
 public virtual void Write(ShaderGenerationContext context)
 {
     context.LogShaderSection("User Written Code");
     context.WriteLine(_parsedCode);
 }
Ejemplo n.º 21
0
 public void Write(ShaderGenerationContext context)
 {
     context.WriteLine($"Tags{{\"Queue\" = \"{FirstLineArguments}\"}}");
 }
Ejemplo n.º 22
0
 internal override void Write(ShaderGenerationContext context)
 {
     context.WriteLine($"{Name} True");
 }
Ejemplo n.º 23
0
 public void Write(ShaderGenerationContext context)
 {
     context.WriteLine($"#define {FirstLineArguments}");
 }
Ejemplo n.º 24
0
 protected override List <string> GetRequiredPassKeywords(ShaderGenerationContext context)
 {
     return(context.CurrentPass.RequiredFragmentKeywords);
 }
Ejemplo n.º 25
0
 public abstract void GeneratePasses(ShaderGenerationContext context, out ShaderPass mainPass, out List <ShaderPass> additionalPasses);
Ejemplo n.º 26
0
 protected abstract List <string> GetRequiredPassKeywords(ShaderGenerationContext context);
Ejemplo n.º 27
0
        public void Write(ShaderGenerationContext context, bool propertyDefinition)
        {
            if (IsDefault)
            {
                return;
            }

            if (propertyDefinition)
            {
                switch (_propertyType)
                {
                case PropertyType.Texture:
                    context.WriteLine($"{_propertyName} (\"{_propertyName}\", 2D) = \"\"{{}}");
                    break;

                case PropertyType.Float:
                    context.WriteLine($"{_propertyName} (\"{_propertyName}\", Float) = {_defaultValue}");
                    break;

                case PropertyType.Float2:
                case PropertyType.Float3:
                case PropertyType.Float4:
                    context.WriteLine($"{_propertyName} (\"{_propertyName}\", Vector) = {_defaultValue}");
                    break;

                case PropertyType.Color:
                    context.WriteLine($"{_propertyName} (\"{_propertyName}\", Color) = {_defaultValue}");
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                switch (_propertyType)
                {
                case PropertyType.Texture:
                    context.WriteLine($"sampler2D {_propertyName};");
                    break;

                case PropertyType.Float:
                    context.WriteLine($"float {_propertyName};");
                    break;

                case PropertyType.Float2:
                    context.WriteLine($"float2 {_propertyName};");
                    break;

                case PropertyType.Float3:
                    context.WriteLine($"float3 {_propertyName};");
                    break;

                case PropertyType.Float4:
                case PropertyType.Color:
                    context.WriteLine($"float4 {_propertyName};");
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Ejemplo n.º 28
0
 protected override List <AttributeConfig> GetRequiredPassAttributes(ShaderGenerationContext context)
 {
     return(context.CurrentPass.RequiredFragmentAttributes);
 }
Ejemplo n.º 29
0
 protected abstract List <AttributeConfig> GetRequiredPassAttributes(ShaderGenerationContext context);