You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have verified that I am running the latest version of ImageSharp
I have verified if the problem exist in both DEBUG and RELEASE mode
I have searched open and closed issues to ensure it has not already been reported
Description
I'm seeing AOT-related exceptions using ImageSharp in Unity3D. This is somewhat expected since Unity IL2CPP performs AOT compilation. However, I had expected these errors to be largely mitigated in ImageSharp 1.0.3.
This is fixed -- see different AOT error in thread below. System.ExecutionEngineException: Attempting to call method 'SixLabors.ImageSharp.Formats.Png.PngDecoderCore::Decode<SixLabors.ImageSharp.PixelFormats.Rgba32>' for which no ahead of time (AOT) code was generated.
System Configuration
Unity IL2CPP AOT Compilation in Unity 2021.1.x.
ImageSharp 1.0.3
Thanks you! It looks like that resolves some of the AOT issues but this one remains (Drawing.DrawImageProcessor+ProcessorFactoryWorker~1:
System.ExecutionEngineException: Attempting to call method 'SixLabors.ImageSharp.Processing.Processors.Drawing.DrawImageProcessor+ProcessorFactoryVisitor`1<SixLabors.ImageSharp.PixelFormats.Rgba32>::Visit<SixLabors.ImageSharp.PixelFormats.Rgba32>' for which no ahead of time (AOT) code was generated.
at SixLabors.ImageSharp.Image`1[TPixel].Accept (SixLabors.ImageSharp.Advanced.IImageVisitor visitor) [0x00000] in <00000000000000000000000000000000>:0
at SixLabors.ImageSharp.Processing.Processors.Drawing.DrawImageProcessor.CreatePixelSpecificProcessor[TPixelBg] (SixLabors.ImageSharp.Configuration configuration, SixLabors.ImageSharp.Image`1[TPixel] source, SixLabors.ImageSharp.Rectangle sourceRectangle) [0x00000] in <00000000000000000000000000000000>:0
at SixLabors.ImageSharp.Processing.DefaultImageProcessorContext`1[TPixel].ApplyProcessor (SixLabors.ImageSharp.Processing.Processors.IImageProcessor processor, SixLabors.ImageSharp.Rectangle rectangle) [0x00000] in <00000000000000000000000000000000>:0
at SixLabors.ImageSharp.Processing.DefaultImageProcessorContext`1[TPixel].ApplyProcessor (SixLabors.ImageSharp.Processing.Processors.IImageProcessor processor) [0x00000] in <00000000000000000000000000000000>:0
at Reproduction+<>c__DisplayClass2_1.<RunTest>b__1 (SixLabors.ImageSharp.Processing.IImageProcessingContext o) [0x00000] in <00000000000000000000000000000000>:0
at System.Action`1[T].Invoke (T obj) [0x00000] in <00000000000000000000000000000000>:0
at Reproduction.RunTest () [0x00000] in <00000000000000000000000000000000>:0
We already have seeded DrawImageProcessor here, but i guess we need to seed DrawImageProcessor<TPixelBg, TPixelFg> also? Im not so familiar with AOT issues.
The existing logic in AotCompilerTools appears to define types without instantiation which is elegant compared to my instantiation hack job. I'm similarly ignorant of how AOT compilation resolves types.
@stonstad can you provide sample source code please? Pretty sure you're using 2 different pixel types (it's not forbidden, it'll just narrow the suspect range :P).
I'm not very familiar with AOT but just from my understanding of generics: MyClass<TGetClass1> and MyClass<TGenClass2> share same code thus AOT should work fine resolving types for actually any reference type.
But ImageSharp's pixel type system consists of values types only and MyClass<PixelType1>/MyClass<PixelType2> cannot share single codegen. And that's where the problem lies:
Drawing processor uses 2 generic value types so we actually need to populate each pair of pixels. For example, code above uses DrawImageProcessor which draws Rgba32 over Rgba32. So this visitor code won't be populated:
// Create Visitor with source type TPixel1varvisitor=newProcessorFactoryVisitor<TPixelBg>(configuration,this,source,sourceRectangle);// Visit image of unknown TPixel2 type which cannot be compiled AOT with current implementationthis.Image.AcceptVisitor(visitor);
I guess you called it somewhere with AotCompileDrawImageProcessor<Rgba32, Rgba32>() type parameters? @stonstad
It should be enough to quick-fix this issue for this specific case of rgba32 -> rgba32, AotCompileDrawImageProcessor<TPixel> call is not needed - it's already populated with current aot implementation.
<TPixel, TPixel> should be threated as a hack for quick fixing this issue. Global solution for this issue is to compile each pair of pixel types for each paired generic type which is not really a good solution for two reasons:
ImageSharp currently has 28 pixel types (and 1 more as feature request), just the number of permutations is 28!/26! = 756 seeding calls for each type using 2 pixel types, just typing that is not really a fun thing to do
I would expect aot binary size to explode thanks to the point above
I can't really think of any viable solution other than separate Roslyn-based compiler for AOT users.
As a trade off IL2CPP by default does not do an exhaustive search for generic virtual implementations - although it is possible to do so. This was done to reduce executable size and conversion time, since the search will find all implementations that could be called, but likely aren’t. This also matched what Mono AOT did at the time and supports most Unity content.
Unity 2021.1 and above IL2CPP supports a --generic-virtual-method-iterations that will perform a deeper search for generic virtual usages and their needed implementations. It appears that for the ImageSharp, at least of the example that @stonstad submitted, a value of 5 is sufficient. IL2CPP command line arguments can set by using the environment variable IL2CPP_ADDITIONAL_ARGS environment variable or using an editor script:
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class SetIl2cppSettings : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
PlayerSettings.SetAdditionalIl2CppArgs("--generic-virtual-method-iterations=5");
}
}
Unity 2021.2 adds a new IL2CPP Code Generation build setting - "Faster (smaller) builds" that will not have this limitation. This mode generates generic code that can work with any type, so it has no limitations on generic instances, although this comes with a runtime performance cost.
As we are looking to release to store this week, and this ticket has been open for two months, we are stripping ImageSharp out of the app for the time being, and substituting an alternative.
We do look forwards to a fix, however, as we like the library very much.
We discovered this during a Release-To-Store-Candidate QA release and unfortunately we haven't been able to get an internal ticket authorised by our product owner to pursue this, his exact words were '... just rip it out and replace it and get on with the release [to store] ...'.
I've added it to my own tech-debt list but looking at one of the comments in the link above, I'm not hopeful that Xamarin [Microsoft] will fix this because of the pending .Net Maui release, I suspect that all of the effort with just two months to go before the GA is being put into .Net Maui, it's associated Blazor integration, and Visual Studio 2022, all of which we are also working with, so I'm afraid this is a low priority for us.
The good news for SixLabors is that Xamarin will only be supported for another year or so, at which point his bug will just disseaper into obscurity.
I do accept it's a Xamarin Compiler issue, and not a SixLabors issue, however, so apologies for us not pursuing it further with them and leaving it in your lap.
Best wishes!
Anthony
EDIT: Thanks for the thumbs up, I just thought I'd add the exact code that caused it to throw in the IValueConverter:
using (Image<Rgba32> imageImageSharp = SixLabors.ImageSharp.Image.Load<Rgba32>(imageBytes))
{
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var displayWidth = mainDisplayInfo.Width;
var density = mainDisplayInfo.Density;
var width = imageImageSharp.Width;
var height = imageImageSharp.Height;
_height = ((displayWidth - (40 * density)) / width * height) / density;
}
Yep so the people involved in that conversation were, shall we say.... Less than helpful.
I know a lot of work has gone into the AOT Compiler for .NET 6 so it could be that this issue simply goes away, (I'm actually very confident following Michal's comments).
It's incredibly frustrating to have these issues and we've put in a ton of effort to try and workaround them. It could be that the workaround above could be a fix enough for the short term but I'd need someone with the tooling to test it (I don't personally work with Xamarin). If we can get someone to do that I will ship it with the next release.
The good news is that I have audited our code base, and although ImageSharp is used quite a lot, it only throws in the IValueConverter above, so I'll put a temporary fix in there using another library, and revisit this and put the ImageSharp code back in when it is finally fixed. It is, after all, a splendid library, and I want to keep using it in my .Net Maui Blazor work.
Activity
stonstad commentedon Jul 16, 2021
I now see that this is aligned with the 1.10 milestone. If ready, is there a way to test a pre-release in Unity3D to confirm resolution?
brianpopow commentedon Jul 16, 2021
You can try our alpha builds from the MyGet feed
stonstad commentedon Jul 16, 2021
Thanks you! It looks like that resolves some of the AOT issues but this one remains (Drawing.DrawImageProcessor+ProcessorFactoryWorker~1:
brianpopow commentedon Jul 16, 2021
We already have seeded DrawImageProcessor here, but i guess we need to seed
DrawImageProcessor<TPixelBg, TPixelFg>also? Im not so familiar with AOT issues.stonstad commentedon Jul 16, 2021
I'm not familar with the conventions the ImageSharp team uses. The following addition (hack) to AotCompilerTools.cs resolves the issue for me:
[-]PNGEncoder/JPEGEncoder AOT Issue[/-][+]DrawImageProcessor AOT Issue[/+]brianpopow commentedon Jul 16, 2021
@stonstad would you mind creating a PR?
stonstad commentedon Jul 16, 2021
The existing logic in AotCompilerTools appears to define types without instantiation which is elegant compared to my instantiation hack job. I'm similarly ignorant of how AOT compilation resolves types.
br3aker commentedon Jul 16, 2021
@stonstad can you provide sample source code please? Pretty sure you're using 2 different pixel types (it's not forbidden, it'll just narrow the suspect range :P).
stonstad commentedon Jul 16, 2021
br3aker commentedon Jul 16, 2021
Thanks!
I'm not very familiar with AOT but just from my understanding of generics:
MyClass<TGetClass1>andMyClass<TGenClass2>share same code thus AOT should work fine resolving types for actually any reference type.But ImageSharp's pixel type system consists of values types only and
MyClass<PixelType1>/MyClass<PixelType2>cannot share single codegen. And that's where the problem lies:ImageSharp/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs
Lines 15 to 19 in 61b137d
Drawing processor uses 2 generic value types so we actually need to populate each pair of pixels. For example, code above uses
DrawImageProcessorwhich drawsRgba32overRgba32. So this visitor code won't be populated:That explains why this solves the issue:
I guess you called it somewhere with
AotCompileDrawImageProcessor<Rgba32, Rgba32>()type parameters? @stonstadIt should be enough to quick-fix this issue for this specific case of rgba32 -> rgba32,
AotCompileDrawImageProcessor<TPixel>call is not needed - it's already populated with current aot implementation.stonstad commentedon Jul 16, 2021
Yes, I'm calling via Seed w/ AotCompileDrawImageProcessor<TPixel, TPixel>() and this works.
br3aker commentedon Jul 17, 2021
<TPixel, TPixel>should be threated as a hack for quick fixing this issue. Global solution for this issue is to compile each pair of pixel types for each paired generic type which is not really a good solution for two reasons:28!/26! = 756seeding calls for each type using 2 pixel types, just typing that is not really a fun thing to doI can't really think of any viable solution other than separate Roslyn-based compiler for AOT users.
9 remaining items
stonstad commentedon Jul 20, 2021
Already done! After reading @MichalStrehovsky's helpful comment I updated the Unity ticket with a link to this thread and cited his helpful analysis.
JimBobSquarePants commentedon Jul 20, 2021
Do we have a link to the actual Unity ticket?
stonstad commentedon Jul 20, 2021
JimBobSquarePants commentedon Jul 20, 2021
Ah ok, no problem
stonstad commentedon Aug 6, 2021
Unity: We successfully reproduced this issue. It will be possible to follow the progress and a chosen resolution in our public Issue Tracker.
https://issuetracker.unity3d.com/issues/system-dot-executionengineexception-error-in-l2cpp-build
scott-ferguson-unity commentedon Aug 11, 2021
As a trade off IL2CPP by default does not do an exhaustive search for generic virtual implementations - although it is possible to do so. This was done to reduce executable size and conversion time, since the search will find all implementations that could be called, but likely aren’t. This also matched what Mono AOT did at the time and supports most Unity content.
Unity 2021.1 and above IL2CPP supports a
--generic-virtual-method-iterationsthat will perform a deeper search for generic virtual usages and their needed implementations. It appears that for the ImageSharp, at least of the example that @stonstad submitted, a value of 5 is sufficient. IL2CPP command line arguments can set by using the environment variableIL2CPP_ADDITIONAL_ARGSenvironment variable or using an editor script:Unity 2021.2 adds a new IL2CPP Code Generation build setting - "Faster (smaller) builds" that will not have this limitation. This mode generates generic code that can work with any type, so it has no limitations on generic instances, although this comes with a runtime performance cost.
HarrisonOfTheNorth commentedon Sep 13, 2021
We are getting a similar error in Xamarin Forms:
It only throws in Release Mode, it does not occur in Debug mode.
It occurs in an IValueConverter, where we merely needed to extract the height of the image.
The libraries that we have loaded are as follows:
As we are looking to release to store this week, and this ticket has been open for two months, we are stripping ImageSharp out of the app for the time being, and substituting an alternative.
We do look forwards to a fix, however, as we like the library very much.
JimBobSquarePants commentedon Sep 13, 2021
Have you raised the issue with Xamarin? Without people raising issues we’ll never get them to fix their compiler.
HarrisonOfTheNorth commentedon Sep 13, 2021
Hi Jim,
No, we haven't raised it with Xamarin, I found the same issue here (closed without a fix or work-around):
SixLabors.ImageSharp: issue 1708
We discovered this during a Release-To-Store-Candidate QA release and unfortunately we haven't been able to get an internal ticket authorised by our product owner to pursue this, his exact words were '... just rip it out and replace it and get on with the release [to store] ...'.
I've added it to my own tech-debt list but looking at one of the comments in the link above, I'm not hopeful that Xamarin [Microsoft] will fix this because of the pending .Net Maui release, I suspect that all of the effort with just two months to go before the GA is being put into .Net Maui, it's associated Blazor integration, and Visual Studio 2022, all of which we are also working with, so I'm afraid this is a low priority for us.
The good news for SixLabors is that Xamarin will only be supported for another year or so, at which point his bug will just disseaper into obscurity.
I do accept it's a Xamarin Compiler issue, and not a SixLabors issue, however, so apologies for us not pursuing it further with them and leaving it in your lap.
Best wishes!
Anthony
EDIT: Thanks for the thumbs up, I just thought I'd add the exact code that caused it to throw in the IValueConverter:
JimBobSquarePants commentedon Sep 13, 2021
Yep so the people involved in that conversation were, shall we say.... Less than helpful.
I know a lot of work has gone into the AOT Compiler for .NET 6 so it could be that this issue simply goes away, (I'm actually very confident following Michal's comments).
It's incredibly frustrating to have these issues and we've put in a ton of effort to try and workaround them. It could be that the workaround above could be a fix enough for the short term but I'd need someone with the tooling to test it (I don't personally work with Xamarin). If we can get someone to do that I will ship it with the next release.
HarrisonOfTheNorth commentedon Sep 13, 2021
The good news is that I have audited our code base, and although ImageSharp is used quite a lot, it only throws in the IValueConverter above, so I'll put a temporary fix in there using another library, and revisit this and put the ImageSharp code back in when it is finally fixed. It is, after all, a splendid library, and I want to keep using it in my .Net Maui Blazor work.
Thanks again, and Ciao for now!
JimBobSquarePants commentedon Jan 23, 2023
According to the linked issue in the public tracker this is fixed.
https://issuetracker.unity3d.com/issues/system-dot-executionengineexception-error-in-l2cpp-build