summaryrefslogtreecommitdiff
path: root/utils/xbox/vxconsole/tex_profile.cpp
blob: 8c3ddf11e4a57ca5c8909cb9ec388a2303c5b2d9 (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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//	TEX_PROFILE.CPP
//
//	Texture Profiling Display.
//=====================================================================================//
#include "vxconsole.h"

#define TEXPROFILE_MAXCOUNTERS				64
#define TEXPROFILE_MAXSAMPLES				512

#define TEXPROFILE_MAJORTICKSIZE			4
#define TEXPROFILE_MAJORTICKBYTES			( TEXPROFILE_MAJORTICKSIZE*1024.0f*1024.0f )

#define TEXPROFILE_HISTORY_MAJORTICKHEIGHT	100
#define TEXPROFILE_HISTORY_NUMMINORTICKS	3
#define TEXPROFILE_HISTORY_LABELWIDTH		50
#define TEXPROFILE_HISTORY_SCALESTEPS		5
#define TEXPROFILE_HISTORY_MINSCALE			0.3f
#define TEXPROFILE_HISTORY_MAXSCALE			3.0f

#define TEXPROFILE_SAMPLES_ITEMHEIGHT		15
#define TEXPROFILE_SAMPLES_BARHEIGHT		10
#define TEXPROFILE_SAMPLES_MAJORTICKWIDTH	200
#define TEXPROFILE_SAMPLES_LABELWIDTH		150
#define TEXPROFILE_SAMPLES_LABELGAP			5
#define TEXPROFILE_SAMPLES_NUMMINORTICKS	3
#define TEXPROFILE_SAMPLES_PEAKHOLDTIME		3000
#define TEXPROFILE_SAMPLES_SCALESTEPS		10
#define TEXPROFILE_SAMPLES_MINSCALE			0.3f
#define TEXPROFILE_SAMPLES_MAXSCALE			3.0f

#define ID_TEXPROFILE_SAMPLES			1
#define ID_TEXPROFILE_HISTORY			2

typedef struct
{
	unsigned int	samples[TEXPROFILE_MAXSAMPLES];
	unsigned int	peakSample;
	char			label[64];
	COLORREF		color;
} profileCounter_t;

HWND				g_texProfile_hWndSamples;
HWND				g_texProfile_hWndHistory;
int					g_texProfile_numCounters;
profileCounter_t	g_texProfile_counters[TEXPROFILE_MAXCOUNTERS];
RECT				g_texProfile_samplesWindowRect;
RECT				g_texProfile_historyWindowRect;
DWORD				g_texProfile_lastPeakTime;
bool				g_texProfile_history_tickMarks = true;
bool				g_texProfile_history_colors = true;
int					g_texProfile_history_scale;
bool				g_texProfile_samples_tickMarks = true;
bool				g_texProfile_samples_colors = true;
int					g_texProfile_samples_scale;
int					g_texProfile_numSamples;
int					g_texProfile_currentFrame;

//-----------------------------------------------------------------------------
//	TexProfile_LoadConfig	
// 
//-----------------------------------------------------------------------------
void TexProfile_LoadConfig()
{
	int		numArgs;
	char	buff[256];

	// profile samples
	Sys_GetRegistryString( "texProfileSamplesWindowRect", buff, "", sizeof( buff ) );
	numArgs = sscanf( buff, "%d %d %d %d", &g_texProfile_samplesWindowRect.left, &g_texProfile_samplesWindowRect.top, &g_texProfile_samplesWindowRect.right, &g_texProfile_samplesWindowRect.bottom );
	if ( numArgs != 4 || g_texProfile_samplesWindowRect.left < 0 || g_texProfile_samplesWindowRect.top < 0 || g_texProfile_samplesWindowRect.right < 0 || g_texProfile_samplesWindowRect.bottom < 0 )
		memset( &g_texProfile_samplesWindowRect, 0, sizeof( g_texProfile_samplesWindowRect ) );
	Sys_GetRegistryInteger( "texProfileSamplesScale", 0, g_texProfile_samples_scale );
	if ( g_texProfile_samples_scale < -TEXPROFILE_SAMPLES_SCALESTEPS || g_texProfile_samples_scale > TEXPROFILE_SAMPLES_SCALESTEPS )
		g_texProfile_samples_scale = 0;

	// profile history
	Sys_GetRegistryString( "texProfileHistoryWindowRect", buff, "", sizeof( buff ) );
	numArgs = sscanf( buff, "%d %d %d %d", &g_texProfile_historyWindowRect.left, &g_texProfile_historyWindowRect.top, &g_texProfile_historyWindowRect.right, &g_texProfile_historyWindowRect.bottom );
	if ( numArgs != 4 || g_texProfile_historyWindowRect.left < 0 || g_texProfile_historyWindowRect.top < 0 || g_texProfile_historyWindowRect.right < 0 || g_texProfile_historyWindowRect.bottom < 0 )
		memset( &g_texProfile_historyWindowRect, 0, sizeof( g_texProfile_historyWindowRect ) );
	Sys_GetRegistryInteger( "texProfileHistoryScale", 0, g_texProfile_history_scale );
	if ( g_texProfile_history_scale < -TEXPROFILE_HISTORY_SCALESTEPS || g_texProfile_history_scale > TEXPROFILE_HISTORY_SCALESTEPS )
		g_texProfile_history_scale = 0;

	Sys_GetRegistryInteger( "texProfileCurrentFrame", 0, g_texProfile_currentFrame );
}

//-----------------------------------------------------------------------------
//	TexProfile_SaveConfig
// 
//-----------------------------------------------------------------------------
void TexProfile_SaveConfig()
{
	char			buff[256];
	WINDOWPLACEMENT wp;

	// profile samples
	if ( g_texProfile_hWndSamples )
	{
		memset( &wp, 0, sizeof( wp ) );
		wp.length = sizeof( WINDOWPLACEMENT );
		GetWindowPlacement( g_texProfile_hWndSamples, &wp );
		g_texProfile_samplesWindowRect = wp.rcNormalPosition;
		sprintf( buff, "%d %d %d %d", wp.rcNormalPosition.left, wp.rcNormalPosition.top, wp.rcNormalPosition.right, wp.rcNormalPosition.bottom );
		Sys_SetRegistryString( "texProfileSamplesWindowRect", buff );
	}
	Sys_SetRegistryInteger( "texProfileSamplesScale", g_texProfile_samples_scale );

	// profile history
	if ( g_texProfile_hWndHistory )
	{
		memset( &wp, 0, sizeof( wp ) );
		wp.length = sizeof( WINDOWPLACEMENT );
		GetWindowPlacement( g_texProfile_hWndHistory, &wp );
		g_texProfile_historyWindowRect = wp.rcNormalPosition;
		sprintf( buff, "%d %d %d %d", wp.rcNormalPosition.left, wp.rcNormalPosition.top, wp.rcNormalPosition.right, wp.rcNormalPosition.bottom );
		Sys_SetRegistryString( "texProfileHistoryWindowRect", buff );
	}
	Sys_SetRegistryInteger( "texProfileHistoryScale", g_texProfile_history_scale );

	Sys_SetRegistryInteger( "texProfileCurrentFrame", g_texProfile_currentFrame );
}

//-----------------------------------------------------------------------------
//	TexProfile_SetTitle
// 
//-----------------------------------------------------------------------------
void TexProfile_SetTitle()
{
	char	titleBuff[128];

	if ( g_texProfile_hWndSamples )
	{
		strcpy( titleBuff, "D3D Usage Snapshot" );
		if ( VProf_GetState() == VPROF_TEXTURE || VProf_GetState() == VPROF_TEXTUREFRAME )
			strcat( titleBuff, " [ON]" );
		if ( g_texProfile_currentFrame )
			strcat( titleBuff, " [FRAME]" );

		SetWindowText( g_texProfile_hWndSamples, titleBuff );
	}

	if ( g_texProfile_hWndHistory )
	{
		strcpy( titleBuff, "D3D Usage History" );
		if ( VProf_GetState() == VPROF_TEXTURE || VProf_GetState() == VPROF_TEXTUREFRAME )
			strcat( titleBuff, " [ON]" );
		if ( g_texProfile_currentFrame )
			strcat( titleBuff, " [FRAME]" );

		SetWindowText( g_texProfile_hWndHistory, titleBuff );
	}
}

//-----------------------------------------------------------------------------
// TexProfile_UpdateWindow
//
//-----------------------------------------------------------------------------
void TexProfile_UpdateWindow()
{
	if ( g_texProfile_hWndSamples && !IsIconic( g_texProfile_hWndSamples ) )
	{
		// visible - force a client repaint
		InvalidateRect( g_texProfile_hWndSamples, NULL, true );
	}

	if ( g_texProfile_hWndHistory && !IsIconic( g_texProfile_hWndHistory ) )
	{
		// visible - force a client repaint
		InvalidateRect( g_texProfile_hWndHistory, NULL, true );
	}
}

//-----------------------------------------------------------------------------
// rc_SetTexProfile
//
//-----------------------------------------------------------------------------
int rc_SetTexProfile( char* commandPtr )
{
	int				i;
	char*			cmdToken;
	int				retAddr;
	int				retVal;
	int				errCode = -1;
	xrProfile_t*	localList;
	int				profileList;
	int				numProfiles;

	// get numProfiles
	cmdToken = GetToken( &commandPtr );
	if ( !cmdToken[0] )
		goto cleanUp;
	sscanf( cmdToken,"%x",&numProfiles );

	// get profile attributes
	cmdToken = GetToken( &commandPtr );
	if ( !cmdToken[0] )
		goto cleanUp;
	sscanf( cmdToken, "%x", &profileList );

	// get retAddr
	cmdToken = GetToken( &commandPtr );
	if ( !cmdToken[0] )
		goto cleanUp;
	sscanf( cmdToken,"%x",&retAddr );

	localList = new xrProfile_t[numProfiles];
	memset( localList, 0, numProfiles*sizeof( xrProfile_t ) );

	// get the caller's profile list 
	DmGetMemory( ( void* )profileList, numProfiles*sizeof( xrProfile_t ), localList, NULL );

	g_texProfile_numCounters = numProfiles;
	if ( g_texProfile_numCounters > TEXPROFILE_MAXCOUNTERS-1 )
		g_texProfile_numCounters = TEXPROFILE_MAXCOUNTERS-1;
	
	for ( i=0; i<g_texProfile_numCounters; i++ )
	{
		// swap the structure
		localList[i].color = BigDWord( localList[i].color );

		// clear the old counter
		memset( &g_texProfile_counters[i], 0, sizeof( profileCounter_t ) );

		V_strncpy( g_texProfile_counters[i].label, localList[i].labelString, sizeof( g_texProfile_counters[i].label ) );
		g_texProfile_counters[i].color = localList[i].color;
	}

	// build out the reserved last counter as total count
	memset( &g_texProfile_counters[g_texProfile_numCounters], 0, sizeof( profileCounter_t ) );
	strcpy( g_texProfile_counters[g_texProfile_numCounters].label, "Total" );
	g_texProfile_counters[i].color = RGB( 255,255,255 );
	g_texProfile_numCounters++;
	
	// set the return code
	retVal = g_texProfile_numCounters-1;
	int xboxRetVal = BigDWord( retVal );
	DmSetMemory( ( void* )retAddr, sizeof( int ), &xboxRetVal, NULL );

	DebugCommand( "0x%8.8x = SetTexProfile( 0x%8.8x, 0x%8.8x )\n", retVal, numProfiles, profileList );

	delete [] localList;

	// success
	errCode = 0;

cleanUp:
	return ( errCode );
}

//-----------------------------------------------------------------------------
// rc_SetTexProfileData
//
//-----------------------------------------------------------------------------
int rc_SetTexProfileData( char* commandPtr )
{
	int				i;
	char*			cmdToken;
	int				errCode = -1;
	int				counters;
	int				currentSample;
	int				total;
	bool			newPeaks;
	unsigned int	localCounters[TEXPROFILE_MAXCOUNTERS];
	DWORD			newTime;

	// get profiles
	cmdToken = GetToken( &commandPtr );
	if ( !cmdToken[0] )
		goto cleanUp;
	sscanf( cmdToken, "%x", &counters );

	// get the caller's profile list 
	if ( g_texProfile_numCounters )
		DmGetMemory( ( void* )counters, ( g_texProfile_numCounters-1 )*sizeof( int ), localCounters, NULL );

	// timeout peaks
	newTime = Sys_GetSystemTime();
	if ( newTime - g_texProfile_lastPeakTime > TEXPROFILE_SAMPLES_PEAKHOLDTIME )
	{
		g_texProfile_lastPeakTime = newTime;
		newPeaks = true;
	} 
	else
		newPeaks = false;

	// next sample
	currentSample = g_texProfile_numSamples % TEXPROFILE_MAXSAMPLES;
	g_texProfile_numSamples++;

	total = 0;
	for ( i=0; i<g_texProfile_numCounters; i++ )
	{
		if ( i != g_texProfile_numCounters-1 )
		{
			g_texProfile_counters[i].samples[currentSample] = localCounters[i];
			total += localCounters[i];
		}
		else
		{
			// reserved total counter
			g_texProfile_counters[i].samples[currentSample] = total;
		}

		if ( newPeaks || g_texProfile_counters[i].peakSample < g_texProfile_counters[i].samples[currentSample] )
			g_texProfile_counters[i].peakSample = g_texProfile_counters[i].samples[currentSample];
	}

	DebugCommand( "SetTexProfileData( 0x%8.8x )\n", counters );

	TexProfile_UpdateWindow();

	// success
	errCode = 0;

cleanUp:
	return ( errCode );
}

//-----------------------------------------------------------------------------
//	TexProfile_ZoomIn
// 
//-----------------------------------------------------------------------------
void TexProfile_ZoomIn( int& scale, int numSteps )
{
	scale++;
	if ( scale > numSteps )
	{
		scale = numSteps;
		return;
	}
	TexProfile_UpdateWindow();
}

//-----------------------------------------------------------------------------
//	TexProfile_ZoomOut
// 
//-----------------------------------------------------------------------------
void TexProfile_ZoomOut( int& scale, int numSteps )
{
	scale--;
	if ( scale < -numSteps )
	{
		scale = -numSteps;
		return;
	}
	TexProfile_UpdateWindow();
}

//-----------------------------------------------------------------------------
//	TexProfile_CalcScale
// 
//-----------------------------------------------------------------------------
float TexProfile_CalcScale( int scale, int numSteps, float min, float max )
{
	float t;

	// from integral scale [-numSteps..numSteps] to float scale [min..max]
	t = ( float )( scale + numSteps )/( float )( 2*numSteps );
	t = min + t*( max-min );

	return t;
}

//-----------------------------------------------------------------------------
//	TexProfileSamples_Draw
// 
//-----------------------------------------------------------------------------
void TexProfileSamples_Draw( HDC hdc, RECT* clientRect )
{
	int		i;
	int		j;
	int		x;
	int		y;
	int		x0;
	int		y0;
	int		w;
	float	t;
	float	scale;
	float	sample;
	char	labelBuff[128];
	HPEN	hBlackPen;
	HPEN	hPenOld;
	HPEN	hGreyPen;
	HBRUSH	hColoredBrush;	    
	HBRUSH	hbrushOld;
	HFONT	hFontOld;
	RECT	rect;
	int		currentSample;
	int		numTicks;
	int		tickWidth;
	int		windowWidth;
	int		windowHeight;

	hBlackPen = CreatePen( PS_SOLID, 1, RGB( 0,0,0 ) );
	hGreyPen  = CreatePen( PS_SOLID, 1, Sys_ColorScale( g_backgroundColor, 0.85f ) );
	hPenOld   = ( HPEN )SelectObject( hdc, hBlackPen );
	hFontOld  = SelectFont( hdc, g_hProportionalFont );

	SetBkColor( hdc, g_backgroundColor );

	// zoom
	scale        = TexProfile_CalcScale( g_texProfile_samples_scale, TEXPROFILE_SAMPLES_SCALESTEPS, TEXPROFILE_SAMPLES_MINSCALE, TEXPROFILE_SAMPLES_MAXSCALE );
	tickWidth    = ( int )( TEXPROFILE_SAMPLES_MAJORTICKWIDTH*scale );
	windowWidth  = clientRect->right-clientRect->left;
	windowHeight = clientRect->bottom-clientRect->top;

	numTicks = ( windowWidth-TEXPROFILE_SAMPLES_LABELWIDTH )/tickWidth + 1;
	if ( numTicks < 0 )
		numTicks = 1;

	rect.left   = 0;
	rect.right  = TEXPROFILE_SAMPLES_LABELWIDTH;
	rect.top    = 0;
	rect.bottom = TEXPROFILE_SAMPLES_ITEMHEIGHT;
	DrawText( hdc, "Name", -1, &rect, DT_LEFT );

	// draw size ticks
	x = TEXPROFILE_SAMPLES_LABELWIDTH;
	y = 0;
	for ( i=0; i<numTicks; i++ )
	{
		// tick labels
		rect.left   = x-40;
		rect.right  = x+40;
		rect.top    = y;
		rect.bottom = y+TEXPROFILE_SAMPLES_ITEMHEIGHT;
		sprintf( labelBuff, "%dMB", i*TEXPROFILE_MAJORTICKSIZE );
		DrawText( hdc, labelBuff, -1, &rect, DT_CENTER );

		// major ticks
		x0 = x;
		y0 = y + TEXPROFILE_SAMPLES_ITEMHEIGHT;
		SelectObject( hdc, hBlackPen );
		MoveToEx( hdc, x0, y0, NULL );
		LineTo( hdc, x0, y0+windowHeight );

		if ( g_texProfile_samples_tickMarks &&  g_texProfile_samples_scale > -TEXPROFILE_SAMPLES_SCALESTEPS )
		{
			// minor ticks
			x0 = x;
			y0 = y + TEXPROFILE_SAMPLES_ITEMHEIGHT;
			SelectObject( hdc, hGreyPen );
			for ( j=0; j<TEXPROFILE_SAMPLES_NUMMINORTICKS; j++ )
			{
				x0 += tickWidth/( TEXPROFILE_SAMPLES_NUMMINORTICKS+1 );

				MoveToEx( hdc, x0, y0, NULL );
				LineTo( hdc, x0, y0+windowHeight );
			}
		}
		x += tickWidth;
	}

	// seperator
	SelectObject( hdc, hBlackPen );
	MoveToEx( hdc, 0, TEXPROFILE_SAMPLES_ITEMHEIGHT, NULL );
	LineTo( hdc, windowWidth, TEXPROFILE_SAMPLES_ITEMHEIGHT );

	// draw labels
	x = 0;
	y = TEXPROFILE_SAMPLES_ITEMHEIGHT;
	for ( i=0; i<g_texProfile_numCounters; i++ )
	{
		if ( !g_texProfile_counters[i].label )
			continue;

		rect.left   = x;
		rect.right  = x+TEXPROFILE_SAMPLES_LABELWIDTH-TEXPROFILE_SAMPLES_LABELGAP;
		rect.top    = y;
		rect.bottom = y+TEXPROFILE_SAMPLES_ITEMHEIGHT;
		DrawText( hdc, g_texProfile_counters[i].label, -1, &rect, DT_VCENTER|DT_RIGHT|DT_SINGLELINE|DT_END_ELLIPSIS|DT_MODIFYSTRING );

		// draw the under line
		MoveToEx( hdc, x, y+TEXPROFILE_SAMPLES_ITEMHEIGHT, NULL );
		LineTo( hdc, x+TEXPROFILE_SAMPLES_LABELWIDTH, y+TEXPROFILE_SAMPLES_ITEMHEIGHT );

		y += TEXPROFILE_SAMPLES_ITEMHEIGHT;
	}

	// draw bars
	SelectObject( hdc, hBlackPen );
	x = TEXPROFILE_SAMPLES_LABELWIDTH;
	y = TEXPROFILE_SAMPLES_ITEMHEIGHT;
	currentSample = g_texProfile_numSamples-1;
	if ( currentSample < 0 )
		currentSample = 0;
	else
		currentSample %= TEXPROFILE_MAXSAMPLES;
	for ( i=0; i<g_texProfile_numCounters; i++ )
	{
		if ( !g_texProfile_counters[i].label )
			continue;

		hColoredBrush = CreateSolidBrush( g_texProfile_samples_colors ? g_texProfile_counters[i].color : g_backgroundColor );
		hbrushOld = ( HBRUSH )SelectObject( hdc, hColoredBrush );

		// bar - count is in bytes, scale to major tick
		t  = ( float )g_texProfile_counters[i].samples[currentSample]/TEXPROFILE_MAJORTICKBYTES;
		w  = ( int )( t * ( float )tickWidth );
		if ( w > windowWidth )
			w = windowWidth;
		x0 = x;
		y0 = y + ( TEXPROFILE_SAMPLES_ITEMHEIGHT-TEXPROFILE_SAMPLES_BARHEIGHT )/2 + 1;
		Rectangle( hdc, x0, y0, x0 + w, y0 + TEXPROFILE_SAMPLES_BARHEIGHT );

		// peak
		t  = ( float )g_texProfile_counters[i].peakSample/TEXPROFILE_MAJORTICKBYTES;
		w  = ( int )( t * ( float )tickWidth );
		if ( w > windowWidth )
			w = windowWidth;
		x0 = x + w;
		y0 = y + TEXPROFILE_SAMPLES_ITEMHEIGHT/2 + 1;

		POINT points[4];
		points[0].x = x0;
		points[0].y = y0-4;
		points[1].x = x0+4;
		points[1].y = y0;
		points[2].x = x0;
		points[2].y = y0+4;
		points[3].x = x0-4;
		points[3].y = y0;
		Polygon( hdc, points, 4 );

		SelectObject( hdc, hbrushOld );
		DeleteObject( hColoredBrush );

		// draw peak sizes
		sample = ( float )g_texProfile_counters[i].peakSample/1024.0f;
		if ( sample >= 0.01F )
		{
			sprintf( labelBuff, "%.2f MB", sample/1024.0f );
			rect.left   = x0 + 8;
			rect.right  = x0 + 8 + 100;
			rect.top    = y;
			rect.bottom = y + TEXPROFILE_SAMPLES_ITEMHEIGHT;
			DrawText( hdc, labelBuff, -1, &rect, DT_VCENTER|DT_LEFT|DT_SINGLELINE );
		}

		y += TEXPROFILE_SAMPLES_ITEMHEIGHT;
	}

	SelectObject( hdc, hFontOld );
	SelectObject( hdc, hPenOld );
	DeleteObject( hBlackPen );
	DeleteObject( hGreyPen );
}

//-----------------------------------------------------------------------------
//	TexProfileHistory_Draw
// 
//-----------------------------------------------------------------------------
void TexProfileHistory_Draw( HDC hdc, RECT* clientRect )
{
	char	labelBuff[128];
	HPEN	hBlackPen;
	HPEN	hPenOld;
	HPEN	hNullPen;
	HPEN	hGreyPen;
	HBRUSH	hColoredBrush;
	HBRUSH	hBrushOld;
	HFONT	hFontOld;
	int		currentSample;
	int		numTicks;
	int		tickHeight;
	int		windowWidth;
	int		windowHeight;
	int		x;
	int		y;
	int		y0;
	int		i;
	int		j;
	int		h;
	int		numbars;
	RECT	rect;
	float	t;
	float	scale;

	hBlackPen  = CreatePen( PS_SOLID, 1, RGB( 0,0,0 ) );
	hGreyPen   = CreatePen( PS_SOLID, 1, Sys_ColorScale( g_backgroundColor, 0.85f ) );
	hNullPen   = CreatePen( PS_NULL, 0, RGB( 0,0,0 ) );
	hPenOld    = ( HPEN )SelectObject( hdc, hBlackPen );
	hFontOld   = SelectFont( hdc, g_hProportionalFont );

	// zoom
	scale        = TexProfile_CalcScale( g_texProfile_history_scale, TEXPROFILE_HISTORY_SCALESTEPS, TEXPROFILE_HISTORY_MINSCALE, TEXPROFILE_HISTORY_MAXSCALE );
	tickHeight   = ( int )( TEXPROFILE_HISTORY_MAJORTICKHEIGHT*scale );
	windowWidth  = clientRect->right-clientRect->left;
	windowHeight = clientRect->bottom-clientRect->top;

	numTicks = windowHeight/tickHeight + 2;
	if ( numTicks < 0 )
		numTicks = 1;

	SetBkColor( hdc, g_backgroundColor );

	x = 0;
	y = windowHeight;
	for ( i=0; i<numTicks; i++ )
	{
		// major ticks
		SelectObject( hdc, hBlackPen );
		MoveToEx( hdc, 0, y, NULL );
		LineTo( hdc, windowWidth, y );

		if ( g_texProfile_history_tickMarks && g_texProfile_history_scale > -TEXPROFILE_HISTORY_SCALESTEPS )
		{
			// minor ticks
			y0 = y;
			SelectObject( hdc, hGreyPen );
			for ( j=0; j<TEXPROFILE_HISTORY_NUMMINORTICKS; j++ )
			{
				y0 += tickHeight/( TEXPROFILE_SAMPLES_NUMMINORTICKS+1 );
				MoveToEx( hdc, 0, y0, NULL );
				LineTo( hdc, windowWidth, y0 );
			}
		}

		// tick labels
		if ( i )
		{
			rect.left   = windowWidth-50;
			rect.right  = windowWidth;
			rect.top    = y-20;
			rect.bottom = y;
			sprintf( labelBuff, "%dMB", i*TEXPROFILE_MAJORTICKSIZE );
			DrawText( hdc, labelBuff, -1, &rect, DT_RIGHT|DT_SINGLELINE|DT_BOTTOM );
		}

		y -= tickHeight;
	}

	// vertical bars
	if ( g_texProfile_numSamples )
	{
		SelectObject( hdc, hNullPen );

		numbars       = windowWidth-TEXPROFILE_HISTORY_LABELWIDTH;
		currentSample = g_texProfile_numSamples-1;
		for ( x=numbars-1; x>=0; x-=4 )
		{
			// all the counters at this sample
			y = windowHeight;
			for ( j=0; j<g_texProfile_numCounters-1; j++ )
			{
				if ( !g_texProfile_counters[j].label )
					continue;

				t  = ( float )g_texProfile_counters[j].samples[currentSample % TEXPROFILE_MAXSAMPLES]/TEXPROFILE_MAJORTICKBYTES;
				h  = ( int )( t * ( float )tickHeight );
				if ( h )
				{
					if ( h > windowHeight )
						h = windowHeight;

					hColoredBrush = CreateSolidBrush( g_texProfile_history_colors ? g_texProfile_counters[j].color : RGB( 80,80,80 ) );
					hBrushOld = ( HBRUSH )SelectObject( hdc, hColoredBrush );

					Rectangle( hdc, x-4, y-h, x, y+1 );
					y -= h;

					SelectObject( hdc, hBrushOld );
					DeleteObject( hColoredBrush );
				}
			}
			currentSample--;
			if ( currentSample < 0 )
			{
				// no data
				break;
			}
		}
	}

	SelectObject( hdc, hFontOld );
	SelectObject( hdc, hPenOld );
	DeleteObject( hBlackPen );
	DeleteObject( hGreyPen );
}

//-----------------------------------------------------------------------------
//	TexProfile_WndProc
// 
//-----------------------------------------------------------------------------
LRESULT CALLBACK TexProfile_WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    WORD			wID = LOWORD( wParam );
	HDC				hdc;
	PAINTSTRUCT		ps; 
	RECT			rect;
	int				id;
	bool			bIsSamples;
	bool			bIsHistory;
	CREATESTRUCT	*createStructPtr;
	bool			bIsEnabled;

	// identify window
	id = ( int )GetWindowLong( hwnd, GWL_USERDATA+0 );
	bIsSamples = ( id == ID_TEXPROFILE_SAMPLES );
	bIsHistory = ( id == ID_TEXPROFILE_HISTORY );
	
	switch ( message )
	{
		case WM_CREATE:
			// set the window identifier
			createStructPtr = ( CREATESTRUCT* )lParam;
			SetWindowLong( hwnd, GWL_USERDATA+0, ( LONG )createStructPtr->lpCreateParams );

			// reset peaks
			g_texProfile_lastPeakTime = 0;
			return 0L;

		case WM_DESTROY:
			TexProfile_SaveConfig();

			if ( bIsSamples )
				g_texProfile_hWndSamples = NULL;
			else if ( bIsHistory )
				g_texProfile_hWndHistory = NULL;

			if ( VProf_GetState() == VPROF_TEXTURE || VProf_GetState() == VPROF_TEXTUREFRAME )
			{
				VProf_Enable( VPROF_OFF );
			}
			return 0L;
	
        case WM_INITMENU:
			if ( bIsSamples )
			{
				CheckMenuItem( ( HMENU )wParam, IDM_TEXPROFILE_TICKMARKS, MF_BYCOMMAND | ( g_texProfile_samples_tickMarks ? MF_CHECKED : MF_UNCHECKED ) );
				CheckMenuItem( ( HMENU )wParam, IDM_TEXPROFILE_COLORS, MF_BYCOMMAND | ( g_texProfile_samples_colors ? MF_CHECKED : MF_UNCHECKED ) );
			}
			else if ( bIsHistory )
			{
				CheckMenuItem( ( HMENU )wParam, IDM_TEXPROFILE_TICKMARKS, MF_BYCOMMAND | ( g_texProfile_history_tickMarks ? MF_CHECKED : MF_UNCHECKED ) );
				CheckMenuItem( ( HMENU )wParam, IDM_TEXPROFILE_COLORS, MF_BYCOMMAND | ( g_texProfile_history_colors ? MF_CHECKED : MF_UNCHECKED ) );
			}
            CheckMenuItem( ( HMENU )wParam, IDM_TEXPROFILE_ENABLE, MF_BYCOMMAND | ( ( VProf_GetState() == VPROF_TEXTURE || VProf_GetState() == VPROF_TEXTUREFRAME ) ? MF_CHECKED : MF_UNCHECKED ) );
			CheckMenuItem( ( HMENU )wParam, IDM_TEXPROFILE_CURRENTFRAME, MF_BYCOMMAND | ( g_texProfile_currentFrame ? MF_CHECKED : MF_UNCHECKED ) );
			return 0L;

		case WM_PAINT:
			GetClientRect( hwnd, &rect );
			hdc = BeginPaint( hwnd, &ps ); 
			if ( bIsSamples )
				TexProfileSamples_Draw( hdc, &rect );
			else if ( bIsHistory )
				TexProfileHistory_Draw( hdc, &rect );
            EndPaint( hwnd, &ps ); 
			return 0L;

		case WM_SIZE:
			// force a redraw
			TexProfile_UpdateWindow();
			return 0L;
	
		case WM_KEYDOWN:
			switch ( wParam )
			{
				case VK_INSERT:
					if ( bIsSamples )
						TexProfile_ZoomIn( g_texProfile_samples_scale, TEXPROFILE_SAMPLES_SCALESTEPS );
					else if ( bIsHistory )
						TexProfile_ZoomIn( g_texProfile_history_scale, TEXPROFILE_HISTORY_SCALESTEPS );
					return 0L;

				case VK_DELETE:
					if ( bIsSamples )
						TexProfile_ZoomOut( g_texProfile_samples_scale, TEXPROFILE_SAMPLES_SCALESTEPS );
					else if ( bIsHistory )
						TexProfile_ZoomOut( g_texProfile_history_scale, TEXPROFILE_HISTORY_SCALESTEPS );
					return 0L;
			}
			break;

        case WM_COMMAND:
            switch ( wID )
            {
				case IDM_TEXPROFILE_TICKMARKS:
					if ( bIsSamples )
						g_texProfile_samples_tickMarks ^= 1;
					else if ( bIsHistory )
						g_texProfile_history_tickMarks ^= 1;
					TexProfile_UpdateWindow();
					return 0L;

				case IDM_TEXPROFILE_COLORS:
					if ( bIsSamples )
						g_texProfile_samples_colors ^= 1;
					else if ( bIsHistory )
						g_texProfile_history_colors ^= 1;
					TexProfile_UpdateWindow();
					return 0L;

				case IDM_TEXPROFILE_ZOOMIN:
					if ( bIsSamples )
						TexProfile_ZoomIn( g_texProfile_samples_scale, TEXPROFILE_SAMPLES_SCALESTEPS );
					else if ( bIsHistory )
						TexProfile_ZoomIn( g_texProfile_history_scale, TEXPROFILE_HISTORY_SCALESTEPS );
					return 0L;

				case IDM_TEXPROFILE_ZOOMOUT:
					if ( bIsSamples )
						TexProfile_ZoomOut( g_texProfile_samples_scale, TEXPROFILE_SAMPLES_SCALESTEPS );
					else if ( bIsHistory )
						TexProfile_ZoomOut( g_texProfile_history_scale, TEXPROFILE_HISTORY_SCALESTEPS );
					return 0L;

				case IDM_TEXPROFILE_ENABLE:
					bIsEnabled = ( VProf_GetState() == VPROF_TEXTURE || VProf_GetState() == VPROF_TEXTUREFRAME );
					bIsEnabled ^= 1;
					if ( !bIsEnabled )
						VProf_Enable( VPROF_OFF );
					else
					{
						if ( !g_texProfile_currentFrame )
							VProf_Enable( VPROF_TEXTURE );
						else
							VProf_Enable( VPROF_TEXTUREFRAME );
					}
					TexProfile_SetTitle();
					return 0L;

				case IDM_TEXPROFILE_CURRENTFRAME:
					bIsEnabled = ( VProf_GetState() == VPROF_TEXTURE || VProf_GetState() == VPROF_TEXTUREFRAME );
					g_texProfile_currentFrame ^= 1;
					if ( bIsEnabled )
					{
						if ( !g_texProfile_currentFrame )
							VProf_Enable( VPROF_TEXTURE );
						else
							VProf_Enable( VPROF_TEXTUREFRAME );
					}
					TexProfile_SetTitle();
					return 0L;
			}
			break;
	}	
	return ( DefWindowProc( hwnd, message, wParam, lParam ) );
}

