1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_SceneBuilder.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: $
16  *---------------------------------------------------------------------------*/
17 
18 
19 #ifndef NW_GFX_SCENEBUILDER_H_
20 #define NW_GFX_SCENEBUILDER_H_
21 
22 #include <nw/gfx/gfx_SceneObject.h>
23 
24 namespace nw
25 {
26 namespace gfx
27 {
28 
29 class SceneNode;
30 
31 //---------------------------------------------------------------------------
32 //! @brief        シーン上のオブジェクトを生成するクラスです。
33 //---------------------------------------------------------------------------
34 class SceneBuilder
35 {
36 private:
37     NW_DISALLOW_COPY_AND_ASSIGN(SceneBuilder);
38 
39 public:
40     //! コンストラクタです。
SceneBuilder()41     SceneBuilder()
42     : m_IsFixedSizeMemory(true),
43       m_MaxChildren(SceneObject::DEFAULT_MAX_CHILDREN),
44       m_MaxCallbacks(SceneObject::DEFAULT_MAX_CALLBACKS),
45       m_BufferOption(0),
46       m_SharedMaterialModel(NULL),
47       m_MaxAnimObjectsPerGroup(1),
48       m_ParticleSetMarginCount(0),
49       m_IsAnimationEnabled(true)
50     {}
51 
52     //! デストラクタです。
~SceneBuilder()53     ~SceneBuilder() {}
54 
55     //! リソースを設定します。
Resource(ResSceneObject resource)56     SceneBuilder& Resource(ResSceneObject resource) { m_Resource = resource; return *this; }
57 
58     //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
59     //!
60     //!        true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
61     //!
62     //!        false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)63     SceneBuilder& IsFixedSizeMemory(bool isFixedSizeMemory)
64     {
65         m_IsFixedSizeMemory = isFixedSizeMemory;
66         return *this;
67     }
68 
69     //! 子の最大数を設定します。
MaxChildren(int maxChildren)70     SceneBuilder& MaxChildren(int maxChildren) { m_MaxChildren = maxChildren; return *this; }
71 
72     //! 管理できるコールバックの最大数を設定します。
MaxCallbacks(int maxCallbacks)73     SceneBuilder& MaxCallbacks(int maxCallbacks) { m_MaxCallbacks = maxCallbacks; return *this; }
74 
75     //! @brief バッファの生成オプションを設定します。
76     //!
77     //!
78     //! @param bufferOption Model::BufferOptionで定義されるフラグの組み合わせです。
79     //!        SharedMaterialModel が指定された場合、BufferOption は指定することができません。
80     //!        BufferOption が指定された場合は動作を停止します。
81     //!
82     //! @param[in] bufferOption Model::BufferOptionで定義されるフラグの組み合わせです。
83     //!
BufferOption(bit32 bufferOption)84     SceneBuilder& BufferOption(bit32 bufferOption) { m_BufferOption = bufferOption; return *this; }
85 
86     //! @brief マテリアルを共有する場合に共有元のモデルを設定します。
87     //!
88     //!        所有権の移動を行いません。
89     //!        マテリアルを最初に生成したモデルがマテリアルの所有権を持ちます。
90     //!        共有元のモデルが先に破棄された場合は未定義の動作となります。
91     //!
92     //! @param[in] model 共有するマテリアルを持つモデルです。
93     //!
SharedMaterialModel(Model * model)94     SceneBuilder& SharedMaterialModel(Model* model) { m_SharedMaterialModel = model; return *this; }
95 
96     //! @brief アニメオブジェクトの最大数を指定します。
97     //!
98     //! @details 複数のAnimGroupを持つ場合は、
99     //!          各AnimGroupごとのアニメオブジェクトの最大数のうち、最も大きいものを指定してください。
100     //!
101     //!          MaxAnimObjectsPerGroup の指定は IsFixedSizeMemory が false でも有効になります。
MaxAnimObjectsPerGroup(s32 maxAnimObjects)102     SceneBuilder& MaxAnimObjectsPerGroup(s32 maxAnimObjects) { m_MaxAnimObjectsPerGroup = maxAnimObjects; return *this; }
103 
104     //! @brief パーティクルセットを生成する場合のバッファを生成する数を設定します。
105     //!
106     //!        値が指定されている場合、リソース内パーティクルセット数に指定された値を加えた数で生成されます。
107     //!        0が指定されている場合、リソース内パーティクルセット数にデフォルトマージンを加えた数で生成されます。
108     //!
ParticleSetMarginCount(int marginCount)109     SceneBuilder& ParticleSetMarginCount(int marginCount) { m_ParticleSetMarginCount = marginCount; return *this; }
110 
111     //! @brief アニメーション可能かを設定します。
112     //!
113     //!        false を指定すると AnimBinding の生成を行いません。
114     //!        そのためアニメーションのバインドができなくなりますが、
115     //!        アニメーション更新の処理が行われなくなるため、
116     //!        シーンアップデートのパフォーマンスが向上します。
IsAnimationEnabled(bool isAnimationEnabled)117     SceneBuilder& IsAnimationEnabled(bool isAnimationEnabled) { m_IsAnimationEnabled = isAnimationEnabled; return *this; }
118 
119     //! @brief        設定されたリソースから、ひとつのシーンオブジェクトを生成します。
120     //!               このとき、リソースに含まれる子階層以下のオブジェクトは生成されませんので、
121     //!               個別に生成する必要があります。
122     //!
123     //! @param[in]    allocator アロケータです。
124     //! @param[in]    deviceAllocator デバイスメモリのアロケータです。
125     //!
126     //! @return       生成したシーンオブジェクトを返します。
127     //!
128     SceneObject* CreateObject(os::IAllocator* allocator, os::IAllocator* deviceAllocator);
129 
130     //! @brief        設定されたリソースをルートノードとしたシーンツリーを生成します。
131     //!               リソースに含まれる子階層以下の参照をたどってすべてインスタンス化していくため、
132     //!               リソースファイルに含まれるモデルすべてを初期化する、などの処理で使用すると
133     //!               子階層のオブジェクトが2重にインスタンス化されます。
134     //!
135     //! @param[in]    allocator アロケータです。
136     //! @param[in]    deviceAllocator デバイスメモリのアロケータです。
137     //!
138     //! @return       生成したルートノードのシーンオブジェクトを返します。
139     //!
140     SceneObject* CreateTree(os::IAllocator* allocator, os::IAllocator* deviceAllocator);
141 
142     //! @brief        生成時に必要なメモリサイズを取得します。
143     //!
144     //! CreateObject() を呼び出すときに必要なメモリサイズを返します。
145     //!
146     //! メモリサイズは SceneBuilder の設定によって変化します。
147     //! すべての設定が終わった後にこの関数を呼び出してください。
148     //!
149     //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
150     size_t GetMemorySize(
151         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
152     ) const
153     {
154         os::MemorySizeCalculator size(alignment);
155 
156         BuildSceneObject(&size, NULL, NULL, m_Resource, NULL, NULL, false, true);
157 
158         return size.GetSizeWithPadding(alignment);
159     }
160 
161     //! @brief        生成時に必要なデバイスメモリサイズを取得します。
162     //!
163     //! CreateObject() を呼び出すときに必要なデバイスメモリサイズを返します。
164     //!
165     //! メモリサイズは SceneBuilder の設定によって変化します。
166     //! すべての設定が終わった後にこの関数を呼び出してください。
167     //!
168     //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
169     size_t GetDeviceMemorySize(
170         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
171     ) const
172     {
173         os::MemorySizeCalculator size(alignment);
174 
175         BuildSceneObject(NULL, &size, NULL, m_Resource, NULL, NULL, false, true);
176 
177         return size.GetSizeWithPadding(alignment);
178     }
179 
180     //! @brief        シーンツリー生成時に必要なメモリサイズを取得します。
181     //!
182     //! CreateTree() を呼び出すときに必要なメモリサイズを返します。
183     //!
184     //! メモリサイズは SceneBuilder の設定によって変化します。
185     //! すべての設定が終わった後にこの関数を呼び出してください。
186     //!
187     //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
188     size_t GetMemorySizeTree(
189         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
190     ) const
191     {
192         os::MemorySizeCalculator size(alignment);
193 
194         BuildSceneObject(&size, NULL, NULL, m_Resource, NULL, NULL, true, true);
195 
196         return size.GetSizeWithPadding(alignment);
197     }
198 
199     //! @brief        シーンツリー生成時に必要なデバイスメモリサイズを取得します。
200     //!
201     //! CreateTree() を呼び出すときに必要なメモリサイズを返します。
202     //!
203     //! メモリサイズは SceneBuilder の設定によって変化します。
204     //! すべての設定が終わった後にこの関数を呼び出してください。
205     //!
206     //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
207     size_t GetDeviceMemorySizeTree(
208         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
209     ) const
210     {
211         os::MemorySizeCalculator size(alignment);
212 
213         BuildSceneObject(NULL, &size, NULL, m_Resource, NULL, NULL, true, true);
214 
215         return size.GetSizeWithPadding(alignment);
216     }
217 
218 private:
219     //----------------------------------------
220     //! @name シーンオブジェクトの生成
221     //@{
222 
223     //! @brief        シーンオブジェクトを生成します。
224     //!
225     //! isCalculation を true にすると生成に必要なメモリサイズを求め、
226     //! オブジェクトの生成を行ないません。
227     //!
228     //! @param        pSize メモリサイズを求めるための MemorySizeCalculator へのポインタです。
229     //! @param        pDeviceSize デバイスメモリサイズを求めるための MemorySizeCalculator へのポインタです。
230     //! @param[in]    parent 親となるSceneNodeです。
231     //! @param[in]    resource リソースです。
232     //! @param[in]    allocator アロケータです。
233     //! @param[in]    deviceAllocator デバイスメモリのアロケータです。
234     //! @param[in]    isRecursive 再帰的に子階層を生成するかどうかのフラグです。
235     //! @param[in]    isCalculation メモリサイズを計算するかどうかのフラグです。
236     //!
237     //! @return       生成したオブジェクトを返します。
238     //!
239     SceneObject* BuildSceneObject(
240         os::MemorySizeCalculator* pSize,
241         os::MemorySizeCalculator* pDeviceSize,
242         SceneNode* parent,
243         ResSceneObject resource,
244         os::IAllocator* allocator,
245         os::IAllocator* deviceAllocator,
246         bool isRecursive,
247         bool isCalculation) const;
248 
249     //! @brief        ノードの子を構築します。
250     //!
251     //! isCalculation を true にすると生成に必要なメモリサイズを求め、
252     //! オブジェクトの生成を行ないません。
253     //!
254     //! @param        pSize メモリサイズを求めるための MemorySizeCalculator へのポインタです。
255     //! @param        pDeviceSize デバイスメモリサイズを求めるための MemorySizeCalculator へのポインタです。
256     //! @param[in]    parent 親のシーンノードです。
257     //! @param[in]    resource リソースです。
258     //! @param[in]    allocator アロケータです。
259     //! @param[in]    deviceAllocator デバイスメモリのアロケータです。
260     //! @param[in]    isCalculation メモリサイズを計算するかどうかのフラグです。
261     //!
262     void BuildChildren(
263         os::MemorySizeCalculator* pSize,
264         os::MemorySizeCalculator* pDeviceSize,
265         SceneNode* parent,
266         ResSceneObject resource,
267         os::IAllocator* allocator,
268         os::IAllocator* deviceAllocator,
269         bool isCalculation) const;
270 
271     //@}
272 
273     ResSceneObject m_Resource;
274     bool m_IsFixedSizeMemory;
275     int m_MaxChildren;
276     int m_MaxCallbacks;
277     bit32 m_BufferOption;
278     Model* m_SharedMaterialModel;
279 
280     int m_MaxAnimObjectsPerGroup;
281     int m_ParticleSetMarginCount;
282 
283     bool m_IsAnimationEnabled;
284 };
285 
286 } // namespace gfx
287 } // namespace nw
288 
289 #endif // NW_GFX_SCENEBUILDER_H_
290