sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 638 forks source link

Data overwritten in MapSubresource() method #825

Open KySpace opened 7 years ago

KySpace commented 7 years ago

I am trying to dynamically update the vertex buffer, once every time I call the following method, where context is the device context member.

public void UpdateVertexBuffer(ScatterVertex data)
            {
                DataBox dataBox =
                this.context.MapSubresource(
                    scatterPointVertexBuffer,
                    0,
                    D3D11.MapMode.WriteNoOverwrite,
                    D3D11.MapFlags.None
                    );
                var pointer = dataBox.DataPointer;
                pointer = Utilities.WriteAndPosition(pointer, ref data);

                this.context.UnmapSubresource(scatterPointVertexBuffer, 0);
            }

I am expecting to keep the old data during the update process. However each time I call this method, the previous data is overwritten. I checked the pointer of dataBox.DataPointer and it remains the same value in every call. But shouldn't the MapSubresource method protect the old data if I choose the WriteNoOverwrite mode? What should I do to keep the previous data during update?

KySpace commented 7 years ago

Using DataStream as output doesn't help either. In either cases if I check the vertex buffer I get only one vertex.

h1cks commented 6 years ago

A few things come to mind. Firstly, you have checked the relevant capabilities I assume as per page map

secondly, I checked my code and use the following call.

var dataBox = a_context.MapSubresource(m_d3dBuffer, D3D11.MapMode.WriteDiscard, D3D11.MapFlags.None, out DataStream stream);

Obviously I use writediscard, but the flag can be what you desire. This outputs the stream you read and write to.