AdamsLair / duality

a 2D Game Development Framework
https://adamslair.github.io/duality
MIT License
1.4k stars 289 forks source link

DrawDevice.AddVertices corrupts caller's data in picking mode #767

Closed ShadowsInRain closed 4 years ago

ShadowsInRain commented 4 years ago

Summary

When implementing custom Duality.Components.Renderer that caches vertex data, my objects rendered pitch-black, unless I purge cache every frame.

How to reproduce

Cache vertex data instead of recreating it every frame.

Workaround

Clone vertex data before sending it to DrawDevice. Works for me, I don't care about performance for now.

Analysis

Documentation for DrawDevice.AddVertices claims that "ownership of the buffer remains at the callsite, while the IDrawDevice copies the required data into internal storage". Debugging reveals that Color field in my vertex data is modified after visiting this method.

It seems that in "picking mode" vertex color is being overridden in-place. I guess cloning vertex buffer before picking mode shenanigans would fix this, but not sure about performance implications.

https://github.com/AdamsLair/duality/blob/8455bd9649861e61787c24e56fe5447bafba084b/Source/Core/Duality/Drawing/DrawDevice.cs#L489

Attachments

ilexp commented 4 years ago

Ohh, good catch. Yep, that's a bug!

Should be a somewhat easy fix by simply applying the picking special case on the appropriate parts of the buffer after copying, like you suggested. I'm adding a Help Wanted tag, in case someone wants to pick this up.

SirePi commented 4 years ago

Fixed by #769