Ejemplo n.º 1
0
 public override void ClearColor(IShaderNodeView nodeView)
 {
     foreach (var type in ConcretePrecision.GetValues(typeof(ConcretePrecision)))
     {
         nodeView.colorElement.RemoveFromClassList(type.ToString());
     }
 }
Ejemplo n.º 2
0
        internal static string ToShaderString(this ConcretePrecision precision)
        {
            switch (precision)
            {
            case ConcretePrecision.Float:
                return("float");

            case ConcretePrecision.Half:
                return("half");

            default:
                return("float");
            }
        }
Ejemplo n.º 3
0
        internal static ConcretePrecision ToConcrete(this GraphPrecision precision, ConcretePrecision graphPrecision)
        {
            switch (precision)
            {
            case GraphPrecision.Single:
                return(ConcretePrecision.Single);

            case GraphPrecision.Half:
                return(ConcretePrecision.Half);

            default:
                return(graphPrecision);
            }
        }
Ejemplo n.º 4
0
        internal static ConcretePrecision ToConcrete(this Precision precision, ConcretePrecision InheritPrecision, ConcretePrecision GraphPrecision)
        {
            switch (precision)
            {
            case Precision.Single:
                return(ConcretePrecision.Single);

            case Precision.Half:
                return(ConcretePrecision.Half);

            case Precision.Inherit:
                return(InheritPrecision);

            default:
                return(GraphPrecision);
            }
        }
Ejemplo n.º 5
0
        public virtual void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            if (asset == null || hasError)
            {
                return;
            }

            registry.RequiresIncludes(asset.includes);

            var graphData = registry.builder.currentNode.owner;
            var graphDefaultConcretePrecision = graphData.graphDefaultConcretePrecision;

            foreach (var function in asset.functions)
            {
                var name   = function.key;
                var source = function.value;
                var graphPrecisionFlags = function.graphPrecisionFlags;

                // the subgraph may use multiple precision variants of this function internally
                // here we iterate through all the requested precisions and forward those requests out to the graph
                for (int requestedGraphPrecision = 0; requestedGraphPrecision <= (int)GraphPrecision.Half; requestedGraphPrecision++)
                {
                    // only provide requested precisions
                    if ((graphPrecisionFlags & (1 << requestedGraphPrecision)) != 0)
                    {
                        // when a function coming from a subgraph asset has a graph precision of "Graph",
                        // that means it is up to the subgraph NODE to decide (i.e. us!)
                        GraphPrecision actualGraphPrecision = (GraphPrecision)requestedGraphPrecision;

                        // subgraph asset setting falls back to this node setting (when switchable)
                        actualGraphPrecision = actualGraphPrecision.GraphFallback(this.graphPrecision);

                        // which falls back to the graph default concrete precision
                        ConcretePrecision actualConcretePrecision = actualGraphPrecision.ToConcrete(graphDefaultConcretePrecision);

                        // forward the function into the current graph
                        registry.ProvideFunction(name, actualGraphPrecision, actualConcretePrecision, sb => sb.AppendLines(source));
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static string ToShaderString(this ConcreteSlotValueType type, ConcretePrecision concretePrecision)
        {
            string precisionString = concretePrecision.ToShaderString();

            return(type.ToShaderString(precisionString));
        }
Ejemplo n.º 7
0
        public string GetDefaultValue(GenerationMode generationMode, ConcretePrecision concretePrecision)
        {
            string defaultValue = GetDefaultValue(generationMode);

            return(defaultValue.Replace(PrecisionUtil.Token, concretePrecision.ToShaderString()));
        }
Ejemplo n.º 8
0
        public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision inheritedPrecision)
        {
            // Hybrid V1 generates a special version of UnityPerMaterial, which has dummy constants for
            // instanced properties, and regular constants for other properties.
            // Hybrid V2 generates a perfectly normal UnityPerMaterial, but needs to append
            // a UNITY_DOTS_INSTANCING_START/END block after it that contains the instanced properties.
            #if !ENABLE_HYBRID_RENDERER_V2
            foreach (var prop in properties)
            {
                prop.ValidateConcretePrecision(inheritedPrecision);
            }

            var batchAll = mode == GenerationMode.Preview;
            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");
            int instancedCount = 0;
            foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
            {
                if (!prop.gpuInstanced)
                {
                    builder.AppendLine(prop.GetPropertyDeclarationString());
                }
                else
                {
                    instancedCount++;
                }
            }

            if (instancedCount > 0)
            {
                builder.AppendLine("#ifdef UNITY_HYBRID_V1_INSTANCING_ENABLED");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        builder.AppendLine(prop.GetPropertyDeclarationString("_dummy;"));
                    }
                }
                builder.AppendLine("#else");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        builder.AppendLine(prop.GetPropertyDeclarationString());
                    }
                }
                builder.AppendLine("#endif");
            }
            builder.AppendLine("CBUFFER_END");

            if (batchAll)
            {
                return;
            }

            foreach (var prop in properties.Where(n => !n.isBatchable || !n.generatePropertyBlock))
            {
                builder.AppendLine(prop.GetPropertyDeclarationString());
            }
            #else
            foreach (var prop in properties)
            {
                prop.ValidateConcretePrecision(inheritedPrecision);
            }

            var batchAll = mode == GenerationMode.Preview;
            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");
            int instancedCount = 0;
            foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
            {
                if (!prop.gpuInstanced)
                {
                    builder.AppendLine(prop.GetPropertyDeclarationString());
                }
                else
                {
                    instancedCount++;
                }
            }

            if (instancedCount > 0)
            {
                builder.AppendLine("// Hybrid instanced properties");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        builder.AppendLine(prop.GetPropertyDeclarationString());
                    }
                }
            }
            builder.AppendLine("CBUFFER_END");

            if (instancedCount > 0)
            {
                builder.AppendLine("#if defined(UNITY_DOTS_INSTANCING_ENABLED)");

                builder.AppendLine("// DOTS instancing definitions");
                builder.AppendLine("UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        var    n    = prop.referenceName;
                        string type = prop.concreteShaderValueType.ToShaderString(prop.concretePrecision);
                        builder.AppendLine($"    UNITY_DOTS_INSTANCED_PROP({type}, {n})");
                    }
                }
                builder.AppendLine("UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)");

                builder.AppendLine("// DOTS instancing usage macros");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        var    n    = prop.referenceName;
                        string type = prop.concreteShaderValueType.ToShaderString(prop.concretePrecision);
                        builder.AppendLine($"#define {n} UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO({type}, Metadata_{n})");
                    }
                }

                builder.AppendLine("#endif");
            }

            if (batchAll)
            {
                return;
            }

            foreach (var prop in properties.Where(n => !n.isBatchable || !n.generatePropertyBlock))
            {
                builder.AppendLine(prop.GetPropertyDeclarationString());
            }
            #endif
        }
Ejemplo n.º 9
0
        public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision inheritedPrecision)
        {
            foreach (var prop in properties)
            {
                prop.ValidateConcretePrecision(inheritedPrecision);
            }

            // build a list of all HLSL properties
            var hlslProps = BuildHLSLPropertyList();

            if (mode == GenerationMode.Preview)
            {
                builder.AppendLine("CBUFFER_START(UnityPerMaterial)");

                // all non-gpu instanced properties (even non-batchable ones!)
                // this is because for preview we convert all properties to UnityPerMaterial properties
                // as we will be submitting the default preview values via the Material..  :)
                foreach (var h in hlslProps)
                {
                    if ((h.declaration == HLSLDeclaration.UnityPerMaterial) ||
                        (h.declaration == HLSLDeclaration.Global))
                    {
                        h.AppendTo(builder);
                    }
                }

                // gpu-instanced properties
                var gpuInstancedProps = hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance);
                if (gpuInstancedProps.Any())
                {
                    builder.AppendLine("#ifdef UNITY_HYBRID_V1_INSTANCING_ENABLED");
                    foreach (var h in gpuInstancedProps)
                    {
                        h.AppendTo(builder, name => name + "_dummy");
                    }
                    builder.AppendLine("#else // V2");
                    foreach (var h in gpuInstancedProps)
                    {
                        h.AppendTo(builder);
                    }
                    builder.AppendLine("#endif");
                }
                builder.AppendLine("CBUFFER_END");
                return;
            }

            // Hybrid V1 generates a special version of UnityPerMaterial, which has dummy constants for
            // instanced properties, and regular constants for other properties.
            // Hybrid V2 generates a perfectly normal UnityPerMaterial, but needs to append
            // a UNITY_DOTS_INSTANCING_START/END block after it that contains the instanced properties.

