public override string ToString()
        {
            var description = "Input Parameters: ";

            var shaderReflection = new SharpDX.D3DCompiler.ShaderReflection(NativeSignature);
            for (int i = 0; i < shaderReflection.Description.InputParameters; i++)
            {
                var parameterDescription = shaderReflection.GetInputParameterDescription(i);
                description += parameterDescription.SemanticName+parameterDescription.SemanticIndex;

                if (i != shaderReflection.Description.InputParameters - 1)
                    description += ", ";
            }

            return description;
        }
Esempio n. 2
0
        public override string ToString()
        {
            var description = "Input Parameters: ";

            var shaderReflection = new SharpDX.D3DCompiler.ShaderReflection(NativeSignature);

            for (int i = 0; i < shaderReflection.Description.InputParameters; i++)
            {
                var parameterDescription = shaderReflection.GetInputParameterDescription(i);
                description += parameterDescription.SemanticName + parameterDescription.SemanticIndex;

                if (i != shaderReflection.Description.InputParameters - 1)
                {
                    description += ", ";
                }
            }

            return(description);
        }
 public static IEnumerable<ConstantBuffeDictionary> From(SharpDX.D3DCompiler.ShaderBytecode byteCode)
 {
     var reflection = new SharpDX.D3DCompiler.ShaderReflection(byteCode);
     return Enumerable.Range(0, reflection.Description.ConstantBuffers)
     .Select(i => reflection.GetConstantBuffer(i))
     .Select(c => ConstantBufferDictionaryFactory.From(c))
     ;
 }
Esempio n. 4
0
        public static ShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List <ConstantBufferData> cbuffers, int sharedIndex, Dictionary <string, SamplerStateInfo> samplerStates, bool debug)
        {
            var dxshader = new ShaderData();

            dxshader.IsVertexShader = isVertexShader;
            dxshader.SharedIndex    = sharedIndex;
            dxshader.Bytecode       = (byte[])byteCode.Clone();

            // Strip the bytecode we're gonna save!
            var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
                             SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;

            if (!debug)
            {
                stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;
            }

            using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
            {
                // Strip the bytecode for saving to disk.
                var stripped = original.Strip(stripFlags);
                {
                    // Only SM4 and above works with strip... so this can return null!
                    if (stripped != null)
                    {
                        dxshader.ShaderCode = stripped;
                    }
                    else
                    {
                        // TODO: There is a way to strip SM3 and below
                        // but we have to write the method ourselves.
                        //
                        // If we need to support it then consider porting
                        // this code over...
                        //
                        // http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
                        //
                        dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
                    }
                }

                // Use reflection to get details of the shader.
                using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
                {
                    // Get the samplers.
                    var samplers = new List <Sampler>();
                    for (var i = 0; i < refelect.Description.BoundResources; i++)
                    {
                        var rdesc = refelect.GetResourceBindingDescription(i);
                        if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
                        {
                            var samplerName = rdesc.Name;

                            var sampler = new Sampler
                            {
                                textureSlot   = rdesc.BindPoint,
                                samplerSlot   = rdesc.BindPoint,
                                parameterName = samplerName
                            };

                            SamplerStateInfo state;
                            if (samplerStates.TryGetValue(samplerName, out state))
                            {
                                sampler.parameterName = state.TextureName ?? samplerName;
                                sampler.state         = state.State;
                            }
                            else
                            {
                                foreach (var s in samplerStates.Values)
                                {
                                    if (samplerName == s.TextureName)
                                    {
                                        sampler.state = s.State;
                                        samplerName   = s.Name;
                                        break;
                                    }
                                }
                            }

                            // Find sampler slot, which can be different from the texture slot.
                            for (int j = 0; j < refelect.Description.BoundResources; j++)
                            {
                                var samplerrdesc = refelect.GetResourceBindingDescription(j);

                                if (samplerrdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Sampler &&
                                    samplerrdesc.Name == samplerName)
                                {
                                    sampler.samplerSlot = samplerrdesc.BindPoint;
                                    break;
                                }
                            }

                            switch (rdesc.Dimension)
                            {
                            case ShaderResourceViewDimension.Texture1D:
                            case ShaderResourceViewDimension.Texture1DArray:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_1D;
                                break;

                            case ShaderResourceViewDimension.Texture2D:
                            case ShaderResourceViewDimension.Texture2DArray:
                            case ShaderResourceViewDimension.Texture2DMultisampled:
                            case ShaderResourceViewDimension.Texture2DMultisampledArray:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D;
                                break;

                            case ShaderResourceViewDimension.Texture3D:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_VOLUME;
                                break;

                            case ShaderResourceViewDimension.TextureCube:
                            case ShaderResourceViewDimension.TextureCubeArray:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_CUBE;
                                break;
                            }

                            samplers.Add(sampler);
                        }
                    }
                    dxshader._samplers = samplers.ToArray();

                    // Gather all the constant buffers used by this shader.
                    dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
                    for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
                    {
                        var cb = new ConstantBufferData(refelect.GetConstantBuffer(i));

                        // Look for a duplicate cbuffer in the list.
                        for (var c = 0; c < cbuffers.Count; c++)
                        {
                            if (cb.SameAs(cbuffers[c]))
                            {
                                cb = null;
                                dxshader._cbuffers[i] = c;
                                break;
                            }
                        }

                        // Add a new cbuffer.
                        if (cb != null)
                        {
                            dxshader._cbuffers[i] = cbuffers.Count;
                            cbuffers.Add(cb);
                        }
                    }
                }
            }

            return(dxshader);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static ShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List<ConstantBufferData> cbuffers, int sharedIndex, Dictionary<string, SamplerStateInfo> samplerStates, bool debug)
        {
            var dxshader = new ShaderData();
            dxshader.IsVertexShader = isVertexShader;
            dxshader.SharedIndex = sharedIndex;
            dxshader.Bytecode = (byte[])byteCode.Clone();

            // Strip the bytecode we're gonna save!
            var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
                             SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;

            if (!debug)
                stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;

            using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
            {
                // Strip the bytecode for saving to disk.
                var stripped = original.Strip(stripFlags);
                {
                    // Only SM4 and above works with strip... so this can return null!
                    if (stripped != null)
                    {
                        dxshader.ShaderCode = stripped;
                    }
                    else
                    {
                        // TODO: There is a way to strip SM3 and below
                        // but we have to write the method ourselves.
                        // 
                        // If we need to support it then consider porting
                        // this code over...
                        //
                        // http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
                        //
                        dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
                    }
                }

                // Use reflection to get details of the shader.
                using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
                {
                    // Get the samplers.
                    var samplers = new List<Sampler>();
                    for (var i = 0; i < refelect.Description.BoundResources; i++)
                    {
                        var rdesc = refelect.GetResourceBindingDescription(i);
                        if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
                        {
                            var samplerName = rdesc.Name;

                            var sampler = new Sampler
                            {
                                textureSlot = rdesc.BindPoint,
                                samplerSlot = rdesc.BindPoint,
                                parameterName = samplerName
                            };
                            
                            SamplerStateInfo state;
                            if (samplerStates.TryGetValue(samplerName, out state))
                            {
                                sampler.parameterName = state.TextureName ?? samplerName;
                                sampler.state = state.State;
                            }
                            else
                            {
                                foreach (var s in samplerStates.Values)
                                {
                                    if (samplerName == s.TextureName)
                                    {
                                        sampler.state = s.State;
                                        samplerName = s.Name;
                                        break;
                                    }
                                }
                            }

                            // Find sampler slot, which can be different from the texture slot.
                            for (int j = 0; j < refelect.Description.BoundResources; j++)
                            {
                                var samplerrdesc = refelect.GetResourceBindingDescription(j);

                                if (samplerrdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Sampler && 
                                    samplerrdesc.Name == samplerName)
                                {
                                    sampler.samplerSlot = samplerrdesc.BindPoint;
                                    break;
                                }
                            }

                            switch (rdesc.Dimension)
                            {
                                case ShaderResourceViewDimension.Texture1D:
                                case ShaderResourceViewDimension.Texture1DArray:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_1D;
                                    break;

                                case ShaderResourceViewDimension.Texture2D:
                                case ShaderResourceViewDimension.Texture2DArray:
                                case ShaderResourceViewDimension.Texture2DMultisampled:
                                case ShaderResourceViewDimension.Texture2DMultisampledArray:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D;
                                    break;

                                case ShaderResourceViewDimension.Texture3D:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_VOLUME;
                                    break;

                                case ShaderResourceViewDimension.TextureCube:
                                case ShaderResourceViewDimension.TextureCubeArray:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_CUBE;
                                    break;
                            }

                            samplers.Add(sampler);
                        }
                    }
                    dxshader._samplers = samplers.ToArray();

                    // Gather all the constant buffers used by this shader.
                    dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
                    for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
                    {
                        var cb = new ConstantBufferData(refelect.GetConstantBuffer(i));

                        // Look for a duplicate cbuffer in the list.
                        for (var c = 0; c < cbuffers.Count; c++)
                        {
                            if (cb.SameAs(cbuffers[c]))
                            {
                                cb = null;
                                dxshader._cbuffers[i] = c;
                                break;
                            }
                        }

                        // Add a new cbuffer.
                        if (cb != null)
                        {
                            dxshader._cbuffers[i] = cbuffers.Count;
                            cbuffers.Add(cb);
                        }
                    }
                }
            }

            return dxshader;
        }
