コード例 #1
0
 private Type CreateStruct(ShaderReflectionVariable cd)
 {
     //TODO:
     //var shaderType = ShaderTypeMapper.GetShaderType(cd);
     //if (shaderType != null)
     //    return shaderType.NetType;
     return(null);
 }
コード例 #2
0
ファイル: MaterialVariable.cs プロジェクト: woncomp/LiliumLab
 public MaterialColorVariable(ShaderReflectionVariable v)
     : base(v)
 {
     unsafe
     {
         Vector4* p = (Vector4*)v.Description.DefaultValue;
         if (p != null) value = *p;
     }
 }
コード例 #3
0
        private Type ResolveType(ShaderReflectionVariable cd)
        {
            var type = _ResolveType(cd);

            if (type != null && cd.Type.Elements > 1)
            {
                return(type.MakeArrayType());
            }

            return(type);
        }
コード例 #4
0
        private static ShaderReflectionVariable GetReflectionVariable(GlobalVariableDeclaration vd)
        {
            ShaderReflectionVariable shaderVar = new ShaderReflectionVariable
            {
                Location = vd.Offset,
                Name     = vd.Name,
                Semantic = vd.Semantic,
                Size     = vd.Type.Size,
                Type     = vd.Type.ReflectionType
            };

            return(shaderVar);
        }
コード例 #5
0
        private static void CompareConstantBufferVariable(ShaderReflectionVariable expected,
                                                          ShaderVariable actual)
        {
            //Assert.AreEqual(expected.Description.DefaultValue, actual.DefaultValue); // TODO
            Assert.AreEqual((int)expected.Description.Flags, (int)actual.Flags);
            Assert.AreEqual(expected.Description.Name, actual.Name);
            Assert.AreEqual(expected.Description.SamplerSize, actual.SamplerSize);
            Assert.AreEqual(expected.Description.Size, actual.Size);
            Assert.AreEqual((uint)expected.Description.StartOffset, actual.StartOffset);
            if (expected.Description.StartSampler != -1 && actual.StartSampler != 0)
            {
                Assert.AreEqual(expected.Description.StartSampler, actual.StartSampler);
            }
            if (expected.Description.StartTexture != -1 && actual.StartTexture != 0)
            {
                Assert.AreEqual(expected.Description.StartTexture, actual.StartTexture);
            }
            Assert.AreEqual(expected.Description.TextureSize, actual.TextureSize);

            CompareConstantBufferVariableType(expected.GetVariableType(), actual.ShaderType);
        }
コード例 #6
0
        private void GetVariablesInformation(PropertyInfo[] proPS)
        {
            foreach (var p in proPS)
            {
                for (int i = 0; i < Techniques.Length; i++)
                {
                    for (int j = 0; j < Techniques[i].Length; j++)
                    {
                        VarInfo v;
                        ShaderReflectionVariable ud = Techniques[i][j].Program.GetUniformDescription(p.Name);

                        if (!_varlookup.TryGetValue(p.Name, out v))
                        {
                            v = new VarInfo()
                            {
                                Name    = p.Name,
                                NetProp = p,
                            };
                            _varlookup.Add(v.Name, v);
                        }
                        if (v.Desc == null)
                        {
                            //Check is v.Desc is equal ud
                            v.Desc    = ud;
                            v.NetType = ud != null ? p.PropertyType : null;
                        }

                        if (ud != null && !Techniques[i][j].ContainsVariable(v.Name))
                        {
                            v.Passes.Add(new PassInfo {
                                Technique = i, Pass = j
                            });
                        }
                    }
                }
            }
        }
コード例 #7
0
        public static void CompileD3D12Shader(H1ShaderCompileInput input, H1ShaderCompileOutput output)
        {
            // process shared/single environments
            String includeContent = "";
            List <SharpDX.Direct3D.ShaderMacro> macros = new List <SharpDX.Direct3D.ShaderMacro>();

            // 1. shared environment
            ProcessShaderCompilerEnvironment(input.SharedEnvironment, ref includeContent, macros);

            // 2. single environment
            ProcessShaderCompilerEnvironment(input.Environment, ref includeContent, macros);

            // load shader file content
            String sourceContent = includeContent + "\n" + LoadShaderFile(input.SourceFileName);

            // preprocess the shader file
            sourceContent = ShaderBytecode.Preprocess(sourceContent, macros.ToArray(), new H1SharpDXCompileInclude());
#if DEBUG
            var shader = ShaderBytecode.Compile(sourceContent, input.EntryPointName, input.Target.ToFormat, SharpDX.D3DCompiler.ShaderFlags.Debug | SharpDX.D3DCompiler.ShaderFlags.SkipOptimization);
#else
            var shader = ShaderBytecode.Compile(sourceContent, input.EntryPointName, input.Target.ToFormat);
#endif
            if (shader.Message != null) // failed to compile the shader
            {
                // @TODO - should log the error for failing compiling shader
                output.IsSucceed = false;
                return;
            }

            // assign the resultant byte code
            output.Code = shader;
            // create shader parameter map
            output.ParameterMap = new H1ShaderParameterMap();

            // reflection for the compiled shader
            ShaderReflection  shaderReflect = new ShaderReflection(shader);
            ShaderDescription shaderDesc    = shaderReflect.Description;

            int bindResCounts = shaderDesc.BoundResources;
            for (int resIdx = 0; resIdx < bindResCounts; ++resIdx)
            {
                InputBindingDescription bindDesc = shaderReflect.GetResourceBindingDescription(resIdx);

                // for constant buffers
                if (bindDesc.Type == ShaderInputType.ConstantBuffer)
                {
                    int            cbIndex = bindDesc.BindPoint;
                    ConstantBuffer cb      = shaderReflect.GetConstantBuffer(cbIndex);

                    ConstantBufferDescription cbDesc;
                    cbDesc = cb.Description;

                    // track all variables in this constant buffer
                    for (int varIdx = 0; varIdx < cbDesc.VariableCount; varIdx++)
                    {
                        ShaderReflectionVariable  variable     = cb.GetVariable(varIdx);
                        ShaderVariableDescription variableDesc = variable.Description;

                        output.ParameterMap.ParameterMap.Add(variableDesc.Name,
                                                             new H1ParameterAllocation(H1ParameterType.Variable, cbIndex, variableDesc.StartOffset, variableDesc.Size));
                    }

                    // add constant buffer parameter
                    output.ParameterMap.ParameterMap.Add(cbDesc.Name,
                                                         new H1ParameterAllocation(H1ParameterType.ConstantBuffer, cbIndex, -1, cbDesc.Size));
                }

                // texture, samplers .... other various GDI data
            }

            // release shader reflection
            shaderReflect.Dispose();
            output.IsSucceed = true; // successfully compiled
        }
コード例 #8
0
        /// <inheritdoc/>
        public ShaderReflectionOld GetReflection(ShaderBytecode shaderBytecode)
        {
            var shaderReflection     = new SharpDX.D3DCompiler.ShaderReflection(shaderBytecode);
            var shaderReflectionDesc = shaderReflection.Description;

            // Extract reflection data
            var shaderReflectionCopy = new ShaderReflectionOld();

            shaderReflectionCopy.BoundResources  = new List <InputBindingDescription>();
            shaderReflectionCopy.ConstantBuffers = new List <ShaderReflectionConstantBuffer>();

            // BoundResources
            for (int i = 0; i < shaderReflectionDesc.BoundResources; ++i)
            {
                var boundResourceDesc = shaderReflection.GetResourceBindingDescription(i);
                shaderReflectionCopy.BoundResources.Add(new InputBindingDescription
                {
                    BindPoint = boundResourceDesc.BindPoint,
                    BindCount = boundResourceDesc.BindCount,
                    Name      = boundResourceDesc.Name,
                    Type      = (ShaderInputType)boundResourceDesc.Type,
                });
            }

            // ConstantBuffers
            for (int i = 0; i < shaderReflectionDesc.ConstantBuffers; ++i)
            {
                var constantBuffer     = shaderReflection.GetConstantBuffer(i);
                var constantBufferDesc = constantBuffer.Description;
                var constantBufferCopy = new ShaderReflectionConstantBuffer
                {
                    Name      = constantBufferDesc.Name,
                    Size      = constantBufferDesc.Size,
                    Variables = new List <ShaderReflectionVariable>(),
                };

                switch (constantBufferDesc.Type)
                {
                case SharpDX.D3DCompiler.ConstantBufferType.ConstantBuffer: constantBufferCopy.Type = ConstantBufferType.ConstantBuffer; break;

                case SharpDX.D3DCompiler.ConstantBufferType.TextureBuffer: constantBufferCopy.Type = ConstantBufferType.TextureBuffer; break;

                default: constantBufferCopy.Type = ConstantBufferType.Unknown; break;
                }

                // ConstantBuffers variables
                for (int j = 0; j < constantBufferDesc.VariableCount; ++j)
                {
                    var variable     = constantBuffer.GetVariable(j);
                    var variableType = variable.GetVariableType().Description;
                    var variableDesc = variable.Description;

                    var variableCopy = new ShaderReflectionVariable
                    {
                        Name        = variableDesc.Name,
                        Size        = variableDesc.Size,
                        StartOffset = variableDesc.StartOffset,
                        Type        = new ShaderReflectionType {
                            Offset = variable.GetVariableType().Description.Offset
                        },
                    };

                    constantBufferCopy.Variables.Add(variableCopy);
                }

                shaderReflectionCopy.ConstantBuffers.Add(constantBufferCopy);
            }

            return(shaderReflectionCopy);
        }
コード例 #9
0
ファイル: MaterialVariable.cs プロジェクト: woncomp/LiliumLab
 public MaterialVariable(ShaderReflectionVariable v)
 {
     Name = v.Description.Name;
     Size = v.Description.Size;
     StartOffset = v.Description.StartOffset;
 }
コード例 #10
0
ファイル: MaterialVariable.cs プロジェクト: woncomp/LiliumLab
 public MaterialFloatVariable(ShaderReflectionVariable v)
     : base(v)
 {
     unsafe
     {
         float* p = (float*)v.Description.DefaultValue;
         if (p != null) value = *p;
     }
 }
コード例 #11
0
        private Type _ResolveType(ShaderReflectionVariable cd)
        {
            var type = cd.Type;

            switch (type.Class)
            {
            case TypeClass.Scalar:
            {
                switch (type.Type)
                {
                case ShaderType.UserDefined: throw new InvalidOperationException();

                case ShaderType.Bool: return(typeof(bool));

                case ShaderType.Int: return(typeof(int));

                case ShaderType.Float: return(typeof(float));
                }
            }
            break;

            case TypeClass.Vector:
                if (type.Columns == 2)
                {
                    return(typeof(Vector2));
                }
                else if (type.Columns == 3)
                {
                    return(typeof(Vector3));
                }
                else if (type.Type == ShaderType.Int)
                {
                    return(typeof(Int4));
                }
                else
                {
                    return(typeof(Vector4));
                }

            case TypeClass.Matrix: return(typeof(Matrix));

            case TypeClass.Object:
            {
                switch (type.Type)
                {
                case ShaderType.Texture:
                    return(typeof(Texture));

                case ShaderType.Texture1D:
                    return(typeof(Texture1D));

                case ShaderType.Texture2D:
                    return(typeof(Texture2D));

                case ShaderType.Texture3D:
                    return(typeof(Texture3D));

                case ShaderType.TextureCube:
                    return(typeof(Texture2D));

                case ShaderType.Sampler:
                case ShaderType.Sampler1D:
                case ShaderType.Sampler2D:
                case ShaderType.Sampler3D:
                case ShaderType.SamplerCube:
                    return(typeof(SamplerState));
                }
                throw new InvalidOperationException("Type " + type.Name + " not Supported");
            }

            case TypeClass.Struct: return(CreateStruct(cd));
            }
            return(null);
        }
