Pixel shaders

Post on 07-Nov-2014

1.675 views 2 download

Tags:

description

 

Transcript of Pixel shaders

Pixel shaders: programming the GPU

By: Venkata Nanda Kishore

What is a Shader?

Set of Software InstructionsDegree of FlexibilityTraits of an entity

Entities,Traits And Degree

Entities:PixelsVerticesGeometric shapes

Traits:Pixels: z-buffer, alpha values etc.Vertices: Texture co-ordinates, Color

Types of Shaders

Initially, Shaders are just pixel shaders

Types of Shaders:Pixel ShadersVertex ShadersGeometric Shaders

But the name remained as it was!

GRAPHICS PIPELINE

VertexShaders RasteriserGeometric

Shaders Pixel Shaders

Effect of Shaders

1. Replaced hard coded effects2. Gives a programmable alternativeFixed Function PipelineProgrammable Function Pipeline3.Parallel Programming

Programming Shaders

• HLSL (Direct 3d)• GLSL (OpenGl)• Cg(Nvidia + Microsoft)

Cg

• What is Cg?• Is it similar to C?• Is it the right question?• Can we use it as a general programming

language?

Background of Cg

• Use of Assembly level language• What is assembly level?• What is high level,low level and middle level?• How easy is it?

Example of assembly level languagefib: mov edx, [esp+8] cmp edx, 0 ja @f mov eax, 0 ret @@: cmp edx, 2 ja @f mov eax, 1 ret @@: push ebx mov ebx, 1 mov ecx, 1 @@: lea eax, [ebx+ecx] cmp edx, 3 jbe @f mov ebx, ecx mov ecx, eax dec edx jmp @b @@: pop ebx ret

Advantage of Cg

• Portable• Easy• Optimize the code

Games that Use Cg

HIT MAN : Blood MoneyBATTLE FIELD 2FAR CRYRACERAnd many more

Syntax and Semantics(1)

Data Types:int, float, half, fixed, bool, sampler*Operators: Arithmetic and logical operators as in CAdditional: arithmetic operations for vector and matrix operrations

Syntax and Semantics(2)

Functions and Control Structures: Similar to C

Standard Cg Library: Specialised GPU programming tasksEg. Texture Mapping functions tex1D and tex2D

Cg Runtime Library:Can be used with OpenGl or DirectX

Example Shaderstruct VertIn //Input vertex { float4 pos : POSITION; float4 color : COLOR0; }; // output vertex struct VertOut { float4 pos : POSITION; float4 color : COLOR0; }; // vertex shader main entry VertOut main(VertIn IN, uniform float4x4 modelViewProj) { VertOut OUT; OUT.pos = mul(modelViewProj, IN.pos); //positionOUT.color = IN.color; // copy input color to output OUT.color.z = 1.0f; // blue component of color = 1.0f return OUT; }

Loading Pixel Shaders in OpenGL

1.Loading extensions:glEnable(GL_FRAGMENT_PROGRAM_ARB);

2.Create Shader Number:glGenProgramsARB(1, &shader_num); glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader_num);

Actually Loading the Shader

• glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(program_string), program_string);

• glDisable(GL_FRAGMENT_PROGRAM_ARB);

• http://joshbeam.com/articles/cg_pixel_shaders_in_opengl/

Examples

https://support.steampowered.com/kb_article.php?ref=4360-TPJL-2065Gives what games need what version of pixel shadershttp://www.nvidia.com/object/feature_pixelshader.htmlhttp://www.toymaker.info/Games/html/pixel_shaders.html