• Advertisement
Sign in to follow this  

Strange access violation problem

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Try adding:

if (FAILED(result2))
// ...


EDIT: Too slow...

Share this post


Link to post
Share on other sites
Well I added assertions there (should've done it earlier -__-) and it seems there's something wrong with this:


//set up Pixel Shader
result2 = 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..

Share this post


Link to post
Share on other sites
Well, which error did it give? It should be one of D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, or E_OUTOFMEMORY.

Share this post


Link to post
Share on other sites
How would I know? :DDDDD Yes, I'm so fucking noob I shouldn't exist but I do and please bear with it. :D

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You can use DXGetErrorDescription()/DXGetErrorString() in dxerr.h in your code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rasenger

//set up Pixel Shader
result2 = 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

Share this post


Link to post
Share on other sites
Yeah... The problem was in the ps code. When I replaced ps code with this simple thing:


float4 ps_main() : COLOR
{
return float4(0.5, 0.075, 0.075, 1.0);
}


It worked. So, now to fix my ****** shader code ->

And thanks for helping this stupid n00b.

Share this post


Link to post
Share on other sites
I don't want to start new topic so I write here.

I wrote simple vertex and pixel shaders with per pixel lighting and one directional light. Well, it works but lighting looks like per-vertex, not per-pixel (read: awful). I've tried to fix this whole day without success... Is there something wrong with my HLSL code or could the problem be elsewhere?


///////////////////
// VERTEX SHADER //
///////////////////

float4x4 matWorldViewProj : WORLDVIEWPROJECTION;
float4x4 matWorld;

struct VS_OUTPUT
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
float3 Normal : TEXCOORD1;
};

VS_OUTPUT vs_main(float4 Position : POSITION, float2 TexCoord : TEXCOORD0, float3 Normal : NORMAL)
{
VS_OUTPUT Out = (VS_OUTPUT) 0;

Out.Position = mul(Position, matWorldViewProj);
Out.TexCoord = TexCoord;
Out.Normal = Normal;

return Out;
}

///////////////////
// PIXEL SHADER //
//////////////////

sampler2D Tex0 = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};

float4 ps_main(float4 Position : POSITION, float2 TexCoord : TEXCOORD0, float3 Normal : TEXCOORD1) : COLOR0
{
float4 LightDir = {1.0f, 1.0f, 0.0f, 1.0f};

return tex2D(Tex0, TexCoord) * saturate(dot(LightDir, Normal));
}


Pic: http://img266.imageshack.us/img266/893/lolqe6.jpg

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement