1. Dire c tx 고수준 셰이딩 언어 HLSL에 대한 소개 Craig Peeper & Jason L. Mitchell
HLSL(High Level Shading Language) DirectX 9.,. HLSL,,,. ShaderX 2 : DirectX 9 & HLSL,. HLSL. HLSL, procedural wood 1) HLSL HLSL. HLSL. f l oat 4x4 vi ew_proj_mat r i x; f l oat 4x4 t ext ure_mat r i x0; st r uct VS_PUTOUT f l oat 4 Pos f l oat 3 Pshade ; : POSITION; : TEXCOORD0; VS_OUTPUT mai n (f l oat 4 vposi t i on : POSITION) VS_OUTPUT Out = (VS_OUTPUT) 0; 1) : (procedure)
1. DirectX HLSL 27 / / Out. Pos = mul (vi ew_proj_mat r i x, vposi t i on) ; / / Pshade Out. Pshade = mul (t ext ure_mat ri x0, vposi t i on) ; ret ur n Out ; vi ew_proj_mat r i x t ext ure_mat r i x0 4 4. f l oat 4 Pos f l oat 3 Pshade VS_OUTPUT. mai n f l oat 4, VS_OUTPUT. f l oat 4 vposi t i on, VS_OUTPUT. POSITION TEXCOORD0. (semantic),. mai n vposi t i on vi ew_proj_mat r i x mul.. mul vposi t i on, mul., vposi t i on mul vpos i t i on (mul ). vposi t i on (clip space), 3D vposi t i on t ext ur e_mat r i x0. ( / ).., 3D Pshade (interpolator). HLSL procedural wood., ps_2_0.
f l oat 4 l i ght Wood; / / f l oat 4 dar kwood; / / f l oat r i ngfreq; / / sampl er Pul setrai nsampl er ; f l oat 4 hl sl _ri ngs (f l oat 4 Pshade : TEXCOORD0) : COLOR f l oat scal eddi st FromZAxi s = sqrt (dot (Pshade.xy, Pshade.xy) ) * ri ngfreq; f l oat bl endfact or = t ex1d (Pul setrai nsampl er, scal eddi st FromZAxi s ) ; ret ur n l er p (dar kwood, l i ght Wood, bl endfact or ) ; 4 2. Pul setrai nsampl er... Pshade,. z-, / Pul se Trai nsampl e r 1. Tex1D( ) (l i ght wood dar kwood). 4D. 4D RGBA.. HLSL. HLSL Direct3D, D3DX,
[ Dire c tx 9 D3D 1. DirectX HLSL 29. DirectX 8.0 Direct3D., 3D.. DirectX 8.0 DirectX 8.1 (vs_1_1 ps_1_1 ~ ps_1_4 ). [ 1-1] D3DXAs sembl eshader () DX3D, Creat epi xel Shade r () Creat ever t exshader () Direct3D. Direct3D ShaderX & ( ). 1-1] Dire c tx8 [ 1-1], DirectX9 HLSL D3DXCompi l eshader ( ) API D3DX, Creat epi xel Shader () Creat e Vert exshader () direct3d..,,. Direct3D HLSL. HLSL D3D
, HLSL DX SDK. DirectX 9 D3DX HLSL 3D. (vs_2_0, vs_3_0, ps_2_0 ps_3_0), HLSL. HLSL. D3DXCompi l eshade r () API HLSL D3DX. API. HLSL, Direct3D. HLSL.. HLSL. HLSL., HLSL.
1. DirectX HLSL 31., HLSL 6. ps_1_1 ps_1_1 4.. HLSL. HLSL.,, if-else.,, if-else, if-else.,. - fxc HLSL D3DX.,. fxc DirectX 9 SDK.,...
-T target ( : vs_2_0) -E name name( : main) -Od -Vd -Zi -Zpr -Zpc -Fo file -Fc file -Fh file -D id = text -nologo HLSL... HLSL. HLSL. HLSL. (*),. asm* bool compi l e const decl * do doubl e el se ext er n f al se f l oat f or hal f i f i n i nl i ne i nout i nt mat r i x* out
1. DirectX HLSL 33 pass* pi xel shader * ret urn sampl er shared st at i c st r i ng* st r uct t echni que* t ext ure* t r ue t ypedef uni f orm vect or * vert exshader * voi d vol at i l e whi l e. aut o break case cat ch char cl ass compi l e const const _cast cont i nue Def aul t del et e dynami c_cast enum expl i ci t f r i end got o l ong mut abl e namespace new operat or pr i vat e prot ect ed publ i c regi st er rei nt erpret _cast short Si gned si zeof st at i c_cast swi t ch t empl at e t hi s t hrow t ry t ypename uni on unsi gned usi ng vi rt ual HLSL.. bool int half float double 32 16 32 64
.,. half double. float. HLSL.. vector vector<type, size> 4, size, type, 2 4. 4 f l oat. f l oat 4 f Vect or0; f l oat f Vect or 1[4] ; vect or f Vect or2 ; vect or <f l oat, 4> f Vect or3; 3 bool. bool 3 bvect or0; bool bvect or1[3] ; vect or <bool, 3> bvect or2; (swizzle).
1. DirectX HLSL 35 x, y, z, w r, g, b, a ( )., f l oat 4 pos = 3.0f, 5.0f, 2.0f, 1.0f ; f l oat val ue0 = pos [0] ; / / val ue0 3.0f f l oat val ue1 = pos.x; / / val ue1 3.0f f l oat val ue2 = pos.g; / / val ue2 5.0f f l oat 2 vec0 = pos.xy; / / vec0 3.0f, 5.0f f l oat 2 vec1 = pos. ry; / /! Ps_2_0.. HLSL 2D. bool, i nt, hal f, f l oat doubl e., 4 4. 2 4 4 f l oat. f l oat 4x4 vi ew_proj_mat r i x; f l oat 4x4 t ext ure_mat r i x0;,., 3 4. f l oat 3x4 mat 0; mat r i x<f l oat, 3, 4> mat 1;
, /. vi ew_proj_mat r i x. f l oat f Val ue = vi ew_proj_mat r i x[0] [0] ;. 0. _m00, _m01, _m02, _m03 _m10, _m11, _m12, _m13 _m20, _m21, _m22, _m23 _m30, _m31, _m32, _m33 1. _11, _12, _13, _14 _21, _22, _23, _24 _31, _32, _33, _34 _41, _42, _43, _44. f l oat 2x2 f Mat = 3.0f, 5.0f, / / 1 2.0f, 1.0f ; / / 2 f l oat val ue0 = f Mat [0] ; / / val ue0 3.0f f l oat val ue1 = f Mat._m00; / / val ue1 3.0f f l oat val ue2 = f Mat._12 / / val ue2 5.0f f l oat val ue3 = f Mat [1] [1] / / val ue3 1.0f f l oat 2 vec0 = f Mat._21_22 ; / / vec0 2.0f, 1.0f f l oat 2 vec1 = f Mat [1] ; / / vec1 2.0f, 1.0f (typ e mo d ifie r) HLSL. const..
1. DirectX HLSL 37 row_maj or col _maj or. row_maj or, col_maj or. (col_major). (sto ra g e c la ss mo d ifie r).,. C, st at i c ext ern ( ). st at i c, API. st at i c API. C, st at i c. ext ern API, ext ern. shared ef f ect. uni f orm HLSL (Set *Shader Const ant *() API ). uni f orm. const.,. ext er n f l oat t ransl ucencycoef f ; const f l oat gl oss_bi as ; st at i c f l oat gl oss_scal e ; f l oat di f f use ; di f f use t ransl ucencycoef f Set ShaderConst ant () API,. const gl oss_bi as Set *Shader Const ant *() API,.
, st at i c gloss_scale Set *ShaderConst ant *() API,. C. f l oat 2x2 f Mat = 3.0f, 5.0f, / / ( 2.0f, 1.0f ; / / 2. ) f l oat 4 vpos = 3.0f, 5.0f, 2.0f, 1.0f ; f l oat f Fact or = 0.2f ; HLSL. 3D.,. f l oat 4 vtone = vbr i ght ness * vexposure ; vbri ght ness vexposure f l oat 4. f l oat 4 vtone ; vtone.x = vbri ght ness.x * vexposure.x; vtone.y = vbri ght ness.y * vexposure.y; vtone. z = vbri ght ness. z * vexposure. z; vtone.w = vbri ght ness.w * vexposure.w; 4D vbri ght ness vexposure.. mul ().
1. DirectX HLSL 39 (c o nstruc to r) HLSL C++,.. f l oat 3 vpos = f l oat 3(4.0f, 1.0f, 2.0f ) ; f l oat f Di f f use = dot (vnormal, f l oat 3(1.0f, 0.0f, 0.0f ) ) ; f l oat 4 vpack = f l oat 4(vPos, f Di f f use ) ; (dot (vnormal, f l oat 3 (1.0f, 0.0f, 0.0f ) dot f l oat 3 ), (vect or3 vect or4 f l oat 4(vPos, f Di f f use) ). f l oat 4 f l oat 3 f l oat f l oat 4. (typ e c a sting ) HLSL.., vresul t f l oat 0.0f f l oat 4(0.0f, 0.0f, 0.0f, 0.0f ). f l oat 4 vresul t = 0.0f ;..,. f l oat 3 vli ght ; f l oat f Fi nal, f Col or ; f Fi nal = vli ght * f Col or ; vli ght x fcol or. f Fi nal = vli ght.x * fcol or.
HLSL.. bool false 0, true 1. bool 0 false, 0 true.. C...........................
1. DirectX HLSL 41 (struc ture ), HLSL., mai n ( f l oat 4 ). NPR Metallic. st r uct VS_OUTPUT f l oat 4 Pos : POSITION; f l oat 3 Vi ew : TEXCOORD0; f l oat 3 Normal : TEXCOORD1; f l oat 3 Li ght 1: TEXCOORD2 ; f l oat 3 Li ght 2 : TEXCOORD3; f l oat 3 Li ght 3 : TEXCOORD4 ; ; HLSL. (sa m p le r) sampler. hl sl_ri ngs (). f l oat 4 l i ght Wood; / / f l oat 4 dar kwood; / / f l oat r i ngfreq; / / sampl er Pul setrai nsampl er ; f l oat 4 hl sl _r i ngs (f l oat 4 Pshade : TEXCOORD0) : COLOR f l oat scal eddi st FromZAxi s = sqrt (dot (Pshade.xy, Pshade.xy) ) * r i ngfreq; f l oat bl endfact or = t ex1d (Pul setrai nsampl er, scal eddi st FromZAxi s ) ; ret ur n l er p (dar kwood, l i ght Wood, bl endfact or ) ;
Pul setrai nsampl er t ex1d(). HLSL D3D API, 3D uv.,. ShaderX 2 : DirectX 9 &,., 2 Sobel (dx, dy ) (Height map) (Normal map). sampl er Input Image ; f l oat 4 mai n (f l oat 2 t oplef t : TEXCOORD0, f l oat 2 l ef t : TEXCOORD1, f l oat 2 bot t omlef t : TEXCOORD2, f l oat 2 t op : TEXCOORD3, f l oat 2 bot t om : TEXCOORD4, f l oat 2 t opri ght : TEXCOORD5, f l oat 2 r i ght : TEXCOORD6, f l oat 2 bot t omri ght : TEXCOORD7) : COLOR / / 8. f l oat 4 t l = t ex2d (I nput I mage, t oplef t ) ; f l oat 4 l = t ex2d (I nput I mage, l ef t ) ; f l oat 4 bl = t ex2d (I nput I mage, bot t omlef t ) ; f l oat 4 t = t ex2d (I nput I mage, t op) ; f l oat 4 b = t ex2d (I nput I mage, bot t om) ; f l oat 4 t r = t ex2d (I nput I mage, t opri ght ) ; f l oat 4 r = t ex2d (I nput I mage, r i ght ) ; f l oat 4 br = t ex2d (I nput I mage, bot t omri ght ) ; / / Sobel dx. / / / / -1 0 1 / / -2 0 2 / / -1 0 1
1. DirectX HLSL 43 f l oat dx = -t l.a - 2.0f *l.a - bl.a + t r.a + 2.0f *r.a + br.a ; / / dy Sobel. / / / / -1-2 -1 / / 0 0 0 / / 1 2 1 f l oat dy = -t l.a - 2.0f *t.a - t r.a + bl.a + 2.0f *b.a + br.a ; / /. f l oat 4 N = f l oat 4 (normal i ze (f l oat 3( -dx, -dy, 1) ), t l.a ) ; / / ( -1...1) (0... 1). ret ur n N * 0.5f + 0.5f ; 1, Input Image t ex2d() 8. DirectX HLSL. ( ), t ex1d() t ex2d(). HLSL. abs () dot (), ref ract (), st ep()., ddx(), ddy(), fwi dt h().
abs(x) acos(x) all(x) any(x) asin(x) atan(x) atan2(y, x) ceil(x) clamp(x, min, max) Clip(x) cos(x) cosh(x) cross(a, b) D3DCOLORtoUBYTE4(x) ddx(x) ddy(x) degrees(x) determinant(m) distance(a, b) dot(a, b) exp(x) exp2(a) faceforward(n, i, ng) floor(x) (x ). x. [ 1, 1]. x 0. x 0. x. [ p/2, p/2]. x. [ p/2, p/2]. y/x. y x [-p, p]. atan2, x 0 y 0. x. [min, max]. x 0. x. texkill. x. x (hyperbolic cosine). a, b. 4D x UBYTE4. x. y. (0 360 ). m. a, b. a, b. e x. 2 x ( ). n * sign(dot(i, ng)). x.
1. DirectX HLSL 45 fmod(a, b) frac(x) frexp(x, out exp) fwidth(x) isfinite(x) isinf(x) isnan(x) a/b ( ). a = i * b + f I, f x, f b. X x. frexp exp. x 0 0. abs(ddx(x))+abs(ddy(x)). x true, false. x +INF INF true, false. x NAN QNAN true, false. CNAN:Net a numbor- ldexp(x, exp) * 2 ex p. len(v) length(v) lerp(a, b, s) log(x) log10(x) log2(x) max(a, b) min(a, b) modf(x, out ip) mul(a, b) normalize(v) pow(x, y) radians(x) reflect(i, n) v. s 0 a, 1 b a b a + s(b a). e x. x indefinite. x 0 +INF. 10 X. x indefinite, 0 +INF. 2 x. x indefinite, 0 +INF. a b. a b. x x., out ip. a b. a. b. a b. a b. v/length(v) v. v 0.. xy. x (radian). v. I, n v = i 2 * dot(i, n) * n.
refract(i, n, eta) round(x) rsqrt(x) saturate(x) v ( I, n, eta ). eta n I refract (0, 0, 0). x. 1 / sqrt(x). x [0, 1]. sign(x) sin(x) x. x < 0-1, x=0 0, 1. x. x > 0 sincos(x, out s, out c) sinh(x) smoothstep(min, max, x) sqrt(x) step(a, x) tan(x) tanh(x) transpose(m) x. sin(x) s, cos(x) c. x. x < min 0, x > max 1. [min, max] 0 1 smooth Hermite. ( ). (x = a)? 1 : 0. x. x. m. mro w s mc o lu m n s. mc o lu m n s mrow s 16. 4 (1D, 2D, 3D, ) 4 (,,, ) 16. tex1d(s, t) 1. s. t. tex1d(s, t, ddx, ddy) (LOD ) 1D. t, ddx, and ddy.. s tex1dproj(s, t) 1. s. t 4D. t t.w.
1. DirectX HLSL 47 tex1dbias(s, t) tex2d(s, t) tex2d(s, t, ddx, ddy) tex2dproj(s, t) tex2dbias(s, t) tex3d(s, t) tex3d(s, t, ddx, ddy) tex3dproj(s, t) tex3dbias(s, t) texcube(s, t) texcube(s, t, ddx, ddy) texcubeproj(s, t) texcubebias(s, t) (bias) 1. s. t 4D. (mip) t.w. 2D. s. t 2. (LOD ). s. t, ddx ddy 2. 2. s. t 4. t (t.w 1 ) t.w. 2. s. t 4. (mip) t.w. 3. s. t 3. (derivatives) 3. s. t, ddx, ddy 3. 3. s. t 4. t t.w. 3. s. t 4. (mip) t.w.. s. t 3.. s. t, ddx, ddy 3.. s, t 4D. t t.w.. s. t 4. (mip) t.w. t ex1d(), t ex2d(), t ex3d() t excube(). ddx ddy ddx(), ddy() LOD(level of detail)., ps_2_0. Tex*proj () (projective)..
t ex2dproj (), (Perspective Shadow map). Tex*bi as,. (over-blurred)., ShaderX 2 : DirectX 9 & Radeon 9700 Animusic Pipedream, t excubebi as ().... / /. f l oat 3 vcubelookup = vref l ect i on + i. Pos/ f EnvMapRadi us ; f l oat 4 cref l ect i on = t excubebi as (t CubeEnv, f l oat 4 (vcubelookup, f Bl ur * f Text urebl ur ) ) * vref l ect i oncol or ;... t excubebi as () t f l oat 4(vCubeLookup.x, vcube Lookup.y, vcubelookup.z, f Bl ur*ftext urebl ur ), f Bl ur*ftext urebl ur t.w t excubebi as (t excube() t.w ). DirectX 9 HLSL. varying uniform. varying. varying (, ). uniform (, ). uniform, varying v t.
1. DirectX HLSL 49 unifo rm uniform HLSL 2.. uniform. uniform. uniform.. / / uni f orm. / / Uni f ormgl obal. f l oat 4 Uni f ormgl obal ; / / uni f orm. / / ' $Uni f ormparam. f l oat 4 mai n( uni f orm f l oat 4 Uni f ormparam ) : POSITION ret ur n Uni f ormgl obal * Uni f ormparam; uniform. uniform. N o t e uniform $. $ (local variable) (global variable). uniform. ( )... API D3DX Effect.
fxc.exe. / / / / Generat ed by Mi crosof t (R) D3DX9 Shader Compi l er / / / / Source : hemi sphere.fx / / Fl ags : / E:VS /T:vs_1_1 / / / / Regi st er s : / / / / Name Reg Si ze / / - - - - - - - - - - - - - - - - - - - - - / / Proj ect i on c0 4 / / Wor l dvi ew c4 3 / / Di r FromLi ght c7 1 / / Di r FromSky c8 1 / / $bhemi c18 1 / / $bdi f f c19 1 / / $bspec c20 1 / / / / / / Def aul t val ues : / / / / Di r FromLi ght / / c7 = 0.577, -0.577, 0.577, 0 ; / / / / Di r FromSky / / c8 = 0, -1, 0, 0 ; va rying varing. uniform. uniform.
1. DirectX HLSL 51., POSITION0.. (,,,, ). D3DDECLUSAGE UsageIndex 1 1. (rasterization).... (:)..,.. / /. st r uct I nst ruct f l oat 4 Pos1 : POSITION1 ; / / Pos. f l oat 4 mai n( f l oat 4 Pos : POSITION0, I nst ruct I n ) : POSITION ret ur n Pos * I n. Pos1;
/ / COLOR0 Col. f l oat 4 mai nps( f l oat 4 Col : COLOR0 ) : COLOR ret ur n Col ;. POSITIONn BLENDWEIGHTn BLENDINDICESn NORMALn PSIZEn COLORn TEXCOORDn TANGENTn BINORMALn TESSFACTORn (.). COLORn TEXCOORDn n PSIZE0, DIFFUSE1...,
1. DirectX HLSL 53... POSITION. TEXCOORDn COLORn... DEPTH. N o t e DEPTH (MRT ).... POSITION PSIZE FOG COLORn TEXCOORDn ( : COLOR0) ( : TEXCOORD0). COLORn DEPTH n
n ( : TEXCOORD3, COLOR0). HLSL. / /. st r uct Out St ruct f l oat 2 Tex2 : TEXCOORD2 ; / / TEXCOORD0 Tex0 f l oat 4 mai n(out f l oat 2 Tex0 : TEXCOORD0, out Out St r uct Out ) : POSITION Tex0 = f l oat 2(1.0, 0.0) ; Out.Tex2 = f l oat 2 (0. 1, 0.2) ; ret ur n f l oat 4(0.5, 0.5, 0.5, 1) ; / / COLOR0 Col f l oat 4 mai nps( out f l oat 4 Col 1 : COLOR1) : COLOR / / 1. Col 1 = f l oat 4(0.0, 0.0, 0.0, 0.0) ; / / ret ur n 0. ret ur n f l oat 4(1.0, 0.9722, 0.3333334, 0) ; st r uct PS_OUT f l oat 4 Col or : COLOR; f l oat Dept h: DEPTH; ; / / / / / / PS_OUT PSFunc1( )... voi d PSFunc2 (out f l oat 4 Col or : COLOR, out f l oat Dept h : DEPTH)
[ 1. DirectX HLSL 55... voi d PSFunc3(out PS_OUT Out ).... NPR Metallic.. [ 1-2]. ATI Developer Relations (http://www.ati.com/developer). 1-2 ] NP RMe ta llic, HLSL NPR Metallic.
f l oat 4x4 vi ew_proj_mat r i x; f l oat 4 vi ew_posi t i on; f l oat 4 l i ght 0; f l oat 4 l i ght 1; f l oat 4 l i ght 2; st r uct VS_OUTPUT f l oat 4 Pos : POSITION; f l oat 3 Vi ew : TEXCOORD0; f l oat 3 Normal : TEXCOORD1; f l oat 3 Li ght 1 : TEXCOORD2 ; f l oat 3 Li ght 2 : TEXCOORD3; f l oat 3 Li ght 3 : TEXCOORD4 ; ; VS_OUTPUT mai n(f l oat 4 i npos : POSITION, f l oat 3 i nnorm : NORMAL) VS_OUTPUT Out = (VS_OUTPUT) 0; / /. Out. Pos = mul (vi ew_proj_mat r i x, i npos ) ; Out.Normal = i nnorm; / /. Out.Vi ew = normal i ze (vi ew_posi t i on - i npos ) ; / /. Out. Li ght 1 = normal i ze (l i ght 0 - i npos ) ; / / 1 Out. Li ght 2 = normal i ze (l i ght 1 - i npos ) ; / / 2 Out. Li ght 3 = normal i ze (l i ght 2 - i npos ) ; / / 3 ret ur n Out ; vi ew_proj_mat ri x, vi ew_posi t i on, l i ght 0, l i ght 1, l i ght 2. API,.
1. DirectX HLSL 57 mai n VS_OUTPUT, 4D 5 3D., 4D, 3D 2D. i npos mul () vi ew_proj_mat r i x, i nnorm., 3D. 3D normal i ze (). 3D 3D,.,. NPRMetallic.vhl,. f xc -nol ogo -T vs_1_1 -Fc -Vd NPRMet al l i c.vhl vs_1_1.,.. / / Paramet ers : / / f l oat 4 l i ght 0; / / f l oat 4 l i ght 1; / / f l oat 4 l i ght 2 ; / / f l oat 4 vi ew_posi t i on; / / f l oat 4x4 vi ew_proj_mat ri x; / / / / Regi st er s : / / Name Reg Si ze / / - - - - - - - - - - - - - - - - - - - - - - - - - / / vi ew_proj_mat r i xc0 4 / / vi ew_posi t i on c4 1
/ / l i ght 1 c5 1 / / l i ght 2 c6 1 / / l i ght 0 c7 1 vs_1_1 dcl _posi t i on v0 dcl _normal v1 mul r0, v0. x, c0 mad r2, v0.y, c1, r0 mad r4, v0. z, c2, r2 mad opos, v0.w, c3, r4 add r 1, -v0, c4 dp4 r 1.w, r 1, r 1 rsq r1.w, r1.w mul ot0. xyz, r 1, r 1.w add r8, -v0, c7 dp4 r8.w, r8, r8 rsq r8.w, r8.w mul ot2. xyz, r8, r8.w add r3, -v0, c5 add r 10, -v0, c6 dp4 r3.w, r3, r3 rsq r3.w, r3.w mul ot3. xyz, r3, r3.w dp4 r 10.w, r 10, r 10 rsq r 10.w, r 10.w mul ot4.xyz, r 10, r10.w mov ot1. xyz, v1. API. 21., mai n POSITION NORMAL dcl_posi t i on dcl_normal. opos, ot0, ot1, ot2, ot3 ot4.., HLSL fxc.
1. DirectX HLSL 59.. NPR Metallic. f l oat 4 Mat eri al ; sampl er Out l i ne ; f l oat 4 mai n(f l oat 3 Vi ew: TEXCOORD0, f l oat 3 Normal : TEXCOORD1, f l oat 3 Li ght 1: TEXCOORD2, f l oat 3 Li ght 2 : TEXCOORD3, f l oat 3 Li ght 3 : TEXCOORD4 ) : COLOR / / f l oat 3 norm = normal i ze (Normal ) ; f l oat 4 out l i ne = t ex1d(out l i ne, 1 - dot (norm, normal i ze (Vi ew) ) ) ; f l oat l i ght i ng = (dot (normal i ze (Li ght 1), norm) * 0.5 + 0.5) + (dot (normal i ze (Li ght 2), norm) * 0.5 + 0.5) + (dot (normal i ze (Li ght 3), norm) * 0.5 + 0.5) ; ret ur n out l i ne * Mat er i al * l i ght i ng;. 4D Mat er i al, Out l i ne. 3D mai n,,, 3,. normal i ze ()...
out l i ne * Mat eri al * l i ght i ng. 4D,.,.. ret ur n out l i ne * Mat er i al * l i ght i ng; ret ur n out l i ne * Mat er i al * f l oat 4 (l i ght i ng, l i ght i ng, l i ght i ng, l i ght i ng) ; l i ght i ng, [ 1-2]. NPR Metallic. f xc -nol ogo -T ps_2_0 -Fc -Vd NPRMet al l i c. phl ps_2_0.. / / / / f l oat 4 Mat eri al ; / / sampl er Out l i ne ; / / / / / / Name Reg Si ze / / - - - - - - - - - - - - - - - - - - - - / / Mat er i al c0 1 / / Out l i ne s0 1 ps_2_0 def c1, 1, 0, 0, 0.5 dcl t 0.xyz dcl t 1.xyz dcl t 2.xyz dcl t 3.xyz dcl t 4.xyz dcl_2d s0
1. DirectX HLSL 61 dp3 r0.w, t 1, t 1 r sq r2.w, r0.w mul r9.xyz, r2.w, t 1 dp3 r9.w, t 0, t 0 r sq r9.w, r9.w mul r4.xyz, r9.w, t 0 dp3 r9.w, r9, r4 add r 11.xy, - r9.w, c1.x t exl d r6, r 11, s0 dp3 r9.w, t 2, t 2 r sq r9.w, r9.w mul r1. xyz, r9.w, t 2 dp3 r9.w, r 1, r9 mad r9.w, r9.w, c1.w, c1.w dp3 r8.w, t 3, t 3 r sq r 10.w, r8.w mul r5.xyz, r 10.w, t 3 dp3 r0.w, r5, r9 mad r9.w, r0.w, c1.w, r9.w add r9.w, r9.w, c1.w dp3 r2.w, t 4, t 4 r sq r 11.w, r2.w mul r1. xyz, r11.w, t 4 dp3 r8.w, r 1, r9 mad r 10.w, r8.w, c1.w, r9.w add r5.w, r 10.w, c1.w mul r6, r6, r5.w mul r0, r6, c0 mov oc0, r0 ( Mat eri al, Out l i ne). API ps_2_0 def. def ALU. HLSL NPR Metallic.
... 1 - dot (norm, normal i ze (Vi ew)... dot (normal i ze (Li ght 1), norm) * 0.5 + 0.5... dcl t n.xyz 3D. HLSL mai n. dc1_2d s0. 0 2D. HLSL t ex1d. Direct3D API 1D. t ex1d() HLSL. HLSL. HLSL. DirectX HLSL, HLSL., HLSL 1.x.,.,.,.. HLSL
1. DirectX HLSL 63. nx1, t ex2d() HLSL.,.,... uniform ( )...,.. HLSL C.,.. ( a b a b. a float4(b,b,b,b)), HLSL.,..
.,. HLSL i nt. i nt. i nt. f l oat. 2.5 float4 4 2 2 2 3. f l oat s. C. i nt. i nt. i nt. i nt,. i nt. f l oat i nt float. i nt i nt. int f l oat. Out Pos = mul (Pos, Wor l dar ray[i ndex] ) ; / / f l oat / / i nt f rc r0.w, r 1.w mul r0.w, c60.x, r 1.w f rc r0.w, r 1.w mul r0.w, c60.x, r 1.w
1. DirectX HLSL 65 add r2.w, - r0.w, r1.w mova a0.x, r0.w mul r9.w, r2.w, c61. x m4x4 opos, v0, c0[a0. x] mova a0.x, r9.w m4x4 opos, v0, c0[a0.x].. ( (static branching), (predicated instructions), (static looping), (dynamic branching), (dynamic looping)). HLSL,., HLSL,..,..,. Ps_1_1 DirectX 9 SDK DepthOfField.,. If. if,. CPU, HLSL. CPU,. predicated~instructions, static~if~blocks dynamic~if~blocks.
vs_1_1, i f (Val ue > 0) Posi t i on = Val ue1; el se Posi t i on = Val ue2 ;, / / Val ue>0 l er p. mov r 1.w, c2.x sl t r0.w, c3.x, r 1.w / / Val ue1 Val ue2 l er p. mov r7, -c1 add r2, r7, c0 mad opos, r0.w, r2, c1 /.. draw /,., draw... CPU.. CPU.,. CPU.
1. DirectX HLSL 67., Direct3D. 4 f l oat (, x, y, z.),. f l oat 4., w w, 1.0. y z, 0.0.. w 1.0, x, y, z. f l oat 3 w 1.0. f l oat 4 1.0...,, i nt f l oat. i nt i nt.. (lo g p, e xp p, lit).,., ps_1_x
.. vs_1_1 vs_2_0. l ogp, expp l i t l og, exp pow.. l og exp 10, l ogp, expp. vs_1_1. hal f... l og l ogp. f l oat LogVal ue = l og(val ue ) ; f l oat LogVal ue = (hal f )l og(val ue ) / / vs_1_1 / / vs_1_1 / / 10. / / 1. l og r0, c0; l ogp r0, c0 ps_ 1_x (ps_1_1, ps_1_2, ps1_3, ps_1_4). HLSL ps_1_x,., ps_1_x. Ps_1_x..
1. DirectX HLSL 69. Ps_1_1 ps_1_3 (. r,.g,.b,.a). Ps_1_4.. ps_1_x HLSL. Ps_1_x source dest 0 1 source.... HLSL. sat urat e () _sat. _ bx2 _bx2 HLSL HLSL. mai n _bx2. f l oat 4 mai n( f l oat 3 Col : COLOR0, f l oat 3 Tex : TEXCOORD0 ) : COLOR0 ret ur n dot (Col, Tex*2-1) ; f l oat 4 mai n( f l oat 3 Col : COLOR0, f l oat 3 Tex : TEXCOORD0 ) : COLOR0 f l oat 3 val = Tex*2 ; val = val -1; ret ur n dot (Col, val ) ; f l oat 4 mai n( f l oat 3 Col : COLOR0, f l oat 3 Tex : TEXCOORD0 ) : COLOR0 ret ur n dot (Col, (Tex -.5f )*2) ;
ps_1_x. ps_1_1 t excoord t 0 dp3 r0, v0, t 0_bx2 Tex*2-1 ps_2_0. _ bias _bi as. f l oat 4 mai n( f l oat 3 Col : COLOR0, f l oat 3 Tex : TEXCOORD0 ) : COLOR0 ret ur n dot (Col, (Tex -.5f ) ) ; mai n. ps_1_1 t excoord t 0 dp3 r0, v0, t 0_bi as _bi as 0 1, ps_1_1, ps_1_2, ps_1_3.. _ x2 (ps_ 1_ 4 only) _x2. f l oat 4 mai n( f l oat 3 Col : COLOR0, f l oat 3 Tex : TEXCOORD0 ) : COLOR0 ret ur n dot (Col, Tex*2) ; HLSL.
1. DirectX HLSL 71 ps_1_4 t excrd r0.xyz, t 0 dp3 r0, v0, r0_x2 _ x2, _ x4, _ x8, _ d2, _ d4 _ d8 destination write destination write ps_1_x, HLSL. (_x2) (_x4) (_d2) ps_1_1 ps_1_3 ps_1_4 6 (_x2, _x4, _x8, _d2, _d4, _d8 ). N 2, 4, 8, 0.5, 0.25, 0.125. st at i c const f l oat N = 2; f l oat 4 mai n( f l oat 4 Col [2] : COLOR0 ) : COLOR0 ret ur n (Col [0] + Col [1] )*N; HLSL. ps_1_1 add_x2 r0, v0, v1 complement ( ) ps_1_x HLSL complement. 0 1. HLSL. f l oat 4 mai n( f l oat 4 Col [2] : COLOR0 ) : COLOR0 ret ur n (1-Col [0] ) * (Col [1] ) ;
HLSL. ps_1_1 mul r0, 1-v0, v1 saturate ( ) _sat.. f l oat 4 mai n( f l oat 4 Col [2] : COLOR0 ) : COLOR0 ret ur n sat urat e (Col [0] ) ; f l oat 4 mai n( f l oat 4 Col [2] : COLOR0 ) : COLOR0 ret ur n cl amp(col [0], 0, 1) ; HLSL. ps_1_1 mov_sat r0, v0 negate ( ) negate. N o t e PS_1_x negate, negate. f l oat 4 mai n( f l oat 4 Col [2] : COLOR0 ) : COLOR0 ret ur n -Col [0] ;
1. DirectX HLSL 73 HLSL. ps_1_1 mov r0, -v0 ps_ 1_x ps_1_x ps_2_0. ps_2_0, ps_1_x. Fxc.exe, ps_1_x. ps_1_x. HLSL, HLSL. HLSL D3DX Effect, Effect. HLSL,. D3DX Effe c t D3DX D3DX Effect DirectX 9 D3DX Effect HLSL. D3DX Effect 3. Effect (Rendering state) HLSL asm. Effect.fx.fxl, techniques Effect. Effect
. techniques DirectX SDK Water... Effe c t Effect, HLSL Effect. Effect. / / VECTOR g_leye ; f l oat 4 Gl obalambi ent = 0.5; f l oat Ka = 1; f l oat Kd = 0.8; f l oat Ks = 0.9; f l oat roughness = 0. 1; f l oat noi sefrequency; MATRIX mat Worl dvi ewproj ; MATRIX mat Worl dvi ew; MATRIX mat ITWor l dvi ew; MATRIX mat Worl d; MATRIX mat Tex0; TEXTURE t Vol umenoi se ; TEXTURE t Mar bl espl i ne ; sampl er Noi sesampl er = sampl er_st at e Text ure = (t Vol umenoi se ) ; Mi nfi l t er = Li near ; MagFi l t er = Li near ; Mi pfi l t er = Li near ; AddressU = Wrap; AddressV = Wrap; AddressW = Wrap; MaxAni sot ropy = 16; ;
1. DirectX HLSL 75 sampl er Mar bl espl i nesampl er = sampl er_st at e Text ure = (t Marbl espl i ne ) ; Mi nfi l t er = Li near ; MagFi l t er = Li near ; Mi pfi l t er = Li near ; AddressU = Cl amp; AddressV = Cl amp; MaxAni sot ropy = 16; ; f l oat 3 snoi se (f l oat 3 x) ret ur n 2.0f * t ex3d (Noi sesampl er, x) - 1.0f ; f l oat 4 ambi ent (voi d) ret ur n Gl obal Ambi ent ; f l oat 4 sof t _di f f use (f l oat 3 Neye, f l oat 3 Peye ) / / (Leye ). f l oat 3 Leye = (g_leye - Peye ) / l engt h(g_leye - Peye ) ; f l oat Ndot L = dot (Neye, Leye ) * 0.5f + 0.5f ; / / N. L ret ur n f l oat 4 (Ndot L, Ndot L, Ndot L, Ndot L) ; f l oat 4 specul ar (f l oat 3 NNeye, f l oat 3 Peye, f l oat k) / / (Leye ). f l oat 3 Leye = (g_leye - Peye ) / l engt h(g_leye - Peye ) ; / / Veye. f l oat 3 Veye = - (Peye / l engt h(peye ) ) ;
/ / hal f -angl e. f l oat 3 Heye = (Leye + Veye ) / l engt h(leye + Veye ) ; / / N.H. f l oat Ndot H = cl amp(dot (NNeye, Heye ), 0.0f, 1.0f ) ; f l oat Ndot H_2 = Ndot H * Ndot H; f l oat Ndot H_4 = Ndot H_2 * Ndot H_2 ; f l oat Ndot H_8 = Ndot H_4 * Ndot H_4 ; f l oat Ndot H_16 = Ndot H_8 * Ndot H_8; f l oat Ndot H_32 = Ndot H_16 * Ndot H_16; ret ur n Ndot H_32 * Ndot H_32 ; f l oat 4 hl sl _bl uemarbl e (f l oat 3 P : TEXCOORD0, f l oat 3 Peye : TEXCOORD1, f l oat 3 Neye : TEXCOORD2) : COLOR f l oat 4 Ct ; f l oat 4 Ci ; f l oat 3 NNeye ; f l oat mar bl e ; f l oat f ; / /. P = P/ 16; mar bl e = -2.0f * snoi se (P * noi sefrequency) + 0.75f ; NNeye = normal i ze (Neye) ; / / ( ) f. Ct = t ex1d (Mar bl espl i nesampl er, mar bl e ) ; / / Ci = Ct * (Ka * ambi ent ( ) + Kd * sof t _di f f use (NNeye, Peye ) ) + Ct.w * Ks * specul ar (NNeye, Peye, roughness ) ; ret ur n Ci ;
1. DirectX HLSL 77 VERTEXSHADER asm_mar bl e_vs = decl asm vs.1. 1 dcl_posi t i on v0 dcl_normal v3 m4x4 opos, v0, c [0] m4x4 r0, v0, c [17] mov ot0, r0 m4x4 ot1, v0, c [4] m3x3 ot2.xyz, v3, c [8] ; / /. / / Pshade (0 ) / / / / t echni que t echni que_hl sl_bl uemar bl e pass P0 / / / /. Vert exshaderconst ant [0] = <mat Wor l dvi ewproj >; Vert exshaderconst ant [4] = <mat Wor l dvi ew>; Vert exshaderconst ant [8] = <mat ITWorl dvi ew>; Vert exshaderconst ant [12] = <mat Wor l d>; Vert exshaderconst ant [17] = <mat Tex0>; Vert exshader = <asm_marbl e_vs>; Pi xel Shader = compi l e ps_2_0 hl sl _bl uemarbl e ( ) ; Cul l Mode = CCW;. Effect t echni que_hl sl _bl uemarbl e technique. HLSL., Effect ( ID3DEf f ect : :Set Mat ri x()
). Effect. HLSL. ams_marbl e_vs. Vert exshader = <asm_marbl e_vs>; hl sl _bl uemarbl e () ps_2_0. Pi xel Shader = compi l e ps_2_0 hl sl _bl uemar bl e ( ) ; hl sl_bl uemarl be HLSL. t ex1d(), ambi ent () sof t _di f f use () Effect. ps_2_0., Noi sesampl er Marbl espl i nesampl er. Effect,. Effect. Effect. Effe c t API Effect.. D3DXCreat eef f ect FromFi l e () API Effect, Effect Effect API., Set Mat r i x().
1. DirectX HLSL 79 / /. m_pef f ect ->Set Mat ri x ( "mat Wor l dvi ewproj ", &m_mat Wor l dvi ewproj ) ; m_pef f ect ->Set Mat ri x ( "mat Wor l dvi ew", &m_mat Wor l dvi ew) ; m_pef f ect ->Set Mat ri x ( "mat ITWor l dvi ew", &m_mat ITWor l dvi ew) ; m_pef f ect ->Set Mat ri x ( "mat Wor l d", &m_mat Wor l d) ; m_pef f ect ->Set Mat ri x ( "mat Tex0", &m_obj ect Paramet er s.m_mat Tex0) ; f l oat vect or. m_peffect ->Set Fl oat ( "noi sefrequency ", &m_f Noi sefreq) ; m_pef f ect ->Set Vect or ( "g_leye ", &g_leye ) ;. m_pef f ect ->Set Text ure ( "t Vol umenoi se ", m_pvol umenoi setext ure) ; m_pef f ect ->Set Text ure ( "t Mar bl espl i ne ", m_pmar bl ecol orspl i netext ure) ;, technique ( ). m_pef f ect ->Set Techni que (m_pef f ect ->Get Techni quebyname ( "t echni que_hl sl _ bl uemar bl e ") ) ; m_peffect ->Begi n(&cpasses, 0) ; f or (i Pass = 0; i Pass < cpasses ; i Pass++) m_pef f ect ->Pass (i Pass ) ; / /. m_pef f ect ->End( ) ;., g_leye,. D3DX Effect.
D3DX Effe c t ISV D3DX. HLSL D3DX Effect. D3DX.. HLSL D3DX Effect HLSL. D3DXAssembl eshader*() D3DXCompi l eshader*(). asm, Creat epi xel Shader () Creat evert exshader ().. i f (FAI LED (hr = D3DXCompi l eshader FromFi l e (g_st rvhlfi l e, NULL, NULL, "mai n", "vs_1_1", NULL, &pcode, NULL, &m_vs_const ant Tabl e) ) ) ret ur n hr ; i f (FAI LED (hr = m_pd3ddevi ce ->Creat evert exshader ( (DWORD*)pCode -> Get Buf f er Poi nt er ( ), &m_hlslvert exshader ) ) ) ret ur n hr ; D3DXCompi l eshader *() D3DXAssembl eshader*(). ( mai n vs_1_1 )., #def i nes,,,,,. D3DXCompi l eshader*(). (,, ).
1. DirectX HLSL 81 HLSL, Creat epi xel Shader () Creat evert exshader (). Effect HLSL, D3DXCompi l eshader*(). D3DXCompi l eshader*()... ID3DXConst ant Tabl e. ID3DXConst ant Tabl e ASCII handle. HLSL. D3DXHANDLE handl e ; i f (handl e = m_ps_const ant Tabl e ->Get Const ant ByName (NULL, "r i ngfreq") ) m_ps_const ant Tabl e ->Set Fl oat (m_pd3ddevi ce, handl e, m_f Ri ngfrequency) ; i f (handl e = m_ps_const ant Tabl e ->Get Const ant ByName (NULL, "l i ght Wood") ) m_ps_const ant Tabl e ->Set Vect or (m_pd3ddevi ce, handl e, &l i ght Wood) ;. i f (handl e = m_ps_const ant Tabl e ->Get Const ant ByName (NULL, "Noi sesampl er ") ) m_ps_const ant Tabl e ->Get Const ant Desc (handl e, &const Desc, &count ) ;
i f (const Desc.Regi st erset == D3DXRS_SAMPLER) m_pd3ddevi ce ->Set Text ure (const Desc.Regi st er I ndex, m_pvol umenoi setext ure) ; / / Noi se. m_pd3ddevi ce ->Set Sampl erst at e (const Desc.Regi st eri ndex,, ) ;, D3DX Effect HLSL.,. ID3DXConst ant Tabl e : :Get Desc (). ID3DXConst ant Tabl e : :Get Const ant ByName () ID3DXConst ant Tabl e : :Get Const ant El ement (). D3DX Effect HLSL ID3DXConst ant Tabl e. SDK DX9.0 DirectX 9.0a, SDK. SDK Direct3D, HLSL D3DX. DirectX SDK asm.
1. DirectX HLSL 83 DirectX 9.0 Direct3D High Level Shading Language(HLSL).,.,. HLSL, HLSL. HLSL ATI 3D Application Research Group. Dan Baker Loren McQuade. Mark Wang Wolfgang.