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