Jump to content

View more

Image of the Day

Working on an auto spawn system. #gamedev #indiedev #screenshotsaturday https://t.co/Mm2kfekz7b
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Does it save on performance/memory a whole lot to use vec3 instead of vec4?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 ill   Members   


Posted 01 May 2012 - 01:01 AM

By default in the fixed function pipeline for OpenGL things like position are actually 4 element vectors. Also I read about how when doing SIMD on vectors and things like that, processors have room for 4 element arrays.

I'm guessing sending things to OpenGL as 3 element vectors saves on bandwidth or something, but when writing actual shaders does it matter at all if I'm using vec3 or can I just use vec4 all over the place with no penalty? What about vec2 even?

#2 Digitalfragment   Members   


Posted 01 May 2012 - 02:00 AM

Depends on what part of the pipeline you are talking about, and the context in which you use the float3 or float4.

SIMD allows for typically 4 floating point operations to be performed simultaneously. In this regards, float4 is better than float3*
* but if you pack 4 float3s together (12 floats), you have 3 SIMD operations instead of 4 if you were treating the float3s as their own float4s (3 floats, padding, 3 floats padding, 3 floats, padding)

If you are talking about GPU, then yes the same principles still apply - but the compiler will generally fix this for you. Set yourself up some simple examples, then look at the assembly output of your shader compiler. Its even smart enough to realize that normalize(normalize(N)) is pointless :)

However, IIRC:
* vertex buffers are best done minimally to reduce bandwidth these days, as opposed to rigidly stick to register aligned strides
* vertex shader outputs (interpolator stage) is best packed into float4s as each output *is* its own register, and the compiler will not do this for you automatically as it has to be done consistently at each part of the pipeline (geo -> vertex, vertex -> pixel, etc)

#3 MaxDZ8   Members   


Posted 01 May 2012 - 03:02 AM

When sending vertex attribs, I would expect no difference. Hardware has been unpacking vec3 for quite a while, you can bet it's full speed.
When we go out of GPU pipelines, most libraries dealing with vec3 always allocate vec4 instead to take advantage of aligned reads. While it is possible to use the "pack" trick pointed above, I have doubts its feasible in general.

Previously "Krohm"

#4 mhagain   Members   


Posted 01 May 2012 - 06:17 AM

Every GPU register (with some exceptions that are not relevant here - such as loop counter registers) is already a 4-element vector so you're not getting any memory saving here. Interesting here to note that the old gl_TexCoord[n], etc, slots were also specified as 4-element vectors.

As a general rule, the GPU works very differently to your CPU, so what's relevant for one won't always be relevant - or even intuitive - for the other.

Where this does become useful is if you're hitting against the max number of register slots, as you could pack e.g. 2 vec2s into a single vec4 and get an extra slot that way. A good shader compiler should make this optimization for you, but I don't know if it's specified and I certainly wouldn't rely on it either way.

Edited by mhagain, 01 May 2012 - 06:24 AM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#5 Krypt0n   Members   


Posted 01 May 2012 - 05:11 PM

nowadays (on NVidia since G80/GTX8800 and ATI since GCN/7970) do not operate on vectors anymore, but on single elements, that way the compiler can easier optimize and a much higher utilization of the GPU is possible. in this place, using vec3 can be a benefit, as the compilers might sometimes not know that some vector elements are 0 or 1 and could be completely rejected (e.g. when those 'default'/'neutral' values come from constant registers). Registers are also organized as individual elements, saving space (if the compiler cannot), can further rise the utilization -> speed.
if you do simple math, it all will be done in a scalar way usually. some compiler can vectorize the code, but whether you're using vec3 or vec4 can give you sometimes a boost or a hit, depending on what operations you are doing and if the compiler can detect what you intend to do. if it's a common pattern, it will vectorize it, no matter if vec3 or vec4.
if you're writing hand optimized vector code e.g. using SSE, it's usually critical to have vec4 code, vec3 leads to a lot of operations to reorganize the vector layout to vec4 before you do the math; basically, the overhead is more than the benefit in most simple cases.
if you have tons of data and you're linearly reading it, it's 25% boost. if you have random reads and you mostly hit the cache, you increase the cached element count, which is also a big boost (it can be more than 25% speed, as those 25% more could be the amount of elements that were exceeding cache with vec4). If you have a lot of data and every read is random into your main memory, then it's probably slower, as in case of aligned vec4 elements, your cpu/gpu would read one cache line (64-256byte), but for vec3 elements, some would cross the cache line and you'd read twice. while the chances are quite low (12/64), the cost of a cache line read is quite high and your CPU/GPU is trying to hide that cost, so the first read might look like 50% or 25% speed loss (compared to cached reads), while the second might fully hit.
that's fully worth it, usually all kind of saving on disk will save you time, even if you do some funky decompression or conversion (e.g. using float16 instead of float32), you will have a saving, I would never waste space, even on SSDs.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.