Skip to content

Commit 224f7bf

Browse files
committed
Fixed a couple of instances where custom memory allocators were not used in main code and FrameRecorder, as well as separating playback helper code from recording.
1 parent 2f06df7 commit 224f7bf

12 files changed

+173
-103
lines changed

CullingThreadpool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ void CullingThreadpool::ClearBuffer()
432432

433433
void CullingThreadpool::RenderTriangles(const float *inVtx, const unsigned int *inTris, int nTris, BackfaceWinding bfWinding, ClipPlanes clipPlaneMask)
434434
{
435-
#if ENABLE_RECORDER != 0
435+
#if MOC_RECORDER_ENABLE != 0
436436
mMOC->RecordRenderTriangles( inVtx, inTris, nTris, mCurrentMatrix, clipPlaneMask, bfWinding, *mVertexLayouts.GetData( ) );
437437
#endif
438438

FillrateTest/FillrateTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ int main(int argc, char* argv[])
378378

379379
};
380380

381-
#if 0 && ENABLE_RECORDER
381+
#if 0 && MOC_RECORDER_ENABLE
382382
moc->RecorderStart( "..\\FrameRecorderPlayer\\FillrateTest.mocrec" );
383383
#endif
384384

@@ -393,7 +393,7 @@ int main(int argc, char* argv[])
393393
printf("Tri: %3dx%3d - Time: %7.2f ms, MTris/s: %6.2f GPixels/s: %5.2f \n", size, size, t * 1000.0f, MTrisPerSecond, GPixelsPerSecond);
394394
}
395395

396-
#if 0 && ENABLE_RECORDER
396+
#if 0 && MOC_RECORDER_ENABLE
397397
moc->RecorderStop( );
398398
#endif
399399

FillrateTest/FillrateTest.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
</PrecompiledHeader>
8787
<WarningLevel>Level3</WarningLevel>
8888
<Optimization>Disabled</Optimization>
89-
<PreprocessorDefinitions>ENABLE_RECORDER=0;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
89+
<PreprocessorDefinitions>MOC_RECORDER_ENABLE=0;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9090
<OpenMPSupport>false</OpenMPSupport>
9191
</ClCompile>
9292
<Link>

FrameRecorder.cpp

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
////////////////////////////////////////////////////////////////////////////////
1616
#include "FrameRecorder.h"
1717

18-
#if ENABLE_RECORDER
18+
#if MOC_RECORDER_ENABLE
1919
#include <assert.h>
2020
#include <algorithm>
2121

@@ -35,21 +35,27 @@ bool MaskedOcclusionCulling::RecorderStart( const char * outputFilePath ) const
3535
FILE * f;
3636
if( fopen_s( &f, outputFilePath, "wb" ) != 0 )
3737
return false;
38-
mRecorder = new FrameRecorder( f, *this );
3938
#else
4039
std::ofstream outStream( outputFilePath, std::ios::out | std::ios::trunc | std::ios::binary );
4140
if( !outStream.is_open( ) )
4241
return false;
43-
mRecorder = new FrameRecorder( std::move( outStream ), *this );
4442
#endif
43+
mRecorder = (FrameRecorder *)mAlignedAllocCallback( 64, sizeof( FrameRecorder ) );
44+
#if MOC_RECORDER_USE_STDIO_FILE
45+
new (mRecorder) FrameRecorder( f, *this );
46+
#else
47+
new (mRecorder) FrameRecorder( std::move( outStream ), *this );
48+
#endif
49+
4550
return true;
4651
}
4752

4853
void MaskedOcclusionCulling::RecorderStop( ) const
4954
{
5055
std::lock_guard<std::mutex> lock( mRecorderMutex );
5156

52-
delete mRecorder;
57+
mRecorder->~FrameRecorder();
58+
mAlignedFreeCallback( mRecorder );
5359
mRecorder = nullptr;
5460
}
5561

@@ -204,6 +210,47 @@ namespace
204210
};
205211
}
206212

