HLSL Shader編程基礎總結

轉自:https://blog.csdn.net/Blues1021/article/details/47093487算法

基本前提概念   

  Shader是一種映射到GPU硬件彙編語言上的高級語言,Shader中的數據類型是要聲明類型和用途的,用途其實就是和着色器寄存器關聯,輸入位置寄存器,輸出位置寄存器,輸出顏色寄存器等。Shader HLSL中的顏色是rgba的類型,不要弄錯了。Shader中的每個類型,函數,字符串都是有含義的。   express

  頂點和像素着色器中,常量表用SetDefault傳入Device將常量表中的字段常量設置初值,避免忘記賦值了而沒有初值的狀況。
  D3DXCompileShaderFromFile用D3DXSHADER_SKIPVALIVATION當已確承認用時不進行任何代碼驗證能夠提升效率不要老用D3DXSHADER_DEBUG那只是開發測試時候用的。windows

HLSL語言基礎總結

I、變量類型

1. 標量類型:
bool, int, half爲16位浮點數,float, double。
初始化和賦值:數組

const static int g_nArraySize = 3;
const static int g_nArraySize2 = {3};
const static int g_nArraySize3 = int(3);

2.向量類型:
1)類型
vector是4D向量,每一個份量都是float類型。
vector<T, n>其中n爲1到4,T爲須要的向量份量類型。
 float2,float3,float4定義的是2維,3維,4維的向量。
2)初始化(直接或者構造函數方式賦值)緩存

vector u = {0.6f, 0.3f, 1.0f, 1.0f};
vector v = {1.0f, 5.0f, 0.2f, 1.0f};
vector u = vector(0.6f, 0.3f, 1.0f, 1.0f);
vector v = vector(1.0f, 5.0f, 0.2f, 1.0f);
float3 = float3(0, 0, 0);

3)訪問和賦值
使用數組下標的語法訪問向量的一個份量,例如訪問第i個向量份量,用vec[i] = 2.0f。
能夠像訪問結構的成員同樣訪問向量vec的一個份量,使用已定義的份量名x,y,z,w,r,g,b和a,如:安全

vec.x = vec.r = 1.0f;
vec.y = vec.g = 2.0f;
vec.z = vec.b = 3.0f;
vec.w = vec.a = 4.0f;

名稱爲r,g,b和a的份量分別對應x,y,z和w的份量。當使用向量來表示顏色時,RGBA符號是更適合的,由於它增強了向量所表示的顏色。app

考慮向量u = (ux, uy, uz, uw),假設咱們要拷貝u的全部份量到一個像v = (ux, uy, uy, uw)這樣的向量v。最直接的方法多是逐個從u往v拷貝每一個份量。但無論怎樣,HLSL提供了一種特殊的語法作這些無序的拷貝,它叫作swizzles:框架

vector u = {l.0f, 2.0f, 3.0f, 4.0f};
vector v = {0.0f, 0.0f, 5.0f, 6.0f};
v = u.xyyw; // v = {1.0f, 2.0f, 2.0f, 4.0f}

拷貝數組時,咱們沒必要拷貝每一個份量。例如,咱們能夠僅拷貝x和y份量:less

vector u = {1.0f, 2.0f, 3.0f, 4.0f};
vector v = {0.0f, 0.0f, 5.0f, 6.0f};
v.xy = u; // v = {l.0f, 2.0f, 5.0f, 6.0f}

3.矩陣類型:

1)類型
matrix爲4x4的矩陣,每一個元素的類型都是float類型。
matrix<T,m,n>中的m,n爲1~4之間。dom

float2x2, float3x3, float4x4

intmxn也是能夠的。
2)初始化(直接或者構造函數方式賦值)

int2x2 m = {1, 2, 3, 4};
float2x2 f2x2 = float2x2(1.0f, 2.0f, 3.0f, 4.0f);

3)訪問和賦值
能夠用二維數組的下標語法訪問矩陣中的項,例如M[i] [j] = value;
此外,咱們能夠像訪問結構的成員那樣訪問矩陣M的項。下列條目已定義:

以1爲基數的:

M._11 = M._12 = M._13 = M._14 = 0.0f;

M._21 = M._22 = M._23 = M._24 = 0.0f;

M._31 = M._32 = M._33 = M._34 = 0.0f;

M._41 = M._42 = M._43 = M._44 = 0.0f;

以0爲基數的:

M._m00 = M._m01 = M._m02 = M._m03 = 0.0f;

M._m10 = M._m11 = M._m12 = M._m13 = 0.0f;

M._m20 = M._m21 = M._m22 = M._m23 = 0.0f;

M._m30 = M._m31 = M._m32 = M._m33 = 0.0f;

有時,咱們想要訪問矩陣中一個特定的行。咱們能夠用一維數組的下標語法來作。例如,要引用矩陣M中第i行的向量,咱們能夠寫:

vector ithRow = M[i]; // get the ith row vector in M

4.數組類型

數組類型和C++同樣,聲明時候能夠不用初始化。
例如:

float  M[4][4];
half   p[4];
vector v[12];

可是數組的大小是有限制的,不能太大了。
詳細參見:https://en.wikipedia.org/wiki/High-Level_Shading_Language
Constant registers,一個Constant register能夠存放一個vector也就是4個float, 那麼Pix Shader能夠存放56個常量matrix類型,只是理論上的。

在C++程序中能夠用傳遞數組的方式爲HLSL傳遞向量或者矩陣。例如:

FLOAT texSize[2] = {imageInfo.Width * 1.0f, imageInfo.Height * 1.0f};
MultiTexCT->SetFloatArray(Device, TexSizeHandle, texSize, 2);

 float 變長數組問題,老是但願程序裏面傳遞一個參數給HLSL來肯定HLSL的數組或者矩陣大小,可是不能作到,只能經過其它途徑解決。

const static int g_num = num;
float fVec[g_num][g_num];

形式給出,而不能用float fVec[g_num * 2]一維的來處理。
這樣設置也是不行的,由於不支持變長的數組,仍是須要明確的數組,若是要計算就在應用程序計算設置進去,而後HLSL根據須要標記進行區分。
或者設置一個足夠大的二維數組,而後只用少部分的;或者設計一個少的數組,而後用屢次Pass繪製來解決,繪製時候:

for each mesh being rendered
for each light affecting the mesh
if (first light)
    render first light with ambient and no blending
else
    render nth light with no ambient and additive belnding

5.結構體

結構的定義和在C++裏同樣。可是,HLSL裏的結構不能有成員函數。

struct MyStruct
{
     matrix T;
     vector n;
     float  f;
     int    x;
     bool   b;
};

MyStruct s; // instantiate
s.f = 5.0f; // member access

II、typedef關鍵字
HLSL的typedef關鍵字功能和C++裏的徹底同樣。例如,咱們能夠給類型vector<float, 3>用下面的語法命名:

typedef vector<float, 3> point;

而後,不用寫成:

vector<float, 3> myPoint;

咱們只需這樣寫:

point myPoint;

這裏是另外兩個例子,它展現瞭如何對常量和數組類型使用typedef關鍵字:

typedef const float CFLOAT;
typedef float point2[2];

III、變量前綴

extern——若是變量以extern關鍵字爲前綴,就意味着該變量可在着色器外被訪問,好比被C++應用程序。僅全局變量能夠以extern關鍵字爲前綴。不是static的全局變量默認就是extern。

uniform——若是變量以uniform關鍵字爲前綴,就意味着此變量在着色器外面被初始化,好比被C++應用程序初始化,而後再輸入進着色器。

