PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/MTA10/core/CRenderItem.Shader.cpp

http://mtasa-blue.googlecode.com/
C++ | 292 lines | 141 code | 38 blank | 113 comment | 20 complexity | 85210c6843cb8ca269e6de91001b62e6 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, AGPL-3.0, Unlicense, GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. /*****************************************************************************
  2. *
  3. * PROJECT: Multi Theft Auto v1.0
  4. * LICENSE: See LICENSE in the top level directory
  5. * FILE: core/CRenderItem.Shader.cpp
  6. * PURPOSE:
  7. * DEVELOPERS: xidiot
  8. *
  9. *****************************************************************************/
  10. #include "StdInc.h"
  11. ////////////////////////////////////////////////////////////////
  12. //
  13. // CShaderItem::PostConstruct
  14. //
  15. //
  16. //
  17. ////////////////////////////////////////////////////////////////
  18. void CShaderItem::PostConstruct ( CRenderItemManager* pManager, const SString& strFilename, const SString& strRootPath, SString& strOutStatus, float fPriority, float fMaxDistance, bool bDebug )
  19. {
  20. m_fPriority = fPriority;
  21. m_fMaxDistanceSq = fMaxDistance * fMaxDistance;
  22. Super::PostConstruct ( pManager );
  23. // Initial creation of d3d data
  24. CreateUnderlyingData ( strFilename, strRootPath, strOutStatus, bDebug );
  25. }
  26. ////////////////////////////////////////////////////////////////
  27. //
  28. // CShaderItem::PreDestruct
  29. //
  30. //
  31. //
  32. ////////////////////////////////////////////////////////////////
  33. void CShaderItem::PreDestruct ( void )
  34. {
  35. ReleaseUnderlyingData ();
  36. Super::PreDestruct ();
  37. }
  38. ////////////////////////////////////////////////////////////////
  39. //
  40. // CShaderItem::IsValid
  41. //
  42. // Check underlying data is present
  43. //
  44. ////////////////////////////////////////////////////////////////
  45. bool CShaderItem::IsValid ( void )
  46. {
  47. return m_pEffectWrap && m_pEffectWrap->m_pD3DEffect;
  48. }
  49. ////////////////////////////////////////////////////////////////
  50. //
  51. // CShaderItem::OnLostDevice
  52. //
  53. // Release device stuff
  54. //
  55. ////////////////////////////////////////////////////////////////
  56. void CShaderItem::OnLostDevice ( void )
  57. {
  58. // Nothing required for CShaderItem
  59. }
  60. ////////////////////////////////////////////////////////////////
  61. //
  62. // CShaderItem::OnResetDevice
  63. //
  64. // Recreate device stuff
  65. //
  66. ////////////////////////////////////////////////////////////////
  67. void CShaderItem::OnResetDevice ( void )
  68. {
  69. // Nothing required for CShaderItem
  70. }
  71. ////////////////////////////////////////////////////////////////
  72. //
  73. // CShaderItem::CreateUnderlyingData
  74. //
  75. //
  76. //
  77. ////////////////////////////////////////////////////////////////
  78. void CShaderItem::CreateUnderlyingData ( const SString& strFilename, const SString& strRootPath, SString& strOutStatus, bool bDebug )
  79. {
  80. assert ( !m_pEffectWrap );
  81. assert ( !m_pShaderInstance );
  82. m_pEffectWrap = NewEffectWrap ( m_pManager, strFilename, strRootPath, strOutStatus, bDebug );
  83. if ( !m_pEffectWrap->IsValid () )
  84. {
  85. SAFE_RELEASE( m_pEffectWrap );
  86. return;
  87. }
  88. m_pEffectWrap->ReadParameterHandles ();
  89. struct
  90. {
  91. D3DXHANDLE& hHandle;
  92. SString strNames;
  93. } handleNames [] = {
  94. m_pEffectWrap->hWorld, "WORLD",
  95. m_pEffectWrap->hView, "VIEW",
  96. m_pEffectWrap->hProjection, "PROJECTION",
  97. m_pEffectWrap->hWorldView, "WORLDVIEW",
  98. m_pEffectWrap->hWorldViewProj, "WORLDVIEWPROJECTION",
  99. m_pEffectWrap->hViewProj, "VIEWPROJECTION",
  100. m_pEffectWrap->hViewInv, "VIEWINVERSE",
  101. m_pEffectWrap->hWorldInvTr, "WORLDINVERSETRANSPOSE",
  102. m_pEffectWrap->hViewInvTr, "VIEWINVERSETRANSPOSE",
  103. m_pEffectWrap->hCamPos, "CAMERAPOSITION",
  104. m_pEffectWrap->hCamDir, "CAMERADIRECTION",
  105. m_pEffectWrap->hTime, "TIME",
  106. m_pEffectWrap->hLightAmbient, "LIGHTAMBIENT",
  107. m_pEffectWrap->hLightDiffuse, "LIGHTDIFFUSE",
  108. m_pEffectWrap->hLightSpecular, "LIGHTSPECULAR",
  109. m_pEffectWrap->hLightDirection, "LIGHTDIRECTION",
  110. };
  111. for ( uint h = 0 ; h < NUMELMS( handleNames ) ; h++ )
  112. {
  113. std::vector < SString > parts;
  114. handleNames[h].strNames.Split( ",", parts );
  115. D3DXHANDLE* phHandle = NULL;
  116. for ( uint n = 0 ; n < parts.size () && phHandle == NULL ; n++ )
  117. {
  118. phHandle = MapFind ( m_pEffectWrap->m_valueHandleMap, parts[n] );
  119. if ( !phHandle )
  120. phHandle = MapFind ( m_pEffectWrap->m_texureHandleMap, parts[n] );
  121. }
  122. handleNames[h].hHandle = phHandle ? *phHandle : NULL;
  123. }
  124. // Create instance to store param values
  125. RenewShaderInstance ();
  126. }
  127. ////////////////////////////////////////////////////////////////
  128. //
  129. // CShaderItem::ReleaseUnderlyingData
  130. //
  131. //
  132. //
  133. ////////////////////////////////////////////////////////////////
  134. void CShaderItem::ReleaseUnderlyingData ( void )
  135. {
  136. SAFE_RELEASE( m_pEffectWrap )
  137. SAFE_RELEASE( m_pShaderInstance );
  138. }
  139. ////////////////////////////////////////////////////////////////
  140. //
  141. // CShaderItem::SetValue
  142. //
  143. // Set one texture
  144. //
  145. ////////////////////////////////////////////////////////////////
  146. bool CShaderItem::SetValue ( const SString& strName, CTextureItem* pTextureItem )
  147. {
  148. if ( D3DXHANDLE* phParameter = MapFind ( m_pEffectWrap->m_texureHandleMap, strName.ToUpper () ) )
  149. {
  150. // Check if value is changing
  151. if ( !m_pShaderInstance->CmpTextureValue( *phParameter, pTextureItem ) )
  152. {
  153. // Check if we need a new shader instance
  154. MaybeRenewShaderInstance ();
  155. if ( *phParameter == m_pEffectWrap->m_hFirstTexture )
  156. {
  157. // Mirror size of first texture declared in effect file
  158. m_uiSizeX = pTextureItem->m_uiSizeX;
  159. m_uiSizeY = pTextureItem->m_uiSizeY;
  160. m_pShaderInstance->m_uiSizeX = m_uiSizeX;
  161. m_pShaderInstance->m_uiSizeY = m_uiSizeY;
  162. }
  163. m_pShaderInstance->SetTextureValue( *phParameter, pTextureItem );
  164. }
  165. return true;
  166. }
  167. return false;
  168. }
  169. ////////////////////////////////////////////////////////////////
  170. //
  171. // CShaderItem::SetValue
  172. //
  173. // Set one bool
  174. //
  175. ////////////////////////////////////////////////////////////////
  176. bool CShaderItem::SetValue ( const SString& strName, bool bValue )
  177. {
  178. if ( D3DXHANDLE* phParameter = MapFind ( m_pEffectWrap->m_valueHandleMap, strName.ToUpper () ) )
  179. {
  180. // Check if value is changing
  181. if ( !m_pShaderInstance->CmpBoolValue( *phParameter, bValue ) )
  182. {
  183. // Check if we need a new shader instance
  184. MaybeRenewShaderInstance ();
  185. m_pShaderInstance->SetBoolValue( *phParameter, bValue );
  186. }
  187. return true;
  188. }
  189. return false;
  190. }
  191. ////////////////////////////////////////////////////////////////
  192. //
  193. // CShaderItem::SetValue
  194. //
  195. // Set up to 16 floats
  196. //
  197. ////////////////////////////////////////////////////////////////
  198. bool CShaderItem::SetValue ( const SString& strName, const float* pfValues, uint uiCount )
  199. {
  200. if ( D3DXHANDLE* phParameter = MapFind ( m_pEffectWrap->m_valueHandleMap, strName.ToUpper () ) )
  201. {
  202. // Check if value is changing
  203. if ( !m_pShaderInstance->CmpFloatsValue( *phParameter, pfValues, uiCount ) )
  204. {
  205. // Check if we need a new shader instance
  206. MaybeRenewShaderInstance ();
  207. m_pShaderInstance->SetFloatsValue( *phParameter, pfValues, uiCount );
  208. }
  209. return true;
  210. }
  211. return false;
  212. }
  213. ////////////////////////////////////////////////////////////////
  214. //
  215. // CShaderItem::SetTessellation
  216. //
  217. //
  218. //
  219. ////////////////////////////////////////////////////////////////
  220. void CShaderItem::SetTessellation ( uint uiTessellationX, uint uiTessellationY )
  221. {
  222. // Check if value is changing
  223. if ( uiTessellationX != m_pShaderInstance->m_uiTessellationX || uiTessellationY != m_pShaderInstance->m_uiTessellationY )
  224. {
  225. // Check if we need a new shader instance
  226. MaybeRenewShaderInstance ();
  227. m_pShaderInstance->m_uiTessellationX = uiTessellationX;
  228. m_pShaderInstance->m_uiTessellationY = uiTessellationY;
  229. }
  230. }
  231. ////////////////////////////////////////////////////////////////
  232. //
  233. // CShaderItem::MaybeRenewShaderInstance
  234. //
  235. // If current instance is in use by something else (i.e. in draw queue), we must create a new instance before changing parameter values
  236. //
  237. ////////////////////////////////////////////////////////////////
  238. void CShaderItem::MaybeRenewShaderInstance ( void )
  239. {
  240. if ( m_pShaderInstance->m_iRefCount > 1 )
  241. RenewShaderInstance ();
  242. }
  243. ////////////////////////////////////////////////////////////////
  244. //
  245. // CShaderItem::RenewShaderInstance
  246. //
  247. // Create/clone a new instance
  248. //
  249. ////////////////////////////////////////////////////////////////
  250. void CShaderItem::RenewShaderInstance ( void )
  251. {
  252. CShaderInstance* pShaderInstance = new CShaderInstance ();
  253. pShaderInstance->PostConstruct ( m_pManager, this );
  254. }