About input slot and vertex buffer

Started by
3 comments, last by bluepig.man 10 years, 11 months ago

Hey,guys.

l am study Directx 11.And recently,l'm studying vertex buffer.But l got a problem about it.

The problem is, when l split position and color info into 2 vertex buffer and use 2 input slot(1 vertex buffer put in 1 input slot ),

but the cube's color is difference with the code use 1 input slot and 1 vertex buffer(The position and color info put in one vertex).

And then l also try using 1 input slot but using 2 vertex buffer,the cube's color difference with the first two cube.

The index buffer is same between this three cube.So why this happen?

Sorry if have any mistakes,my English level is poor.

Thanks!

Advertisement

Can you post the input layout that you use in both cases? Also, please post the vertex shader that you are using in both cases too - this should help us to find what is happening.

One final thing - can you describe in more detail what is the difference in the new method that doesn't look correct?

Oh,sorry. l forgot that, thanks for your remind Tthis is the vertex shader,and three situation shared it.


?cbuffer cbPerObject
{
    float4x4 gWorldViewProj;
};

struct VertexIn
{
    float3 Pos   : POSITION;
    float4 Color : COLOR;
};

struct VertexOut
{
    float4 PosH  : SV_POSITION;
    float4 Color : COLOR;
};

VertexOut VS(VertexIn vin)
{
    VertexOut vout;

    // Transform to homogeneous clip space.
    vout.PosH = mul(float4(vin.Pos, 1.0f), gWorldViewProj);
	
    // Just pass vertex color into the pixel shader.
    vout.Color = vin.Color;
    
    return vout;
}

float4 PS(VertexOut pin) : SV_Target
{
    return pin.Color ;//* sin(0.4f * gTime);
}

technique11 ColorTech
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
	SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
}

And then there is a namespace named Colors,it used to store color info.


namespace Colors
{
	XMGLOBALCONST XMVECTORF32 White     = {1.0f, 1.0f, 1.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Black     = {0.0f, 0.0f, 0.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Red       = {1.0f, 0.0f, 0.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Green     = {0.0f, 1.0f, 0.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Blue      = {0.0f, 0.0f, 1.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Yellow    = {1.0f, 1.0f, 0.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Cyan      = {0.0f, 1.0f, 1.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Magenta   = {1.0f, 0.0f, 1.0f, 1.0f};
	XMGLOBALCONST XMVECTORF32 Silver    = {0.75f, 0.75f, 0.75f, 1.0f};
	XMGLOBALCONST XMVECTORF32 LightSteelBlue = {0.69f, 0.77f, 0.87f, 1.0f};
}

Situation 1: use one input slot and one vertex buffer.

it's the input layout:


D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};

The vertex buffer and indices buffer:


Vertex vertices[] =
{
        //Cube vertex

        { XMFLOAT3(-1.0f, -1.0f, -1.0f), (const float*)&Colors::White },
        { XMFLOAT3(-1.0f, +1.0f, -1.0f), (const float*)&Colors::Black },
        { XMFLOAT3(+1.0f, +1.0f, -1.0f), (const float*)&Colors::Red },
        { XMFLOAT3(+1.0f, -1.0f, -1.0f), (const float*)&Colors::Green },
        { XMFLOAT3(-1.0f, -1.0f, +1.0f), (const float*)&Colors::Blue },
        { XMFLOAT3(-1.0f, +1.0f, +1.0f), (const float*)&Colors::Yellow },
        { XMFLOAT3(+1.0f, +1.0f, +1.0f), (const float*)&Colors::Cyan },
        { XMFLOAT3(+1.0f, -1.0f, +1.0f), (const float*)&Colors::Magenta },
};

D3D11_BUFFER_DESC vbd;
vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * 13;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = vertices;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB));

UINT indices[] = {
    // front face
    0, 1, 2,
    0, 2, 3,

    // back face
    4, 6, 5,
    4, 7, 6,

    // left face
    4, 5, 1,
    4, 1, 0,

    // right face
    3, 2, 6,
    3, 6, 7,

    // top face
    1, 5, 6,
    1, 6, 2,

    // bottom face
    4, 0, 3, 
    4, 3, 7,

};

D3D11_BUFFER_DESC ibd;
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(UINT) * 36;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mBoxIB));

The indices buffer also shared by three situation.

Then the code bind vertex buffer and indices buffer.


    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVB, &stride, &offset);
    md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);

