public GenericMaterialVariable(IEffectsManager manager, IRenderTechnique technique,
                                           GenericMaterialCore materialCore, ConstantBufferDescription constantBufferDescription,
                                           string materialShaderPassName = DefaultPassNames.Default,
                                           string shadowShaderPassName   = DefaultPassNames.ShadowPass,
                                           string wireframePassName      = DefaultPassNames.Wireframe,
                                           string depthPassName          = DefaultPassNames.DepthPrepass)
                : base(manager, technique, constantBufferDescription, materialCore)
            {
                this.materialCore = materialCore;
                materialPass      = technique[materialShaderPassName];
                shadowPass        = technique[shadowShaderPassName];
                wireframePass     = technique[wireframePassName];
                depthPass         = technique[depthPassName];
                shaderResources   = new KeyValuePair <int, ShaderResourceViewProxy> [materialPass.PixelShader.ShaderResourceViewMapping.Count];

                for (var i = 0; i < materialPass.PixelShader.ShaderResourceViewMapping.Count; ++i)
                {
                    var mapping = materialPass.PixelShader.ShaderResourceViewMapping.Mappings[i];
                    resourceIdxDict.Add(mapping.Value.Description.Name, i);
                    shaderResources[i] = new KeyValuePair <int, ShaderResourceViewProxy>(mapping.Key, null);
                }

                samplerResources = new KeyValuePair <int, SamplerStateProxy> [materialPass.PixelShader.SamplerMapping.Count];

                for (var i = 0; i < materialPass.PixelShader.SamplerMapping.Count; ++i)
                {
                    var mapping = materialPass.PixelShader.SamplerMapping.Mappings[i];
                    samplerIdxDict.Add(mapping.Value.Name, i);
                    samplerResources[i] = new KeyValuePair <int, SamplerStateProxy>(mapping.Key, null);
                }


                materialCore.UpdatingResource += MaterialCore_UpdatingResource;
            }
 public GenericPointMaterialVariable(IEffectsManager manager, IRenderTechnique technique,
                                     GenericMaterialCore materialCore, ConstantBufferDescription constantBufferDescription,
                                     string materialShaderPassName = DefaultPassNames.Default,
                                     string shadowShaderPassName   = DefaultPassNames.ShadowPass)
     : base(manager, technique, materialCore, constantBufferDescription, materialShaderPassName, shadowShaderPassName, "")
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MaterialVariable"/> class.
 /// </summary>
 /// <param name="manager">The manager.</param>
 /// <param name="technique">The technique.</param>
 /// <param name="meshConstantBufferDesc">The Constant Buffer description</param>
 public MaterialVariable(IEffectsManager manager, IRenderTechnique technique, ConstantBufferDescription meshConstantBufferDesc)
 {
     Technique = technique;
     if (meshConstantBufferDesc != null)
     {
         ConstantBuffer = manager.ConstantBufferPool.Register(meshConstantBufferDesc);
     }
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantBufferComponent"/> class.
 /// </summary>
 /// <param name="desc">The desc.</param>
 public ConstantBufferComponent(ConstantBufferDescription desc)
 {
     bufferDesc = desc;
     if (desc != null)
     {
         internalByteArray = new byte[desc.StructSize];
     }
 }
Example #5
0
        protected override IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, Entity entity, int parameterIndex, string reference, InitializerParameters initializerParameters)
        {
            if (!ReferenceActions.ContainsKey(reference))
            {
                throw new InvalidOperationException(string.Format("[{0}]: Effect parameter not valid", reference));
            }

            return(ReferenceActions[reference](parameterIndex, entity, initializerParameters));
        }
Example #6
0
        protected override IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, IGraphicsDeviceService source,
                                                                    int parameterIndex, string reference, InitializerParameters initializerParameters)
        {
            if (!ReferenceActions.ContainsKey(reference))
            {
                throw new ArgumentException(string.Format("{0}: {1} not valid.", GetType().Name, reference));
            }

            return(ReferenceActions[reference](parameterIndex, source, initializerParameters));
        }
