forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatsys_regressiontest.cpp
1023 lines (869 loc) · 29.5 KB
/
matsys_regressiontest.cpp
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
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#define PROTECTED_THINGS_DISABLE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/IMaterialSystemHardwareConfig.h"
#include "materialsystem/imaterialproxyfactory.h"
#include "filesystem.h"
#include "bitmap/imageformat.h"
#include "materialsystem/MaterialSystem_Config.h"
#include "tier0/icommandline.h"
#include "tier1/strtools.h"
#include "mathlib/mathlib.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imaterialvar.h"
#include "bitmap/tgawriter.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h>
#include "vstdlib/cvar.h"
#include "KeyValues.h"
#include "tier1/utlbuffer.h"
#include "tier2/tier2.h"
#define CHECKERBOARD_DIMS 16
IMaterial *g_pProceduralMaterial = NULL;
static char g_pCommandLine[1024];
static int g_ArgC = 0;
static char* g_ppArgV[32];
static HWND g_HWnd = 0;
static CreateInterfaceFn g_MaterialsFactory;
static CSysModule *g_MaterialsDLL = NULL;
static int g_RenderWidth = 640;
static int g_RenderHeight = 480;
static int g_RefreshRate = 60;
static bool g_Exiting = false;
void CreateLightmapPages( void );
void RenderFrame( void );
//-----------------------------------------------------------------------------
// Command-line...
//-----------------------------------------------------------------------------
int FindCommand( const char *pCommand )
{
for ( int i = 0; i < g_ArgC; i++ )
{
if ( !stricmp( pCommand, g_ppArgV[i] ) )
return i;
}
return -1;
}
const char* CommandArgument( const char *pCommand )
{
int cmd = FindCommand( pCommand );
if ((cmd != -1) && (cmd < g_ArgC - 1))
{
return g_ppArgV[cmd+1];
}
return 0;
}
void SetupCommandLine( const char* pCommandLine )
{
Q_strncpy( g_pCommandLine, pCommandLine, sizeof( g_pCommandLine ) );
g_ArgC = 0;
char* pStr = g_pCommandLine;
while ( *pStr )
{
pStr = strtok( pStr, " " );
if( !pStr )
{
break;
}
g_ppArgV[g_ArgC++] = pStr;
pStr += strlen(pStr) + 1;
}
}
//-----------------------------------------------------------------------------
// DummyMaterialProxyFactory...
//-----------------------------------------------------------------------------
class DummyMaterialProxyFactory : public IMaterialProxyFactory
{
public:
virtual IMaterialProxy *CreateProxy( const char *proxyName ) {return NULL;}
virtual void DeleteProxy( IMaterialProxy *pProxy ) {}
};
static DummyMaterialProxyFactory g_DummyMaterialProxyFactory;
//-----------------------------------------------------------------------------
// Spew function!
//-----------------------------------------------------------------------------
SpewRetval_t SpewFunc( SpewType_t spewType, char const *pMsg )
{
OutputDebugString( pMsg );
switch( spewType )
{
case SPEW_MESSAGE:
case SPEW_WARNING:
case SPEW_LOG:
OutputDebugString( pMsg );
return SPEW_CONTINUE;
case SPEW_ASSERT:
case SPEW_ERROR:
default:
::MessageBox( NULL, pMsg, "Error!", MB_OK );
return SPEW_DEBUGGER;
}
}
//-----------------------------------------------------------------------------
// Error...
//-----------------------------------------------------------------------------
void DisplayError( const char* pError, ... )
{
va_list argptr;
char msg[1024];
va_start( argptr, pError );
Q_vsnprintf( msg, sizeof( msg ), pError, argptr );
va_end( argptr );
MessageBox( 0, msg, 0, MB_OK );
}
IFileSystem *g_pFileSystem;
static CSysModule *g_pFileSystemModule = NULL;
static CreateInterfaceFn g_pFileSystemFactory = NULL;
static bool FileSystem_LoadDLL( void )
{
g_pFileSystemModule = Sys_LoadModule( "filesystem_stdio.dll" );
Assert( g_pFileSystemModule );
if( !g_pFileSystemModule )
{
return false;
}
g_pFileSystemFactory = Sys_GetFactory( g_pFileSystemModule );
if( !g_pFileSystemFactory )
{
return false;
}
g_pFileSystem = ( IFileSystem * )g_pFileSystemFactory( FILESYSTEM_INTERFACE_VERSION, NULL );
Assert( g_pFileSystem );
if( !g_pFileSystem )
{
return false;
}
return true;
}
void FileSystem_UnloadDLL( void )
{
if ( !g_pFileSystemModule )
return;
Sys_UnloadModule( g_pFileSystemModule );
g_pFileSystemModule = 0;
}
void FileSystem_Init( )
{
if( !FileSystem_LoadDLL() )
{
return;
}
g_pFileSystem->RemoveSearchPath( NULL, "GAME" );
g_pFileSystem->AddSearchPath( "hl2", "GAME", PATH_ADD_TO_HEAD );
}
void FileSystem_Shutdown( void )
{
g_pFileSystem->Shutdown();
FileSystem_UnloadDLL();
}
//-----------------------------------------------------------------------------
// Purpose: Unloads the material system .dll
//-----------------------------------------------------------------------------
void UnloadMaterialSystem( void )
{
if ( !g_MaterialsFactory )
return;
Sys_UnloadModule( g_MaterialsDLL );
g_MaterialsDLL = NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Loads the material system dll
//-----------------------------------------------------------------------------
void LoadMaterialSystem( void )
{
if( g_MaterialsFactory )
{
return;
}
Assert( !g_MaterialsDLL );
g_MaterialsDLL = Sys_LoadModule( "MaterialSystem.dll" );
g_MaterialsFactory = Sys_GetFactory( g_MaterialsDLL );
if ( !g_MaterialsFactory )
{
DisplayError( "LoadMaterialSystem: Failed to load MaterialSystem.DLL\n" );
return;
}
if ( g_MaterialsFactory )
{
g_pMaterialSystem = (IMaterialSystem *)g_MaterialsFactory( MATERIAL_SYSTEM_INTERFACE_VERSION, NULL );
if ( !g_pMaterialSystem )
{
DisplayError( "Could not get the material system interface from materialsystem.dll (a)" );
}
}
else
{
DisplayError( "Could not find factory interface in library MaterialSystem.dll" );
}
}
void InitMaterialSystemConfig(MaterialSystem_Config_t *pConfig)
{
}
CreateInterfaceFn g_MaterialSystemClientFactory;
void Shader_Init( HWND mainWindow )
{
LoadMaterialSystem();
Assert( g_pMaterialSystem );
// FIXME: Where do we put this?
const char* pDLLName;
pDLLName = "shaderapidx9";
// assume that IFileSystem paths have already been set via g_pFileSystem
g_MaterialSystemClientFactory = g_pMaterialSystem->Init(
pDLLName, &g_DummyMaterialProxyFactory, g_pFileSystemFactory, VStdLib_GetICVarFactory() );
if (!g_MaterialSystemClientFactory)
{
DisplayError( "Unable to init shader system\n" );
}
// Get the adapter from the command line....
MaterialVideoMode_t mode;
memset( &mode, 0, sizeof( mode ) );
mode.m_Width = g_RenderWidth;
mode.m_Height = g_RenderHeight;
mode.m_Format = IMAGE_FORMAT_BGRX8888;
mode.m_RefreshRate = g_RefreshRate;
// bool modeSet = g_pMaterialSystem->SetMode( (void*)mainWindow, mode, modeFlags );
// if (!modeSet)
// {
// DisplayError( "Unable to set mode\n" );
// }
g_pMaterialSystemHardwareConfig = (IMaterialSystemHardwareConfig*)
g_MaterialSystemClientFactory( MATERIALSYSTEM_HARDWARECONFIG_INTERFACE_VERSION, 0 );
if ( !g_pMaterialSystemHardwareConfig )
{
DisplayError( "Could not get the material system hardware config interface!" );
}
MaterialSystem_Config_t config;
InitMaterialSystemConfig(&config);
g_pMaterialSystem->OverrideConfig( config, false );
if( FindCommand( "-stub" ) != -1 )
{
g_pMaterialSystem->SetInStubMode( true );
}
}
void Shader_Shutdown( HWND hwnd )
{
// Recursive shutdown
if ( !g_pMaterialSystem )
return;
g_pMaterialSystem->Shutdown( );
g_pMaterialSystem = NULL;
UnloadMaterialSystem();
}
static void CalcWindowSize( int desiredRenderingWidth, int desiredRenderingHeight,
int *windowWidth, int *windowHeight )
{
int borderX, borderY;
borderX = (GetSystemMetrics(SM_CXFIXEDFRAME) + 1) * 2;
borderY = (GetSystemMetrics(SM_CYFIXEDFRAME) + 1) * 2 + GetSystemMetrics(SM_CYSIZE) + 1;
*windowWidth = desiredRenderingWidth + borderX;
*windowHeight = desiredRenderingHeight + borderY;
}
//-----------------------------------------------------------------------------
// Window create, destroy...
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_PAINT:
RenderFrame();
break;
// abort when ESC is hit
case WM_CHAR:
switch(wParam)
{
case VK_ESCAPE:
SendMessage( g_HWnd, WM_CLOSE, 0, 0 );
break;
}
break;
case WM_DESTROY:
g_Exiting = true;
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
bool CreateAppWindow( const char* pAppName, int width, int height )
{
// Register the window class
WNDCLASSEX windowClass =
{
sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
pAppName, NULL
};
RegisterClassEx( &windowClass );
// Create the application's window
g_HWnd = CreateWindow( pAppName, pAppName,
WS_OVERLAPPEDWINDOW,
0, 0, width, height,
GetDesktopWindow(), NULL, windowClass.hInstance, NULL );
ShowWindow (g_HWnd, SW_SHOWDEFAULT);
return (g_HWnd != 0);
}
void DestroyAppWindow()
{
if (g_HWnd)
DestroyWindow( g_HWnd );
}
bool Init( const char* pCommands)
{
// Store off the command line...
SetupCommandLine( pCommands );
FileSystem_Init( );
/* figure out g_Renderwidth and g_RenderHeight */
g_RenderWidth = -1;
g_RenderHeight = -1;
g_RenderWidth = 640;
g_RenderHeight = 480;
int windowWidth, windowHeight;
CalcWindowSize( g_RenderWidth, g_RenderHeight, &windowWidth, &windowHeight );
if( !CreateAppWindow( "matsys_regressiontest", windowWidth, windowHeight ) )
{
return false;
}
Shader_Init( g_HWnd );
g_pMaterialSystem->CacheUsedMaterials();
CreateLightmapPages();
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Return the directory where this .exe is running from
// Output : char
//-----------------------------------------------------------------------------
static char *GetBaseDir( const char *pszBuffer )
{
static char basedir[ MAX_PATH ];
char szBuffer[ MAX_PATH ];
int j;
char *pBuffer = NULL;
Q_strncpy( szBuffer, pszBuffer, sizeof( szBuffer ) );
pBuffer = strrchr( szBuffer,'\\' );
if ( pBuffer )
{
*(pBuffer+1) = '\0';
}
Q_strncpy( basedir, szBuffer, sizeof( basedir ) );
j = strlen( basedir );
if (j > 0)
{
if ( ( basedir[ j-1 ] == '\\' ) ||
( basedir[ j-1 ] == '/' ) )
{
basedir[ j-1 ] = 0;
}
}
return basedir;
}
//-----------------------------------------------------------------------------
// Shutdown
//-----------------------------------------------------------------------------
void Shutdown()
{
// Close the window
DestroyAppWindow();
Shader_Shutdown( g_HWnd );
}
//-----------------------------------------------------------------------------
// Pump windows messages
//-----------------------------------------------------------------------------
void PumpWindowsMessages ()
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
InvalidateRect(g_HWnd, NULL, false);
UpdateWindow(g_HWnd);
}
MaterialSystem_SortInfo_t *g_pMaterialSortInfo = NULL;
struct LightmapInfo_t
{
int m_LightmapDims[2];
int m_LightmapPageDims[2];
int m_OffsetIntoLightmapPage[2];
int m_SortID;
};
LightmapInfo_t g_CheckerboardLightmapInfo;
void AllocateLightmap( LightmapInfo_t& lightmapInfo, int width, int height )
{
lightmapInfo.m_LightmapDims[0] = width;
lightmapInfo.m_LightmapDims[1] = height;
// HACK! We don't ever use this material for anything. . just need it for allocating lightmaps.
CMatRenderContextPtr pRenderContext( materials );
IMaterial *pHackMaterial = g_pMaterialSystem->FindMaterial( "shadertest/lightmappedgeneric", TEXTURE_GROUP_OTHER );
pRenderContext->Bind( pHackMaterial );
lightmapInfo.m_SortID =
g_pMaterialSystem->AllocateLightmap( 16, 16, lightmapInfo.m_OffsetIntoLightmapPage, pHackMaterial );
}
void Lightmap_PostAllocation( LightmapInfo_t& lightmapInfo )
{
g_pMaterialSystem->GetLightmapPageSize( lightmapInfo.m_SortID,
&lightmapInfo.m_LightmapPageDims[0], &lightmapInfo.m_LightmapPageDims[1] );
}
void UpdateLightmap( LightmapInfo_t& lightmapInfo, float *data )
{
g_pMaterialSystem->UpdateLightmap( g_pMaterialSortInfo[lightmapInfo.m_SortID].lightmapPageID,
lightmapInfo.m_LightmapDims, lightmapInfo.m_OffsetIntoLightmapPage, data, NULL, NULL, NULL );
}
void CreateLightmapPages( void )
{
g_pMaterialSystem->BeginLightmapAllocation();
AllocateLightmap( g_CheckerboardLightmapInfo, CHECKERBOARD_DIMS, CHECKERBOARD_DIMS );
g_pMaterialSystem->EndLightmapAllocation();
int numSortIDs = g_pMaterialSystem->GetNumSortIDs();
g_pMaterialSortInfo = new MaterialSystem_SortInfo_t[numSortIDs];
g_pMaterialSystem->GetSortInfo( g_pMaterialSortInfo );
Lightmap_PostAllocation( g_CheckerboardLightmapInfo );
float checkerboardImage[CHECKERBOARD_DIMS*CHECKERBOARD_DIMS*4];
int x, y;
for( y = 0; y < CHECKERBOARD_DIMS; y++ )
{
for( x = 0; x < CHECKERBOARD_DIMS; x++ )
{
if( ( x + y ) & 1 )
{
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+0] = 1.0f;
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+1] = 1.0f;
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+2] = 1.0f;
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+3] = 1.0f;
}
else
{
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+0] = 0.0f;
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+1] = 0.0f;
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+2] = 0.0f;
checkerboardImage[(y*CHECKERBOARD_DIMS+x)*4+3] = 1.0f;
}
}
}
UpdateLightmap( g_CheckerboardLightmapInfo, checkerboardImage );
}
void LightmapTexCoord( CMeshBuilder& meshBuilder, LightmapInfo_t lightmapInfo, float s, float t )
{
// add a one texel border.
float newS = ( s * ( lightmapInfo.m_LightmapDims[0] - 2 ) ) + 1;
float newT = ( t * ( lightmapInfo.m_LightmapDims[1] - 2 ) ) + 1;
newS += lightmapInfo.m_OffsetIntoLightmapPage[0];
newT += lightmapInfo.m_OffsetIntoLightmapPage[1];
newS *= 1.0f / lightmapInfo.m_LightmapPageDims[0];
newT *= 1.0f / lightmapInfo.m_LightmapPageDims[1];
meshBuilder.TexCoord2f( 1, newS, newT );
}
//-----------------------------------------------------------------------------
// Update
//-----------------------------------------------------------------------------
bool Update()
{
PumpWindowsMessages();
return g_Exiting;
}
void ScreenShot( const char *pFilename )
{
// bitmap bits
unsigned char *pImage = ( unsigned char * )malloc( g_RenderWidth * 3 * g_RenderHeight );
// Get Bits from the material system
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->ReadPixels( 0, 0, g_RenderWidth, g_RenderHeight, pImage, IMAGE_FORMAT_RGB888 );
CUtlBuffer outBuf;
if( !TGAWriter::WriteToBuffer( pImage, outBuf, g_RenderWidth, g_RenderHeight, IMAGE_FORMAT_RGB888,
IMAGE_FORMAT_RGB888 ) )
{
Error( "Couldn't write %s\n", pFilename );
}
if ( !g_pFullFileSystem->WriteFile( pFilename, NULL, outBuf ) )
{
Error( "Couldn't write %s\n", pFilename );
}
free( pImage );
}
void SetVar( const char *pVarName, const char *pStringValue )
{
IMaterialVar *pVar = g_pProceduralMaterial->FindVar( pVarName, NULL );
if( pStringValue )
{
pVar->SetStringValue( pStringValue );
}
else
{
pVar->SetUndefined();
}
}
void SetVar( const char *pVarName, int val )
{
IMaterialVar *pVar = g_pProceduralMaterial->FindVar( pVarName, NULL );
pVar->SetIntValue( val );
}
void SetFlag( MaterialVarFlags_t flag, int val )
{
g_pProceduralMaterial->SetMaterialVarFlag( flag, val ? true : false );
}
void DrawBackground( void )
{
CMatRenderContextPtr pRenderContext( materials );
IMaterial *pMaterial = g_pMaterialSystem->FindMaterial( "matsys_regressiontest/background", TEXTURE_GROUP_OTHER );
pRenderContext->Bind( pMaterial );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
#define X 500.0f
#define Y 500.0f
#define Z -500.0f
meshBuilder.Position3f( -X, Y, Z );
meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( X, Y, Z );
meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( X, -Y, Z );
meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( -X, -Y, Z );
meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
meshBuilder.AdvanceVertex();
meshBuilder.End();
pMesh->Draw();
#undef X
#undef Y
#undef Z
}
void BeginFrame( void )
{
g_pMaterialSystem->BeginFrame( 0 );
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->ClearColor3ub( 255, 0, 0 );
pRenderContext->ClearBuffers( true, true );
pRenderContext->Viewport( 0, 0, g_RenderWidth, g_RenderHeight );
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->LoadIdentity();
pRenderContext->PerspectiveX( 90.0f, ( g_RenderWidth / g_RenderHeight), 1.0f, 500000.0f );
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadIdentity();
}
void EndFrame( void )
{
g_pMaterialSystem->EndFrame();
g_pMaterialSystem->SwapBuffers();
}
void DrawQuad( const LightmapInfo_t& lightmapInfo )
{
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->BindLightmapPage( g_pMaterialSortInfo[lightmapInfo.m_SortID].lightmapPageID );
pRenderContext->Bind( g_pProceduralMaterial );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
#define X 350.0f
#define Y (X*(4.0f/3.0f))
#define Z -500.0f
meshBuilder.Position3f( -X, Y, Z );
meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
LightmapTexCoord( meshBuilder, lightmapInfo, 0.0f, 0.0f );
meshBuilder.Normal3f( 0.0f, 0.0f, 1.0f );
meshBuilder.Color4f( 1.0f, 0.0f, 0.0f, 1.0f );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( X, Y, Z );
meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
LightmapTexCoord( meshBuilder, lightmapInfo, 1.0f, 0.0f );
meshBuilder.Normal3f( 0.0f, 0.0f, 1.0f );
meshBuilder.Color4f( 1.0f, 1.0f, 0.0f, 1.0f );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( X, -Y, Z );
meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
LightmapTexCoord( meshBuilder, lightmapInfo, 1.0f, 1.0f );
meshBuilder.Normal3f( 0.0f, 0.0f, 1.0f );
meshBuilder.Color4f( 1.0f, 1.0f, 1.0f, 0.0f );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( -X, -Y, Z );
meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
LightmapTexCoord( meshBuilder, lightmapInfo, 0.0f, 1.0f );
meshBuilder.Normal3f( 0.0f, 0.0f, 1.0f );
meshBuilder.Color4f( 0.0f, 1.0f, 1.0f, 1.0f );
meshBuilder.AdvanceVertex();
meshBuilder.End();
pMesh->Draw();
#undef X
#undef Y
#undef Z
}
void TestLightmappedGeneric_dx80( void )
{
/*
based on lightmappedgeneric_vs11.fxc:
// STATIC: "DETAIL" "0..1"
// STATIC: "ENVMAP" "0..1"
// STATIC: "ENVMAPCAMERASPACE" "0..1"
// STATIC: "ENVMAPSPHERE" "0..1"
// STATIC: "VERTEXCOLOR" "0..1"
// DYNAMIC: "FOGTYPE" "0..1"
// SKIP: $ENVMAPCAMERASPACE && $ENVMAPSPHERE
// SKIP: !$ENVMAP && ( $ENVMAPCAMERASPACE || $ENVMAPSPHERE )
based on lightmappedgeneric_ps11.fxc:
// STATIC: "BASETEXTURE" "0..1"
// STATIC: "ENVMAP" "0..1"
// STATIC: "ENVMAPMASK" "0..1"
// STATIC: "SELFILLUM" "0..1"
// STATIC: "BASEALPHAENVMAPMASK" "0..1"
// SKIP: !$ENVMAP && ( $BASEALPHAENVMAPMASK || $ENVMAPMASK )
// SKIP: !$BASETEXTURE && $BASEALPHAENVMAPMASK
// SKIP: $BASEALPHAENVMAPMASK && $ENVMAPMASK
// SKIP: !$BASETEXTURE && $BASEALPHAENVMAPMASK
// SKIP: $SELFILLUM && $BASEALPHAENVMAPMASK
// SKIP: !$BASETEXTURE && $SELFILLUM
Versions with DETAIL aren't implemented yet in HLSL, but we'll go ahead and test those.
*/
// The following are shader combos from lightmappedgeneric_ps20
for( int BUMPMAP = 0; BUMPMAP <= 1; BUMPMAP++ )
{
for( int NORMALMAPALPHAENVMAPMASK = 0; NORMALMAPALPHAENVMAPMASK <= 1; NORMALMAPALPHAENVMAPMASK++ )
{
// The following are shader combos from lightmappedgeneric_ps11 (that aren't in lightmappedgenerc_vs11)
for( int BASETEXTURE = 0; BASETEXTURE <= 1; BASETEXTURE++ )
{
for( int ENVMAPMASK = 0; ENVMAPMASK <= 1; ENVMAPMASK++ )
{
for( int SELFILLUM = 0; SELFILLUM <= 1; SELFILLUM++ )
{
for( int BASEALPHAENVMAPMASK = 0; BASEALPHAENVMAPMASK <= 1; BASEALPHAENVMAPMASK++ )
{
// The following are shader combos from lightmappedgeneric_vs11
for( int DETAIL = 0; DETAIL <= 1; DETAIL++ )
{
for( int ENVMAP = 0; ENVMAP <= 1; ENVMAP++ )
{
for( int ENVMAPCAMERASPACE = 0; ENVMAPCAMERASPACE <= 1; ENVMAPCAMERASPACE++ )
{
for( int ENVMAPSPHERE = 0; ENVMAPSPHERE <= 1; ENVMAPSPHERE++ )
{
for( int VERTEXCOLOR = 0; VERTEXCOLOR <= 1; VERTEXCOLOR++ )
{
// for( int FOGTYPE = 0; FOGTYPE <= 1; FOGTYPE++ )
{
// conditional beneath here are flags, not shader combos
for( int ALPHATEST = 0; ALPHATEST <= 1; ALPHATEST++ )
{
for( int TRANSLUCENT = 0; TRANSLUCENT <= 1; TRANSLUCENT++ )
{
for( int COLORMODULATE = 0; COLORMODULATE <= 1; COLORMODULATE++ )
{
for( int ALPHAMODULATE = 0; ALPHAMODULATE <= 1; ALPHAMODULATE++ )
{
#if 0
BUMPMAP = 0;
NORMALMAPALPHAENVMAPMASK = 0;
BASETEXTURE = 1;
ENVMAPMASK = 0;
SELFILLUM = 0;
BASEALPHAENVMAPMASK = 1;
DETAIL = 0;
ENVMAP = 1;
ENVMAPCAMERASPACE = 0;
ENVMAPSPHERE = 0;
VERTEXCOLOR = 0;
ALPHATEST = 0;
TRANSLUCENT = 0;
COLORMODULATE = 0;
ALPHAMODULATE = 0;
#endif
// skip commands from shader lightmappedgeneric_vs11
if( ENVMAPCAMERASPACE && ENVMAPSPHERE ) continue;
if( !ENVMAP && ( ENVMAPCAMERASPACE || ENVMAPSPHERE ) ) continue;
// skip commands from shader lihgtmappedgeneric_ps11
if( !ENVMAP && ( BASEALPHAENVMAPMASK || ENVMAPMASK ) ) continue;
if( !BASETEXTURE && BASEALPHAENVMAPMASK ) continue;
if( BASEALPHAENVMAPMASK && ENVMAPMASK ) continue;
if( !BASETEXTURE && BASEALPHAENVMAPMASK ) continue;
if( SELFILLUM && BASEALPHAENVMAPMASK ) continue;
if( !BASETEXTURE && SELFILLUM ) continue;
// skip commands from shader lightmappedgeneric_ps20
if( DETAIL && BUMPMAP ) continue;
if( ENVMAPMASK && BUMPMAP ) continue;
if( NORMALMAPALPHAENVMAPMASK && BASEALPHAENVMAPMASK ) continue;
if( NORMALMAPALPHAENVMAPMASK && ENVMAPMASK ) continue;
if( BASEALPHAENVMAPMASK && ENVMAPMASK ) continue;
if( BASEALPHAENVMAPMASK && SELFILLUM ) continue;
// skip commands from flags that don't make sense (or we don't want to test)
if( !BASETEXTURE && ( ALPHATEST || TRANSLUCENT ) ) continue;
if( TRANSLUCENT && ALPHATEST ) continue;
KeyValues *pKeyValues = new KeyValues( "lightmappedgeneric" );
if ( BASETEXTURE )
{
pKeyValues->SetString( "$basetexture", "matsys_regressiontest/basetexture" );
}
if ( ENVMAPMASK )
{
pKeyValues->SetString( "$envmapmask", "matsys_regressiontest/specularmask" );
}
if ( SELFILLUM )
{
pKeyValues->SetInt( "$selfillum", 1 );
}
if ( BASEALPHAENVMAPMASK )
{
pKeyValues->SetInt( "$basealphaenvmapmask", 1 );
}
if ( BUMPMAP )
{
pKeyValues->SetString( "$bumpmap", "matsys_regressiontest/normalmap_normal" );
}
if ( DETAIL )
{
pKeyValues->SetString( "$detail", "matsys_regressiontest/detail" );
pKeyValues->SetFloat( "$detailscale", 0.5f );
}
if ( ENVMAP )
{
pKeyValues->SetString( "$envmap", "matsys_regressiontest/cubemap" );
}
if ( BASEALPHAENVMAPMASK )
{
pKeyValues->SetInt( "$basealphaenvmapmask", 1 );
}
if ( NORMALMAPALPHAENVMAPMASK )
{
pKeyValues->SetInt( "$normalmapalphaenvmapmask", 1 );
}
if ( TRANSLUCENT )
{
pKeyValues->SetInt( "$translucent", 1 );
}
if ( ENVMAPCAMERASPACE )
{
pKeyValues->SetInt( "$envmapcameraspace", 1 );
}
if ( ENVMAPSPHERE )
{
pKeyValues->SetInt( "$envmapsphere", 1 );
}
if ( VERTEXCOLOR )
{
pKeyValues->SetInt( "$vertexcolor", 1 );
}
if ( ALPHATEST )
{
pKeyValues->SetInt( "$alphatest", 1 );
}
if ( COLORMODULATE )
{
pKeyValues->SetString( "$color", "[1.0 0.8 0.5]" );
}
if ( ALPHAMODULATE )
{
pKeyValues->SetFloat( "$alpha", 0.6f );
}
// FIXME: ignoring fogtype for now.
// hack hack - LEAK - need to figure out how to clear a material out.
g_pProceduralMaterial = g_pMaterialSystem->CreateMaterial( "test", pKeyValues );
g_pProceduralMaterial->Refresh();
BeginFrame();
DrawBackground();
DrawQuad( g_CheckerboardLightmapInfo );
EndFrame();
char filename[MAX_PATH];
sprintf( filename, "%s\\lightmappedgeneric_dx80", CommandLine()->ParmValue( "-targetdir" ) );
if( BUMPMAP ) Q_strcat( filename, "_BUMPMAP", sizeof(filename) );
if( NORMALMAPALPHAENVMAPMASK ) Q_strcat( filename, "_NORMALMAPALPHAENVMAPMASK", sizeof(filename) );
if( BASETEXTURE ) Q_strcat( filename, "_BASE", sizeof(filename) );
if( ENVMAPMASK ) Q_strcat( filename, "_ENVMAPMASK", sizeof(filename) );
if( SELFILLUM ) Q_strcat( filename, "_SELFILLUM", sizeof(filename) );
if( BASEALPHAENVMAPMASK ) Q_strcat( filename, "_BASEALPHAENVMAPMASK", sizeof(filename) );
if( DETAIL ) Q_strcat( filename, "_DETAIL", sizeof(filename) );
if( ENVMAP ) Q_strcat( filename, "_ENVMAP", sizeof(filename) );
if( ENVMAPCAMERASPACE ) Q_strcat( filename, "_ENVMAPCAMERASPACE", sizeof(filename) );
if( ENVMAPSPHERE ) Q_strcat( filename, "_ENVMAPSPHERE", sizeof(filename) );
if( VERTEXCOLOR ) Q_strcat( filename, "_VERTEXCOLOR", sizeof(filename) );
// if( FOGTYPE ) Q_strcat( filename, "_FOGTYPE", sizeof(filename) );
if( ALPHATEST ) Q_strcat( filename, "_ALPHATEST", sizeof(filename) );
if( TRANSLUCENT ) Q_strcat( filename, "_TRANSLUCENT", sizeof(filename) );
if( COLORMODULATE ) Q_strcat( filename, "_COLORMODULATE", sizeof(filename) );
if( ALPHAMODULATE ) Q_strcat( filename, "_ALPHAMODULATE", sizeof(filename) );
if( BUMPMAP ) Q_strcat( filename, "_BUMPMAP", sizeof(filename) );
Q_strcat( filename, ".tga", sizeof(filename) );
ScreenShot( filename );
g_pProceduralMaterial->DecrementReferenceCount();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
//-----------------------------------------------------------------------------
// Render a frame
//-----------------------------------------------------------------------------
void RenderFrame( void )
{
TestLightmappedGeneric_dx80();
}
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE hInstance, LPSTR pCommands, INT )
{
SpewOutputFunc( SpewFunc );
CommandLine()->CreateCmdLine( ::GetCommandLine() );
InitDefaultFileSystem();
if( !CommandLine()->ParmValue( "-targetdir" ) )
{
Error( "Must specify -targetdir on command line!\n" );
}
struct _stat statbuf;
if( 0 != _stat( CommandLine()->ParmValue( "-targetdir" ), &statbuf ) )
{
if( 0 != _mkdir( CommandLine()->ParmValue( "-targetdir" ) ) )
{
Error( "Couldn't create path %s\n", CommandLine()->ParmValue( "-targetdir" ) );
}
}
// Must add 'bin' to the path....
char* pPath = getenv("PATH");
// Use the .EXE name to determine the root directory
char moduleName[ MAX_PATH ];
char szBuffer[ 4096 ];
if ( !GetModuleFileName( hInstance, moduleName, MAX_PATH ) )
{
MessageBox( 0, "Failed calling GetModuleFileName", "Launcher Error", MB_OK );
return 0;
}
// Get the root directory the .exe is in
char* pRootDir = GetBaseDir( moduleName );
#ifdef DBGFLAG_ASSERT
int len =
#endif