0
\$\begingroup\$

I've been using the Graphics-Debugger of VS2012 to see why my triangle is not drawn. It seems to be a problem with my VertexBuffer since all other Buffers show the data I fed them, however, the VertexBuffer contains random data every time I restart the application:

The buffer format used is specified in the 'Graphics Object Table'.

---------------------------------------------------------------------------------------------------------------------------------
Types:  bool, int, xint (an unsigned hex int), uint (unsigned int), int64, xint64, uint64, 
        byte, xbyte, ubyte, 2byte,  x2byte, u2byte, 4byte, x4byte, u4byte, 8byte, x8byte, u8byte,  
        half (a 16-bit float), half2, half3, half4, float, float2, float3, float4, double
Usage:  'float' or 'float3 xint half half xint'
---------------------------------------------------------------------------------------------------------------------------------
Currently used format:  float
---------------------------------------------------------------------------------------------------------------------------------

0   [0x00000000-0x00000003] |       +0.070048094
1   [0x00000004-0x00000007] |    +3.0548307e-043
2   [0x00000008-0x0000000b] |       +0.069985747
3   [0x0000000c-0x0000000f] |    +3.0548307e-043
4   [0x00000010-0x00000013] |       +0.069986284
5   [0x00000014-0x00000017] |    +3.0548307e-043
6   [0x00000018-0x0000001b] |       +0.069986284
7   [0x0000001c-0x0000001f] |    +3.0548307e-043
8   [0x00000020-0x00000023] |       +0.070065737
9   [0x00000024-0x00000027] |    +3.0548307e-043
10  [0x00000028-0x0000002b] |                 +0
11  [0x0000002c-0x0000002f] |                 +0
12  [0x00000030-0x00000033] |                 +0
13  [0x00000034-0x00000037] |                 +0
14  [0x00000038-0x0000003b] |                 +0
15  [0x0000003c-0x0000003f] |                 +0
16  [0x00000040-0x00000043] |    -4.3159552e+008
17  [0x00000044-0x00000047] |    -4.3160208e+008

Or

0   [0x00000000-0x00000003] |           +1.#QNAN
1   [0x00000004-0x00000007] |    +1.2331426e-043
2   [0x00000008-0x0000000b] |           +1.#QNAN
3   [0x0000000c-0x0000000f] |    +1.2331426e-043
4   [0x00000010-0x00000013] |           +1.#QNAN
5   [0x00000014-0x00000017] |    +1.2331426e-043
6   [0x00000018-0x0000001b] |           +1.#QNAN
7   [0x0000001c-0x0000001f] |    +1.2331426e-043
8   [0x00000020-0x00000023] |           +1.#QNAN
9   [0x00000024-0x00000027] |    +1.2331426e-043
10  [0x00000028-0x0000002b] |                 +0
11  [0x0000002c-0x0000002f] |                 +0
12  [0x00000030-0x00000033] |                 +0
13  [0x00000034-0x00000037] |                 +0
14  [0x00000038-0x0000003b] |                 +0
15  [0x0000003c-0x0000003f] |                 +0
16  [0x00000040-0x00000043] |    -4.3159552e+008
17  [0x00000044-0x00000047] |    -4.3160208e+008

As you can see, the last 8 lines are identical (this is always the case) whereas the rest varies randomly. However, even the last 8 entries are not entirely correct.

My InputLayout is set correctly according to the debugger:

SemanticName SemanticIndex Format                      InputSlot AlignedByteOffset InputSlotClass              InstanceDataStepRate 
POSITION     0             DXGI_FORMAT_R32G32B32_FLOAT 0         0                 D3D11_INPUT_PER_VERTEX_DATA 0 
COLOR        0             DXGI_FORMAT_R32G32B32_FLOAT 0         12                D3D11_INPUT_PER_VERTEX_DATA 0 

Here is the Vertex-Struct

struct VERTEXPOSITIONCOLOR
{
    DirectX::XMFLOAT3   position;
    DirectX::XMFLOAT3   color;

    static D3D11_INPUT_ELEMENT_DESC* GetVertexLayout()
    {
        static D3D11_INPUT_ELEMENT_DESC layout[] =
        {
            { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,
                0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
            { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT,
                0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        };
        return layout;
    }

    static UINT GetNumElements(){ return 2; }
};

Here I fill my Vertex-Vector:

VERTEXPOSITIONCOLOR p;
p.position = XMFLOAT3( -1.0f, 0.0f, 0.0f );
p.color    = XMFLOAT3(  1.0f, 0.0f, 0.0f );
m_vVertices.push_back( p );


p.position = XMFLOAT3( 0.0f, 1.0f, 0.0f );
p.color    = XMFLOAT3( 0.0f, 1.0f, 0.0f );
m_vVertices.push_back( p );


p.position = XMFLOAT3(  1.0f, 0.0f, 0.0f );
p.color    = XMFLOAT3(  0.0f, 0.0f, 1.0f );
m_vVertices.push_back( p );

with m_vVertices being of type std::vector<VERTEXPOSITIONCOLOR>.

And here I create the VertexBuffer:

D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = static_cast<UINT>( m_vVertices.size() * GetStrideSize() );
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &m_vVertices; 
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;

HRESULT hr = m_spDevice->CreateBuffer( &bufferDesc, &InitData, &m_spVertexBuffer );

with bufferDesc.ByteWidth having the value 72 and hr having the value S_OK at the end. m_spVertexBuffer is of type CComPtr<ID3D11Buffer>.

Finally I call:

UINT Strides[1];
UINT Offsets[1];
Strides[0] = GetStrideSize(); 
Offsets[0] = 0;
pd3dImmediateContext->IASetVertexBuffers(0, 1, &m_spVertexBuffer.p, Strides, Offsets );

With Strides[0] having the value 24. Here I also set all other buffers.

So what did I do wrong with the VertexBuffer?

\$\endgroup\$
1
\$\begingroup\$

You don't want to set InitData.pSysMem to &m_vVertices. That is setting the system memory pointer to the address the vector object; you want to point instead to the actual data stored in the vector object:

InitData.pSysMem = &m_vVertices[0];
\$\endgroup\$
  • \$\begingroup\$ actually you'll want to use the m_vVertices.data() method, I find that to be clearer in its intend \$\endgroup\$ – ratchet freak Jan 11 '14 at 3:15
  • \$\begingroup\$ data would be clearer, but it's also C++11 only; the "&[0]" idiom is fairly prevalent if a bit ugly. You can also accomplish the same thing via various manipulations of front() and begin(). \$\endgroup\$ – Josh Jan 11 '14 at 17:41

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.