1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_RelativeHashMaterialIdGenerator.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: 22799 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_RELATIVEHASHMATERIALIDGENERATOR_H_
17 #define NW_GFX_RELATIVEHASHMATERIALIDGENERATOR_H_
18 
19 #include <nw/gfx/gfx_IMaterialIdGenerator.h>
20 
21 namespace nw
22 {
23 namespace gfx
24 {
25 
26 //---------------------------------------------------------------------------
27 //! @brief        マテリアルハッシュを用いたマテリアルIDの生成を行うクラスです。
28 //!               RelativeHashMaterialIdGenerator は 1.3.0 で廃止予定です。
29 //!               SortingMaterialIdGenerator か、独自に実装した MaterialIdGenerator を用いてください。
30 //---------------------------------------------------------------------------
31 class RelativeHashMaterialIdGenerator : public IMaterialIdGenerator
32 {
33 private:
34     NW_DISALLOW_COPY_AND_ASSIGN(RelativeHashMaterialIdGenerator);
35 
36     enum MaterialOrder
37     {
38         MATERIAL_ORDER_SHADER_PARAMETERS,
39         MATERIAL_ORDER_ALPHA_TEST,
40         MATERIAL_ORDER_TEXTURE_SAMPLERS,
41         MATERIAL_ORDER_TEXTURE_COMBINER,
42         MATERIAL_ORDER_FRAGMENT_OPERATION,
43         MATERIAL_ORDER_FRAGMENT_LIGHTING_TABLE_PARAMETERS,
44         MATERIAL_ORDER_RASTERIZATION,
45         MATERIAL_ORDER_SHADING_PARMETER,
46         MATERIAL_ORDER_TEXTURE_COORDINATORS,
47         MATERIAL_ORDER_MATERIAL_COLOR,
48         MATERIAL_ORDER_FRAGMENT_LIGHTING
49     };
50 
51 
52 public:
53     NW_UT_RUNTIME_TYPEINFO;
54 
55     //! @brief 設定内容です。
56     struct Description
57     {
58         bool isForceGeneration; //!< 強制的に生成するフラグです。
59         bool isFixedSizeMemory; //!< 最初に固定サイズのメモリを確保するフラグです。
60         int  maxMaterials;      //!< マテリアルの最大数です。
61 
62         //! @brief コンストラクタです。
DescriptionDescription63         Description()
64          : isForceGeneration(false),
65            isFixedSizeMemory(true),
66            maxMaterials(128)
67         {}
68     };
69 
70     //----------------------------------------
71     //! @name 生成
72     //@{
73 
74 
75     //! マテリアルハッシュを用いたマテリアルIDの生成クラスを構築するためのクラスです。
76     //!
77     //! IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。
78     class Builder
79     {
80     public:
81         //! 強制的に生成するフラグを設定します。
IsForceGeneration(bool isForceGeneration)82         Builder& IsForceGeneration(bool isForceGeneration)
83         {
84             m_Description.isForceGeneration = isForceGeneration;
85             return *this;
86         }
87 
88         //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
89         //!
90         //!        true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
91         //!
92         //!        false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)93         Builder& IsFixedSizeMemory(bool isFixedSizeMemory)
94         {
95             m_Description.isFixedSizeMemory = isFixedSizeMemory;
96             return *this;
97         }
98 
99         //! マテリアルの最大数を設定します。
MaxMaterials(int maxMaterials)100         Builder& MaxMaterials(int maxMaterials)
101         {
102             m_Description.maxMaterials = maxMaterials;
103             return *this;
104         }
105 
106         //! @brief        マテリアルIDの生成クラスを生成します。
107         //!               RelativeHashMaterialIdGenerator は 1.3.0 で廃止予定です。
108         //!               SortingMaterialIdGenerator か、独自に実装した MaterialIdGenerator を用いてください。
109         //!
110         //! @param[in]    allocator アロケータです。
111         //!
112         //! @return       生成したマテリアルIDの生成クラスを返します。
113         //!
114         NW_DEPRECATED_FUNCTION(IMaterialIdGenerator* Create(os::IAllocator* allocator));
115 
116     private:
117         Description m_Description;
118     };
119 
120     //! @brief        マテリアルを受け取ります。
121     //!               ここではマテリアルを配列に追加します。
122     //!
123     //! @param[in]    material マテリアルです。
124     //!
Accept(Material * material)125     virtual void Accept(Material* material)
126     {
127         bool isPushed = m_Materials.push_back(material);
128         NW_ASSERT(isPushed);
129     }
130 
131     //! @brief        マテリアルIDを生成します。
132     virtual void Generate();
133 
134     //@}
135 
136 private:
137     //----------------------------------------
138     //! @name コンストラクタ/デストラクタ
139     //@{
140 
141     //! コンストラクタです。
RelativeHashMaterialIdGenerator(os::IAllocator * allocator,bool isForceGeneration,MaterialArray materials)142     RelativeHashMaterialIdGenerator(
143         os::IAllocator* allocator,
144         bool isForceGeneration,
145         MaterialArray materials)
146         : IMaterialIdGenerator(allocator),
147           m_IsForceGeneration(isForceGeneration),
148           m_Materials(materials)
149     {}
150 
151     //@}
152 
153     NW_INLINE u32 CompareHash(u32 materialHash, MaterialOrder order);
154 
155     static const u32 m_DefaultHashes[];
156     bool m_IsForceGeneration;
157 
158     MaterialArray m_Materials;
159 };
160 
161 } // namespace gfx
162 } // namespace nw
163 
164 #endif // NW_GFX_RELATIVEHASHMATERIALIDGENERATOR_H_
165