Sign in to follow this  
iota1410

vertex shader help

Recommended Posts

iota1410    122
i have followed msdn's vertex shader tutorials and been able to successfully compile my code. i am using two buffers. one with x,y,z,rhw and another with color data. when i run it, however, the window pops up, but the picture doesn't show up. i think that my .vsh file might not be correct. if anyone has any examples they could share, i would really appreciate it. here is what mine looks like right now: ------------------- vs_2_0 dcl_position v0 dcl_color v1 mov oPos, v0 mov oD0, v1 ------------------- my render function contains: IDirect3DDevice9_SetVertexDeclaration(g_pd3dDevice, m_pVertexDeclaration ); IDirect3DDevice9_SetVertexShader(g_pd3dDevice, m_pVertexShader ); IDirect3DDevice9_SetStreamSource(g_pd3dDevice, 0, g_pV, 0, sizeof(struct vertex) ); IDirect3DDevice9_SetStreamSource(g_pd3dDevice, 1, g_pC, 0, sizeof(struct pixel) ); IDirect3DDevice9_DrawPrimitive(g_pd3dDevice, D3DPT_TRIANGLELIST, 0, 1 ); in the end nothing is draw in my window. [Edited by - iota1410 on November 9, 2004 2:39:04 PM]

Share this post


Link to post
Share on other sites
vajuras    139
what result did the example state? looking at your code, all it does is copy the input pos into the output register. not sure what that last line accomplishes, looks like a typo. it should be:

mov oD0, v1

this will copy the input color into the output diffuse register.

Share this post


Link to post
Share on other sites
iota1410    122
it was a typo, it should have been

mov oD0, v1

I want to draw a triangle at the position in the vertex buffer, using the colors in the color buffer. The position of my vertices never changes, only their colors. The hope is all I have to do is update my color buffer and then render it to the screen.

Currently all I get is my blue background.

I can get my triangle to appear using the FVF, but I would like to try using a shader.

Share this post


Link to post
Share on other sites
vajuras    139
alright so the shader itself is fine. you only need one stream source. so why this line:

IDirect3DDevice9_SetStreamSource(g_pd3dDevice, 1, g_pC, 0, sizeof(struct pixel) );

not sure what is happening there. Also make sure your vertex declaration is properly set and that you are running in DirectX debug mode D3D_DEBUG_INFO (something like that).

Share this post


Link to post
Share on other sites
iota1410    122
The way I understood streams, the first stream is the vertex buffer with the position info, the second stream is the color info. I got the code off of the Directx SDK. But they never show how the buffers were built in that particular case. Any Ideas?

Share this post


Link to post
Share on other sites
davidx9    184
Double check your vertex declaration. Also you can get the vsh compile errors at run time using this


// Create a temp buffer for shader debugging
LPD3DXBUFFER err;
D3DXCreateBuffer(1024, &err);

//Load effect file
if(FAILED(D3DXCreateEffectFromFile(m_pd3dDevice, "vs_terrain.vsh",
NULL, NULL, 0, NULL, &m_fxTerrain, &err )))
{
char error[1024];
memcpy(error, err->GetBufferPointer(), 1024);
MessageBox(NULL, error, "Error", 0);
return E_FAIL;
}

Share this post


Link to post
Share on other sites
iota1410    122
My declarations are as follows:

#define D3DFVF_VERTEX (D3DFVF_XYZRHW)
#define D3DFVF_PIXEL (D3DFVF_DIFFUSE)
...
...
...
if( FAILED( IDirect3DDevice9_CreateVertexBuffer(g_pd3dDevice, 3*sizeof(struct vertex), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_pV, NULL ) ) )
{
return E_FAIL;
}

if( FAILED( IDirect3DDevice9_CreateVertexBuffer(g_pd3dDevice, 3*sizeof(struct pixel), 0, D3DFVF_PIXEL, D3DPOOL_DEFAULT, &g_pC, NULL ) ) )
{
return E_FAIL;
}

pixel only contains a color value and vertex contains the position data.

Share this post


Link to post
Share on other sites
davidx9    184
When combining streams using a vertex shader, you need to order the data in a way that can be pushed through the pipeline and taken apart by your shader. You can use a Vertex Declaration to do this.

This example shows how to combine a stream of height and normal values with a stream of x and z verts with texture coords



static D3DVERTEXELEMENT9 vertex_description[] =
{
{0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 8, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{0, 16,D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},

{1, 0, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 1},
{1, 4, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
D3DDECL_END()
};



This is an example of how one is defined, but not a solution to your problem.

You then need to create a D3D object from the description

m_pd3dDevice->CreateVertexDeclaration(vertex_description, &vertex_dec)

Then your shader has to arange the input properly, like


struct VS_INPUT
{
float2 Pos : POSITION0;
float2 UV : TEXCOORD0;
float2 UV2 : TEXCOORD1;
float YPos : POSITION1;
float3 norm: NORMAL0;
};



I would give a more useful example implementation but i don't know off the top of my head if it would be correct. The SDK has an average tutorial on this, but there are some really good old Gamedev threads that you can use.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I use the following declaration. I got the code from the SDKs.

D3DVERTEXELEMENT9 decl[] = {
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR, 0 },
D3DDECL_END()};

IDirect3DDevice9_CreateVertexDeclaration(g_pd3dDevice, decl, &m_pVertexDeclaration );

What else am I missing? I really thaought this would be simple. I am not even trying to manipulate the data at this point.

Share this post


Link to post
Share on other sites
vajuras    139
that vertex declaration doesn't look right. 4 floats are 16 bytes so:

D3DVERTEXELEMENT9 decl[] = {
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 16, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR, 0 },
D3DDECL_END()};

Share this post


Link to post
Share on other sites
davidx9    184
Quote:
Original post by vajuras
that vertex declaration doesn't look right. 4 floats are 16 bytes so:

D3DVERTEXELEMENT9 decl[] = {
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 16, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR, 0 },
D3DDECL_END()};


I don't believe this is accurate, cos you need to identify both streams. Thats what the first value is for isn't it?

Share this post


Link to post
Share on other sites
iota1410    122
Well if thats not the problem, what else could I be missing. I got the majority of my code from the DirectX 9.0 SDK article Colorvertex Shader. The only major difference that I see is that I wrote my program in c instead of c++. If any part of my code would be helpful in determining my problem, please let me know.

Share this post


Link to post
Share on other sites
vajuras    139
Quote:
Original post by davidx9
I don't believe this is accurate, cos you need to identify both streams. Thats what the first value is for isn't it?


that's right he is using multiple streams, something I know nothing bout. Sorry bout that.

Share this post


Link to post
Share on other sites
iota1410    122
figured out the problem, I was tring to send a VB of type XYZRHW. This type can only be sent in one stream, not multiple. I have the shader working, just need to figure out how to get my data formatted properly, but that is a question for another thread.

Share this post


Link to post
Share on other sites

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