De-Texturing? Removing Textures from VRAM

Blitz3D Forums/Blitz3D Programming/De-Texturing? Removing Textures from VRAM

_PJ_(Posted 2010) [#1]
Is there any way to remove active textures from the memory?
Perhaps by some pokeing of the heap perhaps if addresses could be known?

Whilst generally assuming good tracking of textures is used, this shouldn't be an issue, but perhaps there's a risk that textures (especially those not applied via file or brush) may get left active and forgotten about and the emory gets eaten up it seems without any means to free it aside from destroying the entity it's attached to.

Graphics3D 1024,768,32,6
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global Cam=CreateCamera()
CameraFogRange Cam,0,50
AmbientLight 80,96,84

Global camlight=CreateLight(3)
LightColor camlight,192,192,184
LightConeAngles camlight,5,60
LightRange camlight,10
EntityParent camlight,Cam,1

	Global cube=CreateCube()
	EntityAutoFade cube,5,50
	EntityColor cube,Rand(255),Rand(255),Rand(255)
	x#=Rnd(0.1,2.0)
	ScaleMesh cube,x#,x#,x#
	EntityShininess cube,Rnd(0.4,1.0)
	PositionEntity cube,0,MeshHeight(cube),5

plane=CreatePlane()
EntityAutoFade plane,25,50
Global ptex=CreateTexture(128,128,264)
SetBuffer TextureBuffer(ptex)
ClsColor 192,192,192
Cls
Color 64,64,64
Rect 0,0,64,64,1
Rect 63,63,64,64,1
SetBuffer BackBuffer()

EntityTexture plane,ptex
FreeTexture ptex
ptex=0

MoveEntity Cam,0,1,0
MoveMouse 512,384
While Not KeyDown(1)
	MoveEntity Cam,KeyDown(205)-KeyDown(203),0,KeyDown(200)-KeyDown(208)
	ShowPointer
	TurnEntity Cam,MouseY()-384,512-MouseX(),0,1
	RotateEntity Cam,EntityPitch(Cam,1),EntityYaw(Cam,1),0,1
	MoveMouse 512,384
	HidePointer
	
	If MouseHit(1) Then AddATexture
	If MouseHit(2) Then FreeATexture
	
	UpdateWorld
	RenderWorld

	Color 32,32,32
	Rect 0,0,1024,60,1
	Color 32,255,32
	Text 0,0,"LEFT MOUSE=ADD NEW TEXTURE, RIGHT MOUSE=FREE TEXTURE"
	Text 0,30,Str(ActiveTextures())
	Flip
	Delay 10
Wend
ClearWorld 1,1,1
EndGraphics
End

Function AddATexture()
		If (ptex)
	;		FreeTexture ptex
	;		ptex=0
		End If
		
		ptex=CreateTexture(128,128,264)
		SetBuffer TextureBuffer(ptex)
		Color Rand(255),Rand(255),Rand(255)
		n=Rand(4,128)
		For f=4 To n Step 2
			Oval 64-(f*0.5),64-(f*0.5),f,f,0
		Next
		SetBuffer BackBuffer()
		EntityTexture cube,ptex
End Function

Function FreeATexture()
	If (ptex)
		FreeTexture ptex
		ptex=0
	End If
End Function


In this code, the commented section represents the tracking. Fortunately, this is simplified and very possible here due in part to the use of a single, global, however, its not always practical.


Yasha(Posted 2010) [#2]
You shouldn't be assigning a new texture to a variable without first freeing the texture that's in it. Textures internally maintain a reference count, so the texture itself will survive as long as entities are making reference to it internally. EDIT: Actually looking at your code, I see you are aware of this part.

If you really want to get rid of the texture without destroying the entity, you can just replace it - the reference counter will detect that there are no more references (it gets engaged on replacement of a texture) and free the texture. Add a FreeTexture call to the last line of AddATexture() to see this in action.


_PJ_(Posted 2010) [#3]
Right.. usually in "normal" practiec, if its a static texture, I do free them after they are applied like that.
This would still always leave an ative texture on any object that has been textured, but y'know, I kinda just realised, I suppose its 'dumb' to think of the single remaining active texture on the cube as eating up memory, even if there were hundreds or thousands of cubes, since even without textures, they would have a graphical presence, whether coloured, lit by light or the generic, default blank whiteness I suppose a single active texture is just as memory intensive as this default "skin" more or less ?


Dreamora(Posted 2010) [#4]
the default skin does require 0.0kb in memory as there is nothing on them. its plain vertex color interpolation.

with a texture its the vertex color interpolation + your texture data


_PJ_(Posted 2010) [#5]
So is there any way to clear that memory without destroying the entity?
What about perhaps destroying the surface(s) or UV info perhaps?


Rroff(Posted 2010) [#6]
AFAIK the only way to "detexture" a mesh is to have a blank default texture and use the no blend blend - but this screws up on some stuff.


Dreamora(Posted 2010) [#7]
you can replace the brush and free the original one. then the reference is gone too


_PJ_(Posted 2010) [#8]
Thanks everyone.

Whilst it's quick and easy to just use EntityTexture, certainly with Brushes there's a lot more control, so it seems Brushes are the way to go for sure!

Thanks a lot :)