Search Unity

Scripting Texture2D.Compress causes access violation when using mipmaps at 1024x1024 or larger

Discussion in '5.3 Beta' started by Zuntatos, Nov 3, 2015.

  1. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    522
    If you create a Texture2D with mipmaps and then compress it, unity crashes with an access violation. It only seems to happens at sizes of 1024x1024 and larger.

    That's basically all there is to it. Without mipmaps, it doesn't crash.

    Reproduction code: (bug reporter is bugged for me)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class test : MonoBehaviour
    4. {
    5.     public void Start ()
    6.     {
    7.         Texture2D texture = new Texture2D (2048, 2048, TextureFormat.RGB24, true, true);
    8.         texture.LoadRawTextureData (new byte[(2048 * 2048 + 1024 * 1024 + 512 * 512 + 256 * 256 + 128 * 128 + 64 * 64 + 32 * 32 + 16 * 16 + 8 * 8 + 4 * 4 + 2 * 2 + 1 * 1) * 3]);
    9.         texture.Compress (true); // this line causes an access violation
    10.     }
    11. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using UnityEngine.UI;
    5.  
    6. public class test : MonoBehaviour
    7. {
    8.     static int MipMapPixels (int width, int height)
    9.     {
    10.         int pixelCount = 0;
    11.         while (true) {
    12.             pixelCount += width * height;
    13.             if (width == 1 || height == 1) {
    14.                 return pixelCount;
    15.             }
    16.             width /= 2;
    17.             height /= 2;
    18.         }
    19.     }
    20.  
    21.  
    22.     static void FillTextureData (byte[] data, int imageSize, bool mipmaps)
    23.     {
    24.         int mipmapOffset = 0;
    25.         while (true) {
    26.             for (int x = 0; x < imageSize; x++) {
    27.                 for (int y = 0; y < imageSize; y++) {
    28.                     int index = (x * imageSize + y + mipmapOffset) * 3;
    29.                     data [index++] = (byte)(255f * x / imageSize);
    30.                     data [index] = (byte)(255f * y / imageSize);
    31.                 }
    32.             }
    33.             if (imageSize == 1 | !mipmaps) {
    34.                 return;
    35.             }
    36.             mipmapOffset += imageSize * imageSize;
    37.             imageSize /= 2;
    38.         }
    39.     }
    40.  
    41.     const int ImageSize = 2048;
    42.  
    43.     Texture2D Bug ()
    44.     {
    45.         Texture2D texture = new Texture2D (ImageSize, ImageSize, TextureFormat.RGB24, true, true);
    46.  
    47.         texture.LoadRawTextureData (new byte[MipMapPixels (ImageSize, ImageSize) * 3]);
    48.         texture.Compress (true); // this line causes an access violation
    49.         return texture;
    50.     }
    51.  
    52.     Texture2D BugFancy ()
    53.     {
    54.         Texture2D texture = new Texture2D (ImageSize, ImageSize, TextureFormat.RGB24, true, true);
    55.         byte[] data = new byte[MipMapPixels (ImageSize, ImageSize) * 3];
    56.         FillTextureData (data, ImageSize, true);
    57.         texture.LoadRawTextureData (data);
    58.         texture.Apply (false, false);
    59.         texture.Compress (true); // this line causes an access violation
    60.         return texture;
    61.     }
    62.  
    63.     Texture2D NoBug ()
    64.     {
    65.         Texture2D texture = new Texture2D (ImageSize, ImageSize, TextureFormat.RGB24, false, true);
    66.         texture.LoadRawTextureData (new byte[ImageSize * ImageSize * 3]);
    67.         texture.Compress (true); // this line is OK as no mipmaps
    68.         return texture;
    69.     }
    70.  
    71.     Texture2D NoBugFancy ()
    72.     {
    73.         Texture2D texture = new Texture2D (ImageSize, ImageSize, TextureFormat.RGB24, false, true);
    74.         byte[] data = new byte[ImageSize * ImageSize * 3];
    75.         FillTextureData (data, ImageSize, false);
    76.         texture.LoadRawTextureData (data);
    77.         texture.Apply (false, false);
    78.         texture.Compress (true); // this line is OK as no mipmaps
    79.         return texture;
    80.     }
    81.  
    82.     public void Start ()
    83.     {
    84.         GetComponent<RawImage> ().texture = Bug();
    85.     }
    86. }
    87.  

    Code has some functions without mipmap that work, and some with it that crash. Also some functions that fill the array before passing it, for the sake of completion.

    Create a new scene, and add the script to any object, and it should crash on startup. If you want to check one ofthe NoBug functions, add script to a RawImage.

    Sidenote, I didn't check NPOT sizes.

    Some edits: Edited code a bit. Added the simplest version I could make of it, except for simplifying that math, but then it's not so obvious what it does. Also; 1024x1024 has a chance to output errors in the console instead; 2048x2048 seems to always crash.

    Errors: 10x this:

    Code (CSharp):
    1.  
    2. width <= image->GetWidth() && height <= image->GetHeight()
    3. UnityEngine.Texture2D:INTERNAL_CALL_Compress(Texture2D, Boolean)
    4. UnityEngine.Texture2D:Compress(Boolean) (at C:\buildslave\unity\build\artifacts\generated\common\runtime\TextureBindings.gen.cs:366)
    5. test:Start() (at Assets\test.cs:9)
    6.  
    7. [c:\buildslave\unity\build\runtime\graphics\SharedTextureData.h line 192]
    8. (Filename: Assets/test.cs Line: 9)
    9.  
    10. sizeX >= 1 && sizeY >= 1 && sizeX <= width && sizeY <= height
    11. UnityEngine.Texture2D:INTERNAL_CALL_Compress(Texture2D, Boolean)
    12. UnityEngine.Texture2D:Compress(Boolean) (at C:\buildslave\unity\build\artifacts\generated\common\runtime\TextureBindings.gen.cs:366)
    13. test:Start() (at Assets\test.cs:9)
    14.  
    15. [C:/buildslave/unity/build/Runtime/Graphics/Image.cpp line 143]
    16. (Filename: Assets/test.cs Line: 9)
    17.  
     
    Last edited: Nov 3, 2015
  2. Jonas_Sid

    Jonas_Sid

    Mobile Graphics Developer Unity Technologies

    Joined:
    May 13, 2015
    Posts:
    33
    Hey,
    I was able to reproduce this bug and have sent it to the developers!
    Issuetracker link is currently being generated, so I will post it laters
    Thanks for reporting the issue
    Also, what is wrong with your bug reporter? :)

    Regards,
    Jonas
    Unity QA
     
    Zuntatos likes this.
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    522
    @jonassid It started up fine and worked as intended, but after uploading the data (progress bar went full and disappeared) it froze up. I've checked the obvious places for some sort of crash report or similar, but couldn't find anything.

    Interestingly enough, it seems my bug report did come through, as I got a confirmation mail ~3 hours later. My previous confirmation mails were all within a few minutes, so I thought it didn't work. I made a second bug report ~2 hours after the first one, but now they're both there. 741751 is a duplicate of 741769. (741769 may have a preferably description).

    P.S: Said bug reports are about a canvas bug I also reported about yesterday (the other thread).
     
  4. Jonas_Sid

    Jonas_Sid

    Mobile Graphics Developer Unity Technologies

    Joined:
    May 13, 2015
    Posts:
    33
    Confirmation mails should come at normal rate now once again :)
    However, there is not much I can do about the bug reporter. Did it start recently or maybe it happens on some specific version of Unity? :)
     
  5. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    522
    @jonassid I've only seen it happen on 5.3.0b5, after I updated from 5.2. I've tried 5.3.0b1 earlier on but the bugs I encountered were already confirmed on the forums, so I didn't use the reporter.
     
  6. povilas

    povilas

    Unity Technologies

    Joined:
    Jan 28, 2014
    Posts:
    428
    @Zuntatos

    I believe the bug has been fixed. It's unclear at this time which beta/RC release will contain the fix.
     
    Zuntatos likes this.