How to read vertices from vertex buffer in Direct3d11

前端 未结 3 860
暗喜
暗喜 2020-12-17 05:55

I have a question regarding vertex buffers. How does one read the vertices from the vertex buffer in D3D11? I want to get a particular vertex\'s position for calculations, i

3条回答
  •  执笔经年
    2020-12-17 06:21

    Drop's answer was helpful. I figured that the reason why I wasn't able to read the buffer was because I didn't have the CPU_ACCESS_FLAG set to D3D11_CPU_ACCESS_READ before. Here

    D3D11_BUFFER_DESC bufferDesc;
    ZeroMemory(&bufferDesc, sizeof(bufferDesc));
    bufferDesc.ByteWidth = iNumElements * sizeof(T);
    bufferDesc.Usage = D3D11_USAGE_DEFAULT;
    bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    bufferDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE ;
    bufferDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
    bufferDesc.StructureByteStride = sizeof(T);
    

    And then to read data I did

        const ID3D11Device& device = *DXUTGetD3D11Device();
        ID3D11DeviceContext& deviceContext = *DXUTGetD3D11DeviceContext();
        D3D11_MAPPED_SUBRESOURCE ms;
        HRESULT hr = deviceContext.Map(g_pParticles, 0, D3D11_MAP_READ, 0, &ms);    
    
        Particle* p = (Particle*)malloc(sizeof(Particle*) * g_iNumParticles);
        ZeroMemory(p, sizeof(Particle*) * g_iNumParticles);
        memccpy(p, ms.pData, 0, sizeof(ms.pData));
        deviceContext.Unmap(g_pParticles, 0);
    
        delete[] p;
    

    I agree it's a performance decline, I wanted to do this, just to be able to debug the values!

    Thanks anyway! =)

提交回复
热议问题