コード例 #12
0
        private ShaderVariable CreateVariable(Type type, ShaderReflectionVariable desc)
        {
            var shaderType = desc.Type;

            if (type.IsArray)
            {
                if (shaderType.Elements == 0)
                {
                    throw new InvalidOperationException("The constant " + desc.Name + "is not an array");
                }
                var e = type.GetElementType();
                if (e == typeof(int))
                {
                    return new IntArrayVariable()
                           {
                               Elements = shaderType.Elements
                           }
                }
                ;
                else if (e == typeof(bool))
                {
                    return new BoolArrayVariable()
                           {
                               Elements = shaderType.Elements
                           }
                }
                ;
                else if (e == typeof(float))
                {
                    return new FloatArrayVariable()
                           {
                               Elements = shaderType.Elements
                           }
                }
                ;
                else if (e == typeof(Matrix))
                {
                    return new MatrixArrayVariable()
                           {
                               Elements = shaderType.Elements
                           }
                }
                ;
                else if (e == typeof(Vector4))
                {
                    return new Vector4ArrayVariable()
                           {
                               Elements = shaderType.Elements
                           }
                }
                ;
                else if (e == typeof(SamplerState))
                {
                    return new SamplerStateVariableArray()
                           {
                               Elements = shaderType.Elements
                           }
                }
                ;
                else if (e.GetInterface(typeof(IShaderResource).Name) != null)
                {
                    return new ResourceVariableArray {
                               Elements = shaderType.Elements
                    }
                }
                ;
                else if (e.IsValueType)
                {
                    var insType = typeof(ShaderVariableArray <>).MakeGenericType(e);

                    var inst = (ShaderVariableArray)Activator.CreateInstance(insType);
                    inst.Elements = shaderType.Elements;
                    return(inst);
                }
                else
                {
                    throw new InvalidOperationException("The type \"" + e.Name + "\" is not supported");
                }
            }
            else
            {
                if (type == typeof(int))
                {
                    return(new IntVariable());
                }
                else if (type == typeof(bool))
                {
                    return(new BoolVariable());
                }
                else if (type == typeof(float))
                {
                    return(new FloatVariable());
                }
                else if (type == typeof(Matrix))
                {
                    return(new MatrixVariable());
                }
                else if (type == typeof(Vector4))
                {
                    return(new Vector4Variable());
                }
                else if (type.IsGenericType)
                {
                    var genDef = type.GetGenericTypeDefinition();
                    if (genDef == typeof(SArray <>))
                    {
                        var arGS    = type.GetGenericArguments();
                        var insType = typeof(RangeVariable <>).MakeGenericType(arGS);
                        return((ShaderVariable)Activator.CreateInstance(insType));
                    }
                    else if (genDef == typeof(Sampler <>))
                    {
                        var arGS    = type.GetGenericArguments();
                        var insType = typeof(SamplerVariable <>).MakeGenericType(arGS);
                        return((ShaderVariable)Activator.CreateInstance(insType));
                    }
                    else if (genDef == typeof(SamplerArray <>))
                    {
                        var arGS    = type.GetGenericArguments();
                        var insType = typeof(SamplerVariableArray <>).MakeGenericType(arGS);
                        return((ShaderVariable)Activator.CreateInstance(insType));
                    }
                    else
                    {
                        throw new InvalidOperationException("The type \"" + type.Name + "\" is not supported");
                    }
                }
                else if (type == typeof(SamplerState))
                {
                    return(new SamplerStateVariable());
                }
                else if (type.GetInterface(typeof(IShaderResource).Name) != null)
                {
                    return(new ResourceVariable());
                }
                else if (type.IsValueType)
                {
                    var insType = typeof(ShaderVariable <>).MakeGenericType(type);
                    return((ShaderVariable)Activator.CreateInstance(insType));
                }
                else
                {
                    throw new InvalidOperationException("The type \"" + type.Name + "\" is not supported");
                }
            }
        }
