SharpDX Shader Bytes - VertexShader accepts nothing

Started by
3 comments, last by renman29 8 years, 11 months ago

Using SharpDX - trying to use precompiled Shader bytes held by ShaderBytes as:

public byte[] spriteVSBytes = { ... tons of numbers ... }

Tried compiled shader bytes for versions: 2, 3, 4/4 lev 9_1 (not 5 yet) - compiled with fxc.exe and all these work in my DX11 c++ version

VS = new VertexShader(dev, shaderData.SpriteVSBytes); // <-- says SpriteVSBytes is invalid.

[source]

// GET DEFAULT SHADERS (from pre-compiled) - create vertex layout
ShaderBytes shaderData = new ShaderBytes(); // an object I made which holds precompiled shader bytes
ShaderSignature signature = new ShaderSignature(shaderData.SpriteVSBytes);
VS = new VertexShader(dev, shaderData.SpriteVSBytes);
PS = new PixelShader(dev, shaderData.SpritePSBytes);
var elements = new[] {
new InputElement(name: "SV_Position", index: 0, format: Format.R32G32B32_Float, offset: InputElement.AppendAligned, slot: 0, slotClass: InputClassification.PerVertexData, stepRate: 0),
new InputElement(name: "COLOR0", index: 0, format: Format.R32G32B32A32_Float, offset: InputElement.AppendAligned, slot: 0, slotClass: InputClassification.PerVertexData, stepRate: 0),
new InputElement(name: "TEXCOORD0", index: 0, format: Format.R32G32_Float, offset: InputElement.AppendAligned, slot: 0, slotClass: InputClassification.PerInstanceData, stepRate: 0),
};
vertLayout = new InputLayout(dev, signature, elements);

[/source]

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.

... but the parameter should be correct. (O_O)

Honestly I want to like SharpDX but with lack of documentation and headache after headache, I'm almost ready the throw in the towel on making a SharpDX version. sad.png

Advertisement

I tried FromFile also and it throws the same error. It seems like it doesn't like the shaders(regardless of shader version) and seems to need the shaders compiled differently.

Maybe I can't use compiled shaders and must compile from file?

Whenever you have a SharpDXException, D3D provides most of the time a debug layer to help you dig into the problem. Follow this post for details.

K - thanks - I'm looking into it now :)

Ok - NOW I got it biggrin.png

Note to others - compile to cso with a c++ project to compile all the shaders - and make sure you remember to copy the NEW cso files to the same folder as the executable. Then if you do this it will work:

[source]

ShaderSignature signature;
using (var vs = ShaderBytecode.FromFile("SpriteVS.cso"))
{
signature = ShaderSignature.GetInputSignature(vs);
VS = new VertexShader(dev, vs);
}
//using (var ps = ShaderBytecode.CompileFromFile("SpritePS.hlsl", "SpritePixelShader", "ps_5_0"))
using (var ps = ShaderBytecode.FromFile("SpritePS.cso"))
{
PS = new PixelShader(dev, ps);
}
var elements = new[] {
new InputElement("SV_Position", 0, Format.R32G32B32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
};
vertLayout = new InputLayout(dev, signature, elements);

[/source]

(also watch your input classification - I accidentally autocompleted a wrong one and never noticed)

(also do not include the 0's or whatever number in Elements - ie: TEXCOORD0 will be TEXCOORD in your vertLayout).

(ALSO - make sure you set w=1 for position in vertex shader - and use color.ToVector4 - and be careful of wrong stride calculation)

Yay! *dances* biggrin.png

This topic is closed to new replies.

Advertisement