I am writting a path tracer in cuda 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:

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);

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

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),

Here is the device code:

__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)) {
  int pixel_index = j * mx + i;
  // same seed, different index
  curand_init(seed + pixel_index, pixel_index, 0,

I would really appreciate any pointers to the problem.

cornell box with two blocks.

Here is another image with different block size and dimensions but with the same problem: another image with different dimensions

  • 1
    $\begingroup$ 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 $\endgroup$ – gallickgunner Sep 30 at 4:01

I managed to fix it. It was indeed related to repeating random sequences. The problem was the following. curandState* randState is an array of curandStates, 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.