How to: Create a vertex buffer (17)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/xiaoyafang123/article/details/89637226

Each vertex vertex buffer contains data, this section describes how to initialize a static vertex buffer, that is, the vertex buffer will not change. For help initialize non-static buffer, see the Remarks section.

Initialize static vertex buffer

  1. Definition describes the structure of the vertex. For example, if the data contains the vertex position data and color data, then the structure will have a vector describing the location and description of the other color vector.
  2. Allocate memory structure defined for the first step (using malloc or new), the buffer is filled with data describing the actual geometry of the vertex.
  3. Create a buffer by filling D3D11_BUFFER_DESC structure. The D3D11_BIND_VERTEX_BUFFER flag to BindFlags members, the size and structure of the transfer from the first step to ByteWidth members.
  4. Create a child resource data described by filling D3D11_SUBRESOURCE_DATA structure. pSysMem members D3D11_SUBRESOURCE_DATA structures should be directed to the resource data created in step 2.
  5. By calling a function and passing D3D11_BUFFER_DESC CreateBuffer and D3D11_SUBRESOURCE_DATA ID3D11Buffer interface address structure and to initialize.

The following code example demonstrates how to create a vertex buffer. This example assumes that a valid ID3D11Device g_pd3dDevice objects.

ID3D11Buffer*      g_pVertexBuffer;

// Define the data-type that
// describes a vertex.
struct SimpleVertexCombined
{
    D3DXVECTOR3 Pos;  
    D3DXVECTOR3 Col;  
};

// Supply the actual vertex data.
SimpleVertexCombined verticesCombo[] =
{
    D3DXVECTOR3( 0.0f, 0.5f, 0.5f ),
    D3DXVECTOR3( 0.0f, 0.0f, 0.5f ),
    D3DXVECTOR3( 0.5f, -0.5f, 0.5f ),
    D3DXVECTOR3( 0.5f, 0.0f, 0.0f ),
    D3DXVECTOR3( -0.5f, -0.5f, 0.5f ),
    D3DXVECTOR3( 0.0f, 0.5f, 0.0f ),
};

// Fill in a buffer description.
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage            = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth        = sizeof( SimpleVertexCombined ) * 3;
bufferDesc.BindFlags        = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags   = 0;
bufferDesc.MiscFlags        = 0;

// Fill in the subresource data.
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = verticesCombo;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;

// Create the vertex buffer.
hr = g_pd3dDevice->CreateBuffer( &bufferDesc, &InitData, &g_pVertexBuffer );

Remark

The following are some methods vertex buffer initialization over time.

  1. Created with D3D10_USAGE_STAGING second buffer, using ID3D11DeviceContext :: Map, ID3D11DeviceContext :: UnMap second buffer is filled, use ID3D11DeviceContext :: CopyResource copy STAGING DEFAULT buffer to buffer.
  2. Copy the data from memory using ID3D11DeviceContext :: UpdateSubresource.
  3. With D3D11_USAGE_DYNAMIC create a buffer, and fill it with ID3D11DeviceContext :: Map, ID3D11DeviceContext :: Unmap.

# 1 and # 2 is useful for changes less than once per frame content. In general, the GPU reads faster, slower speed CPU updates.

# 3 content is useful more than once for each frame changes. In general, GPU reading speed will slow down, but the update speed of the CPU will be faster.

Guess you like

Origin blog.csdn.net/xiaoyafang123/article/details/89637226