0
\$\begingroup\$

I'm getting some wonky geometry in my application when trying to use instancing. I'm not sure if I'm doing the mapping of my per instance data wrong. This is how my mapping looks like:

XMMATRIX scale          = XMMatrixScaling( 1.0f, 1.0f, 1.0f );
XMMATRIX rotation       = XMMatrixRotationY( mRotation * 10.0f );
XMMATRIX translation    = XMMatrixTranslation( 0.0f, 0.0f, 0.0f );

XMMATRIX finalTransform = scale * rotation * translation;

XMFLOAT4X4 world;

XMStoreFloat4x4( &world, XMMatrixTranspose( finalTransform ) );
mBox->PerInstanceData( world );

D3D11_MAPPED_SUBRESOURCE mappedResource;
hr = mDeviceContext->Map( mPerInstanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );

if( SUCCEEDED( hr ) )
{
    memcpy( mappedResource.pData, &mBox->PerInstanceData(), sizeof(PerInstanceData) );
    mDeviceContext->Unmap( mPerInstanceBuffer, 0 ); 
}

I've constructed my input layout like this:

D3D11_INPUT_ELEMENT_DESC inputDescInstanced[] = {                
            { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,  0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
            { "NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
            { "WORLD",    0, DXGI_FORMAT_R32G32B32_FLOAT, 1,  0, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
            { "WORLD",    1, DXGI_FORMAT_R32G32B32_FLOAT, 1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
            { "WORLD",    2, DXGI_FORMAT_R32G32B32_FLOAT, 1, 32, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
            { "WORLD",    3, DXGI_FORMAT_R32G32B32_FLOAT, 1, 48, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
            { "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 1, 64, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
        };

And this is how a part of my shader looks like:

struct VertexIn
{
    float3              position    : POSITION;
    float3              normal      : NORMAL;

    // Instanced data
    row_major float4x4  world       : WORLD;
    float4              color       : COLOR;
    uint                instanceID  : SV_InstanceID;
};

PixelIn VS( VertexIn input )
{
    PixelIn output = (PixelIn)0;

    output.position = mul( float4( input.position, 1.0f ), input.world );
    output.position = mul( output.position, view );
    output.position = mul( output.position, proj );

    output.normal   = normalize( mul( input.normal, (float3x3)input.world ) );
    output.color    = input.color;

    return output;
}

The final output of the cube looks like this:

enter image description here

Any suggestions? Thank you!

\$\endgroup\$
3
  • \$\begingroup\$ DXGI_FORMAT_R32G32B32_FLOAT is not 16 bytes, that's the first problem after a quick look. Either declare it 12 in your layout or specify D3D11_APPEND_ALIGNED_ELEMENT instead. Please check if it changes the result. \$\endgroup\$ Jul 14, 2015 at 16:45
  • \$\begingroup\$ @JánosTuránszki - I changed the format and it still was wierd but then I realized I'm using a 4x4 matrix so I changed the format to DXGI_FORMAT_R32G32B32A32_FLOAT and it solved the problem! Please post your comment as an answer because you deserve a +1 on this one! Super thankful! :) \$\endgroup\$
    – SvinSimpe
    Jul 14, 2015 at 17:16
  • \$\begingroup\$ I'm glad I could help and welcome to the site! \$\endgroup\$ Jul 14, 2015 at 17:30

1 Answer 1

3
\$\begingroup\$

DXGI_FORMAT_R32G32B32_FLOAT is not 16 bytes, that's the first problem after a quick look. Either declare it 12 in your layout or specify D3D11_APPEND_ALIGNED_ELEMENT instead. Or Make it DXGI_FORMAT_R32G32B32A32_FLOAT instead depending on your matrix.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .