RenderTexture not working on mobile

Hi,

I’m trying to use a rendertexture to draw brushes onto a surface. It’s working fine in the Editor and in a Windows standalone build, but I don’t see anything when I build for Android or iOS.

This is my code for the drawing, where canvas is my RenderTexture, originalTexture is the surface I’m drawing onto and brushTexture is the texture for the brush I’m using.

RenderTexture.active = canvas;
        GL.PushMatrix();
        GL.LoadPixelMatrix(0, originalTexture.width, originalTexture.height, 0);
     
        int dW = canvas.width;
        int dH = canvas.height;
        Rect screenRect = new Rect(pos.x - (scaledWidth / 2), pos.y - (scaledHeight / 2), scaledWidth, scaledHeight);
        brushMaterial.SetFloat("_Alpha", opacity);
        Graphics.DrawTexture(screenRect, brushTexture, brushMaterial);
        GL.PopMatrix();
        RenderTexture.active = null;

I’m using a custom shader to provide an alpha value, but I’ve also tested this with the Sprites/Default shader to eliminate the possibility that it’s something to do with my shader code.

Here is my shader code:

Shader "Unlit/PremultipliedShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Alpha("Alpha", Float) = 1
    }
    SubShader
    {
        Tags {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
        }
     
        Cull Off
        Lighting Off
        Blend One OneMinusSrcAlpha

        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
         
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float _Alpha;
            float4 _MainTex_ST;
         
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }
         
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                col = col * _Alpha;
                return col;
            }
            ENDCG
        }
    }
}

I get a warning in the IDE:
Tiled GPU perf. warning: RenderTexture color surface (1024x1024) was not cleared/discarded, doing
which makes me feel like I should be using RenderTexture.GetTemporary instead, but I’m not sure if a) that’s the issue or b) how to use it. I’ve read some threads on this issue which seemed to suggest this warning can be ignored, but maybe they’re not targeting mobile.

All of the textures I’m using are using RGBA32, if that makes a difference.

That is really only a performance warning. Because mobile devices commonly use tile based rendering, you should not expect the RenderTexture to keep its contents of the last frame. (While on desktop hardware that is the case.) So if you don’t clear the RenderTexture between frames, Unity will copy the contents back and forward to match the expected behaviour on desktop hardware. Which, if you don’t need that, is a waste of performance.

So, clearing a RenderTexture takes time on desktop hardware. Not clearing it generally takes time on mobile hardware.

Edit: On to your issue. Is it possible to convert your setup to use OnRenderImage as if it is a postprocess effect?

1 Like

This sounds like you’re doing something else wrong. Subtract things one by one until it works again to narrow down the issue. Is it even really render texture that’s causing it? Is it the format? you find out.

You’re 100% sure it’s actually the rendertexture and not what you’re doing with it?

Post-Processing on the camera give me the same error. Just turn off the emulation does not help, because i build the game for android. So should i just ignore the warning, or never use Post-Processing on mobile platforms? :frowning: