关于设置Shader inputLayout的SharpDXException

时间:2015-04-04 14:13:54

标签: c# exception directx sharpdx

这不常用于抛出此异常但现在确实

    if (shader.ShaderInput == null) shader.ShaderInput = new InputLayout(OneEngineInstance.EngineInstance.Device, ShaderSignature.GetInputSignature(shader.CompilationResult), new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32_Float, 16, 0) });

它给我的唯一有用信息是

    An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll

Additional information: HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.

我不知道为什么会这样做,我对SharpDX来说还是一个新手

AssetsLoader.cs

    using System;
    using System.IO;

    using SharpDX;
    using SharpDX.DXGI;
    using SharpDX.Direct3D;
    using SharpDX.Direct3D11;
    using SharpDX.D3DCompiler;

    using SharpDX.XAudio2;
    using SharpDX.Multimedia;

    using OneEngine.DataTypes;
    using OneEngine.Core;

    namespace OneEngine.Assets
    {
        public static class AssetsLoader
        {
            public static void LoadImage(Image image)
            {
                image.Texture = Texture2D.FromFile<Texture2D>(OneEngineInstance.EngineInstance.Device, image.FilePath);
                image.ShaderResourceView = new ShaderResourceView(OneEngineInstance.EngineInstance.Device, image.Texture);
                image.Sampler = new SamplerState(OneEngineInstance.EngineInstance.Device, new SamplerStateDescription()
                    {
                        // TODO > Make Simplifier classes for these values and adapt to engine settings
                        Filter = Filter.MinMagMipLinear,
                        AddressU = TextureAddressMode.Wrap,
                        AddressV = TextureAddressMode.Wrap,
                        AddressW = TextureAddressMode.Wrap,
                        BorderColor = Color.Black,
                        ComparisonFunction = Comparison.Never,
                        MaximumAnisotropy = 16,
                        MipLodBias = 0,
                        MinimumLod = 0,
                        MaximumLod = 16
                    });
                }

                public static void LoadShader(Shader shader)
                {
                    for (int i = 0; i < shader.ShaderTypes.Length; i++)
                    {
                        switch (shader.ShaderTypes[i])
                        {
                            case EShaderType.VERTEX:
                                shader.CompilationResult = ShaderBytecode.CompileFromFile(shader.FilePath, "VS", "vs_5_0");
                                shader.VertexShader = new VertexShader(OneEngineInstance.EngineInstance.Device, shader.CompilationResult);
                                break;
                            case EShaderType.PIXEL:
                                shader.CompilationResult = ShaderBytecode.CompileFromFile(shader.FilePath, "PS", "ps_5_0");
                                shader.PixelShader = new PixelShader(OneEngineInstance.EngineInstance.Device, shader.CompilationResult);
                                break;
                            case EShaderType.GEOMETRY:
                                shader.CompilationResult = ShaderBytecode.CompileFromFile(shader.FilePath, "GS", "gs_5_0");
                                shader.GeometryShader = new GeometryShader(OneEngineInstance.EngineInstance.Device, shader.CompilationResult);
                                break;
                            case EShaderType.COMPUTE:
                                shader.CompilationResult = ShaderBytecode.CompileFromFile(shader.FilePath, "CS", "cs_5_0");
                                 shader.ComputeShader = new ComputeShader(OneEngineInstance.EngineInstance.Device, shader.CompilationResult);
                                 break;
                            case EShaderType.DOMAIN:
                                shader.CompilationResult = ShaderBytecode.CompileFromFile(shader.FilePath, "DS", "ds_5_0");
                                shader.DomainShader = new DomainShader(OneEngineInstance.EngineInstance.Device, shader.CompilationResult);
                                break;
                            case EShaderType.HULL:
                                shader.CompilationResult = ShaderBytecode.CompileFromFile(shader.FilePath, "HS", "hs_5_0");
                                shader.HullShader = new HullShader(OneEngineInstance.EngineInstance.Device, shader.CompilationResult);
                                break;
                            }
                        }
        if (shader.ShaderInput == null) shader.ShaderInput = new InputLayout(OneEngineInstance.EngineInstance.Device, ShaderSignature.GetInputSignature(shader.CompilationResult), new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32_Float, 16, 0) });
    }

    public static void LoadAudio(Audio audio)
    {
        audio.Stream = new SoundStream(File.OpenRead(audio.FilePath));
        audio.Format = audio.Stream.Format;
        audio.Buffer = new AudioBuffer
        {
            Stream = audio.Stream.ToDataStream(), AudioBytes = (int)audio.Stream.Length, Flags = BufferFlags.EndOfStream
        };
        audio.Stream.Close();
        audio.Voice = new SourceVoice(AudioCore.XAudio, audio.Format, true);
        if (audio.IsLoopable) audio.Buffer.LoopCount = audio.LoopTimes;
    }

    public static void LoadVideo()
    {

    }

    public static void LoadFont()
    {

    }

    public static void LoadLanguage()
    {

    }
}

public enum EShaderType
{
    VERTEX, PIXEL, GEOMETRY, COMPUTE, DOMAIN, HULL
}

}

Shader.cs

using System;

using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.D3DCompiler;

using OneEngine.Assets;

namespace OneEngine.DataTypes
{
public sealed class Shader
{
    private String filePath;
    private EShaderType[] shaderTypes;

    private CompilationResult shaderByteCode;

    private VertexShader vs;
    private PixelShader ps;
    private GeometryShader gs;
    private ComputeShader cs;
    private DomainShader ds;
    private HullShader hs;

    private InputLayout shaderInput;
    private InputElement[] inputElements;

    public Shader(String shaderFilePath, EShaderType[] types, InputElement[] elemets)
    {
        this.filePath = shaderFilePath;
        this.shaderTypes = types;
        this.inputElements = elemets;
    }

    public void RemoveFromMemory()
    {
        if (shaderByteCode != null) shaderByteCode.Dispose();
        if (vs != null) vs.Dispose();
        if (ps != null) ps.Dispose();
        if (gs != null) gs.Dispose();
        if (cs != null) cs.Dispose();
        if (ds != null) ds.Dispose();
        if (hs != null) hs.Dispose();
    }

    public String FilePath
    {
        get { return filePath; }
    }

    public EShaderType[] ShaderTypes
    {
        get { return shaderTypes; }
        set { shaderTypes = value; }
    }

    public CompilationResult CompilationResult
    {
        get { return shaderByteCode; }
        set { shaderByteCode = value; }
    }

    public VertexShader VertexShader
    {
        get { return vs; }
        set { vs = value; }
    }

    public PixelShader PixelShader
    {
        get { return ps; }
        set { ps = value; }
    }

    public GeometryShader GeometryShader
    {
        get { return gs; }
        set { gs = value; }
    }

    public ComputeShader ComputeShader
    {
        get { return cs; }
        set { cs = value; }
    }

    public DomainShader DomainShader
    {
        get { return ds; }
        set { ds = value; }
    }

    public HullShader HullShader
    {
        get { return hs; }
        set { hs = value; }
    }

    public InputLayout ShaderInput
    {
        set { shaderInput = value; }
        get { return shaderInput; }
    }

    public InputElement[] ShaderInputElements
    {
        get { return inputElements; }
    }
}
}

如果您还需要其他任何内容,该项目是开源https://github.com/TheNanonNetwork/OneGames

1 个答案:

答案 0 :(得分:0)

尝试使用DeviceCreationFlags.Debug创建图形设备,如果输入布局无法正确创建,那么它会告诉您原因。

另外,你确定你的Color属性应该是16个字节的偏移量,因为它之前的属性只有12个字节吗?