Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issue on Mac, with deferred path and Metal in graphics

Discussion in 'Shaders' started by nasos_333, Oct 29, 2019.

  1. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,948
    Hi,

    I have an issue with artifacts when i try in Mac PC, in deferred path and Metal in graphics.

    I have pinpointed the issue to be in the way i use the water pro shader of Unity

    https://forum.unity.com/threads/porting-water-foam-and-edge-blend.260926/

    Specifically when i call the below

    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]#ifdef WATER_EDGEBLEND_ON
    4. [*]       half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
    5. [*]       depth = LinearEyeDepth(depth);
    6. [*]       edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.w));
    7. [*]       edgeBlendFactors.y = 1.0-edgeBlendFactors.y;
    8. [*]     #endif
    9. [/LIST]
    10.  
    i get artifacts in water, and when i saturate the i.screenPos.w, they go away (but not works as expected after that)

    Anyone has any idea how to fix this one ? Thanks in advance
     
    Last edited: Oct 29, 2019
  2. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,948
  3. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,948
    i found the issue, i had to use ZWrite off in the shader, seems it creates issue if not defined for transparency in Metal
     
  4. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,948
    I still have a problem with this though, as i need the ZWrite to not be Off to get the correct result for upper and under sides of water at same time, so i wonder if this is a known bug in Mac and Metal.
     
  5. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    34
    Hi, we are facing similar issue with water. When reading data from _CameraDepthTexture and ZWrite On we are getting nasty artifacts.

    I have tried various approaches to get copy of DepthBuffer, but with no result. When sampling _CameraDepthTexture directly artifacts are there, from that _CameraDepthTexture should cause the problems.
     
    nasos_333 likes this.
  6. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    34
    And in comparison to DirectX. There is no event in the frame debugger that copies the depth texture somewhere.

    I see that obtaining depth texture could be expensive on TLDR for example on iOS, but would it be possible to get depth texture just for current tile?
     
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,850
    metal needs you to create a custom copy of the depth buffer when it comes to deferred – which can then be used by the water to do depth related calculations.
    see lux water :)
     
  8. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    34
    This is probably not so straightforward as it looks. We would like to obtain that texture between some opaque draw calls. But only place where it is possible to copy it is AfterDepthTexture, which is way behind what we need ;)
     
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,850
    this is not supported...
     
  10. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    34
    Do you know why it is not supported? Seems a little buggy to me that Grab texture works, so the copy of color buffer is ok, but the copy of depth buffer is not ok? It is almost the same resource produced while rendering. In fact, on the deferred renderer it should be much harder to obtain copy of the color buffer, than the copy of depth buffer i guess.
     
  11. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,850
    grab texture works when rendering opaques?
     
  12. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,948
    Indeed, seems like big bug that needs to be fixed, if adds extra render step for no reason
     
  13. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    34
    Yes, that correct question. I haven't time to test it, but Before/AfterForwardOpaque should be just fine even on the deferred rendering path? From my point of view, color buffer and depth buffer should be accesible here to make a copy. Am i right?
     
  14. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,948
    i tried do the fill of depth buffer in a million ways and none seems to work out correctly, i still get either black artifacts or overlapping polygons or polygon gaps in my water mesh :(
     
  15. Egnech

    Egnech

    Joined:
    Aug 27, 2015
    Posts:
    27

    Any luck with this? I've faced with the same issue and have not found any solution so far..
     
  16. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    34
    After many investigations, you will find that Metal probably passes to _CameraDepthTexture, current depth buffer. From my point of view, due to synchronization it is impossible to read and write to that texture. If you need this functionality on metal best for you will be do a copy beforehand and then pass it to your shader as _CameraDepthTextureCopy for example and don't access _CameraDepthTexture at all. Or you can use _CameraDepthTexture, but then you will have turn off ZWrite and somehow tackle issues connected to that

    You can copy _CameraDepthTexture using Blit into arbitrary texture
     
  17. Egnech

    Egnech

    Joined:
    Aug 27, 2015
    Posts:
    27