简体   繁体   English

在direct3d11中映射资源并分配数据

[英]mapped resources and assigning data in direct3d11

Hi i'm working with shaders and i've just got a quick question about whether or not i can do something here. 嗨,我正在使用着色器,并且我刚刚对我是否可以在这里做些事情提出了一个简短的问题。 When mapping data to a buffer i normally see it done like this. 当将数据映射到缓冲区时,我通常会看到这样做。 Define class or struct to represent whats in the buffer. 定义类或结构以表示缓冲区中的内容。

class MatrixBuffer
{
public:
    D3DXMATRIX worldMatrix;
    D3DXMATRIX viewMatrix;
    D3DXMATRIX projectionMatrix;
} 

then set up a input element description when you make when you create the input layout, i got all that. 然后在创建输入布局时进行输入时设置输入元素描述,我已经掌握了所有这些。 Now, when i see people update the buffers before rendering, i see it mostly done like this: 现在,当我看到人们在渲染之前更新缓冲区时,我看到它基本上是这样完成的:

MatrixBuffer * dataPtr;
if(FAILED(deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)))
        return false;

dataPtr = (MatrixBuffer *) mappedResource.pData;
dataPtr->world = worldMatrix;
dataPtr->view = viewMatrix;
dataPtr->projection = projectionMatrix;

deviceContext->Unmap(m_matrixBuffer, 0);

where the values assigned are actually valid D3DXMATRIX's. 其中分配的值实际上是有效的D3DXMATRIX。 seeing as that's okay, would anything go wrong if i just did it like this? 认为还可以,如果我这样做就可以了吗?

MatrixBuffer* dataPtr;
if(FAILED(deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)))
            return false;

dataPtr = (MatrixBuffer *) mappedResource.pData;
*dataPtr = matrices;

deviceContext->Unmap(m_matrixBuffer, 0);

where matrices is a valid filled out MatrixBuffer object? 有效填充的MatrixBuffer对象在哪里矩阵? Would there be dire consequences later because of some special direct X handles this or is it fine? 稍后会因为某些特殊的直接X处理这个而带来可怕的后果吗?

I don't see anything wrong with your version, from a technical perspective. 从技术角度看,您的版本没有任何问题。

It might be slightly harder to grasp what actually gets copied there, and from a theoretical viewpoint the resulting code should be pretty much the same -- I would expect that any decent optimizer would notice that the "long" version just copies several successive items, and lump them all into one larger copy. 可能很难掌握那里实际复制的内容,而且从理论上讲,结果代码应该几乎相同-我希望任何体面的优化程序都会注意到“长”版本只会复制多个连续的项目,然后将它们全部打包成一个更大的副本。

Try avoiding using mapping as it stalls the pipeline. 尝试避免使用映射,因为它会使管道停顿。 For stuff that gets updated once a frame it's better to have D3D11_USAGE_DEFAULT buffer and then use UpdateSubresource method to push new data in. More on that here: http://blogs.msdn.com/b/shawnhar/archive/2008/04/14/stalling-the-pipeline.aspx 对于一帧更新一次的内容,最好具有D3D11_USAGE_DEFAULT缓冲区,然后使用UpdateSubresource方法来推送新数据。有关更多信息,请参见: http : //blogs.msdn.com/b/shawnhar/archive/2008/04/ 14 / stalling-the-pipeline.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM