1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_ResGraphicsFile.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: 19617 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_RESGRAPHICSFILE_H_
17 #define NW_GFX_RESGRAPHICSFILE_H_
18 
19 #include <nw/ut/ut_BinaryFileFormat.h>
20 #include <nw/ut/ut_ResUtil.h>
21 #include <nw/ut/ut_ResDictionary.h>
22 #include <nw/gfx/res/gfx_ResRevision.h>
23 #include <nw/gfx/res/gfx_ResModel.h>
24 #include <nw/gfx/res/gfx_ResSceneObject.h>
25 #include <nw/gfx/res/gfx_ResShape.h>
26 #include <nw/gfx/res/gfx_ResMaterial.h>
27 #include <nw/gfx/res/gfx_ResParticleEmitter.h>
28 #include <nw/gfx/res/gfx_ResParticleModel.h>
29 #include <nw/gfx/res/gfx_ResCamera.h>
30 #include <nw/gfx/res/gfx_ResLight.h>
31 #include <nw/gfx/res/gfx_ResFog.h>
32 #include <nw/gfx/res/gfx_ResSceneEnvironmentSetting.h>
33 #include <nw/anim/res/anim_ResAnim.h>
34 
35 namespace nw {
36 namespace gfx {
37 namespace res {
38 
39 //! @details :private
40 struct ResFileHeaderData
41 {
42     nw::ut::ResU32  m_Signature;
43     nw::ut::ResU16  m_ByteOrder;
44     nw::ut::ResU16  m_HeaderSize;
45     nw::ut::ResU32  m_Revision;
46     nw::ut::ResU32  m_FileSize;
47     nw::ut::ResU16  m_NumBlocks;
48     u8              padding_[2];
49     nw::ut::BinaryBlockHeader m_BlockHeader;
50 };
51 
52 
53 //! @details :private
54 struct ResGraphicsFileData : public ResFileHeaderData
55 {
56     nw::ut::ResS32 m_ModelsDicCount;
57     nw::ut::Offset toModelsDic;
58     nw::ut::ResS32 m_TexturesDicCount;
59     nw::ut::Offset toTexturesDic;
60     nw::ut::ResS32 m_LutSetsDicCount;
61     nw::ut::Offset toLutSetsDic;
62     nw::ut::ResS32 m_MaterialsDicCount;
63     nw::ut::Offset toMaterialsDic;
64     nw::ut::ResS32 m_ShadersDicCount;
65     nw::ut::Offset toShadersDic;
66     nw::ut::ResS32 m_CamerasDicCount;
67     nw::ut::Offset toCamerasDic;
68     nw::ut::ResS32 m_LightsDicCount;
69     nw::ut::Offset toLightsDic;
70     nw::ut::ResS32 m_FogsDicCount;
71     nw::ut::Offset toFogsDic;
72     nw::ut::ResS32 m_SceneEnvironmentSettingsDicCount;
73     nw::ut::Offset toSceneEnvironmentSettingsDic;
74     nw::ut::ResS32 m_SkeletalAnimsDicCount;
75     nw::ut::Offset toSkeletalAnimsDic;
76     nw::ut::ResS32 m_MaterialAnimsDicCount;
77     nw::ut::Offset toMaterialAnimsDic;
78     nw::ut::ResS32 m_VisibilityAnimsDicCount;
79     nw::ut::Offset toVisibilityAnimsDic;
80     nw::ut::ResS32 m_CameraAnimsDicCount;
81     nw::ut::Offset toCameraAnimsDic;
82     nw::ut::ResS32 m_LightAnimsDicCount;
83     nw::ut::Offset toLightAnimsDic;
84     nw::ut::ResS32 m_EmittersDicCount;
85     nw::ut::Offset toEmittersDic;
86 };
87 
88 
89 //--------------------------------------------------------------------------
90 //! @brief  グラフィックス用のファイルヘッダを表すバイナリリソースクラスです。
91 //---------------------------------------------------------------------------
92 class ResFileHeader : public nw::ut::ResCommon< ResFileHeaderData >
93 {
94 public:
NW_RES_CTOR(ResFileHeader)95     NW_RES_CTOR( ResFileHeader )
96 
97     //---------------------------------------------------------------------------
98     //! @fn           u32 GetSignature() const
99     //! @brief        シグニチャを取得します。
100     //---------------------------------------------------------------------------
101     //---------------------------------------------------------------------------
102     //! @fn           void SetSignature(u32 value)
103     //! @brief        シグニチャを設定します。
104     //---------------------------------------------------------------------------
105     NW_RES_FIELD_PRIMITIVE_DECL( u32, Signature )    // GetSignature(), SetSignature()
106 
107     //---------------------------------------------------------------------------
108     //! @fn           u16 GetByteOrder() const
109     //! @brief        バイトオーダーを取得します。
110     //---------------------------------------------------------------------------
111     //---------------------------------------------------------------------------
112     //! @fn           void SetByteOrder(u16 value)
113     //! @brief        バイトオーダーを設定します。
114     //---------------------------------------------------------------------------
115     NW_RES_FIELD_PRIMITIVE_DECL( u16, ByteOrder )    // GetByteOrder(), SetByteOrder()
116 
117     //---------------------------------------------------------------------------
118     //! @fn           u16 GetHeaderSize() const
119     //! @brief        ヘッダーサイズを取得します。
120     //---------------------------------------------------------------------------
121     //---------------------------------------------------------------------------
122     //! @fn           void SetHeaderSize(u16 value)
123     //! @brief        ヘッダーサイズを設定します。
124     //---------------------------------------------------------------------------
125     NW_RES_FIELD_PRIMITIVE_DECL( u16, HeaderSize )   // GetHeaderSize(), SetHeaderSize()
126 
127     //---------------------------------------------------------------------------
128     //! @fn           u32 GetRevision() const
129     //! @brief        リビジョン情報を取得します。
130     //---------------------------------------------------------------------------
131     //---------------------------------------------------------------------------
132     //! @fn           void SetRevision(u32 value)
133     //! @brief        リビジョン情報を設定します。
134     //---------------------------------------------------------------------------
135     NW_RES_FIELD_PRIMITIVE_DECL( u32, Revision )     // GetRevision(), SetRevision()
136 
137     //---------------------------------------------------------------------------
138     //! @fn           u32 GetFileSize() const
139     //! @brief        ファイルサイズを取得します。
140     //---------------------------------------------------------------------------
141     //---------------------------------------------------------------------------
142     //! @fn           void SetFileSize(u32 value)
143     //! @brief        ファイルサイズを設定します。
144     //---------------------------------------------------------------------------
145     NW_RES_FIELD_PRIMITIVE_DECL( u32, FileSize )     // GetFileSize(), SetFileSize()
146 
147     //---------------------------------------------------------------------------
148     //! @fn           u16 GetNumBlocks() const
149     //! @brief        ブロック数を取得します。
150     //---------------------------------------------------------------------------
151     //---------------------------------------------------------------------------
152     //! @fn           void SetNumBlocks(u16 value)
153     //! @brief        ブロック数を設定します。
154     //---------------------------------------------------------------------------
155     NW_RES_FIELD_PRIMITIVE_DECL( u16, NumBlocks )    // GetNumBlocks(), SetNumBlocks()
156 
157 
158     //---------------------------------------------------------------------------
159     //! @brief        バイナリのファイルヘッダを取得します。
160     //!
161     //! @return       バイナリのファイルヘッダへの参照です。
162     //---------------------------------------------------------------------------
163     const nw::ut::BinaryFileHeader& GetFileHeader() const
164     {
165         return *reinterpret_cast<const nw::ut::BinaryFileHeader*>( this->ptr() );
166     }
167 
168     //---------------------------------------------------------------------------
169     //! @brief        バイナリのブロック情報を取得します。
170     //!
171     //! @details  ResGraphicsFile には kind が "DATA" となっている通常ブロックと "IMAG"
172     //!           となっているテクスチャ、頂点イメージブロックの2種類のブロックが存在します。
173     //!           nw::ut::GetNextBinaryBlockHeader 関数を使用してイメージブロックを取得して、
174     //!           まとめて VRAM に転送することで、イメージデータの VRAMへの転送コストを抑える事が可能です。
175     //!           この場合、アプリケーション側でまとめて転送した VRAM のアドレスと
176     //!           各リソース上のイメージデータのアドレス情報を元に、それぞれの VRAM アドレスを計算し、
177     //!           ResImageTexture や ResVertexStream, ResIndexStream にある SetLocationAddress 関数を
178     //!           使用して Setup 前に設定してください。
179     //!
180     //! @return       バイナリのブロック情報への参照です。
181     //---------------------------------------------------------------------------
GetFirstBlockHeader()182     const nw::ut::BinaryBlockHeader& GetFirstBlockHeader() const
183     {
184         return ref().m_BlockHeader;
185     }
186 
187     //---------------------------------------------------------------------------
188     //! @brief        イメージデータブロックの先頭のデータアドレスを取得します。
189     //!
190     //! @return       イメージデータブロックの先頭のデータアドレスです。
191     //---------------------------------------------------------------------------
GetImageBlockData()192     void* GetImageBlockData()
193     {
194         return const_cast<void*>(this->GetImageBlockDataImpl());
195     }
196 
197     //---------------------------------------------------------------------------
198     //! @brief        イメージデータブロックの先頭のアドレスを取得します。
199     //!
200     //! @return       バイナリのブロック情報への参照です。
201     //---------------------------------------------------------------------------
GetImageBlockData()202     const void* GetImageBlockData() const
203     {
204         return this->GetImageBlockDataImpl();
205     }
206 
207     //---------------------------------------------------------------------------
208     //! @brief        イメージデータブロックのデータサイズを取得します。
209     //!
210     //! @return       イメージデータブロックのデータサイズです。
211     //---------------------------------------------------------------------------
GetImageBlockDataSize()212     s32 GetImageBlockDataSize() const
213     {
214         NW_ASSERT( this->IsValid() );
215 
216         const nw::ut::BinaryFileHeader& header = *reinterpret_cast<const nw::ut::BinaryFileHeader*>( this->ptr() );
217         const nw::ut::BinaryBlockHeader* block = &ref().m_BlockHeader;
218 
219         for (int i = 0; i < header.dataBlocks; ++i)
220         {
221             if (block->kind == nw::ut::ReverseEndian('IMAG'))
222             {
223                 return block->size - sizeof(nw::ut::BinaryBlockHeader);
224             }
225 
226             block = nw::ut::GetNextBinaryBlockHeader( &header, block );
227         }
228 
229         return 0;
230     }
231 
232 private:
233     //---------------------------------------------------------------------------
234     //! @brief        イメージデータブロックの先頭のアドレスを取得します。
235     //---------------------------------------------------------------------------
GetImageBlockDataImpl()236     const void* GetImageBlockDataImpl() const
237     {
238         NW_ASSERT( this->IsValid() );
239 
240         const nw::ut::BinaryFileHeader& header = *reinterpret_cast<const nw::ut::BinaryFileHeader*>( this->ptr() );
241         const nw::ut::BinaryBlockHeader* block = &ref().m_BlockHeader;
242 
243         for (int i = 0; i < header.dataBlocks; ++i)
244         {
245             if (block->kind == nw::ut::ReverseEndian('IMAG'))
246             {
247                 return nw::ut::AddOffsetToPtr(block, sizeof(nw::ut::BinaryBlockHeader));
248             }
249 
250             block = nw::ut::GetNextBinaryBlockHeader( &header, block );
251         }
252 
253         return NULL;
254     }
255 
256 };
257 
258 //--------------------------------------------------------------------------
259 //! @brief  グラフィックス用のファイルを表すバイナリリソースクラスです。
260 //---------------------------------------------------------------------------
261 class ResGraphicsFile : public ResFileHeader
262 {
263 public:
264     enum {
265         BINARY_REVISION = REVISION_RES_GRAPHICS_FILE
266     };
267 
268     NW_RES_CTOR_INHERIT( ResGraphicsFile, ResFileHeader )
269 
270     //---------------------------------------------------------------------------
271     //! @fn           s32 GetVisibilityAnimsIndex(const char * key) const
272     //! @brief        ビジビリティアニメーションリソースの辞書データ中でのインデックス番号を取得します。
273     //---------------------------------------------------------------------------
274     //---------------------------------------------------------------------------
275     //! @fn           s32 GetVisibilityAnimsCount() const
276     //! @brief        ビジビリティアニメーションリソースの要素数を取得します。
277     //---------------------------------------------------------------------------
278     //---------------------------------------------------------------------------
279     //! @fn           anim::res::ResAnim GetVisibilityAnims(int idx)
280     //! @brief        ビジビリティアニメーションリソースを取得します。
281     //---------------------------------------------------------------------------
282     //---------------------------------------------------------------------------
283     //! @fn           s32 GetTexturesIndex(const char * key) const
284     //! @brief        テクスチャリソースの辞書データ中でのインデックス番号を取得します。
285     //---------------------------------------------------------------------------
286     //---------------------------------------------------------------------------
287     //! @fn           s32 GetTexturesCount() const
288     //! @brief        テクスチャリソースの要素数を取得します。
289     //---------------------------------------------------------------------------
290     //---------------------------------------------------------------------------
291     //! @fn           ResTexture GetTextures(int idx)
292     //! @brief        テクスチャリソースを取得します。
293     //---------------------------------------------------------------------------
294     //---------------------------------------------------------------------------
295     //! @fn           s32 GetSkeletalAnimsIndex(const char * key) const
296     //! @brief        スケルタルアニメーションリソースの辞書データ中でのインデックス番号を取得します。
297     //---------------------------------------------------------------------------
298     //---------------------------------------------------------------------------
299     //! @fn           s32 GetSkeletalAnimsCount() const
300     //! @brief        スケルタルアニメーションリソースの要素数を取得します。
301     //---------------------------------------------------------------------------
302     //---------------------------------------------------------------------------
303     //! @fn           anim::res::ResAnim GetSkeletalAnims(int idx)
304     //! @brief        スケルタルアニメーションリソースを取得します。
305     //---------------------------------------------------------------------------
306     //---------------------------------------------------------------------------
307     //! @fn           s32 GetShadersIndex(const char * key) const
308     //! @brief        シェーダリソースの辞書データ中でのインデックス番号を取得します。
309     //---------------------------------------------------------------------------
310     //---------------------------------------------------------------------------
311     //! @fn           s32 GetShadersCount() const
312     //! @brief        シェーダリソースの要素数を取得します。
313     //---------------------------------------------------------------------------
314     //---------------------------------------------------------------------------
315     //! @fn           ResShader GetShaders(int idx)
316     //! @brief        シェーダリソースを取得します。
317     //---------------------------------------------------------------------------
318     //---------------------------------------------------------------------------
319     //! @fn           s32 GetSceneEnvironmentSettingsIndex(const char * key) const
320     //! @brief        シーン環境設定リソースの辞書データ中でのインデックス番号を取得します。
321     //---------------------------------------------------------------------------
322     //---------------------------------------------------------------------------
323     //! @fn           s32 GetSceneEnvironmentSettingsCount() const
324     //! @brief        シーン環境設定リソースの要素数を取得します。
325     //---------------------------------------------------------------------------
326     //---------------------------------------------------------------------------
327     //! @fn           ResSceneEnvironmentSetting GetSceneEnvironmentSettings(int idx)
328     //! @brief        シーン環境設定リソースを取得します。
329     //---------------------------------------------------------------------------
330     //---------------------------------------------------------------------------
331     //! @fn           s32 GetModelsIndex(const char * key) const
332     //! @brief        モデルリソースの辞書データ中でのインデックス番号を取得します。
333     //---------------------------------------------------------------------------
334     //---------------------------------------------------------------------------
335     //! @fn           s32 GetModelsCount() const
336     //! @brief        モデルリソースの要素数を取得します。
337     //---------------------------------------------------------------------------
338     //---------------------------------------------------------------------------
339     //! @fn           ResModel GetModels(int idx)
340     //! @brief        モデルリソースを取得します。
341     //---------------------------------------------------------------------------
342     //---------------------------------------------------------------------------
343     //! @fn           s32 GetMaterialAnimsIndex(const char * key) const
344     //! @brief        マテリアルアニメーションリソースの辞書データ中でのインデックス番号を取得します。
345     //---------------------------------------------------------------------------
346     //---------------------------------------------------------------------------
347     //! @fn           s32 GetMaterialAnimsCount() const
348     //! @brief        マテリアルアニメーションリソースの要素数を取得します。
349     //---------------------------------------------------------------------------
350     //---------------------------------------------------------------------------
351     //! @fn           anim::res::ResAnim GetMaterialAnims(int idx)
352     //! @brief        マテリアルアニメーションリソースを取得します。
353     //---------------------------------------------------------------------------
354     //---------------------------------------------------------------------------
355     //! @fn           s32 GetLutSetsIndex(const char * key) const
356     //! @brief        ルックアップテーブルセットリソースの辞書データ中でのインデックス番号を取得します。
357     //---------------------------------------------------------------------------
358     //---------------------------------------------------------------------------
359     //! @fn           s32 GetLutSetsCount() const
360     //! @brief        ルックアップテーブルセットリソースの要素数を取得します。
361     //---------------------------------------------------------------------------
362     //---------------------------------------------------------------------------
363     //! @fn           ResLookupTableSet GetLutSets(int idx)
364     //! @brief        ルックアップテーブルセットリソースを取得します。
365     //---------------------------------------------------------------------------
366     //---------------------------------------------------------------------------
367     //! @fn           s32 GetLightsIndex(const char * key) const
368     //! @brief        ライトリソースの辞書データ中でのインデックス番号を取得します。
369     //---------------------------------------------------------------------------
370     //---------------------------------------------------------------------------
371     //! @fn           s32 GetLightsCount() const
372     //! @brief        ライトリソースの要素数を取得します。
373     //---------------------------------------------------------------------------
374     //---------------------------------------------------------------------------
375     //! @fn           ResLight GetLights(int idx)
376     //! @brief        ライトリソースを取得します。
377     //---------------------------------------------------------------------------
378     //---------------------------------------------------------------------------
379     //! @fn           s32 GetLightAnimsIndex(const char * key) const
380     //! @brief        ライトアニメーションリソースの辞書データ中でのインデックス番号を取得します。
381     //---------------------------------------------------------------------------
382     //---------------------------------------------------------------------------
383     //! @fn           s32 GetLightAnimsCount() const
384     //! @brief        ライトアニメーションリソースの要素数を取得します。
385     //---------------------------------------------------------------------------
386     //---------------------------------------------------------------------------
387     //! @fn           anim::res::ResAnim GetLightAnims(int idx)
388     //! @brief        ライトアニメーションリソースを取得します。
389     //---------------------------------------------------------------------------
390     //---------------------------------------------------------------------------
391     //! @fn           s32 GetFogsIndex(const char * key) const
392     //! @brief        フォグリソースの辞書データ中でのインデックス番号を取得します。
393     //---------------------------------------------------------------------------
394     //---------------------------------------------------------------------------
395     //! @fn           s32 GetFogsCount() const
396     //! @brief        フォグリソースの要素数を取得します。
397     //---------------------------------------------------------------------------
398     //---------------------------------------------------------------------------
399     //! @fn           ResFog GetFogs(int idx)
400     //! @brief        フォグリソースを取得します。
401     //---------------------------------------------------------------------------
402     //---------------------------------------------------------------------------
403     //! @fn           s32 GetEmittersIndex(const char * key) const
404     //! @brief        エミッターリソースの辞書データ中でのインデックス番号を取得します。
405     //---------------------------------------------------------------------------
406     //---------------------------------------------------------------------------
407     //! @fn           s32 GetEmittersCount() const
408     //! @brief        エミッターリソースの要素数を取得します。
409     //---------------------------------------------------------------------------
410     //---------------------------------------------------------------------------
411     //! @fn           ResParticleEmitter GetEmitters(int idx)
412     //! @brief        エミッターリソースを取得します。
413     //---------------------------------------------------------------------------
414     //---------------------------------------------------------------------------
415     //! @fn           s32 GetCamerasIndex(const char * key) const
416     //! @brief        カメラリソースの辞書データ中でのインデックス番号を取得します。
417     //---------------------------------------------------------------------------
418     //---------------------------------------------------------------------------
419     //! @fn           s32 GetCamerasCount() const
420     //! @brief        カメラリソースの要素数を取得します。
421     //---------------------------------------------------------------------------
422     //---------------------------------------------------------------------------
423     //! @fn           ResCamera GetCameras(int idx)
424     //! @brief        カメラリソースを取得します。
425     //---------------------------------------------------------------------------
426     //---------------------------------------------------------------------------
427     //! @fn           s32 GetCameraAnimsIndex(const char * key) const
428     //! @brief        カメラアニメーションリソースの辞書データ中でのインデックス番号を取得します。
429     //---------------------------------------------------------------------------
430     //---------------------------------------------------------------------------
431     //! @fn           s32 GetCameraAnimsCount() const
432     //! @brief        カメラアニメーションリソースの要素数を取得します。
433     //---------------------------------------------------------------------------
434     //---------------------------------------------------------------------------
435     //! @fn           anim::res::ResAnim GetCameraAnims(int idx)
436     //! @brief        カメラアニメーションリソースを取得します。
437     //---------------------------------------------------------------------------
438 
439     NW_RES_FIELD_CLASS_DIC_DECL( ResModel, Models, nw::ut::ResDicPatricia )                 // GetModels(int idx), GetModels(const char*), GetModelsIndex(const char*), GetModelsCount()
440     NW_RES_FIELD_CLASS_DIC_DECL( ResTexture, Textures, nw::ut::ResDicPatricia )             // GetTextures(int idx), GetTextures(const char*), GetTexturesIndex(const char*), GetTexturesCount()
441     NW_RES_FIELD_CLASS_DIC_DECL( ResLookupTableSet, LutSets, nw::ut::ResDicPatricia )       // GetLutSets(int idx), GetLutSets(const char*), GetLutSetsIndex(const char*), GetLutSetsCount()
442     NW_RES_FIELD_CLASS_DIC_DECL( ResShader, Shaders, nw::ut::ResDicPatricia )               // GetShaders(int idx), GetShaders(const char*), GetShadersIndex(const char*), GetShadersCount()
443     NW_RES_FIELD_CLASS_DIC_DECL( ResCamera, Cameras, nw::ut::ResDicPatricia )               // GetCameras(int idx), GetCameras(const char*), GetCamerasIndex(const char*), GetCamerasCount()
444     NW_RES_FIELD_CLASS_DIC_DECL( ResLight, Lights, nw::ut::ResDicPatricia )                 // GetLights(int idx), GetLights(const char*), GetLightsIndex(const char*), GetLightsCount()
445     NW_RES_FIELD_CLASS_DIC_DECL( ResFog, Fogs, nw::ut::ResDicPatricia )                     // GetFogs(int idx), GetFogs(const char*), GetFogsIndex(const char*), GetFogsCount()
446     NW_RES_FIELD_CLASS_DIC_DECL( ResSceneEnvironmentSetting, SceneEnvironmentSettings, nw::ut::ResDicPatricia ) // GetSceneEnvironmentSettings(int idx), GetSceneEnvironmentSettings(const char*), GetSceneEnvironmentSettingsIndex(const char*), GetSceneEnvironmentCount()
447     NW_RES_FIELD_CLASS_DIC_DECL( anim::res::ResAnim, SkeletalAnims, nw::ut::ResDicPatricia )     // GetSkeletalAnims(int idx), GetSkeletalAnims(const char*), GetSkeletalAnimsIndex(const char*), GetSkeletalAnimsCount()
448     NW_RES_FIELD_CLASS_DIC_DECL( anim::res::ResAnim, MaterialAnims, nw::ut::ResDicPatricia )     // GetMaterialAnims(int idx), GetMaterialAnims(const char*), GetMaterialAnimsIndex(const char*), GetMaterialAnimsCount()
449     NW_RES_FIELD_CLASS_DIC_DECL( anim::res::ResAnim, VisibilityAnims, nw::ut::ResDicPatricia )   // GetVisibilityAnims(int idx), GetVisibilityAnims(const char*), GetVisibilityAnimsIndex(const char*), GetVisibilityAnimsCount()
450     NW_RES_FIELD_CLASS_DIC_DECL( anim::res::ResAnim, CameraAnims, nw::ut::ResDicPatricia )       // GetCameraAnims(int idx), GetCameraAnims(const char*), GetCameraAnimsIndex(const char*), GetCameraAnimsCount()
451     NW_RES_FIELD_CLASS_DIC_DECL( anim::res::ResAnim, LightAnims, nw::ut::ResDicPatricia )        // GetLightAnims(int idx), GetLightAnims(const char*), GetLightAnimsIndex(const char*), GetLightAnimsCount()
452     NW_RES_FIELD_CLASS_DIC_DECL( ResParticleEmitter, Emitters, nw::ut::ResDicPatricia )     // GetEmitters(int idx), GetEmitters(const char*), GetEmittersIndex(const char*), GetEmittersCount()
453 
454     //---------------------------------------------------------------------------
455     //! @brief        ファイル中のコンテントの初期化処理をおこないます。
456     //!               GLオブジェクトの初期化処理と、引数に与えられたファイルを使用した
457     //!               外部参照解決をおこないます。
458     //!
459     //! @param[in]    allocator アロケータです。
460     //! @param[in]    graphicsFile 参照解決を試みるグラフィックスリソースです。
461     //!
462     //! @return       ファイル内のすべての参照が解決済みの場合には、RESOURCE_RESULT_OK を返します。
463     //!               未解決の外部参照が残っている場合には、未解決コンテントの種類を示すフラグを返します。
464     //---------------------------------------------------------------------------
465     Result Setup(os::IAllocator* allocator, ResGraphicsFile graphicsFile);
466 
467     //---------------------------------------------------------------------------
468     //! @brief        自分のファイル内で完結した初期化処理をおこないます。
469     //!
470     //! @param[in]    allocator アロケータです。
471     //!
472     //! @return       ファイル内のすべての参照が解決済みの場合には、RESOURCE_RESULT_OK を返します。
473     //!               未解決の外部参照が残っている場合には、未解決コンテントの種類を示すフラグを返します。
474     //---------------------------------------------------------------------------
Setup(os::IAllocator * allocator)475     Result Setup(os::IAllocator* allocator) { return this->Setup( allocator, *this ); }
476 
477     //---------------------------------------------------------------------------
478     //! @brief        リソースの解放処理をおこないます。
479     //!
480     //! @details      他のファイルから参照されているオブジェクトもすべて解放します。
481     //---------------------------------------------------------------------------
482     void Cleanup();
483 
484     // TODO: 参照カウント方式を用意する。
485     // Result SafeCleanup();
486 
487     //---------------------------------------------------------------------------
488     //! @brief        ResGraphicsFile 内の全ての ResModel を巡回して TFunction を適用します。
489     //!
490     //! @tparam       TFunction ResModel を引数に取る関数(オブジェクト)の型です。
491     //! @param[in]    function 適用する関数(オブジェクト)です。
492     //---------------------------------------------------------------------------
493     template<typename TFunction>
ForeachModel(TFunction function)494     void ForeachModel(TFunction function)
495     {
496         ResModelArray::iterator end = this->GetModels().end();
497         for (ResModelArray::iterator model = this->GetModels().begin();
498             model != end; ++model)
499         {
500             function(*model);
501         }
502     }
503 
504     //---------------------------------------------------------------------------
505     //! @brief        ResGraphicsFile 内の全ての ResModel を巡回して TFunction を適用します。
506     //!               ポインタの特別バージョンです。
507     //!
508     //! @tparam       TFunction ResModel を引数に取る関数(オブジェクト)の型です。
509     //! @param[in]    function 適用する関数(オブジェクト)です。
510     //---------------------------------------------------------------------------
511     template<typename TFunction>
ForeachModel(TFunction * function)512     void ForeachModel(TFunction* function)
513     {
514         this->ForeachModel<TFunction&>(*function);
515     }
516 
517     //---------------------------------------------------------------------------
518     //! @brief        ResGraphicsFile 内の全ての ResMaterial を巡回して TFunction を適用します。
519     //!
520     //! @tparam       TFunction ResMaterial を引数に取る関数(オブジェクト)の型です。
521     //! @param[in]    function 適用する関数(オブジェクト)です。
522     //---------------------------------------------------------------------------
523     template<typename TFunction>
ForeachMaterial(TFunction function)524     void ForeachMaterial(TFunction function)
525     {
526         ResModelArray::iterator end = this->GetModels().end();
527         for (ResModelArray::iterator model = this->GetModels().begin();
528             model != end; ++model)
529         {
530             ResMaterialArray::iterator materialEnd = (*model).GetMaterials().end();
531             for (ResMaterialArray::iterator material = (*model).GetMaterials().begin();
532                 material != materialEnd;
533                 ++material)
534             {
535                 function(*material);
536             }
537         }
538     }
539     //---------------------------------------------------------------------------
540     //! @brief        ResGraphicsFile 内の全ての ResMaterial を巡回して TFunction を適用します。
541     //!               ポインタの特別バージョンです。
542     //!
543     //! @tparam       TFunction ResMaterial を引数に取る関数(オブジェクト)の型です。
544     //! @param[in]    function 適用する関数(オブジェクト)です。
545     //---------------------------------------------------------------------------
546     template<typename TFunction>
ForeachMaterial(TFunction * function)547     void ForeachMaterial(TFunction* function)
548     {
549         this->ForeachMaterial<TFunction&>(*function);
550     }
551 
552 
553     //---------------------------------------------------------------------------
554     //! @brief        ResGraphicsFile 内の全ての ResModel, ResMaterial を巡回して TFunction を適用します。
555     //!
556     //! @tparam       TFunction ResModel, ResMaterial を引数に取る関数(オブジェクト)の型です。
557     //! @param[in]    function 適用する関数(オブジェクト)です。
558     //---------------------------------------------------------------------------
559     template<typename TFunction>
ForeachModelMaterial(TFunction function)560     void ForeachModelMaterial(TFunction function)
561     {
562         ResModelArray::iterator end = this->GetModels().end();
563         for (ResModelArray::iterator model = this->GetModels().begin();
564             model != end; ++model)
565         {
566             ResMaterialArray::iterator materialEnd = (*model).GetMaterials().end();
567             for (ResMaterialArray::iterator material = (*model).GetMaterials().begin();
568                 material != materialEnd;
569                 ++material)
570             {
571                 function(*model, *material);
572             }
573         }
574     }
575     //---------------------------------------------------------------------------
576     //! @brief        ResGraphicsFile 内の全ての ResModel, ResMaterial を巡回して TFunction を適用します。
577     //!               ポインタの特別バージョンです。
578     //!
579     //! @tparam       TFunction ResModel, ResMaterial を引数に取る関数(オブジェクト)の型です。
580     //! @param[in]    function 適用する関数(オブジェクト)です。
581     //---------------------------------------------------------------------------
582     template<typename TFunction>
ForeachModelMaterial(TFunction * function)583     void ForeachModelMaterial(TFunction* function)
584     {
585         this->ForeachModelMaterial<TFunction&>(*function);
586     }
587 
588     //---------------------------------------------------------------------------
589     //! @brief        ResGraphicsFile 内の全ての ResTexture を巡回して TFunction を適用します。
590     //!
591     //! @tparam       TFunction ResTexture を引数に取る関数(オブジェクト)の型です。
592     //! @param[in]    function 適用する関数(オブジェクト)です。
593     //!
594     //! @sa nw::gfx::res::TextureLocationFlagSetter
595     //---------------------------------------------------------------------------
596     template<typename TFunction>
ForeachTexture(TFunction function)597     void ForeachTexture(TFunction function)
598     {
599         ResTextureArray::iterator end = this->GetTextures().end();
600         for (ResTextureArray::iterator texture = this->GetTextures().begin();
601             texture != end; ++texture)
602         {
603             function(*texture);
604         }
605     }
606 
607     //---------------------------------------------------------------------------
608     //! @brief        ResGraphicsFile 内の全ての ResTexture を巡回して TFunction を適用します。
609     //!               ポインタの特別バージョンです。
610     //!
611     //! @tparam       TFunction ResTexture を引数に取る関数(オブジェクト)の型です。
612     //! @param[in]    function 適用する関数(オブジェクト)です。
613     //!
614     //! @sa nw::gfx::res::TextureLocationFlagSetter
615     //---------------------------------------------------------------------------
616     template<typename TFunction>
ForeachTexture(TFunction * function)617     void ForeachTexture(TFunction* function)
618     {
619         this->ForeachTexture<TFunction&>(*function);
620     }
621 
622     //---------------------------------------------------------------------------
623     //! @brief        ResGraphicsFile 内の全ての ResIndexStream を巡回して TFunction を適用します。
624     //!
625     //! @tparam       TFunction ResIndexStream を引数に取る関数(オブジェクト)の型です。
626     //! @param[in]    function 適用する関数(オブジェクト)です。
627     //!
628     //! @sa nw::gfx::res::IndexStreamLocationFlagSetter
629     //---------------------------------------------------------------------------
630     template<typename TFunction>
ForeachIndexStream(TFunction function)631     void ForeachIndexStream(TFunction function)
632     {
633         ResModelArray::iterator modelsEnd = this->GetModels().end();
634         for (ResModelArray::iterator model = this->GetModels().begin();
635             model != modelsEnd ; ++model)
636         {
637             ResShapeArray::iterator shapesEnd = (*model).GetShapes().end();
638             for (ResShapeArray::iterator shape = (*model).GetShapes().begin();
639                 shape != shapesEnd; ++shape)
640             {
641                 ResPrimitiveSetArray::iterator primitiveSetsEnd = (*shape).GetPrimitiveSets().end();
642                 for (ResPrimitiveSetArray::iterator primitiveSet = (*shape).GetPrimitiveSets().begin();
643                     primitiveSet != primitiveSetsEnd; ++primitiveSet)
644                 {
645                     ResPrimitiveArray::iterator primitivesEnd = (*primitiveSet).GetPrimitives().end();
646                     for (ResPrimitiveArray::iterator primitive = (*primitiveSet).GetPrimitives().begin();
647                         primitive != primitivesEnd; ++primitive)
648                     {
649                         ResIndexStreamArray::iterator indexStreamsEnd = (*primitive).GetIndexStreams().end();
650                         for (ResIndexStreamArray::iterator indexStream = (*primitive).GetIndexStreams().begin();
651                             indexStream != indexStreamsEnd; ++indexStream)
652                         {
653                             function(*indexStream);
654                         }
655                     }
656                 }
657             }
658         }
659     }
660 
661     //---------------------------------------------------------------------------
662     //! @brief        ResGraphicsFile 内の全ての ResIndexStream を巡回して TFunction を適用します。
663     //!               ポインタの特別バージョンです。
664     //!
665     //! @tparam       TFunction ResIndexStream を引数に取る関数(オブジェクト)の型です。
666     //! @param[in]    function 適用する関数(オブジェクト)です。
667     //!
668     //! @sa nw::gfx::res::IndexStreamLocationFlagSetter
669     //---------------------------------------------------------------------------
670     template<typename TFunction>
ForeachIndexStream(TFunction * function)671     void ForeachIndexStream(TFunction* function)
672     {
673         this->ForeachIndexStream<TFunction&>(*function);
674     }
675 
676     //---------------------------------------------------------------------------
677     //! @brief        ResGraphicsFile 内の全ての ResVertexStream を巡回して TFunction を適用します。
678     //!
679     //! @tparam       TFunction ResVertexStream を引数に取る関数(オブジェクト)の型です。
680     //! @param[in]    function 適用する関数(オブジェクト)です。
681     //!
682     //! @sa nw::gfx::res::VertexStreamLocationFlagSetter
683     //---------------------------------------------------------------------------
684     template<typename TFunction>
ForeachVertexStream(TFunction function)685     void ForeachVertexStream(TFunction function)
686     {
687         ResModelArray::iterator modelsEnd = this->GetModels().end();
688         for (ResModelArray::iterator model = this->GetModels().begin(); model != modelsEnd ; ++model)
689         {
690             ResShapeArray::iterator shapesEnd = (*model).GetShapes().end();
691             for (ResShapeArray::iterator shape = (*model).GetShapes().begin();
692                 shape != shapesEnd; ++shape)
693             {
694                 ResSeparateDataShape resSeparateDataShape = ResDynamicCast<ResSeparateDataShape>(*shape);
695 
696                 if (resSeparateDataShape.IsValid())
697                 {
698                     ResVertexAttributeArray::iterator vertexAttributesEnd = resSeparateDataShape.GetVertexAttributes().end();
699                     for (ResVertexAttributeArray::iterator vertexAttribute = resSeparateDataShape.GetVertexAttributes().begin();
700                         vertexAttribute != vertexAttributesEnd; ++vertexAttribute)
701                     {
702                         ResVertexStreamBase resVertexStream = ResDynamicCast<ResVertexStreamBase>(*vertexAttribute);
703 
704                         if (resVertexStream.IsValid())
705                         {
706                             function(resVertexStream);
707                         }
708                     }
709                 }
710             }
711         }
712     }
713 
714     //---------------------------------------------------------------------------
715     //! @brief        ResGraphicsFile 内の全ての ResVertexStream を巡回して TFunction を適用します。
716     //!               ポインタの特別バージョンです。
717     //!
718     //! @tparam       TFunction ResVertexStream を引数に取る関数(オブジェクト)の型です。
719     //! @param[in]    function 適用する関数(オブジェクト)です。
720     //!
721     //! @sa nw::gfx::res::VertexStreamLocationFlagSetter
722     //---------------------------------------------------------------------------
723     template<typename TFunction>
ForeachVertexStream(TFunction * function)724     void ForeachVertexStream(TFunction* function)
725     {
726         this->ForeachVertexStream<TFunction&>(*function);
727     }
728 };
729 
730 
731 //---------------------------------------------------------------------------
732 //! @brief        テクスチャや頂点データの配置アドレスを設定する関数オブジェクトです。
733 //!
734 //! @sa nw::gfx::res::ResGraphicsFile::ForeachTexture
735 //! @sa nw::gfx::res::ResGraphicsFile::ForeachIndexStream
736 //! @sa nw::gfx::res::ResGraphicsFile::ForeachVertexStream
737 //---------------------------------------------------------------------------
738 class TransferedVramAddressSetter
739 {
740 public:
TransferedVramAddressSetter(void * fcramImageAddress,void * vramImageAddress)741     TransferedVramAddressSetter(void* fcramImageAddress, void* vramImageAddress)
742      : m_FcramImageAddress(fcramImageAddress),
743        m_VramImageAddress(vramImageAddress)
744     {}
745 
746     //---------------------------------------------------------------------------
747     //! @brief        テクスチャイメージデータに VRAM アドレスを設定します。
748     //---------------------------------------------------------------------------
operator()749     void operator()(nw::gfx::ResTexture resTexture) const
750     {
751          // ImageTextureとCubeTextureのときだけloadFlagを設定.
752         switch( resTexture.ref().typeInfo )
753         {
754         case nw::gfx::ResImageTexture::TYPE_INFO:
755             {
756                 nw::gfx::ResImageTexture resImageTexture = nw::gfx::ResStaticCast<nw::gfx::ResImageTexture>( resTexture );
757 
758                 nw::gfx::ResPixelBasedImage resImage = resImageTexture.GetImage();
759                 const void* locationAddress = this->GetVramLocation( resImage.GetImageData() );
760                 resImage.SetLocationAddress( locationAddress );
761             }
762             break;
763         case nw::gfx::ResCubeTexture::TYPE_INFO:
764             {
765                 nw::gfx::ResCubeTexture resCubeTexture = nw::gfx::ResStaticCast<nw::gfx::ResCubeTexture>( resTexture );
766 
767                 for ( int face = 0; face < nw::gfx::ResCubeTexture::MAX_CUBE_FACE; ++face )
768                 {
769                     nw::gfx::ResPixelBasedImage resImage = resCubeTexture.GetImage( nw::gfx::ResCubeTexture::CubeFace(face) );
770                     const void* locationAddress = this->GetVramLocation( resImage.GetImageData() );
771                     resImage.SetLocationAddress( locationAddress );
772                 }
773             }
774             break;
775         default:
776             break;
777         }
778     }
779 
780     //---------------------------------------------------------------------------
781     //! @brief        インデックスストリームデータに VRAM アドレスを設定します。
782     //---------------------------------------------------------------------------
operator()783     void operator()(nw::gfx::ResIndexStream resIndexStream) const
784     {
785         const void* locationAddress = this->GetVramLocation( resIndexStream.GetStream() );
786         resIndexStream.SetLocationAddress( locationAddress );
787     }
788 
789     //---------------------------------------------------------------------------
790     //! @brief        頂点ストリームデータに VRAM アドレスを設定します。
791     //---------------------------------------------------------------------------
operator()792     void operator()(nw::gfx::ResVertexStreamBase resVertexStream) const
793     {
794         const void* locationAddress = this->GetVramLocation( resVertexStream.GetStream() );
795         resVertexStream.SetLocationAddress( locationAddress );
796     }
797 
798 private:
799 
800     //---------------------------------------------------------------------------
801     //! @brief        FCRAM 上のアドレスから VRAM 上のアドレスを計算します。
802     //!
803     //! @param[in]    fcramLocation FCRAM 上のアドレスです。
804     //!
805     //! @return       VRAM 上のアドレスを返します。
806     //---------------------------------------------------------------------------
GetVramLocation(const void * fcramLocation)807     const void* GetVramLocation(const void* fcramLocation) const
808     {
809         u32 addressOffset = nw::ut::GetOffsetFromPtr( m_FcramImageAddress, fcramLocation );
810         return nw::ut::AddOffsetToPtr( m_VramImageAddress, addressOffset );
811     }
812 
813     void* m_FcramImageAddress;
814     void* m_VramImageAddress;
815 };
816 
817 
818 //---------------------------------------------------------------------------
819 //! @brief        テクスチャ、頂点情報の配置場所を設定する関数オブジェクトです。
820 //!
821 //! @sa nw::gfx::res::ResGraphicsFile::ForeachTexture
822 //! @sa nw::gfx::res::ResGraphicsFile::ForeachIndexStream
823 //! @sa nw::gfx::res::ResGraphicsFile::ForeachVertexStream
824 //---------------------------------------------------------------------------
825 class LocationFlagSetter
826 {
827 public:
LocationFlagSetter(u32 flag)828     LocationFlagSetter(u32 flag) : m_Flag(flag) {}
829 
830     //---------------------------------------------------------------------------
831     //! @brief        テクスチャのロケーションフラグを設定します。
832     //---------------------------------------------------------------------------
operator()833     void operator()(ResTexture resTexture) const
834     {
835          // ImageTextureとCubeTextureのときだけloadFlagを設定.
836         switch( resTexture.ref().typeInfo )
837         {
838         case ResImageTexture::TYPE_INFO:
839             {
840                 ResImageTexture resImageTexture = ResStaticCast<ResImageTexture>( resTexture );
841                 resImageTexture.SetLocationFlag( m_Flag );
842             }
843             break;
844         case ResCubeTexture::TYPE_INFO:
845             {
846                 ResCubeTexture resCubeTexture = ResStaticCast<ResCubeTexture>( resTexture );
847                 resCubeTexture.SetLocationFlag( m_Flag );
848             }
849             break;
850         default:
851             break;
852         }
853     }
854 
855     //---------------------------------------------------------------------------
856     //! @brief        インデックスストリームのロケーションフラグを設定します。
857     //---------------------------------------------------------------------------
operator()858     void operator()(ResIndexStream resIndexStream) const
859     {
860         resIndexStream.SetLocationFlag(m_Flag);
861     }
862 
863     //---------------------------------------------------------------------------
864     //! @brief        頂点ストリームのロケーションフラグを設定します。
865     //---------------------------------------------------------------------------
operator()866     void operator()(ResVertexStreamBase resVertexStream) const
867     {
868         resVertexStream.SetLocationFlag(m_Flag);
869     }
870 
871 private:
872     u32 m_Flag;
873 };
874 
875 //---------------------------------------------------------------------------
876 //! @brief        テクスチャの配置場所を設定する関数オブジェクトです。
877 //!
878 //! @sa nw::gfx::res::ResGraphicsFile::ForeachTexture
879 //---------------------------------------------------------------------------
880 typedef LocationFlagSetter TextureLocationFlagSetter;
881 
882 //---------------------------------------------------------------------------
883 //! @brief        インデックスストリームの配置場所を設定する関数オブジェクトです。
884 //!
885 //! @sa nw::gfx::res::ResGraphicsFile::ForeachIndexStream
886 //---------------------------------------------------------------------------
887 typedef LocationFlagSetter IndexStreamLocationFlagSetter;
888 
889 //---------------------------------------------------------------------------
890 //! @brief        頂点バッファの配置場所を設定する関数オブジェクトです。
891 //!
892 //! @sa nw::gfx::res::ResGraphicsFile::ForeachVertexStream
893 //---------------------------------------------------------------------------
894 typedef LocationFlagSetter VertexStreamLocationFlagSetter;
895 
896 
897 //---------------------------------------------------------------------------
898 //! @brief        エフェクトリソース内、パーティクルマテリアル使用フラグを設定する関数オブジェクトです。
899 //!
900 //! @sa nw::gfx::res::ResGraphicsFile::ForeachModelMaterial
901 //---------------------------------------------------------------------------
902 class ParticleMaterialFlagSetter
903 {
904 public:
operator()905     void operator()(ResModel resModel, ResMaterial resMaterial) const
906     {
907         NW_ASSERT(resMaterial.IsValid());
908 
909         ut::ResTypeInfo resTypeInfo = resModel.GetTypeInfo();
910         if (resTypeInfo == nw::gfx::ResParticleModel::TYPE_INFO)
911         {
912             resMaterial.SetFlags(ut::EnableFlag(resMaterial.GetFlags(), nw::gfx::ResMaterialData::FLAG_PARTICLE_MATERIAL_ENABLED));
913         }
914     }
915 };
916 
917 //---------------------------------------------------------------------------
918 //! @brief       デフォルトシェーダーでの ShaderProgramDescriptionIndex を設定する関数オブジェクトです。
919 //!
920 //! @sa nw::gfx::res::ResGraphicsFile::ForeachMaterial
921 //---------------------------------------------------------------------------
922 class DefaultShaderAutoSelector
923 {
924 public:
operator()925     void operator()(ResModel resModel, ResMaterial resMaterial) const
926     {
927         const char* DEFAULT_SHADER = "DefaultShader";
928         NW_ASSERT(resMaterial.IsValid());
929         ut::ResTypeInfo resTypeInfo = resModel.GetTypeInfo();
930         if (resTypeInfo == ResModel::TYPE_INFO ||
931             resTypeInfo == ResSkeletalModel::TYPE_INFO)
932         {
933             ResReferenceShader referenceShader = ResDynamicCast<ResReferenceShader>(resMaterial.GetShader());
934             // HACK: 1.3 で ASSERT に変更します。
935             // NW_ASSERT(referenceShader.IsValid());
936             if (referenceShader.IsValid())
937             {
938                 if (::std::strcmp(referenceShader.GetPath(), DEFAULT_SHADER) == 0)
939                 {
940                     // アクティブなコーディネータの数によって DescriptionIndex を変えます。
941                     s32 activeCoordinatorsCount = resMaterial.GetActiveTextureCoordinatorsCount();
942 
943                     enum { TEXTURE_COUNT_0 = 3, TEXTURE_COUNT_1 = 2, TEXTURE_COUNT_2 = 1, TEXTURE_COUNT_3 = 0 };
944                     const int table[] =
945                     {
946                         TEXTURE_COUNT_0,
947                         TEXTURE_COUNT_1,
948                         TEXTURE_COUNT_2,
949                         TEXTURE_COUNT_3
950                     };
951 
952                     resMaterial.SetShaderProgramDescriptionIndex(table[activeCoordinatorsCount]);
953                 }
954             }
955         }
956     }
957 };
958 
959 } // namespace res
960 } // namespace gfx
961 } // namespace nw
962 
963 #endif // NW_GFX_RESGRAPHICSFILE_H_
964