const——HLSL中的const關鍵字和C++裏的意思同樣。也就是說,若是變量以const爲前綴,那此變量就是常量,而且不能被改變。

static——若是帶static關鍵字前綴,若它是全局變量,就表示它不是暴露於着色器以外的。換句話說,它是着色器局部的。若是一個局部變量 以static關鍵字爲前綴,它就和C++中static局部變量有相同的行爲。也就是說,該變量在函數首次執行時被一次性初始化,而後在全部函數調用中 維持其值。若是變量沒有被初始化,它就自動初始化爲0。

shared——若是變量以shared關鍵字爲前綴,就提示效果框架:變量將在多個效果間被共享。僅全局變量能夠以shared爲前綴。

volatile——若是變量以volatile關鍵字爲前綴,就提示效果框架:變量將時常被修改。僅全局變量能夠以volatile爲前綴。

其它關鍵字:

注意內建類型,sampler, texture, compile, decl關鍵字用法。

IV、運算符和類型轉換

    運算符,%符號能夠用於整數和浮點數,左右操做數需同號。
    許多HLSL的運算(+,-, * , / )都是在向量的份量級上進行的,v++,u++, u*v。
    比較運算也是在份量上操做的。
    類型轉換和C同樣,能夠強轉且類型精度,維度會提高。
雙目運算符中的類型提高:
值類型提高 bool < int < half < float < double.
維數提高 float定義了轉換到float2,float3不一樣份量上是取相同的數值,故能夠轉換爲float2, float3。而float2沒有定義轉換到float3的,因此不能進行提高轉換。

V、語句
 for循環不能從負數開始,且先後定義的for裏面的變量不能重複,不然會致使衝突。

VI、自定義函數和內置函數
按值傳遞,不支持遞歸,老是內聯的(因此函數要儘可能短小)。
函數形參增長了in, out, inout修飾,默認是int類型。
內置的函數對各標量,向量,矩陣類型的大部分都作了重載,故是能夠直接使用的。
更多的內置函數見:https://msdn.microsoft.com/en-us/library/windows/desktop/ff471376%28v=vs.85%29.aspx

頂點着色器和像素着色器原理和實現

1.原理

頂點着色器的輸入是從物體座標系開始包含位置、頂點法向量、紋理UV值;輸出是到設備規範座標系的頂點位置、顏色和紋理uv值(沒有了頂點法向量),過程當中包括變換和光照,還有對頂點的大小,點焊接,面拆分,LOD邊坍塌,LOD反演點拆分等技術。後面對頂點通過屏幕變換,背面剔除,深度測試, 根據頂點對面片進行uv插值計算,或者顏色(或光照材質顏色)插值。進入像素着色器,像素着色器的輸入是插值後的像素UV紋理座標和像素的顏色,像素着色器狹隘的說是代替多紋理融合着色階段(操做單個像素和每一個像素的紋理座標的能力)其中採用器索引和第i層紋理的關聯能夠在程序中用D3DXCONSTANT_DESC TexDesc來關聯,內部功能包含了alpha測試,stencil測試,顏色融合;像素着色器的輸出是像素的顏色。

頂點着色器(3D shaders)
頂點數據流:頂點格式中相同的類型索引含義和HLSL 頂點着色器輸入結構含義,經過Usage使用類型和UsageIndex使用類型索引進行映射關聯。若是不是很複雜的頂點聲明其實能夠用FVF在程序裏面作,FVF會在渲染管道過程當中會轉換爲頂點聲明。若是有頂點着色器,那麼頂點着色器輸出後,進行屏幕變換,背面裁剪和深度檢測,根據頂點進行面片uv插值或者顏色插值後,那麼獲得的輸出(沒有了像素位置說法,只有像素uv,和顏色)就是像素着色器的輸入。若是沒有頂點着色器,只有像素着色器,那麼像素着色器的輸入就和頂點聲明中的紋理uv和顏色對應做爲輸入(不須要頂點位置和頂點法向量信息 )。
3D shaders act on 3D models or other geometry but may also access the colors and textures used to draw the model or mesh. Vertex shaders are the oldest type of 3d shader, generally modifying on a per-vertex basis. Geometry shaderscan generate new vertices from within the shader.Tessellation shaders are newer 3d shaders that act on batches of vertexes all at once to add detail - such as subdividing a model into smaller groups of triangles or other primitives at runtime, to improve things like curves and bumps, or change other attributes.

As of OpenGL 4.0 and Direct3D 11, a new shader class called a Tessellation Shader has been added. It adds two new shader stages to the traditional model. Tessellation Control Shaders(also known as Hull Shaders) and Tessellation Evaluation Shaders (also known as Domain Shaders), which together allow for simpler meshes to be subdivided into finer meshes at run-time according to a mathematical function. The function can be related to a variety of variables, most notably the distance from the viewing camera to allow active level-of-detail scaling. This allows objects close to the camera to have fine detail, while further away ones can have more coarse meshes, yet seem comparable in quality.

像素着色器(2D shaders)
在頂點聲明中多重紋理座標須要多個uv,和D3DFVF_TEXT3來指明,相同大小的紋理圖能夠用一個uv來聲明。
在紋理採樣階段,用sampler來聲明,能夠用更安全的sampler2D,sampler3D來聲明更安全,採樣器專門使用tex*相關的內置函數來實現。像素着色器,是惟一一個在光柵化後可以修改和過濾像素值的通道。像素着色器在光柵化後,可是若是有修改深度緩存,模板值,顏色值硬件仍是能夠再進行特殊處理,實現更加豐富的顏色效果的。
Pixel shaders, also known asfragment shaders, compute color and other attributes of each "fragment" - a technical term usually meaning a single pixel. 
For instance, a pixel shader is the only kind of shader that can act as a postprocessor or filter for a video stream after it has been rasterized.

程序中Draw命令只是提交渲染,纔是圖形渲染開始的第一步,也就是一個batch提交,還須要commandBuffer, driverBuffer提交到給顯卡硬件輸入,後面纔開始變換和光照(攝像機空間),屏幕變換背面剔除深度剔除、面片插值和像素着色。

2.頂點着色器和像素着色器關鍵函數和實現步驟

0)編寫HLSL Shader腳本,用notpad++和HLSL語法高亮插件http://www.discoverthat.co.uk/games/edit-shaders.htm:
例如:

struct VS_INPUT
{
    vector position  : POSITION;
};
// Output structure describes the vertex that is
// output from the shader.  Here the output
// vertex contains a position and color component.
struct VS_OUTPUT
{
    vector position : POSITION;
    vector diffuse  : COLOR;
};
//
// Main Entry Point, observe the main function
// receives a copy of the input vertex through
// its parameter and returns a copy of the output
// vertex it computes.
//
VS_OUTPUT Main(VS_INPUT input)
{
    // zero out members of output
    VS_OUTPUT output = (VS_OUTPUT)0;
 
    // transform to view space and project
    output.position  = mul(input.position, ViewProjMatrix);
    // set vertex diffuse color to blue
    output.diffuse = Blue;
    return output;
}


若是沒有使用INPUT,OUTPUT中聲明時要求的使用寄存器,那麼main函數中須要指明輸入輸出寄存器,例如:

float4 Main(in float2 base:TEXCOORD0,
                 in float2 spot:TEXCOORD1,
                 in float2 text:TEXCOORD2):COLOR
                 {
                 }

