summaryrefslogtreecommitdiff
path: root/togl/linuxwin/glmgr_flush.inl
blob: fcddd616f72cbf7247760ba59c06209891cec470 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// 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).

FORCEINLINE uint32 bitmix32(uint32 a)
{
	a -= (a<<6);
	//a ^= (a>>17);
	//a -= (a<<9);
	a ^= (a<<4);
	//a -= (a<<3);
	//a ^= (a<<10);
	a ^= (a>>15);
	return a;
}

#ifndef OSX

FORCEINLINE GLuint GLMContext::FindSamplerObject( const GLMTexSamplingParams &desiredParams )
{
	int h = bitmix32( desiredParams.m_bits + desiredParams.m_borderColor ) & ( cSamplerObjectHashSize - 1 );
	while ( ( m_samplerObjectHash[h].m_params.m_bits != desiredParams.m_bits ) || ( m_samplerObjectHash[h].m_params.m_borderColor != desiredParams.m_borderColor ) )
	{
		if ( !m_samplerObjectHash[h].m_params.m_packed.m_isValid )
			break;
		if ( ++h >= cSamplerObjectHashSize )
			h = 0;
	}

	if ( !m_samplerObjectHash[h].m_params.m_packed.m_isValid )
	{
		GLMTexSamplingParams &hashParams = m_samplerObjectHash[h].m_params;
		hashParams = desiredParams;
		hashParams.SetToSamplerObject( m_samplerObjectHash[h].m_samplerObject );
		if ( ++m_nSamplerObjectHashNumEntries == cSamplerObjectHashSize )
		{
			// TODO: Support resizing
			Error( "Sampler object hash is full, increase cSamplerObjectHashSize" );
		}
	}

	return m_samplerObjectHash[h].m_samplerObject;
}

#endif // !OSX

// BE VERY CAREFUL WHAT YOU DO IN HERE. This is called on every batch, even seemingly simple changes can kill perf.
FORCEINLINE void GLMContext::FlushDrawStates( uint nStartIndex, uint nEndIndex, uint nBaseVertex )	// shadersOn = true for draw calls, false for clear calls
{
	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)
	Assert( ( m_drawingFBO == m_boundDrawFBO ) && ( m_drawingFBO == m_boundReadFBO ) ); // this check MUST succeed
	Assert( m_pDevice->m_pVertDecl );

#if GLMDEBUG
	GLM_FUNC;
#endif

	GL_BATCH_PERF( m_FlushStats.m_nTotalBatchFlushes++; )

#if GLMDEBUG
	bool tex0_srgb = (m_boundDrawFBO[0].m_attach[0].m_tex->m_layout->m_key.m_texFlags & kGLMTexSRGB) != 0;

	// you can only actually use the sRGB FB state on some systems.. check caps
	if (m_caps.m_hasGammaWrites)
	{
		GLBlendEnableSRGB_t	writeSRGBState;
		m_BlendEnableSRGB.Read( &writeSRGBState, 0 );	// the client set value, not the API-written value yet..
		bool draw_srgb = writeSRGBState.enable != 0;

		if (draw_srgb)
		{
			if (tex0_srgb)
			{
				// good - draw mode and color tex agree
			}
			else
			{
				// bad

				// Client has asked to write sRGB into a texture that can't do it.
				// there is no way to satisfy this unless we change the RT tex and we avoid doing that.
				// (although we might consider a ** ONE TIME ** promotion.
				// this shouldn't be a big deal if the tex format is one where it doesn't matter like 32F.

				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 ));

				// do we shoot down the srgb-write state for this batch?
				// I think the runtime will just ignore it.
			}
		}
		else
		{
			if (tex0_srgb)
			{
				// odd - client is not writing sRGB into a texture which *can* do it.
				//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 ));
				//writeSRGBState.enable = true;
				//m_BlendEnableSRGB.Write( &writeSRGBState );
			}
			else
			{
				// good - draw mode and color tex agree
			}
		}
	}
#endif

	Assert( m_drawingProgram[ kGLMVertexProgram ] );
	Assert( m_drawingProgram[ kGLMFragmentProgram ] );

	Assert( ( m_drawingProgram[kGLMVertexProgram]->m_type == kGLMVertexProgram ) && ( m_drawingProgram[kGLMFragmentProgram]->m_type == kGLMFragmentProgram ) );
	Assert( m_drawingProgram[ kGLMVertexProgram ]->m_bTranslatedProgram && m_drawingProgram[ kGLMFragmentProgram ]->m_bTranslatedProgram );
	
#if GLMDEBUG
	// Depth compare mode check
	uint nCurMask = 1, nShaderSamplerMask = m_drawingProgram[kGLMFragmentProgram]->m_samplerMask;
	for ( int nSamplerIndex = 0; nSamplerIndex < GLM_SAMPLER_COUNT; ++nSamplerIndex, nCurMask <<= 1 )
	{
		if ( !m_samplers[nSamplerIndex].m_pBoundTex )
			continue;

		if ( m_samplers[nSamplerIndex].m_pBoundTex->m_layout->m_mipCount == 1 )
		{
			if ( m_samplers[nSamplerIndex].m_samp.m_packed.m_mipFilter == D3DTEXF_LINEAR )
			{
				GLMDebugPrintf( "Sampler %u has mipmap filtering enabled on a texture without mipmaps! (texture name: %s, pixel shader: %s)!\n",
					nSamplerIndex, 
					m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?", 
					m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
			}
		}

		if ( ( nShaderSamplerMask & nCurMask ) == 0 )
			continue;

		if ( m_samplers[nSamplerIndex].m_pBoundTex->m_layout->m_mipCount == 1 )
		{
			if ( m_samplers[nSamplerIndex].m_samp.m_packed.m_mipFilter == D3DTEXF_LINEAR )
			{
				// Note this is not always an error - shadow buffer debug visualization shaders purposely want to read shadow depths (and not do the comparison)
				GLMDebugPrintf( "Sampler %u has mipmap filtering enabled on a texture without mipmaps! (texture name: %s, pixel shader: %s)!\n",
					nSamplerIndex, 
					m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?", 
					m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
			}
		}
				
		bool bSamplerIsDepth = ( m_samplers[nSamplerIndex].m_pBoundTex->m_layout->m_key.m_texFlags & kGLMTexIsDepth ) != 0;
		bool bSamplerShadow = m_samplers[nSamplerIndex].m_samp.m_packed.m_compareMode != 0; 

		bool bShaderShadow = ( m_drawingProgram[kGLMFragmentProgram]->m_nShadowDepthSamplerMask & nCurMask ) != 0;
		
		if ( bShaderShadow )
		{
			// Shader expects shadow depth sampling at this sampler index
			// Must have a depth texture and compare mode must be enabled
			if ( !bSamplerIsDepth || !bSamplerShadow )
			{
				// FIXME: This occasionally occurs in L4D2 when CShaderAPIDx8::ExecuteCommandBuffer() sets the TEXTURE_WHITE texture in the flashlight depth texture slot.
				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",
					nSamplerIndex, bSamplerShadow, bSamplerIsDepth, bShaderShadow,
					m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?", 
					m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
			}
		}
		else 
		{
			// Shader does not expect shadow depth sampling as this sampler index
			// We don't care if comparemode is enabled, but we can't have a depth texture in this sampler
			if ( bSamplerIsDepth )
			{
				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",
					nSamplerIndex, 
					m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel ? m_samplers[nSamplerIndex].m_pBoundTex->m_debugLabel : "?", 
					m_drawingProgram[kGLMFragmentProgram]->m_shaderName );
			}
		}
	}
#endif

	if ( m_bDirtyPrograms )
	{
		m_bDirtyPrograms = false;

		CGLMShaderPair *pNewPair = m_pairCache->SelectShaderPair( m_drawingProgram[ kGLMVertexProgram ], m_drawingProgram[ kGLMFragmentProgram ], 0 );

		if ( pNewPair != m_pBoundPair )
		{
#if GL_BATCH_TELEMETRY_ZONES
			tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "NewProgram" );
#endif

			if ( !pNewPair->m_valid )
			{
				if ( !pNewPair->ValidateProgramPair() )
				{
					goto flush_error_exit;
				}
			}

			gGL->glUseProgram( (GLuint)pNewPair->m_program );
			
			GL_BATCH_PERF( m_FlushStats.m_nTotalProgramPairChanges++; )

			if ( !m_pBoundPair )
			{
				GL_BATCH_PERF( m_FlushStats.m_nNewPS++; )
				GL_BATCH_PERF( m_FlushStats.m_nNewVS++; )
			}
			else 
			{
				GL_BATCH_PERF( if ( pNewPair->m_fragmentProg != m_pBoundPair->m_fragmentProg ) m_FlushStats.m_nNewPS++; )
				GL_BATCH_PERF( if ( pNewPair->m_vertexProg != m_pBoundPair->m_vertexProg ) m_FlushStats.m_nNewVS++; )
			}

#if GL_BATCH_PERF_ANALYSIS
			tmMessage( TELEMETRY_LEVEL2, TMMF_ICON_NOTE, "V:%s (V Regs:%u V Bone Regs:%u) F:%s (F Regs:%u)", 
				m_drawingProgram[ kGLMVertexProgram ]->m_shaderName,
				m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_highWater, 
				m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_VSHighWaterBone, 
				m_drawingProgram[ kGLMFragmentProgram ]->m_shaderName, 
				m_drawingProgram[ kGLMFragmentProgram ]->m_descs[kGLMGLSL].m_highWater );
#endif

			m_pBoundPair = pNewPair;

			// set the dirty levels appropriately since the program changed and has never seen any of the current values.
			m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone = 0;
			m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone = m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_highWater;
			m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone = m_drawingProgram[ kGLMVertexProgram ]->m_descs[kGLMGLSL].m_VSHighWaterBone;

			m_programParamsF[kGLMFragmentProgram].m_firstDirtySlotNonBone = 0;
			m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone = m_drawingProgram[ kGLMFragmentProgram ]->m_descs[kGLMGLSL].m_highWater;

			// bool and int dirty levels get set to max, we don't have actual high water marks for them
			// code which sends the values must clamp on these types.
			m_programParamsB[kGLMVertexProgram].m_dirtySlotCount = kGLMProgramParamBoolLimit;
			m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount = kGLMProgramParamBoolLimit;

			m_programParamsI[kGLMVertexProgram].m_dirtySlotCount = kGLMProgramParamInt4Limit;
			m_programParamsI[kGLMFragmentProgram].m_dirtySlotCount = 0;

			// check fragment buffers used (MRT)
			if( pNewPair->m_fragmentProg->m_fragDataMask != m_fragDataMask )
			{
				gGL->glDrawBuffers( pNewPair->m_fragmentProg->m_numDrawBuffers, pNewPair->m_fragmentProg->m_drawBuffers );
				m_fragDataMask = pNewPair->m_fragmentProg->m_fragDataMask;
			}
		}
	}

	Assert( m_ViewportBox.GetData().width == (int)( m_ViewportBox.GetData().widthheight & 0xFFFF ) );
	Assert( m_ViewportBox.GetData().height == (int)( m_ViewportBox.GetData().widthheight >> 16 ) );

	m_pBoundPair->UpdateScreenUniform( m_ViewportBox.GetData().widthheight );
	
	GL_BATCH_PERF( m_FlushStats.m_nNumChangedSamplers += m_nNumDirtySamplers );

