0
\$\begingroup\$

I'm having problems with the Input Assembler.

The first time the data is passed through the pipeline, everything is fine.
But, when the index buffer (with a diffrent/higher index than before) refers to the same vertex in the first vertex buffer, then the other vertex buffers will pass the same data again.

Is it possible to change this behavior?

Input Assembler Stage

Here you can see that the last two vertices have the same data at obj15 and obj17 (obj 19 is the Instance data. This is only one instance) like the 2nd and 3rd vertices. But if you follow the setup below, then the last two should be:
0   4   2      1   -1   -1      1   1   0
0   5   1      -1   1   -1      2   1   0



The setup:

1 index buffer.
4 vertex buffers.
2 constant buffers.


Data for the first Constant buffer (Texture coordinates (float2)):

0.0 0.0 (top left) (UV 0)
1.0 0.0 (top right) (UV 1)
0.0 1.0 (bottom left) (UV 2)
1.0 1.0 (bottom right) (UV 3)


Secound Constant buffer (Normals (float3)):

0.0 0.0 -1.0 (Normal 0)
1.0 0.0 0.0 (Normal 1)
0.0 0.0 0.0 (not relevant) (Normal 2)
0.0 0.0 0.0 (not relevant) (Normal 3)


Data for the first Vertex buffer (Position coordinates (float3)):

-1.0 -1.0 -1.0 (bottom left) (Vertex 0)
-1.0 1.0 -1.0 (top left) (Vertex 1)
1.0 -1.0 -1.0 (bottom right) (Vertex 2)
1.0 1.0 -1.0 (top right) (Vertex 3)


Secound Vertex buffer (index of the Texture data (uint)) (one index per vertex)

0 1 2 (for Face 0)
0 1 2 (for Face 1)


Third Vertex buffer (index of the Normal data (uint)) (one index per face)

0 (for face 0)
1 (would be 0, but for now it is 1) (for face 1)

Fourth Vertex buffer (index of the Instance data (uint))
(irrelevant for this topic)

Data for the Index buffer:

0 1 2 (face 0)
3 2 1 (face 1)



Program Code:

vertex buffers:

vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m->buffer.buffer_info.vertex_count;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //0;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;

vertexData.pSysMem = m->buffer.buffer_info.vertex_type;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;

result = g.d3d.device->CreateBuffer(&vertexBufferDesc, &vertexData, &m->buffer.vertex_buffer);
if (FAILED(result)) return false;


(flags and stride are the same and they also have a data pointer)

texture_indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
texture_indexBufferDesc.ByteWidth = sizeof(TextureIndexType) * m->buffer.buffer_info.texture_index_count;
texture_indexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //0;


normal_indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
normal_indexBufferDesc.ByteWidth = sizeof(NormalIndexType) * m->buffer.buffer_info.normal_index_count;
normal_indexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //0;


index buffer:

indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(IndexType) * m->buffer.buffer_info.index_count;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;


Input Layout:

D3D11_INPUT_ELEMENT_DESC polygonLayout[4];
// Create the vertex input layout description.

polygonLayout[0].SemanticName = "POSITION";
polygonLayout[0].SemanticIndex = 0;
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[0].InputSlot = 0;
polygonLayout[0].AlignedByteOffset = 0;
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[0].InstanceDataStepRate = 0;

polygonLayout[1].SemanticName = "TEXCOORD";
polygonLayout[1].SemanticIndex = 0;
polygonLayout[1].Format = DXGI_FORMAT_R32_UINT;
polygonLayout[1].InputSlot = 1;
polygonLayout[1].AlignedByteOffset = 0;
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0;

polygonLayout[2].SemanticName = "NORMAL";
polygonLayout[2].SemanticIndex = 0;
polygonLayout[2].Format = DXGI_FORMAT_R32_UINT;
polygonLayout[2].InputSlot = 2;
polygonLayout[2].AlignedByteOffset = 0;
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[2].InstanceDataStepRate = 0;

polygonLayout[3].SemanticName = "POSITION";
polygonLayout[3].SemanticIndex = 1;
polygonLayout[3].Format = DXGI_FORMAT_R32_UINT;
polygonLayout[3].InputSlot = 3;
polygonLayout[3].AlignedByteOffset = 0;
polygonLayout[3].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[3].InstanceDataStepRate = 1;


