Esempio n. 1
0
        /// <summary>
        /// Initializes the stage block.
        /// </summary>
        /// <param name="stageBlock">The stage block.</param>
        /// <param name="logger">The logger.</param>
        private void InitStageBlock(StageBlock stageBlock, Logger logger)
        {
            // If null shader, then skip init
            if (stageBlock.Index < 0)
            {
                return;
            }

            stageBlock.Shader = Effect.Pool.GetOrCompileShader(stageBlock.Type, stageBlock.Index);
            var shaderRaw = Effect.Pool.EffectData.Shaders[stageBlock.Index];

            // Cache the input signature
            if (shaderRaw.Type == EffectShaderType.Vertex)
            {
                inputSignatureManager = graphicsDevice.GetOrCreateInputSignatureManager(shaderRaw.InputSignature.Bytecode, shaderRaw.InputSignature.Hashcode);
            }

            for (int i = 0; i < shaderRaw.ConstantBuffers.Count; i++)
            {
                var constantBufferRaw = shaderRaw.ConstantBuffers[i];

                // Constant buffers with a null size are skipped
                if (constantBufferRaw.Size == 0)
                    continue;

                var constantBuffer = Effect.GetOrCreateConstantBuffer(Effect.GraphicsDevice, constantBufferRaw);
                // IF constant buffer is null, it means that there is a conflict
                if (constantBuffer == null)
                {
                    logger.Error("Constant buffer [{0}] cannot have multiple size or different content declaration inside the same effect pool", constantBufferRaw.Name);
                    continue;
                }
                
                // Test if this constant buffer is not already part of the effect
                if (Effect.ConstantBuffers[constantBufferRaw.Name] == null)
                {
                    // Add the declared constant buffer to the effect shader.
                    Effect.ConstantBuffers.Add(constantBuffer);

                    // Declare all parameter from constant buffer at the effect level.
                    foreach (var parameter in constantBuffer.Parameters)
                    {
                        var previousParameter = Effect.Parameters[parameter.Name];
                        if (previousParameter == null)
                        {
                            // Add an effect parameter linked to the approriate constant buffer at the effect level.
                            Effect.Parameters.Add(new EffectParameter((EffectData.ValueTypeParameter) parameter.ParameterDescription, constantBuffer));
                        }
                        else if (parameter.ParameterDescription != previousParameter.ParameterDescription || parameter.buffer != previousParameter.buffer)
                        {
                            // If registered parameters is different
                            logger.Error("Parameter [{0}] defined in Constant buffer [{0}] is already defined by another constant buffer with the definition [{2}]", parameter, constantBuffer.Name, previousParameter);
                        }
                    }
                }
            }

            var constantBufferLinks = new List<ConstantBufferLink>();

            // Declare all resource parameters at the effect level.
            foreach (var parameterRaw in shaderRaw.ResourceParameters)
            {
                EffectParameter parameter;
                var previousParameter = Effect.Parameters[parameterRaw.Name];

                // Skip enmpty constant buffers.
                if (parameterRaw.Type == EffectParameterType.ConstantBuffer && Effect.ConstantBuffers[parameterRaw.Name] == null)
                {
                    continue;
                }

                int resourceIndex = Effect.ResourceLinker.Count;

                if (previousParameter == null)
                {
                    parameter = new EffectParameter(parameterRaw, EffectResourceTypeHelper.ConvertFromParameterType(parameterRaw.Type), Effect.ResourceLinker.Count, Effect.ResourceLinker);
                    Effect.Parameters.Add(parameter);

                    Effect.ResourceLinker.Count += parameterRaw.Count;
                }
                else
                {
                    resourceIndex = ((EffectData.ResourceParameter) previousParameter.ParameterDescription).Slot;

                    if (CompareResourceParameter(parameterRaw, (EffectData.ResourceParameter) previousParameter.ParameterDescription))
                    {
                        // If registered parameters is different
                        logger.Error("Resource Parameter [{0}] is already defined with a different definition [{1}]", parameterRaw, previousParameter.ParameterDescription);
                    }
                    parameter = previousParameter;
                }

                // For constant buffers, we need to store explicit link
                if (parameter.ResourceType == EffectResourceType.ConstantBuffer)
                {
                    constantBufferLinks.Add(new ConstantBufferLink(Effect.ConstantBuffers[parameter.Name], parameter));
                }

                if (stageBlock.Parameters == null)
                {
                     stageBlock.Parameters = new List<EffectParameter>();
                }

                stageBlock.Parameters.Add(parameter);
            }

            stageBlock.ConstantBufferLinks = constantBufferLinks.ToArray();
        }
