Would this be faster than using a TList

BlitzMax Forums/BlitzMax Programming/Would this be faster than using a TList

Leiden(Posted 2006) [#1]
Hi,

I've heard alot about how TLists are slow and such, so I devised an alternative way for me to store Vertex's for a Surface. Basically Vertex information is stored in a TBank and the individual values (such as xyz, rgb, normal info etc) are accessed using PeekFloat with the correct offset. I have managed to put together a working example of the code. One thing I need to know, Is my method of storing Vertex's in a TBank faster than using a TList?

Thanks,

Test.bmx:
Const OFS_VERTEXX 	= 0
Const OFS_VERTEXY 	= 4
Const OFS_VERTEXZ 	= 8
Const OFS_VERTEXU 	= 12
Const OFS_VERTEXV 	= 16
Const OFS_VERTEXW 	= 20
Const OFS_VERTEXR 	= 24
Const OFS_VERTEXG 	= 28
Const OFS_VERTEXB 	= 32
Const OFS_VERTEXA 	= 36
Const OFS_VERTEXNX 	= 40
Const OFS_VERTEXNY 	= 44
Const OFS_VERTEXNZ 	= 48

Type TSurface
	Field Vertices			: TBank = CreateBank(0)
	Field VerticesOffset	: Int
	Field Count				: Int
	Field Triangles			: Int
	
	Method AddTriangle(V1:TVertex, V2:TVertex, V3:TVertex)
		AddVertex(V1)
		AddVertex(V2)
		AddVertex(V3)
	End Method
	
	Method AddVertex(V:TVertex)
		ResizeBank(Vertices, VerticesOffset + 48)
		PokeFloat(Vertices, VerticesOffset, V.X)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.Y)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.Z)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.U)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.V)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.W)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.R)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.G)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.B)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.A)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.NX)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.NY)
		VerticesOffset :+ 4
		PokeFloat(Vertices, VerticesOffset, V.NZ)
		
		Count :+ 1
		Triangles = Count / 3
	End Method
	
	Method Render()
		For Local I:Int = 0 To Count - 1
			glTexCoord2f(PeekFloat(Vertices, 48 * I + OFS_VERTEXU), PeekFloat(Vertices, 48 * I + OFS_VERTEXV))
			glColor4f(PeekFloat(Vertices, 48 * I + OFS_VERTEXR), PeekFloat(Vertices, 48 * I + OFS_VERTEXG), PeekFloat(Vertices, 48 * I + OFS_VERTEXB), PeekFloat(Vertices, 48 * I + OFS_VERTEXA))
			glNormal3f(PeekFloat(Vertices, 48 * I + OFS_VERTEXNX), PeekFloat(Vertices, 48 * I + OFS_VERTEXNY), PeekFloat(Vertices, 48 * I + OFS_VERTEXNZ))
			glVertex3f(PeekFloat(Vertices, 48 * I + OFS_VERTEXX), PeekFloat(Vertices, 48 * I + OFS_VERTEXY), PeekFloat(Vertices, 48 * I + OFS_VERTEXZ))
		Next
	End Method
End Type

Type TVertex
	Field X:Float, Y:Float, Z:Float
	Field U:Float, V:Float, W:Float
	Field R:Float, G:Float, B:Float
	Field A:Float
	Field NX:Float, NY:Float, NZ:Float
End Type


Example.bmx:
SuperStrict

Import "Test.bmx"

Global Event:Int
GLGraphics 800, 600, 0

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 1.33333, 1, 1000)

Global S:TSurface = New TSurface
Global V:TVertex = New TVertex

V.X = 0
V.Y = 1
V.Z = -10
V.R = 1
V.G = 1
V.B = 0.1
S.AddVertex(V)

V.X = -1
V.Y = -1
V.Z = -10
S.AddVertex(V)

V.X = 1
V.Y = -1
V.Z = -10
S.AddVertex(V)

V.X = -1
V.Y = 1
V.Z = -10
S.AddVertex(V)

While(Event <> EVENT_APPTERMINATE)
	PollSystem()
	PollEvent()
	Event = EventID()
	
	glClear(GL_COLOR_BUFFER_BIT)
	
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	
	glBegin(GL_POLYGON)
	S.Render()
	glEnd()
	
	GLDrawText "Triangles: " + S.Triangles, 10, 10
	
	Flip
	glFlush()
Wend
End


Thanks in advance,


TartanTangerine (was Indiepath)(Posted 2006) [#2]
Whilst this maybe faster on the CPU, your bottleneck is almost certainly the BUS to the GPU. So in rendering terms I doubt you will see any difference in speeds.


Leiden(Posted 2006) [#3]
Mhmm, excellent point. This would mean that on a computer with a slower cpu and fast graphics card there would be less chance of a bottle necking in the GPU Bus.


TartanTangerine (was Indiepath)(Posted 2006) [#4]
Depends.... A slow CPU is usually sitting on a old motherboard which is either 1) Pre-AGP 2) Only AGPx1 and 3) Has a slow FSB.

You would also be better off stick all that stuff into a single Vertex Array and Renering that.


Leiden(Posted 2006) [#5]
Like This?
Local Vertices:Float[] = [1.0, -0.5, 0.5, 1.0, 0.5, -1.0, 0.5, 1.0, -1.0, 0.5, -1.0, 1.0, 1.0, 1.0, 0.5]

GLGraphics 800, 600, 0

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 1.33333, 1, 1000)

While(1)
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	
	glTranslatef( 0.0, 0.0, -5.0 )
	glPolygonMode(GL_FRONT,GL_LINE)
	glPolygonMode(GL_BACK,GL_FILL)
	
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	
	glEnableClientState(GL_VERTEX_ARRAY)
	glVertexPointer(3,GL_FLOAT,0,Vertices);
	
	glPushMatrix()
	glRotatef(50, 0, 0, 1)
	glTranslatef(1, 0, 0)
	glBegin(GL_POLYGON)
	For i:Int = 0 To 6
		glArrayElement(i)
	Next
	glEnd()
	glPopMatrix()
	
	glLoadIdentity()
	
	glDisableClientState(GL_VERTEX_ARRAY)
	glFlush()
	Flip
Wend
End


The only problem there is that I cannot add vertices to the array on the fly like I can with the TBank. Else I would have to pre define the size of the array which consumes memory even when there is only a few vertices in the array. Would there be a more efficient way of doing it?