Render to texture - Speed test

Blitz3D Forums/Blitz3D Programming/Render to texture - Speed test

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Ok, i have conducted a fairly, erm, fair test of the speed difference between copyrect + 256 (force texture to VRAM flag) and the Render To Texture function found within the fastlibs library.

My results are all pretty conclusive at a number of different iterations of copyrect and rendertotexture calls per loop.

I found that render to texture is faster by up to 50%. This was conducted using a 512 x 512 texture. The cameraviewport was resized to the texture size in both tests, to ensure the same dimension of pixels was being copied.

However, when switching to smaller sized textures, 256 128 64, the difference was practically not noticable. I'm talking 1 fps over 50 copyrects.

So, for the results. My scene in 20 sphere's, with segment level of 3. A cube to texture my screen renders on. And a texture for the sphere. My rig handles this no problem, at 1700 fps+.



Code used for this test.

Include "FastExt.bb"

Graphics3D 1024,768

InitExt

SetBuffer BackBuffer()

Global camera = CreateCamera()

Global light = CreateLight()

Global ts = 512

; create two textures, one with the render to texture flag applied. Both must have 256 flag applied.
Global texRender2Tex = CreateTexture(ts,ts, 1 + 2 + 256 + FE_ExSIZE + FE_RENDER );+ FE_ZRENDER)
Global texRenderNormal = CreateTexture(ts,ts, 1 + 2 + 256)

Global sphere_texture = CreateTexture(64,64)
SetBuffer TextureBuffer(sphere_texture)
For tloop = 0 To 31
	Color 10+tloop*3 , 100+tloop*4 , 100+tloop*3
	Rect tloop , tloop , (32-tloop)*2 , (32-tloop)*2
Next

SetBuffer BackBuffer()

Dim sphere(20)
For eloop = 0 To 20
	sphere(eloop) = CreateSphere(3)
	PositionEntity sphere(eloop),Rnd(-10,10),Rnd(-10,10),Rnd(10,20)
Next

Global cube = CreateCube()
EntityTexture cube,texRenderNormal
PositionEntity cube,0,-0.5,2
ScaleEntity cube,0.3,0.3,0.3

Global timer, frame, time, fps, mode

While Not KeyHit(1)

	TurnEntity cube,0,1,0

	If KeyHit(57) Then
		mode = 1-mode
		retexture()
	End If

	If mode = 0 Then
		render_to_texture_fake()
	ElseIf mode = 1 Then
		render_to_texture_real()
	End If


	If MilliSecs()<timer+1000 Then
								frame=frame+1
	Else
								fps=frame
								frame=0
								timer=MilliSecs()
	End If

	UpdateWorld
	RenderWorld
	Text 0,0," SPACE to toggle mode"
	If mode = 0 Then
		Text 0,10," COPYRECT method"
	Else
		Text 0,10," RENDER TO TEXTURE method"
	End If
	Text 0,20,"fps="+fps
	Flip 0
Wend

Function retexture()

	Local texture
	
	If mode = 0 Then
		texture = texRenderNormal
	Else
		texture = texRender2Tex
	End If
	
	EntityTexture cube,texture
	
End Function

Function render_to_texture_fake()

	For loop = 0 To 50
		CameraViewport camera,0,0,ts,ts
		RenderWorld
		CopyRect 0,0,ts,ts,0,0,BackBuffer(),TextureBuffer(texRenderNormal)
	Next

	CameraViewport camera,0,0,1024,768
	
End Function

Function render_to_texture_real()

	For loop = 0 To 50
		CameraViewport camera,0,0,ts,ts
		SetBuffer TextureBuffer(texRender2Tex)
		RenderWorld
		SetBuffer BackBuffer()
	Next
	
	CameraViewport camera,0,0,1024,768

End Function


I've hardcoded the resolution. This won't run unless you have the fast lib.dll and include file, which i can't give you, because they are copyrighted :o)

If there is anything i have missed i will try and address them.

To some up, copyrect, on my rig anyway, only really pulls up the performance if the texture being rendered to, is over a certain size, in this case, anything over 256x256. It must be to do with the behind the scenes work of copyrect and rendertotexture taking longer than the actual copy itself.