Example #7
0
            public MorphTargetUploaderCore()
                : base(RenderType.None)
            {
                NeedUpdate = false;

                //Setup cbuffer
                var cbd = new ConstantBufferDescription(DefaultBufferNames.MorphTargetCB, 16); //maybe no slot issue

                cbMorphTarget = AddComponent(new ConstantBufferComponent(cbd));
            }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MaterialVariable"/> class.
 /// </summary>
 /// <param name="manager">The manager.</param>
 /// <param name="technique">The technique.</param>
 /// <param name="meshConstantBufferDesc">The Constant Buffer description</param>
 /// <param name="materialCore"></param>
 public MaterialVariable(IEffectsManager manager, IRenderTechnique technique, ConstantBufferDescription meshConstantBufferDesc, MaterialCore materialCore)
 {
     Technique      = technique;
     EffectsManager = manager;
     if (materialCore != null)
     {
         material = materialCore;
         material.PropertyChanged += MaterialCore_PropertyChanged;
     }
     ConstantBuffer = new ConstantBufferComponent(meshConstantBufferDesc);
 }
Example #9
0
 public GenericMaterialCore(
     string materialShaderPassName,
     string shadowShaderPassName,
     string wireframePassName,
     ConstantBufferDescription constantBufferDesc)
 {
     this.MaterialPassName  = materialShaderPassName;
     this.ShadowPassName    = shadowShaderPassName;
     this.WireframePassName = wireframePassName;
     cbDescription          = constantBufferDesc;
 }
Example #10
0
        protected override IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, Entity entity, int parameterIndex, string reference, InitializerParameters initializerParameters)
        {
            string cameraIdValue = cbParent.Get(Param.Properties.CameraId);
            int    cameraId      = Int32.Parse(cameraIdValue);

            if (!ReferenceActions.ContainsKey(reference))
            {
                throw new InvalidOperationException(string.Format("[{0}]: Camera parameter not valid", reference));
            }

            return(ReferenceActions[reference](parameterIndex, entity, initializerParameters));
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="description"></param>
 public ConstantBufferProxy(ConstantBufferDescription description)
     : base(description.StructSize, description.BindFlags)
 {
     if (description.StructSize % 16 != 0)
     {
         throw new ArgumentException("Constant buffer struct size must be multiple of 16 bytes");
     }
     bufferDesc = new BufferDescription()
     {
         SizeInBytes         = description.StructSize,
         BindFlags           = description.BindFlags,
         CpuAccessFlags      = description.CpuAccessFlags,
         OptionFlags         = description.OptionFlags,
         Usage               = description.Usage,
         StructureByteStride = description.StrideSize
     };
 }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MaterialVariable"/> class.
 /// </summary>
 /// <param name="manager">The manager.</param>
 /// <param name="technique">The technique.</param>
 /// <param name="meshMaterialConstantBufferDesc">The Constant Buffer description</param>
 /// <param name="materialCore"></param>
 public MaterialVariable(IEffectsManager manager, IRenderTechnique technique,
                         ConstantBufferDescription meshMaterialConstantBufferDesc,
                         MaterialCore materialCore)
 {
     Technique      = technique;
     EffectsManager = manager;
     if (materialCore != null)
     {
         material = materialCore;
         material.PropertyChanged += MaterialCore_PropertyChanged;
     }
     materialCBDescription = meshMaterialConstantBufferDesc;
     if (manager != null)
     {
         storage   = manager.StructArrayPool.Register(materialCBDescription.StructSize);
         storageId = storage.GetId();
     }
 }
Example #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericMaterialCore"/> class.
        /// </summary>
        /// <param name="shaderPass">The shader pass. Currently only supports pixel shader parameter properties</param>
        /// <param name="modelMaterialConstantBufferName">Name of the model material constant buffer in pixel shader.</param>
        public GenericMaterialCore(ShaderPass shaderPass, string modelMaterialConstantBufferName)
        {
            if (shaderPass.IsNULL || shaderPass.PixelShader.IsNULL)
            {
                return;
            }
            List <string> properties = new List <string>();
            var           cb         = shaderPass.PixelShader.ConstantBufferMapping.Mappings
                                       .Where(x => x.Value.Name == modelMaterialConstantBufferName).FirstOrDefault();

            if (cb.Value != null)
            {
                cbDescription = new ConstantBufferDescription(cb.Value.Name, cb.Value.bufferDesc.SizeInBytes);
                properties.AddRange(cb.Value.VariableDictionary.Keys);
            }

            PropertieNames = properties.ToArray();
            TextureNames   = shaderPass.PixelShader.ShaderResourceViewMapping.Mappings.Select(x => x.Value.Description.Name).ToArray();
            SamplerNames   = shaderPass.PixelShader.SamplerMapping.Mappings.Select(x => x.Value.Name).ToArray();
        }
Example #14
0
 public GenericMaterialCore(ConstantBufferDescription constantBufferDesc)
 {
     cbDescription = constantBufferDesc;
 }
Example #15
0
 protected override bool ValidateConstantBuffer(ConstantBufferDescription cb)
 {
     return(true);
 }
            public GenericMaterialVariable(IEffectsManager manager, IRenderTechnique technique,
                                           GenericMaterialCore materialCore, ConstantBufferDescription constantBufferDescription,
                                           string materialShaderPassName = DefaultPassNames.Default,
                                           string shadowShaderPassName   = DefaultPassNames.ShadowPass,
                                           string wireframePassName      = DefaultPassNames.Wireframe,
                                           string depthPassName          = DefaultPassNames.DepthPrepass)
                : base(manager, technique, constantBufferDescription, materialCore)
            {
                core            = materialCore;
                materialPass    = technique[materialShaderPassName];
                shadowPass      = technique[shadowShaderPassName];
                wireframePass   = technique[wireframePassName];
                depthPass       = technique[depthPassName];
                shaderResources = new KeyValuePair <int, ShaderResourceViewProxy> [materialPass.PixelShader.ShaderResourceViewMapping.Count];

                for (int i = 0; i < materialPass.PixelShader.ShaderResourceViewMapping.Count; ++i)
                {
                    var mapping = materialPass.PixelShader.ShaderResourceViewMapping.Mappings[i];
                    resourceIdxDict.Add(mapping.Value.Description.Name, i);
                    shaderResources[i] = new KeyValuePair <int, ShaderResourceViewProxy>(mapping.Key, null);
                }

                samplerResources = new KeyValuePair <int, SamplerStateProxy> [materialPass.PixelShader.SamplerMapping.Count];

                for (int i = 0; i < materialPass.PixelShader.SamplerMapping.Count; ++i)
                {
                    var mapping = materialPass.PixelShader.SamplerMapping.Mappings[i];
                    samplerIdxDict.Add(mapping.Value.Name, i);
                    samplerResources[i] = new KeyValuePair <int, SamplerStateProxy>(mapping.Key, null);
                }

                foreach (var texture in materialCore.TextureDict)
                {
                    SetTexture(texture.Key, texture.Value);
                }

                foreach (var sampler in materialCore.SamplerDict)
                {
                    SetSampler(sampler.Key, sampler.Value);
                }

                foreach (var prop in materialCore.FloatDict)
                {
                    WriteValue(prop.Key, prop.Value);
                }
                foreach (var prop in materialCore.Vector2Dict)
                {
                    WriteValue(prop.Key, prop.Value);
                }
                foreach (var prop in materialCore.Vector3Dict)
                {
                    WriteValue(prop.Key, prop.Value);
                }
                foreach (var prop in materialCore.Vector4Dict)
                {
                    WriteValue(prop.Key, prop.Value);
                }
                foreach (var prop in materialCore.MatrixDict)
                {
                    WriteValue(prop.Key, prop.Value);
                }
                materialCore.UpdatingResource += MaterialCore_UpdatingResource;
            }
Example #17
0
        private void ValidateConstantBufferReflection(ConstantBuffer constantBufferRaw, ref ConstantBufferDescription constantBufferRawDesc, EffectConstantBufferDescription constantBuffer, LoggerResult log)
        {
            switch (constantBufferRawDesc.Type)
            {
            case SharpDX.D3DCompiler.ConstantBufferType.ConstantBuffer:
                if (constantBuffer.Type != ConstantBufferType.ConstantBuffer)
                {
                    log.Error($"Invalid buffer type for {constantBuffer.Name}: {constantBuffer.Type} instead of {ConstantBufferType.ConstantBuffer}");
                }
                break;

            case SharpDX.D3DCompiler.ConstantBufferType.TextureBuffer:
                if (constantBuffer.Type != ConstantBufferType.TextureBuffer)
                {
                    log.Error($"Invalid buffer type for {constantBuffer.Name}: {constantBuffer.Type} instead of {ConstantBufferType.TextureBuffer}");
                }
                break;

            default:
                if (constantBuffer.Type != ConstantBufferType.Unknown)
                {
                    log.Error($"Invalid buffer type for {constantBuffer.Name}: {constantBuffer.Type} instead of {ConstantBufferType.Unknown}");
                }
                break;
            }

            // ConstantBuffers variables
            for (int i = 0; i < constantBufferRawDesc.VariableCount; i++)
            {
                var variable                = constantBufferRaw.GetVariable(i);
                var variableType            = variable.GetVariableType();
                var variableDescription     = variable.Description;
                var variableTypeDescription = variableType.Description;

                if (variableTypeDescription.Offset != 0)
                {
                    log.Error($"Unexpected offset [{variableTypeDescription.Offset}] for variable [{variableDescription.Name}] in constant buffer [{constantBuffer.Name}]");
                }

                var binding = constantBuffer.Members[i];
                // Retrieve Link Member
                if (binding.RawName != variableDescription.Name)
                {
                    log.Error($"Variable [{variableDescription.Name}] in constant buffer [{constantBuffer.Name}] has no link");
                }
                else
                {
                    var parameter = new EffectValueDescription()
                    {
                        Type =
                        {
                            Class       = (EffectParameterClass)variableTypeDescription.Class,
                            Type        = ConvertVariableValueType(variableTypeDescription.Type, log),
                            Elements    = variableTypeDescription.ElementCount,
                            RowCount    = (byte)variableTypeDescription.RowCount,
                            ColumnCount = (byte)variableTypeDescription.ColumnCount,
                        },
                        RawName = variableDescription.Name,
                        Offset  = variableDescription.StartOffset,
                        Size    = variableDescription.Size,
                    };

                    if (parameter.Offset != binding.Offset ||
                        parameter.Size != binding.Size ||
                        parameter.Type.Elements != binding.Type.Elements ||
                        ((parameter.Type.Class != EffectParameterClass.Struct) &&    // Ignore columns/rows if it's a struct (sometimes it contains weird data)
                         (parameter.Type.RowCount != binding.Type.RowCount || parameter.Type.ColumnCount != binding.Type.ColumnCount)))
                    {
                        log.Error($"Variable [{variableDescription.Name}] in constant buffer [{constantBuffer.Name}] binding doesn't match what was expected");
                    }
                }
            }
            if (constantBuffer.Size != constantBufferRawDesc.Size)
            {
                log.Error($"Error precomputing buffer size for {constantBuffer.Name}: {constantBuffer.Size} instead of {constantBufferRawDesc.Size}");
            }
        }
Example #18
0
 protected override bool ValidateConstantBuffer(ConstantBufferDescription cb)
 {
     return(cb.ContainsMetadata(Param.Properties.Material));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantBufferComponent"/> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="structSize">Size of the structure.</param>
 public ConstantBufferComponent(string name, int structSize)
 {
     bufferDesc = new ConstantBufferDescription(name, structSize);
 }
Example #20
0
        private ShaderConstantBufferDescription GetConstantBufferReflection(ConstantBuffer constantBufferRaw, ref ConstantBufferDescription constantBufferRawDesc, ShaderConstantBufferDescription linkBuffer, LoggerResult log)
        {
            var constantBuffer = new ShaderConstantBufferDescription
            {
                Name = constantBufferRawDesc.Name,
                Size = constantBufferRawDesc.Size,
            };

            switch (constantBufferRawDesc.Type)
            {
                case SharpDX.D3DCompiler.ConstantBufferType.ConstantBuffer:
                    constantBuffer.Type = ConstantBufferType.ConstantBuffer;
                    break;
                case SharpDX.D3DCompiler.ConstantBufferType.TextureBuffer:
                    constantBuffer.Type = ConstantBufferType.TextureBuffer;
                    break;
                default:
                    constantBuffer.Type = ConstantBufferType.Unknown;
                    break;
            }

            // ConstantBuffers variables
            var members = new List<EffectParameterValueData>();
            for (int i = 0; i < constantBufferRawDesc.VariableCount; i++)
            {
                var variable = constantBufferRaw.GetVariable(i);
                var variableType = variable.GetVariableType();
                var variableDescription = variable.Description;
                var variableTypeDescription = variableType.Description;

                var parameter = new EffectParameterValueData()
                {
                    Param =
                    {
                        Class = (EffectParameterClass)variableTypeDescription.Class,
                        Type = ConvertVariableValueType(variableTypeDescription.Type, log),
                        RawName = variableDescription.Name,
                    },
                    Offset = variableDescription.StartOffset,
                    Size = variableDescription.Size,
                    Count = variableTypeDescription.ElementCount == 0 ? 1 : variableTypeDescription.ElementCount,
                    RowCount = (byte)variableTypeDescription.RowCount,
                    ColumnCount = (byte)variableTypeDescription.ColumnCount,
                };

                if (variableTypeDescription.Offset != 0)
                {
                    log.Error("Unexpected offset [{0}] for variable [{1}] in constant buffer [{2}]", variableTypeDescription.Offset, variableDescription.Name, constantBuffer.Name);
                }

                bool bindingNotFound = true;
                // Retrieve Link Member
                foreach (var binding in linkBuffer.Members)
                {
                    if (binding.Param.RawName == variableDescription.Name)
                    {
                        // TODO: should we replicate linkMember.Count/RowCount/ColumnCount? or use what is retrieved by D3DCompiler reflection
                        parameter.Param.KeyName = binding.Param.KeyName;
                        bindingNotFound = false;
                        break;
                    }
                }

                if (bindingNotFound)
                {
                    log.Error("Variable [{0}] in constant buffer [{1}] has no link", variableDescription.Name, constantBuffer.Name);
                }

                members.Add(parameter);
            }
            constantBuffer.Members = members.ToArray();

            return constantBuffer;
        }
Example #21
0
 protected abstract IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, TSource source, int parameterIndex, string reference, InitializerParameters initializerParameters);
        private void ValidateConstantBufferReflection(ConstantBuffer constantBufferRaw, ref ConstantBufferDescription constantBufferRawDesc, EffectConstantBufferDescription constantBuffer, LoggerResult log)
        {
            switch (constantBufferRawDesc.Type)
            {
                case SharpDX.D3DCompiler.ConstantBufferType.ConstantBuffer:
                    if (constantBuffer.Type != ConstantBufferType.ConstantBuffer)
                        log.Error($"Invalid buffer type for {constantBuffer.Name}: {constantBuffer.Type} instead of {ConstantBufferType.ConstantBuffer}");
                    break;
                case SharpDX.D3DCompiler.ConstantBufferType.TextureBuffer:
                    if (constantBuffer.Type != ConstantBufferType.TextureBuffer)
                        log.Error($"Invalid buffer type for {constantBuffer.Name}: {constantBuffer.Type} instead of {ConstantBufferType.TextureBuffer}");
                    break;
                default:
                    if (constantBuffer.Type != ConstantBufferType.Unknown)
                        log.Error($"Invalid buffer type for {constantBuffer.Name}: {constantBuffer.Type} instead of {ConstantBufferType.Unknown}");
                    break;
            }

            // ConstantBuffers variables
            for (int i = 0; i < constantBufferRawDesc.VariableCount; i++)
            {
                var variable = constantBufferRaw.GetVariable(i);
                var variableType = variable.GetVariableType();
                var variableDescription = variable.Description;
                var variableTypeDescription = variableType.Description;

                if (variableTypeDescription.Offset != 0)
                {
                    log.Error("Unexpected offset [{0}] for variable [{1}] in constant buffer [{2}]", variableTypeDescription.Offset, variableDescription.Name, constantBuffer.Name);
                }

                var binding = constantBuffer.Members[i];
                // Retrieve Link Member
                if (binding.RawName != variableDescription.Name)
                {
                    log.Error("Variable [{0}] in constant buffer [{1}] has no link", variableDescription.Name, constantBuffer.Name);
                }
                else
                {
                    var parameter = new EffectValueDescription()
                    {
                        Type =
                        {
                            Class = (EffectParameterClass)variableTypeDescription.Class,
                            Type = ConvertVariableValueType(variableTypeDescription.Type, log),
                            Elements = variableTypeDescription.ElementCount,
                            RowCount = (byte)variableTypeDescription.RowCount,
                            ColumnCount = (byte)variableTypeDescription.ColumnCount,
                        },
                        RawName = variableDescription.Name,
                        Offset = variableDescription.StartOffset,
                        Size = variableDescription.Size,
                    };

                    if (parameter.Offset != binding.Offset
                        || parameter.Size != binding.Size
                        || parameter.Type.Elements != binding.Type.Elements
                        || ((parameter.Type.Class != EffectParameterClass.Struct) && // Ignore columns/rows if it's a struct (sometimes it contains weird data)
                               (parameter.Type.RowCount != binding.Type.RowCount || parameter.Type.ColumnCount != binding.Type.ColumnCount)))
                    {
                        log.Error("Variable [{0}] in constant buffer [{1}] binding doesn't match what was expected", variableDescription.Name, constantBuffer.Name);
                    }
                }
            }
            if (constantBuffer.Size != constantBufferRawDesc.Size)
            {
                log.Error($"Error precomputing buffer size for {constantBuffer.Name}: {constantBuffer.Size} instead of {constantBufferRawDesc.Size}");
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantBufferComponent"/> class.
 /// </summary>
 /// <param name="desc">The desc.</param>
 public ConstantBufferComponent(ConstantBufferDescription desc)
 {
     bufferDesc = desc;
 }
Example #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantBufferComponent"/> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="structSize">Size of the structure.</param>
 public ConstantBufferComponent(string name, int structSize)
 {
     bufferDesc        = new ConstantBufferDescription(name, structSize);
     internalByteArray = new byte[structSize];
 }
Example #25
0
        private ShaderConstantBufferDescription GetConstantBufferReflection(ConstantBuffer constantBufferRaw, ref ConstantBufferDescription constantBufferRawDesc, ShaderConstantBufferDescription linkBuffer, LoggerResult log)
        {
            var constantBuffer = new ShaderConstantBufferDescription
            {
                Name = constantBufferRawDesc.Name,
                Size = constantBufferRawDesc.Size,
            };

            switch (constantBufferRawDesc.Type)
            {
                case SharpDX.D3DCompiler.ConstantBufferType.ConstantBuffer:
                    constantBuffer.Type = ConstantBufferType.ConstantBuffer;
                    break;
                case SharpDX.D3DCompiler.ConstantBufferType.TextureBuffer:
                    constantBuffer.Type = ConstantBufferType.TextureBuffer;
                    break;
                default:
                    constantBuffer.Type = ConstantBufferType.Unknown;
                    break;
            }

            // ConstantBuffers variables
            var members = new List<EffectParameterValueData>();
            for (int i = 0; i < constantBufferRawDesc.VariableCount; i++)
            {
                var variable = constantBufferRaw.GetVariable(i);
                var variableType = variable.GetVariableType();
                var variableDescription = variable.Description;
                var variableTypeDescription = variableType.Description;

                var parameter = new EffectParameterValueData()
                {
                    Param =
                    {
                        Class = (EffectParameterClass)variableTypeDescription.Class,
                        Type = ConvertVariableValueType(variableTypeDescription.Type, log),
                        RawName = variableDescription.Name,
                    },
                    Offset = variableDescription.StartOffset,
                    Size = variableDescription.Size,
                    Count = variableTypeDescription.ElementCount == 0 ? 1 : variableTypeDescription.ElementCount,
                    RowCount = (byte)variableTypeDescription.RowCount,
                    ColumnCount = (byte)variableTypeDescription.ColumnCount,
                };

                if (variableTypeDescription.Offset != 0)
                {
                    log.Error("Unexpected offset [{0}] for variable [{1}] in constant buffer [{2}]", variableTypeDescription.Offset, variableDescription.Name, constantBuffer.Name);
                }

                bool bindingNotFound = true;
                // Retrieve Link Member
                foreach (var binding in linkBuffer.Members)
                {
                    if (binding.Param.RawName == variableDescription.Name)
                    {
                        // TODO: should we replicate linkMember.Count/RowCount/ColumnCount? or use what is retrieved by D3DCompiler reflection
                        parameter.Param.KeyName = binding.Param.KeyName;
                        bindingNotFound = false;
                        break;
                    }
                }

                if (bindingNotFound)
                {
                    log.Error("Variable [{0}] in constant buffer [{1}] has no link", variableDescription.Name, constantBuffer.Name);
                }

                members.Add(parameter);
            }
            constantBuffer.Members = members.ToArray();

            return constantBuffer;
        }
 public ConstantBufferMapping(int slot, ConstantBufferDescription description)
 {
     Slot        = slot;
     Description = description;
 }
Example #27
0
 protected abstract bool ValidateConstantBuffer(ConstantBufferDescription cb);
 public static ConstantBufferMapping Create(int slot, ConstantBufferDescription description)
 {
     return(new ConstantBufferMapping(slot, description));
 }
        public void Parse(byte[] byteCode, ShaderStage stage)
        {
            ConstantBufferMappings.Clear();
            TextureMappings.Clear();
            UAVMappings.Clear();
            SamplerMappings.Clear();
            using (var reflection = new ShaderReflection(byteCode))
            {
                FeatureLevel = reflection.MinFeatureLevel;
                for (int i = 0; i < reflection.Description.BoundResources; ++i)
                {
                    var res = reflection.GetResourceBindingDescription(i);
                    switch (res.Type)
                    {
                    case ShaderInputType.ConstantBuffer:
                        var cb     = reflection.GetConstantBuffer(res.Name);
                        var cbDesc = new ConstantBufferDescription(res.Name, cb.Description.Size)
                        {
                            Stage = stage, Slot = res.BindPoint
                        };
                        ConstantBufferMappings.Add(res.Name, cbDesc.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.Texture:
                        var tDescT = new TextureDescription(res.Name, stage, TextureType.Texture);
                        TextureMappings.Add(res.Name, tDescT.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.Structured:
                        var tDescStr = new TextureDescription(res.Name, stage, TextureType.Structured);
                        TextureMappings.Add(res.Name, tDescStr.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.TextureBuffer:
                        var tDescTB = new TextureDescription(res.Name, stage, TextureType.TextureBuffer);
                        TextureMappings.Add(res.Name, tDescTB.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.UnorderedAccessViewAppendStructured:
                        var uDescAppend = new UAVDescription(res.Name, stage, UnorderedAccessViewType.AppendStructured);
                        UAVMappings.Add(res.Name, uDescAppend.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.UnorderedAccessViewConsumeStructured:
                        var uDescConsume = new UAVDescription(res.Name, stage, UnorderedAccessViewType.ConsumeStructured);
                        UAVMappings.Add(res.Name, uDescConsume.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.UnorderedAccessViewRWByteAddress:
                        var uDescByte = new UAVDescription(res.Name, stage, UnorderedAccessViewType.RWByteAddress);
                        UAVMappings.Add(res.Name, uDescByte.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.UnorderedAccessViewRWStructuredWithCounter:
                        var uDescStr = new UAVDescription(res.Name, stage, UnorderedAccessViewType.RWStructuredWithCounter);
                        UAVMappings.Add(res.Name, uDescStr.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.UnorderedAccessViewRWTyped:
                        var uDescTyped = new UAVDescription(res.Name, stage, UnorderedAccessViewType.RWTyped);
                        UAVMappings.Add(res.Name, uDescTyped.CreateMapping(res.BindPoint));
                        break;

                    case ShaderInputType.Sampler:
                        SamplerMappings.Add(res.Name, new SamplerMapping(res.BindPoint, res.Name, stage));
                        break;
                    }
                }
            }
        }
Example #30
0
        private void CollectReferences()
        {
            cbReferences      = new Dictionary <int, ConstantBufferDescription>();
            textureReferences = new Dictionary <int, TextureDescription>();
            samplerReferences = new Dictionary <int, SamplerStateDescription>();
            metaData          = new Dictionary <string, string>();

            // Collect Instance references
            var instanceReferences = (from v in InputStruct.Variables
                                      where v.EngineReference != null
                                      group v.EngineReference by v.GetMarkupValue(Param.Properties.InstanceSlot)
                                      into instanceBuffers
                                      select new { Slot = Int32.Parse(instanceBuffers.Key), References = instanceBuffers }).ToArray();

            foreach (var instanceBuffer in instanceReferences)
            {
                cbReferences.Add(cbCount++,
                                 new ConstantBufferDescription("InstanceCB", instanceBuffer.Slot, UpdateType.InstanceFrame, ShaderType.Vertex,
                                                               instanceBuffer.References, Enumerable.Empty <KeyValuePair <string, string> >()));
            }

            // Collect ConstantBuffer references
            foreach (Structs.ConstantBuffer cb in ConstantBuffers)
            {
                var markupData = from v in cb.Variables
                                 from markup in v.Markup
                                 where v.HasMarkup
                                 select markup;

                foreach (var kvp in markupData)
                {
                    // Check whether we defined multiple instances of the same metadata property
                    if (!metaData.ContainsKey(kvp.Key))
                    {
                        metaData.Add(kvp.Key, kvp.Value);
                    }
                    else if (metaData[kvp.Key] != kvp.Value)
                    {
                        LogEvent.Tool.Error("Conflicting metadata: [{0}] and [{1}]", kvp.Value, metaData[kvp.Key]);
                    }
                }

                ConstantBufferDescription cbReference = new ConstantBufferDescription(cb.Name, cb.Index.Value, cb.UpdateType, Type,
                                                                                      cb.References, metaData);
                cbReferences.Add(cbCount++, cbReference);
                metaData.Clear();
            }

            // Collect Texture references
            foreach (var texture in Textures.Where(t => t.EngineReference != null))
            {
                string reference = texture.EngineReference.Value;
                string key       = texture.ContainsMarkup(Texture.Key)
                    ? texture.GetMarkupValue(Texture.Key)
                    : string.Format("{0}.{1}", Name, reference);
                UpdateType updateType   = UpdateType.None;
                int        samplerIndex = 0;
                if (texture.HasMarkup)
                {
                    // Write metadata indicating preferred metadata
                    samplerIndex = int.Parse(texture.GetMarkupValue(Texture.SamplerIndex));
                    updateType   = texture.ContainsMarkup(Texture.UpdateType)
                        ? ReflectionHelper.ParseEnum <UpdateType>(texture.GetMarkupValue(Texture.UpdateType))
                        : UpdateType.SceneStatic;
                }
                TextureDescription tDescription = new TextureDescription(texture.Index.Value, key, reference, samplerIndex, updateType,
                                                                         Type);
                textureReferences.Add(tDescription.Index, tDescription);
            }

            // Collect sampler references
            foreach (var sampler in Samplers)
            {
                string             name         = sampler.GetMarkupValue(Sampler.SamplerName);
                Filter             filter       = ReflectionHelper.ParseEnum <Filter>(sampler.GetMarkupValue(Sampler.Filter));
                TextureAddressMode tAddressMode =
                    ReflectionHelper.ParseEnum <TextureAddressMode>(sampler.GetMarkupValue(Sampler.TextureAddressMode));
                Comparison comparison  = ReflectionHelper.ParseEnum <Comparison>(sampler.GetMarkupValue(Sampler.Comparison));
                var        samplerDesc = new SamplerStateDescription
                {
                    Index              = sampler.Index.Value,
                    Name               = name,
                    Comparison         = comparison,
                    Filter             = filter,
                    TextureAddressMode = tAddressMode,
                };
                samplerReferences.Add(samplerDesc.Index, samplerDesc);
            }
        }