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.

622 lines
26 KiB

  1. // BE VERY VERY CAREFUL what you do in these function. They are extremely hot, and calling the wrong GL API's in here will crush perf. (especially on NVidia threaded drivers).
  2. FORCEINLINE uint32 bitmix32(uint32 a)
  3. {
  4. a -= (a<<6);
  5. //a ^= (a>>17);
  6. //a -= (a<<9);
  7. a ^= (a<<4);
  8. //a -= (a<<3);
  9. //a ^= (a<<10);
  10. a ^= (a>>15);
  11. return a;
  12. }
  13. #ifndef OSX
  14. FORCEINLINE GLuint GLMContext::FindSamplerObject( const GLMTexSamplingParams &desiredParams )
  15. {
  16. int h = bitmix32( desiredParams.m_bits + desiredParams.m_borderColor ) & ( cSamplerObjectHashSize - 1 );
  17. while ( ( m_samplerObjectHash[h].m_params.m_bits != desiredParams.m_bits ) || ( m_samplerObjectHash[h].m_params.m_borderColor != desiredParams.m_borderColor ) )
  18. {
  19. if ( !m_samplerObjectHash[h].m_params.m_packed.m_isValid )
  20. break;
  21. if ( ++h >= cSamplerObjectHashSize )
  22. h = 0;
  23. }
  24. if ( !m_samplerObjectHash[h].m_params.m_packed.m_isValid )
  25. {
  26. GLMTexSamplingParams &hashParams = m_samplerObjectHash[h].m_params;
  27. hashParams = desiredParams;
  28. hashParams.SetToSamplerObject( m_samplerObjectHash[h].m_samplerObject );
  29. if ( ++m_nSamplerObjectHashNumEntries == cSamplerObjectHashSize )
  30. {
  31. // TODO: Support resizing
  32. Error( "Sampler object hash is full, increase cSamplerObjectHashSize" );
  33. }
  34. }
  35. return m_samplerObjectHash[h].m_samplerObject;
  36. }
  37. #endif // !OSX
  38. // BE VERY CAREFUL WHAT YOU DO IN HERE. This is called on every batch, even seemingly simple changes can kill perf.
  39. FORCEINLINE void GLMContext::FlushDrawStates( uint nStartIndex, uint nEndIndex, uint nBaseVertex ) // shadersOn = true for draw calls, false for clear calls
  40. {
  41. Assert( m_drawingLang == kGLMGLSL ); // no support for ARB shaders right now (and NVidia reports that they aren't worth targeting under Windows/Linux for various reasons anyway)
  42. Assert( ( m_drawingFBO == m_boundDrawFBO ) && ( m_drawingFBO == m_boundReadFBO ) ); // this check MUST succeed
  43. Assert( m_pDevice->m_pVertDecl );
  44. #if GLMDEBUG
  45. GLM_FUNC;
  46. #endif
  47. GL_BATCH_PERF( m_FlushStats.m_nTotalBatchFlushes++; )
  48. #if GLMDEBUG
  49. bool tex0_srgb = (m_boundDrawFBO[0].m_attach[0].m_tex->m_layout->m_key.m_texFlags & kGLMTexSRGB) != 0;
  50. // you can only actually use the sRGB FB state on some systems.. check caps
  51. if (m_caps.m_hasGammaWrites)
  52. {
  53. GLBlendEnableSRGB_t writeSRGBState;
  54. m_BlendEnableSRGB.Read( &writeSRGBState, 0 ); // the client set value, not the API-written value yet..
  55. bool draw_srgb = writeSRGBState.enable != 0;
  56. if (draw_srgb)
  57. {
  58. if (tex0_srgb)
  59. {
  60. // good - draw mode and color tex agree
  61. }
  62. else
  63. {
  64. // bad
  65. // Client has asked to write sRGB into a texture that can't do it.
  66. // there is no way to satisfy this unless we change the RT tex and we avoid doing that.
  67. // (although we might consider a ** ONE TIME ** promotion.
  68. // this shouldn't be a big deal if the tex format is one where it doesn't matter like 32F.
  69. GLMPRINTF(("-Z- srgb-enabled FBO conflict: attached tex %08x [%s] is not SRGB", m_boundDrawFBO[0].m_attach[0].m_tex, m_boundDrawFBO[0].m_attach[0].m_tex->m_layout->m_layoutSummary ));
  70. // do we shoot down the srgb-write state for this batch?
  71. // I think the runtime will just ignore it.
  72. }
  73. }
  74. else
  75. {
  76. if (tex0_srgb)
  77. {
  78. // odd - client is not writing sRGB into a texture which *can* do it.
  79. //GLMPRINTF(( "-Z- srgb-disabled FBO conflict: attached tex %08x [%s] is SRGB", m_boundFBO[0].m_attach[0].m_tex, m_boundFBO[0].m_attach[0].m_tex->m_layout->m_layoutSummary ));
  80. //writeSRGBState.enable = true;
  81. //m_BlendEnableSRGB.Write( &writeSRGBState );
  82. }
  83. else
  84. {
  85. // good - draw mode and color tex agree
  86. }
  87. }
  88. }
  89. #endif
  90. Assert( m_drawingProgram[ kGLMVertexProgram ] );
  91. Assert( m_drawingProgram[ kGLMFragmentProgram ] );
  92. Assert( ( m_drawingProgram[kGLMVertexProgram]->m_type == kGLMVertexProgram ) && ( m_drawingProgram[kGLMFragmentProgram]->m_type == kGLMFragmentProgram ) );
  93. Assert( m_drawingProgram[ kGLMVertexProgram ]->m_bTranslatedProgram && m_drawingProgram[ kGLMFragmentProgram ]->m_bTranslatedProgram );
  94. #if GLMDEBUG
  95. // Depth compare mode check
  96. uint nCurMask = 1, nShaderSamplerMask = m_drawingProgram[kGLMFragmentProgram]->m_samplerMask;
  97. for ( int nSamplerIndex = 0; nSamplerIndex < GLM_SAMPLER_COUNT; ++nSamplerIndex, nCurMask <<= 1 )
  98. {
  99. if ( !m_samplers[nSamplerIndex].m_pBoundTex )
  100. continue;
  101. if ( m_samplers[nSamplerIndex].m_pBoundTex->m_layout->m_mipCount == 1 )
  102. {
  103. if ( m_samplers[nSamplerIndex].m_samp.m_packed.m_mipFilter == D3DTEXF_LINEAR )
  104. {
  105. GLMDebugPrintf( "Sampler %u has mipmap filtering enabled on a texture without mipmaps! (texture name: %s, pixel shader: %s)!\n",
  106. nSamplerIndex,
  107. m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?",
  108. m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
  109. }
  110. }
  111. if ( ( nShaderSamplerMask & nCurMask ) == 0 )
  112. continue;
  113. if ( m_samplers[nSamplerIndex].m_pBoundTex->m_layout->m_mipCount == 1 )
  114. {
  115. if ( m_samplers[nSamplerIndex].m_samp.m_packed.m_mipFilter == D3DTEXF_LINEAR )
  116. {
  117. // Note this is not always an error - shadow buffer debug visualization shaders purposely want to read shadow depths (and not do the comparison)
  118. GLMDebugPrintf( "Sampler %u has mipmap filtering enabled on a texture without mipmaps! (texture name: %s, pixel shader: %s)!\n",
  119. nSamplerIndex,
  120. m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?",
  121. m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
  122. }
  123. }
  124. bool bSamplerIsDepth = ( m_samplers[nSamplerIndex].m_pBoundTex->m_layout->m_key.m_texFlags & kGLMTexIsDepth ) != 0;
  125. bool bSamplerShadow = m_samplers[nSamplerIndex].m_samp.m_packed.m_compareMode != 0;
  126. bool bShaderShadow = ( m_drawingProgram[kGLMFragmentProgram]->m_nShadowDepthSamplerMask & nCurMask ) != 0;
  127. if ( bShaderShadow )
  128. {
  129. // Shader expects shadow depth sampling at this sampler index
  130. // Must have a depth texture and compare mode must be enabled
  131. if ( !bSamplerIsDepth || !bSamplerShadow )
  132. {
  133. // FIXME: This occasionally occurs in L4D2 when CShaderAPIDx8::ExecuteCommandBuffer() sets the TEXTURE_WHITE texture in the flashlight depth texture slot.
  134. GLMDebugPrintf( "Sampler %u's compare mode (%u) or format (depth=%u) is not consistent with pixel shader's compare mode (%u) (texture name: %s, pixel shader: %s)!\n",
  135. nSamplerIndex, bSamplerShadow, bSamplerIsDepth, bShaderShadow,
  136. m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?",
  137. m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
  138. }
  139. }
  140. else
  141. {
  142. // Shader does not expect shadow depth sampling as this sampler index
  143. // We don't care if comparemode is enabled, but we can't have a depth texture in this sampler
  144. if ( bSamplerIsDepth )
  145. {
  146. GLMDebugPrintf( "Sampler %u is a depth texture but the pixel shader's shadow depth sampler mask does not expect depth here (texture name: %s, pixel shader: %s)!\n",
  147. nSamplerIndex,
  148. m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?",
  149. m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
  150. }
  151. }
  152. }
  153. #endif
  154. if ( m_bDirtyPrograms )
  155. {
  156. m_bDirtyPrograms = false;
  157. CGLMShaderPair *pNewPair = m_pairCache->SelectShaderPair( m_drawingProgram[ kGLMVertexProgram ], m_drawingProgram[ kGLMFragmentProgram ], 0 );
  158. if ( pNewPair != m_pBoundPair )
  159. {
  160. #if GL_BATCH_TELEMETRY_ZONES
  161. tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "NewProgram" );
  162. #endif
  163. if ( !pNewPair->m_valid )
  164. {
  165. if ( !pNewPair->ValidateProgramPair() )
  166. {
  167. goto flush_error_exit;
  168. }
  169. }
  170. gGL->glUseProgram( (GLuint)pNewPair->m_program );
  171. GL_BATCH_PERF( m_FlushStats.m_nTotalProgramPairChanges++; )
  172. if ( !m_pBoundPair )
  173. {
  174. GL_BATCH_PERF( m_FlushStats.m_nNewPS++; )
  175. GL_BATCH_PERF( m_FlushStats.m_nNewVS++; )
  176. }
  177. else
  178. {
  179. GL_BATCH_PERF( if ( pNewPair->m_fragmentProg != m_pBoundPair->m_fragmentProg ) m_FlushStats.m_nNewPS++; )
  180. GL_BATCH_PERF( if ( pNewPair->m_vertexProg != m_pBoundPair->m_vertexProg ) m_FlushStats.m_nNewVS++; )
  181. }
  182. #if GL_BATCH_PERF_ANALYSIS
  183. tmMessage( TELEMETRY_LEVEL2, TMMF_ICON_NOTE, "V:%s (V Regs:%u V Bone Regs:%u) F:%s (F Regs:%u)",
  184. m_drawingProgram[ kGLMVertexProgram ]->m_shaderName,
  185. m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_highWater,
  186. m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_VSHighWaterBone,
  187. m_drawingProgram[ kGLMFragmentProgram ]->m_shaderName,
  188. m_drawingProgram[ kGLMFragmentProgram ]->m_descs[kGLMGLSL].m_highWater );
  189. #endif
  190. m_pBoundPair = pNewPair;
  191. // set the dirty levels appropriately since the program changed and has never seen any of the current values.
  192. m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone = 0;
  193. m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone = m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_highWater;
  194. m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone = m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_VSHighWaterBone;
  195. m_programParamsF[kGLMFragmentProgram].m_firstDirtySlotNonBone = 0;
  196. m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone = m_drawingProgram[ kGLMFragmentProgram ]->m_descs[kGLMGLSL].m_highWater;
  197. // bool and int dirty levels get set to max, we don't have actual high water marks for them
  198. // code which sends the values must clamp on these types.
  199. m_programParamsB[kGLMVertexProgram].m_dirtySlotCount = kGLMProgramParamBoolLimit;
  200. m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount = kGLMProgramParamBoolLimit;
  201. m_programParamsI[kGLMVertexProgram].m_dirtySlotCount = kGLMProgramParamInt4Limit;
  202. m_programParamsI[kGLMFragmentProgram].m_dirtySlotCount = 0;
  203. // check fragment buffers used (MRT)
  204. if( pNewPair->m_fragmentProg->m_fragDataMask != m_fragDataMask )
  205. {
  206. gGL->glDrawBuffers( pNewPair->m_fragmentProg->m_numDrawBuffers, pNewPair->m_fragmentProg->m_drawBuffers );
  207. m_fragDataMask = pNewPair->m_fragmentProg->m_fragDataMask;
  208. }
  209. }
  210. }
  211. Assert( m_ViewportBox.GetData().width == (int)( m_ViewportBox.GetData().widthheight & 0xFFFF ) );
  212. Assert( m_ViewportBox.GetData().height == (int)( m_ViewportBox.GetData().widthheight >> 16 ) );
  213. m_pBoundPair->UpdateScreenUniform( m_ViewportBox.GetData().widthheight );
  214. GL_BATCH_PERF( m_FlushStats.m_nNumChangedSamplers += m_nNumDirtySamplers );
  215. #if !defined( OSX ) // no support for sampler objects in OSX 10.6 (GL 2.1 profile)
  216. if ( m_bUseSamplerObjects)
  217. {
  218. while ( m_nNumDirtySamplers )
  219. {
  220. const uint nSamplerIndex = m_nDirtySamplers[--m_nNumDirtySamplers];
  221. Assert( ( nSamplerIndex < GLM_SAMPLER_COUNT ) && ( !m_nDirtySamplerFlags[nSamplerIndex]) );
  222. m_nDirtySamplerFlags[nSamplerIndex] = 1;
  223. gGL->glBindSampler( nSamplerIndex, FindSamplerObject( m_samplers[nSamplerIndex].m_samp ) );
  224. GL_BATCH_PERF( m_FlushStats.m_nNumSamplingParamsChanged++ );
  225. #if defined( OSX ) // valid for OSX only if using GL 3.3 context
  226. CGLMTex *pTex = m_samplers[nSamplerIndex].m_pBoundTex;
  227. if( pTex && !( gGL->m_bHave_GL_EXT_texture_sRGB_decode ) )
  228. {
  229. // see if requested SRGB state differs from the known one
  230. bool texSRGB = ( pTex->m_layout->m_key.m_texFlags & kGLMTexSRGB ) != 0;
  231. bool glSampSRGB = m_samplers[nSamplerIndex].m_samp.m_packed.m_srgb;
  232. if ( texSRGB != glSampSRGB ) // mismatch
  233. {
  234. pTex->HandleSRGBMismatch( glSampSRGB, pTex->m_srgbFlipCount );
  235. }
  236. }
  237. #endif
  238. }
  239. }
  240. else
  241. #endif // if !defined( OSX )
  242. {
  243. while ( m_nNumDirtySamplers )
  244. {
  245. const uint nSamplerIndex = m_nDirtySamplers[--m_nNumDirtySamplers];
  246. Assert( ( nSamplerIndex < GLM_SAMPLER_COUNT ) && ( !m_nDirtySamplerFlags[nSamplerIndex]) );
  247. m_nDirtySamplerFlags[nSamplerIndex] = 1;
  248. CGLMTex *pTex = m_samplers[nSamplerIndex].m_pBoundTex;
  249. if ( ( pTex ) && ( !( pTex->m_SamplingParams == m_samplers[nSamplerIndex].m_samp ) ) )
  250. {
  251. SelectTMU( nSamplerIndex );
  252. m_samplers[nSamplerIndex].m_samp.DeltaSetToTarget( pTex->m_texGLTarget, pTex->m_SamplingParams );
  253. pTex->m_SamplingParams = m_samplers[nSamplerIndex].m_samp;
  254. #if defined( OSX )
  255. if( pTex && !( gGL->m_bHave_GL_EXT_texture_sRGB_decode ) )
  256. {
  257. // see if requested SRGB state differs from the known one
  258. bool texSRGB = ( pTex->m_layout->m_key.m_texFlags & kGLMTexSRGB ) != 0;
  259. bool glSampSRGB = m_samplers[nSamplerIndex].m_samp.m_packed.m_srgb;
  260. if ( texSRGB != glSampSRGB ) // mismatch
  261. {
  262. pTex->HandleSRGBMismatch( glSampSRGB, pTex->m_srgbFlipCount );
  263. }
  264. }
  265. #endif
  266. }
  267. }
  268. }
  269. // vertex stage --------------------------------------------------------------------
  270. if ( m_bUseBoneUniformBuffers )
  271. {
  272. // vertex stage --------------------------------------------------------------------
  273. if ( m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone )
  274. {
  275. int firstDirtySlot = m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone;
  276. int dirtySlotHighWater = MIN( m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_highWater, m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone );
  277. GLint vconstLoc = m_pBoundPair->m_locVertexParams;
  278. if ( ( vconstLoc >= 0 ) && ( dirtySlotHighWater > firstDirtySlot ) )
  279. {
  280. #if GL_BATCH_TELEMETRY_ZONES
  281. tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "VSNonBoneUniformUpdate %u %u", firstDirtySlot, dirtySlotHighWater );
  282. #endif
  283. int numSlots = dirtySlotHighWater - DXABSTRACT_VS_FIRST_BONE_SLOT;
  284. // consts after the bones (c217 onwards), since we use the concatenated destination array vc[], upload these consts starting from vc[58]
  285. if( numSlots > 0 )
  286. {
  287. gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMVertexProgram][DXABSTRACT_VS_FIRST_BONE_SLOT], numSlots, &m_programParamsF[kGLMVertexProgram].m_values[(DXABSTRACT_VS_LAST_BONE_SLOT+1)][0] );
  288. dirtySlotHighWater = DXABSTRACT_VS_FIRST_BONE_SLOT;
  289. GL_BATCH_PERF( m_nTotalVSUniformCalls++; )
  290. GL_BATCH_PERF( m_nTotalVSUniformsSet += numSlots; )
  291. GL_BATCH_PERF( m_FlushStats.m_nFirstVSConstant = DXABSTRACT_VS_FIRST_BONE_SLOT; )
  292. GL_BATCH_PERF( m_FlushStats.m_nNumVSConstants += numSlots; )
  293. }
  294. numSlots = dirtySlotHighWater - firstDirtySlot;
  295. // consts before the bones (c0-c57)
  296. if( numSlots > 0 )
  297. {
  298. gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMVertexProgram][firstDirtySlot], dirtySlotHighWater - firstDirtySlot, &m_programParamsF[kGLMVertexProgram].m_values[firstDirtySlot][0] );
  299. GL_BATCH_PERF( m_nTotalVSUniformCalls++; )
  300. GL_BATCH_PERF( m_nTotalVSUniformsSet += dirtySlotHighWater - firstDirtySlot; )
  301. GL_BATCH_PERF( m_FlushStats.m_nFirstVSConstant = firstDirtySlot; )
  302. GL_BATCH_PERF( m_FlushStats.m_nNumVSConstants += (dirtySlotHighWater - firstDirtySlot); )
  303. }
  304. }
  305. m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone = 256;
  306. m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone = 0;
  307. }
  308. if ( m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone )
  309. {
  310. const GLint vconstBoneLoc = m_pBoundPair->m_locVertexBoneParams;
  311. if ( vconstBoneLoc >= 0 )
  312. {
  313. int shaderSlotsBone = 0;
  314. if ( ( m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_VSHighWaterBone > 0 ) && ( m_nMaxUsedVertexProgramConstantsHint > DXABSTRACT_VS_FIRST_BONE_SLOT ) )
  315. {
  316. shaderSlotsBone = MIN( m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_VSHighWaterBone, m_nMaxUsedVertexProgramConstantsHint - DXABSTRACT_VS_FIRST_BONE_SLOT );
  317. }
  318. int dirtySlotHighWaterBone = MIN( shaderSlotsBone, m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone );
  319. if ( dirtySlotHighWaterBone )
  320. {
  321. uint nNumBoneRegs = dirtySlotHighWaterBone;
  322. #if GL_BATCH_TELEMETRY_ZONES
  323. tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "VSBoneUniformUpdate %u", nNumBoneRegs );
  324. #endif
  325. gGL->glUniform4fv( vconstBoneLoc, nNumBoneRegs, &m_programParamsF[kGLMVertexProgram].m_values[DXABSTRACT_VS_FIRST_BONE_SLOT][0] );
  326. GL_BATCH_PERF( m_nTotalVSUniformBoneCalls++; )
  327. GL_BATCH_PERF( m_nTotalVSUniformsBoneSet += nNumBoneRegs; )
  328. GL_BATCH_PERF( m_FlushStats.m_nNumVSBoneConstants += nNumBoneRegs; )
  329. }
  330. m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone = 0;
  331. }
  332. }
  333. }
  334. else
  335. {
  336. if ( m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone )
  337. {
  338. const int nMaxUsedShaderSlots = m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_highWater;
  339. int firstDirtySlot = m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone;
  340. int dirtySlotHighWater = MIN( nMaxUsedShaderSlots, m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone );
  341. GLint vconstLoc = m_pBoundPair->m_locVertexParams;
  342. if ( ( vconstLoc >= 0 ) && ( dirtySlotHighWater > firstDirtySlot ) )
  343. {
  344. #if GL_BATCH_TELEMETRY_ZONES
  345. tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "VSNonBoneUniformUpdate %u %u", firstDirtySlot, dirtySlotHighWater );
  346. #endif
  347. gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMVertexProgram][firstDirtySlot], dirtySlotHighWater - firstDirtySlot, &m_programParamsF[kGLMVertexProgram].m_values[firstDirtySlot][0] );
  348. GL_BATCH_PERF( m_nTotalVSUniformCalls++; )
  349. GL_BATCH_PERF( m_nTotalVSUniformsSet += dirtySlotHighWater - firstDirtySlot; )
  350. GL_BATCH_PERF( m_FlushStats.m_nFirstVSConstant = firstDirtySlot; )
  351. GL_BATCH_PERF( m_FlushStats.m_nNumVSConstants += (dirtySlotHighWater - firstDirtySlot); )
  352. }
  353. m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone = 256;
  354. m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone = 0;
  355. }
  356. }
  357. // see if VS uses i0, b0, b1, b2, b3.
  358. // use a glUniform1i to set any one of these if active. skip all of them if no dirties reported.
  359. // my kingdom for the UBO extension!
  360. // ------- bools ---------- //
  361. if ( m_pBoundPair->m_bHasBoolOrIntUniforms )
  362. {
  363. if ( m_programParamsB[kGLMVertexProgram].m_dirtySlotCount ) // optimize this later after the float param pushes are proven out
  364. {
  365. const uint nLimit = MIN( CGLMShaderPair::cMaxVertexShaderBoolUniforms, m_programParamsB[kGLMVertexProgram].m_dirtySlotCount );
  366. for ( uint i = 0; i < nLimit; ++i )
  367. {
  368. GLint constBoolLoc = m_pBoundPair->m_locVertexBool[i];
  369. if ( constBoolLoc >= 0 )
  370. gGL->glUniform1i( constBoolLoc, m_programParamsB[kGLMVertexProgram].m_values[i] );
  371. }
  372. m_programParamsB[kGLMVertexProgram].m_dirtySlotCount = 0;
  373. }
  374. if ( m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount ) // optimize this later after the float param pushes are proven out
  375. {
  376. const uint nLimit = MIN( CGLMShaderPair::cMaxFragmentShaderBoolUniforms, m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount );
  377. for ( uint i = 0; i < nLimit; ++i )
  378. {
  379. GLint constBoolLoc = m_pBoundPair->m_locFragmentBool[i];
  380. if ( constBoolLoc >= 0 )
  381. gGL->glUniform1i( constBoolLoc, m_programParamsB[kGLMFragmentProgram].m_values[i] );
  382. }
  383. m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount = 0;
  384. }
  385. if ( m_programParamsI[kGLMVertexProgram].m_dirtySlotCount )
  386. {
  387. GLint vconstInt0Loc = m_pBoundPair->m_locVertexInteger0; //glGetUniformLocationARB( prog, "i0");
  388. if ( vconstInt0Loc >= 0 )
  389. {
  390. gGL->glUniform1i( vconstInt0Loc, m_programParamsI[kGLMVertexProgram].m_values[0][0] ); //FIXME magic number
  391. }
  392. m_programParamsI[kGLMVertexProgram].m_dirtySlotCount = 0;
  393. }
  394. }
  395. Assert( ( m_pDevice->m_streams[0].m_vtxBuffer && ( m_pDevice->m_streams[0].m_vtxBuffer->m_vtxBuffer == m_pDevice->m_vtx_buffers[0] ) ) || ( ( !m_pDevice->m_streams[0].m_vtxBuffer ) && ( m_pDevice->m_vtx_buffers[0] == m_pDevice->m_pDummy_vtx_buffer ) ) );
  396. Assert( ( m_pDevice->m_streams[1].m_vtxBuffer && ( m_pDevice->m_streams[1].m_vtxBuffer->m_vtxBuffer == m_pDevice->m_vtx_buffers[1] ) ) || ( ( !m_pDevice->m_streams[1].m_vtxBuffer ) && ( m_pDevice->m_vtx_buffers[1] == m_pDevice->m_pDummy_vtx_buffer ) ) );
  397. Assert( ( m_pDevice->m_streams[2].m_vtxBuffer && ( m_pDevice->m_streams[2].m_vtxBuffer->m_vtxBuffer == m_pDevice->m_vtx_buffers[2] ) ) || ( ( !m_pDevice->m_streams[2].m_vtxBuffer ) && ( m_pDevice->m_vtx_buffers[2] == m_pDevice->m_pDummy_vtx_buffer ) ) );
  398. Assert( ( m_pDevice->m_streams[3].m_vtxBuffer && ( m_pDevice->m_streams[3].m_vtxBuffer->m_vtxBuffer == m_pDevice->m_vtx_buffers[3] ) ) || ( ( !m_pDevice->m_streams[3].m_vtxBuffer ) && ( m_pDevice->m_vtx_buffers[3] == m_pDevice->m_pDummy_vtx_buffer ) ) );
  399. uint nCurTotalBufferRevision;
  400. nCurTotalBufferRevision = m_pDevice->m_vtx_buffers[0]->m_nRevision + m_pDevice->m_vtx_buffers[1]->m_nRevision + m_pDevice->m_vtx_buffers[2]->m_nRevision + m_pDevice->m_vtx_buffers[3]->m_nRevision;
  401. // If any of these inputs have changed, we need to enumerate through all of the expected GL vertex attribs and modify anything in the GL layer that have changed.
  402. // This is not always a win, but it is a net win on NVidia (by 1-4.8% depending on whether driver threading is enabled).
  403. if ( ( nCurTotalBufferRevision != m_CurAttribs.m_nTotalBufferRevision ) ||
  404. ( m_CurAttribs.m_pVertDecl != m_pDevice->m_pVertDecl ) ||
  405. ( m_CurAttribs.m_vtxAttribMap[0] != reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[0] ) ||
  406. ( m_CurAttribs.m_vtxAttribMap[1] != reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[1] ) ||
  407. ( memcmp( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) ) != 0 ) )
  408. {
  409. // This branch is taken 52.2% of the time in the L4D2 test1 (long) timedemo.
  410. #if GL_BATCH_TELEMETRY_ZONES
  411. tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "SetVertexAttribs" );
  412. #endif
  413. m_CurAttribs.m_nTotalBufferRevision = nCurTotalBufferRevision;
  414. m_CurAttribs.m_pVertDecl = m_pDevice->m_pVertDecl;
  415. m_CurAttribs.m_vtxAttribMap[0] = reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[0];
  416. m_CurAttribs.m_vtxAttribMap[1] = reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[1];
  417. memcpy( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) );
  418. unsigned char *pVertexShaderAttribMap = m_pDevice->m_vertexShader->m_vtxAttribMap;
  419. const int nMaxVertexAttributesToCheck = m_drawingProgram[ kGLMVertexProgram ]->m_maxVertexAttrs;
  420. IDirect3DVertexDeclaration9 *pVertDecl = m_pDevice->m_pVertDecl;
  421. const uint8 *pVertexAttribDescToStreamIndex = pVertDecl->m_VertexAttribDescToStreamIndex;
  422. for( int nMask = 1, nIndex = 0; nIndex < nMaxVertexAttributesToCheck; ++nIndex, nMask <<= 1 )
  423. {
  424. uint8 vertexShaderAttrib = pVertexShaderAttribMap[ nIndex ];
  425. uint nDeclIndex = pVertexAttribDescToStreamIndex[vertexShaderAttrib];
  426. if ( nDeclIndex == 0xFF )
  427. {
  428. // Not good - the vertex shader has an attribute which can't be located in the decl!
  429. // The D3D9 debug runtime is also going to complain.
  430. Assert( 0 );
  431. if ( m_lastKnownVertexAttribMask & nMask )
  432. {
  433. m_lastKnownVertexAttribMask &= ~nMask;
  434. gGL->glDisableVertexAttribArray( nIndex );
  435. }
  436. continue;
  437. }
  438. D3DVERTEXELEMENT9_GL *pDeclElem = &pVertDecl->m_elements[nDeclIndex];
  439. Assert( ( ( vertexShaderAttrib >> 4 ) == pDeclElem->m_dxdecl.Usage ) && ( ( vertexShaderAttrib & 0x0F ) == pDeclElem->m_dxdecl.UsageIndex) );
  440. const uint nStreamIndex = pDeclElem->m_dxdecl.Stream;
  441. const D3DStreamDesc *pStream = &m_pDevice->m_streams[ nStreamIndex ];
  442. CGLMBuffer *pBuf = m_pDevice->m_vtx_buffers[ nStreamIndex ];
  443. if ( pBuf == m_pDevice->m_pDummy_vtx_buffer )
  444. {
  445. Assert( pStream->m_vtxBuffer == NULL );
  446. // this shader doesn't use that pair.
  447. if ( m_lastKnownVertexAttribMask & nMask )
  448. {
  449. m_lastKnownVertexAttribMask &= ~nMask;
  450. gGL->glDisableVertexAttribArray( nIndex );
  451. }
  452. continue;
  453. }
  454. Assert( pStream->m_vtxBuffer->m_vtxBuffer == pBuf );
  455. int nBufOffset = pDeclElem->m_gldecl.m_offset + pStream->m_offset;
  456. Assert( nBufOffset >= 0 );
  457. Assert( nBufOffset < (int)pBuf->m_nSize );
  458. if ( pBuf->m_bUsingPersistentBuffer )
  459. {
  460. nBufOffset += pBuf->m_nPersistentBufferStartOffset;
  461. }
  462. SetBufAndVertexAttribPointer( nIndex, pBuf->GetHandle(),
  463. pStream->m_stride, pDeclElem->m_gldecl.m_datatype, pDeclElem->m_gldecl.m_normalized, pDeclElem->m_gldecl.m_nCompCount,
  464. reinterpret_cast< const GLvoid * >( reinterpret_cast< int >( pBuf->m_pPseudoBuf ) + nBufOffset ),
  465. pBuf->m_nRevision );
  466. if ( !( m_lastKnownVertexAttribMask & nMask ) )
  467. {
  468. m_lastKnownVertexAttribMask |= nMask;
  469. gGL->glEnableVertexAttribArray( nIndex );
  470. }
  471. }
  472. for( int nIndex = nMaxVertexAttributesToCheck; nIndex < m_nNumSetVertexAttributes; nIndex++ )
  473. {
  474. gGL->glDisableVertexAttribArray( nIndex );
  475. m_lastKnownVertexAttribMask &= ~(1 << nIndex);
  476. }
  477. m_nNumSetVertexAttributes = nMaxVertexAttributesToCheck;
  478. }
  479. // fragment stage --------------------------------------------------------------------
  480. if ( m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone )
  481. {
  482. GLint fconstLoc;
  483. fconstLoc = m_pBoundPair->m_locFragmentParams;
  484. if ( fconstLoc >= 0 )
  485. {
  486. const int nMaxUsedShaderSlots = m_drawingProgram[kGLMFragmentProgram]->m_descs[kGLMGLSL].m_highWater;
  487. int firstDirtySlot = m_programParamsF[kGLMFragmentProgram].m_firstDirtySlotNonBone;
  488. int dirtySlotHighWater = MIN( nMaxUsedShaderSlots, m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone );
  489. if ( dirtySlotHighWater > firstDirtySlot )
  490. {
  491. #if GL_BATCH_TELEMETRY_ZONES
  492. tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "PSUniformUpdate %u %u", firstDirtySlot, dirtySlotHighWater );
  493. #endif
  494. gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMFragmentProgram][firstDirtySlot], dirtySlotHighWater - firstDirtySlot, &m_programParamsF[kGLMFragmentProgram].m_values[firstDirtySlot][0] );
  495. GL_BATCH_PERF( m_nTotalPSUniformCalls++; )
  496. GL_BATCH_PERF( m_nTotalPSUniformsSet += dirtySlotHighWater - firstDirtySlot; )
  497. GL_BATCH_PERF( m_FlushStats.m_nFirstPSConstant = firstDirtySlot; )
  498. GL_BATCH_PERF( m_FlushStats.m_nNumPSConstants += (dirtySlotHighWater - firstDirtySlot); )
  499. }
  500. m_programParamsF[kGLMFragmentProgram].m_firstDirtySlotNonBone = 256;
  501. m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone = 0;
  502. }
  503. }
  504. return;
  505. flush_error_exit:
  506. m_pBoundPair = NULL;
  507. m_bDirtyPrograms = true;
  508. return;
  509. }