Vertex Shader:

#ifndef NUMBER_TEXTURE
    #define NUMBER_TEXTURE 256      //changes with each model
    #define NUMBER_NORMALS 256      //changes with each model
    #define NUMBER_INSTANCE 256     //stays the same for all models
#endif


cbuffer TextureBuffer : register(b0) {
    //float4 texture_type[(int)((float)NUMBER_TEXTURE / 2.0f) + 1];             //will be converted to a float2 array
    float4 texture_type[2];
};

cbuffer NormalBuffer : register(b1) {
    float4 normal_type[3];
    //float4 normal_type[(int)((float)NUMBER_NORMALS / 3.0f * 4.0f) + 1];           //will be converted to a float3 array
    //float4 NormalBuffer_padding;
};

cbuffer InstanceBuffer : register(b2) {
    matrix Instance[NUMBER_INSTANCE];
};

cbuffer MatrixBuffer : register(b3) {
    matrix view;
    matrix projection;
};

cbuffer CameraBuffer : register(b4) {
    float3 cameraPosition;
    float CameraBuffer_padding;
};

struct VertexInputType
{
    float4 position : POSITION0;
    uint tex : TEXCOORD0;
    uint normal : NORMAL0;
    uint instance : POSITION1;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    //float3 normal : NORMAL;
    //float3 viewDirection : TEXCOORD1;
};


    PixelInputType main(VertexInputType input) {
    PixelInputType output;
    float4 worldPosition;

    //float2 Texture[NUMBER_TEXTURE] = (float2[NUMBER_TEXTURE]) texture_type;
    //float3 Normals[NUMBER_NORMALS] = (float3[NUMBER_NORMALS]) normal_type;
    float2 Texture[4] = (float2[4]) texture_type;
    //float3 Normals[4] = (float3[4]) normal_type;


    matrix world = Instance[input.instance];

    // Calculate the position of the vertex in the world.
    worldPosition = mul(input.position, world);

    output.position = worldPosition;
    output.position = mul(output.position, view);
    output.position = mul(output.position, projection);

    output.tex = Texture[input.tex];



    //output.normal = Normals[input.normal];
    //output.normal = mul(output.normal, (float3x3)world);
    //output.normal = normalize(output.normal);

    // Determine the viewing direction based on the position of the camera and the position of the vertex in the world.
    //output.viewDirection = cameraPosition.xyz - worldPosition.xyz;

    // Normalize the viewing direction vector.
    //output.viewDirection = normalize(output.viewDirection);

    return output;
}


if you are wondering, I was just playing around with padding a bit, but for now I left it away to reduce possible sources of errors. And the #Ifndef is just a trick to compile with visual studio, but then later insert macros with D3DX11CompileFromFile.

And finally the pixel shader.

Texture2D shaderTexture;
SamplerState SampleType;


//cbuffer LightBuffer
//{
    //  float4 ambientColor;
    //  float4 diffuseColor;
    //  float3 lightDirection;
    //  float specularPower;
    //  float4 specularColor;
//};


struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    //float3 normal : NORMAL;
    //float3 viewDirection : TEXCOORD1;
};

float4 main(PixelInputType input) : SV_TARGET
{
    float4 textureColor;
    float3 lightDir;
    float lightIntensity;
    float4 color;
    float3 reflection;
    float4 specular;

    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTexture.Sample(SampleType, input.tex);
    //return textureColor;

    return float4(input.tex.x, 0.0f, input.tex.y, 1.0f);
}



And to be sure that the Texture-vertex and index buffer is correct: enter image description here

\$\endgroup\$
0
\$\begingroup\$

Yeah, the answer is quite simple if you know the mistake.

So, the problem is that the index in the index buffer refers to an element in all of the vertex buffers.

For example, lets take a value from the index buffer, say 2 (one from question) and it would point to the third element in the vertex-buffer, the third element in the coord-buffer and also to the third element in the third element in the normal-buffer.

Solution:

replace the vertex-coord-buffer with the index buffer.
create another constant buffer with the vertex coordinates in it.
delete the index buffer.

call DrawInstanced(...) instead of DrawIndexedInstanced(...).

or just fill up the index buffer with 0 to the number of elements in the new index-vertex-buffer.
(all vertex buffers must have the same number of elements).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.