Fixed glAdjustTexSize()

BlitzMax Forums/BlitzMax Module Tweaks/Fixed glAdjustTexSize()

JoshK(Posted 2007) [#1]
BRL's glAdjustTexSize() function has a few problems.

-I occasionally get the "Unable to calculate tex size" error on my GE Force 8800.

-The function requires feedback from the GPU. This means the GPU has to completely finish what it is doing, and then send the requested data back to the CPU. Syncing the GPU with the CPU like this should be avoided whenever possible.

Rem
bbdoc: Helper function to calculate nearest valid texture size
about: This functions rounds @width and @height up to the nearest valid texture size
End Rem
Function GLAdjustTexSize( width Var,height Var )
	Global MaxTextureSize:Int
	If MaxTextureSize=0 glGetIntegerv GL_MAX_TEXTURE_SIZE,Varptr MaxTextureSize
		
	width=Pow2Size( width )
	height=Pow2Size( height )
	width=Max(1,width)
	width=Min(width,MaxTextureSize)
	height=Max(1,height)
	height=Min(height,MaxTextureSize)

	Function Pow2Size( n )
		Local t=1
		While t<n
			t:*2
		Wend
		Return t
	End Function
	
	Rem
	Repeat
		Local t
		glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
		glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t
		If t Return
		If width=1 And height=1 RuntimeError "Unable to calculate tex size"
		If width>1 width:/2
		If height>1 height:/2
	Forever
	EndRem

End Function



Mahan(Posted 2008) [#2]
First of all I'm very sorry to put up my necromantic powers on display, waking up this old thread ...

... BUT ...

... This is not mere a module tweak as this forum would suggest but It's an fix that actually fixes a known bug in the brl imho.

I got two computers at home for blitzmax development and testing and on one the original GLAdjustTexSize (in the brl) works, but on the other one it fails with the error mentioned in the original post.

Using this version of the function the program works nicely on both.

Looking at the code of this proposed version above I just cannot see how it could fail on any computer (please correct me if I'm wrong here).

My proposition is therefor that this bugfix should go into the brl in a future release of BlitzMax.


slenkar(Posted 2009) [#3]
yes this is a bugfix and should go into the official source


slenkar(Posted 2010) [#4]
still not in there :)


jkrankie(Posted 2010) [#5]
This one doesn't work on older intel GMA chips, maybe that's why it's not been added?

Cheers
Charlie


JoshK(Posted 2010) [#6]
Actually, the proxy texture approach is the most correct way to test textures, since the max size can change depending on the texture format. If that doesn't work on some cards, then a workaround needs to be coded to try to compensate. But the proxy texture is supposed to be the right way to do it, according to the gl spec.


SLotman(Posted 2010) [#7]
I had this exact same problem on a Intel GMA - even posted about it on miniB3D forum - the fix for me was this:



This works on 2 Intel GMA (I think models 330 and 8245), on Geforce 6200, 8800, 9300 and on ATI X1300. Without this, a simple "drawtext" on top of minib3d was failing.