#if !defined( OSX ) // no support for sampler objects in OSX 10.6 (GL 2.1 profile)
	if ( m_bUseSamplerObjects)
	{
		while ( m_nNumDirtySamplers )
		{
			const uint nSamplerIndex = m_nDirtySamplers[--m_nNumDirtySamplers];
			Assert( ( nSamplerIndex < GLM_SAMPLER_COUNT ) && ( !m_nDirtySamplerFlags[nSamplerIndex]) );

			m_nDirtySamplerFlags[nSamplerIndex] = 1;

			gGL->glBindSampler( nSamplerIndex, FindSamplerObject( m_samplers[nSamplerIndex].m_samp ) );

			GL_BATCH_PERF( m_FlushStats.m_nNumSamplingParamsChanged++ );

#if defined( OSX ) // valid for OSX only if using GL 3.3 context 
			CGLMTex *pTex = m_samplers[nSamplerIndex].m_pBoundTex;

			if( pTex && !( gGL->m_bHave_GL_EXT_texture_sRGB_decode ) )
			{
				// see if requested SRGB state differs from the known one
				bool texSRGB = ( pTex->m_layout->m_key.m_texFlags & kGLMTexSRGB ) != 0;
				bool glSampSRGB  = m_samplers[nSamplerIndex].m_samp.m_packed.m_srgb;

				if ( texSRGB != glSampSRGB ) // mismatch
				{
					pTex->HandleSRGBMismatch( glSampSRGB, pTex->m_srgbFlipCount );
				}
			}
#endif
		}
	}
	else
