jpbruyere / vke.net

Vulkan .net library
MIT License
13 stars 3 forks source link

Optimizations #11

Closed Shawdooow closed 2 years ago

Shawdooow commented 2 years ago

Use unsafe magic to cast memory for free!

Also added new SubArray function

Shawdooow commented 2 years ago

Actually to my extreme amazement testing in .net6.0 has revealed that the crazy if statements were as fast as an Unsafe.As cast and MemoryMarshal is slower.

Could be different for the version of net this project is compiled against, definitely warrants further investigation...

image

Shawdooow commented 2 years ago

Using Unsafe.As wasn't any faster than all those crazy if statements because the JIT was working overtime on it. The if statements are slower when the JIT can't guarantee that an array with 3 or greater items is being fed into it. Note the bottom function shown is using floats NOT bytes image

Shawdooow commented 2 years ago

I can't actually compile this repo to test how these changes interact with each other but I can say that I tested them individually and they all appear to work.

jpbruyere commented 2 years ago

Why can't you compile the repo? I can help surely.

Can you guaranty that Vector structs and Quaternion Lenght (sizeof) is always correct. (see https://stackoverflow.com/questions/42695067/unsafe-as-from-byte-array-to-ulong-array).

I have already implemented modifications in other project of mine with Span and ReadOnlySpan, and I've not yet optimized vke. I knew it was to be done, thanks for the time you spend on this.

Shawdooow commented 2 years ago

The size and layout of Vectors and Quaternions in memory shouldn't ever change.

It should also be noted that despite W at the top of those structs in code in memory they are last (which I only learned doing this pr)

Shawdooow commented 2 years ago

On a semi-unrelated note, do you have a slack or discord server related to your vulkan repos?

jpbruyere commented 2 years ago

I have just invited you into my Gitter channel (https://gitter.im/CSharpRapidOpenWidgets/) (I should rename it)