HLSL學習筆記(一):基礎
http://www.cnblogs.com/rainstorm/archive/2013/05/04/3057444.html
前言
五一在家無事,於是學習了一下HLSL,基於XAN4.0的。學習完了也就總結一下,純粹是新手學習的經驗之談,紕漏之處還望見諒,不喜勿噴。
HLSL是一種使用GPU渲染出圖像的技術,不僅可以改變最終呈現的顏色,還可以物體的大小、胖瘦和位置等。例如物體的碎裂效果就可以使用HLSL來渲染得到。
數據類型
數據類型有值類型、向量、矩陣、采樣器、和結構體。
1.值類型
bool 布爾變量
half 16為整形
int 32位整形
float 單精度浮點數
double 雙精度浮點數
聲明方式:float f;
賦值方式:f = 1;
2.向量
聲明方式:float4 f;
賦值方式:f = {1,2,3,4};
取值方式:float3 ff = f.rgb;
說明:向可以通過xyzw或者rgba訪問向量中的指定字段,x或者r就是代表0號字段。不僅可以單獨操作一個字段,還可以對多個字段同時操作,例如3*f.xyz,就是將f中的xyz都乘以個3。
3.矩陣
聲明方式:float2x4 f; 先行後列。
賦值方式:f = {1,1,2,2,3,3,4,4};
取值方式:float ff = f[0][0];
說明:如果要對矩陣做乘法運算,請使用mul函數,另外,mul(ff,f)與mul(f,ff)的結果是不一樣的。
4.采樣器
聲明方式:
texture Texture; //紋理變量
sampler TextureSampler = sampler_state //紋理采樣器
{
Texture = <Texture>; //紋理采樣器使用的紋理對象
MinFilter = Linear; //縮小圖形使用線性濾波
MagFilter = Linear; //放大圖形使用線性濾波
MipFilter = Linear; //Mipmap使用線性濾波
AddressU = Wrap; //U方向上的紋理尋址模式采用Wrap方式
AddressV = Wrap; //V方向上的紋理尋址模式采用Wrap方式
};
賦值方式:在C#中對Texture賦值,effect.Parameters["Texture"].SetValue(Game.Content.Load<Texture2D>("*"));
取值方式:tex2D(TextureSampler, TEXCOORD0);
說明:MinFilter、MagFilter、MipFilter、AddressU、AddressV是可選項,如果不寫將會使用默認值,也就是上面賦予的值。
5.結構體
聲明方式:
struct VertexShaderInput
{
float4 Position : POSITION;
float2 TextureCoordinates : TEXCOORD0;
float3 Normal: NORMAL;
};
VertexShaderInput input;
此處與C#語法有些區別,直接這麽寫,不需要再寫個new什麽的。
賦值方式:與C#語法一致。
取值方式:與C#語法一致。
說明:其它的地方都好理解,關鍵是每個字段後面還有個“小尾巴”,這是什麽玩意啊?這個我也不太清楚,自己想當然感覺吧,應該就是顯存裏面有一些特殊的空間,它們有著自己名字,比如TEXCOORD0什麽的,就像C#中的Dictionary。
所以TextureCoordinates這行的意思是 創建一個float2類型的向量,它的名字是TextureCoordinates,值存儲在TEXCOORD0這個內存空間。
Position的意思對所有POSITION(POSITION0-POSITION9,貌似是這樣。)內存空間的引用。
這也就說明了入口函數的參數為什麽可以千變萬化,其實只是換了個名字,數據該在哪永遠都在哪。
語法
語法什麽的,與C#相差不大,不過還是有點區別的,至於區別的地方,我所知道的上面都寫出來了。
函數列表
本表來自網絡,我對說明做了些修改。
Name | Syntax | Description |
abs | abs(x) | 返回x的絕對值。對x的每個元素都會獨立計算一次。Absolute value (per component). |
acos | acos(x) | 返回x的反余弦值。對x的每個元素都會獨立計算一次。Returns the arccosine of each component of x. |
all | all(x) | 檢測x的所有元數的值是否為0.Test if all components of x are nonzero. |
any | any(x) | 檢測x是否有某個元數的值為0.Test if any component of x is nonzero. |
asfloat | asfloat(x) | 將x轉換為float類型。Convert the input type to a float. |
asin | asin(x) | 返回x的反正弦值。對x的每個元素都會獨立計算一次。 |
asint | asint(x) | 將x轉換為int類型。Convert the input type to an integer. |
asuint | asuint(x) | 將x轉換為uint類型。 |
atan | atan(x) | 返回x的反正切值。 |
atan2 | atan2(y, x) | 返回y、x的反正切值。 |
ceil | ceil(x) | 返回大於或等於x的最小整數。 |
clamp | clamp(x, min, max) | 將x截取在[min, max]範圍內。 |
clip | clip(x) | 如果x中存在值小於0的參數,則丟棄當前像素。 |
cos | cos(x) | 返回x的余弦值。 |
cosh | cosh(x) | 返回x的雙曲余弦值。 |
cross | cross(x, y) | 返回x、y的叉積。 |
D3DCOLORtoUBYTE4 | D3DCOLORtoUBYTE4(x) | 混合和縮放4D向量x用於補償一些對UBYTE4支持的硬件。Swizzles and scales components of the 4D vector x to compensate for the lack of UBYTE4 support in some hardware. |
ddx | ddx(x) | 返回關於屏幕坐標x軸的偏導數。 |
ddy | ddy(x) | 返回關於屏幕坐標y軸的偏導數。 |
degrees | degrees(x) | 將x(弧度)轉換到角度。 |
determinant | determinant(m) | 返回的正方形矩陣m的行列式。 |
distance | distance(x, y) | 返回x、y之間的距離。 |
dot | dot(x, y) | 返回x、y的點積。 |
exp | exp(x) | 返回以e為底數,x為指數的指數函數值。 |
exp2 | exp2(x) | 返回以2為底數,x為指數的指數函數值。對x的每個字段都會計算一次。 |
faceforward | faceforward(n, i, ng) | 檢測多邊形是否位於正面。-n * sign(?(i, ng))。 |
floor | floor(x) | 返回小於等於x的最大整數。 |
fmod | fmod(x, y) | 返回x/y的浮點余數。 |
frac | frac(x) | 返回x的小數部分。 |
frexp | frexp(x, exp) | 返回x的尾數和指數。 |
fwidth | fwidth(x) | 返回 abs(ddx(x)) + abs(ddy(x)), |
GetRenderTargetSampleCount | GetRenderTargetSampleCount() | 返回渲染目標采樣器的個數。Returns the number of render-target samples. |
GetRenderTargetSamplePosition | GetRenderTargetSamplePosition(x) | 返回關於給定采樣器的一個采樣點(x,y)。Returns a sample position (x,y) for a given sample index. |
isfinite | isfinite(x) | 如果x為有限值則返回true,否則返回false。 |
isinf | isinf(x) | 如果x為無限值則返回true,否則返回false。 |
isnan | isnan(x) | 如果x為NAN或QNAN則返回true,否則返回false。 |
ldexp | ldexp(x, exp) | frexp的逆運算,返回 x * 2 ^ exp。 |
length | length(v) | 返回v向量的長度。 |
lerp | lerp(x, y, s) | 對x、y進行插值計算。Returns x + s(y - x)。 |
lit | lit(n ? l, n ? h, m) | 返回光照向量(環境光,漫反射光,鏡面高光,1)。 |
log | log(x) | 返回以e為底的對數。 |
log10 | log10(x) | 返回以10為底的對數。 |
log2 | log2(x) | 返回以2為底的對數。 |
max | max(x, y) | 返回x、y中較大值。 |
min | min(x, y) | 返回x、y中較小值。 |
modf | modf(x, out ip) | 把x分割為整數和小數部分。 |
mul | mul(x, y) | 返回x、y矩陣相乘的積。 |
noise | noise(x) | Generates a random value using the Perlin-noise algorithm. |
normalize | normalize(x) | 返回單位化向量,定義為 x / length(x)。 |
pow | pow(x, y) | 返回x^y。 |
radians | radians(x) | 將x(角度)轉換到弧度。 |
reflect | reflect(i, n) | 返回入射光線i對表面法線n的反射光線。 |
refract | refract(i, n, R) | 返回在入射光線i,表面法線n,折射率為R下的折射光線。 |
round | round(x) | 返回最接近x的整數。 |
rsqrt | rsqrt(x) | 返回x平方根的倒數。 1 / sqrt(x) 。 |
saturate | saturate(x) | 把x截取在[0, 1]之間。 |
sign | sign(x) | 返回x的符號。 |
sin | sin(x) | 返回x的正弦值。 |
sincos | sincos(x, out s, out c) | 返回x的正弦值和余弦值。 |
sinh | sinh(x) | 返回x的雙曲正弦值。 |
smoothstep | smoothstep(min, max, x) | 如果x的範圍是[min, max],則返回一個介於0和1之間的Hermite插值。 |
sqrt | sqrt(x) | 返回x的平方根,對x的每個字段都會計算一次。 |
step | step(a, x) | 返回 (x >= a) ? 1 : 0 。 |
tan | tan(x) | 返回x的正切值。 |
tanh | tanh(x) | 返回x的雙曲正切值。 |
tex1D | tex1D(s, t) | 返回紋理s在t位置的顏色。1D texture lookup. |
tex1Dbias | tex1Dbias(s, t) | 使用bias返回紋理s在t位置的顏色。1D texture lookup with bias. |
tex1Dgrad | tex1Dgrad(s, t, ddx, ddy) | 1D texture lookup with a gradient. |
tex1Dlod | tex1Dlod(s, t) | 使用LOD返回紋理s在t位置的顏色。1D texture lookup with LOD. |
tex1Dproj | tex1Dproj(s, t) | 使用透視分離返回紋理s在t位置的顏色。1D texture lookup with projective divide. |
tex2D | tex2D(s, t) | 返回紋理s在t位置的顏色。 |
tex2Dbias | tex2Dbias(s, t) | 2D texture lookup with bias. |
tex2Dgrad | tex2Dgrad(s, t, ddx, ddy) | 2D texture lookup with a gradient. |
tex2Dlod | tex2Dlod(s, t) | 2D texture lookup with LOD. |
tex2Dproj | tex2Dproj(s, t) | 2D texture lookup with projective divide. |
tex3D | tex3D(s, t) | 3D texture lookup. |
tex3Dbias | tex3Dbias(s, t) | 3D texture lookup with bias. |
tex3Dgrad | tex3Dgrad(s, t, ddx, ddy) | 3D texture lookup with a gradient. |
tex3Dlod | tex3Dlod(s, t) | 3D texture lookup with LOD. |
tex3Dproj | tex3Dproj(s, t) | 3D texture lookup with projective divide. |
texCUBE | texCUBE(s, t) | Cube texture lookup. |
texCUBEbias | texCUBEbias(s, t) | Cube texture lookup with bias. |
texCUBEgrad | texCUBEgrad(s, t, ddx, ddy) | Cube texture lookup with a gradient. |
texCUBElod | tex3Dlod(s, t) | Cube texture lookup with LOD. |
texCUBEproj | texCUBEproj(s, t) | Cube texture lookup with projective divide. |
transpose | transpose(m) | 返回m的轉置矩陣。 |
trunc | trunc(x) | 將x的所有元素從浮點值截斷到整數值。 |
HLSL學習筆記(一):基礎