I've got two questions really.
The first is more a description 开发者_运维问答of events: software uses DirectX 9 and compiles shaders at start-up from resources. The shaders compile fine with both debug and release versions when you run the software from within the IDE, but when you double-click it to run it stand-alone, you start getting lots of weird errors from D3DXCompileShader.
I have put this down to the possibility that Visual Studio has converted the shader text files to Unicode and that this is throwing D3DXCompileShader somehow. Why they compile and run ok when I'm within the IDE but not outside of it is a mystery (note, it still fails if you launch it outside of VS and then attach the debugger to the process). Anyone have any thoughts on this?
My second question relates to attempts to fix the above. Instead of having my own resource data type called SHADER, I've imported a shader as RCDATA, so instead of manually loading the shader text from the resource myself, I'm now able to use D3DXCompileShaderFromResource. I'm getting a bad HRESULT from my call to this however, but the `errors' collection remains empty. There is no debug output from it at all. The code looks like this:
HRESULT result;
result = D3DXCompileShaderFromResource( 0,
MAKEINTRESOURCE(IDR_SHADERPHONG),
NULL,
NULL,
"VERTEXMAIN",
D3DXGetVertexShaderProfile(Window()->GetDevice()),
D3DXSHADER_OPTIMIZATION_LEVEL3,
&MyVertexShaderBuffer,
&errors,
&MyVertexShaderConstants);
The HRESULT is -2005529767, which seems to be "failed". I can find no references on the interwebs to what it can mean in relation to D3DXCompileShaderFromResource!
I'm hoping my clever friends at stackoverflow can offer some assistance here :p. Thanks.
OK, the problem was found (by a colleague). When loading my resource I was grabbing a memory block and incorrectly converting it to std::string. That is, std::string will go through the resource looking for a null terminator, but the resource data won't necessarily have one (it's just a text file). It will eventually find one, however, probably in some part of memory the software isn't supposed to be using!
So, to convert a char buffer from a resource into a std::string, the following should be used:
return std::string(reinterpret_cast<char const*>(pD), bytes);
not this:
return std::string(pD);
, unless of course you know for sure that pD points to a null-terminated string.
精彩评论