<p>I managed to fix it. It was indeed related to repeating random sequences.
The problem was the following. <code>curandState* randState</code> is an array of <code>curandState</code>s, and most calls to <code>curand_*</code> require a pointer of a <code>curandState</code>.
I was sending the <code>randState</code>, the array, to <code>curand_*</code> functions and not for example, a pointer of one of its members.
Now most of my images are like this: <a href="https://i.sstatic.net/asZM7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/asZM7.png" alt="current image output" /></a></p>

CC BY-SA 4.0
2020-10-08T02:30:57.013Z
2020-10-08T02:30:57.013Z
Kaan E.
<p>I am writting a path tracer in <a href="https://github.com/D-K-E/cuda-raytracer.git" rel="nofollow noreferrer">cuda</a> following the Shirley's book. I am having a square pattern as an output.</p>
<p>The size of the block change if I change the block dimensions. For this image, the block dimensions follow the aspect ratio, basically 16 * 9 is block size. I know that without actually showing the entire code, it is difficult to debug it. I am just asking if you have any idea where I should start debugging it. My hunch is that there is a problem with my random number generator.
Here is its code in case I am doing something wrong. Here is the host code:</p>
<pre class="lang-cpp prettyprint-override"><code>int total_pixel_size = WIDTH * HEIGHT;
size_t frameSize = 3 * total_pixel_size;

// declare random state
int SEED = time(NULL);
thrust::device_ptr&lt;curandState&gt; randState1 = thrust::device_malloc&lt;curandState&gt;(frameSize);
CUDA_CONTROL(cudaGetLastError());

// declare random state 2
thrust::device_ptr&lt;curandState&gt; randState2 = thrust::device_malloc&lt;curandState&gt;(1);
CUDA_CONTROL(cudaGetLastError());
rand_init&lt;&lt;&lt;1, 1&gt;&gt;&gt;(thrust::raw_pointer_cast(randState2),
                      SEED);
CUDA_CONTROL(cudaGetLastError());
CUDA_CONTROL(cudaDeviceSynchronize());

dim3 blocks(WIDTH / BLOCK_WIDTH + 1,
              HEIGHT / BLOCK_HEIGHT + 1);
  dim3 threads(BLOCK_WIDTH, BLOCK_HEIGHT);
  render_init&lt;&lt;&lt;blocks, threads&gt;&gt;&gt;(
      WIDTH, HEIGHT, thrust::raw_pointer_cast(randState1),
      SEED);
</code></pre>
<p>Here is the device code:</p>
<pre class="lang-cpp prettyprint-override"><code>
__global__ void rand_init(curandState *randState,
                          int seed) {
  if (threadIdx.x == 0 &amp;&amp; blockIdx.x == 0) {
    curand_init(seed, 0, 0, randState);
  }
}

__global__ void render_init(int mx, int my,
                            curandState *randState,
                            int seed) {
  if (threadIdx.x == 0 &amp;&amp; threadIdx.y == 0) {
    curand_init(seed, 0, 0, randState);
  }
  int i = threadIdx.x + blockIdx.x * blockDim.x;
  int j = threadIdx.y + blockIdx.y * blockDim.y;

  if ((i &gt;= mx) || (j &gt;= my)) {
    return;
  }
  int pixel_index = j * mx + i;
  // same seed, different index
  curand_init(seed + pixel_index, pixel_index, 0,
              &amp;randState[pixel_index]);
}
</code></pre>
<p>I would really appreciate any pointers to the problem.</p>
<p><a href="https://i.sstatic.net/Y3wTY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Y3wTY.png" alt="cornell box with two blocks" /></a>.</p>
<p>Here is another image with different block size and dimensions but with the same problem:
<a href="https://i.sstatic.net/xvhxw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xvhxw.png" alt="another image with different dimensions" /></a></p>

CC BY-SA 4.0
2020-09-30T04:01:11.003Z
Try rounding off your random number to either 0 or 1 (greater than 0.5 = 1, else 0) and printing a black and white image based on that. I think your random numbers repeat sequence in certain blocks which are those squares/rectangles you see. The black and white image will depict better the randomness
gallickgunner
<raytracing><rendering><pathtracing><cuda><debugging>
Why does my image has a square pattern in cuda path tracer?
Kaan E.
# Why does my image has a square pattern in cuda path tracer?

(Asked by: username_0 on 2020-09-20)

I am writting a path tracer in [cuda](https://github.com/D-K-E/cuda-raytracer.git) following the Shirley's book. I am having a square pattern as an output.

The size of the block change if I change the block dimensions. For this image, the block dimensions follow the aspect ratio, basically 16 \* 9 is block size. I know that without actually showing the entire code, it is difficult to debug it. I am just asking if you have any idea where I should start debugging it. My hunch is that there is a problem with my random number generator.
Here is its code in case I am doing something wrong. Here is the host code:

```cpp
int total_pixel_size = WIDTH * HEIGHT;
size_t frameSize = 3 * total_pixel_size;

// declare random state
int SEED = time(NULL);
thrust::device_ptr<curandState> randState1 = thrust::device_malloc<curandState>(frameSize);
CUDA_CONTROL(cudaGetLastError());

// declare random state 2
thrust::device_ptr<curandState> randState2 = thrust::device_malloc<curandState>(1);
CUDA_CONTROL(cudaGetLastError());
rand_init<<<1, 1>>>(thrust::raw_pointer_cast(randState2),
                      SEED);
CUDA_CONTROL(cudaGetLastError());
CUDA_CONTROL(cudaDeviceSynchronize());

dim3 blocks(WIDTH / BLOCK_WIDTH + 1,
              HEIGHT / BLOCK_HEIGHT + 1);
  dim3 threads(BLOCK_WIDTH, BLOCK_HEIGHT);
  render_init<<<blocks, threads>>>(
      WIDTH, HEIGHT, thrust::raw_pointer_cast(randState1),
      SEED);

```

Here is the device code:

```cpp

__global__ void rand_init(curandState *randState,
                          int seed) {
  if (threadIdx.x == 0 && blockIdx.x == 0) {
    curand_init(seed, 0, 0, randState);
  }
}

__global__ void render_init(int mx, int my,
                            curandState *randState,
                            int seed) {
  if (threadIdx.x == 0 && threadIdx.y == 0) {
    curand_init(seed, 0, 0, randState);
  }
  int i = threadIdx.x + blockIdx.x * blockDim.x;
  int j = threadIdx.y + blockIdx.y * blockDim.y;

  if ((i >= mx) || (j >= my)) {
    return;
  }
  int pixel_index = j * mx + i;
  // same seed, different index
  curand_init(seed + pixel_index, pixel_index, 0,
              &randState[pixel_index]);
}

```

I would really appreciate any pointers to the problem.

[![cornell box with two blocks](https://i.sstatic.net/Y3wTY.png)](https://i.sstatic.net/Y3wTY.png).

Here is another image with different block size and dimensions but with the same problem:
[![another image with different dimensions](https://i.sstatic.net/xvhxw.png)](https://i.sstatic.net/xvhxw.png)

**Tags:** raytracing, rendering, pathtracing, cuda, debugging

> **Comments:**
> **username_1**: Try rounding off your random number to either 0 or 1 (greater than 0.5 = 1, else 0) and printing a black and white image based on that. I think your random numbers repeat sequence in certain blocks which are those squares/rectangles you see. The black and white image will depict better the randomness  

---

## 1 answers

### Answer by username_0 on 2020-10-08. Score: 1

I managed to fix it. It was indeed related to repeating random sequences.
The problem was the following. `curandState* randState` is an array of `curandState`s, and most calls to `curand_*` require a pointer of a `curandState`.
I was sending the `randState`, the array, to `curand_*` functions and not for example, a pointer of one of its members.
Now most of my images are like this: [![current image output](https://i.sstatic.net/asZM7.png)](https://i.sstatic.net/asZM7.png)


---

