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

Started by
3 comments, last by Krypt0n 11 years, 11 months ago
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?
Advertisement
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)
When sending vertex attribs, I would expect no difference. Hardware has been unpacking [font=courier new,courier,monospace]vec3[/font] for quite a while, you can bet it's full speed.
When we go out of GPU pipelines, most libraries dealing with [font=courier new,courier,monospace]vec3[/font] always allocate [font=courier new,courier,monospace]vec4[/font] 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"

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

-GPUs:
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.
-CPUs:
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.
-memory:
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.
-DiskSpace
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.

This topic is closed to new replies.

Advertisement