Skip to content

Commit ecdb7a2

Browse files
committed
Improve SCOPE_EXIT functionality
1 parent d41e38c commit ecdb7a2

12 files changed

+305
-250
lines changed

internal/ScopeExit.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
#pragma once
22

3+
#include <utility>
4+
35
namespace Ray {
4-
template <class F> class AtScopeExit {
5-
F &func_;
6+
template <typename F> class AtScopeExit {
7+
F func_;
8+
bool engaged_ = false;
69

710
public:
8-
explicit AtScopeExit(F &func) : func_(func) {}
9-
~AtScopeExit() { func_(); }
11+
explicit AtScopeExit(F func) : func_(std::move(func)), engaged_(true) {}
12+
AtScopeExit(AtScopeExit &&rhs) : func_(std::move(rhs.func_)), engaged_(rhs.engaged_) { rhs.Dismiss(); }
13+
~AtScopeExit() { Execute(); }
14+
15+
AtScopeExit(const AtScopeExit &) = delete;
16+
AtScopeExit &operator=(const AtScopeExit &) = delete;
17+
AtScopeExit &operator=(AtScopeExit &&) = delete;
18+
19+
inline void Dismiss() { engaged_ = false; }
20+
21+
inline void Execute() {
22+
if (engaged_) {
23+
func_();
24+
}
25+
Dismiss();
26+
}
1027
};
11-
}
28+
template <typename F> AtScopeExit<F> make_scope_exit(F &&f) { return AtScopeExit<F>(std::forward<F>(f)); }
29+
} // namespace Ray
1230

13-
#define SCOPE_EXIT_INTERNAL2(lname, aname, ...) \
14-
auto lname = [&]() { __VA_ARGS__; }; \
15-
Ray::AtScopeExit<decltype(lname)> aname(lname);
31+
#define SCOPE_EXIT_INTERNAL2(aname, ...) auto aname = Ray::make_scope_exit([&]() { __VA_ARGS__; });
1632

17-
#define SCOPE_EXIT_CONCAT(x, y) SCOPE_EXIT_ ## x ## y
33+
#define SCOPE_EXIT_CONCAT(x, y) SCOPE_EXIT_##x##y
1834

19-
#define SCOPE_EXIT_INTERNAL1(ctr, ...) \
20-
SCOPE_EXIT_INTERNAL2(SCOPE_EXIT_CONCAT(func_, ctr), \
21-
SCOPE_EXIT_CONCAT(instance_, ctr), __VA_ARGS__)
35+
#define SCOPE_EXIT_INTERNAL1(ctr, ...) SCOPE_EXIT_INTERNAL2(SCOPE_EXIT_CONCAT(instance_, ctr), __VA_ARGS__)
2236

2337
#define SCOPE_EXIT(...) SCOPE_EXIT_INTERNAL1(__COUNTER__, __VA_ARGS__)

tests/test_aux_channels.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern int g_validation_level;
1818

1919
void test_aux_channels(const char *arch_list[], const char *preferred_device) {
2020
using namespace std::chrono;
21+
using namespace Ray;
2122

2223
const char TestName[] = "aux_channels";
2324

@@ -43,21 +44,21 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
4344
// Setup scene
4445
//
4546

46-
Ray::principled_mat_desc_t mat_desc;
47-
mat_desc.base_texture = Ray::TextureHandle{0};
47+
principled_mat_desc_t mat_desc;
48+
mat_desc.base_texture = TextureHandle{0};
4849
mat_desc.roughness = 1.0f;
49-
mat_desc.roughness_texture = Ray::TextureHandle{2};
50+
mat_desc.roughness_texture = TextureHandle{2};
5051
mat_desc.metallic = 1.0f;
51-
mat_desc.metallic_texture = Ray::TextureHandle{3};
52-
mat_desc.normal_map = Ray::TextureHandle{1};
53-
mat_desc.alpha_texture = Ray::TextureHandle{4};
52+
mat_desc.metallic_texture = TextureHandle{3};
53+
mat_desc.normal_map = TextureHandle{1};
54+
mat_desc.alpha_texture = TextureHandle{4};
5455

5556
const char *textures[] = {
5657
"test_data/textures/Fence007A_2K_Color.tga", "test_data/textures/Fence007A_2K_NormalGL.tga",
5758
"test_data/textures/Fence007A_2K_Roughness.tga", "test_data/textures/Fence007A_2K_Metalness.tga",
5859
"test_data/textures/Fence007A_2K_Opacity.tga"};
5960

60-
Ray::settings_t s;
61+
settings_t s;
6162
s.w = test_img_w;
6263
s.h = test_img_h;
6364
s.preferred_device = preferred_device;
@@ -69,7 +70,7 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
6970
const double BaseColor_MinPSNR = 28.44, Normals_MinPSNR = 38.34, Depth_MinPSNR = 43.3;
7071

7172
for (const char **arch = arch_list; *arch; ++arch) {
72-
const auto rt = Ray::RendererTypeFromName(*arch);
73+
const auto rt = RendererTypeFromName(*arch);
7374

7475
for (const bool use_hwrt : {false, true}) {
7576
if (use_hwrt && g_nohwrt) {
@@ -81,24 +82,23 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
8182
const auto start_time = high_resolution_clock::now();
8283

8384
using namespace std::placeholders;
84-
auto parallel_for =
85-
std::bind(&ThreadPool::ParallelFor<Ray::ParallelForFunction>, std::ref(threads), _1, _2, _3);
85+
auto parallel_for = std::bind(&ThreadPool::ParallelFor<ParallelForFunction>, std::ref(threads), _1, _2, _3);
8686

87-
auto renderer = std::unique_ptr<Ray::RendererBase>(Ray::CreateRenderer(s, &g_log_err, parallel_for, rt));
87+
auto renderer = std::unique_ptr<RendererBase>(CreateRenderer(s, &g_log_err, parallel_for, rt));
8888
if (!renderer || renderer->type() != rt || renderer->is_hwrt() != use_hwrt) {
8989
// skip unsupported (we fell back to some other renderer)
9090
continue;
9191
}
9292
if (preferred_device) {
9393
// make sure we use requested device
94-
if (!require(Ray::MatchDeviceNames(renderer->device_name(), preferred_device))) {
94+
if (!require(MatchDeviceNames(renderer->device_name(), preferred_device))) {
9595
std::lock_guard<std::mutex> _(g_stdout_mtx);
9696
printf("Wrong device: %s (%s was requested)\n", renderer->device_name(), preferred_device);
9797
return;
9898
}
9999
}
100100

101-
auto scene = std::unique_ptr<Ray::SceneBase>(renderer->CreateScene());
101+
auto scene = std::unique_ptr<SceneBase>(renderer->CreateScene());
102102

103103
setup_test_scene(threads, *scene, -1, 0.0f, mat_desc, textures, eTestScene::Standard);
104104

@@ -107,8 +107,8 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
107107
schedule_render_jobs(threads, *renderer, scene.get(), s, SampleCount, eDenoiseMethod::None, false,
108108
name_buf);
109109

110-
const auto base_color_pixels = renderer->get_aux_pixels_ref(Ray::eAUXBuffer::BaseColor);
111-
const auto depth_normals_pixels = renderer->get_aux_pixels_ref(Ray::eAUXBuffer::DepthNormals);
110+
const auto base_color_pixels = renderer->get_aux_pixels_ref(eAUXBuffer::BaseColor);
111+
const auto depth_normals_pixels = renderer->get_aux_pixels_ref(eAUXBuffer::DepthNormals);
112112

113113
std::unique_ptr<uint8_t[]> base_color_data_u8(new uint8_t[test_img_w * test_img_h * 3]);
114114
std::unique_ptr<uint8_t[]> normals_data_u8(new uint8_t[test_img_w * test_img_h * 3]);
@@ -119,7 +119,7 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
119119
for (int j = 0; j < test_img_h; j++) {
120120
for (int i = 0; i < test_img_w; i++) {
121121
{ // check base color
122-
Ray::color_rgba_t c = base_color_pixels.ptr[j * base_color_pixels.pitch + i];
122+
color_rgba_t c = base_color_pixels.ptr[j * base_color_pixels.pitch + i];
123123

124124
for (int k = 0; k < 3; ++k) {
125125
if (c.v[k] < 0.0031308f) {
@@ -146,7 +146,7 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
146146
base_color_mse += diff_b * diff_b;
147147
}
148148
{ // check normals
149-
const Ray::color_rgba_t &n = depth_normals_pixels.ptr[j * depth_normals_pixels.pitch + i];
149+
const color_rgba_t &n = depth_normals_pixels.ptr[j * depth_normals_pixels.pitch + i];
150150

151151
const auto r = uint8_t((n.v[0] * 0.5f + 0.5f) * 255);
152152
const auto g = uint8_t((n.v[1] * 0.5f + 0.5f) * 255);
@@ -165,7 +165,7 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
165165
normals_mse += diff_b * diff_b;
166166
}
167167
{ // check depth
168-
const Ray::color_rgba_t &n = depth_normals_pixels.ptr[j * depth_normals_pixels.pitch + i];
168+
const color_rgba_t &n = depth_normals_pixels.ptr[j * depth_normals_pixels.pitch + i];
169169

170170
const auto u8 = uint8_t(n.v[3] * 255);
171171

@@ -197,25 +197,25 @@ void test_aux_channels(const char *arch_list[], const char *preferred_device) {
197197
{
198198
std::lock_guard<std::mutex> _(g_stdout_mtx);
199199
if (g_minimal_output) {
200-
printf("\r%s (%6s, %s): %.1f%% ", name_buf, Ray::RendererTypeName(rt), s.use_hwrt ? "HWRT" : "SWRT",
200+
printf("\r%s (%6s, %s): %.1f%% ", name_buf, RendererTypeName(rt), s.use_hwrt ? "HWRT" : "SWRT",
201201
100.0);
202202
}
203203
printf("(PSNR: %.2f/%.2f dB, %.2f/%.2f dB, %.2f/%.2f dB, Time: %.2fm)\n", base_color_psnr,
204204
BaseColor_MinPSNR, normals_psnr, Normals_MinPSNR, depth_psnr, Depth_MinPSNR, test_duration_m);
205205
fflush(stdout);
206206
}
207207

208-
std::string type_name = Ray::RendererTypeName(rt);
208+
std::string type_name = RendererTypeName(rt);
209209
if (use_hwrt) {
210210
type_name += "_HWRT";
211211
}
212212

213213
snprintf(name_buf, sizeof(name_buf), "test_data/%s/%s_base_color_out.tga", TestName, type_name.c_str());
214-
Ray::WriteTGA(&base_color_data_u8[0], test_img_w, test_img_h, 3, name_buf);
214+
WriteTGA(&base_color_data_u8[0], test_img_w, test_img_h, 3, name_buf);
215215
snprintf(name_buf, sizeof(name_buf), "test_data/%s/%s_normals_out.tga", TestName, type_name.c_str());
216-
Ray::WriteTGA(&normals_data_u8[0], test_img_w, test_img_h, 3, name_buf);
216+
WriteTGA(&normals_data_u8[0], test_img_w, test_img_h, 3, name_buf);
217217
snprintf(name_buf, sizeof(name_buf), "test_data/%s/%s_depth_out.tga", TestName, type_name.c_str());
218-
Ray::WriteTGA(&depth_data_u8[0], test_img_w, test_img_h, 3, name_buf);
218+
WriteTGA(&depth_data_u8[0], test_img_w, test_img_h, 3, name_buf);
219219

220220
require(base_color_psnr >= BaseColor_MinPSNR);
221221
require(normals_psnr >= Normals_MinPSNR);

tests/test_freelist_alloc.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
#include "../internal/FreelistAlloc.h"
44

55
void test_freelist_alloc() {
6+
using namespace Ray;
7+
68
printf("Test freelist_alloc | ");
79

810
{ // basic usage
9-
Ray::FreelistAlloc alloc;
11+
FreelistAlloc alloc;
1012
require(alloc.IntegrityCheck());
1113

1214
const uint16_t pool = alloc.AddPool(2048);
1315
require(pool == 0);
1416
require(alloc.IntegrityCheck());
1517

16-
const Ray::FreelistAlloc::Allocation a = alloc.Alloc(128);
18+
const FreelistAlloc::Allocation a = alloc.Alloc(128);
1719
require(a.offset == 0);
1820
require(a.pool == 0);
1921
require(alloc.IntegrityCheck());
2022

2123
alloc.Free(a.block);
2224
require(alloc.IntegrityCheck());
2325
}
24-
2526
{ // block merging 1
26-
Ray::FreelistAlloc alloc(2048);
27+
FreelistAlloc alloc(2048);
2728
require(alloc.IntegrityCheck());
2829

2930
const auto a = alloc.Alloc(1);
@@ -51,9 +52,8 @@ void test_freelist_alloc() {
5152
alloc.Free(d.block);
5253
require(alloc.IntegrityCheck());
5354
}
54-
5555
{ // block merging 2
56-
Ray::FreelistAlloc alloc(2048);
56+
FreelistAlloc alloc(2048);
5757
require(alloc.IntegrityCheck());
5858

5959
const auto a = alloc.Alloc(123);
@@ -74,9 +74,8 @@ void test_freelist_alloc() {
7474
alloc.Free(d.block);
7575
require(alloc.IntegrityCheck());
7676
}
77-
7877
{ // reuse 1
79-
Ray::FreelistAlloc alloc(8192);
78+
FreelistAlloc alloc(8192);
8079
require(alloc.IntegrityCheck());
8180

8281
const auto a = alloc.Alloc(1024);
@@ -105,9 +104,8 @@ void test_freelist_alloc() {
105104
alloc.Free(d.block);
106105
require(alloc.IntegrityCheck());
107106
}
108-
109107
{ // reuse 2
110-
Ray::FreelistAlloc alloc(8192);
108+
FreelistAlloc alloc(8192);
111109
require(alloc.IntegrityCheck());
112110

113111
const auto a = alloc.Alloc(1024);
@@ -146,9 +144,8 @@ void test_freelist_alloc() {
146144
require(f.offset == 0);
147145
alloc.Free(f.block);
148146
}
149-
150147
{ // multiple pools
151-
Ray::FreelistAlloc alloc;
148+
FreelistAlloc alloc;
152149

153150
const uint16_t pool1 = alloc.AddPool(1024);
154151
require(pool1 == 0);
@@ -183,11 +180,10 @@ void test_freelist_alloc() {
183180
alloc.Free(d.block);
184181
require(alloc.IntegrityCheck());
185182
}
186-
187183
{ // fragmentation
188-
Ray::FreelistAlloc alloc(256 * 1024 * 1024);
184+
FreelistAlloc alloc(256 * 1024 * 1024);
189185

190-
Ray::FreelistAlloc::Allocation allocations[256];
186+
FreelistAlloc::Allocation allocations[256];
191187
for (int i = 0; i < 256; ++i) {
192188
allocations[i] = alloc.Alloc(1 * 1024 * 1024);
193189
require(allocations[i].offset == i * 1024 * 1024);
@@ -233,11 +229,10 @@ void test_freelist_alloc() {
233229
alloc.Free(a.block);
234230
require(alloc.IntegrityCheck());
235231
}
236-
237232
{ // resize pool
238-
Ray::FreelistAlloc alloc(256 * 1024);
233+
FreelistAlloc alloc(256 * 1024);
239234

240-
Ray::FreelistAlloc::Allocation allocations[512];
235+
FreelistAlloc::Allocation allocations[512];
241236
for (int i = 0; i < 256; ++i) {
242237
allocations[i] = alloc.Alloc(1 * 1024);
243238
require(allocations[i].offset == i * 1024);
@@ -256,11 +251,10 @@ void test_freelist_alloc() {
256251
require(alloc.IntegrityCheck());
257252
}
258253
}
259-
260254
{ // block iteration
261-
Ray::FreelistAlloc alloc(256 * 1024);
255+
FreelistAlloc alloc(256 * 1024);
262256

263-
Ray::FreelistAlloc::Allocation allocations[256];
257+
FreelistAlloc::Allocation allocations[256];
264258
for (int i = 0; i < 256; ++i) {
265259
allocations[i] = alloc.Alloc(1 * 1024);
266260
require(allocations[i].offset == i * 1024);
@@ -272,7 +266,7 @@ void test_freelist_alloc() {
272266
require(alloc.IntegrityCheck());
273267
}
274268

275-
Ray::FreelistAlloc::Range r = alloc.GetFirstOccupiedBlock(0);
269+
FreelistAlloc::Range r = alloc.GetFirstOccupiedBlock(0);
276270
int i = 1;
277271
require(r.offset == allocations[i].offset);
278272
while (r.size) {

tests/test_hashmap.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include "../internal/HashMap32.h"
66

77
void test_hashmap() {
8+
using namespace Ray;
9+
810
printf("Test hashmap | ");
911

1012
{ // Basic test
11-
Ray::HashMap32<int, double> cont;
13+
HashMap32<int, double> cont;
1214

1315
for (int i = 0; i < 100; i++) {
1416
require(cont.Insert(12, 12.0));
@@ -62,9 +64,8 @@ void test_hashmap() {
6264
require(cont.Erase(144));
6365
}
6466
}
65-
6667
{ // Test with reallocation
67-
Ray::HashMap32<std::string, int> cont(16);
68+
HashMap32<std::string, int> cont(16);
6869

6970
for (int i = 0; i < 100000; i++) {
7071
std::string key = std::to_string(i);
@@ -81,9 +82,8 @@ void test_hashmap() {
8182

8283
require(cont.size() == 0);
8384
}
84-
8585
{ // Test iteration
86-
Ray::HashMap32<std::string, int> cont(16);
86+
HashMap32<std::string, int> cont(16);
8787

8888
for (int i = 0; i < 100000; i++) {
8989
std::string key = std::to_string(i);

tests/test_huffman.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ void traverse(Ray::Span<const Ray::huff_node_t> nodes, uint32_t i, int level, st
2222
}
2323

2424
void test_huffman() {
25+
using namespace Ray;
26+
2527
printf("Test huffman | ");
2628

2729
const char test_str[] = "aaaaaaaaaaaaaaaaaaaaaaaaaabbdcccsdfasdfasdfwcsddddccccd";
@@ -31,8 +33,8 @@ void test_huffman() {
3133
freq[uint8_t(test_str[i])]++;
3234
}
3335

34-
std::vector<Ray::huff_node_t> nodes;
35-
const uint32_t root = Ray::huff_build_tree(test_str, freq, nodes);
36+
std::vector<huff_node_t> nodes;
37+
const uint32_t root = huff_build_tree(test_str, freq, nodes);
3638
require(nodes[root].freq == 55);
3739

3840
char temp_code[8];

0 commit comments

Comments
 (0)