typedef struct{
float x,y,z;
}Vertex;
....
Vertex V=new Vertex[size];//allocation
.....//bla bla bla
glVertexPointer(3,GL_FLOAT,0,V);
Questions
1)By using a struct/class, is OpenGL taking some time to convert it to some internal format ?
2)Would it be faster if I`d make a 3 times bigger(3*size) float array ?
Data conversion cam cause performance loss ?
I`ve been wondering about that for some time... here`s what`s I do
Relative Games - My apps
Using structs should work just as well.
edit: stupidness. nothing to miss.
[edited by - chawk on January 20, 2004 11:57:41 AM]
edit: stupidness. nothing to miss.
[edited by - chawk on January 20, 2004 11:57:41 AM]
quote:
Original post by chawk
Using structs should work just as well.
This is false. The idea that structs could be changed freely as an array is horribly wrong. Alignment problems could possibly poke in (there's not even guarantee vertex.x, vertex.y, vertex.z would stored in a array-like structure and in that order)!
Nothing prevents the compiler to allocate your structure as follows:
ZZ*YY*WW
|
|
Structure pointer here, * are padded bytes.
Or, even worse
|
|
ZZ is at byte offset structure + 100bytes
YY is at offset + 10
XX is at offset -5
Second example is abolutely exagerated: not a single compiler will ever do something like that however, you get the point.
Not using a low-level format is sometimes a necessary evil, you give a little of performance to gain faster development, easier-to-read code etc. Every conversion takes time (exception: pointer conversion is very fast since there's no such conversion at CPU's ISA level). CVT* instructions are here for a reason after all, and they are all but free (float->int and int->float are rumored to be the most expensive).
This is not really important but I wonder how the code in the example could work. It's so different from my style I hardly understand it. How can new be called on V (which does not like to be a pointer to me)? How can V be casted to an address in the VertexPointer call? Have you defined other operators not shown here (unlikely since this looks oldschool C-like code)?
EDIT: what are you asking in (2)? Of curse it wouldn't! Just allocating memory full of trash is not very likely to increase performance (besides some very rare ASM tricks I saw some time ago).
[edited by - Krohm on January 20, 2004 3:16:52 PM]
Previously "Krohm"
About easier to read code and such, i am using a one dimension array for all vertices in an object and i have defined a getX(vnum,array) operator (or however it's called) and this makes the code a lot easier to read, as easy as vertex classes i think. As i define the operators (or however they're called) and not make functions out of them, the use of vertex arrays really speeds up a lot (actually i #define all operations capable of being put on one line, could anyone tell me if it's faster indeed?). Also, it's directly compatible with OpenGL using VAs and VBOs.
EDIT: spelling
[edited by - Tree Penguin on January 20, 2004 4:30:39 PM]
EDIT: spelling
[edited by - Tree Penguin on January 20, 2004 4:30:39 PM]
quote:
Original post by Tree Penguin
...(actually i #define all operations capable of being put on one line, could anyone tell me if it''s faster indeed?)...
Ahhhhhh!!! No!!!! Evil!!!! Please read why you should use inline functions instead of macros and why inlining is not always a good thing
Enigma
Actually, my original post was a bit lengthy, explaining the evils that structure packing might introduce into an array and I had recommended using #pragma pack. Then, after posting, I went into VC++ and tested if an array of structures was actually packed, and it wasn't. Then I edited out all the stuff I wrote.
With the following 3-byte structure defined:
I allocated an array of 2 of these (local stack memory, not new/malloc). Consider the following screenshot from the VC++ debugger:

Note the offsets that it's moving data into -- all 1-byte aligned. Seems to me that the structures in that array aren't being packed or referenced as such.
Did I mess-up somewhere?
[edited by - chawk on January 21, 2004 1:57:51 AM]
With the following 3-byte structure defined:
typedef struct{ char x, y, z;} Vertex;
I allocated an array of 2 of these (local stack memory, not new/malloc). Consider the following screenshot from the VC++ debugger:

Note the offsets that it's moving data into -- all 1-byte aligned. Seems to me that the structures in that array aren't being packed or referenced as such.
Did I mess-up somewhere?
[edited by - chawk on January 21, 2004 1:57:51 AM]
I`m sorry [Krohm] for inducing you into errors,
the code was like that... I wrote it in the rush and didn`t had the time to even review my post.
Actually this was the method I saw in GameTutorials Tutorials and it works quite perfectly(And I`ve been using it for quite a long time) until... I questioned myself about speed issues...
So again I ask , if I would use a 3*size array of floats, and pass it to glVertexPointer() and the like I would gain performance ?
PS:If I`d convert my vars into GL_UNSIGNED_BYTE and because it would be only 1 byte var instead of 4, would it be faster ?
Vertex *V=new Vertex[size];
the code was like that... I wrote it in the rush and didn`t had the time to even review my post.
Actually this was the method I saw in GameTutorials Tutorials and it works quite perfectly(And I`ve been using it for quite a long time) until... I questioned myself about speed issues...
So again I ask , if I would use a 3*size array of floats, and pass it to glVertexPointer() and the like I would gain performance ?
PS:If I`d convert my vars into GL_UNSIGNED_BYTE and because it would be only 1 byte var instead of 4, would it be faster ?
Relative Games - My apps
quote:
Original post by cippyboy
So again I ask , if I would use a 3*size array of floats, and pass it to glVertexPointer() and the like I would gain performance ?
There will be absolutely no difference in performance. Provided the structure is packed (and since it is working for you, it must be) the array of structures and the array of floats are exactly the same in memory.
quote:
Original post by cippyboy
PS:If I`d convert my vars into GL_UNSIGNED_BYTE and because it would be only 1 byte var instead of 4, would it be faster ?
What does your profiler tell you your bottleneck is? If you''re AGP-bandwidth bound (very unlikely) then yes, converting to GL_UNSIGNED_BYTE will be faster. Otherwise it will probably be the same speed or slower (if the driver converts all data to a floating point format).
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement