顶点缓冲区数据的内存管理
假设我需要渲染静态图片(100星)。 我生成星星数据(位置、颜色、大小)到 std::vector star;
然后我创建一个用于 D3D 渲染的类,其中包含一个缓冲区:
CGalaxyMapRenderer
{
…
CComPtr<ID3D11Buffer> m_spStarBuffer;
}
在 ctor 中,我以这种方式初始化它:
CGalaxyMapRenderer::CGalaxyMapRenderer(const std::vector<SStarData>& vecData)
{
…
const CD3D11_BUFFER_DESC vertexBuffDescr((UINT)(sizeof(SStarData)*stars.size()), D3D11_BIND_VERTEX_BUFFER); //Constant buffer?
D3D11_SUBRESOURCE_DATA initVertexData = {0};
initVertexData.pSysMem = &stars[0];
spDevice->CreateBuffer(&vertexBuffDescr, &initVertexData, &m_spStarBuffer);
…
}
之后我可以销毁 std::vector,因为不再需要它。
问题是:
spDevice->CreateBuffer(&vertexBuffDecr, &initVertexData, &m_spStarBuffer));
为了这段代码的和平,内存分配将在哪里进行? 是图形内存还是当前进程内存?当我不再需要渲染星系图时(例如,当我想移动到下一个级别,不需要星系图时),我将销毁 CGalaxyMapRenderer。 dtor 中将会自动销毁 m_spStarBuffer。 问题是:清除所有缓冲区资源就足够了吗? 或者我应该采取一些额外的步骤来释放内存?
Assume that I need to render static picture (100 stars).
I generate star data (position, color, size) to std::vector stars;
Then I create a class for D3D rendering, which consist a buffer:
CGalaxyMapRenderer
{
…
CComPtr<ID3D11Buffer> m_spStarBuffer;
}
In ctor I initialize it in this way:
CGalaxyMapRenderer::CGalaxyMapRenderer(const std::vector<SStarData>& vecData)
{
…
const CD3D11_BUFFER_DESC vertexBuffDescr((UINT)(sizeof(SStarData)*stars.size()), D3D11_BIND_VERTEX_BUFFER); //Constant buffer?
D3D11_SUBRESOURCE_DATA initVertexData = {0};
initVertexData.pSysMem = &stars[0];
spDevice->CreateBuffer(&vertexBuffDescr, &initVertexData, &m_spStarBuffer);
…
}
After that I may destroy std::vector, as it is no longer needed.
The questions are:
spDevice->CreateBuffer(&vertexBuffDescr, &initVertexData, &m_spStarBuffer));
Where memory allocation will take place for this peace of code?
Will it be graphic memory or current process memory?When I no longer need to render a galaxy map (for example, when I want to move to next level, where no galaxy map required), I am going to destroy CGalaxyMapRenderer.
There will be automatic destruction of m_spStarBuffer in dtor.
The question is: is it enough to clear all the buffer resources?
Or should I make some additional steps in order to make memory free?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于第一个问题,它应该在进程堆上,图形内存仅在一般需要时使用: http://msdn.microsoft.com/en-us/library/ff476501(v=vs.85).aspx
至于第二个问题我是希望自动销毁意味着
m_spStarBuffer
是一个智能指针。下面是创建顶点缓冲区的简单示例: http: //msdn.microsoft.com/en-us/library/ff476899(v=VS.85).aspxAs to first question it should be on the process heap graphic memory is only used when needed in general: http://msdn.microsoft.com/en-us/library/ff476501(v=vs.85).aspx
As to second question I'm hoping by automatic destruction you mean that
m_spStarBuffer
is a smart pointer. Here's a simple example of creating a vertex buffer: http://msdn.microsoft.com/en-us/library/ff476899(v=VS.85).aspx