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