0
votes

I have started using DirectX with C++. I am trying to draw multiple rectangles and triangles on the screen and trying to move them. I am storing the vertices in the Vertex buffer once and drawing. Then I am calling DrawPrimitives multiple time to draw the rectangle. I have seen this thing from How to Draw Two Detached Rectangles in DirectX using the D3DPT_TRIANGLESTRIP Primitive Type. The problem is that when I check the memory consumption of this program from Visual Studios "Performance Profiler" it starts from 26MB and start to grow one by one like 27,28,29,30....... If I remove the DrawPrimitive function the memory consumption in Visual Studio remains constant. I dont know what I am doing wrong? Should the memory consumption is fixed when multiple triangles and rectangles are drawn on screen? My code is:

void render_frame()
{
    //ourVertices conntain multiple rectangle
    LPDIRECT3DVERTEXBUFFER9 v_buffer;
    d3ddev->CreateVertexBuffer(numberofrectangles * sizeof(ourVertices),
        0,
        CUSTOMFVF,
        D3DPOOL_MANAGED,
        &v_buffer,
        NULL);
    VOID* pVoid;    // the void* we were talking about

    v_buffer->Lock(0, 0, (void**)&pVoid, 0);    // locks v_buffer, the buffer we made earlier
    memcpy(pVoid, ourVertices, numberofrectangles * sizeof(ourVertices));
    v_buffer->Unlock();

    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();

    // select which vertex format we are using
    // select the vertex buffer to display
    d3ddev->SetFVF(CUSTOMFVF);
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(Point));
    for (int i = 0; i< size_of_rectangles_list; i++) {
        // copy the vertex buffer to the back buffer
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2); //here is the problem
    }

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);

}
1
If you create you need to release, otherwise it's a resource leak.Retired Ninja
This is a use case for dynamic VBs. BTW, why are you using legacy Direct3D 9? Take a look at PrimitiveBatch in DirectX Tool Kit for DX11.Chuck Walbourn

1 Answers

0
votes

It is as @Retired Ninja said in the comments, you need to Release for every call to CreateVertexBuffer.

You can do this by calling v_buffer->Release() in the render loop (not recommended), or by creating and reusing the vertex buffer outside of the loop.