官术网_书友最值得收藏!

  • Unity Game Optimization
  • Dr. Davide Aversa Chris Dickinson
  • 691字
  • 2021-06-24 12:13:09

Materials and shaders

Render State in Unity is essentially exposed to us via materials. Materials are containers around shaders, short programs that define how the GPU should render incoming vertex and texture data. A shader on its own does not have the necessary knowledge of the state to accomplish anything of value. A shader requires input such as diffuse textures, normal maps, and lighting information, and effectively dictates what Render State variables need to be set in order to render the incoming data.

Shaders are named this way because,  many years ago,  their original implementation was to only handle the lighting and shading  of an object (applying shadows where originally there were none). Their purpose has grown enormously since then, and now they have the much more generic purpose of being a programmable access point to many different kinds of parallel tasks, but the old name still remains.

Every shader needs a material, and every material must have a shader. Even newly imported meshes introduced into the scene without an assigned material are automatically assigned a default (hidden) material, which gives them a basic diffuse shader and a white coloration, so there is no way of getting around this relationship.

Note that a single material can only support a single shader. The use of multiple shaders on the same mesh requires separate materials to be assigned to different parts of the same mesh.

Therefore, if we want to minimize how often Render State changes, then we can do so by reducing the number of materials we use during a scene. This would result in two performance improvements simultaneously: the CPU will spend less time generating and transmitting instructions to the GPU during each frame and the GPU won't need to stop and resynchronize state changes as often.

Let's begin with a simple scene in order to visualize the behavior of materials and batching. However, before we start, we should disable a few rendering options, as they will contribute some extra draw calls, which might be distracting:

  1. Navigate to Edit | Project Settings | Quality and set Shadows to Disable Shadows (or select the default Fastest quality level)
  2. Navigate to Edit | Project Settings | Player, open the Other Settings tab, and disable Static Batching and Dynamic Batchingif they are enabled

Next, we'll create a scene that contains a single directional light with four cubes and four spheres, where each object has its own unique material, position, rotation, and scale, as shown in the following screenshot:

In the preceding screenshot, we can see 9 total batches in the Batching value in the Game window's Stats popup. This value closely represents the number of draw calls used to render the scene. The current view will consume one of these batches that renders the background of the scene, which could be set to Skybox or Solid Color. This is determined by the camera object's Clear Flags settings.

The remaining eight batches are used to draw our eight objects. In each case, the draw call involves preparing the Rendering Pipeline using the material's properties and asking the GPU to render the given mesh at its current transform. We have ensured that each material is unique by giving them each a unique texture file to render. So, each mesh requires a different Render State, and, therefore, each of our eight meshes requires a unique draw call.

As previously mentioned, we can theoretically minimize the number of draw calls by reducing how often we cause the system to change Render State information; so, part of the goal is to reduce the number of materials we use. However, if we configure all objects to use the same material, we still won't see any benefit, and the number of batches will remain at nine:

This is because we're not actually reducing the number of Render State changes, nor are we efficiently grouping mesh information. Unfortunately, the Rendering Pipeline is not smart enough to realize we're overwriting the exact same Render State values and then asking it to render the same meshes over and over again.

主站蜘蛛池模板: 喀喇沁旗| 大埔区| 苍梧县| 清丰县| 民和| 辽宁省| 沭阳县| 舟山市| 荆门市| 凌云县| 镇雄县| 广州市| 伊宁县| 巩留县| 灵璧县| 平邑县| 宁都县| 巴林左旗| 中牟县| 资溪县| 碌曲县| 库尔勒市| 奇台县| 隆尧县| 乐昌市| 平湖市| 卢湾区| 肇州县| 浮梁县| 达州市| 永修县| 屯昌县| 安塞县| 正定县| 邵武市| 扶绥县| 渑池县| 新津县| 合川市| 融水| 集贤县|