Esempio n. 2
0
        private AttributeCollection PrepareAttributes(Logger logger, List<AttributeData> attributes)
        {
            attributes = new List<AttributeData>(attributes);

            for (int i = 0; i < attributes.Count; i++)
            {
                var attribute = attributes[i];
                bool attributeHandled = true;
                switch (attribute.Name)
                {
                    case EffectData.AttributeKeys.Blending:
                        BlendState = graphicsDevice.BlendStates[(string) attribute.Value];
                        if (BlendState == null)
                            logger.Error("Unable to find registered BlendState [{0}]", (string)attribute.Value);
                        break;
                    case EffectData.AttributeKeys.BlendingColor:
                        BlendStateColor = (Color4) (Vector4) attribute.Value;
                        break;
                    case EffectData.AttributeKeys.BlendingSampleMask:
                        BlendStateSampleMask = (uint) attribute.Value;
                        break;

                    case EffectData.AttributeKeys.DepthStencil:
                        DepthStencilState = graphicsDevice.DepthStencilStates[(string) attribute.Value];
                        if (DepthStencilState == null)
                            logger.Error("Unable to find registered DepthStencilState [{0}]", (string)attribute.Value);
                        break;
                    case EffectData.AttributeKeys.DepthStencilReference:
                        DepthStencilReference = (int) attribute.Value;
                        break;

                    case EffectData.AttributeKeys.Rasterizer:
                        RasterizerState = graphicsDevice.RasterizerStates[(string) attribute.Value];
                        if (RasterizerState == null)
                            logger.Error("Unable to find registered RasterizerState [{0}]", (string)attribute.Value);
                        break;
                    default:
                        attributeHandled = false;
                        break;
                }

                if (attributeHandled)
                {
                    attributes.RemoveAt(i);
                    i--;
                }
            }

            return new AttributeCollection(attributes);
        }
Esempio n. 3
0
        /// <summary>
        /// Merges an existing <see cref="EffectData" /> into this instance.
        /// </summary>
        /// <param name="source">The EffectData to merge.</param>
        /// <param name="logger">Logger used to report merging errors.</param>
        /// <param name="allowOverride">if set to <c>true</c> [allow override].</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        /// <remarks>This method is useful to build an archive of several effects.</remarks>
        public bool MergeFrom(EffectData source, Logger logger, bool allowOverride = false)
        {
            bool isMergeOk = true;

            foreach (var effect in source.Effects)
            {
                bool effectAlreadyRegistered = false;

                // Add effect that is not already in the archive with this same name.
                int previousEffectIndex = 0;
                for (; previousEffectIndex < Effects.Count; previousEffectIndex++)
                {
                    var effect2 = Effects[previousEffectIndex];
                    if (effect2.Name == effect.Name)
                    {
                        effectAlreadyRegistered = true;
                        break;
                    }
                }

                if (effectAlreadyRegistered)
                {
                    if (allowOverride)
                    {
                        Effects[previousEffectIndex] = effect;
                    }
                    else
                    {
                        // Skip the effect it it is already registered
                        continue;
                    }
                }
                else
                {
                    Effects.Add(effect);
                }

                foreach (var technique in effect.Techniques)
                {
                    foreach (var pass in technique.Passes)
                    {
                        foreach (var shaderLink in pass.Pipeline)
                        {
                            if (shaderLink == null)
                                continue;

                            if (shaderLink.IsImport)
                            {
                                // If this is an import, we try first to resolve it directly
                                // Else we keep the name as-is
                                var index = FindShaderByName(shaderLink.ImportName);
                                if (index >= 0)
                                {
                                    shaderLink.ImportName = null;
                                    shaderLink.Index = index;
                                }
                            }
                            else if (!shaderLink.IsNullShader)
                            {
                                var shader = source.Shaders[shaderLink.Index];
                                var index = FindShader(shader);
                                if (index >= 0)
                                {
                                    var previousShader = Shaders[index];

                                    if (shader.Name != null)
                                    {
                                        // if shader from this instance is local and shader from source is global => transform current shader to global
                                        if (previousShader.Name == null)
                                        {
                                            previousShader.Name = shader.Name;
                                        }
                                        else if (shader.Name != previousShader.Name)
                                        {
                                            // If shader from this instance is global and shader from source is global => check names. If exported names are different, this is an error
                                            logger.Error("Cannot merge shader [{0}] into this instance, as there is already a global shader with a different name [{1}]", shader.Name, previousShader.Name);
                                            isMergeOk = false;
                                        }
                                    }

                                    shaderLink.Index = index;
                                }
                                else
                                {
                                    shaderLink.Index = Shaders.Count;
                                    Shaders.Add(shader);
                                }
                            }
                        }
                    }
                }
            }

            return isMergeOk;
        }
