Importance sampling environment texture

I’m trying importance sample an environment texture.
For this I’m using a buffer populated by a quite large struct (some floats, 64 float marginal distribution, 64x64 float conditional and a 64x64 float3 pixeldata). The problem is that I can’t simply write e.g.
(1)float3 color = buf[0].pixeldata[0],
since this gives me data corruption (probably because optix doesn’t like me reading the same data from every thread?). However writing
(2) buftype a = buf[0]; float3 color = a.pixeldata[0],
works fine although very slow.

So, my question(s) is:

  1. Is there a better way to setup my solution?( for example, putting the distributions and pixeldata in textures)
  2. Can I use something similar to restrict const in CUDA?

Best regards

Edit: Wrote something false. Don’t know how to remove.

Are you saying that your previous post should be ignored?

No. Sorry for being confusing. I thought I got it working and said I solved it… Turned out I had my debug color as the same value for exception. Silly me.

I am not very sure about your method. I implemented environment map sampling few days ago. You can refer to pbrt or mitsuba’s implementation.

Basically, you can use a TextureSampler for the envmap texture; precompute the marginal and conditional distribution on cpu, then upload them to gpu; to sample a uv coordiante, you will need to implement the upper_bound function on gpu. You can find the details in PBRT book.

Hi, thanks.
I will do something along those lines. However my setup is such that I have two steps of my importance sampling; one coarse for selecting a sub region, and the finer sub region for envmap sampling. I don’t think this will be a major problem, just an inconvenience. I’m still a bit worried though, the behaviour of getting corruption when accessing a buffer element through reference and not copying… is that to be expected, or is it likely something from my part? If I need to make a copy of a buffer element containing a 64 marginal and 64x64 conditional, performance will suffer.
Thus I will now try to put every damn thing in a texture and get the needed data by some indexing smartness. :)