Skip to main content
Code typo
Source Link

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    // I want to release tex here because i believe everytime this method executes tex is not getting garbage collected and stays in memory
    // Only problem is I cannot release it before, and after returning, it is obvious I cannot do that.
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
private Texture2D mTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
private Texture2D mTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    // I want to release tex here because i believe everytime this method executes tex is not getting garbage collected and stays in memory
    // Only problem is I cannot release it before, and after returning, it is obvious I cannot do that.
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
private Texture2D mTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}
Code typo
Source Link

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
private Texture2D mTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
private Texture2D mTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}
Explaination added
Source Link

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

I have a simple method that converts RenderTexture data into Texture2D

public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture, int width, int height,
    TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) {
    
    Texture2D tex = new Texture2D(width, height, textureFormat, mipChain);
    RenderTexture.active = renderTexture;
    
    tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    tex.Apply();
    
    RenderTexture.active = null;
    // Here I want some way to release memory occupied by tex
    return tex;
}

The above mentioned method is working exactly as intended. However, if I call this method multiple times, or in update it starts using so much memory Profiller

How can I avoid this problem?

Edit I cannot share the entire code but this is minimal code to reproduce the issue

[SerializeField] private RenderTexture renderTexture;
   
private void Start() {
    mTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.BGRA32, false);
}

private void Update() {
    mTexture = TextureUtils.RenderTextureToTexture2D(renderTexture, renderTexture.width, renderTexture.height, TextureFormat.BGRA32);
        
    // Some agora sdk stuff to share the mTexture over the network for screen sharing 
}
Source Link
Loading