FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Explosion/Sample/Test/RenderingSampleTest.cpp at master · ExplosionEngine/Explosion · GitHub
ExplosionEngine
Explosion
Repository navigation
Code
Issues
1
(1)
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Explosion
/
Sample
/
Test
/
RenderingSampleTest.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
138 lines (125 loc) · 5.13 KB
Breadcrumbs
Explosion
/
Sample
/
Test
/
RenderingSampleTest.cpp
Copy path
File metadata and controls
138 lines (125 loc) · 5.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
filesystem
>
#
include
<
iostream
>
#
include
<
stdexcept
>
#
include
<
string
>
#
include
<
vector
>
#
include
<
CImg.h
>
#
include
<
Common/Process.h
>
#
define
STB_IMAGE_IMPLEMENTATION
#
include
<
stb_image.h
>
namespace
Sample
::Test {
struct
Params
{
std::string samplePath;
std::string rhi;
std::string baselinePath;
std::string outputDirectory;
};
static
bool
ParseParams
(
const
int
argc,
char
* argv[], Params& outParams)
{
if
(argc !=
9
) {
return
false
;
}
for
(
int
i =
1
; i +
1
< argc; i +=
2
) {
const
std::string argument = argv[i];
const
std::string value = argv[i +
1
];
if
(argument ==
"
--sample
"
) {
outParams.
samplePath
= value;
}
else
if
(argument ==
"
--rhi
"
) {
outParams.
rhi
= value;
}
else
if
(argument ==
"
--baseline
"
) {
outParams.
baselinePath
= value;
}
else
if
(argument ==
"
--output-dir
"
) {
outParams.
outputDirectory
= value;
}
else
{
return
false
;
}
}
return
!outParams.
samplePath
.
empty
() && !outParams.
rhi
.
empty
() && !outParams.
baselinePath
.
empty
() && !outParams.
outputDirectory
.
empty
();
}
static
bool
RunSample
(
const
Params& params)
{
const
std::filesystem::path
outputDirectory
(params.
outputDirectory
);
const
auto
baselinePath = outputDirectory /
"
baseline.png
"
;
const
auto
actualPath = outputDirectory /
"
actual.png
"
;
const
auto
diffPath = outputDirectory /
"
diff.bmp
"
;
std::filesystem::create_directories
(outputDirectory);
std::filesystem::remove
(actualPath);
std::filesystem::remove
(diffPath);
std::filesystem::copy_file
(params.
baselinePath
, baselinePath, std::filesystem::copy_options::overwrite_existing);
std::vector<std::string> arguments = {
"
-headless
"
,
"
-rhi
"
, params.
rhi
,
"
-output
"
, actualPath.
string
()};
#
if
CI
arguments.
emplace_back
(
"
-softwareGpu
"
);
#
endif
std::cout <<
"
Running:
"
<< params.
samplePath
;
for
(
const
auto
& argument : arguments) {
std::cout <<
'
'
<< argument;
}
std::cout << std::endl;
const
auto
exitCode =
Common::Process::Run
(params.
samplePath
, arguments);
if
(!exitCode.
has_value
()) {
std::cerr <<
"
Failed to launch rendering sample
"
<< std::endl;
return
false
;
}
return
exitCode.
value
() ==
0
;
}
static
cimg_library::CImg<
unsigned
char
>
LoadImage
(
const
std::string& path)
{
int
width =
0
;
int
height =
0
;
int
sourceChannels =
0
;
constexpr
int
channels =
4
;
unsigned
char
* pixels =
stbi_load
(path.
c_str
(), &width, &height, &sourceChannels, channels);
if
(pixels ==
nullptr
) {
throw
std::runtime_error
(
"
Failed to load image:
"
+ path);
}
cimg_library::CImg<
unsigned
char
>
image
(width, height,
1
, channels);
cimg_forXYC
(image, x, y, channel) {
image
(x, y,
0
, channel) = pixels[(y * width + x) * channels + channel];
}
stbi_image_free
(pixels);
return
image;
}
static
bool
CompareImages
(
const
Params& params)
{
using
Image = cimg_library::CImg<
unsigned
char
>;
const
auto
outputDirectory =
std::filesystem::path
(params.
outputDirectory
);
const
Image baseline =
LoadImage
((outputDirectory /
"
baseline.png
"
).
string
());
const
Image output =
LoadImage
((outputDirectory /
"
actual.png
"
).
string
());
if
(!baseline.
is_sameXYZC
(output)) {
std::cerr <<
"
Image dimensions differ: baseline=
"
<< baseline.
width
() <<
'
x
'
<< baseline.
height
() <<
'
x
'
<< baseline.
spectrum
()
<<
"
, output=
"
<< output.
width
() <<
'
x
'
<< output.
height
() <<
'
x
'
<< output.
spectrum
() << std::endl;
return
false
;
}
constexpr
double
minimumPsnr =
40.0
;
const
double
mse = baseline.
MSE
(output);
const
double
psnr = baseline.
PSNR
(output);
std::cout <<
"
Image comparison: MSE=
"
<< mse <<
"
, PSNR=
"
<< psnr <<
"
dB
"
<< std::endl;
if
(psnr >= minimumPsnr) {
return
true
;
}
const
auto
diffPath = outputDirectory /
"
diff.bmp
"
;
cimg_library::CImg<
float
>
diff
(baseline);
diff -= output;
diff.
abs
().
normalize
(
0
,
255
).
save_bmp
(diffPath.
string
().
c_str
());
std::cerr <<
"
Image comparison failed: PSNR is below
"
<< minimumPsnr <<
"
dB; diff saved to
"
<< diffPath << std::endl;
return
false
;
}
}
int
main
(
const
int
argc,
char
* argv[])
{
try
{
Sample::Test::Params params;
if
(!
Sample::Test::ParseParams
(argc, argv, params)) {
std::cerr <<
"
Usage: RenderingSample.Test --sample <path> --rhi <name> --baseline <path> --output-dir <path>
"
<< std::endl;
return
1
;
}
if
(!
Sample::Test::RunSample
(params)) {
std::cerr <<
"
Rendering sample failed
"
<< std::endl;
return
1
;
}
return
Sample::Test::CompareImages
(params) ?
0
:
1
;
}
catch
(
const
std::exception& exception) {
std::cerr << exception.
what
() << std::endl;
return
1
;
}
}
Back
|
FazBrowse Home
|
New Git URL