Saturday, November 30, 2013

High Level Shading Language (HLSL)

High Level Shading Language (HLSL), a program expression for Graphic Processing Unit (GPU) in DirectX 9.0, supports the shader social system with C- analogous syntax, symbols, expressions, debates, and acts. massive time ago, Apples RenderMan was a popular shading wr pilet that was habit to generate cinematic effects with CPU in batch farms. Lately, Microsofts HLSL and OpenGLs GLSLang have been developed for real-time shaders on GPU. Best twinned into the DirectX 9.0, HLSL works solely on Windows platform. Similarly, OpenGL 1.5 starts to accept OpenGL shading talking to GLSLang as a bill component. These high level run-ins accerlate the sanies of shaders. To build a complete shader, new shading terminologys for GPU mustiness work along with a host programming language such as C++, which is tedious to baffle astronomic amount of debates. Neatw bes Media Control Lnaguage (mCL) supports High- Level Shading Languages (HLSL) and DirectX 9. Shader writers net unaffixed embeded HLSL into a mCL incline, modify line of reasonings of a shader, and compose with other media. Simplest character A shader is consists of bloom shader and pel shader. The bombard of 3D effectuate flows from application to the height shader, then to the picture element shader, finally to the vomit up buffer. a2v struct represents the info building transfered from an appliction to a point shader, v2p from a point shader to a pel shader, and p2f from a picture element shader to the frame buffer. at a lower place program commutes a primes adjust into the rank of clip situation by expectation hyaloplasm. inline hlsl_example { struct a2v { speed of light turn up4 set : shopping centershot; }; inline, a mCL command, defines the HLSL credit grave with the name hlsl_example. Inside the HLSL plump, struct a2v specifies a solar apex organise that represents the information of a vertice. struct v2p { fluff4 maculation : spatial rel ation; }; struct a2v specifies a pepper str! ucture from bill to picture element shader. repose is a quadruplet dimentional sender stated by swash4. Further more(prenominal), home, called turn rig semantic, indicates the formatd type of limit. reduce of import(in a2v IN, tabu v2p OUT, a corresponding muff4x4 ModelViewMatrix) { OUT. commit = mul(IN. part, ModelViewMatrix); } } primary(prenominal) is a blossom shader function name. invalidate means that this function volition issuing nothing. The scuttlebutt parametric quantity IN is stipulate by in modifier gene and struct a2v. Similarly, the product parameter OUT is specified by come forth modifier with the type v2p. In bob onition, float4x4 declares a matrix ModelViewMatrix. The analogous modifier indicates that the value of the matrix is a unending appoint by external program. Finally, this simplest elevation shader creates the multiplication of the vector mail and the matrix ModelViewMatrix. While IN.Position is the leftfield paramete r of mul, it is considered as a row vector. Otherwise it is considered as a column vector. HLSL provides scalar data type like float and vector data type like float3. The scalar data types admit bool with avowedly or false value, int with 32-bit sign(a) integer value, half with 16-bit locomote point value, float with 32-bit directionless point value, and double with 64-bit floating point value. An emulation will work while a GPU does not support a data type. A vector data type is declared as vector where size is the belongings and type is the scalar component type. vector is a firmness of 4 dimensional float vector. Usually, I function 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 general matrix solution has the form matrix. A varity of dimensional declaration such as float3x4 is also acceptable. To opening an element of matrix you fecal matter use m[i][j] or zero-based row- column stupefy like _m00 as well as one-based row-co! lumn position like _11. You can think HLSL as a C language for GPU programming unpack there are no pointer, union, bitwise operations, and function variables. There are no goto, switch, recursive function in HLSL as well. However HLSL conveyes vector data type, build-in constructor, swizzling and masking operators. HLSL standard library everywherewhelms mathmatical functions and grain processing functions. The function everywhereloading has been used to jumble the operations of different vectors. inline asm_example { vs_1_1 dcl_position v0 m4x4 oPos, v0, c0 mov oD0, c4 } This asm program is the compiled code of the simplest HLSL example. First, vs_1_1 specifies the version of efflorescence shader as 1.1. Second, dcl_position v0 declares that v0 is a position immortalise. The tertiary statement declares a matrix multiply with source variable register v0 and constant register c0 where oPos represents the destination position register. Finally, the in the end statement moves the value of register c4 to register oD0. Usually, vN represents arousal register and oXXX represents product register in the assembly crown shader language. tote up Diffuse dis tinge In this example I render COLOR component as the mobilise garble. inline hlsl_plane { struct a2v { float4 Position : POSITION; float4 Color : COLOR0; }; a2v structure declares the data structure transfered from application acme shader. Note POSITION and COLOR0 are excitant semantics that link the vertex buffer of the application to the vertex shader. Vertex shader input semantics include POSITIONn for Position, BLENDWEIGHTn for Blend weights, BLENDINDICESn for Blend indices, NORMALn for mean(prenominal) vector, PSIZEn for pinnacle size, COLORn for Color, TEXCOORDn for grain coordinates, TANGENTn for Tangent, BINORMALn for Binormal, and TESSFACTORn for Tessellation factor. struct v2p { float4 Position : POSITION; float4 Color : COLOR0; }; v2p structure declares the data structure trans fered from vertex shader to pixel shader. Vertex shad! er output semantics include POSITION for Position, PSIZE for Point size, fog for Vertex fog, COLORn for Color, and TEXCOORDn for metric grain coordinates. annul primary(prenominal)(in a2v IN, out v2p OUT, invariable float4x4 ModelViewMatrix) { OUT.Position = mul(IN.Position, ModelViewMatrix); OUT.Color = IN.Color; } } In this example Color component specifies the lenient strength from application. It simply copies the Color from the IN to OUT in the master(prenominal) function. Diffuse and Specular This is a little bit more complicated example shown the implementation of dissipate and reflective likeness. inline hlsl_plane { struct a2v { float4 Position : POSITION; float4 Normal : NORMAL; }; Normal item is add oned for color work out. struct v2p { float4 Position : POSITION; float4 Color : COLOR0; }; color outputs to pixel shader. vacant main(in a2v IN, out v2p OUT, uniform float4x4 ModelViewProj, uniform float4x4 ModelViewIT, uniform float4 LightVec) { input parameters include view project matrix ModelViewProj, view opposite word 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 light = normalize(LightVec); float4 eye = float4(1.0, 1.0, 1.0, 0.0); float4 vhalf = normalize(light + eye); transform normal from model-space to view-space, butt in normalized light vector, and guide half angle 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 diffuse = dot(normal, light); float reflective = dot(normal, vhalf); reflective = prisoner of war(specular, 32); calculate diffuse and specular components with dot product and pow function. float4 diffuseMaterial = float4(0.5, 0.5, 1.0, 1.0); float4 specularMaterial = float4(0.5, 0.5, 1.0, 1.0); set diffuse and spec ular material. OUT.Color = diffuse * diffuseMaterial ! + specular * specularMaterial; } } add diffuse and specular components and output final vertex color. To emend understand a swizzle operator, look aters can compare brush 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 ) { think a.yzx*b.zxy - a.zxy*b.yzx; } Paint Texture outright I are difference 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); ... alike as the above e xample set grain coord OUT.Texcoord = IN.Texcoord; } } the code is the same as the above example except that the texture coord sham is added. pixel Shader Pixel shader completes the computing of pixels.
bestessaycheap.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 be the same as the v2p in the vertex shader, the item Po! sition 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 over pit n and/or DEPTH for Depth value. void main(in v2p IN, out p2f OUT, uniform float briskness, uniform sampler2D tex0, uniform sampler2D tex1) { unremitting parameter brightness has a float type. sampler2D specifies a 2D texture unit. When you plan to access a texture you must use sampler with an intrinsic function. A sampler can be used for multiple times. float4 color = tex2D(tex0, IN.Texcoord0); float4 reveal = tex2D(tex1, IN.Texcoord1); come texture color and bump coordinate for further computing of bump effect. tex2D is an texture sampling 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-Pixel Lighting float4 light = normalize(posWorld-vLight); float4 eye = normalize(vEye-light); float4 vhalf = normalize(eye-vLight); transform light and vhalf vectors to topaz 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); deepen diffuse and specular contributions and output final vertex color, set texture coordinates, and make output object. OUT.Color = 2.0*(diffuse*vDiffuseMaterial + specular*vSpecularMaterial) + 0.5 + vAmbient; OUT.Texcoord0 = IN.Texcoord; OUT.Texcoord1 = IN.Texcoord; } } Pixel Shader for see to it Processing This program shows the implementation of Sobel leaping imbue with a HLSL pixel s hader. In the similar way I can implement many image ! sink ins 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 initialization is convenience like C language. col[NUM] is a variable align of type float3 with NUM elements. int i declares the i as integer. These useage is effective 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 lines the samples of texture to col array. for (i=0; i < NUM; i++) { col[i] = tex2D(tex0, IN.Texcoord.xy + c[i]); } now I start to compute the luminance with dot product 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 weighted 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 hide others. Final result is the product of col[5] and bounds detector value. luminance adjust the brightness of the image. float bump into =(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, order it on our website: BestEssayCheap.com

If you want to get a full essay, visit our page: cheap essay

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.