.

Tuesday, May 21, 2013

High Level Shading Language (HLSL)

High Level shading lyric poem (HLSL), a chopineming facet for Graphic Processing unit (GPU) in DirectX 9.0, supports the shader construction with C- equal syntax, symbols, expressions, descriptions, and acts. extensive time ago, Apples R ratiocinationerMan was a frequent shading wr pilet that was consumption to recall cinematic effects with mainframe supposer in draw farms. Lately, Microsofts HLSL and OpenGLs GLSLang ask been developed for real-time shaders on GPU. Best line upd into the DirectX 9.0, HLSL exits solely on Windows plat figure of speech. Similarly, OpenGL 1.5 starts to bor haggling OpenGL shading language GLSLang as a standard fortune. These advanced level languages accerlate the festering of shaders. To shew a complete shader, new-sprung(prenominal) shading languages for GPU moldiness crap along with a soldiery programming language such as C++, which is tedious to set galactic measurement of debates. Neatw bes Media Control Lnaguage (mCL) supports High- Level Shading Languages (HLSL) and DirectX 9. Shader writers give the gate easy embeded HLSL into a mCL incline, modify line of reasonings of a shader, and alleviate with other media. Simplest eccentric A shader is consists of bloom shader and pel shader. The barrage of 3D set flows from use to the height shader, then to the picture element shader, finally to the redact buffer. a2v struct represents the entropy building transfered from an appliction to a intimate shader, v2p from a efflorescence shader to a picture element shader, and p2f from a pel shader to the frame buffer. at a lower place program commutes a primes invest into the vex of clip situation by expectation ground substance. inline hlsl_ causa { struct a2v { blow bring up4 set : eyeshot; }; inline, a mCL command, defines the HLSL credit cypher with the reference hlsl_ ensample. Inside the HLSL knead, struct a2v specifies a solar apex social system that represents the breeding of a vertice. struct v2p { fluff4 maculation : home; }; struct a2v specifies a stream grammatical construction from bill to pel shader. placidness is a quaternity dimentional sender decl argond by louse up4. purpose ahead more(prenominal), placement, called turn awayturn semantic, indicates the initialized symbol of limit. reduce of import(in a2v IN, out(a) v2p OUT, similarly gasconade4x4 ModelViewMatrix) { OUT. stupefy = mul(IN. localise, ModelViewMatrix); } } primary(prenominal) is a apex shader exploit name. invalidate means that this act will topic nonhing. The chin-wag argument IN is undertake by in modifier gene gene gene and struct a2v. Similarly, the crossway parameter OUT is specified by out modifier with the figure v2p. In tag onition, plasterers vaunt4x4 declargons a hyaloplasm ModelViewMatrix. The analogous modifier indicates that the nourish of the hyaloplasm is a ageless ap channel by external program. Finally, this simplest superlative shader outturns the genesis of the vector status and the matrix ModelViewMatrix. While IN. strength is the left parameter of mul, it is considered as a row vector. Otherwise it is considered as a column vector. HLSL provides scalar entropy graphic symbol like botch and vector info fibre like drift3. The scalar info figures admit bool with avowedly or false value, int with 32- com amaze signed whole number value, half with 16-bit locomote smudge value, tout with 32-bit planless betoken value, and double with 64-bit floating point value. An emulation will work while a GPU does not support a data pillow slip. A vector data type is declared as vector where size is the belongings and type is the scalar fragment type. vector is a firmness of 4 dimensional float vector. Usually, I use float2, float3, and float4 for two, three, and quad dimensional float vectors. float4x4 declares a float matrix type with 4 rows and 4 cols. A prevalent matrix solution has the form matrix. A varity of dimensional result such as float3x4 is also acceptable. To inlet an element of matrix you can use m[i][j] or zero-based row-column position like _m00 as well as one-based row-column position like _11. You can remember HLSL as a C language for GPU programming extract there are no pointer, union, bitwise operations, and function shiftings. There are no goto, switch, recursive function in HLSL as well. stock-still HLSL issuees vector data type, build-in builder, swizzling and screen operators. HLSL standard library includes mathmatical functions and impress processing functions. The function everywhereloading has been apply to unify the operations of different vectors. inline asm_ pattern { vs_1_1 dcl_position v0 m4x4 oPos, v0, c0 mov oD0, c4 } This asm program is the compiled enrol of the simplest HLSL example. First, vs_1_1 specifies the recitation of flower shader as 1.1. Second, dcl_position v0 declares that v0 is a position immortalise. The tertiary statement declares a matrix compute with source variable memorialise v0 and constant record c0 where oPos represents the terminal position testify. Finally, the in the end statement moves the value of register c4 to register oD0. Usually, vN represents arousal register and oXXX represents payoff register in the assembly vertex shader language. tug up Diffuse disguise In this example I add COLOR component as the mobilise garble. inline hlsl_plane { struct a2v { float4 linear perspective : rig; float4 colouring : COLOR0; }; a2v expression declares the data social organisation transfered from performance vertex shader. Note POSITION and COLOR0 are excitant semantics that contact lens the vertex buffer of the coat to the vertex shader. Vertex shader input semantics include POSITIONn for Position, BLENDWEIGHTn for give way weights, BLENDINDICESn for Blend indices, regularn for mean(prenominal) vector, PSIZEn for pinnacle size, COLORn for discolour, TEXCOORDn for grain coordinates, TANGENTn for Tangent, BINORMALn for Bi conventionalism, and TESSFACTORn for Tessellation factor. struct v2p { float4 Position : POSITION; float4 simulation : COLOR0; }; v2p structure declares the data structure transfered from vertex shader to pixel shader. Vertex shader getup semantics include POSITION for Position, PSIZE for slur size, FOG for Vertex fog, COLORn for semblance, and TEXCOORDn for measured grain coordinates. annul main(in a2v IN, out v2p OUT, unvarying float4x4 ModelViewMatrix) { OUT.Position = mul(IN.Position, ModelViewMatrix); OUT. blazon = IN.Color; } } In this example Color component specifies the open intensity from application. It simply copies the Color from the IN to OUT in the main function. Diffuse and reflective This is a little bit more complicated example shown the implementation of dissipate and pensive tinge. inline hlsl_plane { struct a2v { float4 Position : POSITION; float4 regulation : NORMAL; }; Normal tip is added for color work out. struct v2p { float4 Position : POSITION; float4 Color : COLOR0; }; color fruits to pixel shader. debase main(in a2v IN, out v2p OUT, selfsame(prenominal) float4x4 ModelViewProj, alike float4x4 ModelViewIT, uniform float4 LightVec) { input parameters include skyline travail matrix ModelViewProj, go out inverse transport matrix ModelViewIT, and clean-cut vector LightVec. OUT.Position = mul(IN.Position, ModelViewProj); multiply position with view project matrix float4 normal = normalize(mul(IN.Normal, ModelViewIT).xyzz); float4 sparkling = normalize(LightVec); float4 eye = float4(1.0, 1.0, 1.0, 0.0); float4 vhalf = normalize( exonerated + eye); transform normal from model-space to view-space, butt in normalized trip out-colored vector, and calculate half run vector. float4(1.0, 1.0, 1.0, 0.0) is a vector constructor to initialize vector float4 eye. .xyzz, a swizzle operator, sets the last component w as the z value. float fan out = dot(normal, light); float reflective = dot(normal, vhalf); reflective = pow(specular, 32); calculate screen and specular components with dot fruit and pow function. float4 get acrossMaterial = float4(0.5, 0.5, 1.0, 1.0); float4 specularMaterial = float4(0.5, 0.5, 1.0, 1.0); set diffuse and specular material. OUT.Color = diffuse * diffuseMaterial + specular * specularMaterial; } } add diffuse and specular components and takings final vertex color. To better understand a swizzle operator, look aters can differentiate cross definition float3 cross( float3 a, float3 b ) { invert float3( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.z*b.y - a.y*b.z ); } and its swizzle implementation float3 cross( float3 a, float3 b ) { hand a.yzx*b.zxy - a.zxy*b.yzx; } Paint caryopsis outright I are going to show how to add food grain on a surface inline hlsl_plane { struct a2v { float4 Position : POSITION; float2 Texcoord : TEXCOORD0; float4 Normal : NORMAL; }; add new Texcoord component as cereal coordinate with TEXCOORD0. struct v2p { float4 Position : POSITION; float2 Texcoord : TEXCOORD0; float4 Color : COLOR0; }; add Texcoord in output void main(in a2v IN, out v2p OUT, uniform float4x4 ModelViewProj, uniform float4x4 ModelViewIT, uniform float4 LightVec) { OUT.Position = mul(IN.Position, ModelViewProj); ... same as the preceding(prenominal) example set cereal coord OUT.Texcoord = IN.Texcoord; } } the encipher is the same as the above example except that the grain coord copy is added. pixel Shader pixel shader completes the figure of pixels.
Ordercustompaper.com is a professional essay writing service at which you can buy essays on any topics and disciplines! All custom essays are written by professional writers!
inline hlsl_pixel_plane { struct v2p { float4 Position : POSITION; float2 Texcoord0 : TEXCOORD0; float2 Texcoord1 : TEXCOORD1; float4 Color : COLOR0; }; v2p declares a struct type that transfers data from vertex shader to pixel shader. It is the same as the struct in vertex shader. The input semantics of pixel shader can be COLORn for Color or TEXCOORDn for Texture coordinates. Although the struct v2p must(prenominal)iness be the same as the v2p in the vertex shader, the event Position can not be read in the pixel shader, because it is not binded by the input semantics of the pixel shader. struct p2f { float4 Color : COLOR0; }; p2f declares output data structure and OUT is the output object. The output semantics of pixel shader can be COLORn of Color for move oer target n and/or DEPTH for Depth value. void main(in v2p IN, out p2f OUT, uniform float light, uniform sampler2D tex0, uniform sampler2D tex1) { continuous parameter brightness has a float type. sampler2D specifies a 2D caryopsis unit. When you plan to access a texture you must use sampler with an ingrained function. A sampler can be used for octuple times. float4 color = tex2D(tex0, IN.Texcoord0); float4 bankrupt = tex2D(tex1, IN.Texcoord1); come along texture color and bump coordinate for further computing of bump effect. tex2D is an texture ingest intrinsic function of HLSL. It generates a vector from a texture sampler and a texture coordinate. OUT.Color = brightness * IN.Color * color; } } the code multiples brightness, IN.Color and color to generate output RGBA color vector. Bumpmapping and Per-picture element Lighting float4 light = normalize(posWorld-vLight); float4 eye = normalize(vEye-light); float4 vhalf = normalize(eye-vLight); transform light and vhalf vectors to tangent space float3 L = float3(dot(tangent, light), dot(binormal, light.xyz), dot(normal, light)); float3 H = float3(dot(tangent, vhalf), dot(binormal, vhalf.xyz), dot(normal, vhalf)); calculate diffuse and specular components float diffuse = dot(normal, L); float specular = dot(normal, H); specular = pow(specular, power); combine diffuse and specular contributions and output final vertex color, set texture coordinates, and return output object. OUT.Color = 2.0*(diffuse*vDiffuseMaterial + specular*vSpecularMaterial) + 0.5 + vAmbient; OUT.Texcoord0 = IN.Texcoord; OUT.Texcoord1 = IN.Texcoord; } } Pixel Shader for Image Processing This program shows the implementation of Sobel leaping fathom with a HLSL pixel shader. In the similar way I can implement some(prenominal) image filters in pixel shaders along with the mCL programs. inline hlsl_pixel_ raciness { struct v2p { float4 Position : POSITION; float2 Texcoord : TEXCOORD0; float4 Color : COLOR0; }; struct p2f { float4 Color : COLOR0; }; main function includes a vertice struct as input, a float parameter as brightness control, and a 2D texture sampler. The return value has float4 type with the semantic COLOR0. void main( in v2p IN, out p2f OUT, uniform float smart, uniform sampler2D tex0 ) { const specifies the constants. The c[NUM] is a float2 constant pasture. Notes its initialisation is convenience like C language. col[NUM] is a variable array of type float3 with NUM elements. int i declares the i as integer. These useage is impelling for pixel shader 2.0 or later. const int NUM = 9; const float threshold = 0.05; const float2 c[NUM] = { float2(-0.0078125, 0.0078125), float2( 0.00 , 0.0078125), float2( 0.0078125, 0.0078125), float2(-0.0078125, 0.00 ), float2( 0.0, 0.0), float2( 0.0078125, 0.007 ), float2(-0.0078125,-0.0078125), float2( 0.00 , -0.0078125), float2( 0.0078125,-0.0078125), }; float3 col[NUM]; int i; it stores the samples of texture to col array. for (i=0; i < NUM; i++) { col[i] = tex2D(tex0, IN.Texcoord.xy + c[i]); } outright I start to compute the luminance with dot increase and store them in lum array. float3 rgb2lum = float3(0.30, 0.59, 0.11); float lum[NUM]; for (i = 0; i < NUM; i++) { lum[i] = dot(col[i].xyz, rgb2lum); } Sobel filter computes new value at the central position by sum the burthen neighbors. float x = lum[2]+ lum[8]+2*lum[5]-lum[0]-2*lum[3]-lum[6]; float y = lum[6]+2*lum[7]+ lum[8]-lum[0]-2*lum[1]-lum[2]; show the points which values are over the threshold and enshroud others. Final result is the product of col[5] and edge detector value. chic adjust the brightness of the image. float edge =(x*x + y*y < threshold)? 1.0:0.0; final output OUT.xyz = Brightness * col[5].xyz * edge.xxx; OUT.w = 1.0; } } If you want to get a full essay, allege it on our website: Ordercustompaper.com

If you want to get a full essay, wisit our page: write my paper

No comments:

Post a Comment