1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     gr_Shadow.h
4 
5   Copyright (C)2010 Nintendo Co., Ltd.  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   $Rev: 29329 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_GR_SHADOW_H_
17 #define NN_GR_SHADOW_H_
18 
19 #include <nn/gr/CTR/gr_Prefix.h>
20 
21 namespace nn
22 {
23     namespace gr
24     {
25         namespace CTR
26         {
27 
28             /*!
29                 @brief シャドウの設定のためのクラスです。
30              */
31             class Shadow
32             {
33             public :
34                 /*!
35                     @brief 各設定の初期値を設定します。
36                 */
Shadow(void)37                 Shadow( void ) :
38                 isPerspective( true ),
39                 zBias( 0.0f ), zScale( 1.0f ),
40                 penumbraScale( 0.0f ), penumbraBias( 1.0f )
41                 {
42                 }
43 
44                 /*!
45                     @brief 設定された情報をもとに、シャドウの設定コマンドを生成します。
46 
47                     @param[in] command 描画コマンドの書き込み先の先頭アドレスです。
48                     @param[in] isAddDummyCommand 0x80 へのダミー書き込みコマンドを 3回 生成します。
49                     @param[in] isAddCacheClearCommand レジスタの変更前に、フレームバッファのキャッシュフラッシュをします。
50 
51                     @return    書き込まれた描画コマンドの終端の次のアドレスを返します。
52                  */
53                 bit32* MakeCommand( bit32* command, bool isAddDummyCommand = true, bool isAddCacheClearCommand = true )
54                 {
55                     command = MakeTextureCommand( command, isAddDummyCommand );
56                     command = MakeAttenuationCommand( command, isAddCacheClearCommand );
57 
58                     return command;
59                 }
60 
61                 /*!
62                     @brief 設定された情報をもとに、シャドウのテクスチャの設定コマンドを生成します。
63 
64                     @param[in] command 描画コマンドの書き込み先の先頭アドレスです。
65                     @param[in] isAddDummyCommand レジスタの変更前に、0x80 へのダミーコマンドを 3 回生成します。
66 
67                     @return    書き込まれた描画コマンドの終端の次のアドレスを返します。
68                  */
69                 bit32* MakeTextureCommand( bit32* command, bool isAddDummyCommand = true )
70                 {
71                     // 0x080 へのダミーコマンド を 3 回送ります
72                     if ( isAddDummyCommand )
73                     {
74                         *command++ = 0x0;
75                         *command++ = PICA_CMD_HEADER_BURST_BE( PICA_REG_TEXTURE_FUNC, 0x3, 0x0 );
76 
77                         *command++ = 0x0;
78                         *command++ = 0x0;
79                     }
80 
81                     // 下位 1 bit をクリアします。
82                     bit32 clearMask = ~( 0xfffffe );
83                     u32 zBiasFix24 = Float32ToUnsignedFix24( zBias );
84                     zBiasFix24 &= clearMask;
85 
86                     // 0x8b
87                     *command++ =
88                         PICA_CMD_DATA_TEXTURE_SHADOW( isPerspective,
89                                     // f32 -> 24bit 符号なし固定小数点数 の上位 23 bit
90                                     zBiasFix24,
91                                     // f32の(指数部-127)を符号無し8ビット整数に変換
92                                     FloatToUnsignedByte( zScale ) );
93                     *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_TEXTURE0_SHADOW );
94 
95                     return command;
96                 }
97 
98 
99                 /*!
100                     @brief 設定された情報をもとに、シャドウの減衰の設定コマンドを生成します。
101 
102                     @param[in] command 描画コマンドの書き込み先の先頭アドレスです。
103                     @param[in] isAddCacheClearCommand レジスタの変更前に、フレームバッファのキャッシュフラッシュをします。
104 
105                     @return    書き込まれた描画コマンドの終端の次のアドレスを返します。
106                  */
107                 bit32* MakeAttenuationCommand( bit32* command, bool isAddCacheClearCommand = true )
108                 {
109                     // フレームバッファのキャッシュクリアをします。
110                     if ( isAddCacheClearCommand )
111                     {
112                         // 0x111
113                         *command++ = 0x1;
114                         *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_COLOR_DEPTH_BUFFER_CLEAR1 );
115 
116                         // 0x110
117                         *command++ = 0x1;
118                         *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_COLOR_DEPTH_BUFFER_CLEAR0 );
119 
120                     }
121 
122                     // 0x130
123                     *command++ = ( Float32ToFloat16( - penumbraScale ) ) << 16 |
124                                    Float32ToFloat16( penumbraScale + penumbraBias );
125                     *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_SHADOW );
126 
127                     return command;
128                 }
129 
130                 /*!
131                     @brief シャドウテクスチャへの参照に対するテクスチャ座標生成の透視投影適用の有効/無効にします。
132                            型は bool です。デフォルトは true です。
133                            dmp_Texture[0].isPerspective ユニフォームに相当します。
134                 */
135                 bool isPerspective;
136                 NN_PADDING3;
137 
138                 /*!
139                     @brief 光源座標系のスクリーン空間におけるデプス値導関数の評価値に対するスケールです。
140                            型は f32 です。0.0 より大きな値を設定します。
141                            デフォルトは本来未定義ですが、1.0f に初期化しておきます。
142                            dmp_Texture[0].shadowZBias ユニフォームに相当します。
143                 */
144                 f32 zBias;
145 
146                 /*!
147                     @breif 光源までの距離から減算されるバイアス値です。
148                            型は f32 です。デフォルトは 0.0f です。
149                            dmp_Texture[0].shadowZScale ユニフォームに相当します。
150                 */
151                 f32 zScale;
152 
153                 /*!
154                     @brief 半影のハード化計算時に適用されるスケールです。
155                            型は f32 です。デフォルトは 0.0f です。
156                            dmp_FragOperation.penumbraScale ユニフォームに相当します。
157 
158                 */
159                 f32 penumbraScale;
160 
161                 /*!
162                     @brief  半影のハード化計算時に適用されるバイアスです。
163                             型は f32 です。デフォルトは 1.0f です。
164                             dmp_FragOperation.penumbraBias ユニフォームに相当します。
165                 */
166                 f32 penumbraBias;
167             };
168 
169         } // namespace CTR
170     } // namespace gr
171 } // namespace nn
172 
173 #endif // NN_GR_SHADOW_H_
174