コード例 #13
0
		private static void CompareConstantBufferVariable(ShaderReflectionVariable expected,
			ShaderVariable actual)
		{
			//Assert.AreEqual(expected.Description.DefaultValue, actual.DefaultValue); // TODO
			Assert.AreEqual((int) expected.Description.Flags, (int) actual.Flags);
			Assert.AreEqual(expected.Description.Name, actual.Name);
			Assert.AreEqual(expected.Description.SamplerSize, actual.SamplerSize);
			Assert.AreEqual(expected.Description.Size, actual.Size);
			Assert.AreEqual(expected.Description.StartOffset, actual.StartOffset);
			if (expected.Description.StartSampler != -1 && actual.StartSampler != 0)
				Assert.AreEqual(expected.Description.StartSampler, actual.StartSampler);
			if (expected.Description.StartTexture != -1 && actual.StartTexture != 0)
			Assert.AreEqual(expected.Description.StartTexture, actual.StartTexture);
			Assert.AreEqual(expected.Description.TextureSize, actual.TextureSize);

			CompareConstantBufferVariableType(expected.GetVariableType(), actual.ShaderType);
		}
コード例 #14
0
ファイル: ShaderCompiler.cs プロジェクト: cg123/xenko
        /// <inheritdoc/>
        public ShaderReflectionOld GetReflection(ShaderBytecode shaderBytecode)
        {
            var shaderReflection = new SharpDX.D3DCompiler.ShaderReflection(shaderBytecode);
            var shaderReflectionDesc = shaderReflection.Description;

            // Extract reflection data
            var shaderReflectionCopy = new ShaderReflectionOld();
            shaderReflectionCopy.BoundResources = new List<InputBindingDescription>();
            shaderReflectionCopy.ConstantBuffers = new List<ShaderReflectionConstantBuffer>();

            // BoundResources
            for (int i = 0; i < shaderReflectionDesc.BoundResources; ++i)
            {
                var boundResourceDesc = shaderReflection.GetResourceBindingDescription(i);
                shaderReflectionCopy.BoundResources.Add(new InputBindingDescription
                {
                    BindPoint = boundResourceDesc.BindPoint,
                    BindCount = boundResourceDesc.BindCount,
                    Name = boundResourceDesc.Name,
                    Type = (ShaderInputType)boundResourceDesc.Type,
                });
            }

            // ConstantBuffers
            for (int i = 0; i < shaderReflectionDesc.ConstantBuffers; ++i)
            {
                var constantBuffer = shaderReflection.GetConstantBuffer(i);
                var constantBufferDesc = constantBuffer.Description;
                var constantBufferCopy = new ShaderReflectionConstantBuffer
                {
                    Name = constantBufferDesc.Name,
                    Size = constantBufferDesc.Size,
                    Variables = new List<ShaderReflectionVariable>(),
                };

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

                // ConstantBuffers variables
                for (int j = 0; j < constantBufferDesc.VariableCount; ++j)
                {
                    var variable = constantBuffer.GetVariable(j);
                    var variableType = variable.GetVariableType().Description;
                    var variableDesc = variable.Description;

                    var variableCopy = new ShaderReflectionVariable
                    {
                        Name = variableDesc.Name,
                        Size = variableDesc.Size,
                        StartOffset = variableDesc.StartOffset,
                        Type = new ShaderReflectionType { Offset = variable.GetVariableType().Description.Offset },
                    };

                    constantBufferCopy.Variables.Add(variableCopy);
                }

                shaderReflectionCopy.ConstantBuffers.Add(constantBufferCopy);
            }

            return shaderReflectionCopy;
        }