defold / sample-lights-and-shadows

Example of how to achieve pixel perfect shadows in a 2D view
MIT License
33 stars 5 forks source link

Shadow rendering at wrong distance on AMD cards #12

Closed paulbaucom closed 4 weeks ago

paulbaucom commented 1 year ago

Shadows are rendering at the wrong location on AMD cards. They render at half the distance between occluder and light source. See images in forum post: https://forum.defold.com/t/2d-lights-and-shadows-sample/68970/21?u=pabaucom

This was confirmed an issue on an RX 6800 on driver 23.7.1 and the previous version with all driver settings reset to factory defaults. nVidia cards rendered as expected.

It appears I have fixed it on commit d9c6faa of a fork I was testing on. I would like some feedback on performance impact of this and why it might be necessary on my AMD chip. It looks like step and coord have to be slightly different and not be offset by a whole number.

For reference, commit e451d6e on a test branch also fixed AMD, but then doubled the shadow distance on nVidia. I speculate the AMD driver is doing some optimization leading to this edge case with 1D shadow maps. This is my first experience with shader programming, so I am guessing.

britzl commented 1 year ago

I would like some feedback on performance impact of this and why it might be necessary on my AMD chip.

Let's see if we can get some input from @Jhonnyg

paulbaucom commented 1 year ago

In the PR, I multiplied by 1.00001 and it fixes the issue on AMD while nVidia still renders properly. Adding a small value to coord rather than multiplying by 1+eps does not fix it. My comment in code may be more confusing than accurate and may need to be changed.

I am probably assuming a lot about what AMD is doing to optimize that loop versus what the nVidia driver does. I made those assumptions based on what I was seeing playing with the values of distance, step, and y and running on AMD vs nVidia.

I see the same behavior on a Windows and HTML5 debug or release build. Testing on two computers with latest nVidia and AMD drivers. It is consistent across several previous versions of Defold. I have not tested on my Intel iGPU.