Learning D3D; having issues creating shaders

Started by
3 comments, last by clb 11 years, 6 months ago
So when I execute this code, my program crashes.

[source lang="cpp"]
//load vertex and pixel shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);

//create shader objects
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);[/source]
Despite having the shaders written to them, the blobs are null after the CompileFromFile functions and cause the program to crash when they're dereferenced on the next lines. I'm willing to bet it's because there is no "shaders.hlsl" in my project directory, but then I don't know where it would be. Do I have to download it from somewhere or am I missing something?
Advertisement
Programming is not a "play a psychic and guess the errors without checking" game!


1. D3DX11CompileFromFile returns a HRESULT value that tells the error reason if one occurred. Why make life hard on yourself (and the forum posters) and try to guess the error, when you can actually just ask what it was?


2. You ask D3DX11CompileFromFile to create you the blobs VS and PS. Why do you blindly trust it to do so? I don't think anyone loves Microsoft that much to believe their code is bug-free! You should check their end of the deal, namely, test that the function assigned to VS and PS, and that it's non-null:

ID3D10Blob *VS = 0;
D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
if (!VS)
{
printf("D3DX11CompileFromFile failed to generate ID3D10Blob!\n");
clean up and abort;
}



3. Do you really trust Microsoft enough that even if the function manages to return you a valid blob, that it actually contains anything?

if (VS->GetBufferPointer() == 0 || VS->GetBufferSize() == 0)
{
printf("Error: Generated shader blob was empty!\n");
clean up and return;
}



4. Do you trust the CreateVertex/PixelShader function to always succeed? It also returns a HRESULT.

5. Do you trust the outputted pVS/pPS pointers to be valid?

I recommend you first make your program print out "Error: The file shaders.hlsl was not found!" and benignly failing before actually trying to locate why you fail to load it. The file shaders.hlsl should contain your HLSL shader program code. You need to write it yourself (or copy from whatever tutorial/sample you're following). When dealing with relative paths, using _getcwd to double-check the current working directory is helpful to diagnose where exactly the relative path is being looked at.
You would rather compile using off-iine tool like fxc.exe
You could be sure, that the shader is well-formed and you could load an intermediate code faster than source.
D3DX11CompileFromFile function has ppErrorMsgs argument that receives string with error message what went wrong. Use it.
As a sidenote, kubera's recommendation is very good also for the reason that Windows 8 RT will no longer have D3DX11CompileFromFile but shaders will have to be compiled offline. VS2012 can do this automatically, or then one has to manually use fxc.exe.

This topic is closed to new replies.

Advertisement