//-----------------------------------------------------------------------------
//	TexProfileHistory_Open
// 
//-----------------------------------------------------------------------------
void TexProfileHistory_Open()
{
	HWND	hWnd;
	
	if ( g_texProfile_hWndHistory )
	{
		// only one profile instance
		if ( IsIconic( g_texProfile_hWndHistory ) )
			ShowWindow( g_texProfile_hWndHistory, SW_RESTORE );
		SetForegroundWindow( g_texProfile_hWndHistory );
		return;
	}

	if ( VProf_GetState() == VPROF_OFF )
	{
		if ( !g_texProfile_currentFrame )
			VProf_Enable( VPROF_TEXTURE );
		else
			VProf_Enable( VPROF_TEXTUREFRAME );
	}

	hWnd = CreateWindowEx( 
				WS_EX_CLIENTEDGE,
				"TEXPROFILEHISTORYCLASS",
				"",
				WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
				0,
				0,
				600,
				500,
				g_hDlgMain,
				NULL,
				g_hInstance,
				( void* )ID_TEXPROFILE_HISTORY );
	g_texProfile_hWndHistory = hWnd;

	TexProfile_SetTitle();

	if ( g_texProfile_historyWindowRect.right && g_texProfile_historyWindowRect.bottom )
		MoveWindow( g_texProfile_hWndHistory, g_texProfile_historyWindowRect.left, g_texProfile_historyWindowRect.top, g_texProfile_historyWindowRect.right-g_texProfile_historyWindowRect.left, g_texProfile_historyWindowRect.bottom-g_texProfile_historyWindowRect.top, FALSE );
	ShowWindow( g_texProfile_hWndHistory, SHOW_OPENWINDOW );
}

