Does it save on performance/memory a whole lot to use vec3 instead of vec4?
Members - Reputation: 320
Posted 01 May 2012 - 01:01 AM
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?
Members - Reputation: 869
Posted 01 May 2012 - 02:00 AM
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
* 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)
Crossbones+ - Reputation: 3171
Posted 01 May 2012 - 03:02 AM
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.
Crossbones+ - Reputation: 8141
Posted 01 May 2012 - 06:17 AM
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.
Crossbones+ - Reputation: 2606
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.