一、重要的API
A、ID3D11DeviceContext::Map;
B、ID3D11DeviceContext::Unmap;
C、D3D11_USAGE
二、基础对象的创建
A、Create a Texture
B、Initialize a Texture Programmatically
C、Create a Constant Buffer
三、步骤
A、指定动态用法
1、指定资源动态使用,例如:D3D11_BUFFER_DESC 中指定Usage = D3D11_USAGE_DYNAMIC, 在D3D11_TEXTURE2D_DESC 中指定Usage = D3D11_USAGE_DYNAMI
2、指定资源可写,D3D11_BUFFER_DESC或D3D11_TEXTURE2D_DESC 中指定CPUAccessFlags = D3D11_CPU_ACCESS_WRITE
3、把资源地址传递创建函数, ID3D11Device::CreateBuffer 中传递D3D11_BUFFER_DESC, ID3D11Device::CreateTexture2D中传递D3D11_TEXTURE2D_DESC
// 创建三角形顶点缓冲区
float2 triangleVertices[] =
{
float2(-0.5f, -0.5f),
float2(0.0f, 0.5f),
float2(0.5f, -0.5f),
};
D3D11_BUFFER_DESC vertexBufferDesc = {0};
vertexBufferDesc.ByteWidth = sizeof(float2) * ARRAYSIZE(triangleVertices);
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC, //指定资源动态
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA vertexBufferDescData;
vertexBufferDescData.pSysMem = triangleVertices;
vertexBufferDescData.SysMemSlicePitch = 0;
vertexBufferDescData.SysMemPitch = 0;
DX::ThrowIfFailed(m_d3dDevice->creatBuffer(&vertexBufferDesc, &vertexBufferDescData, &vertexBuffer2));
B、更改动态资源
1、设置新的数据资源,声明D3D11_MAPPED_SUBRESOURCE类型并初始化
2、在ID3D11DeviceContext::Map 传递D3D11_MAP_WRITE_DISCARD ,获得数据的内存的指针,禁用GPU对的访问权限,将指针设置为上一步中D3D11_MAPPED_SUBRESOURCE变量的地址
3、更新数据
4、ID3D11DeviceContext::Unmap 启用GPU对数据的访问
void TriangleRenderer::MapDoubleVertices()
{
D3D11_MAPPED_SUBRESOURCE mappedResource;
ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
float2 vertices[] =
{
float2(-1.0f, -1.0f);
float2(0.0f, 1.0f);
float2(1.0f, -1.0f);
};
// 禁用GPU访问
auto m_d3dContext = m_deviceResources->GetD3DDeviceContext();
m_d3dContext->Map(vertexBuffer2.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
// 更新数据
memccpy(mappedResource.pData, vertices, sizeof(vertices));
// 启用GPU 数据访问权限
m_d3dContext-》Unmap(vertexBuffer2.Get(), 0);
}