//-----------------------------------------------------------------------------
//	TexProfileSamples_Open
// 
//-----------------------------------------------------------------------------
void TexProfileSamples_Open()
{
	HWND	hWnd;
	
	if ( g_texProfile_hWndSamples )
	{
		// only one profile instance
		if ( IsIconic( g_texProfile_hWndSamples ) )
			ShowWindow( g_texProfile_hWndSamples, SW_RESTORE );
		SetForegroundWindow( g_texProfile_hWndSamples );
		return;
	}

	if ( VProf_GetState() == VPROF_OFF )
	{
		if ( !g_texProfile_currentFrame )
			VProf_Enable( VPROF_TEXTURE );
		else
			VProf_Enable( VPROF_TEXTUREFRAME );
	}

	hWnd = CreateWindowEx( 
				WS_EX_CLIENTEDGE,
				"TEXPROFILESAMPLESCLASS",
				"",
				WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
				0,
				0,
				600,
				500,
				g_hDlgMain,
				NULL,
				g_hInstance,
				( void* )ID_TEXPROFILE_SAMPLES );
	g_texProfile_hWndSamples = hWnd;

	TexProfile_SetTitle();

	if ( g_texProfile_samplesWindowRect.right && g_texProfile_samplesWindowRect.bottom )
		MoveWindow( g_texProfile_hWndSamples, g_texProfile_samplesWindowRect.left, g_texProfile_samplesWindowRect.top, g_texProfile_samplesWindowRect.right-g_texProfile_samplesWindowRect.left, g_texProfile_samplesWindowRect.bottom-g_texProfile_samplesWindowRect.top, FALSE );
	ShowWindow( g_texProfile_hWndSamples, SHOW_OPENWINDOW );
}