#endif // if !defined( OSX )
	{
		while ( m_nNumDirtySamplers )
		{
			const uint nSamplerIndex = m_nDirtySamplers[--m_nNumDirtySamplers];
			Assert( ( nSamplerIndex < GLM_SAMPLER_COUNT ) && ( !m_nDirtySamplerFlags[nSamplerIndex]) );

			m_nDirtySamplerFlags[nSamplerIndex] = 1;

			CGLMTex *pTex = m_samplers[nSamplerIndex].m_pBoundTex;

			if ( ( pTex ) && ( !( pTex->m_SamplingParams == m_samplers[nSamplerIndex].m_samp ) ) )
			{
				SelectTMU( nSamplerIndex );

				m_samplers[nSamplerIndex].m_samp.DeltaSetToTarget( pTex->m_texGLTarget, pTex->m_SamplingParams );

				pTex->m_SamplingParams = m_samplers[nSamplerIndex].m_samp;

#if defined( OSX )
				if( pTex && !( gGL->m_bHave_GL_EXT_texture_sRGB_decode ) )
				{
					// see if requested SRGB state differs from the known one
					bool texSRGB = ( pTex->m_layout->m_key.m_texFlags & kGLMTexSRGB ) != 0;
					bool glSampSRGB  = m_samplers[nSamplerIndex].m_samp.m_packed.m_srgb;

					if ( texSRGB != glSampSRGB ) // mismatch
					{
						pTex->HandleSRGBMismatch( glSampSRGB, pTex->m_srgbFlipCount );
					}	
				}
#endif
			}
		}
	}

	// vertex stage --------------------------------------------------------------------
	if ( m_bUseBoneUniformBuffers )
	{
		// vertex stage --------------------------------------------------------------------
		if ( m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone )
		{
			int firstDirtySlot = m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone;
			int dirtySlotHighWater = MIN( m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_highWater, m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone );

			GLint vconstLoc = m_pBoundPair->m_locVertexParams;
			if ( ( vconstLoc >= 0 ) && ( dirtySlotHighWater > firstDirtySlot ) )
			{
#if GL_BATCH_TELEMETRY_ZONES
				tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "VSNonBoneUniformUpdate %u %u", firstDirtySlot, dirtySlotHighWater );
#endif
				int numSlots = dirtySlotHighWater - DXABSTRACT_VS_FIRST_BONE_SLOT;
				
				// consts after the bones (c217 onwards), since we use the concatenated destination array vc[], upload these consts starting from vc[58]
				if( numSlots > 0 )
				{
					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] );

					dirtySlotHighWater = DXABSTRACT_VS_FIRST_BONE_SLOT;

					GL_BATCH_PERF( m_nTotalVSUniformCalls++; )
					GL_BATCH_PERF( m_nTotalVSUniformsSet += numSlots; )

					GL_BATCH_PERF( m_FlushStats.m_nFirstVSConstant = DXABSTRACT_VS_FIRST_BONE_SLOT; )
					GL_BATCH_PERF( m_FlushStats.m_nNumVSConstants += numSlots; )
				}
				
				numSlots = dirtySlotHighWater - firstDirtySlot;

				// consts before the bones (c0-c57)
				if( numSlots > 0 )
				{
					gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMVertexProgram][firstDirtySlot], dirtySlotHighWater - firstDirtySlot, &m_programParamsF[kGLMVertexProgram].m_values[firstDirtySlot][0] );

					GL_BATCH_PERF( m_nTotalVSUniformCalls++; )
					GL_BATCH_PERF( m_nTotalVSUniformsSet += dirtySlotHighWater - firstDirtySlot; )

					GL_BATCH_PERF( m_FlushStats.m_nFirstVSConstant = firstDirtySlot; )
					GL_BATCH_PERF( m_FlushStats.m_nNumVSConstants += (dirtySlotHighWater - firstDirtySlot); )
				}
			}

			m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone = 256;
			m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone = 0;
		}

		if ( m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone )
		{
			const GLint vconstBoneLoc = m_pBoundPair->m_locVertexBoneParams;
			if ( vconstBoneLoc >= 0 )
			{
				int shaderSlotsBone = 0;
				if ( ( m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_VSHighWaterBone > 0 ) && ( m_nMaxUsedVertexProgramConstantsHint > DXABSTRACT_VS_FIRST_BONE_SLOT ) )
				{
					shaderSlotsBone = MIN( m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_VSHighWaterBone, m_nMaxUsedVertexProgramConstantsHint - DXABSTRACT_VS_FIRST_BONE_SLOT );
				}

				int dirtySlotHighWaterBone = MIN( shaderSlotsBone, m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone );
				if ( dirtySlotHighWaterBone )
				{
					uint nNumBoneRegs = dirtySlotHighWaterBone;

#if GL_BATCH_TELEMETRY_ZONES								
					tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "VSBoneUniformUpdate %u", nNumBoneRegs );
#endif

					gGL->glUniform4fv( vconstBoneLoc, nNumBoneRegs, &m_programParamsF[kGLMVertexProgram].m_values[DXABSTRACT_VS_FIRST_BONE_SLOT][0] );

					GL_BATCH_PERF( m_nTotalVSUniformBoneCalls++; )
					GL_BATCH_PERF( m_nTotalVSUniformsBoneSet += nNumBoneRegs; )
					GL_BATCH_PERF( m_FlushStats.m_nNumVSBoneConstants += nNumBoneRegs; )
				}

				m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterBone = 0;
			}
		}

	}
	else
	{
		if ( m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone )
		{
			const int nMaxUsedShaderSlots = m_drawingProgram[kGLMVertexProgram]->m_descs[kGLMGLSL].m_highWater;

			int firstDirtySlot = m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone;
			int dirtySlotHighWater = MIN( nMaxUsedShaderSlots, m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone );

			GLint vconstLoc = m_pBoundPair->m_locVertexParams;
			if ( ( vconstLoc >= 0 ) && ( dirtySlotHighWater > firstDirtySlot ) )
			{
	#if GL_BATCH_TELEMETRY_ZONES
				tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "VSNonBoneUniformUpdate %u %u", firstDirtySlot, dirtySlotHighWater );
	#endif
				gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMVertexProgram][firstDirtySlot], dirtySlotHighWater - firstDirtySlot, &m_programParamsF[kGLMVertexProgram].m_values[firstDirtySlot][0] );

				GL_BATCH_PERF( m_nTotalVSUniformCalls++; )
				GL_BATCH_PERF( m_nTotalVSUniformsSet += dirtySlotHighWater - firstDirtySlot; )

				GL_BATCH_PERF( m_FlushStats.m_nFirstVSConstant = firstDirtySlot; )
				GL_BATCH_PERF( m_FlushStats.m_nNumVSConstants += (dirtySlotHighWater - firstDirtySlot); )
			}

			m_programParamsF[kGLMVertexProgram].m_firstDirtySlotNonBone = 256;
			m_programParamsF[kGLMVertexProgram].m_dirtySlotHighWaterNonBone = 0;
		}
	}


	// see if VS uses i0, b0, b1, b2, b3.
	// use a glUniform1i to set any one of these if active.  skip all of them if no dirties reported.
	// my kingdom for the UBO extension!

	// ------- bools ---------- //
	if ( m_pBoundPair->m_bHasBoolOrIntUniforms )
	{
		if ( m_programParamsB[kGLMVertexProgram].m_dirtySlotCount )	// optimize this later after the float param pushes are proven out
		{
			const uint nLimit = MIN( CGLMShaderPair::cMaxVertexShaderBoolUniforms, m_programParamsB[kGLMVertexProgram].m_dirtySlotCount );
			for ( uint i = 0; i < nLimit; ++i )
			{
				GLint constBoolLoc = m_pBoundPair->m_locVertexBool[i];
				if ( constBoolLoc >= 0 )
					gGL->glUniform1i( constBoolLoc, m_programParamsB[kGLMVertexProgram].m_values[i] );
			}

			m_programParamsB[kGLMVertexProgram].m_dirtySlotCount = 0;
		}

		if ( m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount )	// optimize this later after the float param pushes are proven out
		{
			const uint nLimit = MIN( CGLMShaderPair::cMaxFragmentShaderBoolUniforms, m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount );
			for ( uint i = 0; i < nLimit; ++i )
			{
				GLint constBoolLoc = m_pBoundPair->m_locFragmentBool[i];
				if ( constBoolLoc >= 0 )
					gGL->glUniform1i( constBoolLoc, m_programParamsB[kGLMFragmentProgram].m_values[i] );
			}

			m_programParamsB[kGLMFragmentProgram].m_dirtySlotCount = 0;
		}

		if ( m_programParamsI[kGLMVertexProgram].m_dirtySlotCount )
		{
			GLint vconstInt0Loc = m_pBoundPair->m_locVertexInteger0;									//glGetUniformLocationARB( prog, "i0");
			if ( vconstInt0Loc >= 0 )
			{
				gGL->glUniform1i( vconstInt0Loc, m_programParamsI[kGLMVertexProgram].m_values[0][0] );	//FIXME magic number
			}
			m_programParamsI[kGLMVertexProgram].m_dirtySlotCount = 0;
		}
	}

	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 ) ) );
	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 ) ) );
	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 ) ) );
	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 ) ) );

	uint nCurTotalBufferRevision;
	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;

	// 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.
	// 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).
	if ( ( nCurTotalBufferRevision != m_CurAttribs.m_nTotalBufferRevision ) ||
		( m_CurAttribs.m_pVertDecl != m_pDevice->m_pVertDecl ) ||
		( m_CurAttribs.m_vtxAttribMap[0] != reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[0] ) ||
		( m_CurAttribs.m_vtxAttribMap[1] != reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[1] ) ||
		( memcmp( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) ) != 0 ) )
	{
		// This branch is taken 52.2% of the time in the L4D2 test1 (long) timedemo.

#if GL_BATCH_TELEMETRY_ZONES
		tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "SetVertexAttribs" );
