1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_Drawer.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_LYT_DRAWER_H_
19 #define NW_LYT_DRAWER_H_
20 
21 // 高速化実験を有効にする
22 #ifdef NW_PLATFORM_CTR
23 #define NW_LYT_DRAWER_ENABLE 1
24 #endif
25 
26 #include <nw/font/font_RectDrawer.h>
27 
28 #ifdef NW_LYT_DRAWER_ENABLE
29 //--------------------------------------
30 // 動作設定
31 
32 // ASSERT
33 #if 1
34 #define NW_LYT_DRAWER_GL_ASSERT()      NW_GL_ASSERT()
35 #define NW_LYT_DRAWER_ASSERT( x )      NW_ASSERT( x )
36 #define NW_LYT_DRAWER_NULL_ASSERT( x ) NW_NULL_ASSERT( x )
37 #else
38 #define NW_LYT_DRAWER_ASSERT( x )
39 #define NW_LYT_DRAWER_GL_ASSERT()
40 #define NW_LYT_DRAWER_NULL_ASSERT( x )
41 #endif
42 
43 // 必要のない機能であれば削れば速いかも
44 #define NW_LYT_DRAWER_ENABLE_ALPHA_TEST   1
45 #define NW_LYT_DRAWER_ENABLE_BLEND_FUNC   1
46 #define NW_LYT_DRAWER_ENABLE_USER_TEX_ENV 1
47 
48 //----------------------------------------------------------
49 // マクロ
50 
51 // nngxValidateState
52 #ifdef NW_FONT_RECTDRAWER_USE_DRAW_ARRAYS
53 #define NW_LYT_DRAWER_VALIDATE( state_mask ) \
54     nngxValidateState( state_mask, GL_FALSE )
55 #else
56 #define NW_LYT_DRAWER_VALIDATE( state_mask ) \
57     nngxValidateState( state_mask, GL_TRUE )
58 #endif
59 
60 #if 0
61 #define NW_LYT_DRAWER_ALIGN_32 __attribute__((aligned(32)))
62 #else
63 #define NW_LYT_DRAWER_ALIGN_32
64 #endif
65 
66 //----------------------------------------------------------
67 // 実験用
68 
69 // PictureのDrawを切る
70 // #define NW_LYT_DRAWER_DISABLE_DRAW_PICTURE           1
71 
72 // TextBoxのDrawを切る
73 // #define NW_LYT_DRAWER_DISABLE_DRAW_TEXTBOX           1
74 
75 // WindowのDrawを切る
76 // #define NW_LYT_DRAWER_DISABLE_DRAW_WINOOW            1
77 
78 // UniformDataのPRINT
79 // #define NW_LYT_DRAWER_PRINT_UNIFORM_DATA             1
80 
81 
82 //----------------------------------------------------------
83 
84 #include <GLES2/gl2.h>
85 #include <GLES2/gl2ext.h>
86 #include <nw/lyt/lyt_Types.h>
87 #include <nw/ut/ut_Color.h>
88 
89 namespace nw { namespace lyt {
90 
91 class Layout;
92 class Pane;
93 class Material;
94 class DrawInfo;
95 class Material;
96 class TexMap;
97 class TextBox;
98 class GraphicsResource;
99 
100 //---------------------------------------------------------------------------
101 //! :category 描画
102 //!
103 //! @brief 高速描画クラスです。
104 //!
105 //! @details
106 //! レイアウトを高速に描画します。
107 //!
108 //! Layout::Draw() の代わりに使用します。
109 //!
110 //!  nw::lyt::Drawer drawer(graphicsResource);\n
111 //!  \n
112 //!  pLayout->Animate();\n
113 //!  pLayout->CalculateMtx(drawInfo);\n
114 //!  \n
115 //!  // ユーザの行うGL設定。\n
116 //!  InitGX();\n
117 //!  \n
118 //!  drawer.DrawBegin(drawInfo);\n
119 //!  drawer.Draw(pLayout, drawInfo);\n
120 //!  drawer.DrawEnd(drawInfo);\n
121 //!  \n
122 //!  // DMPGLステートの一貫性を回復します。\n
123 //!  nngxUpdateState(NN_GX_STATE_ALL);
124 //!
125 //! GLの以下の設定はユーザに任せています(lytでは設定しません)。
126 //!
127 //! ・カリング\n
128 //! ・シザー処理\n
129 //! ・ステンシルテスト\n
130 //! ・マスク処理\n
131 //! ・ポリゴンオフセット\n
132 //! ・デプステスト\n
133 //! ・アーリーデプステスト\n
134 //! ・フレームバッファオブジェクト
135 //!
136 //! 設定方法については simple デモのソースを確認してください
137 //! (Drawer は通常のGL関数を使わないため、設定方法が複雑です)。
138 //!
139 //! Drawer の描画関数を使用すると DMPGL ステートが一貫性の無い状態になります。
140 //!
141 //! Drawerの使用後に GL の関数を使用する前には nngxUpdateState() 呼び出しを行ってください。
142 //!
143 //! @since 2010/05/14 初版。DrawManagerを置き換えました。
144 //! @date 2010/09/24 Drawerの初期化後のnngxUpdateState()呼び出しは必要なくなりました。
145 //!
146 //---------------------------------------------------------------------------
147 class Drawer : public font::RectDrawer
148 {
149     typedef RectDrawer  Base;
150 
151 public:
152 
153     //----------------------------------------
154     //! @name コンストラクタ/デストラクタ
155     //@{
156 
157     //! @brief コンストラクタです。
158     //!
159     //! @details
160     //! Initialize() による初期化が必要です。
161     //!
162     //! @sa Initialize
163     //!
164     //! @since 2010/05/14 初版。
165     //!
166     Drawer();
167 
168     //! @brief コンストラクタです。
169     //!
170     //! @details
171     //! オブジェクトの構築と Initialize() の呼び出しを行います。
172     //!
173     //! @param graphicsResource  GraphicsResource を指定します。
174     //!
175     //! @since 2010/04/12 初版。
176     //!
177     explicit Drawer( GraphicsResource& graphicsResource );
178 
179     //@}
180 
181     //----------------------------------------
182     //! @name 初期化/後処理
183     //@{
184 
185     //! @brief 初期化を行います。
186     //!
187     //! @param graphicsResource GraphicsResource を指定します。
188     //! @param vertexBuffer  頂点用バッファ。
189     //!
190     //! @sa Finalize
191     //!
192     //! @since 2010/05/14 初版。
193     //! @date 2010/09/24 nngxUpdateState()の呼び出しは必要なくなりました。
194     //!
195     void Initialize( GraphicsResource& graphicsResource,
196                      void* vertexBuffer = NULL );
197 
198 protected:
199     //! @details :private
Initialize(void * vtxBufCmdBuf,const void * shaderBinary,u32 size)200     void Initialize( void*       vtxBufCmdBuf,
201                      const void* shaderBinary,
202                      u32         size
203     )
204     {
205         Base::Initialize(vtxBufCmdBuf, shaderBinary, size);
206     }
207 
208     //! @details :private
Initialize(void * vertexBuffer,void * commandBuffer,const void * shaderBinary,u32 size)209     void Initialize( void*       vertexBuffer,
210                      void*       commandBuffer,
211                      const void* shaderBinary,
212                      u32         size
213     )
214     {
215         Base::Initialize(vertexBuffer, commandBuffer, shaderBinary, size);
216     }
217 
218 public:
219     //! @brief      開放を行います。
220     //!
221     //! @sa         Initialize
222     //!
223     //! @since      2010/06/02 初版。
224     //!
225     virtual void        Finalize();
226 
227     //@}
228 
229 public:
230     //----------------------------------------
231     //! @name 描画
232     //@{
233 
234     //! @brief 描画を開始します。
235     //!
236     //! @param drawInfo DrawInfo を指定します。
237     //!
238     //! @sa Draw
239     //! @sa DrawEnd
240     //!
241     //! @details
242     //! 本関数を使用すると DMPGL ステートが一貫性の無い状態になります。
243     //! 以後に GL 関数を使用する場合は nngxUpdateState()
244     //! 呼び出しを行ってください。
245     //!
246     //! @since 2010/05/14 初版。StartCmdList より改名しました。
247     //! @date 2010/09/30 nngxUpdateState() 呼び出しについて加筆しました。
248     //!
249     void DrawBegin( const DrawInfo& drawInfo );
250 
251     //! @details :private
252     virtual void DrawBegin(u32 flag = 0);
253 
254     //! @brief レイアウトを描画します。
255     //!
256     //! @details
257     //! DrawBegin(), DrawEnd() の間で複数回の Draw() を行うことができます。
258     //!
259     //! 本関数を使用すると DMPGL ステートが一貫性の無い状態になります。
260     //! 以後に GL 関数を使用する場合は nngxUpdateState()
261     //! 呼び出しを行ってください。
262     //!
263     //! @param pLayout Layout を指定します。
264     //! @param drawInfo DrawInfo を指定します。
265     //!
266     //! @sa DrawBegin
267     //! @sa DrawEnd
268     //!
269     //! @since 2010/05/14 初版。AddCmdList より改名しました。
270     //! @date 2010/09/30 nngxUpdateState() 呼び出しについて加筆しました。
271     //!
272     void Draw( const Layout* pLayout, DrawInfo& drawInfo );
273 
274     //! @brief ペインを描画します。
275     //!
276     //! @details
277     //! 指定したペインおよびその子階層のペインを描画します。
278     //!
279     //! DrawBegin(), DrawEnd() の間で複数回の Draw() を行うことができます。
280     //!
281     //! 本関数を使用すると DMPGL ステートが一貫性の無い状態になります。
282     //! 以後に GL 関数を使用する場合は nngxUpdateState()
283     //! 呼び出しを行ってください。
284     //!
285     //! @param pPane Pane を指定します。
286     //! @param drawInfo DrawInfo を指定します。
287     //!
288     //! @sa DrawBegin
289     //! @sa DrawEnd
290     //!
291     //! @since 2010/05/14 初版。AddCmdList より改名しました。
292     //! @date 2010/09/30 nngxUpdateState() 呼び出しについて加筆しました。
293     //!
294     void Draw( const Pane* pPane, DrawInfo& drawInfo );
295 
296     //! @brief 描画を終了します。
297     //!
298     //! @param drawInfo DrawInfo を指定します。
299     //!
300     //! @details
301     //! 本関数を使用すると DMPGL ステートが一貫性の無い状態になります。
302     //! 以後に GL 関数を使用する場合は nngxUpdateState()
303     //! 呼び出しを行ってください。
304     //!
305     //! @sa DrawBegin
306     //! @sa Draw
307     //!
308     //! @since 2010/05/14 初版。UseCmdList より改名しました。
309     //! @date 2010/09/30 nngxUpdateState() 呼び出しについて加筆しました。
310     //!
DrawEnd(const DrawInfo & drawInfo)311     void DrawEnd( const DrawInfo& drawInfo )
312     {
313         NW_UNUSED_VARIABLE(drawInfo);
314         u32 flag = 0;
315         DrawEnd(flag);
316     }
317 
318     //! @details :private
319     virtual void DrawEnd(u32 flag = 0);
320 
321     //@}
322 
323 public:
324 
325     //! @details :private
326     void DumpCmdList();
327 
328     //! @details :private
329     void SetUpTexEnv( const Material* pMaterial );
330 
331     //! @details :private
332     void SetUpTextures( const Material* pMaterial, bool addDisableCommand = true);
333 
334     //! @details :private
335     void SetUpTextureCoords( const nw::math::VEC4 texCoords[ TexMapMax * 2 ],
336                              const int texCoordNum );
337 
338     //! @details :private
339     void SetUpVtxColors( const nw::ut::Color8 vtxColors[VERTEX_MAX],
340                          const u8 globalAlpha );
341 
342     //! @details :private
343     void SetUpTextBox( const TextBox* pTextBox,
344                        const Material* pMaterial,
345                        const DrawInfo& drawInfo );
346 
347     //! @details :private
348     void SetUpMtx( const nw::math::MTX34& mtx );
349 
350     //! @details :private
351     void SetUpQuad( const nw::lyt::Size& size,
352                     const nw::math::VEC2& basePt );
353 
354     //! @details :private
355     void SetUniformDataEnd();
356 
357     //! @details :private
358     void SetCurrentUniformAlpha( const f32 alpha );
359 
360     //! @details :private
361     u8 CalcTextureCoords( const Material* pMaterial,
362                           const TexCoordQuad* pTexCoordQuad,
363                           nw::math::VEC4 texCoords[ TexMapMax * 2 ] ) const;
364 
365     //! @brief      バッファの内容を書き出します。
366     //!
FlushBuffer()367     void FlushBuffer()
368     {
369 #ifdef NW_FONT_RECTDRAWER_USE_DRAW_BUFFER
370         if (m_UniformAddrIndex > 0)
371         {
372             UniformAndDraw();
373         }
374 #endif
375     }
376 
377 protected:
378     //! @details :private
379     void UniformAndDraw();
380 
381     //! @details :private
382     void SetUpTexEnvType2( const Material* pMaterial );
383 
384     //! @details :private
385     void SetUpTexEnvType3( const Material* pMaterial );
386 
387 #ifdef NW_LYT_DRAWER_ENABLE_USER_TEX_ENV
388     //! @details :private
389     void SetUpGLTexEnvUser( const Material* pMaterial );
390 #endif
391 
392 #ifdef NW_LYT_DRAWER_ENABLE_BLEND_FUNC
393     //! @details :private
394     void SetUpBlendMode(   const Material* pMaterial );
395 #endif
396 
397 #ifdef NW_LYT_DRAWER_ENABLE_ALPHA_TEST
398     //! @details :private
399     void SetUpAlphaTest(   const Material* pMaterial );
400 #endif
401 
402 protected: ////////////////////////////// Command
403     //! @details :private
404     GLuint m_PrevTexObj;
405 
406     //! @details :private
407     int m_ActiveTexureNum;
408 
409     //! @details :private
410     int m_TexCoordNum;
411 
412     //! @details :private
413     bool m_IsBlendDefault;
414 
415     //! @details :private
416     bool m_AlphaTestEnable;
417 
418 protected: ////////////////////////////// Texture Combiner
419 
420     //! @details :private
421     enum TEX_ENV_TYPE
422     {
423         TEX_ENV_TYPE_0_TEX,
424         TEX_ENV_TYPE_1_TEX,
425         TEX_ENV_TYPE_2_TEX,
426         TEX_ENV_TYPE_3_TEX,
427         TEX_ENV_TYPE_TEXT,
428         TEX_ENV_TYPE_USER,
429         TEX_ENV_TYPE_NUM
430     };
431 
432     //! @details :private
433     TEX_ENV_TYPE m_CurrentTexEnvType;
434 
435 #if ! defined(NW_RELEASE)
436     //! @details :private
437     TEX_ENV_TYPE m_PreviousTexEnvType;
438 #endif
439 
440     //! @details :private
441     enum
442     {
443         TEX_ENV_UNIFORM_NUM = 8,
444 #ifdef NW_TARGET_CTR_GL_FINAL
445         TEX_ENV_STAGE_NUM = 6,
446 #else
447         TEX_ENV_STAGE_NUM = 3,
448 #endif
449         TEX_ENV_UNIFORM_VALUE_NUM = 16
450     };
451 };
452 
453 }}
454 #endif  // #ifdef NW_LYT_DRAWER_ENABLE
455 
456 #endif
457