Esempio n. 7
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;
        }
        public static DXShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List <DXConstantBufferData> cbuffers, int sharedIndex, bool debug)
        {
            var dxshader = new DXShaderData();

            dxshader.IsVertexShader = isVertexShader;
            dxshader.SharedIndex    = sharedIndex;
            dxshader.Bytecode       = (byte[])byteCode.Clone();

            // Strip the bytecode we're gonna save!
            var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
                             SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;

            if (!debug)
            {
                stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;
            }

            using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
            {
                // Strip the bytecode for saving to disk.
                var stripped = original.Strip(stripFlags);
                {
                    // Only SM4 and above works with strip... so this can return null!
                    if (stripped != null)
                    {
                        dxshader.ShaderCode = stripped;
                    }
                    else
                    {
                        // TODO: There is a way to strip SM3 and below
                        // but we have to write the method ourselves.
                        //
                        // If we need to support it then consider porting
                        // this code over...
                        //
                        // http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
                        //
                        dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
                    }
                }

                // Use reflection to get details of the shader.
                using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
                {
                    // Get the samplers.
                    var samplers = new List <Sampler>();
                    for (var i = 0; i < refelect.Description.BoundResources; i++)
                    {
                        var rdesc = refelect.GetResourceBindingDescription(i);
                        if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
                        {
                            samplers.Add(new Sampler
                            {
                                index         = rdesc.BindPoint,
                                parameterName = rdesc.Name,

                                // TODO: Detect the sampler type for realz.
                                type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D
                            });
                        }
                    }
                    dxshader._samplers = samplers.ToArray();

                    // Gather all the constant buffers used by this shader.
                    dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
                    for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
                    {
                        var cb = new DXConstantBufferData(refelect.GetConstantBuffer(i));

                        // Look for a duplicate cbuffer in the list.
                        for (var c = 0; c < cbuffers.Count; c++)
                        {
                            if (cb.SameAs(cbuffers[c]))
                            {
                                cb = null;
                                dxshader._cbuffers[i] = c;
                                break;
                            }
                        }

                        // Add a new cbuffer.
                        if (cb != null)
                        {
                            dxshader._cbuffers[i] = cbuffers.Count;
                            cbuffers.Add(cb);
                        }
                    }
                }
            }

            return(dxshader);
        }