0
votes

I'm getting this runtime error: InvalidProgramException: Invalid IL code. I'm using unity 5.3.1f1. The project is an editor extension. A simplified version of the code is:

public unsafe class PPAGraph
{
   PaddedImage downSampleImage;
   int downSampleRate;
   internal void  Calculate(PaddedImage sourceImage)
   {
     sourceImage.DownSample(downSampleImage, downSampleRate); 
   } 

This error occurs on this line.

InvalidProgramException: Invalid IL code in Assets.UPlus.TerrEngine.PaddedImage:DownSample (Assets.UPlus.TerrEngine.PaddedImage,int): IL_00a9: stloc.s 15

Assets.UPlus.TerrEngine.PPAGraph.Calculate (Assets.UPlus.TerrEngine.PaddedImage sourceImage, Boolean isRidge, Boolean sketch, Int32 plength, Int32 portion) (at Assets/UPlus/TerrEngine/Engine/PPA/PPAGraph.cs:1311) Assets.UPlus.Utils.TerraGodContext.CalcSketchPpa (Assets.UPlus.TerrEngine.PaddedImage sketchImg, Int32 sketchDownSampleRate) (at Assets/UPlus/TerrEngine/UnityEngine/TerraGodContext.cs:70) EditorExtensions.Editor.TerrainGodMainPanel.CreatePanel () (at Assets/UPlus/TerrEngine/Engine/Editor/TerrainGODMainPanel.cs:45) EditorExtensions.Editor.TerrainGODWindow.OnGUI () (at Assets/UPlus/TerrEngine/Engine/Editor/TerrainGODWindow.cs:39) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

The DownSample method is:

        public void DownSample(PaddedImage downSampImg, int downsampleRate)
    {
        int dsW = downSampImg.Width;
        int dsH = downSampImg.Height;
        float* dsPix = downSampImg.Pixels,padDSPix=downSampImg.PaddedPixels;
        int pad = Padding;
        int twoPad = pad + pad;
        float* rowMyPix = PaddedPixels;
        rowMyPix += pad*PaddedWidth + pad;
        float* myPix;
        int yStep = downsampleRate * PaddedWidth;
        int dsPad = downSampImg.Padding;
        int twoDSPad = dsPad + dsPad;
        padDSPix += downSampImg.PaddedWidth*dsPad + dsPad;
        if (downSampImg.PixelsPtr != IntPtr.Zero)
        {
            for (int y = 0; y < dsH; y++)
            {
                myPix = rowMyPix;
                for (int x = 0; x < dsW; x++)
                {
                    *padDSPix++ = *dsPix++ = *myPix;
                    myPix += downsampleRate;
                }
                padDSPix += twoDSPad;
                rowMyPix += yStep;
            }
        }
        else
        {
            for (int y = 0; y < dsH; y++)
            {
                myPix = rowMyPix;
                for (int x = 0; x < dsW; x++)
                {
                    *padDSPix++ = *dsPix++ = *myPix;
                    myPix += downsampleRate;
                }
                padDSPix += twoDSPad;
                rowMyPix += yStep;
            }
        }
    }
1
Are you running Unity with Mono on Linux? - Thomas Weller
No it's on windows 7. The project was fine before but after some code changes and also downgrading my visual studio to 2013 and resharper to 9 the error showed up. I don't know if the code changes where th reason or if the downgrade was. Then I installed vs 2015 and resharper 10 again and the error remained. Tried a couple of unityvs versions and even installed a fresh windows. And made a package out of the project and created another project and imported the package. This didn't solve the issue either. - morteza khosravi
I am wondering because of at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs - Thomas Weller
That's weird. I'll install unity and VSTU again and will create a new project to see if that solves the problem - morteza khosravi
Is it possible that you changed compiler options such as changing the architecture. I've seen this sort of thing when attempting to run 64-bit code in a 32-bit process and vice-versa (using plug-ins, not Unity specifically). - CodingGorilla

1 Answers

0
votes

Found the solution. It was not about project setup.

The problem is with line *padDSPix++ = *dsPix++ = *myPix;. Converting it to two seperate statements solved the issue.

pixH = *myPix; 
*padDSPix++ = pixH;
*dsPix++ = pixH; 

It's interesting to see how much the error message is general and misleading.