Babybus-u3d技術交流-減少Draw Call(批渲染)
[http://docs.unity3d.com/Manual/DrawCallBatching.html
參考翻譯 [http://game.ceeger.com/Manual/DrawCallBatching.html
描繪調用批處理 Draw Call Batching
Date:2013-08-11 08:47
To draw an object on the screen, the engine has to issue a draw call to the graphics API (OpenGL ES
in the case of iOS). Every single draw call requires a significant amount of work on the part of the graphics API, causing significant performance overhead on the CPU side.
在屏幕上渲染物體,引擎需要發(fā)出一個描繪調用來訪問圖形API(iOS系統(tǒng)中為OpenGL ES)。每個描繪調用需要進行大量的工作來訪問圖形API,從而導致了CPU方面顯著的性能開銷长豁。
Unity combines a number of objects at runtime and draws them together with a single draw call. This operation is called "batching". The more objects Unity can batch together, the better rendering performance you will get.
Unity在運行時可以將一些物體進行合并玫芦,從而用一個描繪調用來渲染他們恩掷。這一操作捣鲸,我們稱之為"批處理"。一般來說处渣,Unity批處理的物體越多,你就會得到越好的渲染性能蛛砰。
Built-in batching support in Unity has significant benefit over simply combining geometry in the modeling tool (or using the CombineChildren
script from the Standard Assets package). Batching in Unity happens after
visibility determination step. The engine does culling on each object individually, and the amount of rendered geometry is going to be the same as without batching. Combining geometry in the modeling tool, on the other hand, prevents effecient culling and results in much higher amount of geometry being rendered.
Unity中內建的批處理機制所達到的效果要明顯強于使用幾何建模工具(或使用Standard Assets包中的CombineChildren腳本)的批處理效果罐栈。這是因為,Unity引擎的批處理操作是在物體的可視裁剪操作之后進行的泥畅。Unity先對每個物體進行裁剪荠诬,然后再進行批處理,這樣可以使渲染的幾何總量在批處理前后保持不變位仁。但是柑贞,使用幾何建模工具來拼合物體,會妨礙引擎對其進行有效的裁剪操作聂抢,從而導致引擎需要渲染更多的幾何面片钧嘶。
Materials 材質
Only objects sharing the same material can be batched together. Therefore, if you want to achieve good batching, you need to share as many materials among different objects as possible.
只有擁有相同材質的物體才可以進行批處理。因此琳疏,如果你想要得到良好的批處理效果有决,你需要在程序中盡可能地復用材質和物體。
If you have two identical materials which differ only in textures, you can combine those textures into a single big texture - a process often called [texture atlasing
. Once textures are in the same atlas, you can use single material instead.
如果你的兩個材質僅僅是紋理不同空盼,那么你可以通過紋理拼合操作來將這兩張紋理拼合成一張大的紋理书幕。一旦紋理拼合在一起,你就可以使用這個單一材質來替代之前的兩個材質了我注。
If you need to access shared material properties from the scripts, then it is important to note that modifying [Renderer.material
will create a copy of the material. Instead, you should use [Renderer.sharedMaterial
to keep material shared.
如果你需要通過腳本來訪問復用材質屬性按咒,那么值得注意的是改變Renderer.material 將會造成一份材質的拷貝。因此,你應該使用Renderer.sharedMaterial 來保證材質的共享狀態(tài)励七。
Dynamic Batching 動態(tài)批處理
Unity can automatically batch moving objects into the same draw call if they share the same material.
如果動態(tài)物體共用著相同的材質智袭,那么Unity會自動對這些物體進行批處理。
Dynamic batching is done automatically and does not require any additional effort on your side.
動態(tài)批處理操作是自動完成的掠抬,并不需要你進行額外的操作吼野。
Tips: 提示:
Batching dynamic objects has certain overhead per vertex
, so batching is applied only to meshes containing less than 900
vertex attributes in total.批處理動態(tài)物體需要在每個頂點上進行一定的開銷,所以動態(tài)批處理僅支持小于900頂點的網格物體两波。
If your shader is using Vertex Position, Normal and single UV, then you can batch up to 300 verts and if your shader is using Vertex Position, Normal, UV0, UV1 and Tangent, then only 180 verts. 如果你的著色器使用頂點位置瞳步,法線和UV值三種屬性,那么你只能批處理300頂點以下的物體腰奋;如果你的著色器需要使用頂點位置单起,法線,UV0劣坊,UV1和切向量嘀倒,那你只能批處理180頂點以下的物體。
Please note: attribute count limit might be changed in future
請注意:屬性數量的限制可能會在將來進行改變局冰。
Don't use scale. Objects with scale (1,1,1) and (2,2,2) won't batch. 不要使用縮放测蘑。分別擁有縮放大小(1,1,1) 和(2,2,2)的兩個物體將不會進行批處理。
Uniformly scaled objects won't be batched with non-uniformly scaled ones.統(tǒng)一縮放的物體不會與非統(tǒng)一縮放的物體進行批處理康二。
Objects with scale (1,1,1) and (1,2,1) won't be batched. On the other hand (1,2,1) and (1,3,1) will be. 使用縮放尺度(1,1,1) 和 (1,2,1)的兩個物體將不會進行批處理碳胳,但是使用縮放尺度(1,2,1) 和(1,3,1)的兩個物體將可以進行批處理。
Using different material instances will cause batching to fail. 使用不同材質的實例化物體(instance)將會導致批處理失敗沫勿。
Objects with lightmaps have additional (hidden) material parameter: offset/scale in lightmap, so lightmapped objects won't be batched (unless they point to same portions of lightmap) 擁有l(wèi)ightmap的物體含有額外(隱藏)的材質屬性挨约,比如:lightmap的偏移和縮放系數等。所以藕帜,擁有l(wèi)ightmap的物體將不會進行批處理(除非他們指向lightmap的同一部分)烫罩。
Multi-pass shaders will break batching. E.g. Almost all unity shaders supports several lights in forward rendering, effectively doing additional pass for them 多通道的shader會妨礙批處理操作惜傲。比如洽故,幾乎unity中所有的著色器在前向渲染中都支持多個光源,并為它們有效地開辟多個通道盗誊。
Using instances of a prefab automatically are using the same mesh and material. 預設體的實例會自動地使用相同的網格模型和材質时甚。
Static Batching 靜態(tài)批處理
Static batching, on the other hand, allows the engine to reduce draw calls for geometry of any size (provided it does not move and shares the same material). Static batching is significantly more efficient than dynamic batching. You should choose static batching as it will require less CPU power.
相對而言,靜態(tài)批處理操作允許引擎對任意大小的幾何物體進行批處理操作來降低描繪調用(只要這些物體不移動哈踱,并且擁有相同的材質)荒适。因此,靜態(tài)批處理比動態(tài)批處理更加有效开镣,你應該盡量低使用它刀诬,因為它需要更少的CPU開銷。
In order to take advantage of static batching, you need explicitly specify that certain objects are static and will not
move, rotate or scale in the game. To do so, you can mark objects as static using the Static checkbox in the Inspector:
為了更好地使用靜態(tài)批處理邪财,你需要明確指出哪些物體是靜止的陕壹,并且在游戲中永遠不會移動质欲、旋轉和縮放。想完成這一步糠馆,你只需要在檢測器(Inspector)中將Static復選框打勾即可嘶伟,如下圖所示:
Using static batching will require additional memory for storing the combined geometry. If several objects shared the same geometry before static batching, then a copy of geometry will be created for each object, either in the Editor or at runtime. This might not always be a good idea - sometimes you will have to sacrifice rendering performance by avoiding static batching for some objects to keep a smaller memory footprint. For example, marking trees as static in a dense forest level can have serious memory impact.
使用靜態(tài)批處理操作需要額外的內存開銷來儲存合并后的幾何數據。在靜態(tài)批處理之前又碌,如果一些物體共用了同樣的幾何數據九昧,那么引擎會在編輯以及運行狀態(tài)對每個物體創(chuàng)建一個幾何數據的備份。這并不總是一個好的想法毕匀,因為有時候铸鹰,你將不得不犧牲一點渲染性能來防止一些物體的靜態(tài)批處理,從而保持較少的內存開銷皂岔。比如掉奄,將濃密森里中樹設為Static,會導致嚴重的內存開銷凤薛。
Static batching is only available in Unity iOS Advanced.
靜態(tài)批處理目前只支持Unity iOS Advanced姓建。
Further Reading 進一步閱讀
Measuring performance with the Built-in Profiler
用內置分析器測試性能
Rendering Statistics
渲染數據統(tǒng)計窗口