e.s. kohen
e.s. kohen

Reputation: 303

DirectX/C++ How to Dereference Vector Data Array to Initialize D3D11 SUBRESOURCE DATA

I am trying to pass a pointer to an array of Vertex Data, (base class member) into a method used to initialize the Direct3D Vertex Buffer, (ID3D11Buffer).

I want to use the variable, "v4", the pointer to the array, but I think I am not dereferencing the pointer correctly. Could someone point me in the right direction?

I have tried many ways to make this assignment, but I can only get a basic array work, (sometimes I end up with casting to "const void *" errors).

How would I use a pointer?

The line I am having issue with is ... vertexSubResourceData.pSysMem = v1;

(p.s. Using the V.S. C++ Nov 2012 CTP compiler .. not released yet ..)

*Answered: Have confirmed that it is a Visual Studio 2012 C++ Nov. CTP Compiler Bug. Arrays of Vector structures declared, even outside of the class, using C++ 11 initialization syntax are being interpreted incorrectly, (unless of course I am using the incorrect array initialization syntax). Also Note: This only pertains to initialization lists associated with pointers. *

Thanks!

// Simplified Code

void Triangle::InitializeVertexBuffer()
{
    struct V
    {
        float X, Y, Z;    // vertex position
    };

    // Compiles But doesn't work as desired.
    // changed from V* v4 = new V[] // Compiled without range.
    // Pointer to this data would ideally come from base static pointer, or file.
    V* v4 = new V[3]     
    {
        { 0.50f, 0.5f, 0.0f },
        { 0.50f, -0.5f, 0.0f },
        { -0.50f, -0.5f, 0.0f },
    };

    // Compiles and Works to prove compiler issue with initialization lists. 
    V* vectorList = new V[3];
    vectorList[0].X = 0.5f;
    vectorList[0].Y = 0.5f;
    vectorList[0].Z = 0.00f;

    vectorList[1].X = 0.5f;
    vectorList[1].Y = -0.5f;
    vectorList[1].Z = 0.00f;

    vectorList[2].X = -0.5f;
    vectorList[2].Y = -0.5f;
    vectorList[2].Z = 0.00f;


    unsigned int size = sizeof(V) * 3; //sizeof(* Model::Vertices);

    // The Description of the Vertex Buffer
    D3D11_BUFFER_DESC vertexBufferDescription = {0};
    vertexBufferDescription.ByteWidth = size;
    vertexBufferDescription.BindFlags = D3D11_BIND_VERTEX_BUFFER; 

    // Model Data to be transferred to GPU Buffer.

    D3D11_SUBRESOURCE_DATA vertexSubResourceData = {0};
    vertexSubResourceData.SysMemPitch = 0;
    vertexSubResourceData.SysMemSlicePitch = 0;

    // Can't figure out how to dereference v4

    vertexSubResourceData.pSysMem = vectorList; // works, but using "v4" doesn't.

    // vertexSubResourceData.pSysMem = Model::Vertices; 
    // This is what I /really/ want .. Pointer to vertice array data from base class


    NS::DeviceManager::Device->CreateBuffer(
        &vertexBufferDescription, 
        &vertexSubResourceData, 
        &NS::ModelRenderer::VertexBuffer);
}

Upvotes: 0

Views: 1670

Answers (1)

Jesse Good
Jesse Good

Reputation: 52365

When you write V* v4 = new V[]{...}; it is illegal C++. The CTP compiler is accepting malformed code. Try V* v4 = new V[3]{...};

Checking the C++ standard, from 5.3.4p1:

[ expression ] attribute-specifier-seqopt

So, it looks like an expression is required in the []. I believe new V[] is an extension for compilers (still checking into this).

And as ildjarn points out, you want vertexSubResourceData.pSysMem = v4; as &4 would you give you the address of the pointer, not the array.

Update

I think this is a bug in the compiler. Try the old C++03 syntax: V* v4 = new V[3]; v4[0] = ...; and if that works, then it definitely is a bug.

Upvotes: 1

Related Questions