From aa0bf47ec3ea03c5e7c78263e1956af5ab018131 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 2 Mar 2020 02:00:35 +0100 Subject: [PATCH] Extract texture compression functionality. --- capture/build/win32/capture.vcxproj | 2 + capture/build/win32/capture.vcxproj.filters | 6 ++ .../build/win32/import-chrome.vcxproj | 2 + .../build/win32/import-chrome.vcxproj.filters | 6 ++ profiler/build/win32/Tracy.vcxproj | 2 + profiler/build/win32/Tracy.vcxproj.filters | 6 ++ server/TracyTextureCompression.cpp | 65 +++++++++++++++++++ server/TracyTextureCompression.hpp | 48 ++++++++++++++ server/TracyWorker.cpp | 60 +---------------- server/TracyWorker.hpp | 13 +--- update/build/win32/update.vcxproj | 2 + update/build/win32/update.vcxproj.filters | 6 ++ 12 files changed, 151 insertions(+), 67 deletions(-) create mode 100644 server/TracyTextureCompression.cpp create mode 100644 server/TracyTextureCompression.hpp diff --git a/capture/build/win32/capture.vcxproj b/capture/build/win32/capture.vcxproj index 00027ca5..097762a1 100644 --- a/capture/build/win32/capture.vcxproj +++ b/capture/build/win32/capture.vcxproj @@ -137,6 +137,7 @@ + @@ -190,6 +191,7 @@ + diff --git a/capture/build/win32/capture.vcxproj.filters b/capture/build/win32/capture.vcxproj.filters index c19f9c1a..c5ce4b4f 100644 --- a/capture/build/win32/capture.vcxproj.filters +++ b/capture/build/win32/capture.vcxproj.filters @@ -126,6 +126,9 @@ server + + server + @@ -293,5 +296,8 @@ server + + server + \ No newline at end of file diff --git a/import-chrome/build/win32/import-chrome.vcxproj b/import-chrome/build/win32/import-chrome.vcxproj index 709ba6ad..3f266a45 100644 --- a/import-chrome/build/win32/import-chrome.vcxproj +++ b/import-chrome/build/win32/import-chrome.vcxproj @@ -136,6 +136,7 @@ + @@ -187,6 +188,7 @@ + diff --git a/import-chrome/build/win32/import-chrome.vcxproj.filters b/import-chrome/build/win32/import-chrome.vcxproj.filters index 34cc47e0..d50b384e 100644 --- a/import-chrome/build/win32/import-chrome.vcxproj.filters +++ b/import-chrome/build/win32/import-chrome.vcxproj.filters @@ -120,6 +120,9 @@ server + + server + @@ -281,5 +284,8 @@ server + + server + \ No newline at end of file diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index 22819b14..6ac65ca7 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -119,6 +119,7 @@ + @@ -204,6 +205,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index d52a36b4..a58b9fe3 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -192,6 +192,9 @@ server + + server + @@ -479,6 +482,9 @@ server + + server + diff --git a/server/TracyTextureCompression.cpp b/server/TracyTextureCompression.cpp new file mode 100644 index 00000000..ebff4c70 --- /dev/null +++ b/server/TracyTextureCompression.cpp @@ -0,0 +1,65 @@ +#include "../zstd/zstd.h" + +#include "TracyEvent.hpp" +#include "TracyTextureCompression.hpp" + +namespace tracy +{ + +TextureCompression::TextureCompression() + : m_buf( nullptr ) + , m_bufSize( 0 ) + , m_cctx( ZSTD_createCCtx() ) + , m_dctx( ZSTD_createDCtx() ) +{ +} + +TextureCompression::~TextureCompression() +{ + delete[] m_buf; + ZSTD_freeCCtx( m_cctx ); + ZSTD_freeDCtx( m_dctx ); +} + +void TextureCompression::Pack( struct ZSTD_CCtx_s* ctx, char*& buf, size_t& bufsz, const char* image, uint32_t inBytes, uint32_t& csz ) const +{ + const auto maxout = ZSTD_COMPRESSBOUND( inBytes ); + if( bufsz < maxout ) + { + bufsz = maxout; + delete[] buf; + buf = new char[maxout]; + } + assert( ctx ); + const auto outsz = ZSTD_compressCCtx( ctx, buf, maxout, image, inBytes, 3 ); + csz = uint32_t( outsz ); +} + +uint32_t TextureCompression::PackImpl( const char* image, uint32_t inBytes ) +{ + const auto maxout = ZSTD_COMPRESSBOUND( inBytes ); + if( m_bufSize < maxout ) + { + m_bufSize = maxout; + delete[] m_buf; + m_buf = new char[maxout]; + } + assert( m_cctx ); + return (uint32_t)ZSTD_compressCCtx( m_cctx, m_buf, maxout, image, inBytes, 1 ); +} + +const char* TextureCompression::Unpack( const FrameImage& image ) +{ + const auto outsz = size_t( image.w ) * size_t( image.h ) / 2; + if( m_bufSize < outsz ) + { + m_bufSize = outsz; + delete[] m_buf; + m_buf = new char[outsz]; + } + assert( m_dctx ); + ZSTD_decompressDCtx( m_dctx, m_buf, outsz, image.ptr, image.csz ); + return m_buf; +} + +} diff --git a/server/TracyTextureCompression.hpp b/server/TracyTextureCompression.hpp new file mode 100644 index 00000000..2dacfed4 --- /dev/null +++ b/server/TracyTextureCompression.hpp @@ -0,0 +1,48 @@ +#ifndef __TRACY__TEXTURECOMPRESSION_HPP__ +#define __TRACY__TEXTURECOMPRESSION_HPP__ + +#include +#include + +#include "TracySlab.hpp" + +struct ZSTD_CCtx_s; +struct ZSTD_DCtx_s; + +namespace tracy +{ + +struct FrameImage; + +class TextureCompression +{ +public: + TextureCompression(); + ~TextureCompression(); + + void Pack( struct ZSTD_CCtx_s* ctx, char*& buf, size_t& bufsz, const char* image, uint32_t inBytes, uint32_t& csz ) const; + + template + const char* Pack( const char* image, uint32_t inBytes, uint32_t& csz, Slab& slab ) + { + const auto outsz = PackImpl( image, inBytes ); + auto ptr = (char*)slab.AllocBig( outsz ); + memcpy( ptr, m_buf, outsz ); + csz = outsz; + return ptr; + } + + const char* Unpack( const FrameImage& image ); + +private: + uint32_t PackImpl( const char* image, uint32_t inBytes ); + + char* m_buf; + size_t m_bufSize; + struct ZSTD_CCtx_s* m_cctx; + struct ZSTD_DCtx_s* m_dctx; +}; + +} + +#endif diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index a0576f6f..2483d0ac 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -13,8 +13,6 @@ #include #include -#include "../zstd/zstd.h" - #include "../common/TracyProtocol.hpp" #include "../common/TracySystem.hpp" #include "TracyFileRead.hpp" @@ -233,8 +231,6 @@ Worker::Worker( const char* addr, int port ) , m_callstackFrameStaging( nullptr ) , m_traceVersion( CurrentVersion ) , m_loadTime( 0 ) - , m_fiCctx( ZSTD_createCCtx() ) - , m_fiDctx( ZSTD_createDCtx() ) { m_data.sourceLocationExpand.push_back( 0 ); m_data.localThreadCompress.InitZero(); @@ -381,7 +377,6 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks ) : m_hasData( true ) , m_stream( nullptr ) , m_buffer( nullptr ) - , m_fiDctx( ZSTD_createDCtx() ) { auto loadStart = std::chrono::high_resolution_clock::now(); @@ -1478,7 +1473,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks ) data[idx].state.store( JobData::InProgress, std::memory_order_release ); td->Queue( [this, &data, idx, fi] { - PackFrameImage( data[idx].ctx, data[idx].outbuf, data[idx].outsz, data[idx].buf, fi->w * fi->h / 2, fi->csz ); + m_texcomp.Pack( data[idx].ctx, data[idx].outbuf, data[idx].outsz, data[idx].buf, fi->w * fi->h / 2, fi->csz ); data[idx].state.store( JobData::DataReady, std::memory_order_release ); } ); @@ -1769,7 +1764,6 @@ Worker::~Worker() LZ4_freeStreamDecode( (LZ4_streamDecode_t*)m_stream ); delete[] m_frameImageBuffer; - delete[] m_frameImageCompressedBuffer; for( auto& v : m_data.threads ) { @@ -1801,9 +1795,6 @@ Worker::~Worker() { v.second->~LockMap(); } - - if( m_fiCctx ) ZSTD_freeCCtx( m_fiCctx ); - if( m_fiDctx ) ZSTD_freeDCtx( m_fiDctx ); } uint64_t Worker::GetLockCount() const @@ -3150,7 +3141,7 @@ void Worker::AddFrameImageData( uint64_t ptr, const char* data, size_t sz ) dst += 8; } uint32_t csz; - auto image = PackFrameImage( m_frameImageBuffer, sz, csz ); + auto image = m_texcomp.Pack( m_frameImageBuffer, sz, csz, m_slab ); m_pendingFrameImageData.emplace( ptr, FrameImagePending { image, csz } ); } @@ -6203,7 +6194,7 @@ void Worker::Write( FileWrite& f ) f.Write( &fi->w, sizeof( fi->w ) ); f.Write( &fi->h, sizeof( fi->h ) ); f.Write( &fi->flip, sizeof( fi->flip ) ); - const auto image = UnpackFrameImage( *fi ); + const auto image = m_texcomp.Unpack( *fi ); f.Write( image, fi->w * fi->h / 2 ); } @@ -6381,51 +6372,6 @@ const char* Worker::GetFailureString( Worker::Failure failure ) return s_failureReasons[(int)failure]; } -void Worker::PackFrameImage( struct ZSTD_CCtx_s* ctx, char*& buf, size_t& bufsz, const char* image, uint32_t inBytes, uint32_t& csz ) const -{ - const auto maxout = ZSTD_COMPRESSBOUND( inBytes ); - if( bufsz < maxout ) - { - bufsz = maxout; - delete[] buf; - buf = new char[maxout]; - } - assert( ctx ); - const auto outsz = ZSTD_compressCCtx( ctx, buf, maxout, image, inBytes, 3 ); - csz = uint32_t( outsz ); -} - -const char* Worker::PackFrameImage( const char* image, uint32_t inBytes, uint32_t& csz ) -{ - const auto maxout = ZSTD_COMPRESSBOUND( inBytes ); - if( m_frameImageCompressedBufferSize < maxout ) - { - m_frameImageCompressedBufferSize = maxout; - delete[] m_frameImageCompressedBuffer; - m_frameImageCompressedBuffer = new char[maxout]; - } - assert( m_fiCctx ); - const auto outsz = ZSTD_compressCCtx( m_fiCctx, m_frameImageCompressedBuffer, maxout, image, inBytes, 1 ); - csz = uint32_t( outsz ); - auto ptr = (char*)m_slab.AllocBig( outsz ); - memcpy( ptr, m_frameImageCompressedBuffer, outsz ); - return ptr; -} - -const char* Worker::UnpackFrameImage( const FrameImage& image ) -{ - const auto outsz = size_t( image.w ) * size_t( image.h ) / 2; - if( m_frameImageCompressedBufferSize < outsz ) - { - m_frameImageCompressedBufferSize = outsz; - delete[] m_frameImageCompressedBuffer; - m_frameImageCompressedBuffer = new char[outsz]; - } - assert( m_fiDctx ); - ZSTD_decompressDCtx( m_fiDctx, m_frameImageCompressedBuffer, outsz, image.ptr, image.csz ); - return m_frameImageCompressedBuffer; -} - void Worker::SetParameter( size_t paramIdx, int32_t val ) { assert( paramIdx < m_params.size() ); diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index e0aa499a..1160ccaa 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -21,13 +21,11 @@ #include "TracyShortPtr.hpp" #include "TracySlab.hpp" #include "TracyStringDiscovery.hpp" +#include "TracyTextureCompression.hpp" #include "TracyThreadCompress.hpp" #include "TracyVarArray.hpp" -struct ZSTD_CCtx_s; -struct ZSTD_DCtx_s; - namespace tracy { @@ -486,9 +484,7 @@ public: const FailureData& GetFailureData() const { return m_failureData; } static const char* GetFailureString( Failure failure ); - void PackFrameImage( struct ZSTD_CCtx_s* ctx, char*& buf, size_t& bufsz, const char* image, uint32_t inBytes, uint32_t& csz ) const; - const char* PackFrameImage( const char* image, uint32_t inBytes, uint32_t& csz ); - const char* UnpackFrameImage( const FrameImage& image ); + const char* UnpackFrameImage( const FrameImage& image ) { return m_texcomp.Unpack( image ); } const Vector& GetParameters() const { return m_params; } void SetParameter( size_t paramIdx, int32_t val ); @@ -772,10 +768,7 @@ private: unordered_flat_map m_frameImageStaging; char* m_frameImageBuffer = nullptr; size_t m_frameImageBufferSize = 0; - char* m_frameImageCompressedBuffer = nullptr; - size_t m_frameImageCompressedBufferSize = 0; - struct ZSTD_CCtx_s* m_fiCctx = nullptr; - struct ZSTD_DCtx_s* m_fiDctx = nullptr; + TextureCompression m_texcomp; uint64_t m_threadCtx = 0; ThreadData* m_threadCtxData = nullptr; diff --git a/update/build/win32/update.vcxproj b/update/build/win32/update.vcxproj index 32c61369..dc3ac397 100644 --- a/update/build/win32/update.vcxproj +++ b/update/build/win32/update.vcxproj @@ -137,6 +137,7 @@ + @@ -189,6 +190,7 @@ + diff --git a/update/build/win32/update.vcxproj.filters b/update/build/win32/update.vcxproj.filters index 61824d8e..7d304e99 100644 --- a/update/build/win32/update.vcxproj.filters +++ b/update/build/win32/update.vcxproj.filters @@ -123,6 +123,9 @@ server + + server + @@ -287,5 +290,8 @@ server + + server + \ No newline at end of file