#if !ENABLE_HYBRID_RENDERER_V2
            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");

            // non-GPU-instanced batchable properties go first in the UnityPerMaterial cbuffer
            foreach (var h in hlslProps)
            {
                if (h.declaration == HLSLDeclaration.UnityPerMaterial)
                {
                    h.AppendTo(builder);
                }
            }

            // followed by GPU-instanced batchable properties
            var gpuInstancedProperties = hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance);
            if (gpuInstancedProperties.Any())
            {
                builder.AppendLine("#ifdef UNITY_HYBRID_V1_INSTANCING_ENABLED");
                foreach (var hlslProp in gpuInstancedProperties)
                {
                    hlslProp.AppendTo(builder, name => name + "_dummy");
                }
                builder.AppendLine("#else");
                foreach (var hlslProp in gpuInstancedProperties)
                {
                    hlslProp.AppendTo(builder);
                }
                builder.AppendLine("#endif");
            }
            builder.AppendLine("CBUFFER_END");
#else
            // TODO: need to test this path with HYBRID_RENDERER_V2 ...

            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");

            int instancedCount = 0;
            foreach (var h in hlslProps)
            {
                if (h.declaration == HLSLDeclaration.UnityPerMaterial)
                {
                    h.AppendTo(builder);
                }
                else if (h.declaration == HLSLDeclaration.HybridPerInstance)
                {
                    instancedCount++;
                }
            }

            if (instancedCount > 0)
            {
                builder.AppendLine("// Hybrid instanced properties");
                foreach (var h in hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance))
                {
                    h.AppendTo(builder);
                }
            }
            builder.AppendLine("CBUFFER_END");

            if (instancedCount > 0)
            {
                builder.AppendLine("#if defined(UNITY_DOTS_INSTANCING_ENABLED)");

                builder.AppendLine("// DOTS instancing definitions");
                builder.AppendLine("UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)");
                foreach (var h in hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance))
                {
                    var    n    = h.name;
                    string type = h.GetValueTypeString();
                    builder.AppendLine($"    UNITY_DOTS_INSTANCED_PROP({type}, {n})");
                }
                builder.AppendLine("UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)");

                builder.AppendLine("// DOTS instancing usage macros");
                foreach (var h in hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance))
                {
                    var    n    = h.name;
                    string type = h.GetValueTypeString();
                    builder.AppendLine($"#define {n} UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO({type}, Metadata_{n})");
                }
                builder.AppendLine("#endif");
            }
#endif

            builder.AppendNewLine();
            builder.AppendLine("// Object and Global properties");
            foreach (var h in hlslProps)
            {
                if (h.declaration == HLSLDeclaration.Global)
                {
                    h.AppendTo(builder);
                }
            }
        }
Ejemplo n.º 10
0
        public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision inheritedPrecision)
        {
            foreach (var prop in properties)
            {
                prop.ValidateConcretePrecision(inheritedPrecision);
            }

            var batchAll = mode == GenerationMode.Preview;

            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");
            int instancedCount = 0;

            foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
            {
                if (!prop.gpuInstanced)
                {
                    builder.AppendLine(prop.GetPropertyDeclarationString());
                }
                else
                {
                    instancedCount++;
                }
            }

            if (instancedCount > 0)
            {
                builder.AppendLine("#ifdef UNITY_DOTS_INSTANCING_ENABLED");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        builder.AppendLine(prop.GetPropertyDeclarationString("_dummy;"));
                    }
                }
                builder.AppendLine("#else");
                foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
                {
                    if (prop.gpuInstanced)
                    {
                        builder.AppendLine(prop.GetPropertyDeclarationString());
                    }
                }
                builder.AppendLine("#endif");
            }
            builder.AppendLine("CBUFFER_END");

            if (batchAll)
            {
                return;
            }

            foreach (var prop in properties.Where(n => !n.isBatchable || !n.generatePropertyBlock))
            {
                builder.AppendLine(prop.GetPropertyDeclarationString());
            }
        }
Ejemplo n.º 11
0
        public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision inheritedPrecision)
        {
            foreach (var prop in properties)
            {
                prop.ValidateConcretePrecision(inheritedPrecision);
            }

            if (mode == GenerationMode.Preview)
            {
                builder.AppendLine("CBUFFER_START(UnityPerMaterial)");
                foreach (var prop in properties.Where(p => !p.gpuInstanced))    // all non-gpu instanced properties (even non-batchable ones) - preview is weird
                {
                    prop.AppendBatchablePropertyDeclarations(builder);
                    prop.AppendNonBatchablePropertyDeclarations(builder);
                }
                var GPUInstancedProperties = properties.Where(p => p.gpuInstanced);
                if (GPUInstancedProperties.Any())
                {
                    builder.AppendLine("#ifdef UNITY_HYBRID_V1_INSTANCING_ENABLED");
                    foreach (var prop in GPUInstancedProperties)
                    {
                        prop.AppendBatchablePropertyDeclarations(builder, "_dummy;");
                    }
                    builder.AppendLine("#else");
                    foreach (var prop in GPUInstancedProperties)
                    {
                        prop.AppendBatchablePropertyDeclarations(builder);
                    }
                    builder.AppendLine("#endif");
                }
                builder.AppendLine("CBUFFER_END");
                return;
            }

            // Hybrid V1 generates a special version of UnityPerMaterial, which has dummy constants for
            // instanced properties, and regular constants for other properties.
            // Hybrid V2 generates a perfectly normal UnityPerMaterial, but needs to append
            // a UNITY_DOTS_INSTANCING_START/END block after it that contains the instanced properties.

#if !ENABLE_HYBRID_RENDERER_V2
            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");

            // non-GPU instanced properties go first in the UnityPerMaterial cbuffer
            var batchableProperties = properties.Where(n => n.generatePropertyBlock && n.hasBatchableProperties);
            foreach (var prop in batchableProperties)
            {
                if (!prop.gpuInstanced)
                {
                    prop.AppendBatchablePropertyDeclarations(builder);
                }
            }

            var batchableGPUInstancedProperties = batchableProperties.Where(p => p.gpuInstanced);
            if (batchableGPUInstancedProperties.Any())
            {
                builder.AppendLine("#ifdef UNITY_HYBRID_V1_INSTANCING_ENABLED");
                foreach (var prop in batchableGPUInstancedProperties)
                {
                    // TODO: why is this inserting a dummy value?  this won't work on complex properties...
                    prop.AppendBatchablePropertyDeclarations(builder, "_dummy;");
                }
                builder.AppendLine("#else");
                foreach (var prop in batchableGPUInstancedProperties)
                {
                    prop.AppendBatchablePropertyDeclarations(builder);
                }
                builder.AppendLine("#endif");
            }
            builder.AppendLine("CBUFFER_END");
#else
            // TODO: need to test this path with HYBRID_RENDERER_V2 ...

            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");
            int instancedCount = 0;
            foreach (var prop in properties.Where(n => n.generatePropertyBlock && n.hasBatchableProperties))
            {
                if (!prop.gpuInstanced)
                {
                    prop.AppendBatchablePropertyDeclarations(builder);
                }
                else
                {
                    instancedCount++;
                }
            }

            if (instancedCount > 0)
            {
                builder.AppendLine("// Hybrid instanced properties");
                foreach (var prop in properties.Where(n => n.generatePropertyBlock && n.hasBatchableProperties))
                {
                    if (prop.gpuInstanced)
                    {
                        prop.AppendBatchablePropertyDeclarations(builder);
                    }
                }
            }
            builder.AppendLine("CBUFFER_END");

            if (instancedCount > 0)
            {
                builder.AppendLine("#if defined(UNITY_DOTS_INSTANCING_ENABLED)");

                builder.AppendLine("// DOTS instancing definitions");
                builder.AppendLine("UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)");
                foreach (var prop in properties.Where(n => n.generatePropertyBlock && n.hasBatchableProperties))
                {
                    if (prop.gpuInstanced)
                    {
                        var    n    = prop.referenceName;
                        string type = prop.concreteShaderValueType.ToShaderString(prop.concretePrecision);
                        builder.AppendLine($"    UNITY_DOTS_INSTANCED_PROP({type}, {n})");
                    }
                }
                builder.AppendLine("UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)");

                builder.AppendLine("// DOTS instancing usage macros");
                foreach (var prop in properties.Where(n => n.generatePropertyBlock && n.hasBatchableProperties))
                {
                    if (prop.gpuInstanced)
                    {
                        var    n    = prop.referenceName;
                        string type = prop.concreteShaderValueType.ToShaderString(prop.concretePrecision);
                        builder.AppendLine($"#define {n} UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO({type}, Metadata_{n})");
                    }
                }
                builder.AppendLine("#endif");
            }
#endif

            // declare non-batchable properties
            foreach (var prop in properties.Where(n => n.hasNonBatchableProperties || !n.generatePropertyBlock))
            {
                if (prop.hasBatchableProperties && !prop.generatePropertyBlock) // batchable properties that don't generate property block can't be instanced, get put here
                {
                    prop.AppendBatchablePropertyDeclarations(builder);
                }

                prop.AppendNonBatchablePropertyDeclarations(builder);
            }
        }
