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. Dismiss Notice

Accessing MSAA texture

Discussion in 'General Graphics' started by ivyblue, Apr 21, 2017.

  1. ivyblue

    ivyblue

    Joined:
    Aug 25, 2015
    Posts:
    7
    I am trying to access/resolve the MSAA Render Texture/Target in a custom fullscreen pass / image effect. However, when I declare my Texture2DMS in my shader, I get the following error:

    Shader error in 'Custom/customres': Fragment program 'frag': unrecognized sampler type msaaTex (on d3d11)

    Does Unity support Texture2DMS access? Am I missing something in my declaration?
    System: Windows / HLSL / SM 5.0

    Code (CSharp):
    1. Shader "Custom/customres"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         // No culling or depth
    10.         Cull Off ZWrite Off ZTest Always
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.  
    16.             #pragma target 5.0
    17.             #pragma only_renderers d3d11
    18.  
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.            
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             v2f vert (appdata v)
    37.             {
    38.                 v2f o;
    39.                 o.vertex = UnityObjectToClipPos(v.vertex);
    40.                 o.uv = v.uv;
    41.                 return o;
    42.             }
    43.            
    44.             //sampler2D _MainTex;
    45.             Texture2DMS<float4, 4> msaaTex : register(t0);
    46.  
    47.             float4 frag (v2f i) : SV_Target
    48.             {
    49.                 float4 col;
    50.            
    51.                 // this doesn't work
    52.                 col = msaaTex.Load(0, 0);
    53.  
    54.                 return col;
    55.             }
    56.             ENDCG
    57.         }
    58.     }
    59. }
    60.  
     
  2. MatOfPie

    MatOfPie

    Joined:
    Apr 24, 2017
    Posts:
    2
    Hi there,

    I am now stuck with the same issue, after a bit of research it seams that Unity will force the MSAA resolve BEFORE you bind your MSAA texture to your shader, and as a result you won't have access to the sample data because a MSAA depth texture simply cannot be resolved, not a bug thats just how it is defined.

    That's a huge pity in my case because I'm rendering in MSAA for VR, but I don't have any way to access the depth buffer I just rendered my geometries in.

    The only work around ATM is to NOT use MSAA or to force the camera to re-render the scene in a non-MSAA texture using Camera.depthTextureMode (easy fix if your scene is simple) and then use the texture "_CameraDepthTexture" in your shader (the texture will get automatically attached to your shader for you behind the scenes).

    But in my case that is not possible because my scenes are very complex and I can't afford rendering the scene twice (CAD scenario).

    I would love to hear from Unity gurus to know if there is a solution to that problem maybe @Aras ?

    At the moment I am thinking of writing a native C++ plugin to copy the only a single sample channel from a MSAA to a non-MSAA texture behind the scene, but if there would be a simpler hack I would be so happy....

    ta.
     
    Last edited: Apr 24, 2017
  3. tangwilliam

    tangwilliam

    Joined:
    May 4, 2016
    Posts:
    22
    Hi, do you find out some way to solve the problem ? I came into the same problem.And My scene is also complex so it's too expensive to use the depthTextureMode.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    I know this is solved for the SRPs as it ships with a depth blit shader that'll sample from an MSAA depth to a non-MSAA target.
    https://github.com/Unity-Technologi...es.universal/Shaders/Utils/CopyDepthPass.hlsl

    And you can tell a MSAA render texture to not resolve to a non-MSAA target when used as a texture property.
    https://docs.unity3d.com/ScriptReference/RenderTexture-bindTextureMS.html

    Theoretically this should be usable for the built in renderer, though you'll probably need to use your own render textures and set the camera's buffers yourself.
     
    Jason-Michael likes this.
  5. tangwilliam

    tangwilliam

    Joined:
    May 4, 2016
    Posts:
    22
    Thanks for your reply. Our project is using the 2018.1.6 version. And the doc for 2018.1 says that the SRPs is in preview, so I guess it's unsafe to use it for our project.
     
  6. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    Just an FYI: The current implementation of URP/LWRP falls back to a depth pre-pass (which is serious perf hit for us) due to this line:
    // TODO: We don't have support to highp Texture2DMS currently and this breaks depth precision.
    // currently disabling it until shader changes kick in.
    In forwardrenderer.cs

    I have gotten the copy to work, but I am not sure how reliable it will be across devices.
    For example on DX, you can auto-resolve the depth buffer (I am not sure what operation it does to combine 4 depths into 1)
    On mobile, we have to resolve the whole MSAA depth surface to memory and access it as a Texture2DMS<float, samples>. This is expensive but it seems less expensive than the depth pre-pass.
     
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,850
    @DavidSWu this TODO is outdated. Can you please report a bug so that we fix this?
    Support for precision postfixes for all variants of textures is there since 2018.3.0a2 :)
     
  8. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    Thank you for the information, I will report it!
    What exactly are the precision postfixes?
    We use Texture2DMS<float,4>, are we getting low precision results?
    Also are there ways to get 4 pixels at a time? Either the 4 MSAA samples or a form of Gather4 that works on an MSAA index?
    Thanks!
     
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,850
    You can use all of the Texture* (Texture2D, Texture3D and all others) with "_half" or "_float" postfixes, e.g.
    Texture2DMS_float<float, 4>.
    Unity 2020.1 extracts information from "<float, 4>", so it shouldn't be required there anymore.
    Before Unity 2020.1, yes.
    Seems like there's no support for this in HLSL: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-gather
     
  10. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    Thank you alek for the responses!
     
  11. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    I don't think that this is still the case.
    While resolving a depth buffer is not well defined (there is no obvious operation to use, i.e. average, an average of reciprocals, max, etc. ), it seems to be supported on a growing number of platforms.

    If you are only accessing the depth of the pixel that you are rendering to, and you are rendering at the same MSAA and resolution you might be about to use the RenderPass api. This seems like the most efficient solution by far.
    However, I don't know what platforms it will be natively supported on. It seems like with GLES it currently emulates with rendertargets and resolves. On Vulcan and Metal it is native.
     
  12. pw_prg_yinchao

    pw_prg_yinchao

    Joined:
    Feb 14, 2020
    Posts:
    18
    Hysparions likes this.
  13. Hysparions

    Hysparions

    Joined:
    Jan 7, 2019
    Posts:
    29