Strange access violation problem

Started by
10 comments, last by Rasenger 16 years, 9 months ago
I'm trying to use shaders with C++ / DX9. Creating vertex shader works but creating pixel shader doesn't. I get "access violation reading ... 0x000000 blaa blaa"-error here:

d3ddev->CreatePixelShader((DWORD*)code2->GetBufferPointer(), &pixelShader);
Here's some code from my terrain class:

LPDIRECT3DVERTEXDECLARATION9 vertexDeclaration;
LPDIRECT3DVERTEXSHADER9 vertexShader;
LPD3DXCONSTANTTABLE constantTable;
LPDIRECT3DPIXELSHADER9 pixelShader;

...

// Now were're in constructor

vertexDeclaration = NULL;
vertexShader = NULL;
pixelShader = NULL;
constantTable = NULL;

...

/* Vertex declaration */

D3DVERTEXELEMENT9 decl[] = {
	{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
    {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
    D3DDECL_END()};

d3ddev->CreateVertexDeclaration(decl, &vertexDeclaration);

/* Load shaders */

HRESULT result1 = NULL;
HRESULT result2 = NULL;
LPD3DXBUFFER code1 = NULL;
LPD3DXBUFFER code2 = NULL;

//set up Vertex Shader
result1 = D3DXCompileShaderFromFile(L"TerrainVertex.fx",
                                   NULL,
                                   NULL,
                                   "vs_main",
                                   "vs_1_1",
                                   0,
                                   &code1,
                                   NULL,
                                   &constantTable);

d3ddev->CreateVertexShader((DWORD*)code1->GetBufferPointer(), &vertexShader);
code1->Release();

//set up Pixel Shader
result2 = D3DXCompileShaderFromFile(L"TerrainPixel.fx",
                                   NULL,     
                                   NULL,
                                   "ps_main",
                                   "ps_1_1",
                                   0,
                                   &code2,
                                   NULL,
                                   NULL);

/* ||||||  NEXT LINE PRODUCES ACCESS VIOLATION ERROR |||||| */
d3ddev->CreatePixelShader((DWORD*)code2->GetBufferPointer(), &pixelShader);
code2->Release();

Advertisement
Are you sure the return values from all the previous functions indicate success? You don't seem to be doing any checking in your code.
Try adding:
if (FAILED(result2))    // ...


EDIT: Too slow...
Well I added assertions there (should've done it earlier -__-) and it seems there's something wrong with this:

//set up Pixel Shaderresult2 = D3DXCompileShaderFromFile(L"TerrainPixel.fx",                                   NULL,                                        NULL,                                   "ps_main",                                   "ps_1_1",                                   0,                                   &code2,                                   NULL,                                   NULL);


I've checked that the filename is correct, also the HLSL file _should_ be OK. Hmm..
Well, which error did it give? It should be one of D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, or E_OUTOFMEMORY.
How would I know? :DDDDD Yes, I'm so fucking noob I shouldn't exist but I do and please bear with it. :D
There are lots of ways. You can set a breakpoint on the line in the debugger and examine the value directly after the call. You can print the value to a file. You can display the value with a MessageBox call.
Yes but where is the value? I can't just print result2 as it is..
You can use DXGetErrorDescription()/DXGetErrorString() in dxerr.h in your code.
Quote:Original post by Rasenger
//set up Pixel Shaderresult2 = D3DXCompileShaderFromFile(L"TerrainPixel.fx",                                   NULL,                                        NULL,                                   "ps_main",                                   "ps_1_1",                                   0,                                   &code2,                                   NULL,                                   NULL);


I've checked that the filename is correct, also the HLSL file _should_ be OK. Hmm..
Well "should be ok" is nice to say, but evidence would suggest you are wrong [wink]

Running FX files against fxc.exe on the command line or creating a custom build step in VS is a convenient way of checking this. You can get VS to compile each FX file when you compile the rest of your app and it'll catch the errors the same as with any other code...

Alternatively, just refer to your documentation and set your penultimate 'NULL' parameter to be a LPD3DXBUFFER and pass it to OutputDebugStringA() and you'll get a list of the errors during compilation. This should be as standard as checking the return codes.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement