1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[]>
2<html xml:lang="en-US" lang="en-US">
3  <head>
4    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5    <meta http-equiv="Content-Style-Type" content="text/css" />
6    <link rel="stylesheet" href="../../css/document.css" type="text/css" />
7    <title>Optimization Tips</title>
8  </head>
9  <body>
10    <h1 id="newlist1_2_0">Added and Revised Items in 1.2.0</h1>
11    <ul>
12
13    <!-- <GFX>  -->
14    <li><a href="#optimizerenderkey">Optimize render queue key creation</a></li>
15    <!-- </GFX>  --> <!-- <ANIM>  -->
16    <li><a href="#delete_anim_member_auto">Automatically delete unnecessary animation members</a></li>
17    <!-- </ANIM>  -->
18
19    </ul>
20    <h1>CPU/GPU Optimization</h1>
21    <ul>
22
23    <!-- <GFX>  -->
24
25    <li><a href="#document">Read the optimization document</a></li>
26    <li><a href="#option">Enable the optimization option</a></li>
27    <li><a href="#vram">Position vertex data and textures in VRAM</a></li>
28    <li><a href="#doublecmdlist">Multiplex the command list</a></li>
29    <li><a href="#glclear">Do not call glClear</a></li>
30
31    <!-- </GFX>  -->
32
33    </ul>
34    <h1>CPU Optimization</h1>
35    <ul>
36
37    <!-- <GFX>  -->
38    <li><a href="#material_id">Calculate the material ID</a></li>
39    <li><a href="#customrendersort">Customize the sort algorithm for the render queue</a></li>
40    <li><a href="#optimizerenderkey">Optimize render queue key creation</a></li>
41    <li><a href="#shared_skeleton">Share skeletons</a></li>
42    <li><a href="#shared_material">Share materials</a></li>
43    <li><a href="#traverse">Execute traverse only when the scene tree structure has changed</a></li>
44    <li><a href="#skip_update">Omit some traverse and update processing</a></li>
45    <li><a href="#savedcmdlist">Re-use the command list when displaying 3D</a></li>
46    <li><a href="#build_option">Disable features not required by the build option</a></li>
47    <!-- </GFX>  -->
48
49    <!-- <ANIM>  -->
50    <li><a href="#frame_format">Use frame-format animation data</a></li>
51    <li><a href="#change_anim">Re-use the AnimEvaluator when switching animations</a></li>
52    <li><a href="#anim_cache">Use an animation cache as appropriate</a></li>
53    <li><a href="#delete_anim_member">Delete unnecessary animation members</a></li>
54    <li><a href="#delete_anim_member_auto">Automatically delete unnecessary animation members</a></li>
55    <li><a href="#disable_anim">Do not create AnimBinding for unanimated nodes</a></li>
56    <!-- </ANIM>  -->
57
58    </ul>
59    <h1>Vertex Process Optimization</h1>
60    <ul>
61
62    <!-- <GFX>  -->
63    <li><a href="#shader_program">Select a shader program according to the number of textures</a></li>
64    <li><a href="#light_off">Turn off unnecessary lights</a></li>
65    <li><a href="#light_write">Bake lights to vertex colors</a></li>
66    <li><a href="#user_shader">Use user shaders</a></li>
67    <!-- </GFX>  -->
68
69    </ul>
70    <hr />
71
72    <!-- <GFX>  -->
73    <h2 id="document">Read the optimization document</h2>
74    <div class="section">
75    Read the optimization document included with the SDK.<br /> Note that these optimization tips are not included in the document supplied with the SDK.<br />
76    </div>
77    <h2 id="option">Enable the optimization option</h2>
78    <div class="section">
79    Enable the optimization option of the export plug-in.<br /> In some cases, this may have a significant effect on performance.
80    </div>
81    <h2 id="vram">Position vertex data and textures in VRAM</h2>
82    <div class="section">
83    Position vertex data and textures in VRAM.<br /> As shown below, transfer reservations can be made at time of resource initialization by setting positions at the graphics file level.
84    <p class="info">
85    ResGraphicsFile::ForeachTexture(nw::gfx::TextureLocationFlagSetter(NN_GX_MEM_VRAMA | GL_NO_COPY_FCRAM_DMP));<br /> ResGraphicsFile::ForeachIndexStream(nw::gfx::IndexStreamLocationFlagSetter(NN_GX_MEM_VRAMB | GL_NO_COPY_FCRAM_DMP));<br /> ResGraphicsFile::ForeachVertexStream(nw::gfx::VertexStreamLocationFlagSetter(NN_GX_MEM_VRAMB | GL_NO_COPY_FCRAM_DMP));<br />
86    </p>
87    </div>
88    <h2 id="doublecmdlist">Multiplex the command list</h2>
89    <div class="section">
90    Multiplexing the command list allows execution and command generation to be handled simultaneously. <br /> However, tearing will occur if buffers are not swapped correctly, because even transfers from the render buffer to the display buffer can accumulate as commands.<br /> In the demo library, the <a href="../../nw/demo/CommandListSwapper/Overview.html">CommandListSwapper</a> class handles command list multiplexing.
91    </div>
92    <h2 id="glclear">Do not call glClear</h2>
93    <div class="section">
94    Even if RenderBuffer is located in VRAM, glClear affects things such as calculations for interrupts to the CPU.<br /> If possible, clear the screen without calling glClear by rendering beginning from most distant scenery.<br /> Under NW4C, mesh materials of the most distant scenery can be expressed by making the following settings. <br />
95    <ul>
96    <li>Depth test: Always pass (Always)</li>
97    <li>Color depth buffer: Update </li>
98    <li>User layer IDs and render priority to render the most distant model first</li>
99    </ul>
100    Also, set LayerId for SubmitView to prioritize rendering of the celestial sphere as done in the gfx demo.  Whatever you do, you must take care that the most distant model lies inside the Near/Far clip.
101    </div>
102    <h2 id="shared_skeleton">Share skeletons</h2>
103    <div class="section">
104    When creating characters that share motions, you can reduce calculations by having those characters share skeletons.<br /> For details on sharing skeletons, see <a href="./Coordinate.html#shared_skeleton">Sharing skeletons</a>.
105    </div>
106    <h2 id="shared_material">Share materials</h2>
107    <div class="section">
108    When creating models that share materials, you can omit material setup by sharing materials.<BR> To share a material, set the source model to be shared from using the <a href="../../nw/gfx/SceneBuilder/SharedMaterialModel.html">SharedMaterialModel</a> function. If materials are being shared, be sure to destroy the models being shared from only after destroying all models being shared with.
109    </div>
110    <h2 id="traverse">Execute traverse and initialize when the scene tree structure has changed</h2>
111    <div class="section">
112    Only execute traverse using <a href="../../nw/gfx/SceneTraverser/Overview.html">SceneTraverser</a> and initialization using <a href="../../nw/gfx/SceneInitializer/Overview.html">SceneInitializer</a> with <a href="../../nw/gfx/SceneNode/Accept.html">SceneNode::Accept</a> when the tree structure has changed, such as when a child has been added to or removed from the SceneNode. Re-execution is not required when Visible has changed.
113    </div>
114    <h2 id="skip_update">Omit some traverse and update processing</h2>
115    <div class="section">
116    If you want to omit execution of <a href="../../nw/gfx/SceneUpdater/UpdateAll.html">SceneUpdater::UpdateAll</a> and traverse for a particular node, some traverse and update processing can be omitted by preparing several instances of <a href="../../nw/gfx/SceneContext/Overview.html">SceneContext</a>.<BR> For example, you can collect traverse results for each individual <a href="../../nw/gfx/SceneContext/Overview.html">SceneContext</a> by separating the group of nodes you want to update and the ones you don't into separate branches and then traversing only some of them, or by splitting them into separate scene trees and then traversing only some of them. Processing can be reduced by skipping traverse and update when it comes to instances of <a href="../../nw/gfx/SceneContext/Overview.html">SceneContext</a> used to collect nodes whose status does not change. Multiple instances of <a href="../../nw/gfx/SceneContext/Overview.html">SceneContext</a> can be placed in a single <a href="../../nw/gfx/RenderQueue.html">RenderQueue</a> using functions like <a href="../../nw/gfx/SceneUpdater/SubmitView.html">SceneUpdater::SubmitView</a> or <a href="../../nw/gfx/BasicRenderQueue/EnqueueModel.html">RenderQueue::EnqueueModel</a>.<br />
117    </div>
118    <h2 id="material_id">Calculate the material ID</h2>
119    <div class="section">
120    Execute material sort using material IDs with the default sort algorithm.<br /> Material IDs can be calculated using <a href="../../nw/gfx/IMaterialIdGenerator/Overview.html">IMaterialIdGenerator</a>.<br /> The CPU load during rendering can be reduced by setting the material IDs of materials with the same or similar settings to values that are close to each other.<br /> To calculate material IDs, pass IMaterialIdGenerator to <a href="../../nw/gfx/SceneInitializer/Overview.html">SceneInitializer</a>, and then call the functions <a href="../../nw/gfx/SceneInitializer/Begin.html">SceneInitializer::Begin<a href="../../nw/gfx/SceneInitializer/Begin.html">, </a>SceneNode::Accept<a href="../../nw/gfx/SceneNode/Accept.html">, and </a>SceneInitializer::End<a href="../../nw/gfx/SceneInitializer/End.html"> in the order given. </a> The material ID generator algorithm can be customized by inheriting IMaterialIdGenerator.<br /> Carry out implementation while referring to <a href="../../nw/gfx/SortingMaterialIdGenerator/Overview.html">SortingMaterialIdGenerator<a href="../../nw/gfx/SortingMaterialIdGenerator/Overview.html">.
121    </div>
122    <h2 id="customrendersort">Customize the sort algorithm for the render queue</h2>
123    <div class="section">
124    Customize the sort algorithm by customing <a href="../../nw/gfx/RenderElementCompare/Overview.html">RenderElementCompare</a> and the render key factory (<a href="../../nw/gfx/BasicRenderKeyFactory/Overview.html">BasicRenderKeyFactory</a>).
125    </div>
126    <h2 id="optimizerenderkey">Optimize render queue key creation</h2>
127    <div class="section">
128    If a depth sort is not required for other than translucent meshes, you can omit depth-related calculations during key generation and cache key creation results.<BR> By setting &quot;SORT_DEPTH_OF_TRANSLUCENT_MESH&quot; for depth-related calculations using <a href="../../nw/gfx/ISceneUpdater/SetDepthSortMode.html">ISceneUpdater::SetDepthSortMode</a>, you can omit the use a key factory created by <a href="../../nw/gfx/CreatePriorMaterialAndZeroDepthRenderKeyFactory.html">CreatePriorMaterialAndZeroDepthRenderKeyFactory</a>.<BR> The key cash can be enabled by setting the cacheEnabled argument to <a href="../../nw/gfx/BasicRenderQueue/Reset.html">RenderQueue::Reset</a> to true. You can disable the caching of soft keys saved for each mesh by calling <a href="../../nw/gfx/Model/InvalidateRenderKeyCache.html">InvalidateRenderKeyCache</a>. You can also disable caching of softkeys on a per mesh basis by setting the ResMesh::FLAG_VALID_RENDER_KEY_CACHE  flag to zero by using the <a href="../../nw/gfx/res/ResMesh/SetFlags.html">ResMesh::SetFlags</a> function directly.
129    </div>
130    <h2 id="savedcmdlist">Re-use the command list when displaying 3D</h2>
131    <div class="section">
132    When displaying 3D, rather than generating a command list twice for the left and right eyes, you cut down on command generation by generating one command list and re-using it.<br /> Perform rendering during 3D display using the <a href="../../nw/demo/RenderSystem/RenderStereoScene.html">RenderStereoScene</a> function in the demo library. Within this code, use the <a href="../../nw/demo/CommandListSwapper/Overview.html">CommandListSwapper</a> class to save and re-use the command list. <br /> For the process flow, see <a href="./AdvancedFeature.html#stereo">Standard Rendering and Stereo Rendering</a>.
133    </div>
134    <h2 id="build_option">Disable features not required by the build option</h2>
135    <div class="section">
136    Unnecessary features such as determination processes can be skipped by disabling them using a build option.<BR> For details, see <a href="./../macros.html">Macro List</a>
137    </div>
138    <!-- </GFX>  -->
139
140    <!-- <ANIM>  -->
141    <h2 id="frame_format">Use frame-format animation data</h2>
142    <div class="section">
143      <p>
144        The processing load of evaluating animations can be reduced by using frame format animation data. Currently, only skeletal animations can be otuput in frame format.
145      </p>
146      <p>
147        The drawback is that the amount of data increases. Also, the accuracy of evaluating fractional frames goes down when the playback speed is changed.
148      </p>
149    </div>
150    <h2 id="change_anim">Re-use the AnimEvaluator when switching animations</h2>
151    <div class="section">
152      <p>
153        Use <a href="../../nw/gfx/AnimEvaluator/ChangeAnim.html">ChangeAnim</a> when switching animations for the same model.
154      </p>
155      <p>
156        This is faster than re-creating <a href="../../nw/gfx/AnimEvaluator/Overview.html">AnimEvaluator</a> and executing <a href="../../nw/gfx/AnimEvaluator/Bind.html">Bind</a> again.
157      </p>
158    </div>
159    <h2 id="anim_cache">Use an animation cache as appropriate</h2>
160    <div class="section">
161      <p>
162        Using an evaluation result cache is a fast approach when applying animation results to more than one model.<br /> On the other hand, there is overhead if enabled when the cache is not needed.<br /> For more details, see <a href="anim/AdvancedFeature.html#anim_cache">High-Speed Features</a>.
163      </p>
164    </div>
165    <h2 id="delete_anim_member">Delete unnecessary animation members</h2>
166    <div class="section">
167      <p>
168        Animation members can be deleted when exporting binary, such as models, from CreativeStudio. Animation evaluation and particularly blends can be executed at higher speed by deluding unnecessary animation members. Take care not to delete necessary animation members.
169      </p>
170      <p>
171        As an example, assume we have created the following filter definition file and script file for binary output of a model for which only material constant color 0 and 1 can be animated.
172      </p>
173      <h3>OptimizeFilter.xml</h3>
174      <pre>
175&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
176&lt;OptimizeAnimationMemberSettings&gt;
177	&lt;Filters Mode=&quot;positive&quot;&gt;
178		&lt;Path&gt;Materials[&quot;*&quot;].MaterialColor.Constant0&lt;/Path&gt;
179		&lt;Path&gt;Materials[&quot;*&quot;].MaterialColor.Constant1&lt;/Path&gt;
180	&lt;/Filters&gt;
181&lt;/OptimizeAnimationMemberSettings&gt;</pre>
182      <h3>Binarize.py</h3>
183      <p>
184        The script file must be saved using UTF-16 BOM encoding.
185      </p>
186      <pre>
187CreativeStudio.Execute(&quot;FileLoad&quot;, &quot;human.cmdl&quot;)
188CreativeStudio.Execute(&quot;FileLoad&quot;, &quot;human_all.ctex&quot;)
189CreativeStudio.Execute(&quot;OptimizeAnimationMember&quot;, &quot;-sf=OptimizeFilter.xml&quot;)
190CreativeStudio.Execute(&quot;FileSave&quot;, &quot;-o=human.bcres&quot;, &quot;-t=nw4cBinary&quot;)</pre>
191      <p>
192        Binary will be output when the following command is executed on the CreativeStudio console.
193      </p>
194      <pre>NW4C_CreativeStudioConsole.exe -s=Binarize.py</pre>
195      <p>
196        The definition file is made to look as follows when removing only specified members, rather than leaving behind only specified members. Take care that the mode attribute of the Filters member does not go negative.
197      </p>
198      <pre>
199&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
200&lt;OptimizeAnimationMemberSettings&gt;
201	&lt;Filters Mode=&quot;negative&quot;&gt;
202		&lt;Path&gt;IsVisible&lt;/Path&gt;
203		&lt;Path&gt;Meshes[&quot;*&quot;].IsVisible&lt;/Path&gt;
204		&lt;Path&gt;MeshNodeVisibilities[&quot;*&quot;].IsVisible&lt;/Path&gt;
205	&lt;/Filters&gt;
206&lt;/OptimizeAnimationMemberSettings&gt;</pre>
207      <p>
208        Added reset to Mode attributes beginning from version 1.2.0. Although the basic operations of reset are the same as positive, if the specified member has already been deleted and disabled, reset restores it to enabled status.
209      </p>
210      <p>
211        Although the method presented here uses a script file, execution is also possible from the console panel.
212      </p>
213    </div>
214    <h2 id="delete_anim_member_auto">Automatically delete unnecessary animation members</h2>
215    <div class="section">
216      <p>
217        Beginning from version 1.2.0, an operation for extracting necessary members based on animation data and automatically deleting unnecessary ones has been added to CreativeStudio.  Although the main method of use is that given below, note that under the method shown here information on member deletion (disabling) is saved in an intermediate file.  As of version 1.2.0, you cannot check whether a member is enabled or disabled from CreativeStudio.
218      </p>
219      <h3>Optimize loaded content.</h3>
220      <ol>
221        <li>Load all objects and animations to be optimized. </li>
222        <li>Select the objects to be optimized. If no selection is made, all objects will be optimized. </li>
223        <li>Enter and execute the following command on the console panel.
224          <pre>CreativeStudio.Execute(&quot;OptimizeUnusedAnimationMember&quot;)</pre>
225        </li>
226        <li>Optimization will be executed and results displayed on the console panel. </li>
227      </ol>
228      <h3>Create a definition file.</h3>
229      <ol>
230        <li>Load all animations to be optimized. </li>
231        <li>Note that all animations will be optimized regardless of the selection you make.</li>
232        <li>Enter and execute the following command on the console panel.
233          <pre>CreativeStudio.Execute(&quot;OptimizeUnusedAnimationMember&quot;)</pre>
234          or
235          <pre>CreativeStudio.Execute(&quot;OptimizeUnusedAnimationMember&quot;, &quot;-sf=[definition_file_name]&quot;)</pre>
236        </li>
237        <li>The above command saves a definition file. </li>
238        <li>Close all animations and load the objects to be optimized. </li>
239        <li>Select the objects to be optimized. If no selection is made, all objects will be optimized. </li>
240        <li>Enter and execute the following command on the console panel.
241          <pre>CreativeStudio.Execute(&quot;OptimizeAnimationMember&quot;)</pre>
242          or
243          <pre>CreativeStudio.Execute(&quot;OptimizeAnimationMember&quot;, &quot;-sf=[definition_file_name]&quot;)</pre>
244        </li>
245        <li>Optimization will be executed and results displayed on the console panel. </li>
246      </ol>
247      <p>
248        The definition file created by this procedure can also be used from a script file as described in <a href="#delete_anim_member">Deleting Unnecessary Animation Members</a>
249      </p>
250    </div>
251    <h2 id="disable_anim">Do not create AnimBinding for unanimated nodes</h2>
252    <div class="section">
253      <p>
254        If you know that a model will not be animated at time of creation, you can create nodes for which animation is disabled.<BR> You can thereby avoid unnecessary processing during scene updates.
255      </p>
256      <p>
257        For details, see <a href="../../nw/gfx/SceneBuilder/IsAnimationEnabled.html">SceneBuilder::IsAnimationEnabled</a>.
258      </p>
259    </div>
260    <!-- </ANIM>  -->
261    <!-- <GFX>  -->
262    <h2 id="shader_program">Select a shader program according to the number of textures</h2>
263    <div class="section">
264    When using the default shader, you can select a shader program according to the number of textures.<br /> Automatic shader program selection can be set as follows.<br />
265    <p class="info">
266    ResGraphicsFile::ForeachModelMaterial(nw::gfx::DefaultShaderAutoSelector());
267    </p>
268    </div>
269    <h2 id="light_off">Turn off unnecessary lights</h2>
270    <div class="section">
271    If fragment lights are not being used, rather than just turning out lights, disable material fragment lighting settings. <br /> Similarly, disable settings for vertex and hemispherical lights, too. Quaternion calculations by the vertex shader can be omitted by disabling fragment lighting. Normals can be removed when exporting data from CreativeStudio if all lights are disabled.
272    </div>
273    <h2 id="light_write">Bake lights to vertex colors</h2>
274    <div class="section">
275    If vertex processing is a bottleneck, bake vertex lights to the vertex color. If fill is the bottleneck, bake fragment lights to the vertex color.<br /> You can expect improvements for both vertex processing and fill processing.
276    </div>
277    <h2 id="user_shader">Use user shaders</h2>
278    <div class="section">
279    Vertex processing can be improved by using a custom shader when rendering models with an extremely large number of vertices. <br /> However, the CPU load increases when changing shaders, so you must make adjustments such as applying the custom shader to the background model only and using render priority to render it first so that the CPU load does not increase.<br /> For information on user-defined shaders, see <a href="UserShader.html">Creating Shaders</a>.
280    </div>
281    <!-- </GFX>  -->
282  <hr><p>CONFIDENTIAL</p></body>
283</html>
284