简体   繁体   English

不使用memcpy的DirectX11映射顶点

[英]DirectX11 mapping vertex without using memcpy

apparently I have some issue regarding mapping vertexBuffers into the GPU using memcpy 显然我有一些关于使用memcpy将vertexBuffers映射到GPU的问题

This is my initial code 这是我的初始代码

  D3D11_MAPPED_SUBRESOURCE MappedResource;
  ID3D11Buffer *pBuffer = s_vertexBuffers.pBuffers[i].Get();
  HRESULT hr = s_d3dContext->Map(pBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);

  float *vertices = (float *) MappedResource.pData,
        *data = (float *)s_vertexBuffers.pData[i];
  int len = s_vertexBuffers.pStrides[i];
  if (i != VERTEX_BUFFER::COLOR)
  {
      len *= total;
  }

  memcpy(vertices, data, len);
  s_d3dContext->Unmap(pBuffer, 0);

What I'm going to change is the way of mapping (copying data from buffers into GPU) using pointer, like the one pointed on here into something similar to SetShaderParameters function (also on the tutorial), which is using pointer to get the data in the buffer and copying data directly using pointer into it 我要更改的是使用指针映射(将数据从缓冲区复制到GPU)的方法,例如此处指向的指针映射到类似于SetShaderParameters函数(也在本教程中)的一种方法,该函数使用指针获取数据。在缓冲区中,并使用指针直接将数据复制到缓冲区中

Basically, line float *vertices = (float *) MappedResource.pData until memcpy(vertices, data, len); 基本上,行float * vertices =(float *)MappedResource.pData直到memcpy(vertices,data,len);

will be changed into something like the above tutorial pointed out 将变成上面教程中指出的内容

This is what my code looks like right now 这就是我的代码现在的样子

VertexBuffers *dataPtr;
dataPtr = (VertexBuffers *)MappedResource.pData;
dataPtr->pBuffers[i] = s_vertexBuffers.pBuffers[i].Get();
dataPtr->pData[i] = s_vertexBuffers.pData[i];
dataPtr->pSizes[i] = s_vertexBuffers.pSizes[i];
dataPtr->pStrides[i] = s_vertexBuffers.pStrides[i];
if (i != VERTEX_BUFFER::COLOR) 
{
   dataPtr->pStrides[i] *= total;
}

struct VertexBuffers and VERTEX_BUFFER namespace content is struct VertexBuffers和VERTEX_BUFFER命名空间内容为

namespace VERTEX_BUFFER
{
    enum : int
    {
        POSITION,
        COLOR,
        TEXCOORD,
        COUNT
    };
}


 struct VertexBuffers
    {
        ComPtr<ID3D11Buffer>    pBuffers[VERTEX_BUFFER::COUNT];
        void*                   pData[VERTEX_BUFFER::COUNT];
        UINT                    pStrides[VERTEX_BUFFER::COUNT];
        UINT                    pSizes[VERTEX_BUFFER::COUNT];
    };

I think the problem is whenever I'm trying to copy data using pointer, I'm not taking len into account like memcpy does 我认为问题在于,每当我尝试使用指针复制数据时,我都不会像memcpy那样考虑len

Since I'm currently learning DirectX and programming please do correct me if something quite not right about my explanations. 由于我目前正在学习DirectX和编程,因此如果对我的解释有误,请纠正我。

Sorry for the bad code and bad programming skills, and also for the bad english, Cheers! 对不好的代码和不好的编程技能,以及不好的英语,抱歉,加油!

So you want something like this? 所以你想要这样的东西吗?

VertexBuffers *dataPtr;
dataPtr = (VertexBuffers *)MappedResource.pData;
dataPtr->pBuffers[i] = s_vertexBuffers.pBuffers[i].Get();
dataPtr->pSizes[i] = s_vertexBuffers.pSizes[i];
dataPtr->pStrides[i] = s_vertexBuffers.pStrides[i];
if (i != VERTEX_BUFFER::COLOR) 
{
   dataPtr->pStrides[i] *= total;
}
memcpy(dataPtr->pData[i], s_vertexBuffers.pData[i], dataPtr->pStrides[i]);

As you've found out when you have two pointers (say p and q) then all p = q; 正如您发现的那样,当您有两个指针(例如p和q)时,所有p = q; does is copy the pointer. 确实是复制指针。 If you want to copy the data that is being pointed to you should write memcpy(p, q, size_of_data); 如果要复制指向的数据,则应编写memcpy(p, q, size_of_data); .

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

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