sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.68k stars 641 forks source link

ShaderBytecode.CompileFromFile() System.IndexOutOfRangeException #1051

Closed bryaan closed 5 years ago

bryaan commented 5 years ago

No matter what I try I can't get D3DCompiler to compile. I tried it on the online compilers and works fine.

Using directx11 and SharpDx 4.1.0

var vertexShaderByteCode = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(vsFile, "VSMain", "vs_5_0");

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

bryaan commented 5 years ago

My hlsl file

cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
    matrix worldViewProj;
};

struct VSInput
{
    float4 position : POSITION;
    float4 color : COLOR;
};

struct PSInput
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

PSInput VSMain(VSInput input)
{
    PSInput output;

    output.position = input.position;
    //output.position = mul(input.pos, worldViewProj);
    output.color = input.color;

    return output;
}

float4 PSMain(PSInput input) : SV_TARGET
{
    return input.color;
}

Stack trace shows it is failing on Read()

    SharpDX.dll!SharpDX.Interop.Read<byte>(void* pSrc, byte[] data, int offset, int count)  Unknown
    SharpDX.D3DCompiler.dll!SharpDX.D3DCompiler.ShaderBytecode.ShaderBytecode(SharpDX.Direct3D.Blob blob)   Unknown
    SharpDX.D3DCompiler.dll!SharpDX.D3DCompiler.ShaderBytecode.Compile(System.IntPtr textSource, int textSize, string entryPoint, string profile, SharpDX.D3DCompiler.ShaderFlags shaderFlags, SharpDX.D3DCompiler.EffectFlags effectFlags, SharpDX.Direct3D.ShaderMacro[] defines, SharpDX.D3DCompiler.Include include, string sourceFileName, SharpDX.D3DCompiler.SecondaryDataFlags secondaryDataFlags, SharpDX.DataStream secondaryData)    Unknown
    SharpDX.D3DCompiler.dll!SharpDX.D3DCompiler.ShaderBytecode.Compile(string shaderSource, string entryPoint, string profile, SharpDX.D3DCompiler.ShaderFlags shaderFlags, SharpDX.D3DCompiler.EffectFlags effectFlags, SharpDX.Direct3D.ShaderMacro[] defines, SharpDX.D3DCompiler.Include include, string sourceFileName, SharpDX.D3DCompiler.SecondaryDataFlags secondaryDataFlags, SharpDX.DataStream secondaryData)   Unknown
    SharpDX.D3DCompiler.dll!SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(string fileName, string entryPoint, string profile, SharpDX.D3DCompiler.ShaderFlags shaderFlags, SharpDX.D3DCompiler.EffectFlags effectFlags, SharpDX.Direct3D.ShaderMacro[] defines, SharpDX.D3DCompiler.Include include)   Unknown
bryaan commented 5 years ago

If I instead try compiling with fxc, shows that compile goes fine.

I get further up to the signature line when it fails with: "Index was outside the bounds of the array"

            using (var vsByteCode = ShaderBytecode.FromFile(baseDir + "Minicube.vs.fxo"))
            using (var psByteCode = ShaderBytecode.FromFile(baseDir + "Minicube.ps.fxo"))
            {
                _vertexShader = new VertexShader(DeviceTarget, vsByteCode);
                _pixelShader = new PixelShader(DeviceTarget, psByteCode);

                _signature = ShaderSignature.GetInputSignature(vsByteCode);
            }

I am trying to enable the debug to get more info but can't seem to get more than that.

Can anyone point me in the right direction?

xoofx commented 5 years ago

@jkoritzinsky I had a look at the codegen and it seems completely off.

The 3.1.1 version of Blob.GetBufferSize

        internal unsafe PointerSize GetBufferSize()
        {
            return calli(System.Void*(System.Void*), this._nativePointer, *(*(IntPtr*)this._nativePointer + (IntPtr)4 * (IntPtr)sizeof(void*)));
        }

while the latest version:

        internal unsafe PointerSize GetBufferSize()
        {
            PointerSize result;
            object arg_20_0 = calli(System.Void*(System.Void*,System.Void*), this._nativePointer, &result, *(*(IntPtr*)this._nativePointer + (IntPtr)4 * (IntPtr)sizeof(void*)));
            return result;
        }

It is transforming the result into a pointer passed by argument, which is not valid.

This is quite a serious regression. Could you fix this issue in SharpGenTools and then go through all the samples and check that they are working correctly? (that's always what I did when I was doing a new version, I haven't done this for quite a while, but now checking and having even the most basic D3D11 sample not working is not great)

jkoritzinsky commented 5 years ago

@xoofx I just checked in a fix for this bug with a unit test into SharpGenTools master.

xoofx commented 5 years ago

@xoofx I just checked in a fix for this bug with a unit test into SharpGenTools master.

Amazing. Do you need to proceed on publishing a new nuget for it? Btw, What do you think of making SharpGenTools a sub repo so that we can iterate more quickly without pushing an official SharpGenTools package?

jkoritzinsky commented 5 years ago

There should be a new package on the MyGet feed that you could use.

If you want to make SharpGenTools a submodule, go for it! I'm not a super big fan of submodules and it might be a little annoying to get the MSBuild scripts working the way you want, but that is an option.

xoofx commented 5 years ago

There should be a new package on the MyGet feed that you could use.

Not sure I have access to it. The NuGet.config is configured, but it doesn't show up on my side, maybe this is a private feed?

jkoritzinsky commented 5 years ago

I think I forgot to bump the version after the release. Give me a sec to commit that. There should be a package up in about 5 minutes or so.

Commisar-Deth commented 5 years ago

Having the same problem as bryaan, trying to find where you posted but the myget feed url for sharpgentools just routes me back to their homepage, is it private? how can I get hold of it, the last release is 15 days ago and this post is 6 at the time of writing