AutoRender.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. const int AUTO_RENDER_STACK_SIZE = 256 * 1024;
  24. idAutoRender rAutoRender;
  25. /*
  26. ============================
  27. idAutoRender::idAutoRender
  28. ============================
  29. */
  30. idAutoRender::idAutoRender() {
  31. nextRotateTime = 0.0f;
  32. currentRotation = 0.0f;
  33. autoRenderIcon = AUTORENDER_DEFAULTICON;
  34. }
  35. /*
  36. ============================
  37. idAutoRender::Run
  38. ============================
  39. */
  40. int idAutoRender::Run() {
  41. while ( !IsTerminating() ) {
  42. RenderFrame();
  43. }
  44. return 0;
  45. }
  46. /*
  47. ============================
  48. idAutoRender::StartBackgroundAutoSwaps
  49. ============================
  50. */
  51. void idAutoRender::StartBackgroundAutoSwaps( autoRenderIconType_t iconType ) {
  52. if ( IsRunning() ) {
  53. EndBackgroundAutoSwaps();
  54. }
  55. autoRenderIcon = iconType;
  56. idLib::Printf("Starting Background AutoSwaps\n");
  57. const bool captureToImage = true;
  58. common->UpdateScreen( captureToImage );
  59. // unbind any shaders prior to entering the background autoswaps so we don't run
  60. // into any problems with cached vertex shader indices from the main thread
  61. renderProgManager.Unbind();
  62. // unbind all texture units so we don't run into a race condition where the device is owned
  63. // by the autorender thread but an image is trying to be unset from the main thread because
  64. // it is getting purged before our our first frame has been rendered.
  65. globalImages->UnbindAll();
  66. StartThread("BackgroundAutoSwaps", CORE_0B, THREAD_NORMAL, AUTO_RENDER_STACK_SIZE );
  67. }
  68. /*
  69. ============================
  70. idAutoRender::EndBackgroundAutoSwaps
  71. ============================
  72. */
  73. void idAutoRender::EndBackgroundAutoSwaps() {
  74. idLib::Printf("End Background AutoSwaps\n");
  75. StopThread();
  76. }
  77. /*
  78. ============================
  79. idAutoRender::RenderFrame
  80. ============================
  81. */
  82. void idAutoRender::RenderFrame() {
  83. // values are 0 to 1
  84. float loadingIconPosX = 0.5f;
  85. float loadingIconPosY = 0.6f;
  86. float loadingIconScale = 0.025f;
  87. float loadingIconSpeed = 0.095f;
  88. if ( autoRenderIcon == AUTORENDER_HELLICON ) {
  89. loadingIconPosX = 0.85f;
  90. loadingIconPosY = 0.85f;
  91. loadingIconScale = 0.1f;
  92. loadingIconSpeed = 0.095f;
  93. } else if ( autoRenderIcon == AUTORENDER_DIALOGICON ) {
  94. loadingIconPosY = 0.73f;
  95. }
  96. GL_SetDefaultState();
  97. GL_Cull( CT_TWO_SIDED );
  98. const bool stereoRender = false;
  99. const int width = renderSystem->GetWidth();
  100. const int height = renderSystem->GetHeight();
  101. const int guardBand = height / 24;
  102. if ( stereoRender ) {
  103. for ( int viewNum = 0 ; viewNum < 2; viewNum++ ) {
  104. GL_ViewportAndScissor( 0, viewNum * ( height + guardBand ), width, height );
  105. RenderBackground();
  106. RenderLoadingIcon( loadingIconPosX, loadingIconPosY, loadingIconScale, loadingIconSpeed );
  107. }
  108. } else {
  109. GL_ViewportAndScissor( 0, 0, width, height );
  110. RenderBackground();
  111. RenderLoadingIcon( loadingIconPosX, loadingIconPosY, loadingIconScale, loadingIconSpeed );
  112. }
  113. }
  114. /*
  115. ============================
  116. idAutoRender::RenderBackground
  117. ============================
  118. */
  119. void idAutoRender::RenderBackground() {
  120. GL_SelectTexture( 0 );
  121. globalImages->currentRenderImage->Bind();
  122. GL_State( GLS_DEPTHFUNC_ALWAYS | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO );
  123. float mvpMatrix[16] = { 0 };
  124. mvpMatrix[0] = 1;
  125. mvpMatrix[5] = 1;
  126. mvpMatrix[10] = 1;
  127. mvpMatrix[15] = 1;
  128. // Set Parms
  129. float texS[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
  130. float texT[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
  131. renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_S, texS );
  132. renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_T, texT );
  133. // disable texgen
  134. float texGenEnabled[4] = { 0, 0, 0, 0 };
  135. renderProgManager.SetRenderParm( RENDERPARM_TEXGEN_0_ENABLED, texGenEnabled );
  136. // set matrix
  137. renderProgManager.SetRenderParms( RENDERPARM_MVPMATRIX_X, mvpMatrix, 4 );
  138. renderProgManager.BindShader_TextureVertexColor();
  139. RB_DrawElementsWithCounters( &backEnd.unitSquareSurface );
  140. }
  141. /*
  142. ============================
  143. idAutoRender::RenderLoadingIcon
  144. ============================
  145. */
  146. void idAutoRender::RenderLoadingIcon( float fracX, float fracY, float size, float speed ) {
  147. float s = 0.0f;
  148. float c = 1.0f;
  149. if ( autoRenderIcon != AUTORENDER_HELLICON ) {
  150. if ( Sys_Milliseconds() >= nextRotateTime ) {
  151. nextRotateTime = Sys_Milliseconds() + 100;
  152. currentRotation -= 90.0f;
  153. }
  154. float angle = DEG2RAD( currentRotation );
  155. idMath::SinCos( angle, s, c );
  156. }
  157. const float pixelAspect = renderSystem->GetPixelAspect();
  158. const float screenWidth = renderSystem->GetWidth();
  159. const float screenHeight = renderSystem->GetHeight();
  160. const float minSize = Min( screenWidth, screenHeight );
  161. if ( minSize <= 0.0f ) {
  162. return;
  163. }
  164. float scaleX = size * minSize / screenWidth;
  165. float scaleY = size * minSize / screenHeight;
  166. float scale[16] = { 0 };
  167. scale[0] = c * scaleX / pixelAspect;
  168. scale[1] = -s * scaleY;
  169. scale[4] = s * scaleX / pixelAspect;
  170. scale[5] = c * scaleY;
  171. scale[10] = 1.0f;
  172. scale[15] = 1.0f;
  173. scale[12] = fracX;
  174. scale[13] = fracY;
  175. float ortho[16] = { 0 };
  176. ortho[0] = 2.0f;
  177. ortho[5] = -2.0f;
  178. ortho[10] = -2.0f;
  179. ortho[12] = -1.0f;
  180. ortho[13] = 1.0f;
  181. ortho[14] = -1.0f;
  182. ortho[15] = 1.0f;
  183. float finalOrtho[16];
  184. R_MatrixMultiply( scale, ortho, finalOrtho );
  185. float projMatrixTranspose[16];
  186. R_MatrixTranspose( finalOrtho, projMatrixTranspose );
  187. renderProgManager.SetRenderParms( RENDERPARM_MVPMATRIX_X, projMatrixTranspose, 4 );
  188. float a = 1.0f;
  189. if ( autoRenderIcon == AUTORENDER_HELLICON ) {
  190. float alpha = DEG2RAD( Sys_Milliseconds() * speed );
  191. a = idMath::Sin( alpha );
  192. a = 0.35f + ( 0.65f * idMath::Fabs( a ) );
  193. }
  194. GL_SelectTexture( 0 );
  195. if ( autoRenderIcon == AUTORENDER_HELLICON ) {
  196. globalImages->hellLoadingIconImage->Bind();
  197. } else {
  198. globalImages->loadingIconImage->Bind();
  199. }
  200. GL_State( GLS_DEPTHFUNC_ALWAYS | GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
  201. // Set Parms
  202. float texS[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
  203. float texT[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
  204. renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_S, texS );
  205. renderProgManager.SetRenderParm( RENDERPARM_TEXTUREMATRIX_T, texT );
  206. if ( autoRenderIcon == AUTORENDER_HELLICON ) {
  207. GL_Color( 1.0f, 1.0f, 1.0f, a );
  208. }
  209. // disable texgen
  210. float texGenEnabled[4] = { 0, 0, 0, 0 };
  211. renderProgManager.SetRenderParm( RENDERPARM_TEXGEN_0_ENABLED, texGenEnabled );
  212. renderProgManager.BindShader_TextureVertexColor();
  213. RB_DrawElementsWithCounters( &backEnd.unitSquareSurface );
  214. }