In this situation,every surface have 4 colors.

Situation 2 : use two input slot and two vertex buffer.

The inputlayout is changed:


D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};

This is the two vertexbuffer.


	// Create vertex buffer
    Vertex vertices[] =
    {
		//Cube vertex

		{ XMFLOAT3(-1.0f, -1.0f, -1.0f)  },
		{ XMFLOAT3(-1.0f, +1.0f, -1.0f)  },
		{ XMFLOAT3(+1.0f, +1.0f, -1.0f)  },
		{ XMFLOAT3(+1.0f, -1.0f, -1.0f), (const float*)&Colors::Green   },
		{ XMFLOAT3(-1.0f, -1.0f, +1.0f), (const float*)&Colors::Blue    },
		{ XMFLOAT3(-1.0f, +1.0f, +1.0f), (const float*)&Colors::Yellow  },
		{ XMFLOAT3(+1.0f, +1.0f, +1.0f), (const float*)&Colors::Cyan    },
		{ XMFLOAT3(+1.0f, -1.0f, +1.0f), (const float*)&Colors::Magenta },

		//Pyramid vertex
/*
		{ XMFLOAT3( 0.0f,  2.0f,  0.0f), (const float*)&Colors::Red   },
		{ XMFLOAT3(-2.0f, -2.0f, -2.0f), (const float*)&Colors::Green },
		{ XMFLOAT3(+2.0f, -2.0f, -2.0f), (const float*)&Colors::Green },
		{ XMFLOAT3(-2.0f, -2.0f, +2.0f), (const float*)&Colors::Green },
		{ XMFLOAT3(+2.0f, -2.0f, +2.0f), (const float*)&Colors::Green },
		*/
    };

    D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_IMMUTABLE;
    vbd.ByteWidth = sizeof(Vertex) * 8;
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    vbd.StructureByteStride = 0;
    D3D11_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = vertices;
    HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB));	

    Color my_color[] = {

	{ (const float*)&Colors::White	 },
	{ (const float*)&Colors::Black	 },
	{ (const float*)&Colors::Red	 },
	{ (const float*)&Colors::Green   },
	{ (const float*)&Colors::Blue    },
	{ (const float*)&Colors::Yellow  },
	{ (const float*)&Colors::Cyan    },
	{ (const float*)&Colors::Magenta },

   };

    D3D11_BUFFER_DESC vbd2;
    vbd2.Usage = D3D11_USAGE_IMMUTABLE;
    vbd2.ByteWidth = sizeof(Vertex) * 5;
    vbd2.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd2.CPUAccessFlags = 0;
    vbd2.MiscFlags = 0;
    vbd2.StructureByteStride = 0;
    D3D11_SUBRESOURCE_DATA vinitData2;
    vinitData2.pSysMem = vertices;
    HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB2));

Then bind vertex buffer


    UINT stride2 =sizeof(Color);
    md3dImmediateContext->IASetVertexBuffers(1, 1, &mBoxVB2, &stride2, &offset);

And then color of the shape is different with before.Some surface have five colors.But some surface just have three colors.

Situation 3 : use two vertex buffer and one input slot.

The two vertex buffer and index buffer is same with the situation 2.


D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};

The code bind two buffer.


    ID3D11Buffer *mBoxvb[] = { mBoxVB,mBoxVB2 };
    UINT stride[] = { sizeof(Vertex), sizeof(Color) };
    UINT offset[] = { 0, 0 };
    md3dImmediateContext->IASetVertexBuffers(0, 2, mBoxvb, stride, offset);

In this situation the color are block-shaped.

And in situaion 1 and 2,the color are radial.

You should use situation 2's input layout, but use the code binding from situation 3. I suspect that the strides are incorrect in situation 2, since you should have two different strides but you are only specifying one. Try this combination, and see how it turns out.

Thanks, it's work.biggrin.png

This topic is closed to new replies.

Advertisement