Using 2 input slots and 2 vertex buffers

Started by
3 comments, last by Aqua Costa 11 years, 9 months ago
[color=#4A4A4A]I wanna send the position trough one input slot,and the color trough another input slot.Then i wanna create a vertex buffer for position,one for color,and use them.The problem is that I that the color doesn't get processed! The position gets there,but the color not,so everything remains black!

[source lang="cpp"] // Create the vertex input layout.
D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}
};

// Create the input layout
D3D10_PASS_DESC PassDesc;
mTech->GetPassByIndex(0)->GetDesc(&PassDesc);
HR(md3dDevice->CreateInputLayout(vertexDesc, 2, PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize, &mVertexLayout));


mNumVertices = 8;
mNumFaces = 7; // 2 per quad

// Create vertex buffer
Vertex vertices[] =
{
{D3DXVECTOR3(+1.0f, 0.0f, -1.0f), WHITE},
{D3DXVECTOR3(-1.0f, 0.0f, -1.0f), BLACK},
{D3DXVECTOR3(+1.0f, 0.0f, +1.0f), RED},
{D3DXVECTOR3(-1.0f, 0.0f, +1.0f), GREEN},
{D3DXVECTOR3(0.0f, 2.0f, 0.0f), BLUE}, //varf


{D3DXVECTOR3(+4.0f, 0.0f, -1.0f), WHITE},
{D3DXVECTOR3(+6.0f, 0.0f, -1.0f), BLACK},
{D3DXVECTOR3(+5.0f, 0.0f, +1.0f), RED},

};

// Scale the box.
for(DWORD i = 0; i < mNumVertices; ++i)
vertices.pos *= scale;


D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = vertices;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

D3D10_BUFFER_DESC vbd2;
vbd2.Usage = D3D10_USAGE_IMMUTABLE;
vbd2.ByteWidth = sizeof(Vertex) * mNumVertices;
vbd2.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd2.CPUAccessFlags = 0;
vbd2.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData2;
vinitData2.pSysMem = vertices;
HR(md3dDevice->CreateBuffer(&vbd2, &vinitData2, &mVB2));
// Create the index buffer

DWORD indices[] = {
1,0,2,
1,3,2,

0,4,1,
0,4,2,
4,2,3,
4,3,1,





5,6,7
};

D3D10_BUFFER_DESC ibd;
ibd.Usage = D3D10_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3;
ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));



ID3D10Buffer* g[] = {mVB,mVB2};

UINT stride = sizeof(Vertex);

UINT offset = 0;
md3dDevice->IASetVertexBuffers(0, 2, g, &stride, &offset);
md3dDevice->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0);
md3dDevice->DrawIndexed(mNumFaces*3 - 3, 0, 0);
md3dDevice->DrawIndexed(3,mNumFaces * 3 - 3, 0);[/source]
Advertisement
You have some errors in your code.

1-If you want data separated in two vertex buffers store it in different structures, in this case no need to use structures since each buffer only contains one type of data.
So separate position from color like this:


D3DXVECTOR3 verticesPos[] = { positions here };
D3DXCOLOR verticesColor[] = {colors here};



2-The code you're using to create the buffers is creating 2 buffers each containing both positions and colors. So replace to this:

D3D10_BUFFER_DESC vbd;

vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(D3DXVECTOR3) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = verticesPos;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

D3D10_BUFFER_DESC vbd2;
vbd2.Usage = D3D10_USAGE_IMMUTABLE;
vbd2.ByteWidth = sizeof(D3DXCOLOR) * mNumVertices;
vbd2.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd2.CPUAccessFlags = 0;
vbd2.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData2;
vinitData2.pSysMem = verticesColor;
HR(md3dDevice->CreateBuffer(&vbd2, &vinitData2, &mVB2));



3-If you're using multiple vertex buffers you have to pass an array of strides/offsets:


UINT strides[] = { sizeof(D3DXVECTOR3), sizeof(D3DXCOLOR) };

UINT offsets[] = {0,0};
md3dDevice->IASetVertexBuffers(0, 2, g, strides, offsets);

You fill both of the buffers with the same interleaved data (that contains both positions and colors). Since the vertex stride (byte width) is still correct in both buffers, the input assembler treats the first 16 bytes of each vertex found in the second vertex buffer as color (which is presumably not what you want).

Also, enable the debug output to see if you get warnings for invalid states. An invalid pixel shader can cause black results.

Niko Suni

Tiago,can you also tell me how to use structures like you said above?

[background=rgb(250, 251, 252)]How would this work with structures if I would have 2 vertex buffers and the following elements:[/background]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]position[/background][/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]normal[/background][/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]color[/background][/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]texcoord[/background][/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]So I would send position and normal trough 1 buffer,and color and texcoord in another buffer.[/background][/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Please show me how I would use the structures in this.[/background][/font]

Create 2 structures and fill them:

struct PosNormal
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
};


struct ColorTexC
{
D3DXCOLOR color;
D3DXVECTOR2 texC;
};

PosNormal positionNormals[] = { data here };
ColorTexC colorTexCoords[] = { data here };


Replace the size of the buffers and the strides

D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(PosNormal) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = positionNormals;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

D3D10_BUFFER_DESC vbd2;
vbd2.Usage = D3D10_USAGE_IMMUTABLE;
vbd2.ByteWidth = sizeof(ColorTexC) * mNumVertices;
vbd2.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd2.CPUAccessFlags = 0;
vbd2.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData2;
vinitData2.pSysMem = colorTexCoords;
HR(md3dDevice->CreateBuffer(&vbd2, &vinitData2, &mVB2));


UINT strides[] = { sizeof(PosNormal), sizeof(ColorTexC) };
UINT offsets[] = {0,0};
md3dDevice->IASetVertexBuffers(0, 2, buffers, strides, offsets);

This topic is closed to new replies.

Advertisement