0
\$\begingroup\$

I am trying to dynamically update the vertex buffer in a UWP project using SharpDX, 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.

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

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?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Yes u have the same buffer but each time you are writing to the same place, you need to keep track of your position in the buffer and increment it each update and also make sure you don't go out of bounds.

Maybe pull your pointer var out of that method and make it class scoped.

It should work just like writing to a memory stream.

The pointer that databox gives u is the start of the buffer.

private pointer;
private bool First = true;


public void UpdateVertexBuffer(ScatterVertex data)
        {
            DataBox dataBox;
           if (First )
           {

            dataBox =
            this.context.MapSubresource(
                scatterPointVertexBuffer,
                0,
                D3D11.MapMode.WriteDiscard,
                D3D11.MapFlags.None
                );
              pointer = dataBox.DataPointer;
              first = false;
           }
          else
          {
           dataBox =
            this.context.MapSubresource(
                scatterPointVertexBuffer,
                0,
                D3D11.MapMode.WriteNoOverwrite,
                D3D11.MapFlags.None
                );
          }    


            pointer += Utilities.WriteAndPosition(pointer, ref data);

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

I don't normally write C# so sorry if this code is not right but something like that should work to update it now u just have to make sure you don't go out of bounds in the buffer, maybe set First to true and start at the beginning of the buffer again once u reach the end.... would end up like a ring buffer I think.

\$\endgroup\$
8
  • \$\begingroup\$ Yeah, it does work this way. But this does not agree with the MSDN explanation for the Mode. msdn.microsoft.com/en-us/library/windows/desktop/… \$\endgroup\$ Nov 16, 2016 at 6:24
  • \$\begingroup\$ It dose match how the msdn docs say to use it. \$\endgroup\$ Nov 16, 2016 at 6:25
  • \$\begingroup\$ just don't loop it back around, stop writing to it once u have filled it up \$\endgroup\$ Nov 16, 2016 at 6:28
  • \$\begingroup\$ No, MSDN says under D3D11_MAP_WRITE_NO_OVERWRITE, "Resource is mapped for writing; the existing contents of the resource cannot be overwritten (see Remarks). This flag is only valid on vertex and index buffers. The resource must have been created with write access (see D3D11_CPU_ACCESS_WRITE). Cannot be used on a resource created with the D3D11_BIND_CONSTANT_BUFFER flag." \$\endgroup\$ Nov 16, 2016 at 6:32
  • \$\begingroup\$ But you keep writing to the same place so it does.... databox gives u a pointer to the start of the buffer. \$\endgroup\$ Nov 16, 2016 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .