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

[AOT] Integrating Taichi AOT with Unity #4808

Closed
YuCrazing opened this issue Apr 17, 2022 · 7 comments
Closed

[AOT] Integrating Taichi AOT with Unity #4808

YuCrazing opened this issue Apr 17, 2022 · 7 comments
Assignees
Labels
feature request Suggest an idea on this project

Comments

@YuCrazing
Copy link
Contributor

YuCrazing commented Apr 17, 2022

Concisely describe the proposed feature
As a potential large need of the game industry, we should give support to integrate Taichi AOT with commercial game engines. The first one could be Unity.

Describe the solution you'd like (if any)
A reasonable way to integrate Taichi AOT into Unity is via unity native plugin interface, which provides the different low-level graphics APIs (e.g. vulkan/metal/d3d11/d3d12/opengl) and provides the ability to access Unity's rendering pipeline. The NativeRenderingPlugin (NRP) repo demonstrates how to use Unity native plugin interface.

A expected usage of Taichi AOT in Unity
Considering the new features and the portability, firstly, we can explore using Taichi AOT in Unity both with vulkan backend. And for simplicity, we can just treat the Taichi AOT module as a computing module (like compute shader) at the current stage.

Based on C API for Taichi AOT, we can give a user's expected usage of Taichi AOT:

  1. Get the vulkan device from Unity to initialize the Taichi AOT module.
  2. Use taichi_set_runtime_context_arg_* APIs to set kernel's parameters.
  3. Run kernels in the Taichi AOT module.
  4. Copy data back to Unity buffer (via native buffer pointer) to render.

Detailed usage of Unity native plugin interface
When the Unity plugin loads, UnityPluginLoad(IUnityInterfaces* unityInterfaces) is called automatically. You can get a IUnityInterfaces instance here.

Then you can get different low-level interfaces via IUnityInterfaces by using:

interfaces->Get<IUnityGraphicsVulkan>(); // vulkan
// or
interfaces->Get<IUnityGraphicsD3D11>(); // d3d11
// or
interfaces->Get<IUnityGraphicsD3D12v2>(); // d3d12

Let's take vulkan as an example:

The structure of IUnityGraphicsVulkan is source code. Here you can use IUnityGraphicsVulkan::Instance() and IUnityGraphicsVulkan::AccessBuffer(...) to access Unity's vulkan device/queue/buffers.

@YuCrazing YuCrazing added the feature request Suggest an idea on this project label Apr 17, 2022
@k-ye
Copy link
Member

k-ye commented Apr 18, 2022

Excited to see that Taichi is moving toward this direction!

Could you also write down the concrete interfaces provided by Unity RHI pluggins? E.g. IUnityGraphicsVulkan. Thanks!

@k-ye k-ye moved this to Untriaged in Taichi Lang Apr 18, 2022
@k-ye k-ye added this to Taichi Lang Apr 18, 2022
@k-ye k-ye moved this from Untriaged to Todo in Taichi Lang Apr 18, 2022
@YuCrazing
Copy link
Contributor Author

Detailed usage of IUnityGraphicsVulkan is added.

@k-ye k-ye moved this from Todo to In Progress in Taichi Lang Apr 21, 2022
@k-ye
Copy link
Member

k-ye commented Apr 28, 2022

Note: when using the Vulkan resources (e.g. VkInstance, VkDevice) from Unity, or any other platform, note that we need to call the corresponding volk init methods on them. This is done around these places:

if (!VulkanLoader::instance().init()) {

VulkanLoader::instance().load_instance(instance_);

cc @bobcao3

@YuCrazing
Copy link
Contributor Author

Update: Now we can run taichi implicit fem demo on unity (linux platform, vulkan backend) using unity native plugin interface.

image

@YuCrazing
Copy link
Contributor Author

YuCrazing commented May 13, 2022

Implementation details:

  1. Initialize taichi AOT module using unity vulkan instance at the first frame.
  2. Execute computing in taichi AOT module and transfer data to unity vertex buffer every frame.

It sounds quite straightforward. However, there are still some critical problems that need to be solved next:

Taichi side:

  • When using Taichi AOT C++ demo as a shared library in the NRP project, there are many vulkan header conflicts. We should avoid including vulkan headers in taichi vulkan backend header files as possible.
  • Taichi AOT module should provide APIs to let users specify buffer (ndarray) size.

Unity side:

  • Now taichi buffer -> unity vertex buffer data transfer happens on CPU, we need to explore to transfer data on GPU directly via unity native plugin interface.

@PENGUINLIONG
Copy link
Member

PR #5204 introduced a C# language binding generator for unity integration, so C# scripts can directly interact with the C-APIs as a native plugin in the future. It feels to me that it solves most of the problems we have atm, including on-device copy. I'm gonna continue to work on the implicit-fem demo and see if it feels good to Unity developers.

@YuCrazing YuCrazing assigned PENGUINLIONG and unassigned YuCrazing Jul 7, 2022
@PENGUINLIONG
Copy link
Member

Unity integration is considered done!

Repository owner moved this from In Progress to Done in Taichi Lang Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project
Projects
Status: Done
Development

No branches or pull requests

4 participants