Direct3D 11 CreatePixelShader (E_INVLIDARGS)
I am using the fxc.exe utility from directx sdk to compile my shaders and then load them into a byte array in my application (with this code):
inline std::vector<char> ReadToByteArray(char* filename)
{
std::ifstream file;
file.open(filename);
file.seekg(0, std::ios::end);
size_t file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> data(file_size, 0);
file.read(&data[0], file_size);
return data;
}
And then create it:
std::vector<char> pixelShaderBytecode = ReadToByteArray("myps.pso");
ID3D11PixelShader* shader;
device->CreatePixelShader(pixelShaderBytecode.data(), pixelShaderBytecode.size(), nullptr, &shader);
And everything worked well. So far my shader has grown to ~ 980 bytes. Here is my code in HLSL:
struct PInput
{
float4 position: SV_POSITION;
float3 normal : NORMAL;
};
cbuffer CBuffer
{
float4 color;
float4 dirs[8];
};
float4 main(PInput input) : SV_TARGET
{
// Example code...
int directionLightsCount = 2;
float intensity = 0.0f;
// Line 1
intensity += saturate(dot(input.normal, -dirs[0].xyz));
// Line 2
//intensity += saturate(dot(input.normal, -dirs[1].xyz));
float ambient = 0.1f;
return color * saturate(ambient + intensity);
}
The problem is, if I break line 2, then I get the method E_INVALIDARG HRESULT
from ID3D11Device::CreatePixelShader
. And also a warning from the D3D11 debug level:
D3D11 ERROR: ID3D11Device::CreatePixelShader: Pixel Shader is corrupt or in an unrecognized format. [ STATE_CREATION ERROR #192: CREATEPIXELSHADER_INVALIDSHADERBYTECODE]
If I comment out this line again, then everything works. If I change them (0 index from 1) then everything works. The only difference I can see is the size of the compiled file (980 with both lines and 916 with the first line)
Here is the command I am compiling the hlsl code with:
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x64>fxc /
T ps_5_0 /E "main" /Fo "ps.pso" ps.ps
What is the reason for this behavior? Am I missing something important?
Thanx for any help.
EDIT: Forgot to say that both versions compiled without errors. Besides, the vertex shader was created without any problems (1400 bytes). I am using 11_0 function level.
source to share
Simple mistake: incorrect file reading ...
inline std::vector<char> ReadToByteArray(char* filename)
{
std::vector<char> data;
std::ifstream file(filename, std::ios::in | std::ios::binary | std::ios::ate);
data.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&data[0], data.size());
return data;
}
source to share