1)用D3DXCompileShaderFromFile函數在C++代碼中編譯HLSL腳本獲得Shader內部代碼字節和常量句柄ID3DXConstantTable對象。
2)用CreateVertexShader或CreatePixelShader,從Shader內部代碼字節獲得IDirect3DVertexShader9頂點着色器或者IDirect3DPixelShader9像素着色器。
3)用常量句柄TransformConstantTable->SetDefaults(Device);初始化Shader中的常量。
4)用常量句柄獲得Shader內的常量句柄,併爲其設置值,例如:

D3DXHANDLE TransformViewProjHandle = 0;
TransformViewProjHandle = TransformConstantTable->GetConstantByName(0, "ViewProjMatrix");

TransformConstantTable->SetMatrix(
   Device,
   TransformViewProjHandle,
   &ViewProj);
FLOAT texSize[2] = {imageInfo.Width * 1.0f, imageInfo.Height * 1.0f};
 MultiTexCT->SetFloatArray(Device, TexSizeHandle, texSize, 2);
 MultiTexCT->SetInt(Device, ArraySizeHandle, 3);

IDirect3DPixelShader9* MultiTexPS = 0;
ID3DXConstantTable* MultiTexCT    = 0;

 ID3DXBuffer* shader      = 0;
 ID3DXBuffer* errorBuffer = 0;
 hr = D3DXCompileShaderFromFile(
  "mohu_texture.txt",
  0,
  0,
  "main", // entry point function name
  "ps_2_0",
  D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY/*D3DXSHADER_DEBUG*/,
  &shader,
  &errorBuffer,
  &MultiTexCT);

5)若是是像素着色器中存在紋理,還要用紋理對象和Shader中的採樣器關聯和設置採樣參數:

IDirect3DTexture9* BaseTex      = 0;
D3DXHANDLE BaseTexHandle      = 0;
D3DXCONSTANT_DESC BaseTexDesc;

// 實際紋理對象
D3DXCreateTextureFromFile(Device, "crate.bmp", &BaseTex);

// Shader紋理採樣器句柄
BaseTexHandle      = MultiTexCT->GetConstantByName(0, "BaseTex");
MultiTexCT->GetConstantDesc(BaseTexHandle,      &BaseTexDesc, &count);

// 在繪製時候,經過shader採樣器描述,爲多通道的紋理,設置紋理層級和實際紋理 的關聯;從而也使得紋理對象和採樣器對象關聯
Device->SetTexture(     BaseTexDesc.RegisterIndex, BaseTex);
// 在繪製時候,設置紋理採樣狀態參數
  Device->SetSamplerState(BaseTexDesc.RegisterIndex, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  Device->SetSamplerState(BaseTexDesc.RegisterIndex, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  Device->SetSamplerState(BaseTexDesc.RegisterIndex, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

6)設置啓用頂點着色器或者像素着色器

Device->SetPixelShader(MultiTexPS);

7)清理釋放着色器和常量句柄和紋理對象

d3d::Release<ID3DXMesh*>(Teapot); // d3d::Release<IDirect3DVertexBuffer9*>(QuadVB);
d3d::Release<IDirect3DVertexShader9*>(TransformShader);
d3d::Release<ID3DXConstantTable*>(TransformConstantTable);
d3d::Release<IDirect3DTexture9*>(BaseTex);

效果框架原理和實現
effect其實整合了render state和shader的控制兩大部份內容。
關於render state部分
HLSL中的內置類型(特別在效果文件中用得多)見:DX Graphics Documentation文檔目錄\HLSL\Reference\Language Syntax\Variables\Data Types下面有sampler texture PixelShader VertexShader。

內置函數見:
DX Graphics Documentation文檔目錄\HLSL\Reference\HLSL Intrinsic Functions。

HLSL效果文件中的內置狀態和值見:
DX Graphics Documentation文檔目錄\Direct3dD 9\Reference\Effect Reference\Effect Format\Effect States下。
effect state [ [index] ] = expression;形式,外面的[]意思是可選的,內部括號Index是當須要使用數組狀態時候(支持多下標的是要指明下標的,例如紋理層,光照個數)標識的當前索引狀態。
State是上述文檔路徑中的各類state。

最後記得效果文件Pass中引用效果文件不管是本身設置,仍是應用程序設置的變量,都要用括號括起來,不然是非法的。

關於shader部分
建立效果,設置參數,多個技術是由於不一樣硬件須要不一樣的技術GetTechnique;激活技術SetTechnique;開啓技術BeginPass;設置繪製物體前的繪製過程Pass(i)繪製過程有多個是由於設置不一樣的渲染狀態,紋理採用器採樣方法,材質,光照等須要屢次繪製物體;繪製物體DrawIndexPrimitive或DrawSubset等提交一個batch;關閉技術EndPass。效果文件pass中獲取外部和sampler變量須要用括號括起來。
The first step is to organize the state you want to control in an effect. This includes shader state (vertex, hull, domain, geometry, pixel and compute shaders), texture and sampler state used by the shaders, and other non-programmable pipeline state. 

The Geometry shader can generate new graphics primitives, such as points, lines, and triangles, from those primitives that were sent to the beginning of the graphics pipeline。
Geometry shader programs are executed after vertex shaders. They take as input a whole primitive, possibly with adjacency information. For example, when operating on triangles, the three vertices are the geometry shader's input. The shader can then emit zero or more primitives, which are rasterized and their fragments ultimately passed to a pixel shader。

The compute shader provides memory sharing and thread synchronization features to allow more effective parallel programming methods.

Shader渲染自己是支持多通道渲染,在寫程序上的所謂多通道應該是分爲串行的和並行的,在串行的當中(GPU計算內部對提交大量的計算會進行並行處理因此不用關心 ),若是繪製的物體是不一樣的物體,位置不同,那麼須要每次都設置世界座標轉換,而後進行設置技術繪製過程進行渲染; 若是是繪製的物體是相同的物體,例如鏡面正交投影,那麼能夠在繪製過程當中設置不一樣的繪製狀態,紋理採用,模板,融合,光照材質等進行繪製。若是是繪製的並行的多通道應該是在頂點聲明中,使用不一樣的通道,如何控制提升性能有待更深刻的積累?
Shaders的頂點着色器處理網格中的不少頂點能夠同時並行的進行,後臺緩存中的不少像素也能夠不少像素能夠同時進行像素着色處理。
Shaders are written to apply transformations to a large set of elements at a time, for example, to each pixel in an area of the screen, or for every vertex of a model. This is well suited to parallel processing, and most modern GPUs have multiple shader pipelines to facilitate this, vastly improving computation throughput.

效果實現
(0).先編寫Fx腳本。
(1).D3DXCreateEffectFromFile獲取效果對象,用效果對象來代替常量表對象來獲取和設置fx內的Shader變量值。
(2).調用Effect來渲染步驟:
 用GetTechniqueByName獲取技術句柄
SetTechnique激活技術句柄

ID3DXEffect::Begin  啓動技術句柄

ID3DXEffect::BeginPass 開啓繪製過程

ID3DXEffect::CommitChanges提交對當前pass的各類參數修改,要在draw以前調用

ID3DXEffect::EndPass 關閉繪製過程

ID3DXEffect::End 關閉技術句柄

(3).釋放效果框架對象

 d3d::Release<ID3DXEffect*>(FogEffect);

0)編寫Fx腳本
例如,沒有着色器的版本:

technique Fog
{
    pass P0
    {
        //
        // Set Misc render states.

        pixelshader      = null;
        vertexshader     = null;
        fvf              = XYZ | Normal;
        Lighting         = true;
        NormalizeNormals = true;
        SpecularEnable   = false;

        //
        // Fog States

        FogVertexMode = LINEAR; // linear fog function
        FogStart      = 50.0f;  // fog starts 50 units away from viewpoint
        FogEnd        = 300.0f; // fog ends 300 units away from viewpoint

        FogColor      = 0x00CCCCCC; // gray
        FogEnable     = true;       // enable
    }
}

有着色器的版本:

extern matrix WorldViewMatrix;
extern matrix WorldViewProjMatrix;

extern vector Color;
extern vector LightDirection;
extern texture ShadeTex;
//
// Structures
//

struct VS_INPUT
{
    vector position : POSITION;
    vector normal   : NORMAL;
};

struct VS_OUTPUT
{
    vector position : POSITION;
    float2 uvCoords : TEXCOORD;
    vector diffuse  : COLOR;
};
//
// Main
//

VS_OUTPUT Main(VS_INPUT input)
{
    // zero out each member in output
    VS_OUTPUT output = (VS_OUTPUT)0;


    // transform vertex position to homogenous clip space
     output.position = mul(input.position, WorldViewProjMatrix);

    //
    // Transform lights and normals to view space.  Set w
    // components to zero since we're transforming vectors.
    // Assume there are no scalings in the world
    // matrix as well.
    //
    LightDirection.w = 0.0f;
    input.normal.w   = 0.0f;
    LightDirection   = mul(LightDirection, WorldViewMatrix);
    input.normal     = mul(input.normal, WorldViewMatrix);

    //
    // Compute the 1D texture coordinate for toon rendering.
    //
    float u = dot(LightDirection, input.normal);

    //
    // Clamp to zero if u is negative because u
    // negative implies the angle between the light
    // and normal is greater than 90 degrees.  And
    // if that is true then the surface receives
    // no light.
    //
    if( u < 0.0f )
        u = 0.0f;

    //
    // Set other tex coord to middle.
    //
    float v = 0.5f;


    output.uvCoords.x = u;
    output.uvCoords.y = v;

    // save color
    output.diffuse = Color;
   
    return output;
}

//
// Sampler
//

sampler ShadeSampler = sampler_state
{
    Texture   = (ShadeTex);
    MinFilter = POINT; // no filtering for cartoon shading
    MagFilter = POINT;
    MipFilter = NONE;
};


//
// Effect
//

technique Toon
{
    pass P0
    {
        vertexShader = compile vs_1_1 Main();

        Sampler[0] = (ShadeSampler);
    }
}

HLSL效果框架pass,要得到程序外部設置的變量須要用括號()括起來。
效果框架pass要得到sampler構造的採樣器也要用()括號括起來。

1 )獲取效果框架對象, 獲取常量句柄和設置句柄值
用D3DXCreateEffectFromFile從Fx文件獲取效果對象指針

ID3DXEffect* FogEffect   = 0;
ID3DXBuffer* errorBuffer = 0;
 hr = D3DXCreateEffectFromFile(
  Device,
  "fog.txt",
  0,                // no preprocessor definitions
  0,                // no ID3DXInclude interface
  D3DXSHADER_DEBUG, // compile flags
  0,                // don't share parameters
  &FogEffect,
  &errorBuffer);


用效果對象來代替常量表對象來獲取和設置fx內的Shader變量值
例如:

ID3DXBuffer* errorBuffer = 0;
 hr = D3DXCreateEffectFromFile(
  Device,
  "light_tex.txt",
  0,                // no preprocessor definitions
  0,                // no ID3DXInclude interface
  D3DXSHADER_DEBUG, // compile flags
  0,                // don't share parameters
  &LightTexEffect,
  &errorBuffer);

WorldMatrixHandle  = LightTexEffect->GetParameterByName(0, "WorldMatrix");
 ViewMatrixHandle   = LightTexEffect->GetParameterByName(0, "ViewMatrix");
 ProjMatrixHandle   = LightTexEffect->GetParameterByName(0, "ProjMatrix");
 TexHandle          = LightTexEffect->GetParameterByName(0, "Tex");

LightTexEffect->SetMatrix( WorldMatrixHandle, &W);
LightTexEffect->SetMatrix(ViewMatrixHandle, &V);
LightTexEffect->SetMatrix( ProjMatrixHandle, &P);
 
IDirect3DTexture9* tex = 0;
 D3DXCreateTextureFromFile(Device, "Terrain_3x_diffcol.jpg", &tex);
 LightTexEffect->SetTexture(TexHandle, tex);

2 )  繪製過程
用GetTechniqueByName獲取技術句柄

D3DXHANDLE FogTechHandle = 0;
FogTechHandle = FogEffect->GetTechniqueByName("Fog");

用SetTechnique激活技術句柄

FogEffect->SetTechnique( FogTechHandle );

用Begin啓動技術句柄並獲取繪製過程數

FogEffect->Begin(&numPasses, 0);

用BeginPass和開啓繪製過程。

在BeginPass後用CommitChanges提交對當前pass的各類參數修改,要在draw以前調用。

用Draw函數真正繪製物體,繪製過程當中設定了頂點變換,繪製狀態,紋理採樣器狀態,材質光照算法等。

EndPass關閉繪製過程,不關閉會致使內部異常而dump機。

for(int i = 0; i < numPasses; i++)
  {
   FogEffect->BeginPass(i);

   if( TheTerrain )
    TheTerrain->draw(&I, false);

   FogEffect->EndPass();
  }

用End函數關閉技術。

FogEffect->End();

3)清理效果框架對象

void Cleanup()
{
 d3d::Delete<Terrain*>(TheTerrain);
 d3d::Release<ID3DXEffect*>(FogEffect);
}

開發經驗
1)頂點着色器例子,卡通着色,用的是一個灰度紋理
將向量和光照夾角餘弦做爲灰度紋理的u,v=0.5來實現根據光照進行紋理的採樣。
卡通着色的輪廓勾畫或描邊,能夠用爲輪廓邊增長厚度着色來作到,經過爲網格中的面片中的全部邊生成一個直線類型的四邊形,原來的面片不變,用判斷邊是不是輪廓邊來對新生成的線條邊的其中兩個頂點進行偏移,那麼就會對輪廓邊產生一個新的描邊面片而且着色上去,這樣就將輪廓邊勾畫出來了。

2)像素着色的多重紋理,能夠在靜態物體和靜態場景中經過場景物體和光照貼圖融合
獲得靜態光照效果引擎。若是是動態的物體那麼嗨須要光源來進行實時光照纔會獲得較好的光照效果。

像素着色器顏色vector是4維的。

vector a = a0 + vector(-fMoHuScale, -fMoHuScale,0.0f,0.0f); 
vector b = a0 + vector(-fMoHuScale, fMoHuScale,0.0f,0.0f);
vector c = a0 + vector(fMoHuScale, -fMoHuScale,0.0f,0.0f);
vector d = a0 + vector(fMoHuScale, fMoHuScale,0.0f,0.0f);
// combine texel colors
vector cc = (a + b + c + d) / 16.0f;

 

顏色值若是是D3DCOLOR類型的,是用D3DCOLOR_ARGB賦值的,那麼是ARGB類型的顏色,例如效果文件中的FogColor的顏色。
顏色值若是是D3DCOLORVALUE類型的,例如光照,材質中的顏色,那麼是RGBA類型的。


