轉自:https://blog.csdn.net/Blues1021/article/details/47093487
基本前提概念
Shader是一種映射到GPU硬件匯編語言上的高級語言,Shader中的數據類型是要聲明類型和用途的,用途其實就是和着色器寄存器關聯,輸入位置寄存器,輸出位置寄存器,輸出顏色寄存器等。Shader HLSL中的顏色是rgba的類型,不要弄錯了。Shader中的每一個類型,函數,字符串都是有含義的。
頂點和像素着色器中,常量表用SetDefault傳入Device將常量表中的字段常量設置初值,避免忘記賦值了而沒有初值的情況。
D3DXCompileShaderFromFile用D3DXSHADER_SKIPVALIVATION當已確認可用時不進行任何代碼驗證可以提高效率不要老用D3DXSHADER_DEBUG那只是開發測試時候用的。
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符號是更適合的,因為它加強了向量所表示的顏色。
考慮向量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分量:
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之間。
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 model
abs Absolute value (per component). 11
acos Returns the arccosine of each component of x. 11
all Test if all components of x are nonzero. 11
AllMemoryBarrier Blocks execution of all threads in a group until all memory accesses have been completed. 5
AllMemoryBarrierWithGroupSync 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. 5
any Test if any component of x is nonzero. 11
asdouble Reinterprets a cast value into a double. 5
asfloat Convert the input type to a float. 4
asin Returns the arcsine of each component of x. 11
asint Convert the input type to an integer. 4
asuint Reinterprets the bit pattern of a 64-bit type to a uint. 5
asuint Convert the input type to an unsigned integer. 4
atan Returns the arctangent of x. 11
atan2 Returns the arctangent of of two values (x,y). 11
ceil Returns the smallest integer which is greater than or equal to x. 11
clamp Clamps x to the range [min, max]. 11
clip Discards the current pixel, if any component of x is less than zero. 11
cos Returns the cosine of x. 11
cosh Returns the hyperbolic cosine of x. 11
countbits Counts the number of bits (per component) in the input integer. 5
cross Returns the cross product of two 3D vectors. 11
D3DCOLORtoUBYTE4 Swizzles and scales components of the 4D vector xto compensate for the lack of UBYTE4 support in some hardware. 11
ddx Returns the partial derivative of x with respect to the screen-space x-coordinate. 21
ddx_coarse Computes a low precision partial derivative with respect to the screen-space x-coordinate. 5
ddx_fine Computes a high precision partial derivative with respect to the screen-space x-coordinate. 5
ddy Returns the partial derivative of x with respect to the screen-space y-coordinate. 21
ddy_coarse Computes a low precision partial derivative with respect to the screen-space y-coordinate. 5
ddy_fine Computes a high precision partial derivative with respect to the screen-space y-coordinate. 5
degrees Converts x from radians to degrees. 11
determinant Returns the determinant of the square matrix m. 11
DeviceMemoryBarrier Blocks execution of all threads in a group until all device memory accesses have been completed. 5
DeviceMemoryBarrierWithGroupSync 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. 5
distance Returns the distance between two points. 11
dot Returns the dot product of two vectors. 1
dst Calculates a distance vector. 5
EvaluateAttributeAtCentroid Evaluates at the pixel centroid. 5
EvaluateAttributeAtSample Evaluates at the indexed sample location. 5
EvaluateAttributeSnapped Evaluates at the pixel centroid with an offset. 5
exp Returns the base-e exponent. 11
exp2 Base 2 exponent (per component). 11
f16tof32 Converts the float16 stored in the low-half of the uint to a float. 5
f32tof16 Converts an input into a float16 type. 5
faceforward Returns -n * sign(dot(i, ng)). 11
firstbithigh Gets the location of the first set bit starting from the highest order bit and working downward, per component. 5
firstbitlow Returns the location of the first set bit starting from the lowest order bit and working upward, per component. 5
floor Returns the greatest integer which is less than or equal to x. 11
fmod Returns the floating point remainder of x/y. 11
frac Returns the fractional part of x. 11
frexp Returns the mantissa and exponent of x. 21
fwidth Returns abs(ddx(x)) + abs(ddy(x)) 21
GetRenderTargetSampleCount Returns the number of render-target samples. 4
GetRenderTargetSamplePosition Returns a sample position (x,y) for a given sample index. 4
GroupMemoryBarrier Blocks execution of all threads in a group until all group shared accesses have been completed. 5
GroupMemoryBarrierWithGroupSync 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. 5
InterlockedAdd Performs a guaranteed atomic add of value to the dest resource variable. 5
InterlockedAnd Performs a guaranteed atomic and. 5
InterlockedCompareExchange Atomically compares the input to the comparison value and exchanges the result. 5
InterlockedCompareStore Atomically compares the input to the comparison value. 5
InterlockedExchange Assigns value to dest and returns the original value. 5
InterlockedMax Performs a guaranteed atomic max. 5
InterlockedMin Performs a guaranteed atomic min. 5
InterlockedOr Performs a guaranteed atomic or. 5
InterlockedXor Performs a guaranteed atomic xor. 5
isfinite Returns true if x is finite, false otherwise. 11
isinf Returns true if x is +INF or -INF, false otherwise. 11
isnan Returns true if x is NAN or QNAN, false otherwise. 11
ldexp Returns x * 2exp 11
length Returns the length of the vector v. 11
lerp Returns x + s(y - x). 11
lit Returns a lighting vector (ambient, diffuse, specular, 1) 11
log Returns the base-e logarithm of x. 11
log10 Returns the base-10 logarithm of x. 11
log2 Returns the base-2 logarithm of x. 11
mad Performs an arithmetic multiply/add operation on three values. 5
max Selects the greater of x and y. 11
min Selects the lesser of x and y. 11
modf Splits the value x into fractional and integer parts. 11
mul Performs matrix multiplication using x and y. 1
noise Generates a random value using the Perlin-noise algorithm. 11
normalize Returns a normalized vector. 11
pow Returns xy. 11
Process2DQuadTessFactorsAvg Generates the corrected tessellation factors for a quad patch. 5
Process2DQuadTessFactorsMax Generates the corrected tessellation factors for a quad patch. 5
Process2DQuadTessFactorsMin Generates the corrected tessellation factors for a quad patch. 5
ProcessIsolineTessFactors Generates the rounded tessellation factors for an isoline. 5
ProcessQuadTessFactorsAvg Generates the corrected tessellation factors for a quad patch. 5
ProcessQuadTessFactorsMax Generates the corrected tessellation factors for a quad patch. 5
ProcessQuadTessFactorsMin Generates the corrected tessellation factors for a quad patch. 5
ProcessTriTessFactorsAvg Generates the corrected tessellation factors for a tri patch. 5
ProcessTriTessFactorsMax Generates the corrected tessellation factors for a tri patch. 5
ProcessTriTessFactorsMin Generates the corrected tessellation factors for a tri patch. 5
radians Converts x from degrees to radians. 1
rcp Calculates a fast, approximate, per-component reciprocal. 5
reflect Returns a reflection vector. 1
refract Returns the refraction vector. 11
reversebits Reverses the order of the bits, per component. 5
round Rounds x to the nearest integer 11
rsqrt Returns 1 / sqrt(x) 11
saturate Clamps x to the range [0, 1] 1
sign Computes the sign of x. 11
sin Returns the sine of x 11
sincos Returns the sine and cosine of x. 11
sinh Returns the hyperbolic sine of x 11
smoothstep Returns a smooth Hermite interpolation between 0 and 1. 11
sqrt Square root (per component) 11
step Returns (x >= a) ? 1 : 0 11
tan Returns the tangent of x 11
tanh Returns the hyperbolic tangent of x 11
tex1D(s, t) 1D texture lookup. 1
tex1D(s, t, ddx, ddy) 1D texture lookup. 21
tex1Dbias 1D texture lookup with bias. 21
tex1Dgrad 1D texture lookup with a gradient. 21
tex1Dlod 1D texture lookup with LOD. 31
tex1Dproj 1D texture lookup with projective divide. 21
tex2D(s, t) 2D texture lookup. 11
tex2D(s, t, ddx, ddy) 2D texture lookup. 21
tex2Dbias 2D texture lookup with bias. 21
tex2Dgrad 2D texture lookup with a gradient. 21
tex2Dlod 2D texture lookup with LOD. 3
tex2Dproj 2D texture lookup with projective divide. 21
tex3D(s, t) 3D texture lookup. 11
tex3D(s, t, ddx, ddy) 3D texture lookup. 21
tex3Dbias 3D texture lookup with bias. 21
tex3Dgrad 3D texture lookup with a gradient. 21
tex3Dlod 3D texture lookup with LOD. 31
tex3Dproj 3D texture lookup with projective divide. 21
texCUBE(s, t) Cube texture lookup. 11
texCUBE(s, t, ddx, ddy) Cube texture lookup. 21
texCUBEbias Cube texture lookup with bias. 21
texCUBEgrad Cube texture lookup with a gradient. 21
texCUBElod Cube texture lookup with LOD. 31
texCUBEproj Cube texture lookup with projective divide. 21
transpose Returns the transpose of the matrix m. 1
trunc Truncates floating-point value(s) to integer value(s)
————————————————
版權聲明:本文為CSDN博主「博贏天下」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/plaxbsga/article/details/52787860