Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
6.1 KiB

  1. use String::CRC32;
  2. BEGIN {use File::Basename; push @INC, dirname($0); }
  3. require "valve_perl_helpers.pl";
  4. $dynamic_compile = defined $ENV{"dynamic_shaders"} && $ENV{"dynamic_shaders"} != 0;
  5. $depnum = 0;
  6. $baseSourceDir = ".";
  7. my %dep;
  8. sub GetAsmShaderDependencies_R
  9. {
  10. local( $shadername ) = shift;
  11. local( *SHADER );
  12. open SHADER, "<$shadername";
  13. while( <SHADER> )
  14. {
  15. if( m/^\s*\#\s*include\s+\"(.*)\"/ )
  16. {
  17. # make sure it isn't in there already.
  18. if( !defined( $dep{$1} ) )
  19. {
  20. $dep{$1} = 1;
  21. GetAsmShaderDependencies_R( $1 );
  22. }
  23. }
  24. }
  25. close SHADER;
  26. }
  27. sub GetAsmShaderDependencies
  28. {
  29. local( $shadername ) = shift;
  30. undef %dep;
  31. GetAsmShaderDependencies_R( $shadername );
  32. # local( $i );
  33. # foreach $i ( keys( %dep ) )
  34. # {
  35. # print "$shadername depends on $i\n";
  36. # }
  37. return keys( %dep );
  38. }
  39. sub GetShaderType
  40. {
  41. my $shadername = shift;
  42. my $shadertype;
  43. if( $shadername =~ m/\.vsh/i )
  44. {
  45. $shadertype = "vsh";
  46. }
  47. elsif( $shadername =~ m/\.psh/i )
  48. {
  49. $shadertype = "psh";
  50. }
  51. elsif( $shadername =~ m/\.fxc/i )
  52. {
  53. $shadertype = "fxc";
  54. }
  55. else
  56. {
  57. die;
  58. }
  59. return $shadertype;
  60. }
  61. sub GetShaderSrc
  62. {
  63. my $shadername = shift;
  64. if ( $shadername =~ m/^(.*)-----/i )
  65. {
  66. return $1;
  67. }
  68. else
  69. {
  70. return $shadername;
  71. }
  72. }
  73. sub GetShaderBase
  74. {
  75. my $shadername = shift;
  76. if ( $shadername =~ m/-----(.*)$/i )
  77. {
  78. return $1;
  79. }
  80. else
  81. {
  82. my $shadertype = &GetShaderType( $shadername );
  83. $shadername =~ s/\.$shadertype//i;
  84. return $shadername;
  85. }
  86. }
  87. sub DoAsmShader
  88. {
  89. my $argstring = shift;
  90. my $shadername = &GetShaderSrc( $argstring );
  91. my $shaderbase = &GetShaderBase( $argstring );
  92. my $shadertype = &GetShaderType( $argstring );
  93. my $incfile = "";
  94. if( $shadertype eq "fxc" || $shadertype eq "vsh" )
  95. {
  96. $incfile = $shadertype . "tmp9" . $g_tmpfolder . "\\$shaderbase.inc ";
  97. }
  98. my $vcsfile = $shaderbase . $g_vcsext;
  99. my $bWillCompileVcs = 1;
  100. if( ( $shadertype eq "fxc") && $dynamic_compile )
  101. {
  102. $bWillCompileVcs = 0;
  103. }
  104. if( $shadercrcpass{$argstring} )
  105. {
  106. $bWillCompileVcs = 0;
  107. }
  108. if( $bWillCompileVcs )
  109. {
  110. &output_makefile_line( $incfile . "shaders\\$shadertype\\$vcsfile: $shadername @dep\n") ;
  111. }
  112. else
  113. {
  114. # psh files don't need a rule at this point since they don't have inc files and we aren't compiling a vcs.
  115. if( $shadertype eq "fxc" || $shadertype eq "vsh" )
  116. {
  117. &output_makefile_line( $incfile . ": $shadername @dep\n") ;
  118. }
  119. }
  120. my $x360switch = "";
  121. my $moreswitches = "";
  122. if( !$bWillCompileVcs && $shadertype eq "fxc" )
  123. {
  124. $moreswitches .= "-novcs ";
  125. }
  126. if( $g_x360 )
  127. {
  128. $x360switch = "-x360";
  129. if( $bWillCompileVcs && ( $shaderbase =~ m/_ps20$/i ) )
  130. {
  131. $moreswitches .= "-novcs ";
  132. $bWillCompileVcs = 0;
  133. }
  134. }
  135. # if we are psh and we are compiling the vcs, we don't need this rule.
  136. if( !( $shadertype eq "psh" && !$bWillCompileVcs ) )
  137. {
  138. &output_makefile_line( "\tperl $g_SourceDir\\devtools\\bin\\" . $shadertype . "_prep.pl $moreswitches $x360switch -source \"$g_SourceDir\" $argstring\n") ;
  139. }
  140. if( $bWillCompileVcs )
  141. {
  142. &output_makefile_line( "\techo $shadername>> filestocopy.txt\n") ;
  143. my $dep;
  144. foreach $dep( @dep )
  145. {
  146. &output_makefile_line( "\techo $dep>> filestocopy.txt\n") ;
  147. }
  148. }
  149. &output_makefile_line( "\n") ;
  150. }
  151. if( scalar( @ARGV ) == 0 )
  152. {
  153. die "Usage updateshaders.pl shaderprojectbasename\n\tie: updateshaders.pl stdshaders_dx6\n";
  154. }
  155. $g_x360 = 0;
  156. $g_tmpfolder = "_tmp";
  157. $g_vcsext = ".vcs";
  158. while( 1 )
  159. {
  160. $inputbase = shift;
  161. if( $inputbase =~ m/-source/ )
  162. {
  163. $g_SourceDir = shift;
  164. }
  165. elsif( $inputbase =~ m/-x360/ )
  166. {
  167. $g_x360 = 1;
  168. $g_tmpfolder = "_360_tmp";
  169. $g_vcsext = ".360.vcs";
  170. }
  171. elsif( $inputbase =~ m/-execute/ )
  172. {
  173. $g_execute = 1;
  174. }
  175. elsif( $inputbase =~ m/-nv3x/ )
  176. {
  177. $nv3x = 1;
  178. }
  179. else
  180. {
  181. last;
  182. }
  183. }
  184. my @srcfiles = &LoadShaderListFile( $inputbase );
  185. open MAKEFILE, ">makefile\.$inputbase";
  186. open COPYFILE, ">makefile\.$inputbase\.copy";
  187. open INCLIST, ">inclist.txt";
  188. open VCSLIST, ">vcslist.txt";
  189. # make a default dependency that depends on all of the shaders.
  190. &output_makefile_line( "default: ") ;
  191. foreach $shader ( @srcfiles )
  192. {
  193. my $shadertype = &GetShaderType( $shader );
  194. my $shaderbase = &GetShaderBase( $shader );
  195. my $shadersrc = &GetShaderSrc( $shader );
  196. if( $shadertype eq "fxc" || $shadertype eq "vsh" )
  197. {
  198. # We only generate inc files for fxc and vsh files.
  199. my $incFileName = "$shadertype" . "tmp9" . $g_tmpfolder . "\\" . $shaderbase . "\.inc";
  200. &output_makefile_line( " $incFileName" );
  201. &output_inclist_line( "$incFileName\n" );
  202. }
  203. my $vcsfile = $shaderbase . $g_vcsext;
  204. my $compilevcs = 1;
  205. if( $shadertype eq "fxc" && $dynamic_compile )
  206. {
  207. $compilevcs = 0;
  208. }
  209. if( $g_x360 && ( $shaderbase =~ m/_ps20$/i ) )
  210. {
  211. $compilevcs = 0;
  212. }
  213. if( $compilevcs )
  214. {
  215. my $vcsFileName = "..\\..\\..\\game\\hl2\\shaders\\$shadertype\\$shaderbase" . $g_vcsext;
  216. # We want to check for perforce operations even if the crc matches in the event that a file has been manually reverted and needs to be checked out again.
  217. &output_vcslist_line( "$vcsFileName\n" );
  218. $shadercrcpass{$shader} = &CheckCRCAgainstTarget( $shadersrc, $vcsFileName, 0 );
  219. if( $shadercrcpass{$shader} )
  220. {
  221. $compilevcs = 0;
  222. }
  223. }
  224. if( $compilevcs )
  225. {
  226. &output_makefile_line( " shaders\\$shadertype\\$vcsfile" );
  227. # emit a list of vcs files to copy to the target since we want to build them.
  228. &output_copyfile_line( GetShaderSrc($shader) . "-----" . GetShaderBase($shader) . "\n" );
  229. }
  230. }
  231. &output_makefile_line( "\n\n") ;
  232. # Insert all of our vertex shaders and depencencies
  233. $lastshader = "";
  234. foreach $shader ( @srcfiles )
  235. {
  236. my $currentshader = &GetShaderSrc( $shader );
  237. if ( $lastshader ne $currentshader )
  238. {
  239. $lastshader = $currentshader;
  240. @dep = &GetAsmShaderDependencies( $lastshader );
  241. }
  242. &DoAsmShader( $shader );
  243. }
  244. close VCSLIST;
  245. close INCLIST;
  246. close COPYFILE;
  247. close MAKEFILE;
  248. # nuke the copyfile if it is zero length
  249. if( ( stat "makefile\.$inputbase\.copy" )[7] == 0 )
  250. {
  251. unlink "makefile\.$inputbase\.copy";
  252. }
  253. sub output_makefile_line
  254. {
  255. local ($_)=@_;
  256. print MAKEFILE $_;
  257. }
  258. sub output_copyfile_line
  259. {
  260. local ($_)=@_;
  261. print COPYFILE $_;
  262. }
  263. sub output_vcslist_line
  264. {
  265. local ($_)=@_;
  266. print VCSLIST $_;
  267. }
  268. sub output_inclist_line
  269. {
  270. local ($_)=@_;
  271. print INCLIST $_;
  272. }