Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shadercompiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ set(_SOURCES

set(_TEST_SOURCES
tests/Bindless.cpp
tests/DeterminismTest.cpp
tests/ForwardDeclarationTest.cpp
tests/GlobalInputTest.cpp
tests/MetalConversionTest.cpp
Expand Down
4 changes: 2 additions & 2 deletions shadercompiler/EffectCompilerDX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ static PatchAction PatchShader( InputStageType shaderStage, ASTNode* callNode, P
// return type
os << functionHeader->GetType();
// name
InlineString name = state.AllocateName();
InlineString name = state.AllocateGlobalName();
os << ' ' << name << "( ";
bool first = true;

Expand Down Expand Up @@ -376,7 +376,7 @@ static PatchAction PatchShader( InputStageType shaderStage, ASTNode* callNode, P
{
if( callNode->GetChildrenCount() <= uniformIndex && functionHeader->GetChild( i )->GetChildOrNull( 1 ) != nullptr )
{
InlineString uniformName = state.AllocateName();
InlineString uniformName = state.AllocateGlobalName();
Type type = functionHeader->GetChild( i )->GetType();
type.storageClass = 0;
os << type << ' ' << uniformName << " = " << HLSL{ functionHeader->GetChild( i )->GetChildOrNull( 1 ), nullptr };
Expand Down
46 changes: 23 additions & 23 deletions shadercompiler/EffectCompilerMetal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,9 @@ void CollectGlobals(
if( !globalsStruct )
{
InlineString name = MakeInlineString( "GlobalsData" );
globalsStruct = new ASTNode( NT_STRUCT, node->GetLocation(), node->GetScope(), nullptr );
// The GlobalsData struct has no proper location, so give it a dummy one.
FileLocation location{ 1, name };
globalsStruct = new ASTNode( NT_STRUCT, location, node->GetScope(), nullptr );

RegisterSpecifier reg;
reg.shaderProfile.start = nullptr;
Expand Down Expand Up @@ -947,7 +949,7 @@ void CollectGlobals(
}
}

PatchGlobalsInFunctions( functions, cbuffer->GetSymbol(), state.AllocateNameWithPrefix( "cbuffer" ), members );
PatchGlobalsInFunctions( functions, cbuffer->GetSymbol(), state.AllocateGlobalName( "cbuffer" ), members );
}
}

Expand Down Expand Up @@ -2301,7 +2303,7 @@ bool AutoAssignRegisters( ParserState& state, ASTNode* callNode )

ASTNode* NewStructMember( ParserState& state, Symbol* sourceSymbol )
{
auto symbol = state.GetSymbolTable().AddSymbol( state.AllocateNameWithPrefix( sourceSymbol->name ) );
auto symbol = state.GetSymbolTable().AddSymbol( state.AllocateLocalName( sourceSymbol->name ) );
auto nameDecl = state.NewNode( NT_NAME_DECLARATION );
symbol->definition = nameDecl;
symbol->type = sourceSymbol->type;
Expand Down Expand Up @@ -2362,7 +2364,7 @@ void GatherSystemFields( ASTNode* destStruct, std::vector<ASTNode*>& accessors,
auto it = s_systemSemantics.find( name->GetSymbol()->semantic );
if( it != s_systemSemantics.end() )
{
auto param = NewFunctionParameter( state, name->GetType() );
auto param = NewFunctionParameter( state, name->GetType(), state.AllocateLocalName() );
param->GetSymbol()->registerSpecifier[{}] = MetalSystemSemantics( it->second );
destStruct->AddChild( param );
argumentAccessors.push_back( NewVarIdentifier( state, param->GetSymbol() ) );
Expand All @@ -2371,7 +2373,7 @@ void GatherSystemFields( ASTNode* destStruct, std::vector<ASTNode*>& accessors,
// subtract base_vertex from vertex_id to match dx12 behaviour for SV_VertexID
if( name->GetSymbol()->semantic == "SV_VertexID" )
{
auto base_param = NewFunctionParameter( state, hlsl::uint_t );
auto base_param = NewFunctionParameter( state, hlsl::uint_t, state.AllocateLocalName() );
base_param->GetSymbol()->registerSpecifier[{}] = MetalSystemSemantics( MetalSystemSemanticsType::Enum::base_vertex );
destStruct->AddChild( base_param );

Expand All @@ -2387,7 +2389,7 @@ void GatherSystemFields( ASTNode* destStruct, std::vector<ASTNode*>& accessors,
// subtract base_instance from instance_id to match dx12 behaviour for SV_InstanceID
if( name->GetSymbol()->semantic == "SV_InstanceID" )
{
auto base_param = NewFunctionParameter( state, hlsl::uint_t );
auto base_param = NewFunctionParameter( state, hlsl::uint_t, state.AllocateLocalName() );
base_param->GetSymbol()->registerSpecifier[{}] = MetalSystemSemantics( MetalSystemSemanticsType::Enum::base_instance );
destStruct->AddChild( base_param );

Expand Down Expand Up @@ -2494,7 +2496,7 @@ void GatherSystemInputs( ASTNode* destStruct, std::vector<ASTNode*>& accessors,
// subtract base_vertex from vertex_id to match dx12 behaviour for SV_VertexID
if( symbol->semantic == "SV_VertexID" )
{
auto param = NewFunctionParameter( state, hlsl::uint_t );
auto param = NewFunctionParameter( state, hlsl::uint_t, state.AllocateLocalName() );
param->GetSymbol()->registerSpecifier[{}] = MetalSystemSemantics( MetalSystemSemanticsType::Enum::base_vertex );
destStruct->AddChild( param );

Expand All @@ -2510,7 +2512,7 @@ void GatherSystemInputs( ASTNode* destStruct, std::vector<ASTNode*>& accessors,
// subtract base_instance from instance_id to match dx12 behaviour for SV_InstanceID
if( symbol->semantic == "SV_InstanceID" )
{
auto param = NewFunctionParameter( state, hlsl::uint_t );
auto param = NewFunctionParameter( state, hlsl::uint_t, state.AllocateLocalName() );
param->GetSymbol()->registerSpecifier[{}] = MetalSystemSemantics( MetalSystemSemanticsType::Enum::base_instance );
destStruct->AddChild( param );

Expand Down Expand Up @@ -2798,6 +2800,10 @@ ASTNode* PatchShader( PatchShaderType shaderType, ASTNode* callNode, ParserState
{
ZoneScoped;

// Reset the per-stage local name allocator so that the name allocation is consistent for later
// stages (e.g. pixel shader), even if the previous stages are different (e.g. vertex).
state.ResetLocalNames();

Symbol* entryPointSymbol = callNode->GetSymbol();
if( !entryPointSymbol || !entryPointSymbol->definition )
{
Expand All @@ -2815,8 +2821,10 @@ ASTNode* PatchShader( PatchShaderType shaderType, ASTNode* callNode, ParserState
state.GetCurrentLocation().fileName = shaderName.stringValue;
state.GetCurrentLocation().lineNumber = 1;

ASTNode* inputsStruct = NewStruct( state, state.AllocateNameWithPrefix( "StageInData" ) );
ASTNode* outputsStruct = NewStruct( state, state.AllocateNameWithPrefix( "StageOutData" ) );
// Every patch numbers its names from zero, so other stages' patches have structs with the same
// names. Each stage's code only contains its own.
ASTNode* inputsStruct = NewStruct( state, state.AllocateLocalName( "StageInData" ), ALLOW_OVERRIDES );
ASTNode* outputsStruct = NewStruct( state, state.AllocateLocalName( "StageOutData" ), ALLOW_OVERRIDES );

auto header = state.NewNode( NT_FUNCTION_HEADER, shaderName );
auto shaderSymbol = state.GetSymbolTable().AddSymbol( shaderName.stringValue, ALLOW_OVERRIDES );
Expand Down Expand Up @@ -2895,7 +2903,7 @@ ASTNode* PatchShader( PatchShaderType shaderType, ASTNode* callNode, ParserState
{
// Local variable for return value

auto declList = NewVarDeclaration( state, functionHeader->GetType(), state.AllocateNameWithPrefix( "result" ) );
auto declList = NewVarDeclaration( state, functionHeader->GetType(), state.AllocateLocalName( "result" ) );
auto nameSymbol = declList->GetChild( 0 )->GetSymbol();
shaderBody->AddChild( declList );

Expand Down Expand Up @@ -2923,7 +2931,7 @@ ASTNode* PatchShader( PatchShaderType shaderType, ASTNode* callNode, ParserState
state.GetTree()->AddChild( inputsStruct );

// Add [[stage_in]] parameter.
auto stageIn = NewFunctionParameter( state, TypeFromSymbol( inputsStruct->GetSymbol() ), state.AllocateNameWithPrefix( "stageIn" ) );
auto stageIn = NewFunctionParameter( state, TypeFromSymbol( inputsStruct->GetSymbol() ), state.AllocateLocalName( "stageIn" ) );
header->AddChild( stageIn );

stageInSymbol = stageIn->GetSymbol();
Expand Down Expand Up @@ -3051,7 +3059,7 @@ ASTNode* PatchShader( PatchShaderType shaderType, ASTNode* callNode, ParserState
header->SetType( TypeFromSymbol( outputsStruct->GetSymbol() ) );

// Create a local variable for outputs
auto resultDeclList = NewVarDeclaration( state, TypeFromSymbol( outputsStruct->GetSymbol() ), state.AllocateNameWithPrefix( "stageOut" ) );
auto resultDeclList = NewVarDeclaration( state, TypeFromSymbol( outputsStruct->GetSymbol() ), state.AllocateLocalName( "stageOut" ) );
auto resultSymbol = resultDeclList->GetChild( 0 )->GetSymbol();
shaderBody->AddChild( resultDeclList );

Expand Down Expand Up @@ -5110,16 +5118,6 @@ bool EffectCompilerMetal::CompileEffect( const char* source, size_t sourceLength

RemapSystemSemanticsDXtoMetal( shaderNode->GetChild( 1 ) );

std::string shaderCacheKey;
{
state.GetSymbolTable().ResetUsedFlag();
MarkUsedSymbols( shaderNode->GetChild( 1 ), state );

CompilerInputStream os( state, ShadingLanguage::MSL );
os << MSL{ state.GetTree(), &state.GetSymbolTable() };
shaderCacheKey = os.str();
state.GetSymbolTable().ResetUsedFlag();
}
PatchShaderType patchShaderType;
switch( stage.type )
{
Expand Down Expand Up @@ -5183,6 +5181,8 @@ bool EffectCompilerMetal::CompileEffect( const char* source, size_t sourceLength
os << AtomicFn{ "xor", "Xor" };
os << MSL{ state.GetTree(), &state.GetSymbolTable() };

// Key on the exact source code that we intend to feed to the Metal compiler.
std::string shaderCacheKey = os.str();

// Let only one thread compile this permutation.
bool needsToCompile = false;
Expand Down
8 changes: 4 additions & 4 deletions shadercompiler/HLSLParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ struct_specifier(A) ::= OP_STRUCT(B) enter_block OP_LEFT_BRACE OP_RIGHT_BRACE.

parserState->GetSymbolTable().LeaveScope();

Symbol* symbol = parserState->GetSymbolTable().AddSymbol( parserState->AllocateName() );
Symbol* symbol = parserState->GetSymbolTable().AddSymbol( parserState->AllocateGlobalName() );
symbol->definition = A;
symbol->isTypeName = true;

Expand Down Expand Up @@ -2242,7 +2242,7 @@ struct_specifier(A) ::= OP_STRUCT enter_block OP_LEFT_BRACE struct_declaration_l

A = C;

Symbol* symbol = parserState->GetSymbolTable().AddSymbol( parserState->AllocateName() );
Symbol* symbol = parserState->GetSymbolTable().AddSymbol( parserState->AllocateGlobalName() );
symbol->definition = C;
symbol->isTypeName = true;

Expand Down Expand Up @@ -3069,7 +3069,7 @@ cbuffer_declaration(A) ::= buffer_type(T) register_specifier(R) OP_LEFT_BRACE cb
{
A = B;
A->SetToken( &T );
Symbol* symbol = parserState->GetSymbolTable().AddBufferSymbol( parserState->AllocateName() );
Symbol* symbol = parserState->GetSymbolTable().AddBufferSymbol( parserState->AllocateGlobalName() );
symbol->registerSpecifier[R.shaderProfile] = R;
A->SetSymbol( symbol );
A->SetLocation( T.fileLocation );
Expand All @@ -3079,7 +3079,7 @@ cbuffer_declaration(A) ::= buffer_type(T) OP_LEFT_BRACE cbuffer_members(B) OP_R
{
A = B;
A->SetToken( &T );
A->SetSymbol( parserState->GetSymbolTable().AddBufferSymbol( parserState->AllocateName() ) );
A->SetSymbol( parserState->GetSymbolTable().AddBufferSymbol( parserState->AllocateGlobalName() ) );
A->SetLocation( T.fileLocation );
}

Expand Down
19 changes: 18 additions & 1 deletion shadercompiler/ModifiedTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ bool CheckHash( const HashCheckArguments& query )
return true;
}

// Hashes file contents without CR characters, so the hash doesn't depend on how the sources
// were synced: CRLF on Windows clients, LF on macOS/Linux or from a Git checkout.
void AddWithoutCarriageReturns( MD5& md5, const char* data, size_t size )
{
const char* start = data;
const char* end = data + size;
for( const char* p = data; p < end; ++p )
{
if( *p == '\r' )
{
md5.add( start, p - start );
start = p + 1;
}
}
md5.add( start, end - start );
}

void GetSourceHash( const char* sourcePath, const char* parentData, const char* rootPath, CachingIncludeHandler::IncludeType includeType, std::set<std::string>& visited, MD5& md5 )
{
if( visited.find( sourcePath ) != end( visited ) )
Expand All @@ -121,7 +138,7 @@ void GetSourceHash( const char* sourcePath, const char* parentData, const char*
visited.insert( sourcePath );
if( auto opened = s_includeHandler.Open( sourcePath, includeType, parentData, rootPath ) )
{
md5.add( opened->data, opened->size );
AddWithoutCarriageReturns( md5, opened->data, opened->size );

std::cmatch match;
auto begin = opened->data;
Expand Down
88 changes: 60 additions & 28 deletions shadercompiler/OutputHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,6 @@ CodeStream& operator<<( CodeStream& os, const Children<XSL>& children )
return os;
}

bool HasUsedDeclarations( ASTNode* node )
{
for( unsigned i = 0; i < node->GetChildrenCount(); ++i )
{
if( node->GetChild( i ) )
{
if( node->GetChild( i )->GetNodeType() == NT_VAR_DECLARATION_LIST )
{
if( HasUsedDeclarations( node->GetChild( i ) ) )
{
return true;
}
}
else if( node->GetChild( i )->GetNodeType() == NT_NAME_DECLARATION )
{
if( node->GetChild( i )->GetSymbol() && node->GetChild( i )->GetSymbol()->used )
{
return true;
}
}
}
}
return false;
}

std::map<int, std::string> s_operators = {
{ OP_MUL_ASSIGN, "*=" },
{ OP_DIV_ASSIGN, "/=" },
Expand Down Expand Up @@ -1146,7 +1121,7 @@ CodeStream& operator<<( CodeStream& os, const HLSL& hlsl )
os << ";" << Endl();
break;
case NT_CBUFFER: {
if( !HasUsedDeclarations( node ) )
if( !HasUsedMembers( node ) )
{
break;
}
Expand Down Expand Up @@ -1303,11 +1278,56 @@ static const char* MSLCompareFunc( uint8_t func )
}
}

// Returns true for declarations (top-level or struct members) that the MSL writer needs to
// include in the generated MSL code. For declarations that return false for this function,
// nothing must be emitted at all to ensure consistent, cacheable MSL source code is generated.
static bool ShouldWriteToMSL( ASTNode* node )
{
if( !node )
{
return true;
}
switch( node->GetNodeType() )
{
case NT_TECHNIQUE:
return false;
case NT_FUNCTION_DEFINITION:
return node->GetChild( 0 )->GetSymbol()->used;
case NT_FUNCTION_HEADER:
case NT_STRUCT:
case NT_NAME_DECLARATION:
return !node->GetSymbol() || node->GetSymbol()->used;
case NT_FUNCTION_PARAMETER:
// System value fields that stage patching adds to the stage input and output structs.
return true;
case NT_VAR_DECLARATION_LIST:
case NT_STRUCT_MEMBER:
for( unsigned i = 0; i < node->GetChildrenCount(); ++i )
{
if( ShouldWriteToMSL( node->GetChild( i ) ) )
{
return true;
}
}
return false;
default:
// Writing a declaration that isn't needed would make otherwise identical permutations
// differ, breaking deduplication without any error. Every declaration type must be
// handled explicitly above, so fail loudly instead.
assert( false && "ShouldWriteToMSL: unhandled node type" );
return false;
}
}

CodeStream& operator<<( CodeStream& os, const MSL& msl )
{
auto node = msl.node;

os << node->GetLocation();
// Children write their own locations, so the program's location (that of its first declaration) is never needed.
if( node->GetNodeType() != NT_PROGRAM )
{
os << node->GetLocation();
}

auto Child = [&]( size_t index ) -> MSL { return XSLChild( msl, index ); };

Expand Down Expand Up @@ -1880,7 +1900,15 @@ CodeStream& operator<<( CodeStream& os, const MSL& msl )
{
os << "struct " << " " << node->GetSymbol()->name << Endl();
os << "{" << Endl();
os << Indent() << Children<MSL>( msl, "" ) << Unindent() << Endl();
os << Indent();
for( unsigned i = 0; i < node->GetChildrenCount(); ++i )
{
if( ShouldWriteToMSL( node->GetChild( i ) ) )
{
os << Child( i );
}
}
os << Unindent() << Endl();
os << "};" << Endl();
}
break;
Expand All @@ -1901,6 +1929,10 @@ CodeStream& operator<<( CodeStream& os, const MSL& msl )
case NT_PROGRAM:
for( unsigned i = 0; i < node->GetChildrenCount(); ++i )
{
if( !ShouldWriteToMSL( node->GetChild( i ) ) )
{
continue;
}
os << Child( i );
if( node->GetChild( i ) && node->GetChild( i )->GetNodeType() == NT_FUNCTION_HEADER )
{
Expand Down
Loading