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

WorkerThreadPool expose add_template_group_task and add_template_task #1368

Open
ozzr opened this issue Jan 21, 2024 · 4 comments
Open

WorkerThreadPool expose add_template_group_task and add_template_task #1368

ozzr opened this issue Jan 21, 2024 · 4 comments
Labels
enhancement This is an enhancement on the current functionality
Milestone

Comments

@ozzr
Copy link

ozzr commented Jan 21, 2024

Godot version

master

godot-cpp version

master

System information

Windows 10

Issue description

Allow using add_template_group_task and add_template_task methods from WorkerThreadPool class that are available inside modules but in extensions.

Steps to reproduce

look at godot engine source code usage of the mentioned methods

Minimal reproduction project

create a templated method

@dsnopek
Copy link
Contributor

dsnopek commented Apr 9, 2024

Thanks!

Now that we have callable_mp(), you could simulate add_template_group_task(). So, rather than doing:

WorkerThreadPool::get_singleton()->add_template_group_task(this, &MyClass::method, data, ...);

... you can do:

WorkerThreadPool::get_singleton()->add_group_task(callable_mp(this, &MyClass::method).bind(data), ...);

I think that code should work in a module as well!

That said, for compatibility, we could probably generate an add_template_group_task() that would do the callable_mp() thing for you.

@dsnopek dsnopek added the enhancement This is an enhancement on the current functionality label Apr 9, 2024
@dsnopek dsnopek added this to the 4.x milestone Apr 9, 2024
@ozzr
Copy link
Author

ozzr commented Apr 10, 2024

Hi, as long as I know Callables only bind Variant arguments, plus maybe it is intended, but inside modules I havent been able of binding any PackedVector as an argument to a callable. So I started using the add_template_group_task method with pointers to arrays.
This is a method taken from my landscape system under development:

// Inside the header
#ifdef GDEXTENSION
// Workaround Method
	Vector<Vector2i>              _temp_save_region_keys;
	void                          _thread_save_regions(uint32_t index);
#else
//Original Method
	void                          _thread_save_regions(uint32_t index,const Vector2i* pool);
#endif // GDEXTENSION

Also

WorkerThreadPool::get_singleton()->add_template_group_task allows passing data using custom structs and there are diferences in the way each WorkerThreadPool::get_singleton()->add_XXXX_task method schedule the task. Just by looking at the source code I can see this:

// method add_template_group_task
_add_group_task(Callable(), nullptr, nullptr, ud, p_elements, p_tasks, p_high_priority, p_description);

// method add_native_group_task
_add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);

// method add_group_task
_add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);

So, in summary, using add_template_group_task is good for passing custom structs, and using a Callable for mimiking the behaviour will most likely not work

as a plus.... I have read a big chunk of godot source code by now and I have not find any internal use of a Callable binding a PackedVector, instead, for cases where the use of a Vector or PackedVector is necesary, the templated_task method is the one used

@dsnopek
Copy link
Contributor

dsnopek commented Apr 11, 2024

It's not ideal, but you can store pointers as uint64_t as a way to put them into a Variant. This strategy is used in a bunch of places in the XR APIs exposed to GDExtension.

@ozzr
Copy link
Author

ozzr commented Apr 11, 2024

Interesting, I will take a look at it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is an enhancement on the current functionality
Projects
None yet
Development

No branches or pull requests

2 participants