Skip to content

Commit

Permalink
[unity]il2cpp优化改为正式特性,开启宏去掉EXPERIMENTAL,改为PUERTS_IL2CPP_OPTIMIZATION,…
Browse files Browse the repository at this point in the history
…由于现在不用生成代码到native_src_il2cpp,所以生成目录的宏改为PUERTS_CPP_OUTPUT_TO_UPM,也不用自己编译plugin,文档做相应的调整。描述上这也不作为一个模式,而是一个特性。后续Plugin和标准模式合并后,il2cpp优化版本会改为默认打开
  • Loading branch information
chexiongsheng committed Nov 8, 2024
1 parent 47a5505 commit 662f93c
Show file tree
Hide file tree
Showing 42 changed files with 171 additions and 205 deletions.
4 changes: 2 additions & 2 deletions doc/unity/en/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ If you encounter this problem, first check whether you are using the DefaultLoad

`Puer003` is similar to `Puer002`, except that the `ReadFile` function of Loader returns empty. You also need to check why `FileExists` or `Resolve` returns true, but `ReadFile` reads empty.

#### [Puer W001] You are not using upm to install PuerTS or did not add 'PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM' definition.
#### [Puer W001] You are not using upm to install PuerTS or did not add 'PUERTS_CPP_OUTPUT_TO_UPM' definition.

In xil2cpp mode, the product of PuerTS->Generate needs to be recompiled by the plugin to be used. Therefore, Puer needs to put the product of Generate back to the plugin source code directory.

If you did not use the recommended way of [document](./performance/il2cpp.md) to `git clone` and add PuerTS from the package manager, plus adding the `PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM` macro, you need to take the compiled product and compile the plugin yourself.
If you did not use the recommended way of [document](./performance/il2cpp.md) to `git clone` and add PuerTS from the package manager, plus adding the `PUERTS_CPP_OUTPUT_TO_UPM` macro, you need to take the compiled product and compile the plugin yourself.

## Installation relavanted

Expand Down
28 changes: 7 additions & 21 deletions doc/unity/en/performance/il2cpp.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
# xIl2cpp Mode - Special Optimization Mode for Unity il2cpp
# Optimization for Unity il2cpp

> Available Version: >2.0.0
xIl2cpp mode is a new mode added in Puer2.0 version. As the name suggests, the optimization is only for Unity Il2cpp backend. The name xIl2cpp pays tribute to xLua, the predecessor of PuerTS, and represents the technology that enhances optimization based on Il2cpp.

In simple terms, xIl2cpp mode allows il2cpp to directly interact with v8, without going through C# PInvoke to call native plugins. This reduces cross-language consumption and ultimately greatly improves performance, as shown in [xil2cpp mode performance data](./index.md).

There will be significant changes in the way xIl2cpp mode is used, such as different steps for compiling plugins and wrappers, but there is no need to make any changes to the API and usage mechanism.
In simple terms, the idea behind il2cpp optimization is to bypass PInvoke and directly access C# through il2cpp interfaces, reducing the overhead of cross-language calls. This ultimately leads to a significant performance improvement. For more details, see the[xil2cpp mode performance data](./index.md).

## Installation
If you decide to use xIl2cpp mode, it is currently recommended to use the `GitHub Clone and Install with Unity UPM` method mentioned in the [installation guide](../install). The repository address needs to be changed to `https://github.com/Tencent/puerts.git`, and the added path is `[puerts]/unity/Assets/core/upm/package.json`.

The reason why you need to clone the source code is that you need to compile Puer's binary plugin yourself to use the xIl2cpp capability.
To use Il2cpp Optimization, you need to download the Plugin_il2cpp package separately and extract it to your project directory.

Then, you need to first understand the content of the [compilation guide](../other/building). The compilation of the xIl2cpp version is slightly different but similar in general (for example, you no longer need to download the backend yourself).
Alternatively, you can compile it yourself according to the [compilation guide](../other/building).

## Steps to Use
1. Install the PuerTS upm package according to the above steps.
2. Compile the mono version plugin: cd to `puer directory/unity/native_src/`, enter the compilation command that matches your platform, such as `node ../cli make --backend v8_9.4 --platform win --arch x64 --config Debug` (the compilation command itself actually recognizes your platform, so you can also only enter `node ../cli make --backend v8_9.4 --config Debug`).
3. Enter Unity's `Player Settings` and add two Scripting Define Symbols: `PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM`, `EXPERIMENTAL_IL2CPP_PUERTS`. You can also switch the script backend to `il2cpp`. Wait for the script to compile.
4. Generate the code required for compilation: click `Tools/PuerTS/Generate for xIl2cpp mode(All in One)` in Unity. Then switch to cd to `puer directory/unity/native_src_il2cpp`, and enter the same compilation command as step 2.

### Some Detailed Explanations in the Steps to Use
* Because we try to let v8 and il2cpp interact directly, the Wrapper in the il2cpp version is in the form of C++ rather than C#. The FunctionBridge.h generated by `Generate for xIl2cpp mode` plays the role of the original Static Wrapper.
* FunctionBridge.h currently needs to be compiled into nativePlugins, so xIl2cpp mode requires you to frequently compile native plugins yourself.
* Functions with different function signatures will generate a corresponding wrapper function in FunctionBridge.h. Of course, we also support calling through reflection, which has slightly lower performance.
* `Generate for xIl2cpp mode` will traverse all Assemblies and generate wrappers for all functions. In addition, `generate/FunctionBridge.h(Configure)` is also provided, which will only generate wrappers for the configured classes in the generation list, and the rest will be called using reflection mechanism.
* `Generate/FunctionBridge.h` also generates a bridge for calling from C# to JS, so UsingFunc and UsingAction are no longer needed.
* The `PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM` in the installation steps is to allow FunctionBridge.h to be generated directly to the native_src_il2cpp directory and Puerts_il2cpp.cpp to be generated to the Plugins directory. If you are not using the upm method to install puerts, you can omit this def and copy these files to the correct location yourself.
1. Install the PuerTS UPM package as described above.
2. Go to Unity's `Player Settings` and add two Scripting Define Symbols: `PUERTS_CPP_OUTPUT_TO_UPM;PUERTS_IL2CPP_OPTIMIZATION`. You may also switch the script backend to il2cpp. Wait for the script compilation to complete.
3. Generate the code required for compilation: Click on Unity's Menu: `Tools/PuerTS/Generate for Il2cpp optimization(All in One)`.

### FAQ
1. The header file hash_map cannot be found when building iOS.
Expand Down
4 changes: 0 additions & 4 deletions doc/unity/zhcn/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@

`Puer003``Puer002`是类似的,只不过Puer003是Loader的ReadFile接口返回了空。你同时要检查一下为什么`FileExists``Resolve`返回了存在,但ReadFile时却读到了空

#### [Puer W001] You are not using upm to install PuerTS or did not add 'PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM' definition.
在使用xil2cpp模式时,PuerTS->Generate的产物需要经过plugin的重新编译才可用。因此Puer需要将Generate的产物塞回到plugin源码目录处。
如果你没有采用[文档](./performance/il2cpp.md)建议的方式`git clone`并从package manager里添加PuerTS,外加添加了`PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM`宏,你需要自行将编译产物拿去进行plugin编译。

## 安装相关

#### 版本中的 Stable/Latest,RC/Preview 分别意味着什么?
Expand Down
28 changes: 6 additions & 22 deletions doc/unity/zhcn/performance/il2cpp.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
# 为Unity il2cpp特殊优化的模式 - xIl2cpp模式
# il2cpp优化特性
> 可用版本:>2.0.0
xIl2cpp模式是Puer2.0版本新加的模式。顾名思义,优化仅针对Unity Il2cpp backend。xIl2cpp这个名字一方面是致敬PuerTS的前身xLua,也代表它是基于Il2cpp进行加强优化的技术。

简单粗浅地说,xIl2cpp模式是让il2cpp直接与v8交互,不再经由C# PInvoke调用native plugin。减少跨语言的消耗。最终使得性能表现大幅提升,详见[xil2cpp模式性能数据](./index.md)

xIl2cpp模式在使用方式上会有较大的变化,比如需要自己编译Plugin与wrapper生成的步骤不同,API和使用机制上无需做任何改动。
简单地说,il2cpp优化的思路是绕过 PInvoke直接通过il2cpp的接口访问C#,减少跨语言的消耗。最终使得性能表现大幅提升,详见[il2cpp优化特性性能数据](./index.md)

## 安装
如果决定采用xIl2cpp模式,目前只建议使用[安装指南](../install)中提到的`GitHub Clone 并用 Unity UPM 安装`。且仓库地址需要改为`https://github.com/Tencent/puerts.git`,添加的路径为`[puerts]/unity/Assets/core/upm/package.json`

之所以需要你把源码clone下来,是因为你需要自行编译Puer的二进制Plugin,才能使用到xIl2cpp的能力。
要使用Il2cpp优化,需要额外下载Plugin_il2cpp包,解压到项目目录即可。

随后,你需要先了解一下[编译指南](../other/building)的内容,xIl2cpp版本的编译稍有不同但大致类似(比如不再需要自行下载backend)
或者根据[编译指南](../other/building)自行编译

