Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cmaterial_queuefriendly.h"
  9. #include "tier1/callqueue.h"
  10. #include "materialsystem_global.h"
  11. #define USE_QUEUED_MATERIAL_CALLS //uncomment to queue up material changing calls. Comment out to always use instant calls.
  12. #ifdef USE_QUEUED_MATERIAL_CALLS
  13. #define QUEUE_MATERIAL_CALL( FuncName, ... ) \
  14. { \
  15. ICallQueue *pCallQueue = materials->GetRenderContext()->GetCallQueue(); \
  16. if ( !pCallQueue ) \
  17. { \
  18. m_pRealTimeVersion->FuncName( __VA_ARGS__ ); \
  19. } \
  20. else \
  21. { \
  22. pCallQueue->QueueCall( m_pRealTimeVersion, &IMaterialInternal::FuncName, ##__VA_ARGS__ ); \
  23. } \
  24. }
  25. #else
  26. #define QUEUE_MATERIAL_CALL( FuncName, ... ) m_pRealTimeVersion->FuncName( __VA_ARGS__ );
  27. #endif
  28. const char *CMaterial_QueueFriendly::GetName() const
  29. {
  30. return m_pRealTimeVersion->GetName();
  31. }
  32. const char *CMaterial_QueueFriendly::GetTextureGroupName() const
  33. {
  34. return m_pRealTimeVersion->GetTextureGroupName();
  35. }
  36. PreviewImageRetVal_t CMaterial_QueueFriendly::GetPreviewImageProperties( int *width, int *height, ImageFormat *imageFormat, bool* isTranslucent ) const
  37. {
  38. return m_pRealTimeVersion->GetPreviewImageProperties( width, height, imageFormat, isTranslucent );
  39. }
  40. PreviewImageRetVal_t CMaterial_QueueFriendly::GetPreviewImage( unsigned char *data, int width, int height, ImageFormat imageFormat ) const
  41. {
  42. return m_pRealTimeVersion->GetPreviewImage( data, width, height, imageFormat );
  43. }
  44. int CMaterial_QueueFriendly::GetMappingWidth( )
  45. {
  46. return m_pRealTimeVersion->GetMappingWidth();
  47. }
  48. int CMaterial_QueueFriendly::GetMappingHeight( )
  49. {
  50. return m_pRealTimeVersion->GetMappingHeight();
  51. }
  52. int CMaterial_QueueFriendly::GetNumAnimationFrames( )
  53. {
  54. return m_pRealTimeVersion->GetNumAnimationFrames();
  55. }
  56. bool CMaterial_QueueFriendly::InMaterialPage( void )
  57. {
  58. return m_pRealTimeVersion->InMaterialPage();
  59. }
  60. void CMaterial_QueueFriendly::GetMaterialOffset( float *pOffset )
  61. {
  62. m_pRealTimeVersion->GetMaterialOffset( pOffset );
  63. }
  64. void CMaterial_QueueFriendly::GetMaterialScale( float *pScale )
  65. {
  66. m_pRealTimeVersion->GetMaterialScale( pScale );
  67. }
  68. IMaterial *CMaterial_QueueFriendly::GetMaterialPage( void )
  69. {
  70. return m_pRealTimeVersion->GetMaterialPage();
  71. }
  72. void CMaterial_QueueFriendly::IncrementReferenceCount( void )
  73. {
  74. Assert( ThreadInMainThread() );
  75. ++m_nReferenceCount;
  76. m_pRealTimeVersion->IncrementReferenceCount();
  77. }
  78. int CMaterial_QueueFriendly::GetEnumerationID( void ) const
  79. {
  80. return m_pRealTimeVersion->GetEnumerationID();
  81. }
  82. bool CMaterial_QueueFriendly::HasProxy( void ) const
  83. {
  84. return m_pRealTimeVersion->HasProxy();
  85. }
  86. void CMaterial_QueueFriendly::GetReflectivity( Vector& reflect )
  87. {
  88. m_pRealTimeVersion->GetReflectivity( reflect );
  89. }
  90. bool CMaterial_QueueFriendly::GetPropertyFlag( MaterialPropertyTypes_t type )
  91. {
  92. return m_pRealTimeVersion->GetPropertyFlag( type );
  93. }
  94. bool CMaterial_QueueFriendly::IsTwoSided()
  95. {
  96. return m_pRealTimeVersion->IsTwoSided();
  97. }
  98. int CMaterial_QueueFriendly::ShaderParamCount() const
  99. {
  100. return m_pRealTimeVersion->ShaderParamCount();
  101. }
  102. bool CMaterial_QueueFriendly::IsErrorMaterial() const
  103. {
  104. return m_pRealTimeVersion->IsErrorMaterial();
  105. }
  106. bool CMaterial_QueueFriendly::IsSpriteCard()
  107. {
  108. return m_pRealTimeVersion->IsSpriteCard();
  109. }
  110. //TODO: Investigate if these are likely to change at all when setting vars/flags
  111. bool CMaterial_QueueFriendly::IsAlphaTested()
  112. {
  113. return m_pRealTimeVersion->IsAlphaTested();
  114. }
  115. bool CMaterial_QueueFriendly::IsVertexLit()
  116. {
  117. return m_pRealTimeVersion->IsVertexLit();
  118. }
  119. VertexFormat_t CMaterial_QueueFriendly::GetVertexFormat() const
  120. {
  121. return m_pRealTimeVersion->GetVertexFormat();
  122. }
  123. bool CMaterial_QueueFriendly::UsesEnvCubemap( void )
  124. {
  125. return m_pRealTimeVersion->UsesEnvCubemap();
  126. }
  127. bool CMaterial_QueueFriendly::NeedsTangentSpace( void )
  128. {
  129. return m_pRealTimeVersion->NeedsTangentSpace();
  130. }
  131. bool CMaterial_QueueFriendly::NeedsSoftwareSkinning( void )
  132. {
  133. return m_pRealTimeVersion->NeedsSoftwareSkinning();
  134. }
  135. int CMaterial_QueueFriendly::GetNumPasses( void )
  136. {
  137. return m_pRealTimeVersion->GetNumPasses();
  138. }
  139. int CMaterial_QueueFriendly::GetTextureMemoryBytes( void )
  140. {
  141. return m_pRealTimeVersion->GetTextureMemoryBytes();
  142. }
  143. bool CMaterial_QueueFriendly::NeedsLightmapBlendAlpha( void )
  144. {
  145. return m_pRealTimeVersion->NeedsLightmapBlendAlpha();
  146. }
  147. bool CMaterial_QueueFriendly::NeedsSoftwareLighting( void )
  148. {
  149. return m_pRealTimeVersion->NeedsSoftwareLighting();
  150. }
  151. MorphFormat_t CMaterial_QueueFriendly::GetMorphFormat() const
  152. {
  153. return m_pRealTimeVersion->GetMorphFormat();
  154. }
  155. void CMaterial_QueueFriendly::GetLowResColorSample( float s, float t, float *color ) const
  156. {
  157. if ( m_pRealTimeVersion )
  158. m_pRealTimeVersion->GetLowResColorSample( s, t, color );
  159. else
  160. color[ 0 ] = color[ 1 ] = color[ 2 ] = 0.0f;
  161. }
  162. IMaterialVar *CMaterial_QueueFriendly::FindVar( const char *varName, bool *found, bool complain )
  163. {
  164. //TODO: return a queue friendly variable that can be get/set
  165. return m_pRealTimeVersion->FindVar( varName, found, complain );
  166. }
  167. IMaterialVar *CMaterial_QueueFriendly::FindVarFast( char const *pVarName, unsigned int *pToken )
  168. {
  169. //TODO: return a queue friendly variable that can be get/set
  170. return m_pRealTimeVersion->FindVarFast( pVarName, pToken );
  171. }
  172. IMaterialVar **CMaterial_QueueFriendly::GetShaderParams( void )
  173. {
  174. //TODO: return queue friendly variables that can be get/set
  175. return m_pRealTimeVersion->GetShaderParams();
  176. }
  177. void CMaterial_QueueFriendly::DecrementReferenceCount( void )
  178. {
  179. Assert( ThreadInMainThread() );
  180. --m_nReferenceCount;
  181. QUEUE_MATERIAL_CALL( DecrementReferenceCount );
  182. }
  183. void CMaterial_QueueFriendly::DeleteIfUnreferenced()
  184. {
  185. Assert( ThreadInMainThread() );
  186. if ( m_nReferenceCount > 0 )
  187. return;
  188. MaterialSystem()->RemoveMaterial( GetRealTimeVersion() );
  189. QUEUE_MATERIAL_CALL( DeleteIfUnreferenced );
  190. }
  191. void CMaterial_QueueFriendly::RecomputeStateSnapshots()
  192. {
  193. QUEUE_MATERIAL_CALL( RecomputeStateSnapshots );
  194. }
  195. bool CMaterial_QueueFriendly::IsTranslucent()
  196. {
  197. //TODO: need to base this as if the queued state is 100% up to date
  198. return m_pRealTimeVersion->IsTranslucentInternal( GetMaterialVarFlag( MATERIAL_VAR_IGNORE_ALPHA_MODULATION ) ? 1.0f : m_fAlphaModulationOnQueueCompletion );
  199. }
  200. bool CMaterial_QueueFriendly::NeedsPowerOfTwoFrameBufferTexture( bool bCheckSpecificToThisFrame )
  201. {
  202. //bCheckSpecificToThisFrame scares me a bit.
  203. return m_pRealTimeVersion->NeedsPowerOfTwoFrameBufferTexture( bCheckSpecificToThisFrame );
  204. }
  205. bool CMaterial_QueueFriendly::NeedsFullFrameBufferTexture( bool bCheckSpecificToThisFrame )
  206. {
  207. //bCheckSpecificToThisFrame scares me a bit.
  208. return m_pRealTimeVersion->NeedsFullFrameBufferTexture( bCheckSpecificToThisFrame );
  209. }
  210. void CMaterial_QueueFriendly::AlphaModulate( float alpha )
  211. {
  212. QUEUE_MATERIAL_CALL( AlphaModulate, alpha );
  213. m_fAlphaModulationOnQueueCompletion = alpha;
  214. }
  215. void CMaterial_QueueFriendly::ColorModulate( float r, float g, float b )
  216. {
  217. QUEUE_MATERIAL_CALL( ColorModulate, r, g, b );
  218. m_vColorModulationOnQueueCompletion.Init( r, g, b );
  219. }
  220. void CMaterial_QueueFriendly::SetMaterialVarFlag( MaterialVarFlags_t flag, bool on )
  221. {
  222. QUEUE_MATERIAL_CALL( SetMaterialVarFlag, flag, on );
  223. }
  224. bool CMaterial_QueueFriendly::GetMaterialVarFlag( MaterialVarFlags_t flag ) const
  225. {
  226. //TODO: somehow mix both queued and real time states
  227. return m_pRealTimeVersion->GetMaterialVarFlag( flag );
  228. }
  229. void CMaterial_QueueFriendly::SetShader( const char *pShaderName )
  230. {
  231. //TODO: queue it and investigate, seems like a grenade.
  232. m_pRealTimeVersion->SetShader( pShaderName );
  233. }
  234. void CMaterial_QueueFriendly::SetShaderAndParams( KeyValues *pKeyValues )
  235. {
  236. //TODO: queue it and investigate, seems like a grenade.
  237. m_pRealTimeVersion->SetShaderAndParams( pKeyValues );
  238. }
  239. const char *CMaterial_QueueFriendly::GetShaderName() const
  240. {
  241. //TODO: return as if the queue is up to date. Someone could have set the shader very recently
  242. return m_pRealTimeVersion->GetShaderName();
  243. }
  244. void CMaterial_QueueFriendly::Refresh()
  245. {
  246. //TODO: Investigate, this one seems like a grenade.
  247. m_pRealTimeVersion->Refresh();
  248. //QUEUE_MATERIAL_CALL( Refresh );
  249. }
  250. void CMaterial_QueueFriendly::RefreshPreservingMaterialVars()
  251. {
  252. //TODO: Investigate, this one seems like a grenade.
  253. m_pRealTimeVersion->RefreshPreservingMaterialVars();
  254. //QUEUE_MATERIAL_CALL( RefreshPreservingMaterialVars );
  255. }
  256. void CMaterial_QueueFriendly::SetUseFixedFunctionBakedLighting( bool bEnable )
  257. {
  258. QUEUE_MATERIAL_CALL( SetUseFixedFunctionBakedLighting, bEnable );
  259. }
  260. float CMaterial_QueueFriendly::GetAlphaModulation()
  261. {
  262. #ifdef USE_QUEUED_MATERIAL_CALLS
  263. return m_fAlphaModulationOnQueueCompletion;
  264. #else
  265. return m_pRealTimeVersion->GetAlphaModulation();
  266. #endif
  267. }
  268. void CMaterial_QueueFriendly::GetColorModulation( float *r, float *g, float *b )
  269. {
  270. #ifdef USE_QUEUED_MATERIAL_CALLS
  271. *r = m_vColorModulationOnQueueCompletion.x;
  272. *g = m_vColorModulationOnQueueCompletion.y;
  273. *b = m_vColorModulationOnQueueCompletion.z;
  274. #else
  275. m_pRealTimeVersion->GetColorModulation( r, g, b );
  276. #endif
  277. }
  278. void CMaterial_QueueFriendly::CallBindProxy( void *proxyData )
  279. {
  280. //TODO: queue it? Investigate.
  281. return m_pRealTimeVersion->CallBindProxy( proxyData );
  282. }
  283. IMaterial *CMaterial_QueueFriendly::CheckProxyReplacement( void *proxyData )
  284. {
  285. return m_pRealTimeVersion->CheckProxyReplacement( proxyData );
  286. }
  287. void CMaterial_QueueFriendly::PrecacheMappingDimensions()
  288. {
  289. return m_pRealTimeVersion->PrecacheMappingDimensions();
  290. }
  291. void CMaterial_QueueFriendly::FindRepresentativeTexture()
  292. {
  293. return m_pRealTimeVersion->FindRepresentativeTexture();
  294. }
  295. bool CMaterial_QueueFriendly::IsRealTimeVersion( void ) const
  296. {
  297. return false;
  298. }
  299. IMaterialInternal *CMaterial_QueueFriendly::GetRealTimeVersion( void )
  300. {
  301. return m_pRealTimeVersion;
  302. }
  303. IMaterialInternal *CMaterial_QueueFriendly::GetQueueFriendlyVersion( void )
  304. {
  305. return this;
  306. }
  307. void CMaterial_QueueFriendly::UpdateToRealTime( void )
  308. {
  309. m_fAlphaModulationOnQueueCompletion = m_pRealTimeVersion->GetAlphaModulation();
  310. m_pRealTimeVersion->GetColorModulation( &m_vColorModulationOnQueueCompletion.x,
  311. &m_vColorModulationOnQueueCompletion.y,
  312. &m_vColorModulationOnQueueCompletion.z );
  313. m_nReferenceCount = m_pRealTimeVersion->GetReferenceCount();
  314. }