in shaderlab,what does fixed4 mean??

I knew half4,whart is fixed4??

1 Like

scroll down to ‘Date Types’

fixed is a fixed point values
float is a floating point value
half is a floating point value half the size of float

in case you don’t know what a fixed point value is:

2 Likes

thanks, and one more querstion.what does"LOD 200" mean??

LOD stands for level of detail.

Would be helpful if the unity documents actually described these structures, their members and methods. The basic shader examples and explanations in the Unity documents online appear to skip merrily over the basics of how you translate, query, and perform common calculations on these structures. I only guessed things like there was fixed4.a and min() max() functions available. Where’s the spec/headers/help for this stuff. Many thanks

4 Likes

Take a look at: (HLSL reference) https://msdn.microsoft.com/en-us/library/bb509634(v=vs.85).aspx
and: (functions) https://msdn.microsoft.com/en-us/library/ff471376(v=vs.85).aspx

Data types and precision: https://docs.unity3d.com/Manual/SL-DataTypesAndPrecision.html

3 Likes

Great links - although still, searching for fixed4 on either of those pages, or in the entire site came up blank!
I’m pretty much a C++ guy, and just missing the ability to F12 in visual studio on these types makes me scream! :wink: Where’s the real header? Is fixed4 a unity extension?
So far I’ve worked out from looking at other people’s code it has members,
.r
.g
.b
.a
and
.rgb

I want to see
struct (or class) fixed4 {
// what’s here.
}

2 Likes

You can use any combination of r g b and a to extract out the components (including things like rrbb, aaaa etc)

https://docs.unity3d.com/Manual/SL-DataTypesAndPrecision.html

The data types floatN, halfN, and fixedN (N can be 2,3, or 4. For N=1, the N is omitted) refer to different precisions of vectors. These are primitive types, so you won’t be finding any “struct fixed4{…}” anywhere. Furthermore, things like
.r,.g,.b, etc. aren’t strictly members so much as components, and generally it helps to think about them as such when writing shaders, as it makes writing vectorizable code more intuitive.

Every vecN type has N components:
a float4 has X,Y,Z, and W,
a float2 has X and Y, etc.
a float has only X.
Precision doesn’t matter here, as a fixed4 and half4 also have XYZW.
Because vectors are used to represent colors as well as positions or directions, RGBA exists as an alias for XYZW. You can mix and match these however you see fit, although something like float4.ryb is rather odd, and .xyz or .rgb would be better.
The most important part of this is that these components can be rearranged as needed, even to make vectors larger or smaller than the original. float2.xyxy for instance is a float4, whose XYZW components are two copies of the float2’s XY.

A common use for this is having, say, a float value A that you would like to visualize as a grayscale image for testing. you could output a color float4(A.rrr,1), which would be a vector with 1 in the alpha channel, and A.r in the r,g,and b.

It’s good to remember that HLSL/Cg are meant to look and feel like C, but are not C.
If you try to write a shader the way you write a C or C++ program, bad things will happen. The GPU does things quite differently than the CPU, and the language quirks reflect that.

Additionally, unlike the C# you use in Unity (which will likely be using 90% Unity APIs), The meat of a shader, that big CGPROGRAM bit, is largely Unity-less. Unity has set up a number of convenience methods, macros, and variables, and of course the Surface Shader is a Unity code-generation thing, but it doesn’t take long to trace through the intermittent Unity-defined helpers to find the underlying Cg code.

On a slightly unrelated note, if you are looking for headers, these are .cginc files. This is where you can find all of those Unity-defined helpers. But you won’t find fixed4 there.
(Or .hlslinc, or .glslinc, but most are .cginc)
Trying to find a header for fixed4 is a bit like trying to find a header for ‘float’ in C++. Unlike C++ or C#, these vectors don’t have to be defined as a struct of several scalars, because the GPU deals natively in vectors. In fact, the vast majority of calculations that can be done on a vector take the same amount of time as doing it as a scalar. Additionally, swizzling, or the rearranging of components, takes basically no time at all. For that reason, is you have, say, 4 fixed values, or 2 fixed2s, or a fixed3 and a fixed (you get the idea), you can pack them into a fixed4 and do calculations on them really fast.

10 Likes