Skip to content

Commit

Permalink
change random generator
Browse files Browse the repository at this point in the history
  • Loading branch information
netwarm007 committed Jan 4, 2023
1 parent cf392fd commit 282c665
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Framework/Algorism/BVH.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SimpleBVHNode : _implements_ Hitable<T> {
public:
__device__ SimpleBVHNode() : Hitable<T>(HitableType::kBVH) {}
__device__ SimpleBVHNode(Hitable<T>** list, int start, int end,
curandState* local_rand_state)
curandStateMRG32k3a_t* local_rand_state)
: Hitable<T>(HitableType::kBVH) {
struct stack_element_type {
SimpleBVHNode* root;
Expand Down
20 changes: 10 additions & 10 deletions Framework/GeomMath/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ namespace My {
#ifdef __CUDACC__

template <class T>
__device__ T random_f(curandState *local_rand_state) {
__device__ T random_f(curandStateMRG32k3a_t *local_rand_state) {
return curand_uniform(local_rand_state);
}

template <class T>
__device__ T random_f(T min, T max, curandState *local_rand_state) {
__device__ T random_f(T min, T max, curandStateMRG32k3a_t *local_rand_state) {
T scale = max - min;
return min + scale * curand_uniform(local_rand_state);
}

template <class T> requires std::integral<T>
__device__ T random_int(T min, T max, curandState *local_rand_state) {
__device__ T random_int(T min, T max, curandStateMRG32k3a_t *local_rand_state) {
return static_cast<T>(random_f<T>(static_cast<T>(min), static_cast<T>(max), local_rand_state));
}

template <class T, Dimension auto N>
__device__ Vector<T, N> random_v(curandState *local_rand_state) {
__device__ Vector<T, N> random_v(curandStateMRG32k3a_t *local_rand_state) {
auto vec = Vector<T, N>();
for (int i = 0; i < N; i++) {
vec[i] = random_f<T>(local_rand_state);
Expand All @@ -38,7 +38,7 @@ __device__ Vector<T, N> random_v(curandState *local_rand_state) {
}

template <class T, Dimension auto N>
__device__ Vector<T, N> random_v(T min, T max, curandState *local_rand_state) {
__device__ Vector<T, N> random_v(T min, T max, curandStateMRG32k3a_t *local_rand_state) {
auto vec = Vector<T, N>();
for (int i = 0; i < N; i++) {
vec[i] = random_f<T>(min, max, local_rand_state);
Expand All @@ -48,7 +48,7 @@ __device__ Vector<T, N> random_v(T min, T max, curandState *local_rand_state) {
}

template <class T, Dimension auto N>
__device__ Vector<T, N> random_in_unit_sphere(curandState *local_rand_state) {
__device__ Vector<T, N> random_in_unit_sphere(curandStateMRG32k3a_t *local_rand_state) {
while (true) {
auto p = random_v<T, N>(T(-1), T(1), local_rand_state);
if (LengthSquared(p) >= 1) continue;
Expand All @@ -57,14 +57,14 @@ __device__ Vector<T, N> random_in_unit_sphere(curandState *local_rand_state) {
}

template <class T, Dimension auto N>
__device__ Vector<T, N> random_unit_vector(curandState *local_rand_state) {
__device__ Vector<T, N> random_unit_vector(curandStateMRG32k3a_t *local_rand_state) {
auto p = random_in_unit_sphere<T, N>(local_rand_state);
Normalize(p);
return p;
}

template <class T, Dimension auto N>
__device__ Vector<T, N> random_in_hemisphere(const Vector<T, N>& normal, curandState *local_rand_state) {
__device__ Vector<T, N> random_in_hemisphere(const Vector<T, N>& normal, curandStateMRG32k3a_t *local_rand_state) {
auto p = random_in_unit_sphere<T, N>(local_rand_state);
T result;
DotProduct<T, N>(result, p, normal);
Expand All @@ -76,7 +76,7 @@ __device__ Vector<T, N> random_in_hemisphere(const Vector<T, N>& normal, curandS
}

template <class T>
__device__ Vector3<T> random_in_hemisphere_cosine_weighted(const Vector3<T>& normal, curandState *local_rand_state) {
__device__ Vector3<T> random_in_hemisphere_cosine_weighted(const Vector3<T>& normal, curandStateMRG32k3a_t *local_rand_state) {
auto uv = random_v<T, 2>(local_rand_state);
T phi = 2.0 * PI * uv[0];

Expand All @@ -90,7 +90,7 @@ __device__ Vector3<T> random_in_hemisphere_cosine_weighted(const Vector3<T>& nor
}

template <class T>
__device__ Vector3<T> random_in_unit_disk(curandState *local_rand_state) {
__device__ Vector3<T> random_in_unit_disk(curandStateMRG32k3a_t *local_rand_state) {
while (true) {
auto p = Vector3<T>({random_f(T(-1.0), T(1.0), local_rand_state), random_f(T(-1.0), T(1.0), local_rand_state), 0});
if (LengthSquared(p) >= (T)1.0) continue;
Expand Down
Binary file modified Test/RayTracingCUDATest.cu
Binary file not shown.
2 changes: 1 addition & 1 deletion Test/RayTracingCamera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RayTracingCamera {
}

#ifdef __CUDACC__
__device__ Ray<T> get_ray(T s, T t, curandState *local_rand_state) const {
__device__ Ray<T> get_ray(T s, T t, curandStateMRG32k3a_t *local_rand_state) const {
Vector3<T> rd = lens_radius * random_in_unit_disk<T>(local_rand_state);
#else
Ray<T> get_ray(T s, T t) const {
Expand Down
8 changes: 4 additions & 4 deletions Test/TestMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class material {
public:
#ifdef __CUDACC__
__device__ virtual bool scatter(const ray& r_in, const hit_record& hit,
color& attenuation, ray& scattered, curandState *local_rand_state) const = 0;
color& attenuation, ray& scattered, curandStateMRG32k3a_t *local_rand_state) const = 0;
#else
virtual bool scatter(const ray& r_in, const hit_record& hit,
color& attenuation, ray& scattered) const = 0;
Expand All @@ -32,7 +32,7 @@ class lambertian : public material {

#ifdef __CUDACC__
__device__ bool scatter(const ray& r_in, const hit_record& hit, color& attenuation,
ray& scattered, curandState *local_rand_state) const override {
ray& scattered, curandStateMRG32k3a_t *local_rand_state) const override {
auto scatter_direction =
hit.getNormal() + My::random_unit_vector<float_precision, 3>(local_rand_state);
#else
Expand Down Expand Up @@ -61,7 +61,7 @@ class metal : public material {

#ifdef __CUDACC__
__device__ bool scatter(const ray& r_in, const hit_record& hit, color& attenuation,
ray& scattered, curandState *local_rand_state) const override {
ray& scattered, curandStateMRG32k3a_t *local_rand_state) const override {
vec3 reflected = My::Reflect(r_in.getDirection(), hit.getNormal());
scattered = ray(
hit.getP(),
Expand Down Expand Up @@ -90,7 +90,7 @@ class dielectric : public material {

#ifdef __CUDACC__
__device__ bool scatter(const ray& r_in, const hit_record& hit, color& attenuation,
ray& scattered, curandState *local_rand_state) const override {
ray& scattered, curandStateMRG32k3a_t *local_rand_state) const override {
#else
bool scatter(const ray& r_in, const hit_record& hit, color& attenuation,
ray& scattered) const override {
Expand Down

0 comments on commit 282c665

Please sign in to comment.