views:

197

answers:

2

So, I started programming in DirectX11 today, I've had a lot experience with coding, but not specifically DirectX, so I went and look at some tutorials. All was going swell, I could initialize a window, set a background color, but then as soon as I tried to define a shader and draw an object, it just crashes on load. This code is extremely simple, and I've put it in a rar so you can tell me what's wrong. I'm coding in Visual C++ Express Edition because I tried for hours yesterday to get OpenGL reference paths set up in Netbeans to no avail, so I'm not trying to get DirectX working there either yet. Here's the code I have currently: http://www.mediafire.com/?i8w1trkx7c03qts

I also tried downloading and just running the provided exe in www.dx11.org.uk/3dcube.htm, to test if it's my direct X or something, and that one gives me an error of "Failed to create D3D11 device", which from their code is a failure to complete the "D3D11CreateDeviceAndSwapChain" command. So honestly, what am I doing wrong/what is happening? (And any help on how to set the directX library references up in netbeans would be a great help).

+1  A: 

It works for me :)

alt text

I had to remove L prefix of some strings for compiling code: "shaders.hlsl", "WindowClass", "Our First Direct3D Program"

Do you have .hlsl file in the same path than your executable? Also you can't start the program from VS. You will need to launch the .exe from the explorer. But you can set an absolute path to your shader if you don't want this behavior, like that:

D3DX11CompileFromFile("C:\\Users\\Stringer\\Desktop\\DXTest\\shaders.hlsl", ...
D3DX11CompileFromFile("C:\\Users\\Stringer\\Desktop\\DXTest\\shaders.hlsl", ...

Edit: Also when creating a device or device/swapchain always use D3D11_CREATE_DEVICE_DEBUG in debug mode. This will print in VS Output Window helpful warnings and errors (everything looks OK on my side).

    D3D11CreateDeviceAndSwapChain(NULL,
                                  D3D_DRIVER_TYPE_REFERENCE,
                                  NULL,
#ifdef _DEBUG
                                  D3D11_CREATE_DEVICE_DEBUG,
#else
                                  0,
#endif
                                  NULL,
                                  NULL,
                                  D3D11_SDK_VERSION,
                                  &scd,
                                  &swapchain,
                                  &dev,
                                  NULL,
                                  &devcon);

Edit2:

Also always check for error codes everywhere application can fail:

ID3D10Blob* pErrorBlob = NULL;

HRESULT hr = D3DX11CompileFromFile("C:\\Users\\Stringer\\Desktop\\\\DXTest\\shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, &pErrorBlob, 0);
LPVOID pError = NULL;
if( pErrorBlob )
{
    pError = pErrorBlob->GetBufferPointer();
    // then cast to a char* to see it in the locals window
}
Stringer Bell
Ok, uhh, I'm home now and I tried it. I've tried everything, I still can't get it to work. I replaced the path to shaders.hlsl with "D:\\shaders.hlsl", and still it crashes on launch. Yet if I comment out "devcon->Draw(3, 0);", it displays a blue background, as it should. I also tried to remove the L prefix of some strings, but as soon as I did that, it would not compile until it had the L back in place. Any more help?
Twitch
So, I fixed the above error, and it boils down to "dev->CreateVertexShader" throwing an error for some reason.
Twitch
Does it works with a reference device? (`D3D_DRIVER_TYPE_REFERENCE`)
Stringer Bell
Wow, yes it does. Can you explain why it does and why doesn't it work on HARDWARE, SOFTWARE, or UNKNOWN?
Twitch
You can read that: http://msdn.microsoft.com/en-us/library/ff476328(VS.85).aspx.If Reference works and Hardware does not, that means most of the time an issue in the IHV driver (Independent Hardware Vendor, e.g. NVIDIA, ATI etc). Try updating your gfx driver.
Stringer Bell
Also for UNKNOWN and SOFTWARE, this is normal behavior. (SOFTWARE means you have to provide you own implementation of D3D11 and pass the HMODULE handle for the device creation, which you don't do, so it fails)
Stringer Bell
I updated my drivers today, they're the newest. All my other direct X games and c++ tests work that I've downloaded/came with direct x, but not this.
Twitch
And uhh, I tried the first Edit-code you said, but I don't know where, if at all it would print what's wrong. Nothing seems to show.
Twitch
Nevermind, worked out finally my graphics card doesn't support DX11, great oversight on my end. Thanks!
Twitch
Ahh, this is something I was suspecting (because of "the Failed to create D3D11 device" message). What's weird is that you managed to initialize a window and swapchain, set a background color.. anyway I'm glad you found out the issue.
Stringer Bell
A: 

The DX11 code from www.directxtutorial.com attempts to compile shaders as shader model 5. Although DX 10/10.1 class hardware has been updated to take advantage of some DX11 features, shader model 5 isn't one of them.

Change: 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);

to

D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &VS, 0, 0); D3DX11CompileFromFile(L"shaders.hlsl", 0, 0, "PShader", "ps_4_0", 0, 0, 0, &PS, 0, 0);

http://en.wikipedia.org/wiki/DirectX#DirectX_11

Ross Kilgariff