This repository was archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathFGApp.cpp
540 lines (443 loc) · 13.7 KB
/
FGApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright (c) 2018-2020, Zhirnov Andrey. For more information see 'LICENSE'
#include "FGApp.h"
#include "graphviz/GraphViz.h"
#include "framework/Window/WindowGLFW.h"
#include "framework/Window/WindowSDL2.h"
#include "framework/Window/WindowAndroid.h"
#include "stl/Stream/FileStream.h"
#include "stl/Algorithms/StringParser.h"
#ifdef FG_ENABLE_GLSLANG
# include "pipeline_compiler/VPipelineCompiler.h"
#endif
#ifdef FG_ENABLE_LODEPNG
# include "lodepng.h"
#endif
extern void UnitTest_VResourceManager (const FG::FrameGraph &fg);
namespace FG
{
namespace {
static constexpr uint UpdateAllReferenceDumps = false;
}
/*
=================================================
constructor
=================================================
*/
FGApp::FGApp ()
{
_tests.push_back({ &FGApp::Test_CopyBuffer1, 1 });
_tests.push_back({ &FGApp::Test_CopyImage1, 1 });
_tests.push_back({ &FGApp::Test_CopyImage2, 1 });
_tests.push_back({ &FGApp::Test_CopyImage3, 1 });
_tests.push_back({ &FGApp::Test_PushConst1, 1 });
_tests.push_back({ &FGApp::Test_Compute1, 1 });
_tests.push_back({ &FGApp::Test_Compute2, 1 });
_tests.push_back({ &FGApp::Test_DynamicOffset, 1 });
_tests.push_back({ &FGApp::Test_Draw1, 1 });
_tests.push_back({ &FGApp::Test_Draw2, 1 });
_tests.push_back({ &FGApp::Test_Draw3, 1 });
_tests.push_back({ &FGApp::Test_Draw4, 1 });
_tests.push_back({ &FGApp::Test_Draw5, 1 });
_tests.push_back({ &FGApp::Test_Draw6, 1 });
_tests.push_back({ &FGApp::Test_Draw7, 1 });
_tests.push_back({ &FGApp::Test_RawDraw1, 1 });
_tests.push_back({ &FGApp::Test_ExternalCmdBuf1, 1 });
_tests.push_back({ &FGApp::Test_ReadAttachment1, 1 });
_tests.push_back({ &FGApp::Test_AsyncCompute1, 1 });
_tests.push_back({ &FGApp::Test_AsyncCompute2, 1 });
_tests.push_back({ &FGApp::Test_ShaderDebugger1, 1 });
_tests.push_back({ &FGApp::Test_ShaderDebugger2, 1 });
_tests.push_back({ &FGApp::Test_ArrayOfTextures1, 1 });
_tests.push_back({ &FGApp::Test_ArrayOfTextures2, 1 });
_tests.push_back({ &FGApp::ImplTest_Scene1, 1 });
_tests.push_back({ &FGApp::ImplTest_Multithreading1, 1 });
_tests.push_back({ &FGApp::ImplTest_Multithreading2, 1 });
_tests.push_back({ &FGApp::ImplTest_Multithreading3, 1 });
_tests.push_back({ &FGApp::ImplTest_Multithreading4, 1 });
// RTX only
_tests.push_back({ &FGApp::Test_DrawMeshes1, 1 });
_tests.push_back({ &FGApp::Test_TraceRays1, 1 });
_tests.push_back({ &FGApp::Test_TraceRays2, 1 });
_tests.push_back({ &FGApp::Test_TraceRays3, 1 });
_tests.push_back({ &FGApp::Test_ShadingRate1, 1 });
_tests.push_back({ &FGApp::Test_RayTracingDebugger1, 1 });
// very slow
//_tests.push_back({ &FGApp::ImplTest_CacheOverflow1, 1 });
// should not crash
//_tests.push_back({ &FGApp::Test_InvalidID, 1 });
}
/*
=================================================
destructor
=================================================
*/
FGApp::~FGApp ()
{
}
/*
=================================================
OnResize
=================================================
*/
void FGApp::OnResize (const uint2 &size)
{
if ( Any( size == uint2(0) ))
return;
CHECK( _frameGraph->WaitIdle() );
#ifdef FG_ENABLE_VULKAN
VulkanSwapchainCreateInfo swapchain_info;
swapchain_info.surface = BitCast<SurfaceVk_t>( _vulkan.GetVkSurface() );
swapchain_info.surfaceSize = size;
_swapchainId = _frameGraph->CreateSwapchain( swapchain_info, _swapchainId.Release() );
CHECK_FATAL( _swapchainId );
#endif
}
/*
=================================================
_Initialize
=================================================
*/
bool FGApp::_Initialize (WindowPtr &&wnd)
{
#ifdef FG_ENABLE_VULKAN
return _InitializeForVulkan( std::move(wnd) );
#else
return false;
#endif
}
/*
=================================================
_InitializeForVulkan
=================================================
*/
#ifdef FG_ENABLE_VULKAN
bool FGApp::_InitializeForVulkan (WindowPtr &&wnd)
{
const uint2 wnd_size{ 800, 600 };
// initialize window
{
_window = std::move(wnd);
CHECK_ERR( _window->Create( wnd_size, "Test" ));
_window->AddListener( this );
}
// initialize vulkan device
{
CHECK_ERR( _vulkan.CreateInstance( _window->GetVulkanSurface(), "Test", "FrameGraph", _vulkan.GetRecomendedInstanceLayers(), {}, {1,2} ));
CHECK_ERR( _vulkan.ChooseHighPerformanceDevice() );
#ifdef PLATFORM_ANDROID
CHECK_ERR( _vulkan.CreateLogicalDevice( {{VK_QUEUE_GRAPHICS_BIT}}, Default ));
#else
CHECK_ERR( _vulkan.CreateLogicalDevice( {{VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_SPARSE_BINDING_BIT}, {VK_QUEUE_COMPUTE_BIT}, {VK_QUEUE_TRANSFER_BIT}}, Default ));
#endif
// this is a test and the test should fail for any validation error
_vulkan.CreateDebugCallback( DefaultDebugMessageSeverity,
[] (const VulkanDeviceInitializer::DebugReport &rep) { FG_LOGI(rep.message); CHECK_FATAL(not rep.isError); });
}
// setup device info
VulkanDeviceInfo vulkan_info;
VulkanSwapchainCreateInfo swapchain_info;
{
vulkan_info.instance = BitCast<InstanceVk_t>( _vulkan.GetVkInstance() );
vulkan_info.physicalDevice = BitCast<PhysicalDeviceVk_t>( _vulkan.GetVkPhysicalDevice() );
vulkan_info.device = BitCast<DeviceVk_t>( _vulkan.GetVkDevice() );
vulkan_info.maxStagingBufferMemory = ~0_b;
vulkan_info.stagingBufferSize = 8_Mb;
swapchain_info.surface = BitCast<SurfaceVk_t>( _vulkan.GetVkSurface() );
swapchain_info.surfaceSize = _window->GetSize();
for (auto& q : _vulkan.GetVkQueues())
{
VulkanDeviceInfo::QueueInfo qi;
qi.handle = BitCast<QueueVk_t>( q.handle );
qi.familyFlags = BitCast<QueueFlagsVk_t>( q.familyFlags );
qi.familyIndex = q.familyIndex;
qi.priority = q.priority;
qi.debugName = q.debugName;
vulkan_info.queues.push_back( qi );
}
}
// initialize framegraph
{
_frameGraph = IFrameGraph::CreateFrameGraph( vulkan_info );
CHECK_ERR( _frameGraph );
_properties = _frameGraph->GetDeviceProperties();
_swapchainId = _frameGraph->CreateSwapchain( swapchain_info, Default, "Window" );
CHECK_ERR( _swapchainId );
}
// add glsl pipeline compiler
#ifdef FG_ENABLE_GLSLANG
{
//_pplnCompiler = MakeShared<VPipelineCompiler>();
_pplnCompiler = MakeShared<VPipelineCompiler>( vulkan_info.instance, vulkan_info.physicalDevice, vulkan_info.device );
_pplnCompiler->SetCompilationFlags( EShaderCompilationFlags::Quiet |
EShaderCompilationFlags::ParseAnnotations |
EShaderCompilationFlags::UseCurrentDeviceLimits );
_frameGraph->AddPipelineCompiler( _pplnCompiler );
#ifdef FG_ENABLE_GLSL_TRACE
_hasShaderDebugger = _pplnCompiler and FG_EnableShaderDebugging;
#endif
}
#endif // FG_ENABLE_GLSLANG
UnitTest_VResourceManager( _frameGraph );
return true;
}
#endif // FG_ENABLE_VULKAN
/*
=================================================
_Update
=================================================
*/
bool FGApp::_Update ()
{
if ( not _window->Update() )
return false;
if ( not _tests.empty() )
{
TestFunc_t func = _tests.front().first;
const uint max_invoc = _tests.front().second;
bool passed = (this->*func)();
if ( _testInvocations == 0 )
{
_testsPassed += uint(passed);
_testsFailed += uint(not passed);
}
if ( (not passed) or (++_testInvocations >= max_invoc) )
{
_tests.pop_front();
_testInvocations = 0;
// reset
String temp;
_frameGraph->DumpToGraphViz( OUT temp );
_frameGraph->DumpToString( OUT temp );
}
}
else
{
_window->Quit();
FG_LOGI( "Tests passed: " + ToString( _testsPassed ) + ", failed: " + ToString( _testsFailed ));
CHECK_FATAL( _testsFailed == 0 );
}
return true;
}
/*
=================================================
_Destroy
=================================================
*/
void FGApp::_Destroy ()
{
_pplnCompiler = null;
if ( _swapchainId and _frameGraph )
{
_frameGraph->ReleaseResource( INOUT _swapchainId );
}
if ( _frameGraph )
{
_frameGraph->Deinitialize();
_frameGraph = null;
}
#ifdef FG_ENABLE_VULKAN
_vulkan.DestroyLogicalDevice();
_vulkan.DestroyInstance();
#endif
if ( _window )
{
_window->Destroy();
_window.reset();
}
}
/*
=================================================
SavePNG
=================================================
*/
#ifdef FG_ENABLE_LODEPNG
bool FGApp::SavePNG (const String &filename, const ImageView &imageData) const
{
LodePNGColorType colortype = LodePNGColorType(-1);
uint bitdepth = 0;
uint bitperpixel = 0;
switch ( imageData.Format() )
{
case EPixelFormat::RGBA8_UNorm :
case EPixelFormat::BGRA8_UNorm :
colortype = LodePNGColorType::LCT_RGBA;
bitdepth = 8;
bitperpixel = 4*8;
break;
case EPixelFormat::RGBA16_UNorm :
colortype = LodePNGColorType::LCT_RGBA;
bitdepth = 16;
bitperpixel = 4*16;
break;
case EPixelFormat::RGB8_UNorm :
case EPixelFormat::BGR8_UNorm :
colortype = LodePNGColorType::LCT_RGB;
bitdepth = 8;
bitperpixel = 3*8;
break;
case EPixelFormat::RGB16_UNorm :
colortype = LodePNGColorType::LCT_RGB;
bitdepth = 16;
bitperpixel = 3*16;
break;
case EPixelFormat::R8_UNorm :
colortype = LodePNGColorType::LCT_GREY;
bitdepth = 8;
bitperpixel = 8;
break;
case EPixelFormat::R16_UNorm :
colortype = LodePNGColorType::LCT_GREY;
bitdepth = 16;
bitperpixel = 16;
break;
default :
RETURN_ERR( "unsupported pixel format!" );
}
uint err = 0;
if ( imageData.Parts().size() == 1 and imageData.RowPitch() == imageData.RowSize() )
{
err = lodepng::encode( filename, imageData.data(), imageData.Dimension().x, imageData.Dimension().y, colortype, bitdepth );
}
else
{
const size_t row_size = size_t(imageData.RowSize());
Array<uint8_t> pixels; pixels.resize( row_size * imageData.Dimension().y );
for (uint y = 0; y < imageData.Dimension().y; ++y)
{
auto row = imageData.GetRow( y );
std::memcpy( pixels.data() + (row_size * y), row.data(), row_size );
}
err = lodepng::encode( filename, pixels.data(), imageData.Dimension().x, imageData.Dimension().y, colortype, bitdepth );
}
CHECK_ERR( err == 0 );
//const char * error_text = lodepng_error_text( err );
return true;
}
#endif // FG_ENABLE_LODEPNG
/*
=================================================
Visualize
=================================================
*/
bool FGApp::Visualize (StringView name) const
{
#if defined(FG_GRAPHVIZ_DOT_EXECUTABLE) and defined(FS_HAS_FILESYSTEM)
String str;
CHECK_ERR( _frameGraph->DumpToGraphViz( OUT str ));
auto path = FS::path{ FG_TEST_GRAPHS_DIR }.append( name.data() ).replace_extension( "dot" );
CHECK( GraphViz::Visualize( str, path, "png", false, true ));
#else
// not supported
Unused( name );
#endif
return true;
}
/*
=================================================
CompareDumps
=================================================
*/
bool FGApp::CompareDumps (StringView filename) const
{
#ifndef PLATFORM_ANDROID
String fname {FG_TEST_DUMPS_DIR}; fname << '/' << filename << ".txt";
String right;
CHECK_ERR( _frameGraph->DumpToString( OUT right ));
// override dump
if ( UpdateAllReferenceDumps )
{
FileWStream wfile{ fname };
CHECK_ERR( wfile.IsOpen() );
CHECK_ERR( wfile.Write( StringView{right} ));
return true;
}
// read from file
String left;
{
FileRStream rfile{ fname };
CHECK_ERR( rfile.IsOpen() );
CHECK_ERR( rfile.Read( size_t(rfile.Size()), OUT left ));
}
size_t l_pos = 0;
size_t r_pos = 0;
uint2 line_number;
StringView line_str[2];
const auto LeftValid = [&l_pos, &left ] () { return l_pos < left.length(); };
const auto RightValid = [&r_pos, &right] () { return r_pos < right.length(); };
const auto IsEmptyLine = [] (StringView str)
{
for (auto& c : str) {
if ( c != '\n' and c != '\r' and c != ' ' and c != '\t' )
return false;
}
return true;
};
// compare line by line
for (; LeftValid() and RightValid(); )
{
// read left line
do {
StringParser::ReadLineToEnd( left, INOUT l_pos, OUT line_str[0] );
++line_number[0];
}
while ( IsEmptyLine( line_str[0] ) and LeftValid() );
// read right line
do {
StringParser::ReadLineToEnd( right, INOUT r_pos, OUT line_str[1] );
++line_number[1];
}
while ( IsEmptyLine( line_str[1] ) and RightValid() );
if ( line_str[0] != line_str[1] )
{
RETURN_ERR( "in: "s << filename << "\n\n"
<< "line mismatch:" << "\n(" << ToString( line_number[0] ) << "): " << line_str[0]
<< "\n(" << ToString( line_number[1] ) << "): " << line_str[1] );
}
}
if ( LeftValid() != RightValid() )
{
RETURN_ERR( "in: "s << filename << "\n\n" << "sizes of dumps are not equal!" );
}
return true;
#else
Unused( filename );
return true;
#endif // not PLATFORM_ANDROID
}
/*
=================================================
CreateData
=================================================
*/
Array<uint8_t> FGApp::CreateData (BytesU size) const
{
Array<uint8_t> arr;
arr.resize( size_t(size) );
return arr;
}
/*
=================================================
Run
=================================================
*/
void FGApp::Run (void* nativeHandle)
{
Unused( nativeHandle );
FGApp app;
UniquePtr<IWindow> wnd;
#if defined( FG_CI_BUILD )
return;
#elif defined( FG_ENABLE_GLFW )
wnd.reset( new WindowGLFW() );
#elif defined( FG_ENABLE_SDL2 )
wnd.reset( new WindowSDL2() );
#elif defined( PLATFORM_ANDROID )
wnd.reset( new WindowAndroid{ static_cast<android_app*>(nativeHandle) });
#else
# error Unknown window library!
#endif
CHECK_FATAL( app._Initialize( std::move(wnd) ));
for (; app._Update(); ) {}
app._Destroy();
}
} // FG