Basic DirectX11 problem, code crashes as soon as I begin drawing anything to the screen. Also help with Netbeans configuration
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开发者_JS百科 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).
It works for me :)
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
}
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
I've had exactly the same problem using the same tutorial. I simply tried the DirectX 9 version of the same tutorial instead, which worked right away without any problems (apart from the L chars needing removed from the start of the strings, something to do with Unicode support). It seems that the root cause of this issue is lack of support for DX11 on some people's systems.
精彩评论