290 likes | 432 Views
Programcao de Shaders em GPU´s. Linguagens Shaders. Linguagens: Cg, HLSL, OpenGL Shader Language - Ambientes de Desenvolvimento. NVídia FX Composer. ATI Rendermonkey. Tipos de Variáveis. int, float, bool, struct, in, out, string vetores: float2, float3, float4 Float4 cor;
E N D
Linguagens Shaders • Linguagens: Cg, HLSL, OpenGL Shader Language- Ambientes de Desenvolvimento NVídia FX Composer ATI Rendermonkey
Tipos de Variáveis • int, float, bool, struct, in, out, string • vetores: float2, float3, float4 • Float4 cor; • Vermelho = cor[0]; • Vermelho = cor.r; • Vermelho = cor.x; • Float2 parte = cor.xy; (swizzling) • Float2 parte = {cor[0], cor[1]};
Matrizes • floatLxC: float3x4, float4x4... • Matrix <float, 4, 4> Matriz2; • Floar4x4 matriz1; • Float b = matriz1.m11; • Float c = matriz1[0][1];
Funções Intrínsecas • Abs (a), acos(x), all(x) [todos os componentes são não zero], 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), degree(x), determinant(m), distance(a,b), dot(a, b), exp(x), exp2(value a), faceforward(n, i, ng), floor(x), fmod(a, b), frac(x), frexp(x, out exp), fwidth(x), isfinite(x), isinf(x), isnan(x), idexp(x, exp), length(v), lerp(a, b, s), lit(n.l, n.h, m), log(x), log10(x), log2(x), max(a, b), min(a, b), modf(x, out ip), mul (a, B), noise(x), normalize(v), pow(x, y), radians(x), reflect(i, n), refract(i, n, R), round(x), rsqrt(x), saturate(x), sign(x), sin(x), sincos(x, out s, out c), sinh(x), smoothstep(min, max, x), value sqrt(value a), step(a, x), tan(x), tanh(x), tex1D(s, t), tex1D(s, t, ddx, ddy), tex1Dbias(s, t), tex1Dgrad(s, t, ddx, ddy), tex1Dlod(s, t), tex1Dproj(s, t), tex2D(s, t), tex2D(s, t, ddx, ddy), tex2Dbias(s, t), tex2Dgrad(s, t, ddx, ddy), tex2Dlod(s, t), tex2Dproj(s, t), tex3D(s, t), tex3D(s, t, ddx, ddy), tex3Dbias(s, t), tex3Dgrad(s, t, ddx, ddy), tex3Dlod(s, t), tex3Dproj(s, t), texCUBE(s, t), texCUBE(s, t, ddx, ddy), texCUBEbias(s, t), texCUBEgrad(s, t, ddx, ddy), texCUBElod(s, t), texCUBEproj(s, t), transpose(m)
Loops e Condicionais • If, While, do, for ...
c. Vertex Programming Operações nesta etapa:- Transformação da posição do vértice- Geração de coordenadas de textura para a posição do vértice- Iluminação sobre o vértice- Operações para determinar o material a ser aplicado ao vértice Efeitos:- Geração de texturas procedurais- Efeitos de iluminação per-vertex- Animação procedural em vértices- Displacement mapping
Struct input Vertex • Struct VertexInput • { • float4 Position : POSITION0; • float3 Normal : NORMAL; • float4 TextureCoor : TEXCOORD0; • } • BINORMAL[n] • BLENDINDICES[n] • BLENDWEIGHT [n] • COLOR[n] • NORMAL[n] • POSITION[n] • PSIZE[n] • TANGENT[n] • TESSFACTOR[n] • TEXCOORD[n]
Struct output Vertex • Struct VertexOutput • { • float4 Position : POSITION0; • float4 TextureCoor : TEXCOORD0; • } • COLOR[n] • FOG • POSITION • PSIZE • TEXCOORD[n]
c. Vertex Programming Struct VertexInput { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; } Struct VertexOutput { floar4 Position : POSITION; float2 TexCoord : TEXCOORD0; }
c. Vertex Programming VertexOutPut AulaVerterxShader (VertexInput input) { VertexOutput output; WorldViewProjection = mul (mul(World, View), Projection); output.Position = mul(input.Position, WorldViewProjection); output.TexCoord = input.TexCoord; return (output); }
d. Pixel Programming Operações nesta etapa:- Computar a cor de um fragmento- Alterar iluminação “per-pixel”- gl_FragCoord
Struct input Pixel • Struct PixelInput • { • float4 Color : COLOR0; • } • COLOR[n] • TEXCOORD[n] • VFACE • VPOS
Struct output Pixel • Struct PixelOutput • { • float4 Position : POSITION0; • float Depth : DEPTH; • } • COLOR[n] • DEPTH[n]
d. Pixel Programming Struct PixelInput { float2 TexCoord : TEXCOORD0; }
d. Pixel Programming Float4 pixelShader (PixelInput input) : COLOR { return (tex2D (TextureSampler, input.TexCoord) * AmbientColor); }
Exemplo no XNA – Arquivo Ambiente.fx float4 AmbientColor : COLOR0; float4x4 WorldViewProjection : WORLDVIEWPROJECTION; texture Texture; sampler TextureSampler = sampler_state { texture = <Texture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR; };
Exemplo no XNA – Arquivo Ambiente.fx struct VertexInput { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; }; struct VertexOutput { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; }; VertexOutput vertexShader (VertexInput input) { VertexOutput output; WorldViewProjection = mul (mul (World, View), Projection); output.Position = mul(input.Position, WorldViewProjection); output.TexCoord = input.TexCoord; return (output); }
Exemplo no XNA – Arquivo Ambiente.fx struct PixelInput { float2 TexCoord : TEXCOORD0; }; float4 pixelShader (PixelInput input) : COLOR { //return (tex2D (TextureSampler, input.TexCoord)* AmbientColor); return (1.0f, 1.0f, 1.0f, 0.0f); } technique Default { pass P0 { VertexShader = compile vs_1_1 vertexShader(); PixelShader = compile ps_1_1 pixelShader (); } }
Exemplo no XNA – Projeto .net amespace Load3DObject { ///<summary> /// This is the main type for your game ///</summary> public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager graphics; private ContentManager content; private Matrix world; private Model model; private FPS fps; private FirstPersonCamera camera; private InputHandler input; private Texture2D greyAsteroid; private Texture2D originalAsteroid; Effect shader;
Exemplo no XNA – Projeto .net public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); input = new InputHandler(this); Components.Add(input); camera = new FirstPersonCamera(this); Components.Add(camera); }
Exemplo no XNA – Projeto .net protected override void Initialize() { base.Initialize(); shader.Parameters["AmbientColor"].SetValue(0.8f); }
Exemplo no XNA – Projeto .net protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { model = content.Load<Model>(@"Content\Models\asteroid1"); greyAsteroid = content.Load<Texture2D>(@"Content\Textures\asteroid1-grey"); originalAsteroid = content.Load<Texture2D>(@"Content\Textures\asteroid1"); shader = content.Load<Effect>(@"Content\effects\ambiente"); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) { content.Unload(); } }
Exemplo no XNA – Projeto .net protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); }
Exemplo no XNA – Projeto .net protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here world = Matrix.CreateRotationY( MathHelper.ToRadians(270.0f * (float)gameTime.TotalGameTime.TotalSeconds)) * Matrix.CreateTranslation(new Vector3(0, 0, -4000)); DrawModel(ref model, ref world, greyAsteroid); world = Matrix.CreateRotationY( MathHelper.ToRadians(45.0f * (float)gameTime.TotalGameTime.TotalSeconds)) * Matrix.CreateRotationZ( MathHelper.ToRadians(45.0f * (float)gameTime.TotalGameTime.TotalSeconds)) * Matrix.CreateTranslation(new Vector3(0, 0, 4000)); DrawModel(ref model, ref world, originalAsteroid); base.Draw(gameTime); }
Exemplo no XNA – Projeto .net private void DrawModel(ref Model m, ref Matrix world, Texture2D texture) { Matrix[] transforms = new Matrix[m.Bones.Count]; m.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh mesh in m.Meshes) { foreach (ModelMeshPart mp in mesh.MeshParts) { if (texture != null) shader.Parameters["Texture"].SetValue(texture); shader.Parameters["Projection"].SetValue(camera.Projection); shader.Parameters["View"].SetValue(camera.View); shader.Parameters["World"].SetValue(world * mesh.ParentBone.Transform); mp.Effect = shader; mesh.Draw(); } } }