3)常量表中的常量確實爲常量,若是要在HLSL裏面修改它,那麼須要經過聲明其它的變量來實現或者加載着色器文件時候用標識D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3DXSHADER_DEBUG;調試事後用D3DXSHADER_SKIPVALIDATION不須要驗證。

4) 計算時候必定要儘可能簡單,不然太多的全局變量,和算法內部太多的臨時變量,會致使編譯失敗。

5)多個Shader着色器之間,DrawPrimitive或者DrawSubset時候會提交一批
若是是像素着色器那麼不會有問題,若是是頂點着色器繪製不一樣的物體那麼須要SetTranform(D3DTS_WORLD, &curObjMatrix)來從新指明如何將當前的物體座標系變換到世界座標系中,而後再進行繪製渲染便可。

6 )勾畫輪廓邊,描邊總結的三角網格鄰接信息
/*須要知道當前操做的網格信息的類型,例如DX默認內置建立的物體的頂點結構,還須要取得或生產鄰接信息用網格對象GenerateAdjacency獲得,網格頂點結構以下:

struct MeshVertex
{
 D3DXVECTOR3 position;
 D3DXVECTOR3 normal;
 static const DWORD FVF;
};
struct EdgeVertex
{
 D3DXVECTOR3 position;
 D3DXVECTOR3 normal;
 D3DXVECTOR3 faceNormal1;
 D3DXVECTOR3 faceNormal2;
};

 

頂點聲明:

IDirect3DVertexDeclaration9* _decl;
D3DVERTEXELEMENT9 decl[] =
 {
  // offsets in bytes
  {0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
  {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},
  {0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   1},
  {0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   2},
  D3DDECL_END()
 };
 hr = _device->CreateVertexDeclaration(decl, &_decl);
 if(FAILED(hr))
 {
  ::MessageBox(0, "CreateVertexDeclaration() - FAILED", 0, 0);
  return false;
 }
void SilhouetteEdges::render()
{
// 頂點聲明的使用,是和頂點着色器或像素着色器,輸入相對應的,例如見EdgeVertex
 _device->SetVertexDeclaration(_decl);
 _device->SetStreamSource(0, _vb, 0, sizeof(EdgeVertex));
 _device->SetIndices(_ib);
 _device->DrawIndexedPrimitive(
  D3DPT_TRIANGLELIST, 0, 0, _numVerts, 0, _numFaces);
}
d3d::Release<IDirect3DVertexDeclaration9*>(_decl);

(1).從邊考慮(而不是頂點),每條邊有4個頂點;非輪廓邊由於是退化四邊形獲得的退化三角形不會被繪製,輪廓邊是通過移動後的三角形
因此會繪製。
(2).輪廓邊須要其中兩個頂點產生偏移,另外兩個頂點不動而生成三角形,渲染該三角形就獲得描邊。
(3).判斷是不是輪廓邊,經過物體頂點在攝像機空間中的位置向量(就是頂點到攝像機原點的向量)和邊構造的4個頂點中的每一個頂點的
當前三角形法向量和鄰接邊三角形法向量進行點乘,若是兩個點乘後的值符號相反那麼是輪廓邊須要對頂點沿着頂點法向量方向平移並着色,
若是相同那麼是非輪廓邊,不須要平移當作退化三角形來處理。例如:

extern matrix WorldViewMatrix;
extern matrix ProjMatrix;

static vector Black = {0.9f, 0.0f, 0.0f, 0.0f};

//
// Structures
//

struct VS_INPUT
{
 vector position    : POSITION;
 vector normal      : NORMAL0;
 vector faceNormal1 : NORMAL1;
 vector faceNormal2 : NORMAL2;
};

struct VS_OUTPUT
{
 vector position : POSITION;
 vector diffuse  : COLOR;
};

//
// Main
//

VS_OUTPUT Main(VS_INPUT input)
{
 // zero out each member in output
 VS_OUTPUT output = (VS_OUTPUT)0;

 // transform position to view space
 input.position = mul(input.position, WorldViewMatrix);

 // Compute a vector in the direction of the vertex
 // from the eye.  Recall the eye is at the origin
 // in view space - eye is just camera position.
 vector eyeToVertex = input.position;

 // transform normals to view space.  Set w
 // components to zero since we're transforming vectors.
 // Assume there are no scalings in the world
 // matrix as well.
 input.normal.w      = 0.0f;
 input.faceNormal1.w = 0.0f;
 input.faceNormal2.w = 0.0f;

 input.normal      = mul(input.normal,      WorldViewMatrix);
 input.faceNormal1 = mul(input.faceNormal1, WorldViewMatrix);
 input.faceNormal2 = mul(input.faceNormal2, WorldViewMatrix);

 // compute the cosine of the angles between
 // the eyeToVertex vector and the face normals.
 float dot0 = dot(eyeToVertex, input.faceNormal1);
 float dot1 = dot(eyeToVertex, input.faceNormal2);

 // if cosines are different signs (positive/negative)
 // than we are on a silhouette edge.  Do the signs
 // differ?
 if( (dot0 * dot1) < 0.0f )
 {
 // yes, then this vertex is on a silhouette edge,
 // offset the vertex position by some scalar in the
 // direction of the vertex normal.
 input.position += 0.05f * input.normal;
 }

 // transform to homogeneous clip space
 output.position = mul(input.position, ProjMatrix);

 // set outline color
 output.diffuse = Black;

 return output;
}

(4).從網格信息和鄰接信息對象中構建邊的頂點緩存和索引緩存,鄰接信息真正的有用關係,鄰接信息和索引緩存大小同樣,且和索引緩存對應,鄰接信息緩存key是和索引緩存同樣(概念爲邊),
value沒有DX中是USHRT_MAX,有是和當前三角形相鄰的三角形編號,該編號表明索引下標3*value, 3*value + 1, 3*value + 2表明的
三角形,而索引上的value表明的是頂點緩存的key,這樣能夠經過鄰接信息是能夠獲取到相鄰的三角形的頂點信息的。例如:

for(int i = 0; i < mesh->GetNumFaces(); i++)
{
    // 根據當前的面片信息,獲取當前的緩存信息,由於索引緩存和三角形的關係是,索引下標(3*i, 3*i + 1, 3*i + 2)肯定當前編號爲i的三角形
    WORD index0 = indices[i * 3];
    WORD index1 = indices[i * 3 + 1];
    WORD index2 = indices[i* 3 + 2];

    // Now extract the triangles vertices positions
    // 索引緩存上的值是頂點緩存的下標,經過頂點緩存獲得頂點信息
    D3DXVECTOR3 v0 = vertices[index0].position;
    D3DXVECTOR3 v1 = vertices[index1].position;
    D3DXVECTOR3 v2 = vertices[index2].position;

    // 根據當前的面片信息,獲得當前的鄰接信息,由於鄰接信息和索引緩存對應的,鄰接信息下標(3*i, 3*i + 1, 3*i + 2)肯定當前編號爲i的三角形
    // 鄰接信息上的值是表明當前三角形邊,相鄰的三角形的編號。
    WORD faceIndex0 = adj[i * 3];
    WORD faceIndex1 = adj[i* 3 + 1];
    WORD faceIndex2 = adj[i * 3 + 2];
    // 獲得了鄰接三角形的編號,就能夠獲得鄰接三角形的索引緩存值,從而獲得頂點緩存值
    if( faceIndex0 != USHRT_MAX ) // is there an adjacent triangle?
    {
    WORD i0 = indices[faceIndex0 * 3];
    WORD i1 = indices[faceIndex0 * 3 + 1];
    WORD i2 = indices[faceIndex0 * 3 + 2];

    D3DXVECTOR3 v0 = vertices[i0].position;
    D3DXVECTOR3 v1 = vertices[i1].position;
    D3DXVECTOR3 v2 = vertices[i2].position;

    D3DXVECTOR3 edge0 = v1 - v0;
    D3DXVECTOR3 edge1 = v2 - v0;
    D3DXVec3Cross(&faceNormal0, &edge0, &edge1);
    D3DXVec3Normalize(&faceNormal0, &faceNormal0);
    }
}

(5). 索引緩存能夠經過觀察頂點緩存的規律,頂點緩存是四個頂點進行排序的,那麼四個頂點的索引緩存就要6個索引,且他們之間是有相關
順序的,例如:

void SilhouetteEdges::genEdgeIndices(ID3DXMesh* mesh)
{
 DWORD numEdges = mesh->GetNumFaces() * 3;

 _numFaces = numEdges * 2;

 _device->CreateIndexBuffer(
 numEdges * 6 * sizeof(WORD), // 2 triangles per edge
 D3DUSAGE_WRITEONLY,
 D3DFMT_INDEX16,
 D3DPOOL_MANAGED,
 &_ib,
 0);

 WORD* indices = 0;

 _ib->Lock(0, 0, (void**)&indices, 0);

 // 0        1
 // *--------*
 // |  edge  |
 // *--------*
 // 2        3

 for(UINT i = 0; i < numEdges; i++)
 {
 // Six indices to define the triangles of the edge,
 // so every edge we skip six entries in the
 // index buffer.  Four vertices to define the edge,
 // so every edge we skip four entries in the
 // vertex buffer.
 indices[i * 6]     = i * 4 + 0;
 indices[i * 6 + 1] = i * 4 + 1;
 indices[i * 6 + 2] = i * 4 + 2;
 indices[i * 6 + 3] = i * 4 + 1;
 indices[i * 6 + 4] = i * 4 + 3;
 indices[i * 6 + 5] = i * 4 + 2;
 }

 _ib->Unlock();
}

(7)啓用頂點着色器和繪製輪廓邊,以前的正常網格正常繪製,輪廓邊後面繪製

Device->SetVertexShader(ToonShader);
Device->SetTexture(0, ShadeTex);
Meshes[i]->DrawSubset(0);

Device->SetVertexShader(OutlineShader);
Device->SetTexture(0, 0);
OutlineConstTable->SetMatrix(
 Device,
 OutlineWorldViewHandle,
 &worldView);

OutlineConstTable->SetMatrix(
 Device,
 OutlineProjHandle,
 &ProjMatrix);

MeshOutlines[i]->render();

 

7)效果文件的參考信息能夠從SDK文檔中獲取到,例如效果文件中的內置類型,內置函數,和狀態類型。
8) 當用屬性子集繪製物體時候,DrawSubset下標和網格的材質相關,而和Fx中的繪製次數無關
   Fx繪製次數只是說須要不一樣的渲染狀態下,不一樣的頂點和像素着色器下屢次繪製同一個物體。

UINT numPasses = 0;
  LightTexEffect->Begin(&numPasses, 0);

   for(int i = 0; i < numPasses; i++)
   {
   // 這裏繪製會致使部分頂點變換都沒有進行,於是渲染失敗
   //ToonEffect->BeginPass(i);
    for( int j = 0; j < int(Mtrls.size()); j++)
    {
        // 正確的在此繪製,且繪製下標爲i,保證每一個物體或者屬性子集都要進行pass(i)的繪製,
     // 才能將每一個物體或者部分頂點和像素渲染正確
         LightTexEffect->BeginPass(i);
          LightTexEffect->CommitChanges();

         Device->SetMaterial(&(Mtrls[j]));
         Device->SetTexture(0, Textures[j]);
        Mesh->DrawSubset(j);

         LightTexEffect->EndPass();
      }
   
   }
  }
  LightTexEffect->End();

8)霧化效果能夠將場景中的真實感提高到一個新的層次,而且能夠模擬某種類型的天氣狀況。
並且霧化能夠避免遠裁剪面在視覺上帶給玩家不天然的感受。

technique Fog
{
    pass P0
    {
        //
        // Set Misc render states.

        pixelshader      = null;
        vertexshader     = null;
        fvf              = XYZ | Normal;
        Lighting         = true;
        NormalizeNormals = true;
        SpecularEnable   = false;

        //
        // Fog States

        FogVertexMode = LINEAR; // linear fog function
        FogStart      = 50.0f;  // fog starts 50 units away from viewpoint
        FogEnd        = 300.0f; // fog ends 300 units away from viewpoint

        FogColor      = 0x00CCCCCC; // gray
        FogEnable     = true;       // enable
    }
}

9)把程序中的句柄設置給效果文件參數句柄後就能夠釋放內存了,效果文件是拷貝了數據內存的
例如。

IDirect3DTexture9* tex = 0;
 D3DXCreateTextureFromFile(Device, "toonshade.bmp", &tex);
 ToonEffect->SetTexture(ShadeTexHandle, tex);
 d3d::Release<IDirect3DTexture9*>(tex);

10)效果文件中存放頂點着色器和像素着色器,除了pass中的渲染狀態設置,紋理設置,還有就是着色器的使用
直接在腳本里面編譯賦值給內建着色器類型便可,內建紋理採樣器類型也是同樣的,例如:

technique Toon
{
    pass P0
    {
        vertexShader = compile vs_3_0 Main();

        Sampler[0] = (ShadeSampler);
    }
}

———————————————
版權聲明:本文爲CSDN博主「Sam-Cen」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/Blues1021/article/details/47093487

HLSL API

Intrinsic Functions (DirectX HLSL)
The following table lists the intrinsic functions available in HLSL. Each function has a brief description, and a link to a reference page that has more detail about the input argument and return type.

Name Syntax Description
abs abs(x) Absolute value (per component).
acos acos(x) Returns the arccosine of each component of x.
all all(x) Test if all components of x are nonzero.
any any(x) Test if any component of x is nonzero.
asfloat asfloat(x) Convert the input type to a float.
asin asin(x) Returns the arcsine of each component of x.
asint asint(x) Convert the input type to an integer.
asuint asuint(x) Convert the input type to an unsigned integer.
atan atan(x) Returns the arctangent of x.
atan2 atan2(y, x) Returns the arctangent of of two values (x,y).
ceil ceil(x) Returns the smallest integer which is greater than or equal to x.
clamp clamp(x, min, max) Clamps x to the range [min, max].
clip clip(x) Discards the current pixel, if any component of x is less than zero.
cos cos(x) Returns the cosine of x.
cosh cosh(x) Returns the hyperbolic cosine of x.
cross cross(x, y) Returns the cross product of two 3D vectors.
D3DCOLORtoUBYTE4 D3DCOLORtoUBYTE4(x) Swizzles and scales components of the 4D vector x to compensate for the lack of UBYTE4 support in some hardware.
ddx ddx(x) Returns the partial derivative of x with respect to the screen-space x-coordinate.
ddy ddy(x) Returns the partial derivative of x with respect to the screen-space y-coordinate.
degrees degrees(x) Converts x from radians to degrees.
determinant determinant(m) Returns the determinant of the square matrix m.
distance distance(x, y) Returns the distance between two points.
dot dot(x, y) Returns the dot product of two vectors.
exp exp(x) Returns the base-e exponent.
exp2 exp2(x) Base 2 exponent (per component).
faceforward faceforward(n, i, ng) Returns -n * sign(•(i, ng)).
floor floor(x) Returns the greatest integer which is less than or equal to x.
fmod fmod(x, y) Returns the floating point remainder of x/y.
frac frac(x) Returns the fractional part of x.
frexp frexp(x, exp) Returns the mantissa and exponent of x.
fwidth fwidth(x) Returns abs(ddx(x)) + abs(ddy(x))
GetRenderTargetSampleCount GetRenderTargetSampleCount() Returns the number of render-target samples.
GetRenderTargetSamplePosition GetRenderTargetSamplePosition(x) Returns a sample position (x,y) for a given sample index.
isfinite isfinite(x) Returns true if x is finite, false otherwise.
isinf isinf(x) Returns true if x is +INF or -INF, false otherwise.
isnan isnan(x) Returns true if x is NAN or QNAN, false otherwise.
ldexp ldexp(x, exp) Returns x * 2exp
length length(v) Returns the length of the vector v.
lerp lerp(x, y, s) Returns x + s(y - x).
lit lit(n • l, n • h, m) Returns a lighting vector (ambient, diffuse, specular, 1)
log log(x) Returns the base-e logarithm of x.
log10 log10(x) Returns the base-10 logarithm of x.
log2 log2(x) Returns the base-2 logarithm of x.
max max(x, y) Selects the greater of x and y.
min min(x, y) Selects the lesser of x and y.
modf modf(x, out ip) Splits the value x into fractional and integer parts.
mul mul(x, y) Performs matrix multiplication using x and y.
noise noise(x) Generates a random value using the Perlin-noise algorithm.
normalize normalize(x) Returns a normalized vector.
pow pow(x, y) Returns xy.
radians radians(x) Converts x from degrees to radians.
reflect reflect(i, n) Returns a reflection vector.
refract refract(i, n, R) Returns the refraction vector.
round round(x) Rounds x to the nearest integer
rsqrt rsqrt(x) Returns 1 / sqrt(x)
saturate saturate(x) Clamps x to the range [0, 1]
sign sign(x) Computes the sign of x.
sin sin(x) Returns the sine of x
sincos sincos(x, out s, out c) Returns the sine and cosine of x.
sinh sinh(x) Returns the hyperbolic sine of x
smoothstep smoothstep(min, max, x) Returns a smooth Hermite interpolation between 0 and 1.
sqrt sqrt(x) Square root (per component)
step step(a, x) Returns (x >= a) ? 1 : 0
tan tan(x) Returns the tangent of x
tanh tanh(x) Returns the hyperbolic tangent of x
tex1D tex1D(s, t) 1D texture lookup.
tex1Dbias tex1Dbias(s, t) 1D texture lookup with bias.
tex1Dgrad tex1Dgrad(s, t, ddx, ddy) 1D texture lookup with a gradient.
tex1Dlod tex1Dlod(s, t) 1D texture lookup with LOD.
tex1Dproj tex1Dproj(s, t) 1D texture lookup with projective divide.
tex2D tex2D(s, t) 2D texture lookup.
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) Returns the transpose of the matrix m.
trunc trunc(x) Truncates floating-point value(s) to integer value(s)


表 3-1 HLSL內置函數

函數名            用法

abs                         計算輸入值的絕對值。

acos                        返回輸入值反餘弦值。

all                           測試非0值。

any                         測試輸入值中的任何非零值。

asin                         返回輸入值的反正弦值。

atan                        返回輸入值的反正切值。

atan2                       返回y/x的反正切值。

ceil                         返回大於或等於輸入值的最小整數。

clamp                      把輸入值限制在[min, max]範圍內。

clip                         若是輸入向量中的任何元素小於0,則丟棄當前像素。

cos                         返回輸入值的餘弦。

cosh                       返回輸入值的雙曲餘弦。

cross                      返回兩個3D向量的叉積。

ddx                         返回關於屏幕座標x軸的偏導數。

ddy                         返回關於屏幕座標y軸的偏導數。

degrees                   弧度到角度的轉換

determinant              返回輸入矩陣的值。

distance                   返回兩個輸入點間的距離。

dot                          返回兩個向量的點積。

exp                         返回以e爲底數,輸入值爲指數的指數函數值。

exp2                       返回以2爲底數,輸入值爲指數的指數函數值。

faceforward             檢測多邊形是否位於正面。

floor                       返回小於等於x的最大整數。

fmod                       返回a / b的浮點餘數。

frac                        返回輸入值的小數部分。

frexp                       返回輸入值的尾數和指數

fwidth                     返回 abs ( ddx (x) + abs ( ddy(x))。

isfinite                     若是輸入值爲有限值則返回true,不然返回false。

isinf                        如何輸入值爲無限的則返回true。

isnan                       若是輸入值爲NAN或QNAN則返回true。

ldexp                       frexp的逆運算,返回 x * 2 ^ exp。

len / lenth                返回輸入向量的長度。

lerp                         對輸入值進行插值計算。

lit                            返回光照向量(環境光,漫反射光,鏡面高光,1)。

log                          返回以e爲底的對數。

log10                      返回以10爲底的對數。

log2                        返回以2爲底的對數。

max                        返回兩個輸入值中較大的一個。

min                         返回兩個輸入值中較小的一個。

modf                       把輸入值分解爲整數和小數部分。

mul                         返回輸入矩陣相乘的積。

normalize                 返回規範化的向量,定義爲 x / length(x)。

pow                        返回輸入值的指定次冪。

radians                    角度到弧度的轉換。

reflect                     返回入射光線i對錶面法線n的反射光線。

refract                     返回在入射光線i,表面法線n,折射率爲eta下的折射光線v。

round                      返回最接近於輸入值的整數。

rsqrt                       返回輸入值平方根的倒數。

saturate                   把輸入值限制到[0, 1]之間。

sign                        計算輸入值的符號。

sin                          計算輸入值的正弦值。

sincos                     返回輸入值的正弦和餘弦值。

sinh                        返回x的雙曲正弦。

smoothstep              返回一個在輸入值之間平穩變化的插值。

sqrt                         返回輸入值的平方根。

step                        返回(x >= a)? 1 : 0。

tan                          返回輸入值的正切值。

fanh                        返回輸入值的雙曲線切線。

transpose                 返回輸入矩陣的轉置。

tex1D*                    1D紋理查詢。

tex2D*                    2D紋理查詢。

tex3D*                    3D紋理查詢。
texCUBE*                立方紋理查詢。

Intrinsic Functions (DirectX HLSL)
The following table lists the intrinsic functions available in HLSL. Each function has a brief description, and a link to a reference page that has more detail about the input argument and return type.

Name Description Minimum shader modelabs Absolute value (per component). 11acos Returns the arccosine of each component of x. 11all Test if all components of x are nonzero. 11AllMemoryBarrier Blocks execution of all threads in a group until all memory accesses have been completed. 5AllMemoryBarrierWithGroupSync Blocks execution of all threads in a group until all memory accesses have been completed and all threads in the group have reached this call. 5any Test if any component of x is nonzero. 11asdouble Reinterprets a cast value into a double. 5asfloat Convert the input type to a float. 4asin Returns the arcsine of each component of x. 11asint Convert the input type to an integer. 4asuint Reinterprets the bit pattern of a 64-bit type to a uint. 5asuint Convert the input type to an unsigned integer. 4atan Returns the arctangent of x. 11atan2 Returns the arctangent of of two values (x,y). 11ceil Returns the smallest integer which is greater than or equal to x. 11clamp Clamps x to the range [min, max]. 11clip Discards the current pixel, if any component of x is less than zero. 11cos Returns the cosine of x. 11cosh Returns the hyperbolic cosine of x. 11countbits Counts the number of bits (per component) in the input integer. 5cross Returns the cross product of two 3D vectors. 11D3DCOLORtoUBYTE4 Swizzles and scales components of the 4D vector xto compensate for the lack of UBYTE4 support in some hardware. 11ddx Returns the partial derivative of x with respect to the screen-space x-coordinate. 21ddx_coarse Computes a low precision partial derivative with respect to the screen-space x-coordinate. 5ddx_fine Computes a high precision partial derivative with respect to the screen-space x-coordinate. 5ddy Returns the partial derivative of x with respect to the screen-space y-coordinate. 21ddy_coarse Computes a low precision partial derivative with respect to the screen-space y-coordinate. 5ddy_fine Computes a high precision partial derivative with respect to the screen-space y-coordinate. 5degrees Converts x from radians to degrees. 11determinant Returns the determinant of the square matrix m. 11DeviceMemoryBarrier Blocks execution of all threads in a group until all device memory accesses have been completed. 5DeviceMemoryBarrierWithGroupSync Blocks execution of all threads in a group until all device memory accesses have been completed and all threads in the group have reached this call. 5distance Returns the distance between two points. 11dot Returns the dot product of two vectors. 1dst Calculates a distance vector. 5EvaluateAttributeAtCentroid Evaluates at the pixel centroid. 5EvaluateAttributeAtSample Evaluates at the indexed sample location. 5EvaluateAttributeSnapped Evaluates at the pixel centroid with an offset. 5exp Returns the base-e exponent. 11exp2 Base 2 exponent (per component). 11f16tof32 Converts the float16 stored in the low-half of the uint to a float. 5f32tof16 Converts an input into a float16 type. 5faceforward Returns -n * sign(dot(i, ng)). 11firstbithigh Gets the location of the first set bit starting from the highest order bit and working downward, per component. 5firstbitlow Returns the location of the first set bit starting from the lowest order bit and working upward, per component. 5floor Returns the greatest integer which is less than or equal to x. 11fmod Returns the floating point remainder of x/y. 11frac Returns the fractional part of x. 11frexp Returns the mantissa and exponent of x. 21fwidth Returns abs(ddx(x)) + abs(ddy(x)) 21GetRenderTargetSampleCount Returns the number of render-target samples. 4GetRenderTargetSamplePosition Returns a sample position (x,y) for a given sample index. 4GroupMemoryBarrier Blocks execution of all threads in a group until all group shared accesses have been completed. 5GroupMemoryBarrierWithGroupSync Blocks execution of all threads in a group until all group shared accesses have been completed and all threads in the group have reached this call. 5InterlockedAdd Performs a guaranteed atomic add of value to the dest resource variable. 5InterlockedAnd Performs a guaranteed atomic and. 5InterlockedCompareExchange Atomically compares the input to the comparison value and exchanges the result. 5InterlockedCompareStore Atomically compares the input to the comparison value. 5InterlockedExchange Assigns value to dest and returns the original value. 5InterlockedMax Performs a guaranteed atomic max. 5InterlockedMin Performs a guaranteed atomic min. 5InterlockedOr Performs a guaranteed atomic or. 5InterlockedXor Performs a guaranteed atomic xor. 5isfinite Returns true if x is finite, false otherwise. 11isinf Returns true if x is +INF or -INF, false otherwise. 11isnan Returns true if x is NAN or QNAN, false otherwise. 11ldexp Returns x * 2exp 11length Returns the length of the vector v. 11lerp Returns x + s(y - x). 11lit Returns a lighting vector (ambient, diffuse, specular, 1) 11log Returns the base-e logarithm of x. 11log10 Returns the base-10 logarithm of x. 11log2 Returns the base-2 logarithm of x. 11mad Performs an arithmetic multiply/add operation on three values. 5max Selects the greater of x and y. 11min Selects the lesser of x and y. 11modf Splits the value x into fractional and integer parts. 11mul Performs matrix multiplication using x and y. 1noise Generates a random value using the Perlin-noise algorithm. 11normalize Returns a normalized vector. 11pow Returns xy. 11Process2DQuadTessFactorsAvg Generates the corrected tessellation factors for a quad patch. 5Process2DQuadTessFactorsMax Generates the corrected tessellation factors for a quad patch. 5Process2DQuadTessFactorsMin Generates the corrected tessellation factors for a quad patch. 5ProcessIsolineTessFactors Generates the rounded tessellation factors for an isoline. 5ProcessQuadTessFactorsAvg Generates the corrected tessellation factors for a quad patch. 5ProcessQuadTessFactorsMax Generates the corrected tessellation factors for a quad patch. 5ProcessQuadTessFactorsMin Generates the corrected tessellation factors for a quad patch. 5ProcessTriTessFactorsAvg Generates the corrected tessellation factors for a tri patch. 5ProcessTriTessFactorsMax Generates the corrected tessellation factors for a tri patch. 5ProcessTriTessFactorsMin Generates the corrected tessellation factors for a tri patch. 5radians Converts x from degrees to radians. 1rcp Calculates a fast, approximate, per-component reciprocal. 5reflect Returns a reflection vector. 1refract Returns the refraction vector. 11reversebits Reverses the order of the bits, per component. 5round Rounds x to the nearest integer 11rsqrt Returns 1 / sqrt(x) 11saturate Clamps x to the range [0, 1] 1sign Computes the sign of x. 11sin Returns the sine of x 11sincos Returns the sine and cosine of x. 11sinh Returns the hyperbolic sine of x 11smoothstep Returns a smooth Hermite interpolation between 0 and 1. 11sqrt Square root (per component) 11step Returns (x >= a) ? 1 : 0 11tan Returns the tangent of x 11tanh Returns the hyperbolic tangent of x 11tex1D(s, t) 1D texture lookup. 1tex1D(s, t, ddx, ddy) 1D texture lookup. 21tex1Dbias 1D texture lookup with bias. 21tex1Dgrad 1D texture lookup with a gradient. 21tex1Dlod 1D texture lookup with LOD. 31tex1Dproj 1D texture lookup with projective divide. 21tex2D(s, t) 2D texture lookup. 11tex2D(s, t, ddx, ddy) 2D texture lookup. 21tex2Dbias 2D texture lookup with bias. 21tex2Dgrad 2D texture lookup with a gradient. 21tex2Dlod 2D texture lookup with LOD. 3tex2Dproj 2D texture lookup with projective divide. 21tex3D(s, t) 3D texture lookup. 11tex3D(s, t, ddx, ddy) 3D texture lookup. 21tex3Dbias 3D texture lookup with bias. 21tex3Dgrad 3D texture lookup with a gradient. 21tex3Dlod 3D texture lookup with LOD. 31tex3Dproj 3D texture lookup with projective divide. 21texCUBE(s, t) Cube texture lookup. 11texCUBE(s, t, ddx, ddy) Cube texture lookup. 21texCUBEbias Cube texture lookup with bias. 21texCUBEgrad Cube texture lookup with a gradient. 21texCUBElod Cube texture lookup with LOD. 31texCUBEproj Cube texture lookup with projective divide. 21transpose Returns the transpose of the matrix m. 1trunc Truncates floating-point value(s) to integer value(s)————————————————版權聲明:本文爲CSDN博主「博贏天下」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。原文連接:https://blog.csdn.net/plaxbsga/article/details/52787860

相關文章
相關標籤/搜索