⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vsh_prep.pl

📁 hl2 source code. Do not use it illegal.
💻 PL
📖 第 1 页 / 共 2 页
字号:
# NOTE: These must match the same values in macros.vsh!
$cModelViewProj0	= "c4";
$cModelViewProj1	= "c5";
$cModelViewProj2	= "c6";
$cModelViewProj3	= "c7";

$cModelView0		= "c12";
$cModelView1		= "c13";
$cModelView2		= "c14";
$cModelView3		= "c15";

$cModel0			= "c42";
$cModel1			= "c43";
$cModel2			= "c44";

# NOTE: These must match the same values in macros.vsh!
$vPos				= "v0";
$vBoneWeights		= "v1";
$vBoneIndices		= "v2";
$vNormal			= "v3";
$vColor				= "v5";
$vSpecular			= "v6";
$vTexCoord0			= "v7";
$vTexCoord1			= "v8";
$vTexCoord2			= "v9";
$vTexCoord3			= "v10";
$vTangentS			= "v11";
$vTangentT			= "v12";
$vUserData			= "v14";


sub ReadInputFile
{
	local( $filename ) = shift;
	local( *INPUT );
	local( @output );
	open INPUT, "<$filename" || die;

	local( $line );
	local( $linenum ) = 1;
	while( $line = <INPUT> )
	{
		$line =~ s/\n//g;
		local( $postfix ) = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
		$postfix .= "; LINEINFO($filename)($linenum)\n";
		if( $line =~ m/\#include\s+\"(.*)\"/i )
		{
			push @output, &ReadInputFile( $1 );
		}
		else
		{
			push @output, $line . $postfix;
		}
		$linenum++;
	}

	close INPUT;
	return @output;
}

sub IsPerl
{
	local( $line ) = shift;
	if( $line =~ m/^\s*sub.*\,/ )
	{
		return 0;
	}
	if( $line =~ m/^\s*if\s*\(/		||
		$line =~ m/^\s*else/		||
		$line =~ m/^\s*elsif/		||
		$line =~ m/^\s*for\s*\(/	||
		$line =~ m/^\s*\{/			||
		$line =~ m/^sub\s*/		||
		$line =~ m/^\s*\}/			||
		$line =~ m/^\s*\&/			||
		$line =~ m/^\s*\#/			||
		$line =~ m/^\s*\$/			||
		$line =~ m/^\s*print/			||
		$line =~ m/^\s*return/			||
		$line =~ m/^\s*exit/			||
		$line =~ m/^\s*die/			||
		$line =~ m/^\s*eval/			||
		$line =~ m/^\s*local/		 ||
		$line =~ m/^\s*alloc\s+/		||
		$line =~ m/^\s*free\s+/		
		)
	{
		return 1;
	}
	return 0;
}

# NOTE: These should match g_LightCombinations in vertexshaderdx8.cpp!
@g_staticLightTypeArray = ( "none", "static", 
				  "none", "none", "none", "none", "none", 
				  "none", "none", "none", "none", "none", 
				  "static", "static", "static", "static", "static", 
				  "static", "static", "static", "static", "static" );

@g_ambientLightTypeArray = ( "none", "none", 
				  "ambient", "ambient", "ambient", "ambient", "ambient", "ambient", 
				  "ambient", "ambient", "ambient", "ambient", 
				  "ambient", "ambient", "ambient", "ambient", "ambient", "ambient", 
				  "ambient", "ambient", "ambient", "ambient" );

@g_localLightType1Array = ( "none", "none", 
				  "none", "spot", "point", "directional", "spot", "spot", 
				  "spot", "point", "point", "directional",
				  "none", "spot", "point", "directional", "spot", "spot", 
				  "spot", "point", "point", "directional" );

@g_localLightType2Array = ( "none", "none", 
				  "none", "none", "none", "none", "spot", "point", 
				  "directional", "point", "directional", "directional",
				  "none", "none", "none", "none", "spot", "point", 
				  "directional", "point", "directional", "directional" );

# translate the output into something that takes us back to the source line
# that we care about in msdev
sub TranslateErrorMessages
{
	local( $origline );
	while( $origline = shift )
	{
		if( $origline =~ m/(.*)\((\d+)\)\s+:\s+(.*)$/i )
		{
			local( $filename ) = $1;
			local( $linenum ) = $2;
			local( $error ) = $3;
			local( *FILE );
			open FILE, "<$filename" || die;
			local( $i );
			local( $line );
			for( $i = 0; $i < $linenum; $i++ )
			{
				$line = <FILE>;
			}
			if( $line =~ m/LINEINFO\((.*)\)\((.*)\)/ )
			{
				print "$1\($2\) : $error\n";
				print "$filename\($linenum\) : original error location\n";
			}
			close FILE;
		}
		else
		{
			$origline =~ s/successful compile\!.*//gi;
			if( !( $origline =~ m/^\s*$/ ) )
			{
				#print $origline;
			}
		}
	}
}


sub CountInstructions
{
	local( $line );
	local( $count ) = 0;
	while( $line = shift )
	{
		# get rid of comments
		$line =~ s/;.*//gi;
		$line =~ s/\/\/.*//gi;
		# skip the vs1.1 statement
		$line =~ s/^\s*vs.*//gi;
		# if there's any text left, it's an instruction
		if( $line =~ /\S/gi )
		{
			$count++;
		}
	}
	return $count;
}

sub UsesRegister
{
	local( $registerName ) = shift;
	local( $line );
	while( $line = shift )
	{
#		print $line;
		# get rid of comments
		$line =~ s/;.*//gi;
		$line =~ s/\/\/.*//gi;
		# if there's any text left, it's an instruction
		if( $line =~ /\b$registerName\b/gi )
		{
			return 1;
		}
	}
	return 0;
}

sub PadString
{
	local( $str ) = shift;
	local( $desiredLen ) = shift;
	local( $len ) = length $str;
	while( $len < $desiredLen )
	{
		$str .= " ";
		$len++;
	}
	return $str;
}

sub FixupAllocateFree
{
	local( $line ) = shift;
	$line =~ s/\&AllocateRegister\s*\(\s*\\(\S+)\s*\)/&AllocateRegister( \\$1, \"\\$1\" )/g;
	$line =~ s/\&FreeRegister\s*\(\s*\\(\S+)\s*\)/&FreeRegister( \\$1, \"\\$1\" )/g;
	$line =~ s/alloc\s+(\S+)\s*/local( $1 ); &AllocateRegister( \\$1, \"\\$1\" );/g;
	$line =~ s/free\s+(\S+)\s*/&FreeRegister( \\$1, \"\\$1\" );/g;
	return $line;
}

# This is used to make the generated pl files all pretty.
sub GetLeadingWhiteSpace
{
	local( $str ) = shift;
	if( $str =~ m/^;\S/ || $str =~ m/^; \S/ )
	{
		return "";
	}
	$str =~ s/^;/ /g; # count a leading ";" as whitespace as far as this is concerned.
	$str =~ m/^(\s*)/;
	return $1;
}

$filename = shift;
if( $filename =~ m/-dx9/i )
{
	$g_dx9 = 1;
	$filename = shift;
	$vshtmp = "vshtmp9";
}
else
{
	$vshtmp = "vshtmp8";
}

if( !stat $vshtmp )
{
	mkdir $vshtmp, 0777 || die $!;
}

# suck in all files, including $include files.
@input = &ReadInputFile( $filename );

# See if it uses skinning and/or lighting
$usesSkinning = 0;
$usesLighting = 0;
$usesHeightFog = 0;
for( $i = 0; $i < scalar( @input ); $i++ )
{
	$line = $input[$i];
	# remove comments
	$line =~ s/\#.*$//gi;
	$line =~ s/;.*$//gi;
	if( $line =~ m/\$g_numBones/ )
	{
		$usesSkinning = 1;
	}
	if( $line =~ m/\$g_staticLightType/ ||
		$line =~ m/\$g_ambientLightType/ ||
		$line =~ m/\$g_localLightType1/ ||
		$line =~ m/\$g_localLightType2/ )
	{
		$usesLighting = 1;
	}
	if( $line =~ m/\$g_fogType/ )
	{
		$usesHeightFog = 1;
	}
}

# Translate the input into a perl program that'll unroll everything and
# substitute variables.
while( $inputLine = shift @input )
{
	$inputLine =~ s/\n//g;
	# leave out lines that are only whitespace.
	if( $inputLine =~ m/^\s*; LINEINFO.*$/ )
	{
		next;
	}
	local( $inputLineNoLineNum ) = $inputLine;
	$inputLineNoLineNum =~ s/; LINEINFO.*//gi;
	if( &IsPerl( $inputLineNoLineNum ) )
	{
		$inputLineNoLineNum = &FixupAllocateFree( $inputLineNoLineNum );
		push @outputProgram, $inputLineNoLineNum . "\n";
	}
	else
	{
		# make asm lines that have quotes in them not barf.
		$inputLine =~ s/\"/\\\"/g;
		push @outputProgram, &GetLeadingWhiteSpace( $inputLine ) . "push \@output, \"" . 
			$inputLine . "\\n\";\n";
	}
}

$outputProgram = join "", @outputProgram;

$filename_base = $filename;
$filename_base =~ s/\.vsh//i;

open DEBUGOUT, ">$vshtmp" . "/$filename_base.pl" || die;
print DEBUGOUT $outputProgram;
close DEBUGOUT;
#print $outputProgram;

if( $usesSkinning )
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -