I’m working on a thing and trying to make sure that it works under both DirectX and OpenGL. I was experiencing some flickering in DirectX, while it looked like it was supposed to in OpenGL.
I’ve recreated the issue here:
The flickering looks like this:
Click to add a square. The most recently added 1 or 2 squares render normally, while the previous squares either don’t appear at all or flicker.
If I use
VertexBuffer instead of
DynamicVertexBuffer the problem completely disappears.
I know I have dealt with this before when porting from XNA to MonoGame although I don’t remember the exact cause.
If in your code you change “readonly VertexBuffer Buffer;” to “readonly DynamicVertexBuffer Buffer;” you will get an extra argument for SetData called SetDataOptions.
From what I remember with DX11 SetDataOptions.NoOverwrite works when setting data to new array locations or locations which haven’t been rendered recently (this works well for example with particle systems). While this seems to fix your glitch with static squares I believe there will be issues if your squares were to move around as updates are not guaranteed to occur before being rendered (in my case cars would glitch back and forwards when moving).
With the SetDataOptions.None I remember flickering unless I only used one SetData per draw. So currently for most DynamicVertexBuffers I build an array of visible objects and submit once per frame with SetDataOptions.None on changes occurring only.
NoOverwrite got rid of the flickering. I also did a brief test where I move the squares each frame and I didn’t see any immediate issues.
Will it cause memory leaks if I only ever use
There won’t be any addition considerations for memory leaks when using the SetDataOptions.NoOverwrite option, just dispose of buffers when they are no longer needed as usual.
I rediscovered this XNA article which explains the issue further and why different SetDataOptions are needed (please note it doesn’t cover any DX11 changes or the additional SetDataOptions.None option though).
Thanks for the explanation. I had misunderstood what
NoOverwrite after reading just the snippet in the Monogame documentation.