Skip to content

Commit

Permalink
Moved texture/image access calls result to an SSA alias.
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonJoker committed Apr 3, 2022
1 parent 598ae16 commit be5555a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
38 changes: 15 additions & 23 deletions source/CompilerSpirV/SpirvExprVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,31 +684,23 @@ namespace spirv
shuffle.emplace_back( loadedLhsId );
shuffle.emplace_back( rhsId );
ast::expr::SwizzleKind rhsSwizzleKind;
auto rhsCount = getComponentCount( expr->getRHS()->getType()->getKind() );
assert( rhsCount <= 4u );

if ( expr->getRHS()->getKind() == ast::expr::Kind::eSwizzle
&& static_cast< ast::expr::Swizzle const & >( *expr->getRHS() ).getOuterExpr()->getKind() != ast::expr::Kind::eIntrinsicCall
&& static_cast< ast::expr::Swizzle const & >( *expr->getRHS() ).getOuterExpr()->getKind() != ast::expr::Kind::eCombinedImageAccessCall
&& static_cast< ast::expr::Swizzle const & >( *expr->getRHS() ).getOuterExpr()->getKind() != ast::expr::Kind::eImageAccessCall )
switch ( rhsCount )
{
rhsSwizzleKind = static_cast< ast::expr::Swizzle const & >( *expr->getRHS() ).getSwizzle();
}
else
{
auto rhsCount = getComponentCount( expr->getRHS()->getType()->getKind() );
assert( rhsCount >= 2u && rhsCount <= 4u );

switch ( rhsCount )
{
case 2u:
rhsSwizzleKind = ast::expr::SwizzleKind::e01;
break;
case 3u:
rhsSwizzleKind = ast::expr::SwizzleKind::e012;
break;
default:
rhsSwizzleKind = ast::expr::SwizzleKind::e0123;
break;
}
case 1u:
rhsSwizzleKind = ast::expr::SwizzleKind::e0;
break;
case 2u:
rhsSwizzleKind = ast::expr::SwizzleKind::e01;
break;
case 3u:
rhsSwizzleKind = ast::expr::SwizzleKind::e012;
break;
default:
rhsSwizzleKind = ast::expr::SwizzleKind::e0123;
break;
}

auto swizzleComponents = convert( getSwizzleComponents( lhsSwizzleKind
Expand Down
17 changes: 15 additions & 2 deletions source/ShaderAST/Visitors/TransformSSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,14 @@ namespace ast
, ast::expr::SwizzleKind::e012 );
}
}

if ( srcType->getKind() != ast::type::Kind::eVoid )
{
auto type = m_result->getType();
auto alias = doCreateAliasVar( type
, std::move( m_result ) );
m_result = ast::expr::makeIdentifier( m_cache, alias );
}
}

void visitInitExpr( expr::Init * expr )override
Expand Down Expand Up @@ -1322,8 +1330,8 @@ namespace ast
}

m_result = ast::expr::makeCombinedImageAccessCall( returnType
, kind
, std::move( args ) );
, kind
, std::move( args ) );

if ( returnComponentsCount != InvalidComponentCount && returnComponentsCount != count )
{
Expand All @@ -1347,6 +1355,11 @@ namespace ast

m_result = ast::expr::makeSwizzle( std::move( m_result ), swizzleKind );
}

auto type = m_result->getType();
auto alias = doCreateAliasVar( type
, std::move( m_result ) );
m_result = ast::expr::makeIdentifier( m_cache, alias );
}

private:
Expand Down
28 changes: 28 additions & 0 deletions test/ShaderWriter/TestWriterShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,33 @@ namespace
, CurrentCompilers );
testEnd();
}

void arraySsboTextureLookup( test::sdw_test::TestCounts & testCounts )
{
testBegin( "arraySsboTextureLookup" );
using namespace sdw;
{
FragmentWriter writer;
auto fragUvw = writer.declInput< Vec3 >( "fragUvw", 0u );
auto outColor = writer.declOutput< Vec4 >( "outColor", 0u );

sdw::Ssbo colorsSsbo = writer.declStorageBuffer( "colorsBuffer", 0u, 1u );
auto colors = colorsSsbo.declMemberArray< Vec4 >( "colors" );
colorsSsbo.end();

auto volumeTexture = writer.declCombinedImg< UCombinedImage3DR32 >( "volumeTexture", 1u, 1u );

writer.implementMain(
[&]( sdw::FragmentIn in, sdw::FragmentOut out )
{
outColor = colors[volumeTexture.sample( fragUvw )];
} );
test::writeShader( writer
, testCounts
, CurrentCompilers );
}
testEnd();
}
}

sdwTestSuiteMain( TestWriterShader )
Expand All @@ -1652,6 +1679,7 @@ sdwTestSuiteMain( TestWriterShader )
basicPipeline( testCounts );
voxelPipeline( testCounts );
tessellationPipeline( testCounts );
arraySsboTextureLookup( testCounts );
sdwTestSuiteEnd();
}

Expand Down

0 comments on commit be5555a

Please sign in to comment.