Skip to content
Draft
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
8 changes: 8 additions & 0 deletions trinity/Tr2IndirectDrawBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void Tr2IndirectDrawBufferWriter::SetPerObjectData( Tr2RenderContextEnum::Shader
auto addr = m_renderContext.UploadConstants( buffer );
m_buffer->perObject[stage] = addr;
#endif
m_writtenPerObjectData |= 1u << stage;
}

void Tr2IndirectDrawBufferWriter::SetPerObjectData( Tr2RenderContextEnum::ShaderType stage, const void* data, size_t size )
Expand All @@ -111,6 +112,7 @@ void Tr2IndirectDrawBufferWriter::SetPerObjectData( Tr2RenderContextEnum::Shader
auto addr = m_renderContext.UploadConstants( data, size );
m_buffer->perObject[stage] = addr;
#endif
m_writtenPerObjectData |= 1u << stage;
}

void Tr2IndirectDrawBufferWriter::DrawIndexed( uint32_t indexCountPerInstance, uint32_t instanceCount, uint32_t startIndexLocation, uint32_t baseVertexLocation, uint32_t startInstanceLocation )
Expand All @@ -136,6 +138,7 @@ void Tr2IndirectDrawBufferWriter::Next()
m_buffer++;
++m_commandCount;
#endif
m_writtenPerObjectData = 0;
}

bool Tr2IndirectDrawBufferWriter::HasMaterialConstants( Tr2RenderContextEnum::ShaderType stage ) const
Expand All @@ -160,6 +163,11 @@ bool Tr2IndirectDrawBufferWriter::HasPerObjectData( Tr2RenderContextEnum::Shader
#endif
}

bool Tr2IndirectDrawBufferWriter::IsPerObjectDataWritten( Tr2RenderContextEnum::ShaderType stage ) const
{
return ( m_writtenPerObjectData & ( 1u << stage ) ) != 0;
}


Tr2IndirectDrawBuffer::~Tr2IndirectDrawBuffer()
{
Expand Down
2 changes: 2 additions & 0 deletions trinity/Tr2IndirectDrawBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ class Tr2IndirectDrawBufferWriter

bool HasMaterialConstants( Tr2RenderContextEnum::ShaderType stage ) const;
bool HasPerObjectData( Tr2RenderContextEnum::ShaderType stage ) const;
bool IsPerObjectDataWritten( Tr2RenderContextEnum::ShaderType stage ) const;

private:
const Tr2IndirectDrawBufferLayout& m_layout;
Tr2RenderContextAL& m_renderContext;
uint32_t m_commandCount;
uint32_t m_writtenPerObjectData = 0;
#if TRINITY_PLATFORM == TRINITY_DIRECTX12
Tr2IndirectDrawBuffer::Allocation m_allocation;
uint8_t* m_buffer;
Expand Down
15 changes: 10 additions & 5 deletions trinity/Tr2RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,17 +694,22 @@ void Tr2RenderContextBase::RenderGdprBatches( ITriRenderBatchAccumulator* batche
{
batch.m_objectData->ApplyConstantBuffers( bin.writer, *primaryContext );
}
else if( bin.writer.HasPerObjectData( Tr2RenderContextEnum::VERTEX_SHADER ) || bin.writer.HasPerObjectData( Tr2RenderContextEnum::PIXEL_SHADER ) )
for( auto stage : { Tr2RenderContextEnum::VERTEX_SHADER, Tr2RenderContextEnum::PIXEL_SHADER } )
{
// Unwritten per-object root CBV addresses in the indirect args hang the GPU
if( !bin.writer.HasPerObjectData( stage ) || bin.writer.IsPerObjectDataWritten( stage ) )
{
continue;
}
// Indirect args are reused between draws without being cleared, so an unwritten per-object
// slot holds a stale value: a root CBV address that hangs the GPU on DX12, or a constant
// buffer page index that crashes on Metal.
static const uint8_t s_zeroPerObjectData[4096] = {};
bin.writer.SetPerObjectData( Tr2RenderContextEnum::VERTEX_SHADER, s_zeroPerObjectData, sizeof( s_zeroPerObjectData ) );
bin.writer.SetPerObjectData( Tr2RenderContextEnum::PIXEL_SHADER, s_zeroPerObjectData, sizeof( s_zeroPerObjectData ) );
bin.writer.SetPerObjectData( stage, s_zeroPerObjectData, sizeof( s_zeroPerObjectData ) );
static std::atomic<uint32_t> s_missingObjectDataLogs = 0;
if( s_missingObjectDataLogs.fetch_add( 1 ) < 50 )
{
auto* effect = dynamic_cast<Tr2Effect*>( batch.m_material );
CCP_LOGERR( "GDPR: effect '%s' declares perObjectData but the c++ batch provides no perObjectData; drawing with zeroed perObjectData", effect ? effect->GetEffectPathName() : "<unknown>" );
CCP_LOGERR( "GDPR: effect '%s' declares perObjectData but the c++ batch provides no perObjectData for shader stage %u; drawing with zeroed perObjectData", effect ? effect->GetEffectPathName() : "<unknown>", uint32_t( stage ) );
}
}
bin.writer.DrawIndexed( batch.m_indexCountPerInstance, batch.m_instanceCount, batch.m_startIndexLocation, batch.m_baseVertexLocation, batch.m_startInstanceLocation );
Expand Down
5 changes: 5 additions & 0 deletions trinityal/metal/MetalUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@

id<MTLBuffer> ConstantBufferAllocator::GetPage( uint32_t page )
{
CCP_ASSERT( page < MAX_PAGE_COUNT );
if( page >= MAX_PAGE_COUNT )
{
return nil;
}
return m_pages[page];
}

Expand Down