Jump to content

  • Log In with Google      Sign In   
  • Create Account


Learning D3D; having issues creating shaders


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 SuperStingray   Members   -  Reputation: 101

Like
0Likes
Like

Posted 02 October 2012 - 02:29 AM

So when I execute this code, my program crashes.

[source lang="cpp"]//load vertex and pixel shadersID3D10Blob *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 objectsdev->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?

Edited by SuperStingray, 02 October 2012 - 02:30 AM.


Sponsor:

#2 clb   Members   -  Reputation: 1780

Like
0Likes
Like

Posted 02 October 2012 - 05:40 AM

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.

Edited by clb, 02 October 2012 - 07:54 AM.

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#3 kubera   Members   -  Reputation: 903

Like
1Likes
Like

Posted 02 October 2012 - 06:00 AM

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.

#4 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
2Likes
Like

Posted 02 October 2012 - 06:45 AM

D3DX11CompileFromFile function has ppErrorMsgs argument that receives string with error message what went wrong. Use it.

#5 clb   Members   -  Reputation: 1780

Like
0Likes
Like

Posted 02 October 2012 - 07:53 AM

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.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS