-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_pixels.cpp
72 lines (64 loc) · 2.47 KB
/
active_pixels.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
#include "active_pixels.h"
#include "test_utils.h"
#include "thrust_utils.h"
#include <thrust/execution_policy.h>
#include <thrust/sequence.h>
#include <thrust/remove.h>
struct is_invalid_ray {
DEVICE bool operator()(int idx) {
return is_zero(rays[idx].dir);
}
const Ray *rays = nullptr;
};
void init_active_pixels(const BufferView<Ray> &rays,
BufferView<int> active_pixels,
bool use_gpu) {
assert(rays.size() == active_pixels.size());
DISPATCH(use_gpu, thrust::sequence, active_pixels.begin(), active_pixels.end());
auto op = is_invalid_ray{rays.begin()};
DISPATCH(use_gpu, thrust::remove_if,
active_pixels.begin(), active_pixels.end(),
active_pixels.begin(), op);
}
struct is_valid_intersection {
DEVICE bool operator()(int pixel_id) {
return isects[pixel_id].valid();
}
const Intersection *isects = nullptr;
};
void update_active_pixels(const BufferView<int> &active_pixels,
const BufferView<Intersection> &isects,
BufferView<int> &new_active_pixels,
bool use_gpu) {
auto op = is_valid_intersection{isects.begin()};
auto new_end = DISPATCH(use_gpu, thrust::copy_if,
active_pixels.begin(), active_pixels.end(),
new_active_pixels.begin(), op);
new_active_pixels.count = new_end - new_active_pixels.begin();
}
void test_active_pixels(bool use_gpu) {
auto num_pixels = 1024;
auto rays_buffer = Buffer<Ray>(use_gpu, num_pixels);
auto rays = rays_buffer.view(0, num_pixels);
for (int i = 0; i < num_pixels; i++) {
rays[i] = Ray{Vector3{0, 0, 0}, Vector3{0, 0, 1}};
}
auto active_pixels_buffer = Buffer<int>(use_gpu, num_pixels);
auto active_pixels = active_pixels_buffer.view(0, num_pixels);
init_active_pixels(rays, active_pixels, use_gpu);
equal_or_error(__FILE__, __LINE__, num_pixels, active_pixels.size());
auto isects_buffer = Buffer<Intersection>(use_gpu, num_pixels);
auto isects = isects_buffer.view(0, num_pixels);
for (int i = 0; i < num_pixels; i++) {
if (i % 2 == 0) {
isects[i] = Intersection{0, 0};
} else {
isects[i] = Intersection{-1, -1};
}
}
update_active_pixels(active_pixels,
isects,
active_pixels,
use_gpu);
equal_or_error(__FILE__, __LINE__, num_pixels / 2, active_pixels.size());
}