Dot product vs Direct vector components sum performance in shaders
Asked Answered
K

2

6

I'm writing CG shaders for advanced lighting calculation for game based on Unity. Sometimes it is needed to sum all vector components. There are two ways to do it:

  1. Just write something like: float sum = v.x + v.y + v.z;
  2. Or do something like: float sum = dot(v,float3(1,1,1));

I am really curious about what is faster and looks better for code style.

It's obvious that if we have same question for CPU calculations, the first simle way is much better. Because of:

a) There is no need to allocate another float(1,1,1) vector

b) There is no need to multiply every original vector "v" components by 1.

But since we do it in shader code, which runs on GPU, I belive there is some great hardware optimization for dot product function, and may be allocation of float3(1,1,1) will be translated in no allocation at all.

float4 _someVector;

void surf (Input IN, inout SurfaceOutputStandard o){
   float sum = _someVector.x + _someVector.y + _someVector.z + _someVector.w;
    // VS
   float sum2 = dot(_someVector, float4(1,1,1,1));
}
Kowalski answered 5/9, 2019 at 11:20 Comment(0)
L
6

Check this link.

Vec3 Dot has a cost of 3 cycles, while Scalar Add has a cost of 1. Thus, in almost all platforms (AMD and NVIDIA):

float sum = v.x + v.y + v.z; has a cost of 2 float sum = dot(v,float3(1,1,1)); has a cost of 3

The first implementation should be faster.

Linlithgow answered 16/9, 2019 at 20:40 Comment(0)
F
5

Implementation of the Dot product in cg: https://developer.download.nvidia.com/cg/dot.html

IMHO difference is immeasurable, in 98% of the cases, but first one should be faster, because multiplication is a "more expensive" operation

Fragmentation answered 5/9, 2019 at 12:38 Comment(2)
Multiplication is generally more expensive, but if there is hardware for multiply + accumulate, and the addition runs through the same operation, it might be about the same. en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_operationBallet
Hmm interesting, will read this later on this week!Fragmentation

© 2022 - 2024 — McMap. All rights reserved.