Esempio n. 4
0
        /// <summary>
        /// Compiles an XML font file description to a file. Optionally output dependency file.
        /// </summary>
        /// <param name="sourceXmlFile">The source XML file.</param>
        /// <param name="outputFile">The output file.</param>
        /// <param name="dependencyFile">The dependency file.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static ContentCompilerResult CompileAndSave(string sourceXmlFile, string outputFile, string dependencyFile = null)
        {
            var logger = new Logger();
            var result = new ContentCompilerResult { Logger = logger };
            try
            {
                var fontDescription = FontDescription.Load(sourceXmlFile);

                var defaultOutputFile = Path.GetFileNameWithoutExtension(sourceXmlFile);

                // Compiles to SpriteData
                outputFile = outputFile ?? defaultOutputFile;

                result.IsContentGenerated = true;
                if(dependencyFile != null)
                {
                    if(!FileDependencyList.CheckForChanges(dependencyFile))
                    {
                        result.IsContentGenerated = false;
                    }
                }

                if(result.IsContentGenerated)
                {
                    // Make sure that directory name doesn't collide with filename
                    var directoryName = Path.GetDirectoryName(outputFile + ".tmp");
                    if(!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    using(var stream = new NativeFileStream(outputFile, NativeFileMode.Create, NativeFileAccess.Write))
                    {
                        Compile(fontDescription, stream);

                        if(dependencyFile != null)
                        {
                            var dependencies = new FileDependencyList();
                            dependencies.AddDefaultDependencies();
                            dependencies.AddDependencyPath(sourceXmlFile);
                            dependencies.Save(dependencyFile);
                        }
                    }
                }
            }
            catch(FontException ex)
            {
                logger.Error(ex.Message);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
            }

            return result;
        }
Esempio n. 5
0
        private PropertyCollection PrepareProperties(Logger logger, CommonData.PropertyCollection properties)
        {
            var passProperties = new PropertyCollection();

            foreach (var property in properties)
            {
                switch (property.Key)
                {
                    case EffectData.PropertyKeys.Blending:
                        BlendState = graphicsDevice.BlendStates[(string)property.Value];
                        if (BlendState == null)
                            logger.Error("Unable to find registered BlendState [{0}]", (string)property.Value);
                        break;
                    case EffectData.PropertyKeys.BlendingColor:
                        BlendStateColor = (Color4)(Vector4)property.Value;
                        break;
                    case EffectData.PropertyKeys.BlendingSampleMask:
                        BlendStateSampleMask = (uint)property.Value;
                        break;

                    case EffectData.PropertyKeys.DepthStencil:
                        DepthStencilState = graphicsDevice.DepthStencilStates[(string)property.Value];
                        if (DepthStencilState == null)
                            logger.Error("Unable to find registered DepthStencilState [{0}]", (string)property.Value);
                        break;
                    case EffectData.PropertyKeys.DepthStencilReference:
                        DepthStencilReference = (int)property.Value;
                        break;

                    case EffectData.PropertyKeys.Rasterizer:
                        RasterizerState = graphicsDevice.RasterizerStates[(string)property.Value];
                        if (RasterizerState == null)
                            logger.Error("Unable to find registered RasterizerState [{0}]", (string)property.Value);
                        break;
                    default:
                        passProperties[new PropertyKey(property.Key)] = property.Value;
                        break;
                }
            }

            return passProperties;
        }
Esempio n. 6
0
        /// <summary>
        /// Binds the specified effect data to this instance.
        /// </summary>
        /// <param name="effectDataArg">The effect data arg.</param>
        /// <param name="cloneFromEffect">The clone from effect.</param>
        /// <exception cref="System.InvalidOperationException">If no techniques found in this effect.</exception>
        /// <exception cref="System.ArgumentException">If unable to find effect [effectName] from the EffectPool.</exception>
        internal void InitializeFrom(EffectData.Effect effectDataArg, Effect cloneFromEffect)
        {
            RawEffectData = effectDataArg;

            // Clean any previously allocated resources
            if (DisposeCollector != null)
            {
                DisposeCollector.DisposeAndClear();
            }
            ConstantBuffers.Clear();
            Parameters.Clear();
            Techniques.Clear();
            ResourceLinker = ToDispose(new EffectResourceLinker());
            if (effectConstantBuffersCache != null)
            {
                effectConstantBuffersCache.Clear();
            }

            // Copy data
            IsSupportingDynamicCompilation = RawEffectData.Arguments != null;
            ShareConstantBuffers = RawEffectData.ShareConstantBuffers;

            // Create the local effect constant buffers cache
            if (!ShareConstantBuffers)
                effectConstantBuffersCache = new Dictionary<EffectConstantBufferKey, EffectConstantBuffer>();

            var logger = new Logger();
            int techniqueIndex = 0;
            int totalPassCount = 0;
            EffectPass parentPass = null;
            foreach (var techniqueRaw in RawEffectData.Techniques)
            {
                var name = techniqueRaw.Name;
                if (string.IsNullOrEmpty(name))
                    name = string.Format("${0}", techniqueIndex++);

                var technique = new EffectTechnique(this, name);
                Techniques.Add(technique);

                int passIndex = 0;
                foreach (var passRaw in techniqueRaw.Passes)
                {
                    name = passRaw.Name;
                    if (string.IsNullOrEmpty(name))
                        name = string.Format("${0}", passIndex++);

                    var pass = new EffectPass(logger, this, technique, passRaw, name);

                    pass.Initialize(logger);

                    // If this is a subpass, add it to the parent pass
                    if (passRaw.IsSubPass)
                    {
                        if (parentPass == null)
                        {
                            logger.Error("Pass [{0}] is declared as a subpass but has no parent.");
                        }
                        else
                        {
                            parentPass.SubPasses.Add(pass);
                        }
                    }
                    else
                    {
                        technique.Passes.Add(pass);
                        parentPass = pass;
                    }
                }

                // Count the number of passes
                totalPassCount += technique.Passes.Count;
            }

            if (totalPassCount == 0)
                throw new InvalidOperationException("No passes found in this effect.");

            // Log all the exception in a single throw
            if (logger.HasErrors)
                throw new InvalidOperationException(Utilities.Join("\n", logger.Messages));

            // Initialize the resource linker when we are done with all pass/parameters
            ResourceLinker.Initialize();

            //// Sort all parameters by their resource types
            //// in order to achieve better local cache coherency in resource linker
            Parameters.Items.Sort((left, right) =>
                {
                    // First, order first all value types, then resource type
                    var comparison = left.IsValueType != right.IsValueType ? left.IsValueType ? -1 : 1 : 0;

                    // If same type
                    if (comparison == 0)
                    {
                        // Order by resource type
                        comparison = ((int)left.ResourceType).CompareTo((int)right.ResourceType);

                        // If same, order by resource index
                        if (comparison == 0)
                        {
                            comparison = left.Offset.CompareTo(right.Offset);
                        }
                    }
                    return comparison;
                });

            // Prelink constant buffers
            int resourceIndex = 0;
            foreach (var parameter in Parameters)
            {
                // Recalculate parameter resource index
                if (!parameter.IsValueType)
                {
                    parameter.Offset = resourceIndex++;
                }

                // Set the default values 
                parameter.SetDefaultValue();

                if (parameter.ResourceType == EffectResourceType.ConstantBuffer)
                    parameter.SetResource(ConstantBuffers[parameter.Name]);
            }

            // Compute slot links
            foreach (var technique in Techniques)
            {
                foreach (var pass in technique.Passes)
                {
                    foreach (var subPass in pass.SubPasses)
                    {
                        subPass.ComputeSlotLinks();
                    }
                    pass.ComputeSlotLinks();
                }
            }

            // Setup the first Current Technique.
            CurrentTechnique = this.Techniques[0];

            // Initialize predefined parameters used by Model.Draw (to speedup things internally)
            DefaultParameters = new EffectDefaultParameters(this);

            // If this is a clone, we need to 
            if (cloneFromEffect != null)
            {
                // Copy the content of the constant buffers to the new instance.
                for (int i = 0; i < ConstantBuffers.Count; i++)
                {
                    cloneFromEffect.ConstantBuffers[i].CopyTo(ConstantBuffers[i]);
                }

                // Copy back all bound resources except constant buffers
                // that are already initialized with InitializeFrom method.
                for (int i = 0; i < cloneFromEffect.ResourceLinker.Count; i++)
                {
                    if (cloneFromEffect.ResourceLinker.BoundResources[i] is EffectConstantBuffer)
                        continue;

                    ResourceLinker.BoundResources[i] = cloneFromEffect.ResourceLinker.BoundResources[i];
                    unsafe
                    {
                        ResourceLinker.Pointers[i] = cloneFromEffect.ResourceLinker.Pointers[i];
                    }
                }

                // If everything was fine, then we can register it into the pool
                Pool.AddEffect(this);
            }

            // Allow subclasses to complete initialization.
            Initialize();
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes the specified effect bytecode.
        /// </summary>
        /// <param name="effectDataArg">The effect bytecode.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        private void Initialize(EffectData.Effect effectDataArg)
        {
            if (effectDataArg == null)
                throw new ArgumentException(string.Format("Unable to find effect [{0}] from the EffectPool", Name), "effectName");

            rawEffect = effectDataArg;

            ShareConstantBuffers = effectDataArg.ShareConstantBuffers;

            // Create the local effect constant buffers cache
            if (!ShareConstantBuffers)
                effectConstantBuffersCache = new Dictionary<EffectConstantBufferKey, EffectConstantBuffer>();

            if (effectDataArg.Techniques.Count == 0)
                throw new InvalidOperationException("No techniques found in this effect");

            var logger = new Logger();
            int techniqueIndex = 0;
            int totalPassCount = 0;
            EffectPass parentPass = null;
            foreach (var techniqueRaw in effectDataArg.Techniques)
            {
                var name = techniqueRaw.Name;
                if (string.IsNullOrEmpty(name))
                    name = string.Format("${0}", techniqueIndex++);

                var technique = new EffectTechnique(this, name);
                Techniques.Add(technique);

                int passIndex = 0;
                foreach (var passRaw in techniqueRaw.Passes)
                {
                    name = passRaw.Name;
                    if (string.IsNullOrEmpty(name))
                        name = string.Format("${0}", passIndex++);

                    var pass = new EffectPass(logger, this, technique, passRaw, name);

                    pass.Initialize(logger);

                    // If this is a subpass, add it to the parent pass
                    if (passRaw.IsSubPass)
                    {
                        if (parentPass == null)
                        {
                            logger.Error("Pass [{0}] is declared as a subpass but has no parent");
                        }
                        else
                        {
                            parentPass.SubPasses.Add(pass);
                        }
                    }
                    else
                    {
                        technique.Passes.Add(pass);
                        parentPass = pass;
                    }
                }

                // Count the number of passes
                totalPassCount += technique.Passes.Count;
            }

            if (totalPassCount == 0)
                throw new InvalidOperationException("No passes found in this effect");

            // Log all the exception in a single throw
            if (logger.HasErrors)
                throw new InvalidOperationException(Utilities.Join("\n", logger.Messages));

            // Initialize the resource linker when we are done with all pass/parameters
            ResourceLinker.Initialize();

            //// Sort all parameters by their resource types
            //// in order to achieve better local cache coherency in resource linker
            Parameters.Items.Sort((left, right) =>
                {
                    // First, order first all value types, then resource type
                    var comparison = left.IsValueType != right.IsValueType ? left.IsValueType ? -1 : 1 : 0;

                    // If same type
                    if (comparison == 0)
                    {
                        // Order by resource type
                        comparison = ((int)left.ResourceType).CompareTo((int)right.ResourceType);

                        // If same, order by resource index
                        if (comparison == 0)
                        {
                            comparison = left.Offset.CompareTo(right.Offset);
                        }
                    }
                    return comparison;
                });

            // Prelink constant buffers
            int resourceIndex = 0;
            foreach (var parameter in Parameters)
            {
                // Recalculate parameter resource index
                if (!parameter.IsValueType)
                {
                    parameter.Offset = resourceIndex++;
                }

                // Set the default values 
                parameter.SetDefaultValue();

                if (parameter.ResourceType == EffectResourceType.ConstantBuffer)
                    parameter.SetResource(0, ConstantBuffers[parameter.Name]);
            }

            // Compute slot links
            foreach (var technique in Techniques)
            {
                foreach (var pass in technique.Passes)
                {
                    foreach (var subPass in pass.SubPasses)
                    {
                        subPass.ComputeSlotLinks();
                    }
                    pass.ComputeSlotLinks();
                }
            }

            // Setup the first Current Technique.
            CurrentTechnique = this.Techniques[0];
        }