Skip to content

Commit b410804

Browse files
authored
Merge pull request #242 from jss2a98aj/backport-assorted
[backport] Assorted fixes and optimizations
2 parents 549f449 + ab15412 commit b410804

File tree

13 files changed

+102
-89
lines changed

13 files changed

+102
-89
lines changed

core/math/color.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ struct [[nodiscard]] Color {
187187

188188
_FORCE_INLINE_ Color srgb_to_linear() const {
189189
return Color(
190-
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow((r + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
191-
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow((g + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
192-
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow((b + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
190+
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow(float((r + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
191+
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow(float((g + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
192+
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow(float((b + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
193193
a);
194194
}
195195
_FORCE_INLINE_ Color linear_to_srgb() const {
196196
return Color(
197-
r < 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * Math::pow(r, 1.0f / 2.4f) - 0.055f,
198-
g < 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * Math::pow(g, 1.0f / 2.4f) - 0.055f,
199-
b < 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * Math::pow(b, 1.0f / 2.4f) - 0.055f, a);
197+
r < 0.0031308f ? 12.92f * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
198+
g < 0.0031308f ? 12.92f * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
199+
b < 0.0031308f ? 12.92f * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
200200
}
201201

202202
static Color hex(uint32_t p_hex);

core/os/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#include "core/templates/safe_refcount.h"
3939

4040
SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
41+
thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
4142

42-
thread_local Thread::ID Thread::caller_id = Thread::UNASSIGNED_ID;
4343
#endif
4444

4545
Thread::PlatformFunctions Thread::platform_functions;

core/os/thread.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,22 @@ class Thread {
107107
static PlatformFunctions platform_functions;
108108

109109
ID id = UNASSIGNED_ID;
110+
110111
static SafeNumeric<uint64_t> id_counter;
111112
static thread_local ID caller_id;
112113
THREADING_NAMESPACE::thread thread;
113114

114115
static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
115116

116117
static void make_main_thread() { caller_id = MAIN_ID; }
117-
static void release_main_thread() { caller_id = UNASSIGNED_ID; }
118+
static void release_main_thread() { caller_id = id_counter.increment(); }
118119

119120
public:
120121
static void _set_platform_functions(const PlatformFunctions &p_functions);
121122

122123
_FORCE_INLINE_ ID get_id() const { return id; }
123124
// get the ID of the caller thread
124125
_FORCE_INLINE_ static ID get_caller_id() {
125-
if (unlikely(caller_id == UNASSIGNED_ID)) {
126-
caller_id = id_counter.increment();
127-
}
128126
return caller_id;
129127
}
130128
// get the ID of the main thread

core/variant/variant.h

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -258,53 +258,53 @@ class Variant {
258258

259259
void _clear_internal();
260260

261-
_FORCE_INLINE_ void clear() {
262-
static const bool needs_deinit[Variant::VARIANT_MAX] = {
263-
false, //NIL,
264-
false, //BOOL,
265-
false, //INT,
266-
false, //FLOAT,
267-
true, //STRING,
268-
false, //VECTOR2,
269-
false, //VECTOR2I,
270-
false, //RECT2,
271-
false, //RECT2I,
272-
false, //VECTOR3,
273-
false, //VECTOR3I,
274-
true, //TRANSFORM2D,
275-
false, //VECTOR4,
276-
false, //VECTOR4I,
277-
false, //PLANE,
278-
false, //QUATERNION,
279-
true, //AABB,
280-
true, //BASIS,
281-
true, //TRANSFORM,
282-
true, //PROJECTION,
283-
284-
// misc types
285-
false, //COLOR,
286-
true, //STRING_NAME,
287-
true, //NODE_PATH,
288-
false, //RID,
289-
true, //OBJECT,
290-
true, //CALLABLE,
291-
true, //SIGNAL,
292-
true, //DICTIONARY,
293-
true, //ARRAY,
294-
295-
// typed arrays
296-
true, //PACKED_BYTE_ARRAY,
297-
true, //PACKED_INT32_ARRAY,
298-
true, //PACKED_INT64_ARRAY,
299-
true, //PACKED_FLOAT32_ARRAY,
300-
true, //PACKED_FLOAT64_ARRAY,
301-
true, //PACKED_STRING_ARRAY,
302-
true, //PACKED_VECTOR2_ARRAY,
303-
true, //PACKED_VECTOR3_ARRAY,
304-
true, //PACKED_COLOR_ARRAY,
305-
true, //PACKED_VECTOR4_ARRAY,
306-
};
261+
static constexpr bool needs_deinit[Variant::VARIANT_MAX] = {
262+
false, //NIL,
263+
false, //BOOL,
264+
false, //INT,
265+
false, //FLOAT,
266+
true, //STRING,
267+
false, //VECTOR2,
268+
false, //VECTOR2I,
269+
false, //RECT2,
270+
false, //RECT2I,
271+
false, //VECTOR3,
272+
false, //VECTOR3I,
273+
true, //TRANSFORM2D,
274+
false, //VECTOR4,
275+
false, //VECTOR4I,
276+
false, //PLANE,
277+
false, //QUATERNION,
278+
true, //AABB,
279+
true, //BASIS,
280+
true, //TRANSFORM,
281+
true, //PROJECTION,
282+
283+
// misc types
284+
false, //COLOR,
285+
true, //STRING_NAME,
286+
true, //NODE_PATH,
287+
false, //RID,
288+
true, //OBJECT,
289+
true, //CALLABLE,
290+
true, //SIGNAL,
291+
true, //DICTIONARY,
292+
true, //ARRAY,
307293

294+
// typed arrays
295+
true, //PACKED_BYTE_ARRAY,
296+
true, //PACKED_INT32_ARRAY,
297+
true, //PACKED_INT64_ARRAY,
298+
true, //PACKED_FLOAT32_ARRAY,
299+
true, //PACKED_FLOAT64_ARRAY,
300+
true, //PACKED_STRING_ARRAY,
301+
true, //PACKED_VECTOR2_ARRAY,
302+
true, //PACKED_VECTOR3_ARRAY,
303+
true, //PACKED_COLOR_ARRAY,
304+
true, //PACKED_VECTOR4_ARRAY,
305+
};
306+
307+
_FORCE_INLINE_ void clear() {
308308
if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
309309
_clear_internal();
310310
}
@@ -800,7 +800,9 @@ class Variant {
800800
_FORCE_INLINE_ Variant() :
801801
type(NIL) {}
802802
_FORCE_INLINE_ ~Variant() {
803-
clear();
803+
if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
804+
_clear_internal();
805+
}
804806
}
805807
};
806808

doc/classes/Basis.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
<return type="Quaternion" />
139139
<description>
140140
Returns this basis's rotation as a [Quaternion].
141-
[b]Note:[/b] Quatenions are much more suitable for 3D math but are less intuitive. For user interfaces, consider using the [method get_euler] method, which returns Euler angles.
141+
[b]Note:[/b] Quaternions are much more suitable for 3D math but are less intuitive. For user interfaces, consider using the [method get_euler] method, which returns Euler angles.
142142
</description>
143143
</method>
144144
<method name="get_scale" qualifiers="const">

doc/classes/CollisionPolygon2D.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</brief_description>
66
<description>
77
A node that provides a polygon shape to a [CollisionObject2D] parent and allows to edit it. The polygon can be concave or convex. This can give a detection shape to an [Area2D], turn [PhysicsBody2D] into a solid object, or give a hollow shape to a [StaticBody2D].
8-
[b]Warning:[/b] A non-uniformly scaled [CollisionShape2D] will likely not behave as expected. Make sure to keep its scale the same on all axes and adjust its shape resource instead.
8+
[b]Warning:[/b] A non-uniformly scaled [CollisionPolygon2D] will likely not behave as expected. Make sure to keep its scale the same on all axes and adjust its polygon instead.
99
</description>
1010
<tutorials>
1111
</tutorials>

editor/plugins/path_3d_editor_plugin.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,19 @@ void Path3DGizmo::redraw() {
336336
// Path3D as a ribbon.
337337
ribbon_ptr[i] = p1;
338338

339-
// Fish Bone.
340-
const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
341-
const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
342-
343-
const int bone_idx = i * 4;
344-
345-
bones_ptr[bone_idx] = p1;
346-
bones_ptr[bone_idx + 1] = p_left;
347-
bones_ptr[bone_idx + 2] = p1;
348-
bones_ptr[bone_idx + 3] = p_right;
339+
if (i % 4 == 0) {
340+
// Draw fish bone every 4 points to reduce visual noise and performance impact
341+
// (compared to drawing it for every point).
342+
const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
343+
const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
344+
345+
const int bone_idx = i * 4;
346+
347+
bones_ptr[bone_idx] = p1;
348+
bones_ptr[bone_idx + 1] = p_left;
349+
bones_ptr[bone_idx + 2] = p1;
350+
bones_ptr[bone_idx + 3] = p_right;
351+
}
349352
}
350353

351354
add_collision_segments(_collision_segments);

scene/2d/line_2d.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ Rect2 Line2D::_edit_get_rect() const {
4141
if (_points.size() == 0) {
4242
return Rect2(0, 0, 0, 0);
4343
}
44-
Vector2 d = Vector2(_width, _width);
45-
Rect2 bounding_rect = Rect2(_points[0] - d, 2 * d);
44+
Vector2 min = _points[0];
45+
Vector2 max = min;
4646
for (int i = 1; i < _points.size(); i++) {
47-
bounding_rect.expand_to(_points[i] - d);
48-
bounding_rect.expand_to(_points[i] + d);
47+
min = min.min(_points[i]);
48+
max = max.max(_points[i]);
4949
}
50-
return bounding_rect;
50+
return Rect2(min, max - min).grow(_width);
5151
}
5252

5353
bool Line2D::_edit_use_rect() const {

scene/2d/path_2d.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ void Path2D::_notification(int p_what) {
138138
draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width, false);
139139
}
140140

141-
// Draw fish bones
141+
// Draw fish bone every 4 points to reduce visual noise and performance impact
142+
// (compared to drawing it for every point).
142143
{
143144
PackedVector2Array v2p;
144145
v2p.resize(3);
145146
Vector2 *w = v2p.ptrw();
146147

147-
for (int i = 0; i < sample_count; i++) {
148+
for (int i = 0; i < sample_count; i += 4) {
148149
const Vector2 p = r[i].get_origin();
149150
const Vector2 side = r[i].columns[1];
150151
const Vector2 forward = r[i].columns[0];

scene/3d/path_3d.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,19 @@ void Path3D::_update_debug_mesh() {
131131
// Path3D as a ribbon.
132132
ribbon_ptr[i] = p1;
133133

134-
// Fish Bone.
135-
const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
136-
const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
137-
138-
const int bone_idx = i * 4;
139-
140-
bones_ptr[bone_idx] = p1;
141-
bones_ptr[bone_idx + 1] = p_left;
142-
bones_ptr[bone_idx + 2] = p1;
143-
bones_ptr[bone_idx + 3] = p_right;
134+
if (i % 4 == 0) {
135+
// Draw fish bone every 4 points to reduce visual noise and performance impact
136+
// (compared to drawing it for every point).
137+
const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
138+
const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
139+
140+
const int bone_idx = i * 4;
141+
142+
bones_ptr[bone_idx] = p1;
143+
bones_ptr[bone_idx + 1] = p_left;
144+
bones_ptr[bone_idx + 2] = p1;
145+
bones_ptr[bone_idx + 3] = p_right;
146+
}
144147
}
145148

146149
Array ribbon_array;

servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co
22612261
for (uint32_t v = 0; v < rb->get_view_count(); v++) {
22622262
real_t fov = p_render_data->scene_data->cam_projection.get_fov();
22632263
real_t aspect = p_render_data->scene_data->cam_projection.get_aspect();
2264-
real_t fovy = p_render_data->scene_data->cam_projection.get_fovy(fov, aspect);
2264+
real_t fovy = p_render_data->scene_data->cam_projection.get_fovy(fov, 1.0 / aspect);
22652265
Vector2 jitter = p_render_data->scene_data->taa_jitter * Vector2(rb->get_internal_size()) * 0.5f;
22662266
RendererRD::FSR2Effect::Parameters params;
22672267
params.context = rb_data->get_fsr2_context();

servers/rendering/rendering_device_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Error RenderingDeviceDriver::_reflect_spirv(VectorView<ShaderStageSPIRVData> p_s
259259
}
260260
}
261261

262-
if (existing > 0) {
262+
if (existing >= 0) {
263263
r_reflection.specialization_constants.write[existing].stages.set_flag(stage_flag);
264264
} else {
265265
r_reflection.specialization_constants.push_back(sconst);

tests/core/math/test_color.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ TEST_CASE("[Color] Linear <-> sRGB conversion") {
160160
CHECK_MESSAGE(
161161
color_srgb.srgb_to_linear().is_equal_approx(Color(0.35, 0.5, 0.6, 0.7)),
162162
"The sRGB color converted back to linear color space should match the expected value.");
163+
CHECK_MESSAGE(
164+
Color(1.0, 1.0, 1.0, 1.0).srgb_to_linear() == (Color(1.0, 1.0, 1.0, 1.0)),
165+
"White converted from sRGB to linear should remain white.");
166+
CHECK_MESSAGE(
167+
Color(1.0, 1.0, 1.0, 1.0).linear_to_srgb() == (Color(1.0, 1.0, 1.0, 1.0)),
168+
"White converted from linear to sRGB should remain white.");
163169
}
164170

165171
TEST_CASE("[Color] Named colors") {

0 commit comments

Comments
 (0)