-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCompiler.cpp
More file actions
1080 lines (1027 loc) · 29.6 KB
/
Compiler.cpp
File metadata and controls
1080 lines (1027 loc) · 29.6 KB
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
#undef _HAS_STD_BYTE
#include "Compiler.h"
#include "SfxEffect.h"
#include "StringFunctions.h"
#include "StringToWString.h"
#include "FileLoader.h"
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <sstream> // for ostringstream
#include <cstdio>
#include <cassert>
#include <fstream>
#include <iostream>
#include <regex>
#include <functional>
#if PLATFORM_STD_FILESYSTEM==1
#include <filesystem>
namespace fs = std::filesystem;
#elif PLATFORM_STD_FILESYSTEM==2
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
#ifndef _MSC_VER
typedef int errno_t;
#include <errno.h>
#endif
#ifdef _MSC_VER
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h>
#include <io.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "Sfx.h"
#include "SfxClasses.h"
#ifdef _MSC_VER
#define YY_NO_UNISTD_H
#include <windows.h>
#include <direct.h>
#else
typedef struct stat Stat;
#endif
#include "GeneratedFiles/SfxScanner.h"
#include "SfxProgram.h"
#include "StringFunctions.h"
#include "StringToWString.h"
#include "SfxEffect.h"
#include "SfxErrorCheck.h"
#include "Preprocessor.h"
using namespace std;
typedef std::function<void(const std::string &)> OutputDelegate;
extern bool ShaderInstanceHasSemantic(std::shared_ptr<ShaderInstance> shaderInstance, const char* semantic);
#if 0
static std::string WStringToUtf8(const wchar_t *src_w)
{
int src_length=(int)wcslen(src_w);
#ifdef _MSC_VER
int size_needed = WideCharToMultiByte(CP_UTF8, 0,src_w, (int)src_length, NULL, 0, NULL, NULL);
#else
int size_needed=2*src_length;
#endif
char *output_buffer = new char [size_needed+1];
#ifdef _MSC_VER
WideCharToMultiByte (CP_UTF8,0,src_w,(int)src_length,output_buffer, size_needed, NULL, NULL);
#else
wcstombs(output_buffer, src_w, (size_t)size_needed );
#endif
output_buffer[size_needed]=0;
std::string str_utf8=std::string(output_buffer);
delete [] output_buffer;
return str_utf8;
}
#endif
static void FixRelativePaths(std::string &str,const std::string &sourcePathUtf8)
{
int pos=0;
int eol=(int)str.find("\n");
if(eol<0)
eol=(int)str.length();
while(eol>=0)
{
string line=str.substr(pos,eol-pos);
if(line[0]=='.')
{
line=sourcePathUtf8+line;
str.replace(pos,eol-pos,line);
}
pos=(int)str.find("\n",pos+1);
if(pos<0)
pos=(int)str.length();
else
pos++;
eol=(int)str.find("\n",pos);
}
}
bool terminate_command=false;
bool command_running=false;
bool RunDOSCommand(const wchar_t *wcommand, const string &sourcePathUtf8, ostringstream& log,const SfxConfig &sfxConfig, OutputDelegate outputDelegate)
{
if(terminate_command)
return false;
bool has_errors=false;
#ifndef _MSC_VER
int res=system(WStringToString(wcommand).c_str());
has_errors=(res!=0);
#else
bool pipe_compiler_output=true;
STARTUPINFOW startInfo;
PROCESS_INFORMATION processInfo;
ZeroMemory( &startInfo, sizeof(startInfo) );
startInfo.cb = sizeof(startInfo);
ZeroMemory( &processInfo, sizeof(processInfo) );
wchar_t com[7500];
wcscpy_s(com,wcommand);
startInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startInfo.wShowWindow = SW_HIDE;
HANDLE hReadOutPipe = NULL;
HANDLE hWriteOutPipe = NULL;
HANDLE hReadErrorPipe = NULL;
HANDLE hWriteErrorPipe = NULL;
SECURITY_ATTRIBUTES secAttrib;
// Set the bInheritHandle flag so pipe handles are inherited.
if(pipe_compiler_output)
{
secAttrib.nLength = sizeof(SECURITY_ATTRIBUTES);
secAttrib.bInheritHandle = TRUE;
secAttrib.lpSecurityDescriptor = NULL;
if(!CreatePipe( &hReadOutPipe, &hWriteOutPipe, &secAttrib, 1200 )||!CreatePipe( &hReadErrorPipe, &hWriteErrorPipe, &secAttrib, 1200 ))
{
SFX_BREAK("Failed to create pipes.");
}
}
startInfo.hStdOutput = hWriteOutPipe;
startInfo.hStdError = hWriteErrorPipe;
if(!CreateProcessW( NULL, // No module name (use command line)
com, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, //CREATE_NO_WINDOW, // No nasty console windows
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startInfo, // Pointer to STARTUPINFO structure
&processInfo ) // Pointer to PROCESS_INFORMATION structure
)
{
log << "Failed to create process:" << WStringToUtf8(com) << std::endl;
return false;
}
// Wait until child process exits.
if(processInfo.hProcess==nullptr)
{
log <<"Error: Could not find the executable for "<<WStringToUtf8(com)<<std::endl;
return false;
}
command_running=true;
HANDLE WaitHandles[] = {
processInfo.hProcess, hReadOutPipe, hReadErrorPipe
};
const DWORD BUFSIZE = 4096;
BYTE buff[BUFSIZE];
while (1)
{
if(terminate_command)
{
std::cerr<<"Terminating process.\n";
if(TerminateProcess(processInfo.hProcess,1))
{
break;
}
}
DWORD dwBytesRead, dwBytesAvailable=100000;
DWORD numObjects = pipe_compiler_output ? 3 : 1;
DWORD dwWaitResult = WaitForMultipleObjects(numObjects, WaitHandles, FALSE, 20000L);
// Read from the pipes...
if(pipe_compiler_output)
{
while( PeekNamedPipe(hReadOutPipe, NULL, 0, NULL, &dwBytesAvailable, NULL) && dwBytesAvailable )
{
ReadFile(hReadOutPipe, buff, BUFSIZE-1, &dwBytesRead, 0);
std::string str((char*)buff, (size_t)dwBytesRead);
outputDelegate(str);
if(terminate_command)
break;
}
while( PeekNamedPipe(hReadErrorPipe, NULL, 0, NULL, &dwBytesAvailable, NULL) && dwBytesAvailable )
{
ReadFile(hReadErrorPipe, buff, BUFSIZE-1, &dwBytesRead, 0);
std::string str((char*)buff, (size_t)dwBytesRead);
outputDelegate(str);
// Force to failed if error...
if(sfxConfig.failOnCerr)
has_errors = true;
if(terminate_command)
break;
}
}
// Process is done, or we timed out:
if (dwWaitResult == WAIT_TIMEOUT)
{
log << "Timeout executing " << WStringToString(com)<< std::endl;
has_errors = true;
}
if (dwWaitResult == WAIT_OBJECT_0 || dwWaitResult == WAIT_TIMEOUT)
break;
//if (dwWaitResult > WAIT_OBJECT_0 && dwWaitResult < WAIT_OBJECT_0 + numObjects)
//{
//SFX_CERR << "Pipe closed: " << dwWaitResult << std::endl;
//pipe_compiler_output = false;
//}
if (dwWaitResult == WAIT_FAILED)
{
DWORD err = GetLastError();
char* msg;
// Ask Windows to prepare a standard message for a GetLastError() code:
if (!FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL))
break;
log <<"Error message: "<<msg<<std::endl;
{
CloseHandle( processInfo.hProcess );
CloseHandle( processInfo.hThread );
exit(21);
}
}
}
command_running=false;
if(terminate_command)
exit(1828);
DWORD exitCode=0;
if(!GetExitCodeProcess(processInfo.hProcess, &exitCode))
{
log << "Failed to get exit code for command: "<< WStringToUtf8(wcommand).c_str()<< std::endl;
return false;
}
if(exitCode!=0&&exitCode!= 0xc0000005)//0xc0000005 is a bullshit error dxc occasionally throws up for no good reason.
{
log << "Exit code: 0x" <<std::hex<< (int)exitCode << std::endl;
has_errors=true;
}
//WaitForSingleObject( processInfo.hProcess, INFINITE );
CloseHandle( processInfo.hProcess );
CloseHandle( processInfo.hThread );
if(has_errors)
TerminateProcess(processInfo.hProcess,1);
#endif
if(has_errors)
{
log << "Errors from command: " << WStringToUtf8(wcommand).c_str() << std::endl;
return false;
}
return true;
}
using namespace sfx;
using std::string;
extern std::vector<std::string> extra_arguments;
string ToString(PixelOutputFormat pixelOutputFormat)
{
switch(pixelOutputFormat)
{
case FMT_UNKNOWN:
return "";
case FMT_32_GR:
return "32gr";
case FMT_32_AR:
return "32ar";
case FMT_FP16_ABGR:
return "float16abgr";
case FMT_UNORM16_ABGR:
return "unorm16abgr";
case FMT_SNORM16_ABGR:
return "snorm16abgr";
case FMT_UINT16_ABGR:
return "uint16abgr";
case FMT_SINT16_ABGR:
return "sint16abgr";
case FMT_32_ABGR:
return "float32abgr";
default:
return "invalid";
}
}
string ToPragmaString(PixelOutputFormat pixelOutputFormat)
{
switch(pixelOutputFormat)
{
case FMT_UNKNOWN:
return "FMT_UNKNOWN";
case FMT_32_GR:
return "FMT_32_GR";
case FMT_32_AR:
return "FMT_32_AR";
case FMT_FP16_ABGR:
return "FMT_FP16_ABGR";
case FMT_UNORM16_ABGR:
return "FMT_UNORM16_ABGR";
case FMT_SNORM16_ABGR:
return "FMT_SNORM16_ABGR";
case FMT_UINT16_ABGR:
return "FMT_UINT16_ABGR";
case FMT_SINT16_ABGR:
return "FMT_SINT16_ABGR";
case FMT_32_ABGR:
return "FMT_32_ABGR";
default:
return "FMT_UNKNOWN";
}
}
static int do_mkdir(const wchar_t *path_w)
{
int status = 0;
#ifdef _MSC_VER
struct _stat64i32 st;
std::wstring wstr=(path_w);
if (_wstat (wstr.c_str(), &st) != 0)
#else
std::string path_utf8=WStringToString(path_w);
Stat st;
if (stat(path_utf8.c_str(), &st)!=0)
#endif
{
/* Directory does not exist. EEXIST for race condition */
#ifdef _MSC_VER
// no slash? It's not a directory.
if(wstr.find_first_of(L'/')>=wstr.length()&&wstr.find_first_of(L'\\')>=wstr.length())
{
errno=0;
return 0;
}
if (_wmkdir(wstr.c_str()) != 0 && errno != EEXIST)
#else
if (mkdir(path_utf8.c_str(),S_IRWXU) != 0 && errno != EEXIST)
#endif
status = -1;
}
else if (!(st.st_mode & S_IFDIR))
{
//errno = ENOTDIR;
status = -1;
}
errno=0;
return(status);
}
static int nextslash(const std::wstring &str,int pos)
{
int slash=(int)str.find(L'/',pos);
int back=(int)str.find(L'\\',pos);
if(slash<0||(back>=0&&back<slash))
slash=back;
return slash;
}
int mkpath(const std::wstring &filename_utf8)
{
int status = 0;
int pos=1;// skip first / in Unix pathnames.
while (status == 0 && (pos = nextslash(filename_utf8,pos))>=0)
{
status = do_mkdir(filename_utf8.substr(0,pos).c_str());
pos++;
}
return (status);
}
string FillInVariablesSM51(const string &src,std::shared_ptr<ShaderInstance> shaderInstance)
{
std::regex param_re("\\{shader_model\\}");
string ret=src;
if (shaderInstance)
{
std::string sm = "";
switch (shaderInstance->shaderType)
{
case ShaderType::VERTEX_SHADER:
sm = "vs_5_1";
break;
case ShaderType::FRAGMENT_SHADER:
sm = "ps_5_1";
break;
case ShaderType::COMPUTE_SHADER:
sm = "cs_5_1";
break;
default:
sm = "null_null";
break;
}
ret = std::regex_replace(src, param_re, sm);
}
return ret;
}
string FillInVariable(const string &src, const std::string &var,const std::string &rep)
{
std::regex param_re(string("\\{")+var+"\\}");
string ret = std::regex_replace(src, param_re, rep);
return ret;
}
string FillInVariables(const string &src, std::shared_ptr<ShaderInstance> shaderInstance)
{
std::regex param_re("\\{shader_model\\}");
string ret = src;
if (shaderInstance)
ret = std::regex_replace(src, param_re, shaderInstance->m_profile);
return ret;
}
void ReplaceRegexes(string &src, const std::map<string,string> &replace)
{
for (auto i : replace)
{
std::regex param_re(std::string("\\b")+i.first+"\\b");
src = std::regex_replace(src, param_re, i.second);
}
}
static std::map<std::string,std::string> useCompilerPath;
static bool checkedCompilerPath=false;
extern void CalcCompilerPathToUse(const SfxConfig &sfxConfig,const SfxOptions &sfxOptions)
{
std::string usePath="";
std::string compiler_exe = sfxConfig.compiler;
size_t space_pos=compiler_exe.find(" ");
if(space_pos<compiler_exe.size())
{
compiler_exe=compiler_exe.substr(0,space_pos);
}
size_t dot_pos=compiler_exe.find(".");
if(dot_pos>= compiler_exe.size())
{
compiler_exe+=".exe";
}
for(const auto &p:sfxConfig.compilerPaths)
{
std::filesystem::path pth(p);
pth=pth.append(compiler_exe);
pth=pth.lexically_normal();
if(sfxOptions.verbose)
std::cout << "Checking: " << pth.generic_string().c_str() << std::endl;
if(fs::exists(pth))
{
if(sfxOptions.verbose)
std::cout << "Using: " << pth.generic_string().c_str() << std::endl;
useCompilerPath[sfxConfig.api]=usePath=p;
break;
}
}
}
wstring BuildCompileCommand(std::shared_ptr<ShaderInstance> shaderInstance,const SfxConfig &sfxConfig,const SfxOptions &sfxOptions,wstring targetDir,wstring outputFile,
wstring generatedSourceFilename,ShaderType t, PixelOutputFormat pixelOutputFormat)
{
if (sfxConfig.compiler.empty())
return L"";
if(useCompilerPath.find(sfxConfig.api)==useCompilerPath.end())
{
CalcCompilerPathToUse(sfxConfig,sfxOptions);
}
// std::cout<< "Using path " << useCompilerPath.c_str() << std::endl;
wstring command;
string stageName = "NO_STAGES_IN_JSON";
// Add the exe path
auto st=sfxConfig.stages.find((int)t);
// If there are any stages explicit in the json file, we must find one. Otherwise, assume {stage} is not in the command.
if(sfxConfig.stages.size())
{
if (st != sfxConfig.stages.end())
stageName=st->second;
else
return L"";
}
std::string currentCompiler = FillInVariable(sfxConfig.compiler,"stage",stageName);
if(useCompilerPath[sfxConfig.api].length())
currentCompiler=useCompilerPath[sfxConfig.api]+"/"s+currentCompiler;
command += Utf8ToWString(currentCompiler) ;
// Add additional options (requested from the XX.json)
string options;
if (sfxConfig.forceSM51)
{
options = FillInVariablesSM51(sfxConfig.defaultOptions, shaderInstance);
}
else
{
options = FillInVariables(sfxConfig.defaultOptions, shaderInstance);
}
command += wstring(L" ") + Utf8ToWString(options);
// Add extra arguments (this have the include dirs)
for (auto i : extra_arguments)
{
command += L" ";
std::string str=i;
size_t Ipos = str.find("-I");
if(sfxConfig.includeOption.length()>0&&Ipos==0)
{
string newArgument;
str.replace(str.begin(), str.begin() + 2,sfxConfig.includeOption);
size_t pos = str.find(';');
str.replace(str.begin() + pos, str.begin() + pos + 1, ",");
}
command += Utf8ToWString(str);
}
command += L" ";
// Add entry point option
if (sfxConfig.entryPointOption.length() && shaderInstance->m_profile.find("lib_6_") == std::string::npos)
{
command += Utf8ToWString(std::regex_replace(sfxConfig.entryPointOption, std::regex("\\{name\\}"), shaderInstance->entryPoint)) + L" ";
}
string filename_root=WStringToString(outputFile);
size_t dot_pos = filename_root.rfind(".");
if(dot_pos<filename_root.size())
filename_root=filename_root.substr(0,dot_pos);
if(sfxOptions.debugInfo)
{
command+=Utf8ToWString(sfxConfig.debugOption)+L" ";
if (sfxConfig.debugOutputFileOption.length())
{
command += Utf8ToWString(std::regex_replace(sfxConfig.debugOutputFileOption, std::regex("\\{filename_root\\}"), filename_root)) + L" ";
}
}
else
{
if(sfxConfig.releaseOptions.length())
{
command += Utf8ToWString(std::regex_replace(sfxConfig.releaseOptions, std::regex("\\{filename_root\\}"), filename_root)) + L" ";
}
}
if(sfxConfig.optimizationLevelOption.length()&&sfxOptions.optimizationLevel>=0)
{
command += Utf8ToWString(sfxConfig.optimizationLevelOption);
wchar_t ostr[]=L"0 ";
ostr[0]=L'0'+(wchar_t)sfxOptions.optimizationLevel;
command += ostr;
}
if (sfxConfig.outputOption.size())
{
command += Utf8ToWString(sfxConfig.outputOption);
command += L"\"";
command += outputFile;
command += L"\" ";
}
// Input argument
// The switch compiler needs an extension
/*
// Check if we are generating GLSL
bool isGlSL = sfxConfig.sourceExtension == "glsl";
if (isGlSL)
{
switch (t)
{
case sfx::EXPORT_SHADER:
case sfx::VERTEX_SHADER:
command += L"--vertex-shader=";
break;
case sfx::FRAGMENT_SHADER:
command += L"--pixel-shader=";
break;
case sfx::COMPUTE_SHADER:
command += L"--compute-shader=";
break;
}
}*/
command += L"\"";
command += generatedSourceFilename.c_str();
command += L"\"";
return command;
}
bool RewriteOutput(const SfxConfig &sfxConfig
,const SfxOptions &sfxOptions, string sourcePathUtf8,const map<int,string> &fileList,ostringstream *log,string str)
{
if(sfxConfig.compilerMessageRegex.size())
{
try
{
std::regex re(sfxConfig.compilerMessageRegex.c_str(), std::regex_constants::icase | std::regex::extended);
std::smatch base_match;
while (std::regex_search(str, base_match, re))
{
str = std::regex_replace(str, re, sfxConfig.compilerMessageReplace + "\n");
base_match = std::smatch();
}
}
catch (std::exception &)
{
}
catch (...)
{
}
}
if(!sfxConfig.lineDirectiveFilenames)
{
// If we have a number followed by a bracket at the start
std::smatch base_match;
std::regex re("([0-9]+)\\(([0-9?]+)\\):");
while(std::regex_search(str,base_match,re))
{
string filenum=str.substr(base_match[1].first-str.begin(),base_match[1].length());
int num=atoi(filenum.c_str());
auto f=fileList.find(num);
string filename="unknown file";
if(f!=fileList.end())
{
filename=f->second;
}
str.replace(base_match[1].first,base_match[1].second,filename);
base_match=std::smatch();
}
}
bool has_errors=false;
size_t pos = str.find("Error");
if(pos>=str.length())
pos = str.find("error");
if(pos>=str.length())
pos=str.find("failed");
if(pos<str.length())
has_errors=true;
FixRelativePaths(str,sourcePathUtf8);
int bracket_pos=(int)str.find("(");
if(bracket_pos>0)
{
int close_bracket_pos =(int)str.find(")",bracket_pos);
int comma_pos =(int)str.find(",",bracket_pos);
if(comma_pos>bracket_pos&&comma_pos<close_bracket_pos)
{
str.replace(comma_pos,close_bracket_pos-comma_pos,"");
}
}
(*log)<< str.c_str();
return has_errors;
}
#pragma optimize("",off)
bool IsShaderUnchanged(std::string sourceFilename,std::string outputFilename,const std::string &src)
{
if(!std::filesystem::exists(outputFilename))
return false;
if(!std::filesystem::exists(sourceFilename))
return false;
std::filesystem::file_time_type binary_time = std::filesystem::last_write_time(outputFilename);
std::filesystem::file_time_type source_time = std::filesystem::last_write_time(sourceFilename);
if(binary_time<=source_time)
return false;
std::ostringstream sstr;
ifstream ifs(sourceFilename.c_str());;
sstr << ifs.rdbuf();
if(src!=sstr.str())
return false;
return true;
}
int Compile(std::shared_ptr<ShaderInstance> shaderInstance
,const string &sourceFile
,string targetFile
,ShaderType t
,PixelOutputFormat pixelOutputFormat
,const string &sharedSource
,ostringstream& sLog
,const SfxConfig &sfxConfig
,const SfxOptions &sfxOptions
,map<int,string> fileList
,std::ofstream &combinedBinary
,BinaryMap &binaryMap
,const Declaration* rtState )
{
string filenameOnly = GetFilenameOnly( sourceFile);
wstring targetFilename=StringToWString(filenameOnly);
int pos = (int)targetFilename.rfind(L".");
if(pos>=0)
targetFilename=targetFilename.substr(0,pos);
if(shaderInstance->variantName.size())
targetFilename+=L"_"+StringToWString(shaderInstance->variantName);
else
targetFilename+=L"_"+StringToWString(shaderInstance->m_functionName);
// Add the preamble:
string preamble = "";
preamble += sfxConfig.preamble + "\n";
if(t==COMPUTE_SHADER)
preamble += sfxConfig.computePreamble;
// Pssl recognizes the shaderInstance type using a suffix to the filename, before the .pssl extension:
wstring shaderTypeSuffix;
switch(t)
{
case EXPORT_SHADER:
shaderTypeSuffix=L"ve";
break;
case VERTEX_SHADER:
shaderTypeSuffix=L"vv";
break;
case TESSELATION_CONTROL_SHADER: //SetHullShader:
break;
case TESSELATION_EVALUATION_SHADER://SetDomainShader:
break;
case GEOMETRY_SHADER:
shaderTypeSuffix=L"g";
break;
case FRAGMENT_SHADER:
{
if (rtState && pixelOutputFormat == FMT_UNKNOWN)
{
RenderTargetFormatState* rt = (RenderTargetFormatState*)rtState;
for (int i = 0; i < 8; i++)
{
if (rt->formats[i] != FMT_UNKNOWN)
{
preamble += "#pragma PSSL_target_output_format(target " + std::to_string(i) + " ";
preamble += ToPragmaString(rt->formats[i]);
preamble += ")\n";
}
}
shaderTypeSuffix = StringToWString(rt->name) + L"_p";
}
else if(pixelOutputFormat!=FMT_UNKNOWN)
{
string frm=ToString(pixelOutputFormat);
preamble+="#pragma PSSL_target_output_format(default ";
preamble+=ToPragmaString(pixelOutputFormat);
preamble+=")\n";
if(frm=="invalid")
return 0;
if(frm.size())
shaderTypeSuffix=StringToWString(frm)+L"_";
shaderTypeSuffix+=L"p";
}
}
break;
case COMPUTE_SHADER:
shaderTypeSuffix=L"c";
break;
case RAY_GENERATION_SHADER:
case MISS_SHADER:
case CALLABLE_SHADER:
case CLOSEST_HIT_SHADER:
case ANY_HIT_SHADER:
case INTERSECTION_SHADER:
{
if (sfxConfig.supportRaytracing)
{
break;
}
else
{
auto RTShaderTypeToStr = [](const ShaderType t) -> const char*
{
switch (t)
{
case RAY_GENERATION_SHADER: return "RAY_GENERATION";
case MISS_SHADER: return "MISS";
case CALLABLE_SHADER: return "CALLABLE";
case CLOSEST_HIT_SHADER: return "CLOSEST_HIT";
case ANY_HIT_SHADER: return "ANY_HIT";
case INTERSECTION_SHADER: return "INTERSECTION";
default: return "NON_RT";
}
};
std::cout << "Warning: Raytracing shaderInstance not supported. Type: " << RTShaderTypeToStr(t) << " Name: " << shaderInstance->m_functionName << ".\n";
return 1; //Return 1 here, not compiling raytracing shaders is okay.
}
}
default:
break;
};
if(shaderTypeSuffix.length())
targetFilename+=L"_"+shaderTypeSuffix;
//Check for multiview compatibility
bool multiview = false;
multiview |= ShaderInstanceHasSemantic(shaderInstance, "SV_ViewID");
multiview |= ShaderInstanceHasSemantic(shaderInstance, "SV_ViewId");
if (!sfxConfig.supportMultiview && multiview)
{
std::cout << "Warning: Multiview shaderInstance not supported. Name: " << shaderInstance->m_functionName << ".\n";
return 1; //Return 1 here, not compiling multiview shaders is okay.
}
// Add the root signature (we want to keep it at the top of the file):
if (!sfxConfig.graphicsRootSignatureSource.empty())
{
FileLoader fileLoader;
const char* rsSourcePath = fileLoader.FindFileInPathStack(sfxConfig.graphicsRootSignatureSource.c_str(), sfxConfig.shaderPaths);
if (!rsSourcePath)
{
std::cerr << "This platform requires a rootsignature file but no file was found in the shaderInstance paths" << std::endl;
return 0;
}
std::ifstream rootSrcFile(rsSourcePath);
std::string rootSrc((std::istreambuf_iterator<char>(rootSrcFile)), (std::istreambuf_iterator<char>()));
preamble += rootSrc;
}
wstring generatedSourceFilename ;
if(sfxOptions.intermediateDirectory.length())
generatedSourceFilename+= StringToWString(sfxOptions.intermediateDirectory+ "/");
generatedSourceFilename+=targetFilename+wstring(L".")+Utf8ToWString(sfxConfig.sourceExtension);
char buffer[_MAX_PATH];
string wd="";
#ifdef _MSC_VER
if(_getcwd(buffer,_MAX_PATH))
#else
if(getcwd(buffer,_MAX_PATH))
#endif
wd=string(buffer)+"/";
string tempf=WStringToUtf8(generatedSourceFilename);
if(tempf.find(":")>=tempf.length())
tempf=wd+"/"+tempf;
find_and_replace(tempf,"\\","/");
find_and_replace(wd,"\\","/");
if (gEffect->GetOptions()->disableLineWrites)
preamble += "//";
size_t lineno=count_lines_in_string(preamble)+1;// add 1 because we're inserting a line into the same file.
preamble+=stringFormat("#line %d \"%s\"\n",lineno,tempf.c_str());
preamble += shaderInstance->m_preamble;
if(t==COMPUTE_SHADER)
{
if (sfxConfig.computePreamble.empty())
{
preamble += "#define USE_COMPUTE_SHADER 1\n";
}
}
for(auto i:sfxConfig.define)
{
preamble+="#define "+i.first;
preamble+=" "+i.second;
preamble+="\n";
}
for(auto i:sfxConfig.keywords)
{
preamble+="#define "+i.first;
preamble+=" "+i.second;
preamble+="\n";
}
// Shader source!
string src = preamble;
src += sharedSource;
src+=shaderInstance->m_augmentedSource;
const char *strSrc=src.c_str();
wstring targetDir=StringToWString(GetDirectoryFromFilename(targetFile));
if(targetDir.size())
targetDir+=L"/";
mkpath(targetDir);
mkpath(StringToWString(sfxOptions.intermediateDirectory)+L"/");
wstring outputFile = ((sfxOptions.wrapOutput ? StringToWString(sfxOptions.intermediateDirectory) : targetDir) + L"/") + (targetFilename + L".") + Utf8ToWString(sfxConfig.outputExtension);
// Now: if the source we want to save is the same as the file that's there,
// AND the date of the target file is AFTER the date of the source, that means
// that this shader hasn't changed, even if the sfx file has.
// Therefore we don't compile it.
bool unchanged=IsShaderUnchanged(WStringToUtf8(generatedSourceFilename),WStringToUtf8(outputFile),src);
if(!unchanged)
{
#ifdef _MSC_VER
ofstream ofs(generatedSourceFilename.c_str());
#else
ofstream ofs(WStringToUtf8(generatedSourceFilename).c_str());
#endif
ofs.write(strSrc, strlen(strSrc));
ofs.close();
}
#ifdef _MSC_VER
// Now delete the corresponding sdb's
wstring sdbFile=generatedSourceFilename.substr(0,generatedSourceFilename.length()-5);
WIN32_FIND_DATAW fd;
HANDLE hFind = FindFirstFileW((sdbFile+L"*.sdb").c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
DeleteFileW(((targetDir)+ fd.cFileName).c_str());
} while (FindNextFileW(hFind, &fd));
FindClose(hFind);
}
#endif
// Add the output filename
int slash = (int)outputFile.rfind(L"/");
int backslash = (int)outputFile.rfind(L"\\");
if (backslash > slash)
slash = backslash;
string sbf = WStringToUtf8(outputFile.substr(slash + 1, outputFile.length() - slash - 1).c_str());
if (t == FRAGMENT_SHADER)
{
shaderInstance->sbFilenames[pixelOutputFormat] = sbf;
}
else if (t == EXPORT_SHADER)
{
shaderInstance->sbFilenames[1] = sbf;
}
else
shaderInstance->sbFilenames[0] = sbf;
ostringstream log;
// Get the compile command
wstring compile_command;
if(!unchanged)
{
compile_command=BuildCompileCommand
(
shaderInstance,
sfxConfig,
sfxOptions,
targetDir,
outputFile,
generatedSourceFilename,
t,
pixelOutputFormat
);
// If no compiler provided, we can return now (perhaps we are only interested in
// the shader source)
if (compile_command.empty())
{
if(sfxOptions.verbose)
std::cout<<WStringToUtf8(generatedSourceFilename).c_str()<<"\n";
// But let's in that case, wrap up the GENERATED SOURCE in sfxb:
if (sfxOptions.wrapOutput)
{
//concatenate
#ifdef _MSC_VER
std::ifstream if_c(generatedSourceFilename.c_str(), std::ios_base::binary);
#else
std::ifstream if_c(WStringToUtf8(generatedSourceFilename).c_str(), std::ios_base::binary);
#endif
if(!if_c.good())
{
SFX_BREAK("Failed to load generated shader source");
exit(1002);
}
std::streampos startp = combinedBinary.tellp();
combinedBinary << if_c.rdbuf();
std::streampos endp = combinedBinary.tellp();
size_t sz=endp - startp;
if(!sz)
{
SFX_BREAK("Empty output shader ");
std::cerr<<"Empty output shader "<<WStringToUtf8(generatedSourceFilename)<<"\n";
exit(1001);
}
binaryMap[sbf] = std::make_tuple(startp, sz);
}
return true;
}
if(sfxOptions.verbose)
std::cout<<WStringToUtf8(compile_command).c_str()<<std::endl;
}
// Run the provided .exe!
OutputDelegate cc=std::bind(&RewriteOutput,sfxConfig,sfxOptions,wd,fileList,&log,std::placeholders::_1);
bool created_output=false;
bool res=false;
int repetitions=0;
while(!created_output)
{
bool write_log=false;
if(!unchanged)
{
res=RunDOSCommand(compile_command.c_str(),wd,log,sfxConfig,cc);
const string &log_str=log.str();
if (sfxOptions.verbose)
{
std::cout << tempf.c_str() << "(0): info: Temporary shader source file." << std::endl;