kode80 / kode80SSR

An open source screen space reflections implementation for Unity3D 5.
BSD 2-Clause "Simplified" License
382 stars 92 forks source link

Reflections not rendering correctly #2

Closed StigOlavsen closed 9 years ago

StigOlavsen commented 9 years ago

So the reflections didn't seem to work for me, see the two first screenshots below, first from the frame debugger, and the second from the game view. This is on a 2011 MBP with a Radeon 6870M GPU.

I tried hacking the shader a bit, and found that if I replace the last return statement

return half4(tex2D( _MainTex, i.uv).rgb, 0.0);

with a simple

return half4(0.0, 0.0, 0.0, 0.0);

the reflections seem to render correctly, as you can see in the two last screenshots.

Original code, frame debugger:

screenshot 2015-03-13 04 10 32

Original code, full render:

screenshot 2015-03-13 04 10 43

Modified, frame debugger:

screenshot 2015-03-13 04 11 02

Modified, full render:

screenshot 2015-03-13 04 11 15

kode80 commented 9 years ago

That is the strangest thing. All that last line does is return a color if there is no intersection in the ray cast. That pass is then blurred and then finally blended with the original frame buffer in the final pass. Setting it to return black has little to no affect on final image (since the final blend pass reads from the original unaltered frame buffer anyway) so I'm at a loss as to why it's having such a major impact on your hardware. The only thing I can think of is that it's that final texture read that's pushing you over the edge - but that seems highly unlikely considering all the texture reads before and after that.

Can you confirm that this is the latest revision and no other code has been altered locally? Also, does it appear to be scene specific - do you get the exact same results with other scenes?

I'm tempted to just switch to returning black in the SSR pass to fix it for others that may run into this - but I really don't like 'fixes' where I don't understand the underlying issue...

StigOlavsen commented 9 years ago

Yeah it seems really strange. Too many texture reads or too many instructions is a possibility I guess, but I think Unity should report that as an error.

The code is from the last commit on March 12, and I've made no changes. Also it's the same result in all scenes, not specific to this.

If I increase the number of iterations and pixel stride, it appears to render reflections only in the lower left as in the screenshot below. It follow the diagonal no matter which aspect ratio I use. Even like this though, I don't get reflections in the final render.

Some of the noisy pixels will also move around when I click the game viewport, which I guess causes it to redraw the frame. Looks like some kind of uninitialised memory maybe.

I agree about fixing things without knowing exactly why, it can often come back to bite you in the ass.

Also, my fix above doesn't seem to be optimal, if you look near the stairs there are some highlights that shouldn't be there. To fix this, I ended up removing the if test, storing the raytrace in a bool instead, and then multiplying by this at the end. I created a patch for this if you want to take a look: https://gist.github.com/h0rdak/9fd79ccd165d03b3d33e

This not only fixes the issue for me, but saves an if test ;)

screenshot 2015-03-13 23 21 41

kode80 commented 9 years ago

AFAIK those artifacts definitely look like you're hitting a texture/instruction limit. I've started an optimization branch and have refactored the raycast code - seeing a performance increase of around 40% in some cases! (I can run 300 iterations per ray at almost 30fps now). I also included a slightly modified version of your if-removal (I instead just multiply alpha by the bool since this controls the reflection/framebuffer blending and reduces the need to multiply RGB).

If you get a chance could you check out the optimization branch and see if a) it fixes these issues and b) gives you a decent performance increase on your hardware?

Cheers!

StigOlavsen commented 9 years ago

Yes, thank you! It now works nicely as-is, and render time is down from 6-8ms per frame to 3-4ms with the default settings in the example scene.

BTW, thanks for sharing the shader, much appreciated!

kode80 commented 9 years ago

Excellent!!