1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_GraphicsResource.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_GRAPHICSRESOURCE_H_
19 #define NW_LYT_GRAPHICSRESOURCE_H_
20 
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 
24 #include <nw/math/math_Types.h>
25 #include <nw/font/font_WideTextWriter.h>
26 #include <nw/ut/ut_Rect.h>
27 
28 #include <nw/lyt/lyt_Config.h>
29 #include <nw/lyt/lyt_Types.h>
30 
31 namespace nw
32 {
33 namespace lyt
34 {
35 
36 class Layout;
37 
38 //---------------------------------------------------------------------------
39 //! :category 描画
40 //!
41 //! @brief 複数の %Layout で共通に使用される %OpenGL のリソースを保持するクラスです。
42 //!
43 //! @details
44 //! DrawInfo::SetGraphicsResource() で DrawInfo に設定します。
45 //!
46 //! @since 2009/09/18 初版。
47 //---------------------------------------------------------------------------
48 class GraphicsResource
49 {
50 public:
51 #ifdef NW_LYT_DMPGL_ENABLED
52     //! :private
53     //! @brief キャッシュされた Uniform Location の ID です。
54     enum Uniform
55     {
56         UNIFORM_uProjection,
57         UNIFORM_uModelView,
58         UNIFORM_uTexMtx0,
59         UNIFORM_uTexMtx1,
60         UNIFORM_uTexMtx2,
61         UNIFORM_uColor,
62         UNIFORM_uTransform,
63         UNIFORM_uFrameSpec,
64         UNIFORM_uVertexColor,
65         UNIFORM_uVertexTexCoord0,
66         UNIFORM_uVertexTexCoord1,
67         UNIFORM_uVertexTexCoord2,
68         UNIFORM_uRcpTexSize0,
69         UNIFORM_dmp_Texture0_samplerType,
70         UNIFORM_dmp_Texture1_samplerType,
71         UNIFORM_dmp_Texture2_samplerType,
72         UNIFORM_dmp_Texture3_samplerType,
73         UNIFORM_dmp_TexEnv0_combineRgb,
74         UNIFORM_dmp_TexEnv1_combineRgb,
75         UNIFORM_dmp_TexEnv2_combineRgb,
76         UNIFORM_dmp_TexEnv3_combineRgb,
77         UNIFORM_dmp_TexEnv4_combineRgb,
78         UNIFORM_dmp_TexEnv5_combineRgb,
79         UNIFORM_dmp_TexEnv0_combineAlpha,
80         UNIFORM_dmp_TexEnv1_combineAlpha,
81         UNIFORM_dmp_TexEnv2_combineAlpha,
82         UNIFORM_dmp_TexEnv3_combineAlpha,
83         UNIFORM_dmp_TexEnv4_combineAlpha,
84         UNIFORM_dmp_TexEnv5_combineAlpha,
85         UNIFORM_dmp_TexEnv0_srcRgb,
86         UNIFORM_dmp_TexEnv1_srcRgb,
87         UNIFORM_dmp_TexEnv2_srcRgb,
88         UNIFORM_dmp_TexEnv3_srcRgb,
89         UNIFORM_dmp_TexEnv4_srcRgb,
90         UNIFORM_dmp_TexEnv5_srcRgb,
91         UNIFORM_dmp_TexEnv0_srcAlpha,
92         UNIFORM_dmp_TexEnv1_srcAlpha,
93         UNIFORM_dmp_TexEnv2_srcAlpha,
94         UNIFORM_dmp_TexEnv3_srcAlpha,
95         UNIFORM_dmp_TexEnv4_srcAlpha,
96         UNIFORM_dmp_TexEnv5_srcAlpha,
97         UNIFORM_dmp_TexEnv0_operandRgb,
98         UNIFORM_dmp_TexEnv1_operandRgb,
99         UNIFORM_dmp_TexEnv2_operandRgb,
100         UNIFORM_dmp_TexEnv3_operandRgb,
101         UNIFORM_dmp_TexEnv4_operandRgb,
102         UNIFORM_dmp_TexEnv5_operandRgb,
103         UNIFORM_dmp_TexEnv0_operandAlpha,
104         UNIFORM_dmp_TexEnv1_operandAlpha,
105         UNIFORM_dmp_TexEnv2_operandAlpha,
106         UNIFORM_dmp_TexEnv3_operandAlpha,
107         UNIFORM_dmp_TexEnv4_operandAlpha,
108         UNIFORM_dmp_TexEnv5_operandAlpha,
109         UNIFORM_dmp_TexEnv0_scaleRgb,
110         UNIFORM_dmp_TexEnv1_scaleRgb,
111         UNIFORM_dmp_TexEnv2_scaleRgb,
112         UNIFORM_dmp_TexEnv3_scaleRgb,
113         UNIFORM_dmp_TexEnv4_scaleRgb,
114         UNIFORM_dmp_TexEnv5_scaleRgb,
115         UNIFORM_dmp_TexEnv0_scaleAlpha,
116         UNIFORM_dmp_TexEnv1_scaleAlpha,
117         UNIFORM_dmp_TexEnv2_scaleAlpha,
118         UNIFORM_dmp_TexEnv3_scaleAlpha,
119         UNIFORM_dmp_TexEnv4_scaleAlpha,
120         UNIFORM_dmp_TexEnv5_scaleAlpha,
121         UNIFORM_dmp_TexEnv0_constRgba,
122         UNIFORM_dmp_TexEnv1_constRgba,
123         UNIFORM_dmp_TexEnv2_constRgba,
124         UNIFORM_dmp_TexEnv3_constRgba,
125         UNIFORM_dmp_TexEnv4_constRgba,
126         UNIFORM_dmp_TexEnv5_constRgba,
127         UNIFORM_dmp_TexEnv0_bufferColor,
128         UNIFORM_dmp_TexEnv1_bufferInput,
129         UNIFORM_dmp_TexEnv2_bufferInput,
130         UNIFORM_dmp_TexEnv3_bufferInput,
131         UNIFORM_dmp_TexEnv4_bufferInput,
132         UNIFORM_dmp_FragOperation_enableAlphaTest,
133         UNIFORM_dmp_FragOperation_alphaRefValue,
134         UNIFORM_dmp_FragOperation_alphaTestFunc,
135         UNIFORM_MAX
136     };
137 
138     //! :private
139     //! バッファオブジェクトのインデックスです。
140     enum VBO
141     {
142         VBO_ELEMENT,
143         VBO_VERTEX_INDEX,
144         VBO_MAX
145     };
146 #endif // NW_LYT_DMPGL_ENABLED
147 
148     //----------------------------------------
149     //! @name コンストラクタ/デストラクタ
150     //@{
151 
152     //! @brief コンストラクタです。
153     //!
154     //! @since 2009/09/18 初版。
155     //!
156     GraphicsResource();
157 
158     //! @brief デストラクタです。
159     //!
160     //! @since 2009/09/18 初版。
161     //!
162     ~GraphicsResource();
163 
164     //@}
165 
166     //----------------------------------------
167     //! @name 初期化/開放
168     //@{
169 
170     //! @brief 初期化を開始します。
171     //!
172     //! @details
173     //! この関数の呼び出し後に、GetResourcePath() と SetResource() を使ってリソース
174     //! ファイルを設定します。
175     //!
176     //! 最後に FinishSetup() を呼び出して初期化を完了します。
177     //!
178     //! @sa GetResourcePath
179     //! @sa SetResource
180     //! @sa FinishSetup
181     //!
182     //! @since 2009/09/18 初版。
183     //!
184     void StartSetup();
185 
186     //! @brief リソースファイルのパスを取得します。
187     //!
188     //! @param index リソースファイルのインデックス番号です(index >= 0)。
189     //!
190     //! @return
191     //! リソースファイルのパスを返します。
192     //! インデックスに対応するリソースがない場合には NULL を返します。
193     //!
194     //! @sa StartSetup
195     //! @sa GetResourceName
196     //!
197     //! @since 2009/09/18 初版。
198     //!
199     const wchar_t* GetResourcePath(int index);
200 
201     //! @brief リソースファイルを設定します。
202     //!
203     //! @details
204     //! index にはパスを取得した時のインデックスを指定します。
205     //!
206     //! bFree に true を指定した場合には、ライブラリは content の指す
207     //! メモリ領域を Layout::FreeMemory() で開放します。その場合、
208     //! content は Layout::AllocMemory() で割り当てられていなければなりません。
209     //!
210     //! @param index リソースファイルのインデックス番号です。
211     //! @param content リソースファイルの内容です。
212     //! @param fileSize リソースファイルのサイズです。
213     //! @param bFree content のメモリ削除をライブラリに任せます。
214     //!
215     //! @sa StartSetup
216     //! @sa Layout::AllocMemory
217     //!
218     //! @date 2010/04/23 bFree 引数を追加しました。
219     //! @since 2009/09/18 初版。
220     //!
221     void SetResource(int index, void* content, u32 fileSize, bool bFree = true);
222 
223     //! @brief 初期化を完了します。
224     //!
225     //! @return 初期化が正常に完了したら true を返します。
226     //!
227     //! @sa StartSetup
228     //!
229     //! @since 2009/09/18 初版。
230     //!
231     bool FinishSetup();
232 
233     //! @brief リソースを開放します。
234     //!
235     //! @details
236     //! デストラクタの呼び出し以前にリソースを開放しなければならない場合に
237     //! 使用します。
238     //!
239     //! @since 2009/09/18 初版。
240     //!
241     void Finalize();
242 
243     //! @brief 初期化が済んでいるか調べます。
244     //!
245     //! @return 初期化が済んでいれば true を返します。
246     //!
247     //! @since 2009/09/18 初版。
248     //!
Initialized()249     bool Initialized() const
250     {
251         return m_Initialized;
252     }
253 
254     //@}
255 
256 #ifdef NW_LYT_DMPGL_ENABLED
257     //! :private
258     //!
259     //! @brief ペインの描画に使用するプログラムオブジェクトを得ます。
260     //!
261     //! @return プログラムオブジェクトのハンドルを返します。
262     //!
263     //! @since 2009/09/18 初版。
264     //!
GetGlProgram()265     GLuint GetGlProgram() const
266     {
267         return m_GlProgram;
268     }
269 #endif
270 
271     //! :private
272     //!
273     //! @brief Drawer 用のシェーダーバイナリを取得します。
274     //!
GetRectShaderBinary()275     const void* GetRectShaderBinary() const
276     {
277         return m_pRectShaderBinary;
278     }
279 
280     //! :private
281     //!
282     //! @brief Drawer 用のシェーダーバイナリのサイズを取得します。
283     //!
GetRectShaderBinarySize()284     u32 GetRectShaderBinarySize() const
285     {
286         return m_RectShaderBinarySize;
287     }
288 
289 #ifdef NW_LYT_DMPGL_ENABLED
290     //! :private
291     //!
292     //! @brief デバッグ情報の描画に使用するプログラムオブジェクトを得ます。
293     //!
294     //! @return プログラムオブジェクトのハンドルを返します。
295     //!
296     //!
297     //! @since 2009/09/18 初版。
298     //!
GetGlProgramDebug()299     GLuint GetGlProgramDebug() const
300      {
301          return m_GlProgramDebug;
302      }
303 #endif
304 
305     //! :private
306     //!
307     //! @brief TextBox ペインの描画に使用する TextWriter を取得します。
308     //!
309     //! @return TextWriter への参照を返します。
310     //!
311     //! @since 2009/09/18 初版。
312     //!
GetTextWriter()313     font::WideTextWriter& GetTextWriter()
314     {
315         return m_TextWriter;
316     }
317 
318     //! :private
319     //!
320     //! @brief TextBox ペインの描画に使用する TextWriter を取得します。
321     //!
322     //! @return TextWriter への参照を返します。
323     //!
324     //! @since 2009/09/18 初版。
325     //!
GetTextWriter()326     const font::WideTextWriter& GetTextWriter() const
327     {
328         return m_TextWriter;
329     }
330 
331     //! :private
332     //!
333     //! @brief TextBox ペインの描画に使用する TextWriterResource を取得します。
334     //!
335     //! @return TextWriterResource への参照を返します。
336     //!
337     //! @since 2009/09/18 初版。
338     //!
GetTextWriterResource()339     font::TextWriterResource& GetTextWriterResource()
340     {
341          return m_TextWriterResource;
342     }
343 
344     //! :private
345     //!
346     //! @brief TextBox ペインの描画に使用する TextWriterResource を取得します。
347     //!
348     //! @return TextWriterResource への参照を返します。
349     //!
350     //! @since 2009/09/18 初版。
351     //!
GetTextWriterResource()352     const font::TextWriterResource& GetTextWriterResource() const
353     {
354         return m_TextWriterResource;
355     }
356 
357 #ifdef NW_LYT_DMPGL_ENABLED
358 
359     //! :private
360     //!
361     //! @brief モデルビュー行列を取得します。
362     //!
363     //! @return モデルビュー行列を返します。
364     //!
365     //! @since 2009/09/18 初版。
366     //!
GetMtxModelView()367     const math::MTX34&  GetMtxModelView() const
368     {
369         return m_MtxModelView;
370     }
371 
372     //! :private
373     //!
374     //! @brief モデルビュー行列を設定します。
375     //!
SetMtxModelView(const math::MTX34 m)376     void SetMtxModelView(const math::MTX34 m)
377     {
378         m_MtxModelView = m;
379         m_MtxModelViewLoaded = false;
380     }
381 
382     //! :private
383     //!
384     //! @brief テクスチャが使用するテクスチャ座標の番号を取得します。
385     //!
386     //! @param index テクスチャのインデックスです。
387     //!
388     //! @return 使用するテクスチャ座標の番号です。
389     //!
GetTexCoordSrc(int index)390     int GetTexCoordSrc(int index) const
391     {
392         return m_TexCoordSrc[index];
393     }
394 
395     //! :private
396     //!
397     //! @brief テクスチャが使用するテクスチャ座標の番号を設定します。
398     //!
399     //! @param index テクスチャのインデックスです。
400     //! @param src 使用するテクスチャ座標の番号です。
401     //!
SetTexCoordSrc(int index,int src)402     void SetTexCoordSrc(int index, int src)
403     {
404         m_TexCoordSrc[index] = src;
405     }
406 
407     //! :private
408     //!
409     //! @brief 射影行列を設定します。
410     //!
411     //! @param mtx 射影行列です。
412     //!
413     void SetProjectionMtx(const nw::math::MTX44& mtx);
414 
415     //! :private
416     //!
417     //! @brief キャッシュされた Uniform Location を取得します。
418     //!
419     //! @param uniformName  Uniform 列挙型の値を指定します。
420     //!
421     //! @return Uniform Location を返します。
422     //!
GetUniformLocation(int uniformName)423     GLint GetUniformLocation(int uniformName) const
424     {
425         return m_UniformLocation[uniformName];
426     }
427 
428     //! :private
429     //!
430     //! @brief 前回の描画で使用したTexEnvの段数を得ます。
431     //!
432     //! @return TexEnvの段数を返します。
433     //!
GetNumTexEnv()434     u32 GetNumTexEnv() const
435     {
436         return m_NumTexEnv;
437     }
438 
439     //! :private
440     //!
441     //! @brief 描画中の情報のキャッシュを初期化します。
442     //!
443     void ResetGlProgramState();
444 
445     //! :private
446     //!
447     //! @brief 描画中の情報のキャッシュを初期化します。
448     //!
449     void ResetGlState();
450 
451     //! :private
452     //!
453     //! @brief 描画で使用するTexEnvの段数を記録します。
454     //!
SetNumTexEnv(u32 numTexEnv)455     void SetNumTexEnv(u32 numTexEnv)
456     {
457         m_NumTexEnv = static_cast<u8>(numTexEnv);
458     }
459 
460     //! :private
461     //!
462     //! @brief 前回の描画でのTexEnvの設定が簡易設定によるものか調べます。
463     //!
464     //! @return 自動設定であれば true を返します。
465     //!
GetTexEnvAuto()466     bool GetTexEnvAuto() const
467     {
468         return m_TexEnvAuto;
469     }
470 
471     //! :private
472     //!
473     //! @brief 前回の描画で使用されたテクスチャ枚数を取得します。
474     //!
475     //! @return テクスチャ枚数を返します。
476     //!
GetNumTexMap()477     u8 GetNumTexMap() const
478     {
479         return m_NumTexMap;
480     }
481 
482     //! :private
483     //!
484     //! @brief TexEnvへの設定情報を記録します。
485     //!
486     //! @param value  簡易設定であれば true を指定します。
487     //! @param numTexMap  使用するテクスチャ枚数を指定します。
488     //!
489     void SetTexEnvAuto(bool value, u8 numTexMap = 0)
490     {
491         m_TexEnvAuto = value;
492         m_NumTexMap = numTexMap;
493     }
494 
495     //! :private
496     //!
497     //! @brief モデルビュー行列をシェーダーにロードします。
498     //!
LoadMtxModelView()499     void LoadMtxModelView()
500     {
501         if (!m_MtxModelViewLoaded)
502         {
503             m_MtxModelViewLoaded = true;
504             GLint loc = this->GetUniformLocation(this->UNIFORM_uModelView);
505             glUniform4fv(loc, 3, m_MtxModelView.a);
506         }
507     }
508 
509     //! :private
510     //!
511     //! @brief シェーダープログラムを設定します。
512     //!
513     //! @param program シェーダープログラムのIDです。
514     //!
UseProgram(GLuint program)515     void UseProgram(GLuint program)
516     {
517         glUseProgram(program);
518     }
519 
520 public:
521     //! :private
522     //!
523     //! @brief バッファオブジェクトを取得します。
524     //!
GetVBO(int index)525     GLuint GetVBO(int index) const
526     {
527         return m_GlVertexBufferObject[index];
528     }
529 
530     //! :private
531     //!
532     //! @brief プログラムオブジェクトを設定します。
533     //!
SetupProgram()534     void SetupProgram()
535     {
536         if (m_FirstDraw)
537         {
538             m_FirstDraw = false;
539             this->ActiveVBO();
540         }
541     }
542 
543 protected:
544     //! :private
545     //!
546     //! @brief 共通で利用するバッファオブジェクトを初期化します。
547     //!
548     void InitVBO();
549 
550     //! :private
551     //!
552     //! @brief 共通で利用するバッファオブジェクトを有効化します。
553     //!
554     void ActiveVBO();
555 
556 #endif // NW_LYT_DMPGL_ENABLED
557 
558 protected:
559     //! @details :private
560     void* m_pRectShaderBinary;
561 
562     //! @details :private
563     u32 m_RectShaderBinarySize;
564 
565     //! @details :private
566     font::TextWriterResource m_TextWriterResource;
567 
568     //! @details :private
569     font::WideTextWriter m_TextWriter;
570 #ifdef NW_LYT_DMPGL_ENABLED
571 
572     //! @details :private
573     GLuint m_GlProgram;
574 
575     //! @details :private
576     GLuint m_GlProgramDebug;
577 
578     //! @details :private
579     math::MTX34 m_MtxModelView;
580 
581     //! @details :private
582     s8 m_TexCoordSrc[TexMapMax];
583 
584     //! @details :private
585     u8 m_NumTexEnv;
586 
587     //! @details :private
588     u8 m_NumTexMap;
589 
590     //! @details :private
591     GLuint m_GlVertexBufferObject[VBO_MAX];
592 
593     //! @details :private
594     GLint m_UniformLocation[UNIFORM_MAX];
595 
596     //! @details :private
597     bool m_TexEnvAuto;
598 
599     //! @details :private
600     bool m_MtxModelViewLoaded;
601 
602     //! @details :private
603     bool m_FirstDraw;
604 #endif
605 
606     //! @details :private
607     bool m_Initialized;
608 };
609 
610 } // namespace nw::lyt
611 } // namespace nw
612 
613 #endif // NW_LYT_GRAPHICSRESOURCE_H_
614 
615