Skip to content

Commit b92d9c2

Browse files
author
Luc Forget
committed
FPGA friendly local random seed generator
1 parent 70c34f9 commit b92d9c2

4 files changed

Lines changed: 43 additions & 15 deletions

File tree

include/constant_medium.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class constant_medium {
6060
/// Distance between the two hitpoints affect of probability
6161
/// of the ray hitting a smoke particle
6262
const auto distance_inside_boundary = (rec2.t - rec1.t) * ray_length;
63-
auto rng = LocalPseudoRNG { toseed(r.direction()) ^ toseed(r.origin()) };
63+
auto rng = LocalPseudoRNG { toseed(r.direction(), r.origin()) };
6464
const auto hit_distance = neg_inv_density * sycl::log(rng.real());
6565

6666
/// With lower density, hit_distance has higher probabilty

include/material.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct lambertian_material {
1717

1818
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
1919
ray& scattered) const {
20-
LocalPseudoRNG rng { toseed(r_in.direction()) ^ toseed(r_in.origin()) };
20+
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
2121
vec scatter_direction = rec.normal + rng.unit_vec();
2222
scattered = ray(rec.p, scatter_direction, r_in.time());
2323
// Attenuation of the ray hitting the object is modified based on the color
@@ -37,7 +37,7 @@ struct metal_material {
3737

3838
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
3939
ray& scattered) const {
40-
LocalPseudoRNG rng { toseed(r_in.direction()) ^ toseed(r_in.origin()) };
40+
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
4141
vec reflected = reflect(unit_vector(r_in.direction()), rec.normal);
4242
scattered = ray(rec.p, reflected + fuzz * rng.in_unit_ball(), r_in.time());
4343
// Attenuation of the ray hitting the object is modified based on the color
@@ -68,7 +68,7 @@ struct dielectric_material {
6868
ray& scattered) const {
6969
// Attenuation of the ray hitting the object is modified based on the color
7070
// at hit point
71-
LocalPseudoRNG rng { toseed(r_in.direction()) ^ toseed(r_in.origin()) };
71+
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
7272
attenuation *= albedo;
7373
real_t refraction_ratio = rec.front_face ? (1.0f / ref_idx) : ref_idx;
7474
vec unit_direction = unit_vector(r_in.direction());
@@ -116,7 +116,7 @@ struct isotropic_material {
116116

117117
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation,
118118
ray& scattered) const {
119-
LocalPseudoRNG rng { toseed(r_in.direction()) ^ toseed(r_in.origin()) };
119+
LocalPseudoRNG rng { toseed(r_in.direction(), r_in.origin()) };
120120
scattered = ray(rec.p, rng.in_unit_ball(), r_in.time());
121121
attenuation *= dev_visit([&](auto&& t) { return t.value(rec); }, albedo);
122122
return true;

include/rtweekend.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <algorithm>
55
#include <cmath>
6+
#include <cstdint>
67
#include <cstdlib>
78
#include <cstring>
89
#include <limits>
@@ -45,6 +46,23 @@ uint32_t toseed(vec const& val) {
4546
return x * x * y * y * z * z;
4647
}
4748

49+
uint32_t toseed(vec const& val1, vec const& val2) {
50+
uint32_t x1, y1, z1, x2, y2, z2;
51+
std::memcpy(&x1, &val1.x(), sizeof(uint32_t));
52+
std::memcpy(&y1, &val1.y(), sizeof(uint32_t));
53+
std::memcpy(&z1, &val1.z(), sizeof(uint32_t));
54+
std::memcpy(&x2, &val2.x(), sizeof(uint32_t));
55+
std::memcpy(&y2, &val2.y(), sizeof(uint32_t));
56+
std::memcpy(&z2, &val2.z(), sizeof(uint32_t));
57+
uint32_t shifted1 = x1 << 26;
58+
uint32_t shifted2 = (x2 & 63) << 21;
59+
uint32_t shifted3 = (y1 & 63) << 15;
60+
uint32_t shifted4 = (y2 & 63) << 10;
61+
uint32_t shifted5 = (z1 & 63) << 5;
62+
uint32_t shifted6 = (z2 & 63);
63+
return shifted1 ^ shifted2 ^ shifted3 ^ shifted4 ^ shifted5 ^ shifted6;
64+
}
65+
4866
class LocalPseudoRNG {
4967
public:
5068
inline LocalPseudoRNG(std::uint32_t init_state = xorshift<>::initial_state)

src/main.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <algorithm>
33
#include <chrono>
44
#include <cstdint>
5+
#include <ctime>
56
#include <iostream>
67
#include <iterator>
78
#include <math.h>
@@ -79,17 +80,29 @@ int main(int argc, char* argv[]) {
7980
hittables.emplace_back(sphere(point { 0, -1000, 0 }, 1000, m));
8081
t = checker_texture(color { 0.9f, 0.9f, 0.9f }, color { 0.4f, 0.2f, 0.1f });
8182

82-
LocalPseudoRNG rng;
83+
LocalPseudoRNG rng{static_cast<uint32_t>(std::time(nullptr))};
8384

84-
for (int a = -11; a < 11; a += 3) {
85-
for (int b = -11; b < 11; b += 3) {
85+
for (int a = -11; a < 11; a++) {
86+
for (int b = -11; b < 11; b++) {
87+
auto choose_mat = rng.real();
8688
// Spheres are placed at a point randomly displaced from a,b
8789
point center(a + 0.9f * rng.real(), 0.2f, b + 0.9f * rng.real());
88-
if (sycl::length((center - point(4, 0.2f, 0))) > 0.9f) {
90+
if (choose_mat < 0.70f) {
8991
// Lambertian
9092
auto albedo = rng.vec_t() * rng.vec_t();
9193
hittables.emplace_back(
9294
sphere(center, 0.2f, lambertian_material(albedo)));
95+
} else if (choose_mat < 0.95f) {
96+
// metal
97+
auto albedo = rng.vec_t(0.5f, 1);
98+
auto fuzz = rng.real(0, 0.5f);
99+
hittables.emplace_back(
100+
sphere(center, 0.2f, metal_material(albedo, fuzz)));
101+
} else {
102+
// glass
103+
hittables.emplace_back(
104+
sphere(center, 0.2f,
105+
dielectric_material(1.5f, color { 1.0f, 1.0f, 1.0f })));
93106
}
94107
}
95108
}
@@ -127,23 +140,20 @@ int main(int argc, char* argv[]) {
127140
hittables.emplace_back(sphere(point { 0, 1, -2.25f }, 1,
128141
metal_material(color(0.7f, 0.6f, 0.5f), 0.0f)));
129142

130-
// t = image_texture::image_texture_factory("../images/SYCL.png", 5);
131-
132-
// // Add a sphere with a SYCL logo in the background
133143
hittables.emplace_back(
134144
sphere { point { -60, 3, 5 }, 4, lambertian_material { t } });
135145

136146
// Add a metallic monolith
137147
hittables.emplace_back(
138148
box { point { 6.5f, 0, -1.5f }, point { 7.0f, 3.0f, -1.0f },
139-
metal_material { color { 0.7f, 0.6f, 0.5f }, 0.25f } });/**/
149+
metal_material { color { 0.7f, 0.6f, 0.5f }, 0.25f } });
140150

141151
// Add a smoke ball
142152
sphere smoke_sphere =
143153
sphere { point { 5, 1, 3.5f }, 1,
144154
lambertian_material { color { 0.75f, 0.75f, 0.75f } } };
145-
/*hittables.emplace_back(
146-
constant_medium { smoke_sphere, 1, color { 1, 1, 1 } });*/
155+
hittables.emplace_back(
156+
constant_medium { smoke_sphere, 1, color { 1, 1, 1 } });
147157

148158
// SYCL queue
149159
sycl::queue myQueue;

0 commit comments

Comments
 (0)