Search Unity

Question Flipped picture in game view

Discussion in 'General Graphics' started by AlexTorbin, Dec 17, 2020.

  1. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    I'm playing with custom render pipeline, which is quite simple at the moment. First, I render scene with MRT shader, which outputs 2 textures: color and custom data. Then DrawSkybox() into color tex. And finally Blit() from color tex to CameraTarget with post fx shader, which uses data tex. All shader code is written on HLSL and I'm using Core RP Library. To this moment I never had an issue with flipped picture, but I knew it could happen.

    So, today I was rewriting some code. The only meaningful thing I did was changing declaration of render textures. I changed this:
    Code (CSharp):
    1. buffer.GetTemporaryRT(colorTexID, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.Default);
    to this:
    Code (CSharp):
    1. RenderTextureDescriptor basicTexDesc = new RenderTextureDescriptor()
    2.         {
    3.             dimension = TextureDimension.Tex2D,
    4.             graphicsFormat = GraphicsFormat.R8G8B8A8_UNorm,
    5.             width = camera.pixelWidth,
    6.             height = camera.pixelHeight,
    7.             depthBufferBits = 0,
    8.             msaaSamples = 1,
    9.             sRGB = false,
    10.             useMipMap = false,
    11.             autoGenerateMips = false
    12.         };
    13.  
    14. buffer.GetTemporaryRT(colorTexID, basicTexDesc, FilterMode.Point);

    After that change the render flipped upside down. So I went to dedicated manual page and began to try different stuff they suggest. I believe I only need to adjust post fx shader, right? So, adding this to Vertex program doesn't do anything:
    Code (CSharp):
    1. #if UNITY_UV_STARTS_AT_TOP
    2. if (_MainTex_TexelSize.y < 0)
    3.     output.uv.y = 1-output.uv.y;
    4. #endif
    This fixed an issue, but ONLY in editor window, in game view it remains flipped:
    Code (CSharp):
    1. float u = input.uv.x;
    2. float v = (_ProjectionParams.x > 0) ? input.uv.y : 1 - input.uv.y;
    3. output.uv = float2(u, v);
    What am I doing wrong?
     
  2. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    So, the "fix" was to remove ProjParams check condition (always* flip input.uv.y instead). Somehow this makes it consistent between editor and game view. But I'm certainly not satisfied with this, I believe that in some cases it will surely break. So the questions remain - why getting temporary RT with Descriptor makes them flip? Why is it different in editor and game view? What should I do to reliably prevent this behavior?

    *I've found that doing this in fx shader also works consistently for all cameras.
    Code (CSharp):
    1. #if UNITY_UV_STARTS_AT_TOP
    2.     output.uv = float2(input.uv.x, 1 - input.uv.y);
    3. #else
    4.     output.uv = input.uv;
    5. #endif
    Does it seal the deal, or should I fight flipping on earlier stages, when rendering geometry into textures?
     
    Last edited: Dec 18, 2020