1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_SortingMaterialIdGenerator.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: 24209 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_SORTINGMATERIALIDGENERATOR_H_
17 #define NW_GFX_SORTINGMATERIALIDGENERATOR_H_
18 
19 #include <nw/gfx/gfx_IMaterialIdGenerator.h>
20 
21 namespace nw
22 {
23 namespace gfx
24 {
25 
26 namespace internal
27 {
28     //---------------------------------------------------------------------------
29     //! @brief        マテリアルのソート用のキーと値です。
30     //---------------------------------------------------------------------------
31     struct MaterialKeyValue
32     {
33         u32 uniqueId;
34         u32 key;
35         u32 subKey;
36         Material* material;
37 
38     };
39 }
40 
41 //---------------------------------------------------------------------------
42 //! @brief        ソートを行った結果からマテリアルIDの生成を行うクラスです。
43 //!
44 //! @details      マテリアルIDの生成は以下の優先度で行われます。
45 //!               バイナリ比較 > シェーダー比較 > 参照テーブル比較
46 //---------------------------------------------------------------------------
47 class SortingMaterialIdGenerator : public IMaterialIdGenerator
48 {
49 private:
50     NW_DISALLOW_COPY_AND_ASSIGN(SortingMaterialIdGenerator);
51 
52     typedef ut::MoveArray<internal::MaterialKeyValue> MaterialKeyValueArray;
53 public:
54     NW_UT_RUNTIME_TYPEINFO;
55 
56     //! @brief 設定内容です。
57     struct Description
58     {
59         bool isFixedSizeMemory; //!< 最初に固定サイズのメモリを確保するフラグです。
60         int  maxMaterials;      //!< マテリアルの最大数です。
61 
62         //! @brief コンストラクタです。
DescriptionDescription63         Description()
64          : isFixedSizeMemory(true),
65            maxMaterials(128)
66         {}
67     };
68 
69     //----------------------------------------
70     //! @name 生成
71     //@{
72 
73 
74     //! @brief ResMaterialのバイナリリソースのポインタからマテリアルIDの生成を行うクラスを構築するためのクラスです。
75     //!
76     //!        IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。
77     class Builder
78     {
79     public:
80         //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
81         //!
82         //!        true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
83         //!
84         //!        false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)85         Builder& IsFixedSizeMemory(bool isFixedSizeMemory)
86         {
87             m_Description.isFixedSizeMemory = isFixedSizeMemory;
88             return *this;
89         }
90 
91         //! マテリアルの最大数を設定します。
MaxMaterials(int maxMaterials)92         Builder& MaxMaterials(int maxMaterials)
93         {
94             m_Description.maxMaterials = maxMaterials;
95             return *this;
96         }
97 
98         //! @brief        マテリアルIDの生成クラスを生成します。
99         //!
100         //! @param[in]    allocator アロケータです。
101         //!
102         //! @return       生成したマテリアルIDの生成クラスを返します。
103         //!
104         IMaterialIdGenerator* Create(os::IAllocator* allocator);
105 
106     private:
107         Description m_Description;
108     };
109 
110     //! @brief        マテリアルを受け取ります。
111     //!               ここではマテリアルを配列に追加します。
112     //!
113     //! @param[in]    material マテリアルです。
114     //!
115     virtual void Accept(Material* material);
116 
117     //! @brief        マテリアルIDを生成します。
118     virtual void Generate();
119 
120     //@}
121 
122 private:
123 
124     //----------------------------------------
125     //! @name コンストラクタ/デストラクタ
126     //@{
127 
128     //! コンストラクタです。
SortingMaterialIdGenerator(os::IAllocator * allocator,MaterialKeyValueArray materials,MaterialKeyValueArray materialsWorkSpace)129     SortingMaterialIdGenerator(
130         os::IAllocator* allocator,
131         MaterialKeyValueArray materials,
132         MaterialKeyValueArray materialsWorkSpace)
133         : IMaterialIdGenerator(allocator),
134           m_Materials(materials),
135           m_MaterialsWorkSpace(materialsWorkSpace)
136     {}
137 
138     //@}
139 
140     MaterialKeyValueArray m_Materials;
141     MaterialKeyValueArray m_MaterialsWorkSpace;
142 };
143 
144 } // namespace gfx
145 } // namespace nw
146 
147 #endif // NW_GFX_SORTINGMATERIALIDGENERATOR_H_
148