## 使用步骤
1. 按照上述步骤安装好PuerTS的upm包。
2. 编译mono版本plugin:cd到`puer目录/unity/native_src/`,输入符合你平台的编译命令,比如`node ../cli make --backend v8_9.4 --platform win --arch x64 --config Debug`。(编译命令本身其实也会识别你的平台,所以你也可以只输入`node ../cli make --backend v8_9.4 --config Debug`
3. 进入Unity的`Player Settings`,添加两个Scripting Define Symbols: `PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM;EXPERIMENTAL_IL2CPP_PUERTS`。顺便可将script backend切换为`il2cpp`。等待脚本编译。
4. 生成编译所需的代码:点击Unity的`Tools/PuerTS/Generate for xIl2cpp mode(All in One)`。然后切出去cd到`puer目录/unity/native_src_il2cpp`,输入和步骤2相同的编译命令。

### 使用步骤里的一些详细解释
* 由于我们尽量让v8和il2cpp直接交互,所以il2cpp版本里,Wrapper就是C++的形式而非C#的形式。`Generate for xIl2cpp mode`里生成的FunctionBridge.h承担的就是原有的Static Wrapper角色。
* FunctionBridge.h目前是需要编译进nativePlugins的,所以xIl2cpp模式需要你经常自行编译native plugin。
* 不同函数签名的函数会在FunctionBridge.h里生成一个对应的wrapper函数。当然我们也支持反射的方式调用,性能略有损耗。
* `Generate for xIl2cpp mode`中,会全量遍历所有Assembly,生成所有函数的wrapper。另外也提供了`generate/FunctionBridge.h(Configure)`,只会为生成列表中配置的类生成wrapper,其余使用反射机制调用。
* `Generate/FunctionBridge.h`同时还会生成C#到JS调用的bridge,因此不再需要UsingFunc和UsingAction
* 安装步骤中`PUERTS_CPP_OUTPUT_TO_NATIVE_SRC_UPM`就是为了让FunctionBridge.h直接生成到native_src_il2cpp目录,Puerts_il2cpp.cpp生成到Plugins目录。如果你不是使用upm方式安装puerts,则可以不添加该def,自行将这些文件复制到正确位置。

2. 进入Unity的`Player Settings`,添加两个Scripting Define Symbols: `PUERTS_CPP_OUTPUT_TO_UPM;PUERTS_IL2CPP_OPTIMIZATION`。顺便可将script backend切换为`il2cpp`。等待脚本编译。
3. 生成编译所需的代码:点击Unity的`Tools/PuerTS/Generate for Il2cpp optimization(All in One)`

### FAQ
1. ios构建时报hash_map头找不到。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FOR } from './tte.mjs'

export default function TypingTemplate(rawInfo) {
return `
#if EXPERIMENTAL_IL2CPP_PUERTS && ENABLE_IL2CPP
#if PUERTS_IL2CPP_OPTIMIZATION && ENABLE_IL2CPP
using System;
using System.Collections.Generic;
using System.Reflection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ namespace PuertsStaticWrap
{
return new RegisterInfo
{
#if !EXPERIMENTAL_IL2CPP_PUERTS
#if !PUERTS_IL2CPP_OPTIMIZATION
BlittableCopy = ${item.BlittableCopy},
#endif
Members = new System.Collections.Generic.Dictionary<string, MemberRegisterInfo>
{
${FOR(listToJsArray(item.Members), member=> `
{"${member.Name}${member.IsStatic ? '_static' : ''}", new MemberRegisterInfo { Name = "${member.Name}", IsStatic = ${member.IsStatic}, MemberType = MemberType.${member.MemberType}, UseBindingMode = BindingMode.${member.UseBindingMode}
#if !EXPERIMENTAL_IL2CPP_PUERTS
#if !PUERTS_IL2CPP_OPTIMIZATION
${member.UseBindingMode == 'FastBinding' ? referWrapperMember(item.WrapperName, member.Constructor, member.Method, member.PropertyGetter, member.PropertySetter) : ''}
#endif
}},
Expand All @@ -50,7 +50,7 @@ namespace PuertsStaticWrap
jsEnv.AddRegisterInfoGetter(typeof(${CS.Puerts.TypeExtensions.GetFriendlyName(item.Type)}), GetRegisterInfo_${item.WrapperName});`
if (item.BlittableCopy) {
ret += `
#if !EXPERIMENTAL_IL2CPP_PUERTS
#if !PUERTS_IL2CPP_OPTIMIZATION
${item.BlittableCopy ? item.WrapperName + ".InitBlittableCopy(jsEnv);": ""}
#endif`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default function TypingTemplate(data) {
}

return $
`#if !(EXPERIMENTAL_IL2CPP_PUERTS && ENABLE_IL2CPP)
`#if !(PUERTS_IL2CPP_OPTIMIZATION && ENABLE_IL2CPP)
${FOR(toJsArray(data.Namespaces), name => `
using ${name};`
)}
Expand Down
Loading

0 comments on commit 662f93c

Please sign in to comment.