Openb3d (FreeTexture crash)

Started by markcwm, June 12, 2017, 16:48:52

Previous topic - Next topic

Krischan

Good to hear Mark, and I really hope that OpenB3D becomes the real successor to miniB3D which has been never finished - if you have time for it. I'm not very familar with the C++ subroutines to help finding the deeper bugs but I can do the testing, and my current project already uses a lot of the OpenB3D functions.
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Krischan

#31
Umm Mark, I'm not sure but I think I've found the solution for our problem. Your version could be ok if you can confirm this: just switch the FreeEntity/FreeTexture order in my demo and it'll run fine, I only had one (!) crash and I couldn't reproduce this one again. But I don't trust this solution so please try it yourself.

If it works I can't believe I've overseen this the last days as it makes sense to clear the texture first and then free the entity (to which the texture has been assigned!). I don't know why this worked flawless in miniB3D.

Wrong:
c.ent.FreeEntity
c.tex.FreeTexture

Right:
c.tex.FreeTexture
c.ent.FreeEntity

or to make sure the texture will be cleared from VRAM, see below:
c.tex.FreeTexture
glDeleteTextures 1, TTexture(c.tex).texture
c.ent.FreeEntity

Just one thing: my VRAM usage in the Open Hardware Monitor stays constant at 14,7% (I have 4GB VRAM) which is 0,6% more than before if I add the glDeleteTextures command. If I don't add it, the usage climbs up to 16.7% (see attached image). Perhaps you should add this command to the Freetexture routine to really free up the VRAM from all deleted textures?

Here's the complete test source I've used (with the glDeleteTextures command disabled)
Code (BlitzMax) Select
SuperStrict

Framework openb3d.B3dglgraphics
'Framework sidesign.minib3d

Import brl.tgaloader
Import brl.Map

Graphics3D(1024, 600)

Global cam:TCamera = CreateCamera()
Global BSP:TBSP = New TBSP

Print "Start: " + GCMemAlloced()

Local ms:Int = MilliSecs()

For Local i:Int = 1 To 100

Print "Run #" + i
BSP.AddAll
BSP.DeleteAll

Next

Print MilliSecs() - ms

End

Type TBSP

Field pivot:TPivot
Field candle:TCandle

Field candlemap:TMap
Global candlelist:TList

Method New()

candlemap = CreateMap()
candlelist = CreateList()

End Method

Method AddAll()

Local num:Int = Rand(100, 1000)

For Local i:Int = 1 To num

BSP.AddCandle(i)

Next

GCCollect()
Print "Created " + num + " Candles"': " + GCMemAlloced() + " Bytes used"

End Method

Method DeleteAll()

Local num:Int

For Local c:TCandle = EachIn candlelist

DeleteCandle(c)
num:+1

Next

GCCollect()
Print "Deleted " + num + " Candles: " + GCMemAlloced() + " Bytes used"

End Method

Method AddCandle(id:Int)

Local c:TCandle = New TCandle
c.id = id
c.ent = CreateSprite()
c.tex = LoadTexture("candle.tga")
c.x = Rnd(-100, 100)
c.y = Rnd(-10, 10)
c.z = Rnd(-100, 100)

PositionEntity c.ent, c.x, c.y, c.z

MapInsert(candlemap, String(c.id), c)

End Method

Method DeleteCandle(c:TCandle)

MapRemove(candlemap, String(c.id))
ListRemove(candlelist, c)

c.tex.FreeTexture
'glDeleteTextures 1, TTexture(c.tex).texture
c.ent.FreeEntity

c = Null

End Method

End Type

Type TCandle

Method New()

ListAddLast(TBSP.candlelist, Self)

End Method

Field id:Int
Field ent:TSprite
Field tex:TTexture
Field parent:TEntity
Field child:TEntity
Field x:Float
Field y:Float
Field z:Float

End Type


and the fixed Method in TTexture.bmx:
Code (BlitzMax) Select
' SMALLFIXES New function from www.blitzbasic.com/Community/posts.php?topic=88263#1002039
Method FreeTexture()

If exists
ListRemove( tex_list,Self ) ; tex_list_id:-1

FreeTexture_( GetInstance(Self) )
FreeObject( GetInstance(Self) )
glDeleteTextures 1, texture
exists=0
EndIf

End Method
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

markcwm

#32
No I tried FreeTexture before FreeEntity and it's not a solution, also you don't want to have to do this as like you say, it makes no sense as entity and texture are different types.

I do have a solution though, the problem is in the c++, the textures are stored in tex_list and repeated entries of texture objects are prevented. So if you load the same texture again and then delete that reference, the second texture needs this reference so it crashes. When I removed this feature it stopped crashing, and I've been able to make it crash consistently. This doesn't happen in Minib3d because Tlist is more stable than c++ lists. So the fix is to have an extra list for repeat textures so it knows how many there are to delete.

Also, yes I think we need glDeleteTextures in there as it is in Minib3d and you have proved it does something, so thanks. I just need to get a commit out now.

Krischan

Sounds good, looking forward to a fixed version to play with :-D
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

markcwm

#34
It's out, have fun. Hope it works, it did seem to on my initial test.

It has glDeleteTextures in FreeTexture and is managed by a new list named tex_list_all to check for repeat texture entries.

If you wonder how this relates to shader material textures, they don't get added to a list and can't be deleted other than with FreeShader.

Edit: oops, it still crashes.

markcwm

#35
I really think I've nailed this bug down now Krischan! I forgot to add to FreeTexture in TTexture.bmx and I made a mistake in how to add tex_list_all in texture.cpp. Other than that I believe my theory was correct! Tested it well and it runs nicely.

It's inspiring to know I can come back after a year and get into coding again quite easily, it's all still there in the mind, I suppose you could say it is "lurking". I was lurking quite often last year. lol

Krischan

Good job Mark, it seems to work now! Unfortunately I've got a lot of work this week so I only can take a deeper look at the weekend to continue working on my Legend of Faerghail project and finish the media and level loader/unloader.
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Naughty Alien

..guys..which version i have to download?? Im using BMX vanilla 1.5 .. i did try https://github.com/markcwm/openb3d.mod ,
but it just doesnt work and i cant compile modules(using blide)..can you point me at proper version, please..

Krischan

#38
I'm using BlitzMax OS 1.52 with Mark's latest OpenB3D version, TDM GCC 5.102 (MinGW). This is the latest "Non-BlitzmaxNG" setup. As BLIde is not 100% compatible to Blitzmax NG yet you should use try this setup first unless you need 64bit support.

You can download my precompiled Blitzmax Installation for Windows, don't forget to set the MinGW environment variable/path to the MinGW subfolder. It includes a batch in the "bin" folder to recompile a new version from Mark and works with BLIde's latest version under Windows 10 (but you shouldn't (re)compile modules using BLIde anymore).
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Naughty Alien

..i did exactly as you suggested, and this is error im having now..



Krischan

#40
The 11th commandment: Thou shalt not build modules with BLIde anymore (unless there is a new patch, if ever).



And make sure your environment settings match the MinGW path (press Win+Pause, in my setup it is for example c:\Apps\Blitzmax\MinGW):
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Naughty Alien

..that is fine, i have set environment variable...now what would be 'manual' way of compiling mod which should do job?

Krischan

#42
Go to the BIN subfolder, open a commandline window and type (or create a Batch file and execute it)

bmk makemods -a openb3d

Beside that I tried again building modules in BLIde using my setup I've uploaded (see previous posts) and it still worked, so there must be something wrong with your setup. If it still doesn't work you can try my setup. But compiling modules in BLIde won't work correct if you're using Blitzmax NG (and BLIde has no option to build x64 yet).

You can use BLIde to check if your MinGW setup is correct (Tools > Configure the MinGW compiler):
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Naughty Alien

..okay..now, modules are built without error, however, i cant run example as im getting this error..


Krischan

There is still something wrong with your setup. Did you try my Blitzmax installation?
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects