Sign in to follow this  

DX9c - Need some insight into debugging

This topic is 3313 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

Brace yourself for a long post. I'm testing out my engine with a simple vertex buffer but I cannot seem to get anything rendered. Everything gets setup correctly (shaders, vertex buffers, etc) and nothing returns an error code. I wrap all the basic D3D9 structures and classes in my own classes, and as I step through them, none of them return errors. I'm assuming everything goes fine. The shader I'm testing out is a simple one, hers is the source:
// +----------------------------------+
// |         Declared Globals         |
// +----------------------------------+

float4x4 matWorldViewProjection;


// +----------------------------------+
// |           Vertex Shader          |
// +----------------------------------+

struct VSOutput
{
	float4 position : POSITION;
};

// Vertex Shader
VSOutput VS(float4 position : POSITION)
{
	VSOutput OUT;

	// Multiply the Position of the Vertex By the 
	// World, View, and projection matrices
	OUT.position = mul(position, matWorldViewProjection);

	return OUT;
}


// +----------------------------------+
// |           Pixel Shader           |
// +----------------------------------+

struct PSOutput
{
	float4 color : COLOR;
};

// Pixel Shader
PSOutput PS(VSOutput IN)
{
    PSOutput OUT;
    
    // Create a Generic Color
    OUT.color = float4(1.0, 1.0, 1.0, 1.0);
    
    return OUT;
}


// +----------------------------------+
// |            Techniques            |
// +----------------------------------+

technique Simple 
{
    pass P0
    {       
        VertexShader = compile vs_3_0 VS();
        PixelShader = compile ps_3_0 PS();
    }
}

I create the vertex buffer using CreateVertexBuffer() and CreateVertexDeclaration(), so there is nothing special in the code. All of that is wrapped inside the cube class' create() function. I initialize the shader and pass in my WorldProjView matrix into it.
// Creates a Vertex buffer & vertex declaration, etc. Everything goes OK.
cube->create(renderer);

// Creates a View matrix (Wraps D3DXMatrixPerspectiveFovLH)
cam->lookAt(Vector(0, 0, -5), Vector(0, 1, 0), Vector(0, 0, 0));

// Sets the Shader variable (Wraps ID3DXBaseEffect::SetMatrix)
shader->setConstant("matWorldViewProjection", ShaderVar(cube->getWorldPose() * cam->getViewMatrix() * cam->getProjectionMatrix()));

// User a technique in the shader
shader->setTechnique("Simple");


The projection matrix is already calculated when the camera is instantiated. The math is fine because I tested it against the D3DXMATRIX functionality and the results are the same. When I render the scene the following code happens:
if (FAILED(device->Clear(NULL, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0xff,0x80,0x00,0x80), 1.0f, NULL)))
{
  return false;
}

// Start the Shader (Wraps ID3DXEffect::Begin)
int numPasses = curShader->begin();

// Apply the constants that were set (Wraps ID3DXEffect::CommitChanges)
curShader->applyChanges();

// Grab the vertex buffer form our cube
vBuf = cube->getVertexBuffer();

for (int k = 0; k < numPasses; ++k)
{
  // Start the shader pass (Wraps ID3DXEffect::BeginPass)
  curShader->beginPass(k);

  if (SUCCEEDED(device->SetStreamSource(0, vBuf, 0, cube->vrtxDeclSz)))
  {
    // This is a declaration that only has a D3DDECLUSAGE_POSITION value
    if (SUCCEEDED(device->SetVertexDeclaration(vBuf->vrtxDecl)))
    {
      if (FAILED(device->DrawPrimitive(D3DPT_TRIANGLELIST), 0, vBuf->getNumVertices())))
      {
        LOG("Failed To Render Primitive.");
      }
    }
  }

  // Finish the shader pass (Wraps ID3DXEffect::EndPass)
  curShader->endPass();
}

// Wraps ID3DXEffect::End
curShader->end();

// Present to the screen
device->Present(NULL, NULL, NULL, NULL);

When I run this, I get the screen being cleared properly to the color desired, and everything is being properly presented onto the screen. No errors occur. Using PIX, I get the following output:
IDirect3DDevice9::TestCooperativeLevel()
IDirect3DDevice9::Clear(0x00000000, NULL, 0x00000001, D3DCOLOR_ARGB(0xff,0x80,0x00,0x80), 1.000f, 0x00000000)
IDirect3DDevice9::TestCooperativeLevel()
IDirect3DDevice9::BeginScene()
IDirect3DDevice9::SetVertexShader(0x032632E0)
IDirect3DDevice9::SetVertexShaderConstantF(0, 0x01CE0170, 4)
IDirect3DDevice9::SetPixelShader(0x032631F8)
IDirect3DDevice9::SetStreamSource(0, 0x032A2FC8, 0, 16)
IDirect3DDevice9::SetVertexDeclaration(0x03262C68)
IDirect3DDevice9::DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6)
IDirect3DDevice9::TestCooperativeLevel()
IDirect3DDevice9::EndScene()
IDirect3DDevice9::TestCooperativeLevel()
IDirect3DDevice9::Present(NULL, NULL, NULL, NULL)

The Vertex Buffer looks like this:
0	1.000
1	0.000
2	0.000
3	0.000
4	0.000
5	1.000
6	0.000
7	0.000
8	-1.000
9	0.000
10	0.000
11	0.000
12	-1.000
13	0.000
14	0.000
15	0.000
16	0.000
17	1.000
18	0.000
19	0.000
20	1.000
21	0.000
22	0.000
23	0.000

and the Vertex declaration is this:
0 0 FLOAT4 DEFAULT POSITION 0 
D3DDECL_END     

I honestly Don't know where the problem is and since this is the first time I'm actually using shaders, I am lost as to where to look or how to debug. Any help is appreciated. Thanks in advance.

Share this post


Link to post
Share on other sites
You're trying to draw 6 triangles, but there are only 2 in the vertex buffer. Maybe that's the problem. Also, have you turned on the debug D3D libraries and set the debug level to max?

Share this post


Link to post
Share on other sites
The control panel comes with dx sdk, it's dxcpl.exe.
Oh as f8k8 said, the last parameter in DrawPrimitive is the primitive count, with TRIANGLELIST it's the vertex_count / 3.
But there should still be 2 triangles on screen, so you could try with cullmode set to none.

Share this post


Link to post
Share on other sites
Quote:
Original post by KOzymandias
Oh as f8k8 said, the last parameter in DrawPrimitive is the primitive count, with TRIANGLELIST it's the vertex_count / 3.
But there should still be 2 triangles on screen, so you could try with cullmode set to none.


I set the cullmode to None, and fixed the bug (good eye). I cranked up the debugging on DirectX and there is a breakpoint triggered inside IDirect3DVertexBuffer9::Lock(). The output tells me: "Can specify D3DLOCK_DISCARD or D3DLOCK_NOOVERWRITE for only Vertex Buffers created with D3DUSAGE_DYNAMIC."

I am confused. The only time I can fill the buffer is if i specify D3DUSAGE_DYNAMIC?

Also, fixing this problem does not get anything to render. PIX is telling me that the vertex buffer is correct and has data in it. Is there a way to step through a shader?

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
Lock with a flag of 0 for static buffers.

Is there a reason you're using float4 position instead of float3? Is there a reason that W is 0? That's going to ruin lots of calculations, as it's typically 1.


Ah alright, this solves my problem. I changed it to float3 and it works fine now!

Thanks everyone for all your help.

Share this post


Link to post
Share on other sites

This topic is 3313 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this