What does "2..xxx" mean in shaderlab? - unity3d

float3 f = float3(1,2,3);
f *= 2..xxx;
I have no idea what ..xxx does. I got the code from here

It's a "swizzle" operation. In this case of a scalar constant 2.0.
2..xxx is equivalent to float3(2.0, 2.0, 2.0)
You can find more info here in the "Vector swizzle operator" section.

Related

How can i initialize constant with derived values in HLSL?

I'm trying to migrate this glsl code into hlsl (unity shader). But the compiler complains about the following lines:
#define Length float
const Length m = 1.0;
const Length km = 1000.0 * m;
where km is derived from m, and the error msg said:
'km': initial value must be a literal expression
Is there any way to solve this without just replacing m with its literal value manually?
I tried to google this but found nothing related, or maybe this question is just a complaint about HLSL's weak compiler.
According to glsl-to-hlsl-reference, we should use static const qualifiers in hlsl.

"The compiler is unable to type-check this expression in reasonable time" for a simple formula

I have what appears to be a rather simple arithmetic expression:
let N = 2048
// var c = (0..<N).map{ sin( 2.0 * .pi * Float($0) / (Float(N)/2.0)) }
let sinout = (0..<N * 10).map { x in
sin(2 * .pi * Float(x) / Float(N / 2))
}
But this is generating:
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
Why is such a simple equation not parse-able by the Swift compiler? How do we write equations that Swift can actually parse? This must be a major headache for persons writing DSP and/or linear algebra libraries: what workarounds or patterns do you use?
You just have to explicitly set the return type of your map expression:
map { x -> Float in
Sometimes it is hard for Swift to compile some seemingly easy code. The best thing you can do in those cases is modulate it in smaller chunks. I honestly think that this is an error that should be fixed but that for some reason is still there.

Hints on getting basic arithmetic expressions to be parsed in Swift

Consider the following expression:
let N = 2048
var c = (0..<N).map{ f -> Float in sin( 2 * .pi * f / (N/2)) }
Swift can not really parse it:
This is already a very small expression: it's absurd to break it into even smaller pieces. So I am trying to use type-casts. But I am getting weary of adding many explicit type casts :
let N = 2048
var c: [Float] = (0..<N).map{ f -> Float in
Float(sin( 2.0 * .pi * f / (Float(N/2)))) }
Even with the above the error continues
Why is swift so weak in parsing these simple arithmetic expressions? What can I do short of breaking it into pieces of the form
let c = a * b
let f = c * d
That is just too simplistic to be practical for signal processing. I am guessing that there were tricks to get the compiler to be a bit more intelligent: please do share.
The issue is that the arithmetic operators (+,-,* and /) have a lot of overloads. Hence, when you write expressions containing a lot of those operators, the compiler cannot resolve them in time.
This is especially true when you have type errors. The compiler tries to find the correct overload, but cannot do so, since your types are mismatching and there's no matching overload. However, by the time the compiler could infer this, it's already past the timeout for resolving expressions and hence you get that error instead of the actual type error.
As soon as you resolve the type errors by casting all Ints to Float, the single line expression compiles just fine.
let c = (0..<N).map{ f -> Float in sin( 2 * .pi * Float(f) / Float(N/2)) }
Once you do that, you don't even need the named closure argument and type annotation of the return value anymore.
let c = (0..<N).map{ sin(2 * .pi * Float($0) / Float(N/2)) }
That looks like java. What about
let N = 2048
var c = (0..<N).map{ f in
sin( 2.0 * .pi * Float(f) / Float(N/2))
}

What's the difference between M_PI and M_PI_2?

I forked a project from Github, Xcode shows a lot of warnings:
'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value
of correct type and avoid casting.
and
'M_PI_2' is deprecated: Please use 'Double.pi' or '.pi' to get the value
of correct type and avoid casting.
Since both M_PI and M_PI_2 are prompted to be replaced by Double.pi, I assume there are in fact the same value. However, there's this code in the project:
switch angle {
case M_PI_2:
...
case M_PI:
...
case Double.pi * 3:
...
default:
...
}
I'm really confused here, are M_PI and M_PI_2 different? or are they just the same?
UPDATE:
It turns out to be my blunder, Xcode says 'M_PI_2' is deprecated: Please use Double.pi / 2 or .pi / 2 to get the value of correct type and avoid casting. so it isn't a bug, just too hard to notice the difference of 2 prompts.
Use Double.pi / 2 for M_PI_2 and Double.pi for M_PI.
You can also use Float.pi and CGFloat.pi.
In Swift 3 & 4, pi is defined as a static variable on the floating point number types Double, Float and CGFloat.
These constants are related to the implementations of different functions in the math library:
s_cacos.c: __real__ res = (double) M_PI_2 - __real__ y;
s_cacosf.c: __real__ res = (float) M_PI_2 - __real__ y;
s_cacosh.c: ? M_PI - M_PI_4 : M_PI_4)
...
s_clogf.c: __imag__ result = signbit (__real__ x) ? M_PI : 0.0;
M_PI, M_PI_2, and M_PI_4 show up quite often but there's no 2.0 * M_PI. 2π is just not that useful, at least in implementing libm.
M_PI_2 and M_PI_4, their existences are well justified. The documentation of the GNU C library suggests that "these constants come from the Unix98 standard and were also available in 4.4BSD". Compilers were not that smart back at that time. Typing M_PI/4 instead of M_PI_4 may cause an unnecessary division. Although modern compilers can optimize that away (GCC uses mpfr since 2008 so even rounding is done correctly), using numeric constants is still a more portable way to write high-performance code.
M_PI is defined as a macro
#define M_PI 3.14159265358979323846264338327950288
in math.h and part of the POSIX standard.
Follow This
You can find reference answer here
Check This Solution - How you can use it

objective-c : what is the equivalent of java Math.toRadians(EndLat - StartLat) in objective-c?

what is the equivalent of java function
Double EndLat;
Double StartLat;
Math.toRadians(EndLat - StartLat);
in objective-c?
#include <math.h>
(EndLat - StartLat) * M_PI / 180.0;
You might want to put this in a function if you're going to be using it a lot
Well Math.toRadians(a) seems to compute the radians from the angle a.
According to any sufficient basic math text, the equivalent code would be
result = a * 3.14159265358979323846 / 180.0