1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_FrameBuffer.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: 22599 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_FRAME_BUFFER_H_
17 #define NW_GFX_FRAME_BUFFER_H_
18 
19 #include <gles2/gl2.h>
20 #include <nw/ut/ut_Color.h>
21 
22 namespace nw {
23 namespace gfx {
24 
25 //---------------------------------------------------------------------------
26 //! @brief        フレームバッファの設定です。
27 //---------------------------------------------------------------------------
28 class FrameBufferObject
29 {
30 public:
31 
32     //! @brief 設定内容です。
33     struct Description
34     {
35         GLuint fboID;     //!< フレームバッファオブジェクトのIDを指定します。
36         u32 height;       //!< バッファの高さ情報です。
37         u32 width;        //!< バッファの幅情報です。
38         u32 colorFormat;  //!< カラーバッファのフォーマットです。
39         u32 depthFormat;  //!< デプスバッファのフォーマットです。
40         u32 colorAddress; //!< カラーバッファのアドレスを指定します。
41         u32 depthAddress; //!< デプスバッファのアドレスを指定します。
42 
43         bool useBlock32;  //!< ブロック32モードを使用するかどうかを指定します。EarlyDepthTest を使用しない場合は通常 false を設定します。
44 
45         //! @brief デフォルトコンストラクタです。
DescriptionDescription46         Description()
47         {
48             fboID = 0;
49             useBlock32 = false;
50             colorAddress = NULL;
51             depthAddress = NULL;
52         }
53     };
54 
55     //---------------------------------------------------------------------------
56     //! @brief        過去互換のための、フレームバッファオブジェクトID へのキャスト演算子です。
57     //!
58     //! @return       gl の FBO を使用している場合は fbo のIDを返します。使用していない場合は 0 を返します。
59     //---------------------------------------------------------------------------
GLuint()60     operator GLuint() const
61     {
62         return m_Description.fboID;
63     }
64 
65     //---------------------------------------------------------------------------
66     //! @brief        フレームバッファ設定を取得します。
67     //!
68     //! @return       フレームバッファの設定情報です。
69     //---------------------------------------------------------------------------
GetDescription()70     const Description& GetDescription() const { return m_Description; }
GetDescription()71     Description& GetDescription() { return m_Description; }
72 
73     //---------------------------------------------------------------------------
74     //! @brief        フレームバッファ設定をセットします。
75     //!
76     //! @param[in]    description フレームバッファ設定です。
77     //---------------------------------------------------------------------------
78     void  SetDescription(const Description& description);
79 
80     //---------------------------------------------------------------------------
81     //! @brief        gl のフレームバッファオブジェクトのIDを取得します。
82     //!
83     //! @return       gl のフレームバッファオブジェクトのIDです。
84     //---------------------------------------------------------------------------
GetFboID()85     GLuint GetFboID() const { return m_Description.fboID; }
86 
87     //---------------------------------------------------------------------------
88     //! @brief        gl のフレームバッファオブジェクトのIDを設定します。
89     //!
90     //! @param[in]    fboID gl のフレームバッファオブジェクトのIDです。
91     //---------------------------------------------------------------------------
92     void   SetFboID(GLuint fboID);
93 
94     //---------------------------------------------------------------------------
95     //! @brief        フレームバッファの高さを取得します。
96     //!
97     //! @return       フーレムバッファの高さです。
98     //---------------------------------------------------------------------------
GetHeight()99     u32    GetHeight() const { return m_Description.height; }
100 
101     //---------------------------------------------------------------------------
102     //! @brief        フレームバッファの高さを設定します。
103     //!
104     //! @param[in]    height  フレームバッファの高さです。
105     //---------------------------------------------------------------------------
SetHeight(u32 height)106     void   SetHeight(u32 height) { m_Description.height = height; }
107 
108     //---------------------------------------------------------------------------
109     //! @brief        フレームバッファの幅を取得します。
110     //!
111     //! @return       フレームバッファの幅です。
112     //---------------------------------------------------------------------------
GetWidth()113     u32    GetWidth() const { return m_Description.width; }
114 
115     //---------------------------------------------------------------------------
116     //! @brief        フレームバッファの幅を設定します。
117     //!
118     //! @param[in]    width   フレームバッファの幅です。
119     //---------------------------------------------------------------------------
SetWidth(u32 width)120     void   SetWidth(u32 width) { m_Description.width = width; }
121 
122     //---------------------------------------------------------------------------
123     //! @brief        カラーバッファのフォーマットを取得します。
124     //!
125     //! @return       カラーバッファのフォーマットです。
126     //---------------------------------------------------------------------------
GetColorFormat()127     u32    GetColorFormat() const { return m_Description.colorFormat; }
128 
129     //---------------------------------------------------------------------------
130     //! @brief        カラーバッファのフォーマットを設定します。
131     //!
132     //! @param[in]    colorFormat  カラーバッファのフォーマットです。
133     //---------------------------------------------------------------------------
SetColorFormat(u32 colorFormat)134     void   SetColorFormat(u32 colorFormat) { m_Description.colorFormat = colorFormat; }
135 
136     //---------------------------------------------------------------------------
137     //! @brief        デプスバッファのフォーマットを取得します。
138     //!
139     //! @return       デプスバッファのフォーマットです。
140     //---------------------------------------------------------------------------
GetDepthFormat()141     u32    GetDepthFormat() const { return m_Description.depthFormat; }
142 
143     //---------------------------------------------------------------------------
144     //! @brief        デプスバッファのフォーマットを設定します。
145     //!
146     //! @param[in]    depthFormat  デプスバッファのフォーマットです。
147     //---------------------------------------------------------------------------
SetDepthFormat(u32 depthFormat)148     void   SetDepthFormat(u32 depthFormat) { m_Description.depthFormat = depthFormat; }
149 
150     //---------------------------------------------------------------------------
151     //! @brief        カラーバッファのアドレスを取得します。
152     //!
153     //! @return       カラーバッファのアドレスです。
154     //---------------------------------------------------------------------------
GetColorAddress()155     u32    GetColorAddress() const { return m_Description.colorAddress; }
156 
157     //---------------------------------------------------------------------------
158     //! @brief        カラーバッファのアドレスを設定します。
159     //!
160     //! @param[in]    colorAddress  カラーバッファのアドレスです。
161     //---------------------------------------------------------------------------
SetColorAddress(u32 colorAddress)162     void   SetColorAddress(u32 colorAddress) { m_Description.colorAddress = colorAddress; }
163 
164     //---------------------------------------------------------------------------
165     //! @brief        デプスバッファのアドレスを取得します。
166     //!
167     //! @return       デプスバッファのアドレスです。
168     //---------------------------------------------------------------------------
GetDepthAddress()169     u32    GetDepthAddress() const { return m_Description.depthAddress; }
170 
171     //---------------------------------------------------------------------------
172     //! @brief        デプスバッファのアドレスを設定します。
173     //!
174     //! @param[in]    depthAddress  デプスバッファのアドレスです。
175     //---------------------------------------------------------------------------
SetDepthAddress(u32 depthAddress)176     void   SetDepthAddress(u32 depthAddress) { m_Description.depthAddress = depthAddress; }
177 
178     //---------------------------------------------------------------------------
179     //! @brief        フレームバッファの設定用コマンドを設定します。
180     //---------------------------------------------------------------------------
181     void ActivateBuffer() const;
182 
183     //---------------------------------------------------------------------------
184     //! @brief        フレームバッファからディスプレイバッファへの転送コマンドを設定します。
185     //!
186     //! @param[in]    dstAddress    転送先アドレスです。
187     //! @param[in]    dstFormat     転送先のフォーマットです。
188     //! @param[in]    antiAliasMode アンチエイリアスフィルタのモードです。
189     //! @param[in]    yFlip         Yフリップをおこなうかどうかのフラグです。
190     //---------------------------------------------------------------------------
TransferRenderImage(u32 dstAddress,GLenum dstFormat,GLenum antiAliasMode,bool yFlip)191     void TransferRenderImage(u32 dstAddress, GLenum dstFormat, GLenum antiAliasMode, bool yFlip) const
192     {
193         const void* srcAddress = reinterpret_cast<const void*>(this->GetColorAddress());
194         const u32 width        = this->GetWidth();
195         const u32 height       = this->GetHeight();
196         const u32 srcFormat    = this->GetColorFormat();
197 
198         nngxAddB2LTransferCommand(
199             srcAddress, width, height, srcFormat,
200             reinterpret_cast<void*>(dstAddress), width, height, dstFormat,
201             antiAliasMode, yFlip, 8);
202     }
203 
204     enum
205     {
206         //! カラーバッファをクリアするフラグです。
207         CLEAR_MASK_COLOR = GL_COLOR_BUFFER_BIT,
208 
209         //! デプスバッファとステンシルバッファをクリアするフラグです。
210         //! ステンシルバッファ単体でのクリアはできません。
211         CLEAR_MASK_DEPTH = GL_DEPTH_BUFFER_BIT,
212 
213         //! カラーバッファ、デプスバッファの両方をクリアするフラグです。
214         CLEAR_MASK_ALL   = CLEAR_MASK_COLOR | CLEAR_MASK_DEPTH
215     };
216 
217     //---------------------------------------------------------------------------
218     //! @brief        フレームバッファのクリアを実行します。
219     //!
220     //! @details      AcitvateFrameBuffer で有効化されているフレームバッファに対して、
221     //!               glClear 相当のクリア処理を実行します。
222     //!
223     //! @param[in]    mask         クリアするバッファを指定するマスク値です。
224     //! @param[in]    clearColor   クリアカラーです。
225     //! @param[in]    clearDepth   クリアデプスです。
226     //! @param[in]    clearStencil クリアステンシル値です。
227     //---------------------------------------------------------------------------
228     void ClearBuffer( u32 mask, const ut::FloatColor& clearColor, f32 clearDepth, u8 clearStencil = 0 ) const;
229     void ClearBuffer( const ut::FloatColor& clearColor, f32 clearDepth, u8 clearStencil = 0 ) const
230     {
231         this->ClearBuffer( CLEAR_MASK_ALL, clearColor, clearDepth, clearStencil );
232     }
233 
234 private:
235     Description m_Description;
236 };
237 
238 } // namespace gfx
239 } // namespace nw
240 
241 
242 #endif // NW_GFX_ACTIVATE_COMMAND_H_
243