constantBuffer获取错误的值(有效的内存复制)

时间:2019-05-31 03:10:18

标签: resources buffer directx deep-copy staging

我正在尝试为数组实现常量缓冲区。

所以我正在使用MEMCOPY宏并映射到常量缓冲区中。

首先,将数据写入常量缓冲区

    struct TestStruct {
        Vector2 a[10];
        Vector2 b[10];

        TestStruct() {}
    };

    TestStruct* vs = new TestStruct();
    Vector2* a = new Vector2[10];
    Vector2* b = new Vector2[10];
    for (int i = 0; i < 10; ++i)
    {
        a[i] = Vector2(i);
        b[1] = Vector2(i);
    }
    CopyMemory(vs->a, a, sizeof(Vector2)*10);
    CopyMemory(vs->b, b, sizeof(Vector2)*10);

    D3D11_MAPPED_SUBRESOURCE mappedData;
    dContext->Map(buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);
    CopyMemory(mappedData.pData, data, sizeof(T));
    dContext->Unmap(buffer.Get(), 0);
    dContext->PSSetConstantBuffers(0, 1, buffer.GetAddressOf());

并读取缓冲区以检查其中的数据

    dContext->CopyResource(stagingBuffer.Get(), buffer);
    D3D11_MAPPED_SUBRESOURCE mappedResource;
    ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
    dContext->Map(stagingBuffer.Get(), 0, D3D11_MAP_READ, 0, &mappedResource);
    ZeroMemory(allocatedData, bufferDesc.ByteWidth);
    CopyMemory(allocatedData, mappedResource.pData, bufferDesc.ByteWidth);
    dContext->Unmap(buffer, 0);

我可以从“ allocatedData”中读取的数据是

a[0] = (0,0)
a[1] = (1,1) 
a[2] = (2,2)
a[3] = (3,3) 
a[4] = (4,4) 
a[5] = (5,5) 
a[6] = (6,6) 
a[7] = (7,7) 
a[8] = (8,8) 
a[9] = (9,9) 

b[0] = (0,0)
b[1] = (9,9)
b[2] = (0,0)
b[3] = (0,0)
b[4] = (0,0)
b[5] = (0,0)
b[6] = (0,0)
b[7] = (0,0)
b[8] = (0,0)
b[9] = (0,0)

'a'具有正确的预期值 但是“ b”的值不正确。 我只是猜测如何使用内存复制或分配数据可能存在问题。

0 个答案:

没有答案