Cylindric
Cylindric

Reputation: 5894

Using a vector of structs to populate a DirectX D3D10_SUBRESOURCE_DATA

I have a simple struct that contains vertex information that will end up in a geometry shader:

struct SpriteVertex
{
    float topLeft[2];
    float dimensions[2];
    float opacity;  
};

I populate a std::vector<SpriteVertex> m_Sprites with various values in my Map class (numbers hard-coded in to simplify the example):

// the container is defined in the renderer.h:
std::vector<SpriteVertex>* m_SpriteList;


// the rest is the body of renderer.cpp:
SpriteVertex v;

v.topLeft[0] = 0;
v.topLeft[1] = 0;
v.dimensions[0] = 0.08;
v.dimensions[1] = 0.106667;
v.opacity = 1;
m_Sprites.push_back(v);

v.topLeft[0] = 0.08;
v.topLeft[1] = 0;
v.dimensions[0] = 0.08;
v.dimensions[1] = 0.106667;
v.opacity = 1;
m_Sprites.push_back(v);

v.topLeft[0] = 0.16;
v.topLeft[1] = 0;
v.dimensions[0] = 0.08;
v.dimensions[1] = 0.106667;
v.opacity = 1;
m_Sprites.push_back(v);

Then, later on in my renderer class, I populate a buffer with this data:

D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = &(m_SpriteList[0]);

D3D10_BUFFER_DESC bd;
bd.Usage = D3D10_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SpriteVertex)*(numSprites);
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;

pD3DDevice->CreateBuffer(&bd, &initData, &pVertexBuffer));

// Set vertex buffer
UINT stride = sizeof(SpriteVertex);
UINT offset = 0;
pD3DDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);

pColorMap->SetResource(pTexture1);
for(UINT p = 0; p < techDesc.Passes; p++)
{
    pTechnique->GetPassByIndex(p)->Apply(0);
    pD3DDevice->Draw(numSprites, 0);
}

And all I get is a blank screen.

If I replace the vector with a simple array, the sprites are drawn fine:

SpriteVertex verts[3];
verts[0].topLeft[0] = 0.0f;
verts[0].topLeft[1] = 0.0f;
verts[0].dimensions[0] = 0.08f;
verts[0].dimensions[1] = 0.106667;
verts[0].opacity = 1;

verts[1].topLeft[0] = 0.08f;
verts[1].topLeft[1] = 0.0f;
verts[1].dimensions[0] = 0.08f;
verts[1].dimensions[1] = 0.106667;
verts[1].opacity = 1;

verts[2].topLeft[0] = 0.16f;
verts[2].topLeft[1] = 0.0f;
verts[2].dimensions[0] = 0.08f;
verts[2].dimensions[1] = 0.106667;
verts[2].opacity = 1;    

D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = &verts;

Obviously I could just create a temp array and populate it with the values from my vector, or just use an array in the first place, but I am under the impression that I can access the 'traditional' array in a vector in this way.

Am I just misusing a pointer or reference somewhere?

Upvotes: 1

Views: 1569

Answers (1)

Eddie Edwards
Eddie Edwards

Reputation: 428

If you have a pointer std::vector<SpriteVertex>* m_SpriteList then you need to dereference the pointer first, before applying the [] operator, otherwise you just get the address of the pointer. In this case you need &((*m_SpriteList)[0]).

The type system can't help you here as you're writing to a void *. A good idea is to first write to a local pointer const SpriteVertex * verts = &m_vector[0] which then catches the mistake with the type system.

Upvotes: 1

Related Questions