utopia-rise / godot-kotlin-native

Kotlin bindings for Godot Engine
MIT License
290 stars 16 forks source link

Int/Float vs Long/Double #185

Open CedNaru opened 4 years ago

CedNaru commented 4 years ago

A quick summary of the use of 32/64 bits values in Godot.

Godot internally uses 32 bits Integers in its structs/classes and 32 bits Floats as well (named real-t). Godot supporting 64 bits float is still no ready but should be possible by changing real-t to 64bits. In some rare functions, Godot still uses 64 bits variables when it's explicitly needed (like returning a 64 bits Hash).

In order to communicate with the scripting API (for GdScript and GdNative), Godot creates the Variant class to wrap any primitives, core, and object Godot can use. Variant internally stores a 4bits variable to identify the type. There is no information about the size of Int/Float inside. So for convenience, they are widened to the largest one (aka 64 bits).

Godot sends all its information to native scripts as Variant and the ClassDB doesn't hold information about the actual size either. The direct consequence is that we can't use 32bits values in the Icalls, so all of them have to return Long/Double.

The native functions we register to Godot don't have to be Long/Double. The Int and Float parameters will be cast to Long/Double behind the hood and cast back to their original size automatically when the native functions are called from Godot.

The last case is about the core types. Their internal variables should be 32 bits to match Godot and to keep their size in check. The main issue is the consistency of types, we would end up core types returning 32bits values and Icalls returning 64 bits.

So the idea is to keep the internal values of cores as 32 bits, but all methods/properties will still return a 64 bits version. That way we keep the memory footprint at a minimal size but still remains consistent with Godot Objects.

Summary:

What do you think ?

chippmann commented 4 years ago

@CedNaru

Registered Function: Support 32 and 64 bits with auto wrapping

You mean the entry generation and like it is currently on master merge

CedNaru commented 4 years ago

Yes that's what I meant.

chippmann commented 4 years ago

Ok. I think this is the best approach we have on this problem. So I'm in.

piiertho commented 4 years ago

If we cannot get the size info, we don't have choice :/

raniejade commented 4 years ago

Cores: Internally use 32 bits but returns 64 bits.

@CedNaru what do you mean by this?

Registered Function: Support 32 and 64 bits with auto wrapping. Includes properties and signals as well right? And when you say auto-wrapping, do you mean widening and de-widening (when appropriate) when sending and receiving real numbers to/from godot?

CedNaru commented 4 years ago

Cores internally use 32 bits, but the property getters and setters will use a 64 bits type, it would be then narrowed. Same with the functions of core type. Let's say I want to write vect2.length(), despite the x and y being Float, the function would return a Double.

And yeah I meant registered function, properties and signals with widening ( and opposite) when necessary. Just like Cedric already did. I said wrapping, because we would directly use Variant wrapping and unwrapping but it was probably clumsy to use that term now that it's going to be removed.

chippmann commented 4 years ago

@CedNaru this can be closed now right?