Skip to content

Commit

Permalink
Merge pull request godotengine#140 from jss2a98aj/cherry-pick-4.4-ass…
Browse files Browse the repository at this point in the history
…orted

[4.4 backport] Assorted fixes
  • Loading branch information
Bioblaze authored Nov 18, 2024
2 parents c869fb5 + 0911c92 commit 187cf0b
Show file tree
Hide file tree
Showing 29 changed files with 369 additions and 153 deletions.
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ if env.msvc and not methods.using_clang(env): # MSVC
"/wd4245",
"/wd4267",
"/wd4305", # C4305 (truncation): double to float or real_t, too hard to avoid.
"/wd4324", # C4820 (structure was padded due to alignment specifier)
"/wd4514", # C4514 (unreferenced inline function has been removed)
"/wd4714", # C4714 (function marked as __forceinline not inlined)
"/wd4820", # C4820 (padding added after construct)
Expand Down
47 changes: 42 additions & 5 deletions core/os/spin_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

#include "core/typedefs.h"

#ifdef _MSC_VER
#include <intrin.h>
#endif

#if defined(__APPLE__)

#include <os/lock.h>
Expand All @@ -52,19 +56,52 @@ class SpinLock {

#else

#include "core/os/thread.h"

#include <atomic>

class SpinLock {
mutable std::atomic_flag locked = ATOMIC_FLAG_INIT;
_ALWAYS_INLINE_ static void _cpu_pause() {
#if defined(_MSC_VER)
// ----- MSVC.
#if defined(_M_ARM) || defined(_M_ARM64) // ARM.
__yield();
#elif defined(_M_IX86) || defined(_M_X64) // x86.
_mm_pause();
#endif
#elif defined(__GNUC__) || defined(__clang__)
// ----- GCC/Clang.
#if defined(__i386__) || defined(__x86_64__) // x86.
__builtin_ia32_pause();
#elif defined(__arm__) || defined(__aarch64__) // ARM.
asm volatile("yield");
#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) // PowerPC.
asm volatile("or 27,27,27");
#elif defined(__riscv) // RISC-V.
asm volatile(".insn i 0x0F, 0, x0, x0, 0x010");
#endif
#endif
}

static_assert(std::atomic_bool::is_always_lock_free);

class alignas(Thread::CACHE_LINE_BYTES) SpinLock {
mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);

public:
_ALWAYS_INLINE_ void lock() const {
while (locked.test_and_set(std::memory_order_acquire)) {
// Continue.
while (true) {
bool expected = false;
if (locked.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
break;
}
do {
_cpu_pause();
} while (locked.load(std::memory_order_relaxed));
}
}

_ALWAYS_INLINE_ void unlock() const {
locked.clear(std::memory_order_release);
locked.store(false, std::memory_order_release);
}
};

Expand Down
18 changes: 18 additions & 0 deletions core/os/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "core/templates/safe_refcount.h"
#include "core/typedefs.h"

#include <new>

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
Expand Down Expand Up @@ -85,6 +87,20 @@ class Thread {
void (*term)() = nullptr;
};

#if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) // This would be OK with NDK >= 26.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winterference-size"
#endif
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#else
// At a negligible memory cost, we use a conservatively high value.
static constexpr size_t CACHE_LINE_BYTES = 128;
#endif

private:
friend class Main;

Expand Down Expand Up @@ -135,6 +151,8 @@ class Thread {

typedef uint64_t ID;

static constexpr size_t CACHE_LINE_BYTES = sizeof(void *);

enum : ID {
UNASSIGNED_ID = 0,
MAIN_ID = 1
Expand Down
25 changes: 21 additions & 4 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,35 @@ void CharString::copy_from(const char *p_cstr) {
/* String */
/*************************************************************************/

Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const {
// Splits the URL into scheme, host, port, path. Strip credentials when present.
Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const {
// Splits the URL into scheme, host, port, path, fragment. Strip credentials when present.
String base = *this;
r_scheme = "";
r_host = "";
r_port = 0;
r_path = "";
r_fragment = "";

int pos = base.find("://");
// Scheme
if (pos != -1) {
r_scheme = base.substr(0, pos + 3).to_lower();
base = base.substr(pos + 3, base.length() - pos - 3);
bool is_scheme_valid = true;
for (int i = 0; i < pos; i++) {
if (!is_ascii_alphanumeric_char(base[i]) && base[i] != '+' && base[i] != '-' && base[i] != '.') {
is_scheme_valid = false;
break;
}
}
if (is_scheme_valid) {
r_scheme = base.substr(0, pos + 3).to_lower();
base = base.substr(pos + 3, base.length() - pos - 3);
}
}
pos = base.find("#");
// Fragment
if (pos != -1) {
r_fragment = base.substr(pos + 1);
base = base.substr(0, pos);
}
pos = base.find("/");
// Path
Expand Down
2 changes: 1 addition & 1 deletion core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class String {
String c_escape_multiline() const;
String c_unescape() const;
String json_escape() const;
Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const;
Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const;

String property_name_encode() const;

Expand Down
35 changes: 25 additions & 10 deletions drivers/d3d12/rendering_device_driver_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2177,17 +2177,32 @@ void RenderingDeviceDriverD3D12::command_pipeline_barrier(CommandBufferID p_cmd_
}

// Define the barrier groups and execute.

D3D12_BARRIER_GROUP barrier_groups[3] = {};
barrier_groups[0].Type = D3D12_BARRIER_TYPE_GLOBAL;
barrier_groups[1].Type = D3D12_BARRIER_TYPE_BUFFER;
barrier_groups[2].Type = D3D12_BARRIER_TYPE_TEXTURE;
barrier_groups[0].NumBarriers = global_barriers.size();
barrier_groups[1].NumBarriers = buffer_barriers.size();
barrier_groups[2].NumBarriers = texture_barriers.size();
barrier_groups[0].pGlobalBarriers = global_barriers.ptr();
barrier_groups[1].pBufferBarriers = buffer_barriers.ptr();
barrier_groups[2].pTextureBarriers = texture_barriers.ptr();
cmd_list_7->Barrier(ARRAY_SIZE(barrier_groups), barrier_groups);
uint32_t barrier_groups_count = 0;

if (!global_barriers.is_empty()) {
D3D12_BARRIER_GROUP &barrier_group = barrier_groups[barrier_groups_count++];
barrier_group.Type = D3D12_BARRIER_TYPE_GLOBAL;
barrier_group.NumBarriers = global_barriers.size();
barrier_group.pGlobalBarriers = global_barriers.ptr();
}

if (!buffer_barriers.is_empty()) {
D3D12_BARRIER_GROUP &barrier_group = barrier_groups[barrier_groups_count++];
barrier_group.Type = D3D12_BARRIER_TYPE_BUFFER;
barrier_group.NumBarriers = buffer_barriers.size();
barrier_group.pBufferBarriers = buffer_barriers.ptr();
}

if (!texture_barriers.is_empty()) {
D3D12_BARRIER_GROUP &barrier_group = barrier_groups[barrier_groups_count++];
barrier_group.Type = D3D12_BARRIER_TYPE_TEXTURE;
barrier_group.NumBarriers = texture_barriers.size();
barrier_group.pTextureBarriers = texture_barriers.ptr();
}

cmd_list_7->Barrier(barrier_groups_count, barrier_groups);
}

/****************/
Expand Down
4 changes: 2 additions & 2 deletions drivers/gles3/shaders/canvas.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ void main() {

color_interp = color;

vertex = (canvas_transform * vec4(vertex, 0.0, 1.0)).xy;

if (use_pixel_snap) {
vertex = floor(vertex + 0.5);
// precision issue on some hardware creates artifacts within texture
// offset uv by a small amount to avoid
uv += 1e-5;
}

vertex = (canvas_transform * vec4(vertex, 0.0, 1.0)).xy;

vertex_interp = vertex;
uv_interp = uv;

Expand Down
4 changes: 2 additions & 2 deletions editor/debugger/editor_debugger_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ Error EditorDebuggerServerTCP::start(const String &p_uri) {

// Optionally override
if (!p_uri.is_empty() && p_uri != "tcp://") {
String scheme, path;
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
String scheme, path, fragment;
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
}
Expand Down
3 changes: 2 additions & 1 deletion editor/plugins/asset_library_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,8 @@ void EditorAssetLibrary::_request_image(ObjectID p_for, int p_asset_id, String p
String url_host;
int url_port;
String url_path;
Error err = trimmed_url.parse_url(url_scheme, url_host, url_port, url_path);
String url_fragment;
Error err = trimmed_url.parse_url(url_scheme, url_host, url_port, url_path, url_fragment);
if (err != OK) {
if (is_print_verbose_enabled()) {
ERR_PRINT(vformat("Asset Library: Invalid image URL '%s' for asset # %d.", trimmed_url, p_asset_id));
Expand Down
5 changes: 3 additions & 2 deletions modules/openxr/openxr_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1980,8 +1980,9 @@ bool OpenXRAPI::poll_events() {
if (local_floor_emulation.enabled) {
local_floor_emulation.should_reset_floor_height = true;
}
if (event->poseValid && xr_interface) {
xr_interface->on_pose_recentered();

if (xr_interface) {
xr_interface->on_reference_space_change_pending();
}
} break;
case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED: {
Expand Down
10 changes: 8 additions & 2 deletions modules/openxr/openxr_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,12 @@ void OpenXRInterface::process() {
if (head.is_valid()) {
head->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity, head_confidence);
}

if (reference_stage_changing) {
// Now that we have updated tracking information in our updated reference space, trigger our pose recentered signal.
emit_signal(SNAME("pose_recentered"));
reference_stage_changing = false;
}
}

void OpenXRInterface::pre_render() {
Expand Down Expand Up @@ -1318,8 +1324,8 @@ void OpenXRInterface::on_state_exiting() {
emit_signal(SNAME("instance_exiting"));
}

void OpenXRInterface::on_pose_recentered() {
emit_signal(SNAME("pose_recentered"));
void OpenXRInterface::on_reference_space_change_pending() {
reference_stage_changing = true;
}

void OpenXRInterface::on_refresh_rate_changes(float p_new_rate) {
Expand Down
3 changes: 2 additions & 1 deletion modules/openxr/openxr_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class OpenXRInterface : public XRInterface {
private:
OpenXRAPI *openxr_api = nullptr;
bool initialized = false;
bool reference_stage_changing = false;
XRInterface::TrackingStatus tracking_state;

// At a minimum we need a tracker for our head
Expand Down Expand Up @@ -207,7 +208,7 @@ class OpenXRInterface : public XRInterface {
void on_state_stopping();
void on_state_loss_pending();
void on_state_exiting();
void on_pose_recentered();
void on_reference_space_change_pending();
void on_refresh_rate_changes(float p_new_rate);
void tracker_profile_changed(RID p_tracker, RID p_interaction_profile);

Expand Down
4 changes: 2 additions & 2 deletions modules/websocket/editor/editor_debugger_server_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ Error EditorDebuggerServerWebSocket::start(const String &p_uri) {

// Optionally override
if (!p_uri.is_empty() && p_uri != "ws://") {
String scheme, path;
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
String scheme, path, fragment;
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
}
Expand Down
3 changes: 2 additions & 1 deletion modules/websocket/emws_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ Error EMWSPeer::connect_to_url(const String &p_url, Ref<TLSOptions> p_tls_option
String host;
String path;
String scheme;
String fragment;
int port = 0;
Error err = p_url.parse_url(scheme, host, port, path);
Error err = p_url.parse_url(scheme, host, port, path, fragment);
ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);

if (scheme.is_empty()) {
Expand Down
3 changes: 2 additions & 1 deletion modules/websocket/wsl_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ Error WSLPeer::connect_to_url(const String &p_url, Ref<TLSOptions> p_options) {
String host;
String path;
String scheme;
String fragment;
int port = 0;
Error err = p_url.parse_url(scheme, host, port, path);
Error err = p_url.parse_url(scheme, host, port, path, fragment);
ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
if (scheme.is_empty()) {
scheme = "ws://";
Expand Down
23 changes: 14 additions & 9 deletions scene/2d/gpu_particles_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ void GPUParticles2D::_notification(int p_what) {
RS::get_singleton()->particles_set_speed_scale(particles, 0);
}
set_process_internal(true);
set_physics_process_internal(true);
previous_position = get_global_position();
} break;

Expand All @@ -711,15 +712,6 @@ void GPUParticles2D::_notification(int p_what) {
} break;

case NOTIFICATION_INTERNAL_PROCESS: {
const Vector3 velocity = Vector3((get_global_position() - previous_position).x, (get_global_position() - previous_position).y, 0.0) /
get_process_delta_time();

if (velocity != previous_velocity) {
RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
previous_velocity = velocity;
}
previous_position = get_global_position();

if (one_shot) {
time += get_process_delta_time();
if (time > emission_time) {
Expand All @@ -739,6 +731,19 @@ void GPUParticles2D::_notification(int p_what) {
}
}
} break;

case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
// Update velocity in physics process, so that velocity calculations remain correct
// if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
const Vector3 velocity = Vector3((get_global_position() - previous_position).x, (get_global_position() - previous_position).y, 0.0) /
get_physics_process_delta_time();

if (velocity != previous_velocity) {
RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
previous_velocity = velocity;
}
previous_position = get_global_position();
} break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion scene/2d/light_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ Vector2 PointLight2D::get_texture_offset() const {
}

PackedStringArray PointLight2D::get_configuration_warnings() const {
PackedStringArray warnings = Node2D::get_configuration_warnings();
PackedStringArray warnings = Light2D::get_configuration_warnings();

if (!texture.is_valid()) {
warnings.push_back(RTR("A texture with the shape of the light must be supplied to the \"Texture\" property."));
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/physics/rigid_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ void RigidBody2D::_notification(int p_what) {
PackedStringArray RigidBody2D::get_configuration_warnings() const {
Transform2D t = get_transform();

PackedStringArray warnings = CollisionObject2D::get_configuration_warnings();
PackedStringArray warnings = PhysicsBody2D::get_configuration_warnings();

if (ABS(t.columns[0].length() - 1.0) > 0.05 || ABS(t.columns[1].length() - 1.0) > 0.05) {
warnings.push_back(RTR("Size changes to RigidBody2D will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
Expand Down
Loading

0 comments on commit 187cf0b

Please sign in to comment.