Ejemplo n.º 12
0
        public void ProvideFunction(string name, GraphPrecision graphPrecision, ConcretePrecision concretePrecision, Action <ShaderStringBuilder> generator)
        {
            // appends code, construct the standalone code string
            var originalIndex = builder.length;

            builder.AppendNewLine();

            var startIndex = builder.length;

            generator(builder);
            var length = builder.length - startIndex;
            var code   = builder.ToString(startIndex, length);

            // validate some assumptions around generics
            bool isGenericName        = name.Contains("$");
            bool isGenericFunc        = code.Contains("$");
            bool isGeneric            = isGenericName || isGenericFunc;
            bool containsFunctionName = code.Contains(name);

            var curNode = builder.currentNode;

            if (isGenericName != isGenericFunc)
            {
                curNode.owner.AddValidationError(curNode.objectId, $"Function {name} provided by node {curNode.name} contains $precision tokens in the name or the code, but not both. This is very likely an error.");
            }

            if (!containsFunctionName)
            {
                curNode.owner.AddValidationError(curNode.objectId, $"Function {name} provided by node {curNode.name} does not contain the name of the function.  This is very likely an error.");
            }

            int graphPrecisionFlag    = (1 << (int)graphPrecision);
            int concretePrecisionFlag = (1 << (int)concretePrecision);

            FunctionSource existingSource;

            if (m_Sources.TryGetValue(name, out existingSource))
            {
                // function already provided
                existingSource.nodes.Add(builder.currentNode);

                // let's check if the requested precision variant has already been provided (or if it's not generic there are no variants)
                bool concretePrecisionExists = ((existingSource.concretePrecisionFlags & concretePrecisionFlag) != 0) || !isGeneric;

                // if this precision was already added -- remove the duplicate code from the builder
                if (concretePrecisionExists)
                {
                    builder.length -= (builder.length - originalIndex);
                }

                // save the flags
                existingSource.graphPrecisionFlags    = existingSource.graphPrecisionFlags | graphPrecisionFlag;
                existingSource.concretePrecisionFlags = existingSource.concretePrecisionFlags | concretePrecisionFlag;

                // if validate, we double check that the two function declarations are the same
                if (m_Validate)
                {
                    if (code != existingSource.code)
                    {
                        var errorMessage = string.Format("Function `{0}` has conflicting implementations:{1}{1}{2}{1}{1}{3}", name, Environment.NewLine, code, existingSource.code);
                        foreach (var n in existingSource.nodes)
                        {
                            n.owner.AddValidationError(n.objectId, errorMessage);
                        }
                    }
                }
            }
            else
            {
                var newSource = new FunctionSource
                {
                    code                   = code,
                    isGeneric              = isGeneric,
                    graphPrecisionFlags    = graphPrecisionFlag,
                    concretePrecisionFlags = concretePrecisionFlag,
                    nodes                  = new HashSet <AbstractMaterialNode> {
                        builder.currentNode
                    }
                };

                m_Sources.Add(name, newSource);
                names.Add(name);
            }

            // fully concretize any generic code by replacing any precision tokens by the node's concrete precision
            if (isGeneric && (builder.length > originalIndex))
            {
                int start = originalIndex;
                int count = builder.length - start;
                builder.Replace(PrecisionUtil.Token, concretePrecision.ToShaderString(), start, count);
            }
        }
        public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision inheritedPrecision)
        {
            foreach (var prop in properties)
            {
                prop.SetConcretePrecision(inheritedPrecision);
            }

            var batchAll = mode == GenerationMode.Preview;

            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");
            foreach (var prop in properties.Where(n => batchAll || (n.generatePropertyBlock && n.isBatchable)))
            {
                builder.AppendLine(prop.GetPropertyDeclarationString());
            }
            builder.AppendLine("CBUFFER_END");
            builder.AppendNewLine();

            if (batchAll)
            {
                return;
            }

            foreach (var prop in properties.Where(n => !n.isBatchable || !n.generatePropertyBlock))
            {
                builder.AppendLine(prop.GetPropertyDeclarationString());
            }
        }