213+
void FrameRecorder::RecordClearBuffer( )
214+
{
215+
char header = 3;
216+
Write( &header, 1 );
217+
}
218+
219+
void FrameRecorder::RecordRenderTriangles( MaskedOcclusionCulling::CullingResult cullingResult, const float *inVtx, const unsigned int *inTris, int nTris, const float *modelToClipMatrix, MaskedOcclusionCulling::ClipPlanes clipPlaneMask, MaskedOcclusionCulling::BackfaceWinding bfWinding, const MaskedOcclusionCulling::VertexLayout & vtxLayout )
220+
{
221+
char header = 0;
222+
Write( &header, 1 );
223+
WriteTriangleRecording( cullingResult, inVtx, inTris, nTris, modelToClipMatrix, clipPlaneMask, bfWinding, vtxLayout );
224+
}
225+
226+
void FrameRecorder::RecordTestRect( MaskedOcclusionCulling::CullingResult cullingResult, float xmin, float ymin, float xmax, float ymax, float wmin )
227+
{
228+
char header = 1;
229+
Write( &header, 1 );
230+
231+
Write( &cullingResult, sizeof( cullingResult ) );
232+
Write( &xmin, sizeof( xmin ) );
233+
Write( &ymin, sizeof( ymin ) );
234+
Write( &xmax, sizeof( xmax ) );
235+
Write( &ymax, sizeof( ymax ) );
236+
Write( &wmin, sizeof( wmin ) );
237+
}
238+
239+
void FrameRecorder::RecordTestTriangles( MaskedOcclusionCulling::CullingResult cullingResult, const float *inVtx, const unsigned int *inTris, int nTris, const float *modelToClipMatrix, MaskedOcclusionCulling::ClipPlanes clipPlaneMask, MaskedOcclusionCulling::BackfaceWinding bfWinding, const MaskedOcclusionCulling::VertexLayout & vtxLayout )
240+
{
241+
char header = 2;
242+
Write( &header, 1 );
243+
WriteTriangleRecording( cullingResult, inVtx, inTris, nTris, modelToClipMatrix, clipPlaneMask, bfWinding, vtxLayout );
244+
}
245+
246+
247+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
248+
// Masked occlusion culling recording
249+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
250+
251+
252+
#if MOC_RECORDER_ENABLE_PLAYBACK
253+
207254
static bool ReadTriangleRecording( FrameRecording::TrianglesEntry & outEntry, InStreamWrapper & inStream )
208255
{
209256
// read culling result
@@ -240,7 +287,7 @@ static bool ReadTriangleRecording( FrameRecording::TrianglesEntry & outEntry, In
240287

241288
// read vertices
242289
outEntry.mVertices.resize( vertexSize / 4 * vertexCount ); // pre-allocate data
243-
if( inStream.Read( (char*) outEntry.mVertices.data(), vertexSize * vertexCount ) != (vertexSize * vertexCount) )
290+
if( inStream.Read( (char*)outEntry.mVertices.data( ), vertexSize * vertexCount ) != ( vertexSize * vertexCount ) )
244291
{
245292
assert( false );
246293
return false;
@@ -269,7 +316,7 @@ static bool ReadTriangleRecording( FrameRecording::TrianglesEntry & outEntry, In
269316
return false;
270317
}
271318

272-
if( (outEntry.mHasModelToClipMatrix = (hasMatrix != 0)) )
319+
if( ( outEntry.mHasModelToClipMatrix = ( hasMatrix != 0 ) ) )
273320
{
274321
if( inStream.Read( (char*)outEntry.mModelToClipMatrix, 16 * sizeof( float ) ) != 16 * sizeof( float ) )
275322
{
@@ -309,44 +356,6 @@ static bool ReadTriangleRecording( FrameRecording::TrianglesEntry & outEntry, In
309356
return true;
310357
}
311358

312-
void FrameRecorder::RecordClearBuffer( )
313-
{
314-
char header = 3;
315-
Write( &header, 1 );
316-
}
317-
318-
void FrameRecorder::RecordRenderTriangles( MaskedOcclusionCulling::CullingResult cullingResult, const float *inVtx, const unsigned int *inTris, int nTris, const float *modelToClipMatrix, MaskedOcclusionCulling::ClipPlanes clipPlaneMask, MaskedOcclusionCulling::BackfaceWinding bfWinding, const MaskedOcclusionCulling::VertexLayout & vtxLayout )
319-
{
320-
char header = 0;
321-
Write( &header, 1 );
322-
WriteTriangleRecording( cullingResult, inVtx, inTris, nTris, modelToClipMatrix, clipPlaneMask, bfWinding, vtxLayout );
323-
}
324-
325-
void FrameRecorder::RecordTestRect( MaskedOcclusionCulling::CullingResult cullingResult, float xmin, float ymin, float xmax, float ymax, float wmin )
326-
{
327-
char header = 1;
328-
Write( &header, 1 );
329-
330-
Write( &cullingResult, sizeof( cullingResult ) );
331-
Write( &xmin, sizeof( xmin ) );
332-
Write( &ymin, sizeof( ymin ) );
333-
Write( &xmax, sizeof( xmax ) );
334-
Write( &ymax, sizeof( ymax ) );
335-
Write( &wmin, sizeof( wmin ) );
336-
}
337-
338-
void FrameRecorder::RecordTestTriangles( MaskedOcclusionCulling::CullingResult cullingResult, const float *inVtx, const unsigned int *inTris, int nTris, const float *modelToClipMatrix, MaskedOcclusionCulling::ClipPlanes clipPlaneMask, MaskedOcclusionCulling::BackfaceWinding bfWinding, const MaskedOcclusionCulling::VertexLayout & vtxLayout )
339-
{
340-
char header = 2;
341-
Write( &header, 1 );
342-
WriteTriangleRecording( cullingResult, inVtx, inTris, nTris, modelToClipMatrix, clipPlaneMask, bfWinding, vtxLayout );
343-
}
344-
345-
346-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
347-
// Masked occlusion culling recording
348-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
349-
350359
bool FrameRecording::Load( const char * inputFilePath, FrameRecording & outRecording )
351360
{
352361
outRecording.Reset();
@@ -438,4 +447,6 @@ bool FrameRecording::Load( const char * inputFilePath, FrameRecording & outRecor
438447
return true;
439448
}
440449

441-
#endif // #if ENABLE_RECORDER
450+
#endif // #if MOC_RECORDER_ENABLE_PLAYBACK
451+
452+
#endif // #if MOC_RECORDER_ENABLE

FrameRecorder.h

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
/*!
1919
* \file FrameRecorder.h
20-
* \brief Masked occlusion culling recorder class (set ENABLE_RECORDER to 1 to enable)
20+
* \brief Masked occlusion culling recorder class (set MOC_RECORDER_ENABLE to 1 to enable)
2121
*
22-
* Masked occlusion culling recorder class (To enable, set ENABLE_RECORDER to 1 in MaskedOcclusionCulling.h)
22+
* Masked occlusion culling recorder class (To enable, set MOC_RECORDER_ENABLE to 1 in MaskedOcclusionCulling.h)
2323
*
2424
* Enables gathering and storing all triangle rendering and all testing calls and their results to a file, for
2525
* later playback and performance testing.
@@ -42,10 +42,17 @@
4242
/*!
4343
* Whether to use FILE or std::ofstream/istream for file access (to avoid compatibility issues in some environments)
4444
*/
45-
#define MOC_RECORDER_USE_STDIO_FILE 0
45+
#define MOC_RECORDER_USE_STDIO_FILE 1
4646
#endif
4747

48-
#if ENABLE_RECORDER
48+
#if MOC_RECORDER_ENABLE
49+
50+
#ifndef MOC_RECORDER_ENABLE_PLAYBACK
51+
/*!
52+
* Whether to enable compilation of the playback code (not needed for recording only)
53+
*/
54+
#define MOC_RECORDER_ENABLE_PLAYBACK 0
55+
#endif
4956

5057
#if MOC_RECORDER_USE_STDIO_FILE
5158
#include <stdio.h>
@@ -54,7 +61,6 @@
5461
#endif
5562

5663
#include <mutex>
57-
#include <vector>
5864

5965
class FrameRecorder
6066
{
@@ -86,6 +92,36 @@ class FrameRecorder
8692
void RecordTestTriangles( MaskedOcclusionCulling::CullingResult cullingResult, const float *inVtx, const unsigned int *inTris, int nTris, const float *modelToClipMatrix, MaskedOcclusionCulling::ClipPlanes clipPlaneMask, MaskedOcclusionCulling::BackfaceWinding bfWinding, const MaskedOcclusionCulling::VertexLayout &vtxLayout );
8793
};
8894

95+
#if MOC_RECORDER_ENABLE_PLAYBACK
96+
#include <vector>
97+
98+
#if 0 // For future use - in case all vector uses below need conversion to custom allocator
99+
template <class T>
100+
struct MOCVectorAllocator
101+
{
102+
const MaskedOcclusionCulling::pfnAlignedAlloc m_alloc;
103+
const MaskedOcclusionCulling::pfnAlignedFree m_free;
104+
typedef T value_type;
105+
MOCVectorAllocator( ) = delete;
106+
MOCVectorAllocator( MaskedOcclusionCulling::pfnAlignedAlloc alloc, MaskedOcclusionCulling::pfnAlignedFree free ) noexcept : m_alloc( alloc ), m_free( free ) { }
107+
template <class U> constexpr MOCVectorAllocator( const MOCVectorAllocator<U>& c ) noexcept : m_alloc( c.m_alloc ), m_free( c.m_free ) {}
108+
T* allocate( std::size_t n )
109+
{
110+
if( n > std::size_t( -1 ) / sizeof( T ) ) throw std::bad_alloc( );
111+
if( auto p = static_cast<T*>( m_alloc( 64, n * sizeof( T ) ) ) ) return p;
112+
throw std::bad_alloc( );
113+
}
114+
void deallocate( T* p, std::size_t ) noexcept
115+
{
116+
m_free( p );
117+
}
118+
};
119+
template <class T, class U>
120+
inline bool operator==( const MOCVectorAllocator<T>&, const MOCVectorAllocator<U>& ) { return true; }
121+
template <class T, class U>
122+
inline bool operator!=( const MOCVectorAllocator<T>&, const MOCVectorAllocator<U>& ) { return false; }
123+
#endif
124+
89125
struct FrameRecording
90126
{
91127
struct TrianglesEntry
@@ -146,4 +182,6 @@ struct FrameRecording
146182
static bool Load( const char * inputFilePath, FrameRecording & outRecording );
147183
};
148184

149-
#endif // #if ENABLE_RECORDER
185+
#endif // #if MOC_RECORDER_ENABLE_PLAYBACK
186+
187+
#endif // #if MOC_RECORDER_ENABLE

FrameRecorderPlayer/FrameRecorderPlayer.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
#include "../MaskedOcclusionCulling.h"
3131
#include "../FrameRecorder.h"
3232

33-
#if !ENABLE_RECORDER
34-
#error This project needs to be compiled with ENABLE_RECORDER set to 1
33+
#if !MOC_RECORDER_ENABLE
34+
#error This project needs to be compiled with MOC_RECORDER_ENABLE set to 1
35+
#endif
36+
37+
#if !MOC_RECORDER_ENABLE_PLAYBACK
38+
#error This project needs to be compiled with MOC_RECORDER_ENABLE_PLAYBACK set to 1
3539
#endif
3640

3741
////////////////////////////////////////////////////////////////////////////////////////

FrameRecorderPlayer/FrameRecorderPlayer.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
</PrecompiledHeader>
8787
<WarningLevel>Level3</WarningLevel>
8888
<Optimization>Disabled</Optimization>
89-
<PreprocessorDefinitions>ENABLE_RECORDER=1;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
89+
<PreprocessorDefinitions>MOC_RECORDER_ENABLE=1;MOC_RECORDER_ENABLE_PLAYBACK=1;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9090
</ClCompile>
9191
<Link>
9292
<SubSystem>Console</SubSystem>
@@ -99,7 +99,7 @@
9999
</PrecompiledHeader>
100100
<WarningLevel>Level3</WarningLevel>
101101
<Optimization>Disabled</Optimization>
102-
<PreprocessorDefinitions>ENABLE_RECORDER=1;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
102+
<PreprocessorDefinitions>MOC_RECORDER_ENABLE=1;MOC_RECORDER_ENABLE_PLAYBACK=1;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
103103
<AdditionalOptions>-msse4.1 %(AdditionalOptions)</AdditionalOptions>
104104
</ClCompile>
105105
<Link>
@@ -115,7 +115,7 @@
115115
<Optimization>MaxSpeed</Optimization>
116116
<FunctionLevelLinking>true</FunctionLevelLinking>
117117
<IntrinsicFunctions>true</IntrinsicFunctions>
118-
<PreprocessorDefinitions>ENABLE_RECORDER=1;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118+
<PreprocessorDefinitions>MOC_RECORDER_ENABLE=1;MOC_RECORDER_ENABLE_PLAYBACK=1;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
119119
</ClCompile>
120120
<Link>
121121
<SubSystem>Console</SubSystem>
@@ -132,7 +132,7 @@
132132
<Optimization>MaxSpeed</Optimization>
133133
<FunctionLevelLinking>true</FunctionLevelLinking>
134134
<IntrinsicFunctions>true</IntrinsicFunctions>
135-
<PreprocessorDefinitions>ENABLE_RECORDER=1;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
135+
<PreprocessorDefinitions>MOC_RECORDER_ENABLE=1;MOC_RECORDER_ENABLE_PLAYBACK=1;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
136136
<AdditionalOptions>-msse4.1 %(AdditionalOptions)</AdditionalOptions>
137137
</ClCompile>
138138
<Link>

0 commit comments

Comments
 (0)