#endif

		m_CurAttribs.m_nTotalBufferRevision = nCurTotalBufferRevision;
		m_CurAttribs.m_pVertDecl = m_pDevice->m_pVertDecl;
		m_CurAttribs.m_vtxAttribMap[0] = reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[0];
		m_CurAttribs.m_vtxAttribMap[1] = reinterpret_cast<const uint64 *>(m_pDevice->m_vertexShader->m_vtxAttribMap)[1];
		memcpy( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) );

		unsigned char *pVertexShaderAttribMap = m_pDevice->m_vertexShader->m_vtxAttribMap;
		const int nMaxVertexAttributesToCheck = m_drawingProgram[ kGLMVertexProgram ]->m_maxVertexAttrs;

		IDirect3DVertexDeclaration9	*pVertDecl = m_pDevice->m_pVertDecl;
		const uint8	*pVertexAttribDescToStreamIndex = pVertDecl->m_VertexAttribDescToStreamIndex;

		for( int nMask = 1, nIndex = 0; nIndex < nMaxVertexAttributesToCheck; ++nIndex, nMask <<= 1 )
		{
			uint8 vertexShaderAttrib = pVertexShaderAttribMap[ nIndex ];

			uint nDeclIndex = pVertexAttribDescToStreamIndex[vertexShaderAttrib];
			if ( nDeclIndex == 0xFF )
			{
				// Not good - the vertex shader has an attribute which can't be located in the decl! 
				// The D3D9 debug runtime is also going to complain.
				Assert( 0 );

				if ( m_lastKnownVertexAttribMask & nMask )
				{
					m_lastKnownVertexAttribMask &= ~nMask;
					gGL->glDisableVertexAttribArray( nIndex );
				}
				continue;
			}

			D3DVERTEXELEMENT9_GL *pDeclElem = &pVertDecl->m_elements[nDeclIndex];

			Assert( ( ( vertexShaderAttrib >> 4 ) == pDeclElem->m_dxdecl.Usage ) && ( ( vertexShaderAttrib & 0x0F ) == pDeclElem->m_dxdecl.UsageIndex) );

			const uint nStreamIndex = pDeclElem->m_dxdecl.Stream;
			const D3DStreamDesc *pStream = &m_pDevice->m_streams[ nStreamIndex ];

			CGLMBuffer *pBuf = m_pDevice->m_vtx_buffers[ nStreamIndex ];
			if ( pBuf == m_pDevice->m_pDummy_vtx_buffer )
			{
				Assert( pStream->m_vtxBuffer == NULL );

				// this shader doesn't use that pair.
				if ( m_lastKnownVertexAttribMask & nMask )
				{
					m_lastKnownVertexAttribMask &= ~nMask;
					gGL->glDisableVertexAttribArray( nIndex );
				}
				continue;
			}
			Assert( pStream->m_vtxBuffer->m_vtxBuffer == pBuf );

			int nBufOffset = pDeclElem->m_gldecl.m_offset + pStream->m_offset;
			Assert( nBufOffset >= 0 );
			Assert( nBufOffset < (int)pBuf->m_nSize );
			if ( pBuf->m_bUsingPersistentBuffer )
			{
				nBufOffset += pBuf->m_nPersistentBufferStartOffset;
			}

			SetBufAndVertexAttribPointer( nIndex, pBuf->GetHandle(), 
				pStream->m_stride, pDeclElem->m_gldecl.m_datatype, pDeclElem->m_gldecl.m_normalized, pDeclElem->m_gldecl.m_nCompCount, 
				reinterpret_cast< const GLvoid * >( reinterpret_cast< int >( pBuf->m_pPseudoBuf ) + nBufOffset ), 
				pBuf->m_nRevision );

			if ( !( m_lastKnownVertexAttribMask & nMask ) )
			{
				m_lastKnownVertexAttribMask |= nMask;
				gGL->glEnableVertexAttribArray( nIndex );
			}
		}

		for( int nIndex = nMaxVertexAttributesToCheck; nIndex < m_nNumSetVertexAttributes; nIndex++ )
		{
			gGL->glDisableVertexAttribArray( nIndex );
			m_lastKnownVertexAttribMask &= ~(1 << nIndex);
		}

		m_nNumSetVertexAttributes = nMaxVertexAttributesToCheck;
	}

	// fragment stage --------------------------------------------------------------------
	if ( m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone )
	{
		GLint fconstLoc;
		fconstLoc = m_pBoundPair->m_locFragmentParams;
		if ( fconstLoc >= 0 )
		{
			const int nMaxUsedShaderSlots = m_drawingProgram[kGLMFragmentProgram]->m_descs[kGLMGLSL].m_highWater;

			int firstDirtySlot = m_programParamsF[kGLMFragmentProgram].m_firstDirtySlotNonBone;
			int dirtySlotHighWater = MIN( nMaxUsedShaderSlots, m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone );

			if ( dirtySlotHighWater > firstDirtySlot )
			{
#if GL_BATCH_TELEMETRY_ZONES
				tmZone( TELEMETRY_LEVEL2, TMZF_NONE, "PSUniformUpdate %u %u", firstDirtySlot, dirtySlotHighWater );
#endif

				gGL->glUniform4fv( m_pBoundPair->m_UniformBufferParams[kGLMFragmentProgram][firstDirtySlot], dirtySlotHighWater - firstDirtySlot, &m_programParamsF[kGLMFragmentProgram].m_values[firstDirtySlot][0] );

				GL_BATCH_PERF( m_nTotalPSUniformCalls++; )
				GL_BATCH_PERF( m_nTotalPSUniformsSet += dirtySlotHighWater - firstDirtySlot; )

				GL_BATCH_PERF( m_FlushStats.m_nFirstPSConstant = firstDirtySlot; )
				GL_BATCH_PERF( m_FlushStats.m_nNumPSConstants += (dirtySlotHighWater - firstDirtySlot); )
			}
			m_programParamsF[kGLMFragmentProgram].m_firstDirtySlotNonBone = 256;
			m_programParamsF[kGLMFragmentProgram].m_dirtySlotHighWaterNonBone = 0;
		}
	}

	return;

flush_error_exit:
	m_pBoundPair = NULL;
	m_bDirtyPrograms = true;
	return;
}