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