//-----------------------------------------------------------------------------
//	TexProfile_Clear
// 
//-----------------------------------------------------------------------------
void TexProfile_Clear()
{
	// clear counters and history
	g_texProfile_numCounters = 0;
	g_texProfile_numSamples  = 0;

	TexProfile_UpdateWindow();
}

//-----------------------------------------------------------------------------
//	TexProfile_Init
// 
//-----------------------------------------------------------------------------
bool TexProfile_Init()
{
	WNDCLASS wndclass;

	// set up our window class
	memset( &wndclass, 0, sizeof( wndclass ) );
	wndclass.style         = 0;
	wndclass.lpfnWndProc   = TexProfile_WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = g_hInstance;
	wndclass.hIcon         = g_hIcons[ICON_APPLICATION];
	wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground = g_hBackgroundBrush;
	wndclass.lpszMenuName  = MAKEINTRESOURCE( MENU_TEXPROFILE );
	wndclass.lpszClassName = "TEXPROFILESAMPLESCLASS";
	if ( !RegisterClass( &wndclass ) )
		return false;

	// set up our window class
	memset( &wndclass, 0, sizeof( wndclass ) );
	wndclass.style         = 0;
	wndclass.lpfnWndProc   = TexProfile_WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = g_hInstance;
	wndclass.hIcon         = g_hIcons[ICON_APPLICATION];
	wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground = g_hBackgroundBrush;
	wndclass.lpszMenuName  = MAKEINTRESOURCE( MENU_TEXPROFILE );
	wndclass.lpszClassName = "TEXPROFILEHISTORYCLASS";
	if ( !RegisterClass( &wndclass ) )
		return false;
	
	TexProfile_LoadConfig();

	return true;
}