Ejemplo n.º 14
0
 public TestCase(GraphPrecision graphPrecision, Precision parentPrecision0, Precision parentPrecision1, GraphPrecision resultGraph, ConcretePrecision resultConcrete)
 {
     this.graphPrecision   = graphPrecision;
     this.parentPrecision0 = parentPrecision0;
     this.parentPrecision1 = parentPrecision1;
     this.resultGraph      = resultGraph;
     this.resultConcrete   = resultConcrete;
 }
Ejemplo n.º 15
0
        public void GetPropertiesDeclaration(ShaderStringBuilder builder, GenerationMode mode, ConcretePrecision defaultPrecision)
        {
            foreach (var prop in properties)
            {
                // set up switched properties to use the inherited precision
                prop.SetupConcretePrecision(defaultPrecision);
            }

            // build a list of all HLSL properties
            var hlslProps = BuildHLSLPropertyList();

            if (mode == GenerationMode.Preview)
            {
                builder.AppendLine("CBUFFER_START(UnityPerMaterial)");

                // all non-gpu instanced properties (even non-batchable ones!)
                // this is because for preview we convert all properties to UnityPerMaterial properties
                // as we will be submitting the default preview values via the Material..  :)
                foreach (var h in hlslProps)
                {
                    if ((h.declaration == HLSLDeclaration.UnityPerMaterial) ||
                        (h.declaration == HLSLDeclaration.Global))
                    {
                        h.AppendTo(builder);
                    }
                }

                // DOTS instanced properties
                var dotsInstancedProperties = hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance);
                if (dotsInstancedProperties.Any())
                {
                    foreach (var h in dotsInstancedProperties)
                    {
                        h.AppendTo(builder);
                    }
                }
                builder.AppendLine("CBUFFER_END");
                builder.AppendLine("#define UNITY_ACCESS_HYBRID_INSTANCED_PROP(var, type) var");
                return;
            }

            builder.AppendLine("CBUFFER_START(UnityPerMaterial)");

            int instancedCount = 0;

            foreach (var h in hlslProps)
            {
                if (h.declaration == HLSLDeclaration.UnityPerMaterial)
                {
                    h.AppendTo(builder);
                }
                else if (h.declaration == HLSLDeclaration.HybridPerInstance)
                {
                    instancedCount++;
                }
            }

            if (instancedCount > 0)
            {
                builder.AppendLine("// Hybrid instanced properties");
                foreach (var h in hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance))
                {
                    h.AppendTo(builder);
                }
            }
            builder.AppendLine("CBUFFER_END");

            if (instancedCount > 0)
            {
                builder.AppendLine("#if defined(UNITY_DOTS_INSTANCING_ENABLED)");

                builder.AppendLine("// DOTS instancing definitions");
                builder.AppendLine("UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)");
                foreach (var h in hlslProps.Where(h => h.declaration == HLSLDeclaration.HybridPerInstance))
                {
                    var    n    = h.name;
                    string type = h.GetValueTypeString();
                    builder.AppendLine($"    UNITY_DOTS_INSTANCED_PROP({type}, {n})");
                }
                builder.AppendLine("UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)");

                builder.AppendLine("// DOTS instancing usage macros");
                builder.AppendLine("#define UNITY_ACCESS_HYBRID_INSTANCED_PROP(var, type) UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(type, var)");
                builder.AppendLine("#else");
                builder.AppendLine("#define UNITY_ACCESS_HYBRID_INSTANCED_PROP(var, type) var");
                builder.AppendLine("#endif");
            }

            builder.AppendNewLine();
            builder.AppendLine("// Object and Global properties");
            foreach (var h in hlslProps)
            {
                if (h.declaration == HLSLDeclaration.Global)
                {
                    h.AppendTo(builder);
                }
            }
        }