Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Semi-random crashes when creating Textures  (Read 17344 times)

0 Members and 3 Guests are viewing this topic.

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #15 on: November 15, 2018, 08:38:10 am »
I tried the SFML.net (C++) test again today and it resulted in a std::bad_alloc: crash... It makes more sense that it crashes the same as the CSFML and SFML.net tests but why in the hell was it working fine the last couple of days when I tested it?? :S
My Windows updated last night but I'm not sure if that would have affected it at all...


I also tried the SFML.Net test today as a 64bit build (everything before was using 32bit) and it manages to create a lot more textures but it still looks like they are being stored in the system RAM and not the GPU RAM.



The working set keeps rising as more textures are made while the GPU memory doesn't budge at all.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #16 on: November 15, 2018, 08:48:33 am »
10 GB of memory used, whereas you said it was hardly using 100 MB in your first posts. What happened?
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #17 on: November 15, 2018, 08:52:32 am »
Well that's using the 64bit version, so that's with hundreds of textures and not just 40 like before. Also I was only looking at Task Manager before (and not the Resource Manager) which still only shows it using less than 100MB.

Edit: The working set for the 32bit version (when it crashes after loading around 40 textures) is 1.5GB
« Last Edit: November 15, 2018, 08:56:11 am by Torrunt »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #18 on: November 15, 2018, 10:16:18 am »
Ok, those results seem consitent. Now the question is: why does SFML.Net keep all that stuff in memory? From a quick look at the code, it only calls sfTexture_create from CSFML, which calls Texture::loadFromFile from SFML.

Can you show the complete source code that you used for your latest .Net test?
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #19 on: November 15, 2018, 10:24:52 am »
Same code as before, just using the 2.2 64-bit dlls from: https://www.sfml-dev.org/download/sfml.net/
Code: [Select]
using System;
using System.Collections.Generic;
using SFML.Graphics;

namespace TextureCrashTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Texture> textures = new List<Texture>();

            for (int i = 0; i < 400; i++)
            {
                Texture texture = new Texture("image.png");
                textures.Add(texture);
                Console.WriteLine(i);
            }
        }
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #20 on: November 15, 2018, 10:58:27 am »
Are you using version 2.2 of CSFML as well?

You should consider switching to 2.5 for everything; you'll have to recompile a few things, and use PR #157 of SFML.Net, but it might be worth it.
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #21 on: November 15, 2018, 11:02:27 am »
I'm using the latest stuff on GitHub. I just downloaded 2.2 to quickly test the 64bit version.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #22 on: November 15, 2018, 11:16:55 am »
So, the exact same code, with everything in version 2.5 (carefully check that, all masters may not be in sync), produces the same result? (except with less images due to less memory space available in 32-bits).
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #23 on: November 15, 2018, 11:22:56 am »
Yep, same results no matter which version of SFML/CSFML/SFML.Net I use.

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #24 on: November 16, 2018, 01:13:14 pm »
Was messing around with my SFML.Net test again today. I can get it to work correctly if I create a new texture (without passing a file name or stream) then use Texture.Bind and OpenGL (glTexImage2D) to load in the texture. Doing that seems to place the textures in the GPU memory and not the system memory.

Here's the code I used:
(just grabbed the code for image loading via DevIL that I use for another project)

Code: [Select]
using System;
using System.Collections.Generic;
using System.IO;
using SFML.Window;
using SFML.System;
using SFML.Graphics;
using Tao.OpenGl;
using DevIL.Unmanaged;

namespace TextureCrashTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IL.Initialize();

            RenderWindow Window = new RenderWindow(new VideoMode(1920, 1080), "test", Styles.Default);
            Window.Size = new Vector2u(1920, 1080);
            Window.Position = new Vector2i(100, 100);

            List<Texture> textures = new List<Texture>();

            for (int i = 0; i < 400; i++)
            {
                Texture texture = GetTexture("image.png");
                textures.Add(texture);
                Console.WriteLine(i);
            }
        }

        private static Texture GetTexture(string filename)
        {
            int imageID = IL.GenerateImage();
            IL.BindImage(imageID);

            IL.Enable(ILEnable.AbsoluteOrigin);
            IL.SetOriginLocation(DevIL.OriginLocation.UpperLeft);

            bool loaded = false;
            using (FileStream fileStream = File.OpenRead(filename))
                loaded = IL.LoadImageFromStream(fileStream);
            if (!loaded)
                return null;

            bool success = IL.ConvertImage(DevIL.DataFormat.RGBA, DevIL.DataType.UnsignedByte);
            if (!success)
                return null;

            int width = IL.GetImageInfo().Width;
            int height = IL.GetImageInfo().Height;

            Texture texture = new Texture((uint)width, (uint)height);
            Texture.Bind(texture);
            {
                Gl.glTexImage2D(
                    Gl.GL_TEXTURE_2D, 0, IL.GetInteger(ILIntegerMode.ImageBytesPerPixel),
                    width, height, 0,
                    IL.GetInteger(ILIntegerMode.ImageFormat), ILDefines.IL_UNSIGNED_BYTE,
                    IL.GetData());
            }
            Texture.Bind(null);

            IL.DeleteImages(new ImageID[] { imageID });

            return texture;
        }

    }
}

I might have to just do a work-around like this for my game for now. Though I probably won't use DevIL since my game only needs to load pngs.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #25 on: November 16, 2018, 01:49:55 pm »
This is exactly what SFML is supposed to do :-\

What if you use Texture.Update instead of Texture.Bind + glTexImage2D?
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #26 on: November 16, 2018, 02:18:05 pm »
Hmmm... using Texture.Update instead doesn't cause a crash but the GPU memory doesn't go up at all while the system memory does. Though the working set stays around 1.3-1.4GB while containing to load textures past 40.

That's with replacing the Texture.Bind code with: texture.Update(IL.GetImageData());


I'll test out and see what happens when I try to load images in via something different than DevIL.

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #27 on: November 19, 2018, 11:46:47 am »
I managed to get texture loading working fine using this:

Code: [Select]
private static Texture GetTexture(Stream stream, IntRect area = default(IntRect))
{
Image image = new Image(stream);
Texture texture = new Texture(image.Size.X, image.Size.Y);

if (area == default(IntRect))
area = new IntRect(0, 0, (int)image.Size.X, (int)image.Size.Y);

Texture.Bind(texture);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, area.Width, area.Height, 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, image.Pixels);
Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, area.Left, area.Top, area.Width, area.Height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, image.Pixels);
Texture.Bind(null);

return texture;
}

I noticed that if I pass null to glTexImage2D instead of the pixel byte array I get the same problem as before.
Seems like SFML in Texture::create, passes null to it as well:
https://github.com/SFML/SFML/blob/ff87e1c92265574f5c9785117b00907b7a61cf7d/src/SFML/Graphics/Texture.cpp#L208

Not sure why that is causing issues, or if that is the true cause or not.