Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Unable to ReadPixel from RenderTexture at sizes near 2k

Discussion in 'General Graphics' started by Vykaash, Mar 17, 2023.

  1. Vykaash

    Vykaash

    Joined:
    Jan 15, 2018
    Posts:
    2
    Trying to understand the workflow of using RenderTextures and using Texture2D to visualize results from fragment shader and am running into issues with RenderTexture sizes approaching 2k by 2k.
    Results at 2000x2000
    upload_2023-3-16_19-23-58.png
    Results at 2048x2048
    upload_2023-3-16_19-25-22.png

    These results are taken from the Play window. I have tried removing logic from the shader by always returning black but it still appears as that slight grey color. I've also tested for an upper limit and it happens when the texture hits 2035x2035. 2034x2034 works as expected. I checked maxTextureSize and it returns over 16k.
    The script
    Code (CSharp):
    1.  
    2.  
    3.     public void OnEnable()
    4.     {
    5.         output = new RenderTexture(
    6.             mapSize, mapSize, 0,
    7.             RenderTextureFormat.ARGB32,
    8.             RenderTextureReadWrite.Linear
    9.         );
    10.  
    11.         meshMat = GetComponent<MeshRenderer>().materials[0];
    12.         debugTexture = new Texture2D(mapSize, mapSize, TextureFormat.ARGB32, -1, true);
    13.         debugTexture.filterMode = FilterMode.Point;
    14.         debugTexture.wrapMode = TextureWrapMode.Clamp;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.        // generate new diagram every time space is pressed
    20.         if (Input.GetKeyDown("space"))
    21.         {
    22.            // run at the end of rendering
    23.             RenderPipelineManager.endContextRendering += blit;
    24.         }
    25.     }
    26.  
    27.     private void blit(ScriptableRenderContext c, List<Camera> cams)
    28.     {
    29.         RenderPipelineManager.endContextRendering -= blit;
    30.  
    31.        // random seeds and associated color for diagram
    32.         float[] pos = {
    33.            Random.Range(0.0f, 1.0f),Random.Range(0.0f, 1.0f), 1.0f, 0.0f,0.0f,
    34.            Random.Range(0.0f, 1.0f),Random.Range(0.0f, 1.0f),1.0f, 1.0f,0.0f,
    35.            Random.Range(0.0f, 1.0f),Random.Range(0.0f, 1.0f),1.0f, 1.0f,1.0f,
    36.            Random.Range(0.0f, 1.0f),Random.Range(0.0f, 1.0f),0.0f, 1.0f,1.0f,
    37.            Random.Range(0.0f, 1.0f),Random.Range(0.0f, 1.0f),0.0f, 0.0f,1.0f,
    38.              };
    39.  
    40.         // send to shader
    41.         voronoiMaterial.SetFloatArray("pos", pos);
    42.         Graphics.Blit(null, output, voronoiMaterial);
    43.         updateTexture();
    44.     }
    45.  
    46.     private void updateTexture()
    47.     {
    48.        // output should be active but make sure anyways
    49.         RenderTexture.active = output;
    50.        // read entire texture
    51.         debugTexture.ReadPixels(
    52.             new Rect(0, 0, mapSize, mapSize),
    53.             0, 0, false
    54.         );
    55.         debugTexture.Apply(false);
    56.         meshMat.SetTexture("_MainTex", debugTexture);
    57.     }
    58.  
    The shader:
    Code (CSharp):
    1. Shader "Unlit/Voronoi"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.     SubShader
    7.     {
    8.         Tags { "RenderType"="Opaque" }
    9.  
    10.         Pass
    11.         {
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.  
    16.             #include "UnityCG.cginc"
    17.             float pos[25];
    18.             struct appdata
    19.             {
    20.                 float4 vertex : POSITION;
    21.                 float2 texcoord: TEXCOORD0;
    22.             };
    23.  
    24.             struct v2f
    25.             {
    26.                 float4 vertex : SV_POSITION;
    27.                 float2 uv: TEXCOORD0;
    28.             };
    29.  
    30.             float uvArrDistSqrd (float2 uv, float x, float y)
    31.             {
    32.                 float dx = uv.x - x;
    33.                 float dy = uv.y - y;
    34.                 return dx*dx + dy*dy;
    35.             }
    36.  
    37.             v2f vert (appdata v)
    38.             {
    39.                 v2f o;
    40.                 o.vertex = UnityObjectToClipPos(v.vertex);
    41.                 o.uv = v.texcoord;
    42.                 return o;
    43.             }
    44.  
    45.             fixed4 frag (v2f i) : SV_Target
    46.             {
    47.                 float2 uv = i.uv;
    48.                 float currDistance = uvArrDistSqrd(uv, pos[0], pos[1]);
    49.                 float4 color = float4(pos[2], pos[3], pos[4], 1.0f);
    50.                 // if (currDistance < 0.00025f) {
    51.                 //     color = float4(0, 0,0,1.0f);
    52.                 // }
    53.  
    54.                 [unroll(4)] for (int i = 1; i < 5; ++i)
    55.                 {
    56.                     int offset = i * 5;
    57.                     float d = uvArrDistSqrd(uv, pos[offset], pos[offset+1]);
    58.                     // if (d < 0.00025f) {
    59.                     //     return float4(0, 0,0,1.0f);
    60.                     // }
    61.                     if (d < currDistance)
    62.                     {
    63.                         currDistance = d;
    64.                         color = float4(pos[offset + 2],pos[offset + 3], pos[offset + 4], 1.0f);
    65.                     }
    66.                 }
    67.                 return color;
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }
    System:
    Unity: 2021.3.11f1 3D HDRP scene
    CPU: Ryzen9 5900x
    GPU: AMD 6900xt
    Mem: 32GB

    Question:

    I'm curious if there is some limitation I'm not aware of? Or perhaps timing issue? Any help is appreciated.
     
    Last edited: Mar 20, 2023
  2. Vykaash

    Vykaash

    Joined:
    Jan 15, 2018
    Posts:
    2
    The issue no longer occurs. I'm not sure what the issue was to begin with, but it simply started working as expected.