First tackling Shaders. Having initial confusion with a DirectX Tutorial
I'm following a DirectX Tutorial with very little knowledge of C++ on Visual C++ 2010. I know enough to follow/understand everything up to this far, but now I'm trying to include a HLSL shader. I have the contents I need, but I don't know what kind of file the HLS开发者_运维知识库L needs to be. Is there a format that the file needs to be made in? I have tried putting the shader code a .cpp file but I get an Access Violation when I use the CreateVertexShader function. I put it in a header file, but it read the HLSL code like C++ and got syntax errors. The tutorial is here: http://www.directxtutorial.com/Tutorial11/B-A/BA5.aspx
The code in the code on the bottom of the page is identical, and it compiles, but gets an access violation in debugging. Could anyone show me what I need to do for it to run properly?
Thanks.
That site is very bad, since it lacks any error checking at all. Every directX call that returns HRESULT MUST be checked for errors. If it returns S_OK everything is ok. Check the documentation and make sure you check EVERY directX return value.
Your shader probably has errors and fails to compile so you probably pass a NULL pointer to CreateVertexShader.
Here is how I check for errors:
First put this somewhere (you don't need all of those includes and libs for this, but I'm not sure which are a must for this to work):
#include <D3D11.h>
#include <DXGI.h>
#include <DxErr.h>
#include <D3D11Shader.h>
#include <D3Dcompiler.h>
#include <D3DX11async.h>
#if defined(_DEBUG)
#pragma comment(lib,"d3dx11d.lib")
#else
#pragma comment(lib,"d3dx11.lib")
#endif
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"dxgi.lib")
#pragma comment(lib,"DxErr.lib")
#pragma comment(lib,"d3dcompiler.lib")
#ifndef HR
#define HR(x){HRESULT hr=x;if(FAILED(hr)){DXTraceW(__FILE__,(DWORD)__LINE__,hr,L#x,true);TRUE;}}
#endif
Now, for every directX call that returns HRESULT (all Create* calls or the compile_shader_from_file calls), do this:
HR(device->CreateVertexShader(shaderBlob->GetBufferPointer(),shaderBlob->GetBufferSize(),NULL,&vs));
Note the "HR(" at the beggining and obviously another ")" at the very end. If this directX call fails you will get an error message.
Additionaly for shaders you must get some more data to see what you actually screwed up when writing HLSL code. Here is how I do it:
ID3D10Blob* shaderBlob=NULL;
ID3D10Blob* errorBlob=NULL;
HR(D3DX11CompileFromFileA(fileName.c_str(),macros,NULL,mainFunction.c_str(),shaderModel.c_str(),
shaderCompileFlags,0,NULL,&shaderBlob,&errorBlob,NULL));
if(errorBlob)
{
char* es=reinterpret_cast<char*>(errorBlob->GetBufferPointer());
NXMsg("ERROR",es);// Shows error message box
errorBlob->Release();
return NULL;
}
The format of the files with the HLSL code doesn't matter. Usually people choose something like .hlsl, .vs (vertex shader), .ps(pixel shader) or whatever.
精彩评论