GPUOpen-LibrariesAndSDKs / RadeonProRender-Baikal

MIT License
334 stars 78 forks source link

Random Sample Issue in OpenCL Kernel #184

Open zjxeditor opened 6 years ago

zjxeditor commented 6 years ago

Hi, please pay attention to this piece of code in monte_carlo_renderer.cl (actually, this pattern appears in many kernels):

        int idx = pixel_idx[global_id];
        int y = idx / output_width;
        int x = idx % output_width;

        // Get pointer to ray & path handles
        GLOBAL ray* my_ray = rays + global_id;

        // Initialize sampler
        Sampler sampler;
#if SAMPLER == SOBOL
        uint scramble = random[x + output_width * y] * 0x1fe3434f;

        if (frame & 0xF)
        {
            random[x + output_width * y] = WangHash(scramble);
        }

        Sampler_Init(&sampler, frame, SAMPLE_DIM_CAMERA_OFFSET, scramble);
#elif SAMPLER == RANDOM
        uint scramble = x + output_width * y * rng_seed;
        Sampler_Init(&sampler, scramble);
#elif SAMPLER == CMJ
        uint rnd = random[x + output_width * y];
        uint scramble = rnd * 0x1fe3434f * ((frame + 133 * rnd) / (CMJ_DIM * CMJ_DIM));
        Sampler_Init(&sampler, frame % (CMJ_DIM * CMJ_DIM), SAMPLE_DIM_CAMERA_OFFSET, scramble);
#endif

We know that MonteCarloRenderer call RenderTile method to render. When the output size is bigger than kTileSizeX * kTileSizeY, then each time we only render a tile. That is why we use pixel_idx buffer to establish the relationship between ray index and output index. We use x + output_width * y to read or write data in the random buffer, however, the random buffer size is kTileSizeX * kTileSizeY. So the index may be out of bound. It should be correct to just use the global_id to read or write the random buffer. I don't know how OpenCL handle the out of range problem. There is something wrong with the logic. Hope for reply, thanks :-)