jcowles / gpu-ray-tracing

A ray tracer, built in Unity with compute shaders
Apache License 2.0
141 stars 19 forks source link

Black screen #1

Closed nezix closed 6 years ago

nezix commented 6 years ago

Hello,

I was impatient to try your implementation in Unity but I have black screen on a freshly cloned repo on Unity 2017.1 and 2017.3. Windows 10 / 780 Ti. The AccumulatedColorTexture is filled with black.

jcowles commented 6 years ago

The texture is cloned on startup, so if you're looking at it in the editor inspector, it will always be black.

Does the viewport render correctly?

nezix commented 6 years ago

No it does not.

jcowles commented 6 years ago

Any errors / warnings?

nezix commented 6 years ago

Sadly no

jcowles commented 6 years ago

Well, that's going to be tricky to debug remotely.

My guess is that something in the compute shader isn't compiling correctly -- what if you just set the texture to red in the raytrace kernel and return right away? If that works, you might need to manually bisect it until you find the line that's causing trouble.

jcowles commented 6 years ago

And thanks for reporting the bug(!), I'm sure other people are hitting it too.

nezix commented 6 years ago

I will try to debug it ! I keep you updated.

nezix commented 6 years ago

Something goes wrong line 273 of RayTracer.compute.

If I change the line from _MainTex[xy] += vec4(r.color, 1); to _MainTex[xy] = vec4(r.color, 1);

I get that image

jcowles commented 6 years ago

Ah, that line is actually somewhat shady - what if you grab the value, add to it and then store it back?

nezix commented 6 years ago

vec4 colTmp = _MainTex[xy];

colTmp += vec4(r.color,1);

_MainTex[xy] = colTmp;

Does not do the trick.

jcowles commented 6 years ago

That buffer is never explicitly cleared, perhaps my driver is initializing it to 0 and yours is defaults to garbage (NaN, etc).

Right after initialization (the link I sent) set Render texture.active equal to the newly allocated texture then GL.Clear() it.

nezix commented 6 years ago

m_accumulatedImage = new RenderTexture(MainTexture.descriptor); m_accumulatedImage.enableRandomWrite = true; RenderTexture.active = m_accumulatedImage; GL.Clear(false, true, Color.white); m_accumulatedImage.Create();

Still a black screen. It has to be a driver issue anyway.

If I uncomment the MainTex[xy] += vec4(r.color, 1); line, I have a white screen as expected.

jcowles commented 6 years ago

Yeah, seems like a driver issue, what version are you using?

nezix commented 6 years ago

I am on 390.77. I am currently updating to 391.35

jcowles commented 6 years ago

Ah there is some interesting interaction between the compute shader and the texture (looking into it more now). I think the solution is to write to a single value to the texture from the compute shader and then accumulate into the final texture with a blit after; in pseudo code:

RayTraceKernels.SetTexture(m_rayTraceKernel, "_MainTexInput", m_tempSingleValueImage);
...
compute.Dispatch(...);
Graphics.Blit(m_tempSingleValueImage, m_accumulatedImage, AdditiveMaterial);

Let me know if this works -- or if you don't want to invest more time, I'll push a branch for you to try later tonight.

nezix commented 6 years ago

I updated to the latest driver version and I still get the black screen. I see what you mean. I don't have much time today but I can look into it probably tomorrow. Thanks for your support !

jcowles commented 6 years ago

Thanks for reporting and trying to debug it! I'll probably just make that change and push it (sometime later), this seems like a good change regardless.

jcowles commented 6 years ago

Oh, it looks like reading and writing RWTexture2D -- tex2d + float4 specifically -- is just not supported/required (see this thread and the one it references).

nezix commented 6 years ago

I tried to use 2 different RenderTexture but could not make it work properly. Did you find a workaround ?

jcowles commented 6 years ago

I've confirmed with friends at NVIDIA that float4 with RWTexture2D was not supported on your generation of GPU. I think I have a decent work around, hopefully I'll have some time tonight to finish it up.

jcowles commented 6 years ago

Ok, this is still a work in progress, but want to give it a try? https://github.com/jcowles/gpu-ray-tracing/commit/55dd69dfb79c4b4f5911c14a9189e849ec0ba789

It skips the texture entirely and color is accumulated directly from the rays in the full screen resolve.

nezix commented 6 years ago

Works like a charm. I really appreciate your support !

jcowles commented 6 years ago

Sweet!

Note that there is still a bug with this version (some bad aliasing was introduced), so I'll track that down and then merge it back into the main branch.

nezix commented 6 years ago

I added the https://github.com/Chman/SMAA asset but it does not fully solve the aliasing issue. Thanks again !

jcowles commented 6 years ago

Yeah, this is a legit bug somewhere, it should look as clean as the images in the blog post.

jcowles commented 6 years ago

Ah, FYI, the aliasing bug is due to the difference in resolution of the accumulation buffer and the screen resolution -- the texture was filtered via the texture sampler in the full screen resolve pass, where sampling the rays directly has no filtering.

jcowles commented 6 years ago

The final fix has been pushed to master as ae99186