DX Vertex Buffers - Made Easy - REALLY!

BlitzMax Forums/BlitzMax Programming/DX Vertex Buffers - Made Easy - REALLY!

TartanTangerine (was Indiepath)(Posted 2005) [#1]
Still needs optimising and considering 2 days ago I had no idea what a vertex buffer was I think this is pretty damn fast. One way to speed this up would be to use indexedVB, basically you re-use the same vertices multiple times in the same model - I've no idea how to use them :(

I've constructed this in such a way that you can use all of the Flexible Vertex Formats, ie. Transformed, Lit, Specular lighting etc. Transformed vertices are great for HUD's since the matrix calculations don't touch them - They stay just where you put them. You can also choose to store the buffer in vidmem or sysmem, you will need to enable TnL to take advantage of vidmem storage otherwise it'll just slow things down.

The enclosed code will allow you to create a buffer of any size (2000 verts is optimum) and will allow you to rotate,scale and translate the vertices with simple commands. I have also created some quad functions so that you can replace your normal DrawImage commands with Vertex Buffer commands. Quads are not much faster since they are only 4 vertexes and use very little bandwidth

One very good application for this would be a tile map. You could use this code and part of my single surface code to cache a whole tilemap to a buffer. Now when you want to display the tilemap you just draw the buffer, move it all with one command, rotate it and scale it. Now that would be very fast. Objects rendered from the buffer will be clipped by clip-planes, unless you turn that feature off.

Before you download this code you need to be aware that some small changes will have to be made to the d3d7.bmx file in the DirectX Mod. These changes are necessary as the existing declarations for the VertexBuffer Functions are incomplete.

Locate :
Type IDirect3D7 Extends IUnknown

Add/Replace the Method :
Method CreateVertexBuffer(vbdesc:Byte Ptr ,d3dvertexbuffer:IDirect3DVertexBuffer7 Var,dword)

Locate :
Type IDirect3DDevice7 Extends IUnknown

Add/Replace the Method :
Method DrawPrimitiveVB(primtypem,d3dvertexbuffer:Byte Ptr,startvert,numverts,flags)

Locate :
IDirect3DVertexBuffer7 Extends IUnknown

Add the Methods :
Method Lock(Flags,lplpData:Byte Ptr Var,Size)
Method Unlock()
Method Optimize(lpD3DDevice:Byte Ptr,dwFlags)
Method ProcessVertices(dwVertexOp,dwDestIndex,dwCount,lpSrcBuffer,dwSrcIndex,lpD3DDevice,dwFlags)
Method GetVertexBufferDesc(lpVBDesc:Byte Ptr Var)

I think that's all. I've tried to make this as stand-alone as possible.

The code : http://www.indiepath.com/tim/vertex_buffers.zip (don't steal the image it's licenced to me not you!)

If you want to enable TnL features then you need to go here :- http://www.blitzbasic.com/Community/posts.php?topic=51795

Have fun.


Steve Elliott(Posted 2005) [#2]
Tim you really need to be talking to Mark about integrating your optimizations into an official update when they are 100% complete and tested (you still seem to be experimenting).


TartanTangerine (was Indiepath)(Posted 2005) [#3]
Steve, I have no interest in that.

And yeah I'm experimenting, how else are we going to get fixes and extra functionality?

Anyway I'm tired of playing now, vertex buffers don't make money.


Steve Elliott(Posted 2005) [#4]
That's a shame because having used an optimization of yours my program then refused to "create a surface" when trying to set a graphics mode - the only solution was to uninstall and re-install Max.

I'm not saying this will happen everytime - but this is the sort of thing that can happen if you play about with the internal workings of Max. So a complete and tested module would prevent this sort of user error? Perhaps BRL can complete the work. ;-)

Just to be clear, your experimenting and sharing code is welcome so thanks - even if we end up messing things up somehow. ;-)


Anyway I'm tired of playing now, vertex buffers don't make money.



But Blitz Max Modules do. ;-)


TartanTangerine (was Indiepath)(Posted 2005) [#5]
That's a shame because having used an optimization of yours my program then refused to "create a surface" when trying to set a graphics mode - the only solution was to uninstall and re-install Max.

Yeah that's why I'm *now* trying to build this stuff on top of the modules instead of inside them.

I'm not saying this will happen everytime - but this is the sort of thing that can happen if you play about with the internal workings of Max so a complete and tested version would be great - perhaps BRL can complete the work. ;-)

ROFL, Ummn the reason I am making these changes is because BRL did not complete the work. I don't think BRL are really focussed on 2d do you?

Just to be clear, your experimenting and sharing code is welcome so thanks - even if we end up messing things up somehow. ;-)

Yeah I know. Just make sure that when you finish all these great games that Indiepath gets first refusal for publishing. :P

But Blitz Max Modules do. ;-)

Yeah but developers are tight fisted so and so's....


Steve Elliott(Posted 2005) [#6]

Yeah that's why I'm *now* trying to build this stuff on top of the modules instead of inside them.


Sounds good.


ROFL, Ummn the reason I am making these changes is because BRL did not complete the work. I don't think BRL are really focussed on 2d do you?


:-D

To be fair, reading Mark's comments in one of the DirectX modules he does say it's work in progress. But if they're not focused on 2d they should be because it's actually 2d in 3d which opens-up all sorts of possiblities.


Yeah but developers are tight fisted so and so's....


Can't speak for other's but if a (stable) graphics module doubled the speed of my program I'd pay for it.


TartanTangerine (was Indiepath)(Posted 2005) [#7]

Can't speak for other's but if a (stable) graphics module doubled the speed of my program I'd pay for it.


Granted, I would too. However due to the nature of such a beast it is necessary to directly access the graphics drivers, the majority of the DirectX commands for instance have already been declared in the DirectX mods, in some cases, like vertex buffers they were not correctly declared. So, if you want all these nice features you are going to have to tweak the official mods to get them.


Yan(Posted 2005) [#8]
We shall have to wait and see what Simon gets up to after the GUI stuff is out.

Good stuff Tim, thanks for sharing! :o)


GW(Posted 2005) [#9]
Did anyone else get this to work?
I recieve a mav on the line:
myVertexBuffer.lock(DDLOCK_WAIT|DDLOCK_WRITEONLY|DDLOCK_NOOVERWRITE)