Direct3D c ++ api如何更新顶点缓冲区? [英] Direct3D c++ api how to update vertex buffer?

查看:226
本文介绍了Direct3D c ++ api如何更新顶点缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么如何使用 IASetVertexBuffers 方法更新绑定到设备对象的顶点缓冲区中的值?在调用 Draw() Present()之前,还会更改此缓冲区中的值。也可以根据缓冲区中的这些新值更新映像吗?

解决方案

要通过CPU更新顶点缓冲区,您必须首先创建一个允许CPU写入的动态顶点缓冲区。为此,请将 ID3D11Device :: CreateBuffer 用法设置为 D3D11_USAGE_DYNAMIC CPUAccessFlags 设置为 D3D11_CPU_ACCESS_WRITE 。示例:

  D3D11_BUFFER_DESC desc; 
ZeroMemory(& desc,sizeof(desc));
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.ByteWidth = size;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
d3dDevice-> CreateBuffer(& desc,initialVertexData,& vertexBuffer);

既然您有一个动态顶点缓冲区,您可以使用 ID3D11DeviceContext :: Map ID3D11DeviceContext :: Unmap 。示例:

  D3D11_MAPPED_SUBRESOURCE资源; 
d3dDeviceContext-> Map(vertexBuffer,0,D3D11_MAP_WRITE_DISCARD,0,& resource);
memcpy(resource.pData,sourceData,vertexDataSize);
d3dDeviceContext-> Unmap(vertexBuffer,0);

其中sourceData是要放入缓冲区的新顶点数据。



这是更新顶点缓冲区的一种方法,您需要上传一组全新的顶点数据并丢弃先前的内容。还有其他方法来更新顶点缓冲区。例如,您可以保留当前内容并只修改某些值,或者只更新顶点缓冲区的某些区域而不更新整个区域。

每种方法都有其自己的用法和性能特征。这完全取决于你的数据是什么以及你打算如何使用它。 此NVIDIA演示文稿提供了一些建议最好的方式来更新您的缓冲区为不同的用途。

是的,您将要调用此和 IASetVertexBuffers 之前 Draw() Present()来查看当前帧的更新结果。在调用 IASetVertexBuffers 之前,您不一定需要更新顶点缓冲区内容。这些可以以任何顺序。


So how can one update values in vertex buffer bound into device object using IASetVertexBuffers method? Also will changing values in this buffer before call to Draw() and Present()? Also will the image be updated according to these new values in buffer?

解决方案

To update a vertex buffer by the CPU, you must first create a dynamic vertex buffer that allows the CPU to write to it. To do this, call ID3D11Device::CreateBufferwith Usage set to D3D11_USAGE_DYNAMIC and CPUAccessFlags set to D3D11_CPU_ACCESS_WRITE. Example:

D3D11_BUFFER_DESC desc;
ZeroMemory( &desc, sizeof( desc ) );
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.ByteWidth = size;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
d3dDevice->CreateBuffer( &desc, initialVertexData, &vertexBuffer );

Now that you have a dynamic vertex buffer, you can update it using ID3D11DeviceContext::Map and ID3D11DeviceContext::Unmap. Example:

D3D11_MAPPED_SUBRESOURCE resource;
d3dDeviceContext->Map( vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource );
memcpy( resource.pData, sourceData, vertexDataSize );
d3dDeviceContext->Unmap( vertexBuffer, 0 );

where sourceData is the new vertex data you want to put into the buffer.

This is one method for updating a vertex buffer where you are uploading a whole new set of vertex data and discarding previous contents. There are also other ways to update a vertex buffer. For example, you could leave the current contents and only modify certain values, or you could update only certain regions of the vertex buffer instead of the whole thing.

Each method will have its own usage and performance characteristics. It all depends on what your data is and how you intend on using it. This NVIDIA presentation gives some advice on the best way to update your buffers for different usages.

Yes, you will want to call this and IASetVertexBuffers before Draw() and Present() to see the updated results for the current frame. You don't necessarily need to update the vertex buffer contents before calling IASetVertexBuffers. Those can be in either order.

这篇关于Direct3D c ++ api如何更新顶点缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