Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for double precision floats #288 #12299

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ opts = Variables(customs, ARGUMENTS)

# Target build options
opts.Add('arch', "Platform-dependent architecture (arm/arm64/x86/x64/mips/...)", '')
opts.Add(EnumVariable('bits', "Target platform bits", 'default', ('default', '32', '64')))
opts.Add(EnumVariable('bits', "Target platform bits", 'default', ('default', '32', '64', 'fat')))
opts.Add(EnumVariable('float', "Target float bits", 'default', ('default', '32', '64')))
opts.Add('p', "Platform (alias for 'platform')", '')
opts.Add('platform', "Target platform (%s)" % ('|'.join(platform_list), ), '')
opts.Add(EnumVariable('target', "Compilation target", 'debug', ('debug', 'release_debug', 'release')))
Expand Down Expand Up @@ -217,6 +218,16 @@ if (env_base['no_editor_splash']):
if not env_base['deprecated']:
env_base.Append(CPPDEFINES=['DISABLE_DEPRECATED'])

if (env_base["float"] == "64"):
if (env_base["bits"] == "32"):
print("64-bit double-precision floats are slow on 32-bit CPUs, please compile for 64-bit CPUs instead.")
sys.exit(255)
if env_base.msvc:
env_base.Append(CCFLAGS=['/DREAL_T_IS_DOUBLE'])
else:
env_base.Append(CCFLAGS=['-DREAL_T_IS_DOUBLE'])
#else: #In the far future, we may add ".single" to single-precision builds, but that's not necessary or desired right now.

env_base.platforms = {}

selected_platform = ""
Expand Down Expand Up @@ -368,6 +379,9 @@ if selected_platform in platform_list:
else:
suffix = "." + selected_platform

if (env_base["float"] == "64"):
suffix = ".double"

if (env["target"] == "release"):
if env["tools"]:
print("Tools can only be built with targets 'debug' and 'release_debug'.")
Expand Down
10 changes: 6 additions & 4 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,21 +604,23 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
ERR_FAIL_MUL_OF(count, 4, ERR_INVALID_DATA);
ERR_FAIL_COND_V(count < 0 || count * 4 > len, ERR_INVALID_DATA);

PoolVector<float> data;
PoolVector<real_t> data;

if (count) {
//const float*rbuf=(const float*)buf;
data.resize(count);
PoolVector<float>::Write w = data.write();
PoolVector<real_t>::Write w = data.write();
for (int32_t i = 0; i < count; i++) {

w[i] = decode_float(&buf[i * 4]);
w[i] = decode_float(&buf[i * sizeof(real_t)]);
}

w = PoolVector<real_t>::Write();
}
r_variant = data;

