Gl_arb_vertex_buffer_object

Mars_999
Unregistered
 
Post: #1
I don't know if Mac has this extentsion available yet? If so has anyone here worked with it yet? I am going to implement it to try and speed up my engine. My question is, is it ok to use this with the extension?

Code:
struct Vertex
{
float x;
float y;
float z;
};

Vertex terrain[MAP_Z][MAP_X];

I would like to store the terrain array in the VBO. I know you can store normal arrays but unsure about multi-dimension arrays? Thanks
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
If the multidimensional array is statically rather than dynamically allocated, it's probably OK.

You almost certainly want to declare your vertex struct like this:

Code:
struct Vertex
{
    float x, y, z;
    float _unused;
};

for speed reasons.

ARB_vbo isn't available on the Mac yet.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #3
Quote:Originally posted by OneSadCookie
If the multidimensional array is statically rather than dynamically allocated, it's probably OK.

You almost certainly want to declare your vertex struct like this:

Code:
struct Vertex
{
    float x, y, z;
    float _unused;
};

for speed reasons.

ARB_vbo isn't available on the Mac yet.


I am using static, meaning I am not using new or malloc(). From what I am seeing on the specs I don't see anyone using multi-dim arrays only float array[].
What would I use the _unused variable for? Thanks OSC. Oh BTW that sucks for Mac hope its available on Mac when I get my G5. Wow
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
You wouldn't use the _unused variable for anything. It's like a magic go-fast switch.
Quote this message in a reply
Moderator
Posts: 437
Joined: 2002.09
Post: #5
Quote:Originally posted by OneSadCookie
You wouldn't use the _unused variable for anything. It's like a magic go-fast switch.


Alignment?

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
yep
Quote this message in a reply
Mars_999
Unregistered
 
Post: #7
Quote:Originally posted by MattDiamond
Alignment?


I am confused on alignment? Do you mean use that extra float to break up the data? e.g. so you know that three floats make up x,y,z and the next float is the start of a new vertex? If so isn't that a waste of memory?
Quote this message in a reply
ClarustheDogCow
Unregistered
 
Post: #8
No, the purpose of alignment is in the best case senario have the memory accesed in the largest or most efficient blocks. For most desktop computers, it is a double, which is two longs, which is two floats. This is what I belive "int" was created for, to refer to the most efficient memory acessor.

The byte count for this struct is:
float x 4
float y 4
float z 4
= 12
When two doubles would require 16 bytes. Thus:
float x 4
float y 4
float z 4
float _ 4
= 16

Would this use more memory? Yes. It also allows for the compiler to do behind the scenes optimizations with these efficent memory blocks.

I think there is a preprocessor switch or something that automatically aligns structs, but I can't quite remeber what it is.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #9
ok now I get it. But how much faster would it be? So if I had a
Code:
struct FOO
{
int x;
int y;
int waste;
int waste_again;
};

And if I used shorts I would need 8 if they are 2bytes each? Is this done all the time? Adding in wasted memory to speed up code?
Quote this message in a reply
ClarustheDogCow
Unregistered
 
Post: #10
No, all you would need is:
Code:
struct FOO
{
    int x;
    int y;
};
Assuming an int is 4 bytes (typical), because a double is only 8 bytes.
A better way to optimize from using a short is to use an int (assuming memory conditions allow for it). The same applies for char. Ints generally have faster math functions.
If you had to use a short, it would be like this:
Code:
struct FOO
{
    short x;
    short y;
    short z;
    short a;
};
You don't have to waste the memory. Mabye it is a void* (which is also 4 bytes) to a struct color. Mabye it is a Vertex* to create a linked list.
I don't know how common it is. In most cases you can get better performance out of changing methods. But sometimes you can't, and little tricks like these can go a long while, especially with very common data like verticies. Say you have 5000 verticies. Multiply each time you access x,y, and z by the amount of time saved by aligning. It adds up quickly (although the amount of timed saved is very small if you were to only access the struct once).
Optimizations are all about balance. Is this something your average programmer uses, or even knows about? Probably not. Would a company like ID design their data to use proper alignment? Most definantly.

I keep saying generally, because in most cases it will be like that. There are the minority of cases where you might be working on a processor like a Z80 where a short would be a whole lot faster than an long, and is even defined as an int. But for x86 and PPC, there isn't much doubt. If I'm wrong, please correct me.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #11
I know most people here haven't gotten to play around with VBO's yet due to PC has them so far. But I am on a PC for now and am playing around with adding them in. I am having a slight problem with getting them to work. I have my vertex arrays setup for vertex data and indices. I am unsure but looks like I can load both vertex and indices arrays into VBO's? Here is what I got for setup on VBO's and I crash out when I use them.

Code:
//create buffers
unsigned int vertex_buffer = 0;
unsigned int indices_buffer = 0;

//setup VBO's
glGenBuffersARB(1, &vertex_buffer);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertex_buffer);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(terrain), terrain, GL_STATIC_DRAW_ARB);

glGenBuffersARB(1, &indices_buffer);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indices_buffer);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(indices), indices, GL_STATIC_DRAW_ARB);

//in my rendering function
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertex_buffer);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indices_buffer);
//I am using glDrawElements()

I can't see anything wrong with what is code here?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
I'm sure those aren't all meant to be GL_ELEMENT_BUFFER_ARB, even knowing nothing about the extension...
Quote this message in a reply
Post Reply