if (r_len) {
(*r_len) += 4 + count * sizeof(float);
(*r_len) += 4 + count * sizeof(real_t);
}

} break;
Expand Down
2 changes: 1 addition & 1 deletion core/type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ struct GetTypeInfo<const Variant &> {

MAKE_TEMPLATE_TYPE_INFO(Vector, uint8_t, Variant::POOL_BYTE_ARRAY)
MAKE_TEMPLATE_TYPE_INFO(Vector, int, Variant::POOL_INT_ARRAY)
MAKE_TEMPLATE_TYPE_INFO(Vector, float, Variant::POOL_REAL_ARRAY)
MAKE_TEMPLATE_TYPE_INFO(Vector, real_t, Variant::POOL_REAL_ARRAY)
MAKE_TEMPLATE_TYPE_INFO(Vector, String, Variant::POOL_STRING_ARRAY)
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector2, Variant::POOL_VECTOR2_ARRAY)
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector3, Variant::POOL_VECTOR3_ARRAY)
Expand Down
8 changes: 4 additions & 4 deletions core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,9 +836,9 @@ Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int
return ret;
}

Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
Vector<real_t> String::split_floats(const String &p_splitter, bool p_allow_empty) const {

Vector<float> ret;
Vector<real_t> ret;
int from = 0;
int len = length();

Expand All @@ -859,9 +859,9 @@ Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty)
return ret;
}

Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
Vector<real_t> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {

Vector<float> ret;
Vector<real_t> ret;
int from = 0;
int len = length();

Expand Down
5 changes: 3 additions & 2 deletions core/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "core/cowdata.h"
#include "core/typedefs.h"
#include "core/vector.h"
#include "math/math_defs.h"

/**
@author Juan Linietsky <reduzio@gmail.com>
Expand Down Expand Up @@ -267,8 +268,8 @@ class String {
Vector<String> split(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> rsplit(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> split_spaces() const;
Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
Vector<real_t> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
Vector<real_t> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;

Expand Down
6 changes: 4 additions & 2 deletions core/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class Variant {

operator PoolVector<uint8_t>() const;
operator PoolVector<int>() const;
operator PoolVector<real_t>() const;
operator PoolVector<float>() const;
operator PoolVector<double>() const;
operator PoolVector<String>() const;
operator PoolVector<Vector3>() const;
operator PoolVector<Color>() const;
Expand All @@ -220,7 +221,8 @@ class Variant {
operator Vector<Variant>() const;
operator Vector<uint8_t>() const;
operator Vector<int>() const;
operator Vector<real_t>() const;
operator Vector<float>() const;
operator Vector<double>() const;
operator Vector<String>() const;
operator Vector<StringName>() const;
operator Vector<Vector3>() const;
Expand Down
8 changes: 4 additions & 4 deletions core/variant_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,16 +1096,16 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,

} else if (id == "PoolRealArray" || id == "FloatArray") {

Vector<float> args;
Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
Vector<real_t> args;
Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
if (err)
return err;

PoolVector<float> arr;
PoolVector<real_t> arr;
{
int len = args.size();
arr.resize(len);
PoolVector<float>::Write w = arr.write();
PoolVector<real_t>::Write w = arr.write();
for (int i = 0; i < len; i++) {
w[i] = args[i];
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/gles2/rasterizer_scene_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2658,7 +2658,7 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C
};

if (!asymmetrical) {
float vw, vh, zn;
real_t vw, vh, zn;
camera.get_viewport_size(vw, vh);
zn = p_projection.get_z_near();

Expand Down
7 changes: 5 additions & 2 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@ void RasterizerSceneGLES3::_draw_sky(RasterizerStorageGLES3::Sky *p_sky, const C
};

if (!asymmetrical) {
float vw, vh, zn;
real_t vw, vh, zn;
camera.get_viewport_size(vw, vh);
zn = p_projection.get_z_near();

Expand Down Expand Up @@ -4173,7 +4173,10 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const
state.ubo_data.shadow_dual_paraboloid_render_zfar = 0;
state.ubo_data.opaque_prepass_threshold = 0.99;

p_cam_projection.get_viewport_size(state.ubo_data.viewport_size[0], state.ubo_data.viewport_size[1]);
real_t viewport_size_0 = state.ubo_data.viewport_size[0];
real_t viewport_size_1 = state.ubo_data.viewport_size[1];

p_cam_projection.get_viewport_size(viewport_size_0, viewport_size_1);

if (storage->frame.current_rt) {
state.ubo_data.screen_pixel_size[0] = 1.0 / storage->frame.current_rt->width;
Expand Down
9 changes: 8 additions & 1 deletion drivers/gles3/shader_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ void ShaderGLES3::bind_uniforms() {
continue;
}

glUniformMatrix4fv(location, 1, false, &(C->get().matrix[0][0]));
// Force the real_t conversion to float since opengles 3 does not support 64 bit floats.
GLfloat uniform[16];
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
uniform[a * 4 + b] = static_cast<float>(C->get().matrix[a][b]);
}
}
glUniformMatrix4fv(location, 1, false, uniform);
C = C->next();
};

Expand Down
Loading